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
|
(* TEST_BELOW
(* Blank lines added here to preserve locations. *)
*)
(**************************************************************)
(* This suite tests the pattern-matching compiler *)
(* it should just compile and run. *)
(* While compiling the following messages are normal: *)
(**************************************************************)
let test msg f arg r =
if f arg <> r then begin
prerr_endline msg ;
failwith "Malaise"
end
;;
type t = A | B | C | D | E | F
;;
let f x = match x with
| A | B | C -> 1
| D | E -> 2
| F -> 3;;
test "un" f C 1 ;
test "un" f D 2 ;
test "un" f F 3 ; ()
;;
let g x = match x with
1 -> 1
| 2 -> 2
| 3 -> 3
| 4 | 5 -> 4
| 6 -> 5
| 7 | 8 -> 6
| 9 -> 7
| _ -> assert false
;;
test "deux" g 5 4 ;
test "deux" g 6 5 ;
test "deux" g 9 7 ; ()
;;
let g x = match x with
1 -> 1
| 2 -> 2
| 3 -> 3
| 4 | 5 -> 4
| 6 -> 5
| 7 | 8 -> 6
| 9 -> 7
| _ -> 8;;
test "trois" g 10 8
;;
let g x= match x with
1 -> 1
| 2 -> 2
| 3 -> 3
| 4 | 5 -> 4
| 6 -> 5
| 4|5|7 -> 100
| 7 | 8 -> 6
| 9 -> 7
| _ -> 8
;;
test "quatre" g 4 4 ;
test "quatre" g 7 100 ; ()
;;
let h x =
match x with
(1,1) -> 1
| (2|3), 1 -> 2
| 2,(2|3) -> 3
| (4,4) -> 5
| _ -> 100
;;
test "cinq" h (2,2) 3 ;
test "cinq" h (2,1) 2 ;
test "cinq" h (2,4) 100 ; ()
;;
(* idem hh (2,5) *)
let hh x = match x with
| 1,1 -> 1
| 2,1 -> 2
| (2|3),(1|2|3|4) -> 3
| 2,5 -> 4
| (4,4) -> 5
| _ -> 100
;;
let hhh x = match x with
| 1,1 -> 1
| (2|3),1 -> 2
| 2,2 -> 3
| _ -> 100
;;
let h x =
match x with
(1,1) -> 1
| 3,1 -> 2
| 2,(2|3) -> 3
| (4,4) -> 5
| _ -> 100
;;
let h x = match x with
1 -> 1
| 2|3 -> 2
| 4 -> 4
| 5 -> 5
| 6|7 -> 6
| 8 -> 8
| _ -> 100
;;
let f x = match x with
| ((1|2),(3|4))|((3|4),(1|2)) -> 1
| (3,(5|6)) -> 2
| _ -> 3
;;
test "six" f (1,3) 1 ;
test "six" f (3,2) 1 ;
test "six" f (3,5) 2 ;
test "six" f (3,7) 3 ; ()
;;
type tt = {a : bool list ; b : bool}
let f = function
| {a=([]|[true])} -> 1
| {a=false::_}|{b=(true|false)} -> 2
;;
test "sept" f {a=[] ; b = true} 1 ;
test "sept" f {a=[true] ; b = false} 1 ;
test "sept" f {a=[false ; true] ; b = true} 2 ;
test "sept" f {a=[false] ; b = false} 2 ; ()
;;
let f = function
| (([]|[true]),_) -> 1
| (false::_,_)|(_,(true|false)) -> 2
;;
test "huit" f ([],true) 1 ;
test "huit" f ([true],false) 1 ;
test "huit" f ([false ; true], true) 2 ;
test "huit" f ([false], false) 2 ; ()
;;
let split_cases = function
| `Nil | `Cons _ as x -> `A x
| `Snoc _ as x -> `B x
;;
test "oubli" split_cases `Nil (`A `Nil);
test "oubli" split_cases (`Cons 1) (`A (`Cons 1));
test "oubli" split_cases (`Snoc 1) (`B (`Snoc 1)) ; ()
;;
type t1 = A of int | B of int
let f1 = function
| (A x | B x) -> x
;;
test "neuf" f1 (A 1) 1 ;
test "neuf" f1 (B 1) 1 ;
;;
type coucou = A of int | B of int * int | C
;;
let g = function
| (A x | B (_,x)) -> x
| C -> 0
;;
test "dix" g (A 1) 1 ;
test "dix" g (B (1,2)) 2 ;
;;
let h = function
| ([x]|[1 ; x ]|[1 ; 2 ; x]) -> x
| _ -> 0
;;
test "encore" h [1] 1 ;
test "encore" h [1;2] 2 ;
test "encore" h [1;2;3] 3 ;
test "encore" h [0 ; 0] 0 ; ()
;;
let f = function
| (x,(0 as y)) | (y,x) -> y-x
;;
test "foo1" f (1,0) (-1);
test "foo1" f (1,2) (-1)
;;
let f = function (([]|[_]) as x)|(_::([] as x))|(_::_::x) -> x
;;
test "zob" f [] [] ;
test "zob" f [1] [1] ;
test "zob" f [1;2;3] [3]
;;
type zob = A | B | C | D of zob * int | E of zob * zob
let rec f = function
| (A | B | C) -> A
| D (x,i) -> D (f x,i)
| E (x,_) -> D (f x,0)
;;
test "fin" f B A ;
test "fin" f (D (C,1)) (D (A,1)) ;
test "fin" f (E (C,A)) (D (A,0)) ; ()
;;
type length =
Char of int | Pixel of int | Percent of int | No of string | Default
let length = function
| Char n -> n | Pixel n -> n
| _ -> 0
;;
test "length" length (Char 10) 10 ;
test "length" length (Pixel 20) 20 ;
test "length" length Default 0 ;
test "length" length (Percent 100) 0 ; ()
;;
let length2 = function
| Char n -> n | Percent n -> n
| _ -> 0
;;
test "length2" length2 (Char 10) 10 ;
test "length2" length2 (Pixel 20) 0 ;
test "length2" length2 Default 0 ;
test "length2" length2(Percent 100) 100 ; ()
;;
let length3 = function
| Char _ | No _ -> true
| _ -> false
;;
test "length3" length3 (Char 10) true ;
test "length3" length3 (No "") true ;
test "length3" length3 (Pixel 20) false ;
test "length3" length3 Default false ;
test "length3" length3(Percent 100) false ; ()
;;
type hevea = A | B | C
let h x = match x with
| A -> 1
| B|C -> 2
;;
test "hevea" h A 1 ;
test "hevea" h B 2 ;
test "hevea" h B 2 ; ()
;;
type lambda =
Lvar of int
| Lconst of int
| Lapply of lambda * lambda list
| Lfunction of bool * int list * lambda
| Llet of bool * int * lambda * lambda
| Lletrec of (int * lambda) list * lambda
| Lprim of string * lambda list
| Lswitch of lambda * lambda_switch
| Lstaticfail
| Lcatch of lambda * lambda
| Lstaticraise of int * lambda list
| Lstaticcatch of lambda * (int * int list) * lambda
| Ltrywith of lambda * int * lambda
| Lifthenelse of lambda * lambda * lambda
| Lsequence of lambda * lambda
| Lwhile of lambda * lambda
| Lfor of int * lambda * lambda * bool * lambda
| Lassign of int * lambda
| Lsend of lambda * lambda * lambda list
| Levent of lambda * lambda_event
| Lifused of int * lambda
and lambda_switch =
{ sw_numconsts: int; (* Number of integer cases *)
sw_consts: (int * lambda) list; (* Integer cases *)
sw_numblocks: int; (* Number of tag block cases *)
sw_blocks: (int * lambda) list; (* Tag block cases *)
sw_checked: bool ; (* True if bound checks needed *)
sw_nofail: bool} (* True if should not fail *)
and lambda_event =
{ lev_loc: int;
lev_kind: bool ;
lev_repr: int ref option;
lev_env: int list }
let rec approx_present v l = true
let rec lower_bind v arg lam = match lam with
| Lifthenelse (cond, ifso, ifnot) -> 1
| Lswitch (ls,({sw_consts=[i,act] ; sw_blocks = []} as _sw))
when not (approx_present v ls) -> 2
| Lswitch (ls,({sw_consts=[] ; sw_blocks = [i,act]} as _sw))
when not (approx_present v ls) -> 3
| Llet (true , vv, lv, l) -> 4
| _ -> 5
;;
test "lower_bind" (lower_bind 0 0) (Llet (true,0, Lvar 1, Lvar 2)) 4 ;
test "lower_bind" (lower_bind 0 0) (Lvar 0) 5 ;
test "lower_bind" (lower_bind 0 0) (Lifthenelse (Lvar 0, Lvar 1, Lvar 2)) 1
;;
type field_kind =
Fvar of field_kind option ref
| Fpresent
| Fabsent
let unify_kind (k1, k2) = match k1, k2 with
(Fvar r, (Fvar _ | Fpresent)) -> 1
| (Fpresent, Fvar r) -> 2
| (Fpresent, Fpresent) -> 3
| _ -> 4
let r = ref (Some Fpresent)
;;
test "unify" unify_kind (Fvar r, Fpresent) 1 ;
test "unify" unify_kind (Fvar r, Fvar r) 1 ;
test "unify" unify_kind (Fvar r, Fabsent) 4 ;
test "unify" unify_kind (Fpresent, Fvar r) 2 ;
test "unify" unify_kind (Fpresent, Fpresent) 3 ;
test "unify" unify_kind (Fabsent, Fpresent) 4 ; ()
;;
type youyou = A | B | C | D of youyou
let foo (k1, k2) = match k1,k2 with
| D _, (A|D _) -> 1
| (A|B),D _ -> 2
| C,_ -> 3
| _, (A|B|C) -> 4
;;
test "foo2" foo (D A,A) 1 ;
test "foo2" foo (D A,B) 4 ;
test "foo2" foo (A,A) 4 ; ()
;;
type yaya = A | B
;;
let yaya = function
| A,_,_ -> 1
| _,A,_ -> 2
| B,B,_ -> 3
| A,_,(100|103) -> 5
;;
test "yaya" yaya (A,A,0) 1 ;
test "yaya" yaya (B,A,0) 2 ;
test "yaya" yaya (B,B,100) 3 ; ()
;;
let yoyo = function
| [],_,_ -> 1
| _,[],_ -> 2
| _::_,_::_,_ -> 3
| [],_,(100|103|104) -> 5
| [],_,(100|103) -> 6
| [],_,(1000|1001|1002|20000) -> 7
;;
test "yoyo" yoyo ([],[],0) 1 ;
test "yoyo" yoyo ([1],[],0) 2 ;
test "yoyo" yoyo ([1],[1],100) 3 ; ()
;;
let youyou = function
| (100|103|104) -> 1
| (100|103|101) -> 2
| (1000|1001|1002|20000) -> 3
| _ -> -1
;;
test "youyou" youyou 100 1 ;
test "youyou" youyou 101 2 ;
test "youyou" youyou 1000 3
;;
type autre =
| C | D | E of autre | F of autre * autre | H of autre | I | J | K of string
let rec autre = function
| C,_,_ -> 1
| _,C,_ -> 2
| D,D,_ -> 3
| (D|F (_,_)|H _|K _),_,_ -> 4
| (_, (D|I|E _|F (_, _)|H _|K _), _) -> 8
| (J,J,((C|D) as x |E x|F (_,x))) | (J,_,((C|J) as x)) -> autre (x,x,x)
| (J, J, (I|H _|K _)) -> 9
| I,_,_ -> 6
| E _,_,_ -> 7
;;
test "autre" autre (J,J,F (D,D)) 3 ;
test "autre" autre (J,J,D) 3 ;
test "autre" autre (J,J,I) 9 ;
test "autre" autre (H I,I,I) 4 ;
test "autre" autre (J,J,H I) 9 ; ()
;;
type youpi = YA | YB | YC
and hola = X | Y | Z | T of hola | U of hola | V of hola
let xyz = function
| YA,_,_ -> 1
| _,YA,_ -> 2
| YB,YB,_ -> 3
| ((YB|YC), (YB|YC), (X|Y|Z|V _|T _)) -> 6
| _,_,(X|U _) -> 8
| _,_,Y -> 5
;;
test "xyz" xyz (YC,YC,X) 6 ;
test "xyz" xyz (YC,YB,U X) 8 ;
test "xyz" xyz (YB,YC,X) 6 ; ()
;;
(* This test is for the compiler itself *)
let eq (x,y) = x=y
;;
test "eq" eq ("coucou", "coucou") true ; ()
;;
(* Test guards, non trivial *)
let is_none = function
| None -> true
| _ -> false
let guard x = match x with
| (Some _, _) when is_none (snd x) -> 1
| (Some (pc, _), Some pc') when pc = pc' -> 2
| _ -> 3
;;
test "guard" guard (Some (1,1),None) 1 ;
test "guard" guard (Some (1,1),Some 1) 2 ;
test "guard" guard (Some (2,1),Some 1) 3 ; ()
;;
let orstring = function
| ("A"|"B"|"C") -> 2
| "D" -> 3
| _ -> 4
;;
test "orstring" orstring "A" 2 ;
test "orstring" orstring "B" 2 ;
test "orstring" orstring "C" 2 ;
test "orstring" orstring "D" 3 ;
test "orstring" orstring "E" 4 ; ()
;;
type var_t = [`Variant of [ `Some of string | `None | `Foo] ]
let crash (pat:var_t) =
match pat with
| `Variant (`Some tag) -> tag
| `Variant (`None) -> "none"
| _ -> "foo"
;;
test "crash" crash (`Variant `None) "none" ;
test "crash" crash (`Variant (`Some "coucou")) "coucou" ;
test "crash" crash (`Variant (`Foo)) "foo" ; ()
;;
let flatguard c =
let x,y = c in
match x,y with
| (1,2)|(2,3) when y=2 -> 1
| (1,_)|(_,3) -> 2
| _ -> 3
;;
test "flatguard" flatguard (1,2) 1 ;
test "flatguard" flatguard (1,3) 2 ;
test "flatguard" flatguard (2,3) 2 ;
test "flatguard" flatguard (2,4) 3 ; ()
;;
(* Jerome's bugs *)
type f =
| ABSENT
| FILE
| SYMLINK
| DIRECTORY
type r =
| Unchanged
| Deleted
| Modified
| PropsChanged
| Created
let replicaContent2shortString rc =
let (typ, status) = rc in
match typ, status with
_, Unchanged -> " "
| ABSENT, Deleted -> "deleted "
| FILE, Created -> "new file"
| FILE, Modified -> "changed "
| FILE, PropsChanged -> "props "
| SYMLINK, Created -> "new link"
| SYMLINK, Modified -> "chgd lnk"
| DIRECTORY, Created -> "new dir "
| DIRECTORY, Modified -> "chgd dir"
| DIRECTORY, PropsChanged -> "props "
(* Cases that can't happen... *)
| ABSENT, (Created | Modified | PropsChanged)
| SYMLINK, PropsChanged
| (FILE|SYMLINK|DIRECTORY), Deleted
-> "assert false"
;;
test "jerome_constr"
replicaContent2shortString (ABSENT, Unchanged) " " ;
test "jerome_constr"
replicaContent2shortString (ABSENT, Deleted) "deleted " ;
test "jerome_constr"
replicaContent2shortString (FILE, Modified) "changed " ;
test "jerome_constr"
replicaContent2shortString (DIRECTORY, PropsChanged) "props " ;
test "jerome_constr"
replicaContent2shortString (FILE, Deleted) "assert false" ;
test "jerome_constr"
replicaContent2shortString (SYMLINK, Deleted) "assert false" ;
test "jerome_constr"
replicaContent2shortString (SYMLINK, PropsChanged) "assert false" ;
test "jerome_constr"
replicaContent2shortString (DIRECTORY, Deleted) "assert false" ;
test "jerome_constr"
replicaContent2shortString (ABSENT, Created) "assert false" ;
test "jerome_constr"
replicaContent2shortString (ABSENT, Modified) "assert false" ;
test "jerome_constr"
replicaContent2shortString (ABSENT, PropsChanged) "assert false" ;
;;
let replicaContent2shortString rc =
let (typ, status) = rc in
match typ, status with
_, `Unchanged -> " "
| `ABSENT, `Deleted -> "deleted "
| `FILE, `Created -> "new file"
| `FILE, `Modified -> "changed "
| `FILE, `PropsChanged -> "props "
| `SYMLINK, `Created -> "new link"
| `SYMLINK, `Modified -> "chgd lnk"
| `DIRECTORY, `Created -> "new dir "
| `DIRECTORY, `Modified -> "chgd dir"
| `DIRECTORY, `PropsChanged -> "props "
(* Cases that can't happen... *)
| `ABSENT, (`Created | `Modified | `PropsChanged)
| `SYMLINK, `PropsChanged
| (`FILE|`SYMLINK|`DIRECTORY), `Deleted
-> "assert false"
;;
test "jerome_variant"
replicaContent2shortString (`ABSENT, `Unchanged) " " ;
test "jerome_variant"
replicaContent2shortString (`ABSENT, `Deleted) "deleted " ;
test "jerome_variant"
replicaContent2shortString (`FILE, `Modified) "changed " ;
test "jerome_variant"
replicaContent2shortString (`DIRECTORY, `PropsChanged) "props " ;
test "jerome_variant"
replicaContent2shortString (`FILE, `Deleted) "assert false" ;
test "jerome_variant"
replicaContent2shortString (`SYMLINK, `Deleted) "assert false" ;
test "jerome_variant"
replicaContent2shortString (`SYMLINK, `PropsChanged) "assert false" ;
test "jerome_variant"
replicaContent2shortString (`DIRECTORY, `Deleted) "assert false" ;
test "jerome_variant"
replicaContent2shortString (`ABSENT, `Created) "assert false" ;
test "jerome_variant"
replicaContent2shortString (`ABSENT, `Modified) "assert false" ;
test "jerome_variant"
replicaContent2shortString (`ABSENT, `PropsChanged) "assert false" ;
;;
(* bug 319 *)
type ab = A of int | B of int
type cd = C | D
let ohl = function
| (A (p) | B (p)), C -> p
| (A (p) | B (p)), D -> p
;;
test "ohl" ohl (A 0,C) 0 ;
test "ohl" ohl (B 0,D) 0 ; ()
;;
(* bug 324 *)
type pottier =
| A
| B
;;
let pottier x =
match x with
| (( (A, 1) | (B, 2)),A) -> false
| _ -> true
;;
test "pottier" pottier ((B,2),A) false ;
test "pottier" pottier ((B,2),B) true ;
test "pottier" pottier ((A,2),A) true ; ()
;;
(* bug 325 in bytecode compiler *)
let coquery q = match q with
| y,0,([modu;defs]| [defs;modu;_]) -> y+defs-modu
| _ -> 0
;;
test "coquery" coquery (1,0,[1 ; 2 ; 3]) 0 ;
test "coquery" coquery (1,0,[1 ; 2]) 2 ; ()
;;
(*
Two other variable in or-pat tests
*)
type vars = A of int | B of (int * int) | C
;;
let vars1 = function
| (A x | B (_,x)) -> x
| C -> 0
;;
test "vars1" vars1 (A 1) 1 ;
test "vars1" vars1 (B (1,2)) 2 ; ()
;;
let vars2 = function
| ([x]|[1 ; x ]|[1 ; 2 ; x]) -> x
| _ -> 0
;;
test"vars2" vars2 [1] 1 ;
test"vars2" vars2 [1;2] 2 ;
test"vars2" vars2 [1;2;3] 3 ;
test"vars2" vars2 [0 ; 0] 0 ; ()
;;
(* Bug 342 *)
type eber = {x:int; y: int; z:bool}
let eber = function
| {x=a; z=true}
| {y=a; z=false} -> a
;;
test "eber" eber {x=0 ; y=1 ; z=true} 0 ;
test "eber" eber {x=1 ; y=0 ; z=false} 0 ; ()
;;
(* Chaining interval tests *)
let escaped = function
| '\"' | '\\' | '\n' | '\t' -> 2
| c -> 1
;;
test "escaped" escaped '\"' 2 ;
test "escaped" escaped '\\' 2 ;
test "escaped" escaped '\n' 2 ;
test "escaped" escaped '\t' 2 ;
test "escaped" escaped '\000' 1 ;
test "escaped" escaped ' ' 1 ;
test "escaped" escaped '\000' 1 ;
test "escaped" escaped '[' 1 ;
test "escaped" escaped ']' 1 ;
test "escaped" escaped '!' 1 ;
test "escaped" escaped '#' 1 ;
()
;;
(* For compilation speed (due to J. Garigue) *)
exception Unknown_Reply of int
type command_reply =
RPL_TRYAGAIN
| RPL_TRACEEND
| RPL_TRACELOG
| RPL_ADMINEMAIL
| RPL_ADMINLOC2
| RPL_ADMINLOC1
| RPL_ADMINME
| RPL_LUSERME
| RPL_LUSERCHANNELS
| RPL_LUSERUNKNOWN
| RPL_LUSEROP
| RPL_LUSERCLIENT
| RPL_STATSDLINE
| RPL_STATSDEBUG
| RPL_STATSDEFINE
| RPL_STATSBLINE
| RPL_STATSPING
| RPL_STATSSLINE
| RPL_STATSHLINE
| RPL_STATSOLINE
| RPL_STATSUPTIME
| RPL_STATSLLINE
| RPL_STATSVLINE
| RPL_SERVLISTEND
| RPL_SERVLIST
| RPL_SERVICE
| RPL_ENDOFSERVICES
| RPL_SERVICEINFO
| RPL_UMODEIS
| RPL_ENDOFSTATS
| RPL_STATSYLINE
| RPL_STATSQLINE
| RPL_STATSKLINE
| RPL_STATSILINE
| RPL_STATSNLINE
| RPL_STATSCLINE
| RPL_STATSCOMMANDS
| RPL_STATSLINKINFO
| RPL_TRACERECONNECT
| RPL_TRACECLASS
| RPL_TRACENEWTYPE
| RPL_TRACESERVICE
| RPL_TRACESERVER
| RPL_TRACEUSER
| RPL_TRACEOPERATOR
| RPL_TRACEUNKNOWN
| RPL_TRACEHANDSHAKE
| RPL_TRACECONNECTING
| RPL_TRACELINK
| RPL_NOUSERS
| RPL_ENDOFUSERS
| RPL_USERS
| RPL_USERSSTART
| RPL_TIME
| RPL_NOTOPERANYMORE
| RPL_MYPORTIS
| RPL_YOURESERVICE
| RPL_REHASHING
| RPL_YOUREOPER
| RPL_ENDOFMOTD
| RPL_MOTDSTART
| RPL_ENDOFINFO
| RPL_INFOSTART
| RPL_MOTD
| RPL_INFO
| RPL_ENDOFBANLIST
| RPL_BANLIST
| RPL_ENDOFLINKS
| RPL_LINKS
| RPL_CLOSEEND
| RPL_CLOSING
| RPL_KILLDONE
| RPL_ENDOFNAMES
| RPL_NAMREPLY
| RPL_ENDOFWHO
| RPL_WHOREPLY
| RPL_VERSION
| RPL_SUMMONING
| RPL_INVITING
| RPL_TOPIC
| RPL_NOTOPIC
| RPL_CHANNELMODEIS
| RPL_LISTEND
| RPL_LIST
| RPL_LISTSTART
| RPL_WHOISCHANNELS
| RPL_ENDOFWHOIS
| RPL_WHOISIDLE
| RPL_WHOISCHANOP
| RPL_ENDOFWHOWAS
| RPL_WHOWASUSER
| RPL_WHOISOPERATOR
| RPL_WHOISSERVER
| RPL_WHOISUSER
| RPL_NOWAWAY
| RPL_UNAWAY
| RPL_TEXT
| RPL_ISON
| RPL_USERHOST
| RPL_AWAY
| RPL_NONE
let get_command_reply n =
match n with
263 -> RPL_TRYAGAIN
| 319 -> RPL_WHOISCHANNELS
| 318 -> RPL_ENDOFWHOIS
| 317 -> RPL_WHOISIDLE
| 316 -> RPL_WHOISCHANOP
| 369 -> RPL_ENDOFWHOWAS
| 314 -> RPL_WHOWASUSER
| 313 -> RPL_WHOISOPERATOR
| 312 -> RPL_WHOISSERVER
| 311 -> RPL_WHOISUSER
| 262 -> RPL_TRACEEND
| 261 -> RPL_TRACELOG
| 259 -> RPL_ADMINEMAIL
| 258 -> RPL_ADMINLOC2
| 257 -> RPL_ADMINLOC1
| 256 -> RPL_ADMINME
| 255 -> RPL_LUSERME
| 254 -> RPL_LUSERCHANNELS
| 253 -> RPL_LUSERUNKNOWN
| 252 -> RPL_LUSEROP
| 251 -> RPL_LUSERCLIENT
| 250 -> RPL_STATSDLINE
| 249 -> RPL_STATSDEBUG
| 248 -> RPL_STATSDEFINE
| 247 -> RPL_STATSBLINE
| 246 -> RPL_STATSPING
| 245 -> RPL_STATSSLINE
| 244 -> RPL_STATSHLINE
| 243 -> RPL_STATSOLINE
| 242 -> RPL_STATSUPTIME
| 241 -> RPL_STATSLLINE
| 240 -> RPL_STATSVLINE
| 235 -> RPL_SERVLISTEND
| 234 -> RPL_SERVLIST
| 233 -> RPL_SERVICE
| 232 -> RPL_ENDOFSERVICES
| 231 -> RPL_SERVICEINFO
| 221 -> RPL_UMODEIS
| 219 -> RPL_ENDOFSTATS
| 218 -> RPL_STATSYLINE
| 217 -> RPL_STATSQLINE
| 216 -> RPL_STATSKLINE
| 215 -> RPL_STATSILINE
| 214 -> RPL_STATSNLINE
| 213 -> RPL_STATSCLINE
| 212 -> RPL_STATSCOMMANDS
| 211 -> RPL_STATSLINKINFO
| 210 -> RPL_TRACERECONNECT
| 209 -> RPL_TRACECLASS
| 208 -> RPL_TRACENEWTYPE
| 207 -> RPL_TRACESERVICE
| 206 -> RPL_TRACESERVER
| 205 -> RPL_TRACEUSER
| 204 -> RPL_TRACEOPERATOR
| 203 -> RPL_TRACEUNKNOWN
| 202 -> RPL_TRACEHANDSHAKE
| 201 -> RPL_TRACECONNECTING
| 200 -> RPL_TRACELINK
| 395 -> RPL_NOUSERS
| 394 -> RPL_ENDOFUSERS
| 393 -> RPL_USERS
| 392 -> RPL_USERSSTART
| 391 -> RPL_TIME
| 385 -> RPL_NOTOPERANYMORE
| 384 -> RPL_MYPORTIS
| 383 -> RPL_YOURESERVICE
| 382 -> RPL_REHASHING
| 381 -> RPL_YOUREOPER
| 376 -> RPL_ENDOFMOTD
| 375 -> RPL_MOTDSTART
| 374 -> RPL_ENDOFINFO
| 373 -> RPL_INFOSTART
| 372 -> RPL_MOTD
| 371 -> RPL_INFO
| 368 -> RPL_ENDOFBANLIST
| 367 -> RPL_BANLIST
| 365 -> RPL_ENDOFLINKS
| 364 -> RPL_LINKS
| 363 -> RPL_CLOSEEND
| 362 -> RPL_CLOSING
| 361 -> RPL_KILLDONE
| 366 -> RPL_ENDOFNAMES
| 353 -> RPL_NAMREPLY
| 315 -> RPL_ENDOFWHO
| 352 -> RPL_WHOREPLY
| 351 -> RPL_VERSION
| 342 -> RPL_SUMMONING
| 341 -> RPL_INVITING
| 332 -> RPL_TOPIC
| 331 -> RPL_NOTOPIC
| 324 -> RPL_CHANNELMODEIS
| 323 -> RPL_LISTEND
| 322 -> RPL_LIST
| 321 -> RPL_LISTSTART
| 306 -> RPL_NOWAWAY
| 305 -> RPL_UNAWAY
| 304 -> RPL_TEXT
| 303 -> RPL_ISON
| 302 -> RPL_USERHOST
| 301 -> RPL_AWAY
| 300 -> RPL_NONE
| _ -> raise (Unknown_Reply n)
(* Bug 454 *)
type habert_a=
| A of habert_c
| B of habert_c
and habert_c= {lvar:int; lassoc: habert_c;lnb:int}
let habert=function
| (A {lnb=i}|B {lnb=i}) when i=0 -> 1
| A {lassoc=({lnb=j});lnb=i} -> 2
| _ -> 3
;;
let rec ex0 = {lvar=0 ; lnb=0 ; lassoc=ex1}
and ex1 = {lvar=1 ; lnb=1 ; lassoc=ex0} in
test "habert" habert (A ex0) 1 ;
test "habert" habert (B ex0) 1 ;
test "habert" habert (A ex1) 2 ;
test "habert" habert (B ex1) 3 ;
(* Problems with interval test in arithmetic mod 2^31, bug #359 *)
(* From manuel Fahndrich *)
type type_expr = [
| `TTuple of type_expr list
| `TConstr of type_expr list
| `TVar of string
| `TVariant of string list
| `TBlock of int
| `TCopy of type_expr
]
and recurs_type_expr = [
| `TTuple of type_expr list
| `TConstr of type_expr list
| `TVariant of string list
]
let rec maf te =
match te with
| `TCopy te -> 1
| `TVar _ -> 2
| `TBlock _ -> 2
| #recurs_type_expr as desc ->
let te =
(match desc with
`TTuple tl ->
4
| `TConstr tl ->
5
| `TVariant (row) ->
6
)
in
te
;;
let base = `TBlock 0
;;
test "maf" maf (`TCopy base) 1 ;
test "maf" maf (`TVar "test") 2 ;
test "maf" maf (`TBlock 0) 2 ;
test "maf" maf (`TTuple []) 4 ;
test "maf" maf (`TConstr []) 5 ;
test "maf" maf (`TVariant []) 6
;;
(* PR#3517
Using ``get_args'' in place or an ad-hoc ``matcher'' function for tuples.
Has made the compiler [3.05] to fail.
*)
type t_seb = Uin | Uout
;;
let rec seb = function
| ((i, Uin) | (i, Uout)), Uout -> 1
| ((j, Uin) | (j, Uout)), Uin -> 2
;;
test "seb" seb ((0,Uin),Uout) 1 ;
test "seb" seb ((0,Uout),Uin) 2 ;
()
;;
(* Talk with Jacques
- type 'b is still open ??
- better case generation, accept intervals of size 1 when ok_inter is
false (in Switch)
*)
type ('a, 'b) t_j = A of 'a | B of 'b * 'a | C
let f = function
| A (`A|`C) -> 0
| B (`B,`D) -> 1
| C -> 2
let g x = try f x with Match_failure _ -> 3
let _ =
test "jacques" g (A `A) 0 ;
test "jacques" g (A `C) 0 ;
test "jacques" g (B (`B,`D)) 1 ;
test "jacaues" g C 2 ;
(* test "jacques" g (B (`A,`D)) 3 ; (* type incorrect expected behavior ? *)*)
()
(*
Compilation bug, segfault, because of incorrect compilation
of unused match case .. -> "11"
*)
type t_l = A | B
let f = function
| _, _, _, _, _, _, _, _, _, _, _, _, _, B, _, _ -> "0"
| _, _, _, B, A, _, _, _, _, _, _, _, _, _, _, _ -> "1"
| _, _, _, B, _, A, _, _, A, _, _, _, _, _, _, _ -> "2"
| _, _, _, _, _, _, _, _, _, _, B, A, _, A, _, _ -> "3"
| _, _, _, _, _, _, _, B, _, _, _, _, B, _, A, A -> "4"
| A, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ -> "5"
| _, _, _, _, _, _, _, B, _, B, _, _, _, _, _, _ -> "6"
| _, B, _, _, _, _, _, _, _, _, _, _, _, _, _, _ -> "7"
| _, A, A, _, A, _, B, _, _, _, _, _, _, _, _, B -> "8"
| _, _, _, _, B, _, _, _, _, _, _, _, _, _, B, _ -> "9"
| _, _, _, _, _, _, _, _, _, _, _, B, _, _, _, _ -> "10"
| _, _, _, _, _, A, _, _, _, _, B, _, _, _, _, _ -> "11"
| B, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ -> "12"
| _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ -> "13"
let _ =
test "luc" f (B, A, A, A, A, A, A, A, A, A, A, B, A, A, A, A) "10" ;
test "luc" f (B, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A) "12" ;
()
(*
By Gilles Peskine, compilation raised some assert false i make_failactionneg
*)
type bg = [
| `False
| `True
]
type vg = [
| `A
| `B
| `U of int
| `V of int
]
type tg = {
v : vg;
x : bg;
}
let predg x = true
let rec gilles o = match o with
| {v = (`U data | `V data); x = `False} when predg o -> 1
| {v = (`A|`B) ; x = `False}
| {v = (`U _ | `V _); x = `False}
| {v = _ ; x = `True}
-> 2
(*
Match in trywith should always have a default case
*)
exception Found of string * int
exception Error of string
let lucexn e =
try
try raise e with Error msg -> msg
with Found (s,r) -> s^Int.to_string r
let () =
test "lucexn1" lucexn (Error "coucou") "coucou" ;
test "lucexn2" lucexn (Found ("int: ",0)) "int: 0" ;
()
(*
PR#5758: different representations of floats
*)
let pr5758 x str =
match (x, str) with
| (1. , "A") -> "Matched A"
| (1.0, "B") -> "Matched B"
| (1. , "C") -> "Matched C"
| result ->
match result with
| (1., "A") -> "Failed match A then later matched"
| _ -> "Failed twice"
;;
let () =
test "pr5758" (pr5758 1.) "A" "Matched A"
;;
(* TEST
include testing;
*)
include Testing
|