oni / cf / t / t_cf.ml

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
(*---------------------------------------------------------------------------*
  $Change$
  Copyright (c) 2003-2010, James H. Woodyatt
  All rights reserved.
  
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  
    Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
    
    Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the
    distribution
  
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  OF THE POSSIBILITY OF SUCH DAMAGE. 
 *---------------------------------------------------------------------------*)

Random.self_init ();;

let jout = Cf_journal.stdout
let _ = jout#setlimit `Notice

(*
Gc.set {
    (Gc.get ()) with
    (* Gc.verbose = 0x3ff; *)
    Gc.verbose = 0x14;
};;

Gc.create_alarm begin fun () ->
    let min, pro, maj = Gc.counters () in
    Printf.printf "[Gc] minor=%f promoted=%f major=%f\n" min pro maj;
    flush stdout
end
*)

module T1 = struct
    module R = Cf_rbtree.Map(Cf_ordered.Int_order)
    open R
    
    let decreasing = to_seq_decr
    
    let rec printlist s nl =
        assert begin
            let b = Buffer.create 40 in
            Printf.bprintf b "%s: [" s;
            List.iter (fun n -> Printf.bprintf b " %d;" n) nl;
            Printf.bprintf b " ]\n";
            jout#debug "%s" (Buffer.contents b)
        end
    
    let to_list tree =
        Cf_seq.fold (fun v (e, _) -> e :: v) [] (decreasing tree)
        
    let testcontent tree lst =
        printlist "testcontent" lst;
        let c = to_list tree in
        if c <> lst then begin
            printlist "found" c;
            failwith "testcontent"
        end
        
    let testfind tree key =
        let msg = Printf.sprintf "testfind %d" key in
        printlist msg (to_list tree);
        try
            let e = search key tree in
            assert (member key tree);
            if (e / 2) <> key then failwith "testfind key <> e / 2"
        with
        | Not_found ->
            assert (not (member key tree));
            failwith "testfind -> Not_found"
    
    let testextract tree key =
        let msg = Printf.sprintf "testextract %d" key in
        printlist msg (to_list tree);
        try
            let e, nt = extract key tree in
            if (e / 2) <> key then failwith "testextract -> e <> key";
            nt
        with
        | Not_found ->
            failwith "testextract -> Not_found"
    
    let test0 () =
        let t1, old = insert (1, 2) nil in
        if old <> None then failwith "t1 replaced a nil";
        testcontent t1 [ 1 ];
        testfind t1 1;
        let d1 = testextract t1 1 in
        testcontent d1 [];
        let t2, old = insert (2, 4) t1 in
        if old <> None then failwith "t2 replaced a nil";
        testcontent t2 [ 1; 2 ];
        testfind t2 1;
        testfind t2 2;
        let d1 = testextract t2 1 in
        testcontent d1 [ 2 ];
        let d2 = testextract t2 2 in
        testcontent d2 [ 1 ];
        let d12 = testextract d1 2 in
        testcontent d12 [];
        let d21 = testextract d2 1 in
        testcontent d21 [];
        let t3, old = insert (3, 6) t2 in
        if old <> None then failwith "t3 replaced a nil";
        testfind t3 1;
        testfind t3 2;
        testfind t3 3;
        let d1 = testextract t3 1 in
        testcontent d1 [ 2; 3 ];
        let d2 = testextract t3 2 in
        testcontent d2 [ 1; 3 ];
        let d3 = testextract t3 3 in
        testcontent d3 [ 1; 2 ];
        testfind d1 3;
        testfind d1 2;
        testfind d2 1;
        testfind d2 3;
        testfind d3 1;
        testfind d3 2;
        let d12 = testextract d1 2 in
        testcontent d12 [ 3 ];
        let d23 = testextract d2 3 in
        testcontent d23 [ 1 ];
        let d31 = testextract d3 1 in
        testcontent d31 [ 2 ];
        testfind d12 3;
        testfind d23 1;
        testfind d31 2;
        let d123 = testextract d12 3 in
        let d231 = testextract d23 1 in
        let d312 = testextract d31 2 in
        testcontent d123 [];
        testcontent d231 [];
        testcontent d312 [];
        ()
    
    let test () = test0 ()
end

module T2 = struct        
    module S1 = Set.Make(Cf_ordered.Int_order)
    module S2 = Cf_rbtree.Set(Cf_ordered.Int_order)
    
    let bound = 64
    let iterations = 512
    
    (*
    let print_list =
        let rec loop = function
            | [] -> ()
            | hd :: [] -> Printf.printf "%d" hd
            | hd :: tl -> Printf.printf "%d; " hd; loop tl
        in
        fun id r ->
            Printf.printf "%s [ " id;
            loop r;
            print_string " ]\n";
            flush stdout
    *)
    
    let rec loop n s1 s2 =
        let x = Random.int bound in
        let msg, s1, s2 =
            if S1.mem x s1 then
                "remove failure", S1.remove x s1, S2.clear x s2
            else
                "add failure", S1.add x s1, S2.put x s2
        in
        let n = pred n in
        let e1 = S1.elements s1 and e2 = S2.to_list_incr s2 in
        (*
        print_list "e1" e1;
        print_list "e2" e2;
        *)
        if e1 <> e2 then failwith msg;
        if n > 0 then loop n s1 s2
    
    let test () =
        loop iterations S1.empty S2.nil
end

module T3 = struct
    module M = Cf_rbtree.Map(Cf_ordered.Int_order)
    
    let test1 () =
        let m = M.replace (0, "zero") M.nil in
        let s = M.search 0 m in
        assert (s = "zero");
        assert (M.member 0 m)
    
    let nearest_succ key m =
        match Lazy.force (M.nearest_incr key m) with
        | Cf_seq.Z ->
            raise Not_found
        | Cf_seq.P (hd, _) ->
            hd
    
    let nearest_pred key m =
        match Lazy.force (M.nearest_decr key m) with
        | Cf_seq.Z ->
            raise Not_found
        | Cf_seq.P (hd, _) ->
            hd
    
    let test2 () =
        let m = [
            1, "one";
            3, "three";
            5, "five";
            7, "seven";
            9, "nine";
            11, "eleven";
            13, "thirteen";
            15, "fifteen";
            17, "seventeen";
            19, "nineteen";
        ] in
        let m = M.of_list_incr m in
        if nearest_succ 0 m <> (1, "one") then
            failwith "nearest_succ 0";
        if nearest_succ 1 m <> (1, "one") then
            failwith "nearest_succ 01";
        if nearest_succ 2 m <> (3, "three") then
            failwith "nearest_succ 2";
        if nearest_pred 20 m <> (19, "nineteen") then
            failwith "nearest_pred 20";
        if nearest_pred 19 m <> (19, "nineteen") then
            failwith "nearest_pred 19";
        if nearest_pred 18 m <> (17, "seventeen") then
            failwith "nearest_pred 18";
        ()
    
    let test () =
        test1 ();
        test2 ();
