1 (* Auxiliary functions for test cases
*)
5 fun check b
= if b
then "OK" else "WRONG";
6 fun check
' f
= (if f () then "OK" else "WRONG") handle _
=> "EXN";
8 fun range (from
, to
) p
=
11 (from
> to
) orelse (p from
) andalso (range (from
+1, to
) p
)
14 fun checkrange bounds
= check
o range bounds
;
16 fun tst0 s s
' = print (s ^
" \t" ^ s
' ^
"\n");
17 fun tst s b
= tst0
s (check b
);
18 fun tst
' s f
= tst0
s (check
' f
);
20 fun tstrange s bounds
= (tst s
) o range bounds
24 PS
1995-02-25, 1997-03-07
27 (*KILL
05/11/1997 11:00. tho
.:
31 val _
= print
"\nFile listpair.sml: Testing structure ListPair...\n";
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)]
40 | take
n (x
:: xr
) = x
:: take (n
-1) xr
43 val 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
);
49 val 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
)));
53 val test2b
= tst
"test2b" (ab
= zip(unzip ab
));
55 val test3a
= tst
"test3a" (map (fn (x
, y
) => x
+ y
) (a
, b
) =
56 List.map (fn (x
,y
) => x
+ y
) (zip(a
, b
)));
60 fun h
[] r
= r |
h (x
::xr
) r
= h
xr (r
+r
+x
): int;
61 val isum
= h (take (length b
) a
) 0
63 fun reset () = v
:= 0;
64 fun incrv i
= v
:= 2 * !v
+ i
;
65 fun checkv () = tst
"checkv" (!v
= isum
);
68 val test3b
= (reset (); map (incrv
o #
1) (a
, b
) seq (); checkv());
70 val test4
= (reset (); app (incrv
o #
1) (a
, b
); checkv());
72 val test5a
= tst
"test5a" (all (fn _
=> false) (a
, [])
73 andalso not (exists (fn _
=> true) ([], b
)));
75 val test5b
= tst
"test5b" (exists (fn (x
, y
) => x
= 3) (a
, b
)
76 andalso all (fn (x
, y
) => y
<= 50) (a
, b
));
78 val 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
));
83 val test5d
= (reset(); all (fn (x
,y
) => (incrv x
; true)) (a
, b
) seq ();
85 val test5e
= (reset(); exists (fn (x
,y
) => (incrv x
; false)) (a
, b
) seq ();
89 fun foldrchk 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
=
94 List.foldl (fn ((x
, y
), r
) => f(x
, y
, r
)) e (zip(xs
, ys
))
96 val 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) [] []);
102 val 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) [] []);