Commit | Line | Data |
---|---|---|
7f918cf1 CE |
1 | (* |
2 | Test various basis library functions. Quite incomplete. | |
3 | ||
4 | This program should terminate without raising an exception and without | |
5 | printing anything if no bugs are discovered. | |
6 | If a bug is discovered, "assertion failed:" will be printed, followed | |
7 | by a string uniquely identifying the bug. | |
8 | *) | |
9 | ||
10 | fun assert (msg,b) = | |
11 | ((*print (concat ["trying " ^ msg ^ "\n"]);*) | |
12 | if b then () | |
13 | else print ("assertion failed: " ^ msg ^ "\n")) | |
14 | ||
15 | (*------------------------------------------------------------------*) | |
16 | (* Array *) | |
17 | (*------------------------------------------------------------------*) | |
18 | ||
19 | local | |
20 | open Array | |
21 | fun extract (arr, s, l) = ArraySlice.vector (ArraySlice.slice (arr, s, l)) | |
22 | val copy = fn {src, si, len, dst, di} => | |
23 | ArraySlice.copy {src = ArraySlice.slice (src, si, len), | |
24 | dst = dst, di = di} | |
25 | fun appi f (arr, s, l) = | |
26 | ArraySlice.appi (fn (i,x) => f (i+s,x)) (ArraySlice.slice (arr, s, l)) | |
27 | ||
28 | val a0 = array (0,()) | |
29 | ||
30 | val a1 = array (100,1) | |
31 | ||
32 | val a2 = fromList [0,1,2] | |
33 | ||
34 | val a3 = tabulate (13, fn x => x) | |
35 | val _ = update (a3,11,9) | |
36 | ||
37 | val v1 = extract (a3, 0, NONE) | |
38 | ||
39 | val v2 = extract (a3, 1, SOME 3) | |
40 | ||
41 | val a4 = array (10,47) | |
42 | val _ = copy {src = a3, si = 10, len = SOME 3, | |
43 | dst = a4, di = 1} | |
44 | ||
45 | val a5 = array (100, 0) | |
46 | val _ = appi (fn (i,_) => update (a5,i,i)) (a5, 0, NONE) | |
47 | ||
48 | val _ = | |
49 | List.app assert | |
50 | [("Array.length 0", length a0 = 0), | |
51 | ("Array.length 1", length a1 = 100), | |
52 | ("Array.length 2", length a2 = 3), | |
53 | ("Array.length 3", length a3 = 13), | |
54 | ("Array.sub 1", sub (a1, 50) = 1), | |
55 | ("Array.sub 2", sub (a2, 2) = 2), | |
56 | ("Array.sub 3a", sub (a3, 10) = 10), | |
57 | ("Array.sub 3b", sub (a3, 11) = 9), | |
58 | ("Vector.length 1", Vector.length v1 = 13), | |
59 | ("Vector.length 2", Vector.length v2 = 3), | |
60 | ("Vector.sub 1", sub (a4, 1) = sub (a3, 10)), | |
61 | ("Vector.sub 2", sub (a4, 2) = sub (a3, 11)), | |
62 | ("Vector.sub 3", sub (a4, 3) = sub (a3, 12)), | |
63 | ("Vector.sub 4", sub (a5, 50) = 50)] | |
64 | ||
65 | fun swap (a,i,j) = | |
66 | let val t = sub (a,i) | |
67 | in update (a, i, sub (a,j)) ; | |
68 | update (a, j, t) | |
69 | end | |
70 | ||
71 | fun bubbleSort (a, op <) = | |
72 | let val n = length a | |
73 | fun loop i = | |
74 | if i = n | |
75 | then () | |
76 | else (let | |
77 | fun loop j = | |
78 | if j = 0 | |
79 | then () | |
80 | else if sub (a,j) < sub (a,j-1) | |
81 | then (swap (a,j,j-1) ; loop (j-1)) | |
82 | else () | |
83 | in loop i | |
84 | end ; | |
85 | loop (i+1)) | |
86 | in loop 0 | |
87 | end | |
88 | ||
89 | fun isSorted (a, op <=) = | |
90 | let | |
91 | val max = length a - 1 | |
92 | fun loop i = | |
93 | i = max orelse (sub (a, i) <= sub (a, i + 1) | |
94 | andalso loop (i + 1)) | |
95 | in loop 0 | |
96 | end | |
97 | ||
98 | val size = 2000 | |
99 | val a = tabulate (size, fn i => size - i) | |
100 | val _ = bubbleSort (a, op <) | |
101 | val _ = assert ("bubbleSort", isSorted (a, op <=)) | |
102 | ||
103 | in | |
104 | end |