end

module T4 = struct
    (* open Cf_vwdeque *)
    (* open Cf_kotdeque *)
    open Cf_deque
    
    let aux msg i _ (* q *) =
        assert begin
            (* jout#debug "%s i=%d q=%s\n" msg i (sprint string_of_int q) *)
            (**) jout#debug "%s i=%d\n" msg i (**)
        end
        
    let modaux msg modulo i q =
        if ((i / modulo) * modulo) = i then aux msg i q
    
    let test1 pop push =
        let n = 1024 in
        let rec loop1 i q =
            aux "+" i q;
            if i < n then loop1 (succ i) (push i q) else loop2 0 q
        and loop2 i q =
            (* aux "-" i q; *)
            if i < n then begin
                match pop q with
                | None ->
                    failwith "[1] pop: None"
                | Some (j, q) ->
                    if i <> j then
                        jout#fail "[1] i <> j j=%d" j;
                    loop2 (succ i) q
            end
            else
                match pop q with
                | None -> ()
                | Some _ -> failwith "[1] pop: Some _"
        in
        loop1 0 nil
    
    let test2 pop push ncat =
        let size = 10000 (* and m = 1 *) in
        let chunk = 100 in
        let rec cons i q = function
            | j when j > 0 ->
                (* modaux "+" m i q; *)
                cons (succ i) (push i q) (pred j)
            | _ ->
                i, q
        in
        let rec load i j z =
            let i, j, q0, q1 =
                if z > chunk then begin
                    let z = z / chunk in
                    let z = if z < chunk then chunk else z in
                    let i, j, q0 = load i j z in
                    let i, j, q1 = load i j z in
                    i, j, q0, q1
                end
                else begin
                    assert (z = chunk);
                    let x = Random.int z in
                    let x = if x > j then j else x in
                    let j = j - x in
                    let i, q0 = cons i nil x in
                    let y = Random.int z in
                    let y = if y > j then j else y in
                    let j = j - y in
                    let i, q1 = cons i nil y in
                    i, j, q0, q1 
                end
            in
            let q' = ncat q0 q1 in
            (* aux "%" i q'; *)
            if j > 0 && z > chunk then begin
                let i, j, q = load i j z in
                let q' = ncat q' q in
                (* aux "%" i q'; *)
                i, j, q'
            end
            else
                i, j, q'
        in
        let rec unload i q =
            (* modaux "-" m i q; *)
            if i < size then begin
                match pop q with
                | None ->
                    failwith "[2] pop: None"
                | Some (j, q) ->
                    if i <> j then
                        failwith (Printf.sprintf "[2] i <> j i=%d j=%d" i j);
                    unload (succ i) q
            end
            else
                match pop q with
                | None -> ()
                | Some _ -> failwith "[2] pop: Some _"
        in
        let i, j, q = load 0 size size in
        if i <> size then failwith "[2] load: i <> size";
        if j <> 0 then failwith "[2] load: j <> 0";
        unload 0 q
    
    let test () =
        test1 A.pop B.push;
        test1 B.pop A.push;
        test2 A.pop B.push catenate;
        test2 B.pop A.push (fun q2 q1 -> catenate q1 q2);
        ()
end

module T5 = struct
    open Cf_seq
        
    let (!$) s = Cf_seq.head s, Cf_seq.tail s
    
    let test () =
        let s = of_substring "abc" 0 in
        let ch, s = !$ s in
        if ch <> 'a' then failwith "next 0";
        let _ = !$ s in
        let ch, s = !$ s in
        if ch <> 'b' then failwith "next 1";
        let _ = !$ s in
        let _ = !$ s in
        let ch, s = !$ s in
        if ch <> 'c' then failwith "next 2";
        try ignore (!$ s); failwith "terminal" with Cf_seq.Empty -> ()
end

module T6 = struct
    open Printf
    open Cf_clex.Op
    open Cf_llscan.Op
        
    module L1 = struct        
        let test1 lexer =
            let token0 = "abaabaababbabb" in
            let input = token0 (* ^ "jhw" *) in
            let s = Cf_seq.of_substring input 0 in
            let token, s' =
                match lexer s with
                | Some x -> x
                | None -> failwith "No match! [L1.1]"
            in
            if Lazy.force s' <> Cf_seq.Z then
                failwith "Unexpected trailer [1]!";
            if token <> token0 then
                failwith (sprintf "Bad match! [msg='%s']" token)
        
        let test2 lexer =
            let token0 = "abaabaababbabb" in
            let input = token0 ^ "jhw" in
            let s = Cf_seq.of_substring input 0 in
            let token, s' =
                match lexer s with
                | Some x -> x
                | None -> failwith "No match! [L1.2]"
            in
            if Lazy.force s' = Cf_seq.Z then
                failwith "Expected trailer [2]!";
            if token <> token0 then
                failwith (sprintf "Bad match! [msg='%s']" token)
    end
    
    module L2 = struct
        let p =
            let q0 = Cf_fmt_llscan.scanf "%3u" (fun y -> y) in
            let q1 = Cf_fmt_llscan.scanf "%c" (fun y -> y) in
            let q2 = Cf_fmt_llscan.scanf "%3u" (fun y -> y) in
            q0 >>= fun v0 ->
            q1 >>= fun v1 ->
            q2 >>= fun v2 ->
            Cf_llscan.ret (v0, v1, v2)
                
        let y s = p (Cf_seq.of_string s)
        
        let test () =
            match y "1234567" with
            | Some ((123, '4', 567), z) when Lazy.force z = Cf_seq.Z ->
                ()
            | Some ((123, '4', 567), z) ->
                failwith (Cf_seq.to_string z)
            | Some ((v0, v1, v2), _) ->
                failwith (Printf.sprintf "%02u, '%c', %02u" v0 v1 v2)
            | _ ->
                failwith "No match! [L2]"
    end
    
    module L3 = struct
        let qlist_ = [
            "abc", "abc";
            "(a*b+c?.[`t +?]|d)", "`(a`*b`+c`?`.`[``t `+`?`]`|d`)";
        ]
        
        let loop (a, b) =
            let s = Cf_dyn.stringcommute Cf_regx.unquote b in
            if s <> a then
                failwith (Printf.sprintf "unquoting \"%s\" <> \"%s\"" s a);
            let s = Cf_dyn.stringcommute Cf_regx.quote a in
            if s <> b then
                failwith (Printf.sprintf "quoting \"%s\" <> \"%s\"" s b);
            ()
        
        let test () = List.iter loop qlist_
    end
    
    module L4 = struct
        let lex =
            Cf_clex.create !@[
                !$"`s+" $= None;
                !$"`w+" $> (fun x -> Some x);
            ]
        
        let rec loop acc z =
            match lex z with
            | None ->
                Some (List.rev acc, z)
            | Some (None, z) ->
                loop acc z
            | Some (Some tok, z) ->
                loop (tok :: acc) z
        
        let p =
            loop [] >>= fun arr ->
            Cf_llscan.fin >>= fun () ->
            Cf_llscan.ret arr
        
        let input_ = "bim bam boom"
        
        let test () =
            let s = Cf_seq.of_string input_ in
            match p s with
            | Some ([ "bim"; "bam"; "boom" ], s)
              when Lazy.force s = Cf_seq.Z ->
                ()
            | _ ->
                failwith "parse error!"
    end
    
    module L5 = struct
        let xlist = [
            "a", [ "a" ], [ "x"; "ax"; "" ];
            
            "abc", [ "abc" ], [ "xxxx" ];
            
            "[abc]+", [ "abccabc" ], [ "abcdabcd" ];
            
            "ab+c*d?e", [ "abe"; "abbcde" ], [ "ae"; "acde"; "xxxx" ];
            
            "a(bc)+d", [ "abcd"; "abcbcd" ], [ "abc"; "abcabc"; "abcdd" ];
            
            "(a|b)*abb", [ "abababb"; "ababbabb" ], [ "ababab"; "cde" ];
            
            "[_`a][_`i]*", [ "id0"; "_"; "long_id" ], [ "0"; "" ];
            
            "[ `t]*`w+", [ "foo"; "\t  foo" ], [ "\n"; " "; "" ];
        ]
        
        let yes_ s x v =
            if not (Cf_regx.test x v) then
                jout#fail "Expected '%s' to match expression '%s'." v s
        
        let no_ s x v =
            if Cf_regx.test x v then
                jout#fail "Expected '%s' to match expression '%s'." v s
        
        let test1 (s, yes, no) =
            let x = Cf_regx.of_string s in
            List.iter (yes_ s x) yes;
            List.iter (no_ s x) no
        
        let test () =
            List.iter test1 xlist
    end
    
    let test () =
        let lexer =
            Cf_clex.create begin
                (!*(!:'a' $| !:'b')) $& !$"abb" $> (fun x -> x)
            end
        in
        L1.test1 lexer;
        L1.test2 lexer;
        L2.test ();
        L3.test ();
        L4.test ();
        L5.test ()
end

module T7 = struct
    open Cf_llscan.Op
    
    let digit =
        let zero = int_of_char '0' in
        let sat = Cf_llscan.sat (function '0'..'9' -> true | _ -> false) in
        sat >>= fun ch -> Cf_llscan.ret ((int_of_char ch) - zero)
        
    let unumber =
        let rec to_int x = function
            | [] -> x
            | hd :: tl -> to_int (hd + (x * 10)) tl
        in
        ?+digit >>= fun (d0, dn) -> Cf_llscan.ret (to_int 0 (d0 :: dn))
    
    let plus = ?. '+'
    let minus = ?. '-'
    let mult = ?. '*'
    let div = ?. '/'
    let lparen = ?. '('
    
    let rparen = Cf_llscan.alt [
        ?. ')';
        Cf_llscan.err ~f:(fun _ -> failwith "Expected ')'") ();
    ]
    
    let number = Cf_llscan.alt [
        unumber;
        
        begin
            plus >>= fun _ ->
            unumber >>= fun n ->
            Cf_llscan.ret n
        end;
        
        begin
            minus >>= fun _ ->
            unumber >>= fun n ->
            Cf_llscan.ret (-n)
        end;
    ]
    
    type fval_t = Mul of int | Div of int
    
    let rec expr s = Cf_llscan.alt [
        begin
            term >>= fun hd ->
            ?*expr_c >>= fun tl ->
            Cf_llscan.ret (List.fold_left (+) hd tl)
        end;
        
        term;
    ] s
    
    and expr_c s = Cf_llscan.alt [
        begin
            plus >>= fun _ ->
            term >>= fun n ->
            Cf_llscan.ret n
        end;
        
        begin
            minus >>= fun _ ->
            term >>= fun n ->
            Cf_llscan.ret (-n)
        end;
    ] s
    
    and term =
        let f a = function Mul x -> a * x | Div x -> a / x in
        fun s -> Cf_llscan.alt [
            begin
                factor >>= fun hd ->
                ?*term_c >>= fun tl ->
                Cf_llscan.ret (List.fold_left f hd tl)
            end;
            
            term;
        ] s
    
    and term_c s = Cf_llscan.alt [
        begin
            mult >>= fun _ ->
            factor >>= fun n ->
            Cf_llscan.ret (Mul n)
        end;
        
        begin
            div >>= fun _ ->
            factor >>= fun n ->
            if n = 0 then failwith "Div 0";
            Cf_llscan.ret (Div n)
        end;
    ] s
    
    and factor s = Cf_llscan.alt [
        begin
            lparen >>= fun _ ->
            expr >>= fun e ->
            rparen >>= fun _ ->
            Cf_llscan.ret e
        end;
        
        number
    ] s
    
    let run s =
        begin
            expr >>= fun e ->
            Cf_llscan.fin >>= fun () ->
            Cf_llscan.ret e
        end s
    
    let calc s =
        match run (Cf_seq.of_string s) with
        | Some (o, tl) when Lazy.force tl = Cf_seq.Z -> o
        | Some _ -> failwith "Unparsed trailer"
        | None -> failwith "Parse error"
    
    let positive = [
        "123",  123;
        "-256", -256;
        "2+3", 5;
        "8/(4-2)+1", 5;
    ]
    
    let t_pos (s, x) =
        let x' = calc s in
        if x' <> x then begin
            let msg = Printf.sprintf "'%s' <> %d (%d)" s x x' in
            failwith msg
        end
    
    let test () =
        List.iter t_pos positive
end

module T8 = struct
    module L1 = struct
        module C = Cf_cmonad
        open C.Op
        
        module type M = sig
            val get_int: (unit, int) C.t
            val get_line: (unit, string) C.t
            val get_float: (unit, float) C.t
            
            val put_int: int -> (unit, unit) C.t
            val put_string: string -> (unit, unit) C.t
            val put_float: float -> (unit, unit) C.t
        end
        
        module M: M = struct
            let get_int f = f (read_int ())
            let get_line f = f (read_line ())
            let get_float f = f (read_float ())
            
            let put_int n f = f (print_int n)
            let put_string s f = f (print_string s)
            let put_float x f = f (print_float x)
        end
        
        let get_name =
            M.put_string "What is your name? " >>= fun _ ->
            M.get_line
        
        let get_color =
            M.put_string "What is your favorite color? " >>= fun _ ->
            M.get_line
        
        let get_foo =
            M.put_string "What is a continuation monad? " >>= fun _ ->
            M.get_line
        
        let query =
            get_name >>= fun name ->
            get_color >>= fun color ->
            get_foo >>= fun foo ->
            M.put_string (Printf.sprintf "%s, %s, %s\n" name color foo)
        
        let test () =
            print_string ">>> L1\n";
            C.eval query ();
            print_string "<<< L1\n";
    end
    
    module L2 = struct
        module C = Cf_cmonad
        module SC = Cf_scmonad
        open SC.Op
        
        module type M = sig
            val get_int: (int, unit, int) SC.t
            val get_line: (int, unit, string) SC.t
            val get_float: (int, unit, float) SC.t
            
            val put_int: int -> (int, unit, unit) SC.t
            val put_string: string -> (int, unit, unit) SC.t
            val put_float: float -> (int, unit, unit) SC.t
        end
        
        module M: M = struct
            let get_int f y = f (read_int ()) y
            let get_line f y = f (read_line ()) y
            let get_float f y = f (read_float ()) y
            
            let put_int n f y = f (print_int n) y
            let put_string s f y = f (print_string s) y
            let put_float x f y = f (print_float x) y
        end
                
        let get_name =
            SC.load >>= fun n ->
            let msg = Printf.sprintf "[%d] What is your name? " n in
            M.put_string msg >>= fun _ ->
            SC.store (succ n) >>= fun _ ->
            M.get_line
        
        let get_color =
            SC.load >>= fun n ->
            let msg = Printf.sprintf "[%d] What is your favorite color? " n in
            M.put_string msg >>= fun _ ->
            SC.store (succ n) >>= fun _ ->
            M.get_line
        
        let get_foo =
            SC.load >>= fun n ->
            let msg = Printf.sprintf
                "[%d] What is a state continuation monad? " n
            in
            M.put_string msg >>= fun _ ->
            SC.store (succ n) >>= fun _ ->
            M.get_line
        
        let query =
            get_name >>= fun name ->
            get_color >>= fun color ->
            get_foo >>= fun foo ->
            M.put_string (Printf.sprintf "%s, %s, %s\n" name color foo)
        
        let test () =
            print_string ">>> L2\n";
            let c =
                C.Op.( >>= ) (SC.down query 0) begin fun s ->
                C.return (Printf.printf "state=%d\n" s)
                end
            in
            C.eval c ();
            print_string "<<< L2\n";
    end
    
    let test () =
        (*
        L1.test ();
        L2.test ();
        *)
        assert true
end

module T9_old = struct
    (*
    let rec logflow w =
        match Lazy.force w with
        | Cf_flow.Z ->
            print_string "| Z\n";
            flush stdout;
            Cf_flow.Z
        | Cf_flow.P (hd, tl) ->
            (* if (int_of_float hd) mod 1000 = 0 then begin *)
                Printf.printf "| P %f\n" hd;
                flush stdout;
            (* end; *)
            Cf_flow.P (hd, lazy (logflow tl))
        | Cf_flow.Q f ->
            Cf_flow.Q begin fun i ->
                Printf.printf "| Q %d\n" i;
                flush stdout;
                logflow (lazy (f i))
            end
    
    let rec circuit0 =
        Cf_flow.Q begin fun n ->
            let hd = (float_of_int n) /. 2.0 in
            let tl = Lazy.lazy_from_val circuit0 in
            Cf_flow.P (hd, tl)
        end
    *)
    
    open Cf_cmonad.Op
    open Cf_state_gadget
            
    let rec divider i o =
        let i = (i :> ('a, int, float) rx) in
        let o = (o :> ('b, int, float) tx) in
        let rec loop () =
            guard begin
                i#get begin fun (`I n) ->
                    load >>= fun state ->
                    store (succ state) >>= fun () ->
                    let oval = (float_of_int n) /. 2.0 in
                    o#put (`O oval) >>= fun () ->
                    loop ()
                end
            end
        in
        loop ()
    
    let ingest isrc =
        let isrc = (isrc :> ('a, int, float) tx) in
        let rec loop () =
            read >>= fun i ->
            isrc#put (`I i) >>= loop
        in
        loop ()
    
    let render osnk =
        let osnk = (osnk :> ('a, int, float) rx) in
        let rec loop () =
            guard begin
                osnk#get begin fun (`O n) ->
                    write n >>= loop
                end
            end
        in
        loop ()
    
    let gadget x =
        simplex >>= fun (efRx, efTx) ->
        simplex >>= fun (enRx, enTx) ->
        start (ingest enTx) () >>= fun () ->
        start (render efRx) () >>= fun () ->
        start (divider enRx efTx) x
    
    let input =
        let rec loop n = Cf_seq.P (n, lazy (loop (succ n))) in
        fun () -> lazy (loop 1)
    
    let output =
        let rec loop n =
            let x = (float_of_int n) /. 2.0 in
            Cf_seq.P (x, lazy (loop (succ n)))
        in
        fun () -> lazy (loop 1)
    
    open Cf_flow.Op
    
    let test () =
        let n = 100 in
        let i = Cf_seq.limit n (input ()) in
        let o = Cf_seq.limit n (output ()) in
        let divflow = eval (gadget 0) () in
        (* let divflow = lazy (logflow divflow) in *)
        let x = Cf_flow.to_seq (Cf_flow.of_seq i -=- divflow) in
        if not (Cf_seq.equal x o) then failwith "Transform failed!"
end

module T9 = struct
    (*
    let rec logflow w =
        match Lazy.force w with
        | Cf_flow.Z ->
            print_string "| Z\n";
            flush stdout;
            Cf_flow.Z
        | Cf_flow.P (hd, tl) ->
            (* if (int_of_float hd) mod 1000 = 0 then begin *)
                Printf.printf "| P %f\n" hd;
                flush stdout;
            (* end; *)
            Cf_flow.P (hd, lazy (logflow tl))
        | Cf_flow.Q f ->
            Cf_flow.Q begin fun i ->
                Printf.printf "| Q %d\n" i;
                flush stdout;
                logflow (lazy (f i))
            end
    
    let rec circuit0 =
        Cf_flow.Q begin fun n ->
            let hd = (float_of_int n) /. 2.0 in
            let tl = Lazy.lazy_from_val circuit0 in
            Cf_flow.P (hd, tl)
        end
    *)
    
    open Cf_cmonad.Op
    open Cf_gadget
        
    class divider i o =
        let i = (i :> ('a, int, float) rx) in
        let o = (o :> ('b, int, float) tx) in
        object(self)
            inherit [int, float] start
            inherit [int, float] next
            
            val count = 0
            
            method private input (`I n) =
                o#put (`O ((float_of_int n) /. 2.0)) >>= fun () ->
                {< count = succ count >}#next
            
            method private guard = i#get self#input
        end
    
    let ingest isrc =
        let isrc = (isrc :> ('a, int, float) tx) in
        let rec loop () =
            read >>= fun i ->
            isrc#put (`I i) >>= loop
        in
        loop ()
    
    let render osnk =
        let osnk = (osnk :> ('a, int, float) rx) in
        let rec loop () =
            guard begin
                osnk#get begin fun (`O n) ->
                    write n >>= loop
                end
            end
        in
        loop ()
    
    let gadget () =
        simplex >>= fun (efRx, efTx) ->
        simplex >>= fun (enRx, enTx) ->
        start (ingest enTx) >>= fun () ->
        start (render efRx) >>= fun () ->
        let m = new divider enRx efTx in
        m#start
    
    let input =
        let rec loop n = Cf_seq.P (n, lazy (loop (succ n))) in
        fun () -> lazy (loop 1)
    
    let output =
        let rec loop n =
            let x = (float_of_int n) /. 2.0 in
            Cf_seq.P (x, lazy (loop (succ n)))
        in
        fun () -> lazy (loop 1)
    
    open Cf_flow.Op
    
    let test () =
        let n = 100 in
        let i = Cf_seq.limit n (input ()) in
        let o = Cf_seq.limit n (output ()) in
        let divflow = eval (gadget ()) in
        (* let divflow = lazy (logflow divflow) in *)
        let x = Cf_flow.to_seq (Cf_flow.of_seq i -=- divflow) in
        if not (Cf_seq.equal x o) then failwith "Transform failed!"
end

module T10 = struct
(*
3fffe33e1b840309 365  Sun -1000000-12-31 23:59:59 +0000
3fffe33e1b84030a 000  Mon -999999-01-01 00:00:00 +0000
3ffffff1868b8409 364  Fri -1-12-31 23:59:59 +0000
3ffffff1868b840a 000  Sat 0-01-01 00:00:00 +0000
3ffffff1886e1757 000  Mon 1-01-01 01:01:01 +0000
3ffffff8df7db65f 000  Wed 1000-01-01 12:04:37 +0000
3ffffffec03dbf89 364  Tue 1799-12-31 23:59:59 +0000
3fffffff7c558189 364  Sun 1899-12-31 23:59:59 +0000
3fffffff7c55818a 000  Mon 1900-01-01 00:00:00 +0000
3fffffffffffffff 364  Wed 1969-12-31 23:59:49 +0000
4000000000000000 364  Wed 1969-12-31 23:59:50 +0000
4000000000000009 364  Wed 1969-12-31 23:59:59 +0000
400000000000000a 000  Thu 1970-01-01 00:00:00 +0000
400000000000000b 000  Thu 1970-01-01 00:00:01 +0000
4000000004b25808 181  Fri 1972-06-30 23:59:58 +0000
4000000004b25809 181  Fri 1972-06-30 23:59:59 +0000
4000000004b2580a 181  Fri 1972-06-30 23:59:60 +0000
4000000004b2580b 182  Sat 1972-07-01 00:00:00 +0000
4000000004b2580c 182  Sat 1972-07-01 00:00:01 +0000
4000000030e7241b 364  Sun 1995-12-31 23:59:58 +0000
4000000030e7241c 364  Sun 1995-12-31 23:59:59 +0000
4000000030e7241d 364  Sun 1995-12-31 23:59:60 +0000
4000000030e7241e 000  Mon 1996-01-01 00:00:00 +0000
4000000030e7241d 364  Sun 1995-12-31 23:59:60 +0000
4000000030e7241e 000  Mon 1996-01-01 00:00:00 +0000
4000000030e7241f 000  Mon 1996-01-01 00:00:01 +0000
4000000030e72420 000  Mon 1996-01-01 00:00:02 +0000
4000000033b8489d 180  Mon 1997-06-30 23:59:59 +0000
4000000033b8489e 180  Mon 1997-06-30 23:59:60 +0000
4000000033b8489f 181  Tue 1997-07-01 00:00:00 +0000
4000000033df0226 210  Wed 1997-07-30 08:57:43 +0000
4000000034353637 275  Fri 1997-10-03 18:14:48 +0000
4000000037d77955 251  Thu 1999-09-09 09:09:09 +0000
40000000386d439f 364  Fri 1999-12-31 23:59:59 +0000
40000000386d43a0 000  Sat 2000-01-01 00:00:00 +0000
40000000386d43a1 000  Sat 2000-01-01 00:00:01 +0000
4000000038bb0c1f 058  Mon 2000-02-28 23:59:59 +0000
4000000038bc5d9f 059  Tue 2000-02-29 23:59:59 +0000
4000000038bc5da0 060  Wed 2000-03-01 00:00:00 +0000
400000003a4fc89f 365  Sun 2000-12-31 23:59:59 +0000
40000000f4875017 000  Fri 2100-01-01 17:42:15 +0000
40000001b09f1217 000  Wed 2200-01-01 17:42:15 +0000
40000007915fc517 000  Wed 3000-01-01 17:42:15 +0000
4000003afff53a97 000  Sat 10000-01-01 17:42:15 +0000
40001ca4f3758a1f 364  Fri 999999-12-31 23:59:59 +0000
40001ca4f3758a20 000  Sat 1000000-01-01 00:00:00 +0000
*)

    module L1 = struct
        open Cf_stdtime
        
        let x = [
            "0x400000000000000a", {
                year = 1970;
                month = 1;
                day = 1;
                hour = 0;
                minute = 0;
                second = 0;
            };
            
            "0x4000000004b2580a", {
                year = 1972;
                month = 6;
                day = 30;
                hour = 23;
                minute = 59;
                second = 60;
            };
            
            "0x4000000004b2580b", {
                year = 1972;
                month = 7;
                day = 1;
                hour = 0;
                minute = 0;
                second = 0;
            };
            
            "0x4000000004b2580c", {
                year = 1972;
                month = 7;
                day = 1;
                hour = 0;
                minute = 0;
                second = 1;
            };
            
            "0x4000000030e7241b", {
                year = 1995;
                month = 12;
                day = 31;
                hour = 23;
                minute = 59;
                second = 58;
            };
            
            "0x4000000030e7241c", {
                year = 1995;
                month = 12;
                day = 31;
                hour = 23;
                minute = 59;
                second = 59;
            };
        ]
        
        let tsprintf j =
            let mjd =
                Cf_gregorian.to_mjd ~year:j.year ~month:j.month ~day:j.day
            in
            Printf.sprintf
                "{ mjd=%d %04d-%02d-%02d %02d:%02d:%02d }"
                mjd j.year j.month j.day j.hour j.minute j.second
    
        let y (i, j) =
            (*
            Printf.printf "TAI epoch+%s, %s\n" i (tsprintf j);
            flush stdout;
            *)
            let i = Cf_tai64.add_int64 Cf_tai64.first (Int64.of_string i) in
            let j' = utc_of_tai64 i in
            if j <> j' then failwith (tsprintf j');
            let i' =
                utc_to_tai64_unsafe ~year:j.year ~month:j.month ~day:j.day
                ~hour:j.hour ~minute:j.minute ~second:j.second
            in
            if i <> i' then
                let n = Cf_tai64.sub i' Cf_tai64.first in
                failwith (Int64.format "0x%016x" n)
    end

    let test () = List.iter L1.y L1.x
end

module T11 = struct
    open Cf_scmonad.Op
    
    module String_set = Cf_rbtree.Set(String)
    
    (* val memoize: (string, string) Cf_flow.t *)
    let memoize =
        let rec loop () =
            Cf_flow.readSC >>= fun s ->
            Cf_scmonad.load >>= fun u ->
            if String_set.member s u then
                loop ()
            else
                let u = String_set.put s u in
                Cf_scmonad.store u >>= fun () ->
                Cf_flow.writeSC s >>= fun () ->
                loop ()
        in
        Cf_flow.evalSC (loop ()) String_set.nil
    
    (* val uniq: string list -> string list *)
    let uniq s =
        let z = Cf_seq.of_list s in
        let z = Cf_flow.commute memoize z in
        Cf_seq.to_list z
    
    let test () =
        let s1 = [ "Hello"; "World!"; "Hello"; "AGAIN!" ] in
        let s2 = [ "Hello"; "World!"; "AGAIN!" ] in
        let s2' = uniq s1 in
        if s2 <> s2' then failwith "Error in uniq!"
end

module T12 = struct
    open Cf_state_gadget
    open Cf_cmonad.Op
    
    let gadget limit =
        read >>= fun () ->
        duplex >>= fun ((inRx, outTx), (outRx, inTx)) ->
        let inRx = (inRx :> (unit, unit, unit) rx) in
        let outTx = (outTx :> (unit, unit, unit) tx) in
        let outRx = (outRx :> (unit, unit, unit) rx) in
        let inTx = (inTx :> (unit, unit, unit) tx) in
        inTx#put () >>= fun () ->
        wrap inRx outTx Cf_flow.nop >>= fun () ->
        let rec loop () =
            guard begin
                outRx#get begin fun () ->
                    load >>= fun n ->
                    if n < limit then begin
                        (*
                        if n mod 1000 = 0 then begin
                            Printf.printf "n=%08u\n" n;
                            flush stdout
                        end;
                        *)
                        store (succ n) >>= fun () ->
                        inTx#put () >>= loop
                    end
                    else
                        Cf_cmonad.nil 
                end
            end
        in
        start (loop ()) 0
    
    let test () =
        let limit = 10000 in
        let gc = Gc.get () in
        Gc.set { gc with Gc.stack_limit = 4 * 0x400 };
        let w = eval (gadget limit) () in
        match Lazy.force w with
        | Cf_flow.Q f -> let _ = f () in Gc.set gc
        | _ -> failwith "Cf_state_gadget not evaluated to Cf_flow.Q state."
end

module T13 = struct
    module M = Cf_rbtree.Map (Cf_ordered.Int_order)

    let test1a () =
        let tree = M.of_list (List.map (fun a -> (a,())) [9;1;8;3;7;5;4]) in
        let list = Cf_seq.to_list (Cf_seq.first (M.nearest_incr 6 tree)) in
        match list with
        | [ 7; 8; 9 ] -> ()
        | _ ->
            print_char '[';
            List.iter (Printf.printf " %d;") list;
            print_string " ]";
            print_newline ();
            failwith "Cf_rbtree.nearest_incr error [6]!"

    let test1b () =
        let tree = M.of_list (List.map (fun a -> (a,())) [9;1;8;3;7;5;4]) in
        let list = Cf_seq.to_list (Cf_seq.first (M.nearest_incr 5 tree)) in
        match list with
        | [ 5; 7; 8; 9 ] -> ()
        | _ ->
            print_char '[';
            List.iter (Printf.printf " %d;") list;
            print_string " ]";
            print_newline ();
            failwith "Cf_rbtree.nearest_incr error [5]!"
    
    let test2 n =
        let tree = M.of_list (List.map (fun a -> (a,())) [9;1;8;3;7;5;4]) in
        let list = Cf_seq.to_list (Cf_seq.first (M.nearest_decr n tree)) in
        match list with
        | [ 5; 4; 3; 1 ] -> ()
        | _ ->
            print_char '[';
            List.iter (Printf.printf " %d;") list;
            print_string " ]";
            print_newline ();
            jout#fail "Cf_rbtree.nearest_decr error [%d]!" n
    
    let test () =
        test1a ();
        test1b ();
        test2 5;
        test2 6
end

(*
module T14 = struct
    open Cf_gadget
    open Cf_cmonad.Op
    
    let gadget limit =
        read >>= fun () ->
        duplex >>= fun ((inRx, outTx), (outRx, inTx)) ->
        let inRx = (inRx :> (unit, unit, unit) rx) in
        let outTx = (outTx :> (unit, unit, unit) tx) in
        let outRx = (outRx :> (unit, unit, unit) rx) in
        let inTx = (inTx :> (unit, unit, unit) tx) in
        inTx#put () >>= fun () ->
        wrap inRx outTx Cf_flow.nop >>= fun () ->
        let rec loop () =
            guard begin
                outRx#get begin fun () ->
                    load >>= fun n ->
                    if n < limit then begin
                        (*
                        if n mod 1000 = 0 then begin
                            Printf.printf "n=%08u\n" n;
                            flush stdout
                        end;
                        *)
                        store (succ n) >>= fun () ->
                        inTx#put () >>= loop
                    end
                    else
                        Cf_cmonad.nil 
                end
            end
        in
        start (loop ()) 0
    
    let test () =
        let limit = 10000 in
        let gc = Gc.get () in
        Gc.set { gc with Gc.stack_limit = 4 * 0x400 };
        let w = eval (gadget limit) () in
        match Lazy.force w with
        | Cf_flow.Q f -> let _ = f () in Gc.set gc
        | _ -> failwith "Cf_state_gadget not evaluated to Cf_flow.Q state."
end
*)


(*
module Obsolete_1 = struct
    module V6 = Cf_ip6_addr
    open Printf
    
    let v6_unicast_cases = [
        "::DEAD:BEEF:D00D", V6.U_reserved;
        "::1", V6.U_loopback;
        "4000::1", V6.U_unassigned;
        "::10.0.1.1", V6.U_v4compat;
        "::FFFF:17.201.23.45", V6.U_v4mapped;
        "2002::1", V6.U_global;
        "FE80::203:93ff:feba:7eba", V6.U_link;
        "FEC0::1", V6.U_site;
        "::224.0.0.1", V6.U_reserved;
    ]
    
    let v6_unicast_test () =
        List.iter begin fun (addr, fmt) ->
            match V6.pton addr with
            | Some addr' ->
                if fmt <> V6.unicast_format (V6.is_unicast addr') then
                    failwith (sprintf "V6.unicast_format %s" addr)
            | None ->
                failwith (sprintf "unrecognized %s" addr)
        end v6_unicast_cases
    
    let test () =
        v6_unicast_test ()
end

module Obsolete_2 = struct
    let len = 10
    
    let test () =
        try
            let bind =
                (Cf_ip4_addr.loopback :> Cf_ip4_addr.opaque Cf_ip4_addr.t), 0
            in
            let listen = new Cf_tcp4_socket.listener bind in
            listen#listen 1;
            let bind = listen#getsockname in
            let active = new Cf_tcp4_socket.initiator bind in
            active#connect;
            let a, _ = listen#accept in
            let a = new Cf_tcp4_socket.endpoint a in
            let b = new Cf_tcp4_socket.endpoint (active#socket) in
            a#setsockopt Cf_ip_common.tcp_nodelay true;
            b#setsockopt Cf_ip_common.tcp_nodelay true;
            let laddr = a#getpeername and raddr = b#getsockname in
            let lhost, lport = laddr in
            let rhost, rport = raddr in
            if lhost <> rhost then
                failwith "O2 error: host a#getpeername <> b#getsockname";
            if lport <> rport then
                failwith "O2 error: port a#getpeername <> b#getsockname";
            let laddr = a#getsockname and raddr = b#getpeername in
            let lhost, lport = laddr in
            let rhost, rport = raddr in
            if lhost <> rhost then
                failwith "O2 error: host a#getsockname <> b#getpeername";
            if lport <> rport then
                failwith "O2 error: port a#getsockname <> b#getpeername";
            let tx = String.make len 'x' and rx = String.create len in
            let n = a#send tx 0 len in
            if n <> len then failwith "O2 error: tx incomplete!";
            a#shutdown Unix.SHUTDOWN_SEND;
            let n = b#recv rx 0 len in
            if n <> len then failwith "O2 error: rx incomplete!";
            if tx <> rx then failwith "O2 error: tx <> rx!";
            a#close;
            b#close;
            listen#close
        with
        | Unix.Unix_error (e, fn, _) ->
            failwith (Printf.sprintf "O2 error: %s in %s.\n"
                (Unix.error_message e) fn)
end

module Obsolete_3 = struct
    let len = 10
    
    let test () =
        try
            let bind =
                (Cf_ip6_addr.loopback :> Cf_ip6_addr.opaque Cf_ip6_addr.t),
                0, 0l
            in
            let listen = new Cf_tcp6_socket.listener bind in
            listen#listen 1;
            let bind = listen#getsockname in
            let active = new Cf_tcp6_socket.initiator bind in
            active#connect;
            let a = new Cf_tcp6_socket.endpoint (active#socket) in
            let b, _ = listen#accept in
            let b = new Cf_tcp6_socket.endpoint b in
            a#setsockopt Cf_ip_common.tcp_nodelay true;
            b#setsockopt Cf_ip_common.tcp_nodelay true;
            let laddr = a#getpeername and raddr = b#getsockname in
            let lhost, lport, lscope = laddr in
            let rhost, rport, rscope = raddr in
            if lhost <> rhost then
                failwith "O3 error: host a#getpeername <> b#getsockname";
            if lport <> rport then
                failwith "O3 error: port a#getpeername <> b#getsockname";
            if lscope <> rscope then
                failwith "O3 error: port a#getpeername <> b#getsockname";
            let laddr = a#getsockname and raddr = b#getpeername in
            let lhost, lport, lscope = laddr in
            let rhost, rport, rscope = raddr in
            if lhost <> rhost then
                failwith "O3 error: host a#getsockname <> b#getpeername";
            if lport <> rport then
                failwith "O3 error: port a#getsockname <> b#getpeername";
            if lscope <> rscope then
                failwith "O3 error: port a#getsockname <> b#getpeername";
            let tx = String.make len 'x' and rx = String.create len in
            let n = a#send tx 0 len in
            if n <> len then failwith "O3 error: tx incomplete!";
            a#shutdown Unix.SHUTDOWN_SEND;
            let n = b#recv rx 0 len in
            if n <> len then failwith "O3 error: rx incomplete!";
            if tx <> rx then failwith "O3 error: tx <> rx!";
            a#close;
            b#close;
            listen#close
        with
        | Unix.Unix_error (e, fn, _) ->
            failwith (Printf.sprintf "O3 error: %s in %s.\n"
                (Unix.error_message e) fn)
end

module Obsolete_4 = struct
    open Cf_uri
    
    module L1 = struct
        let test () = ()
    end
    
    module L2 = struct
    
        (* http://a/b/c/d;p?q *)
        let base = {
            abs_scheme = "http";
            abs_special = S_hier {
                abs_hier_query = Some "q";
                abs_hier_path = `Net {
                    net_authority = A_server (Some {
                        srv_user = None;
                        srv_host = H_hostname "a";
                        srv_port = None;
                    });
                    
                    net_path = [
                        { seg_name = "b"; seg_params = [] };
                        { seg_name = "c"; seg_params = [] };
                        { seg_name = "d"; seg_params = [ "p" ] };
                    ];
                };
            };
        }
        
        let basestr = Cf_message.contents (message_of_uri (A base))
        
        let resolve_list = [
            "g:h"           , "g:h";
            "g"             , "http://a/b/c/g";
            "./g"           , "http://a/b/c/g";
            "g/"            , "http://a/b/c/g/";
            "/g"            , "http://a/g";
            "//g"           , "http://g";
            "?y"            , "http://a/b/c/?y";
            "g?y"           , "http://a/b/c/g?y";
            "#s"            , "http://a/b/c/d;p?q#s";
            "g#s"           , "http://a/b/c/g#s";
            "g?y#s"         , "http://a/b/c/g?y#s";
            ";x"            , "http://a/b/c/;x";
            "g;x"           , "http://a/b/c/g;x";
            "g;x?y#s"       , "http://a/b/c/g;x?y#s";
            "."             , "http://a/b/c/";
            "./"            , "http://a/b/c/";
            ".."            , "http://a/b/";
            "../"           , "http://a/b/";
            "../g"          , "http://a/b/g";
            "../.."         , "http://a/";
            "../../"        , "http://a/";
            "../../g"       , "http://a/g";
        ]
        
        let resolve (relstr, expectstr) =
            let rel = Cf_message.create relstr in
            let result = message_to_absolute_uri_reference ~base rel in
            let resultstr =
                Cf_message.contents (message_of_uri_reference result)
            in
            if resultstr <> expectstr then begin
                let s = Printf.sprintf
                    "reference URI resolution error \
                    [rel=\"%s\" expect=\"%s\" result=\"%s\"\n"
                    relstr expectstr resultstr
                in
                failwith s
            end;
            ignore (message_of_uri_reference result)
        
        let unresolved_list = [
            "http://a", "../b";
        ]
        
        let unresolved (basestr, relstr) =
            let base = message_to_uri (Cf_message.create basestr) in
            let rel = message_to_uri (Cf_message.create relstr) in
            let base =
                match base with
                | A base -> base
                | _ -> invalid_arg "base not absolute"
            in
            let rel =
                match rel with
                | R rel -> rel
                | _ -> invalid_arg "rel not relative"
            in
            try
                ignore (refer_to_base ~base ~rel);
                failwith "expected to catch Rel_undefined."
            with
            | Rel_undefined ->
                ()            
        
        let test () =
            if basestr <> "http://a/b/c/d;p?q" then
                failwith "base URI emit error";
            List.iter resolve resolve_list;
            List.iter unresolved unresolved_list
    end
    
    let test () =
        L1.test ();
        L2.test ();
end

module Obsolete_5 = struct
    open Cf_poll
    
    class clock n dt =
        object
            inherit [unit] time dt as super
            
            val mutable count_ = n
                        
            method private service _ =
                let tai, frac = Cf_tai64n.decompose epoch_ in
                let utc = Cf_stdtime.utc_of_tai64 tai in
                jout#info
                    "O5.clock#service: %04u-%02u-%02u %02u:%02u:%02u.%06u "
                    utc.Cf_stdtime.year utc.Cf_stdtime.month utc.Cf_stdtime.day
                    utc.Cf_stdtime.hour utc.Cf_stdtime.minute
                    utc.Cf_stdtime.second frac;
                if count_ > 0 then begin
                    count_ <- pred count_;
                    state_
                end
                else
                    Cf_poll.Final ()
            
            method count = count_
        end
    
    class interrupt n dt =
        object(self:'self)
            inherit [int] signal Sys.sigint as super
            
            val clock_ = new clock n dt
            
            method private load_ p =
                clock_#load p;
                super#load_ p
            
            method private unload_ p =
                super#unload_ p;
                clock_#unload
            
            method private service _ =
                clock_#unload;
                Cf_poll.Final clock_#count
            
            method canget =
                if not super#canget then begin
                    if not clock_#canget then
                        false
                    else begin
                        self#unload;
                        state_ <- Cf_poll.Final 0;
                        true
                    end
                end
                else begin
                    clock_#unload;
                    true
                end
        end
    
    let test () =
        let e = new interrupt 10 0.05 in
        let p = create () in
        e#load p;
        let save = Unix.sigprocmask Unix.SIG_SETMASK [] in
        let more = ref More in
        while not e#canget do
            if !more <> More then failwith "!More";
            more := cycle p
        done;
        if !more = More then more := cycle p;
        if !more <> Last then failwith "!Last";
        ignore (Unix.sigprocmask Unix.SIG_SETMASK save)
end
*)

let main () =
    let tests = [
        T1.test; T2.test; T3.test; T4.test; T5.test; T6.test; T7.test;
        T8.test; T9.test; T10.test; T11.test; T12.test; T13.test;
        (*; T15.test *)
        (*
        Obsolete_1.test;
        Obsolete_2.test;
        Obsolete_3.test;
        Obsolete_4.test;
        Obsolete_5.test;
        *)
    ] in
    Printf.printf "1..%d\n" (List.length tests);
    flush stdout;
        
    let test i f =
        begin
            try
                (* let tms0 = Unix.times () in *)
                f ();
                (*
                let tms1 = Unix.times () in
                let ut = tms1.Unix.tms_utime -. tms0.Unix.tms_utime in
                let st = tms1.Unix.tms_stime -. tms0.Unix.tms_stime in
                Printf.printf "ok %d (ut=%f st=%f)\n" i ut st
                *)
                Printf.printf "ok %d\n" i
            with
            | Failure(s) ->
                Printf.printf "not ok %d (Failure \"%s\")\n" i s
            | x ->
                Printf.printf "not ok %d\n" i;
                flush stdout;
                raise x
        end;
        flush stdout;
        succ i
    in
    let _ = List.fold_left test 1 tests in
    exit 0
;;

main ();;

(*--- $File$ ---*)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.