Import Debian changes 20180207-1
[hcoop/debian/mlton.git] / regression / listpair.sml
CommitLineData
7f918cf1
CE
1(* Auxiliary functions for test cases *)
2
3infix 1 seq
4fun e1 seq e2 = e2;
5fun check b = if b then "OK" else "WRONG";
6fun check' f = (if f () then "OK" else "WRONG") handle _ => "EXN";
7
8fun range (from, to) p =
9 let open Int
10 in
11 (from > to) orelse (p from) andalso (range (from+1, to) p)
12 end;
13
14fun checkrange bounds = check o range bounds;
15
16fun tst0 s s' = print (s ^ " \t" ^ s' ^ "\n");
17fun tst s b = tst0 s (check b);
18fun tst' s f = tst0 s (check' f);
19
20fun tstrange s bounds = (tst s) o range bounds
21
22
23(* test/listpair.sml
24 PS 1995-02-25, 1997-03-07
25*)
26
27(*KILL 05/11/1997 11:00. tho.:
28use "auxil.sml";
29*)
30
31val _ = print "\nFile listpair.sml: Testing structure ListPair...\n";
32
33local
34 open ListPair
35 val a = [1, 2, 3, 4, 5, 6]
36 val b = [10, 40, 50, 50]
37 val ab = [(1, 10), (2, 40), (3, 50), (4, 50)]
38 fun take 0 xs = []
39 | take n [] = []
40 | take n (x :: xr) = x :: take (n-1) xr
41in
42
43val test1 = tst "test1" (zip([], []) = []
44 andalso zip ([], a) = []
45 andalso zip(a, []) = []
46 andalso zip(a, b) = ab
47 andalso zip(b, a) = List.map (fn (x,y) => (y,x)) ab);
48
49val test2a = tst "test2a" (([], []) = unzip []
50 andalso (a, a) = unzip(zip(a,a))
51 andalso (take (length b) a, b) = unzip(zip(a, b))
52 andalso (b, take (length b) a) = unzip(zip(b, a)));
53val test2b = tst "test2b" (ab = zip(unzip ab));
54
55val test3a = tst "test3a" (map (fn (x, y) => x + y) (a, b) =
56 List.map (fn (x,y) => x + y) (zip(a, b)));
57
58local
59 val v = ref 0
60 fun h [] r = r | h (x::xr) r = h xr (r+r+x): int;
61 val isum = h (take (length b) a) 0
62in
63 fun reset () = v := 0;
64 fun incrv i = v := 2 * !v + i;
65 fun checkv () = tst "checkv" (!v = isum);
66end;
67
68val test3b = (reset (); map (incrv o #1) (a, b) seq (); checkv());
69
70val test4 = (reset (); app (incrv o #1) (a, b); checkv());
71
72val test5a = tst "test5a" (all (fn _ => false) (a, [])
73 andalso not (exists (fn _ => true) ([], b)));
74
75val test5b = tst "test5b" (exists (fn (x, y) => x = 3) (a, b)
76 andalso all (fn (x, y) => y <= 50) (a, b));
77
78val test5c = tst "test5c" (not (exists (fn (x, y) => x = 5) (a, b))
79 andalso not (exists (fn (x, y) => y = 5) (b, a))
80 andalso all (fn (x, y) => x <> 6) (a, b)
81 andalso all (fn (x, y) => y <> 6) (b, a));
82
83val test5d = (reset(); all (fn (x,y) => (incrv x; true)) (a, b) seq ();
84 checkv());
85val test5e = (reset(); exists (fn (x,y) => (incrv x; false)) (a, b) seq ();
86 checkv());
87
88local
89 fun foldrchk f e xs ys =
90 foldr f e (xs, ys) =
91 List.foldr (fn ((x, y), r) => f(x, y, r)) e (zip(xs, ys))
92 fun foldlchk f e xs ys =
93 foldl f e (xs, ys) =
94 List.foldl (fn ((x, y), r) => f(x, y, r)) e (zip(xs, ys))
95in
96val test6 = tst' "test6" (fn _ =>
97 foldrchk (fn (x, y, (r1, r2)) => (x-r1, y div r2)) (0, 10) a b
98 andalso foldrchk (fn (x, y, (r1, r2)) => (x div r1, y div r2)) (0, 0) [] b
99 andalso foldrchk (fn (x, y, (r1, r2)) => (x div r1, y div r2)) (0, 0) a []
100 andalso foldrchk (fn (x, y, (r1, r2)) => (x div r1, y div r2)) (0, 0) [] []);
101
102val test7 = tst' "test7" (fn _ =>
103 foldlchk (fn (x, y, (r1, r2)) => (x-r1, y div r2)) (0, 10) a b
104 andalso foldlchk (fn (x, y, (r1, r2)) => (x div r1, y div r2)) (0, 0) [] b
105 andalso foldlchk (fn (x, y, (r1, r2)) => (x div r1, y div r2)) (0, 0) a []
106 andalso foldlchk (fn (x, y, (r1, r2)) => (x div r1, y div r2)) (0, 0) [] []);
107end
108
109end;
110