File: word8array.sml

package info (click to toggle)
mlton 20130715-3
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 60,900 kB
  • ctags: 69,386
  • sloc: xml: 34,418; ansic: 17,399; lisp: 2,879; makefile: 1,605; sh: 1,254; pascal: 256; python: 143; asm: 97
file content (188 lines) | stat: -rw-r--r-- 8,320 bytes parent folder | download | duplicates (7)
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
(* Auxiliary functions for test cases *)

infix 1 seq
fun e1 seq e2 = e2;
fun check b = if b then "OK" else "WRONG";
fun check' f = (if f () then "OK" else "WRONG") handle _ => "EXN";

fun range (from, to) p = 
    let open Int 
    in
        (from > to) orelse (p from) andalso (range (from+1, to) p)
    end;

fun checkrange bounds = check o range bounds;

fun tst0 s s' = print (s ^ "    \t" ^ s' ^ "\n");
fun tst  s b = tst0 s (check  b);
fun tst' s f = tst0 s (check' f);

fun tstrange s bounds = (tst s) o range bounds  

(* test/word8array.sml -- some test cases for Word8Array 
   PS 1994-12-21, 1995-05-11 *)

(*KILL 05/11/1997 11:05. tho.:
use "auxil.sml";
*)

val _ = print "Testing Word8Array...\n";

local
    open Word8Array 
    infix 9 sub;
    val array0 = fromList [];
    val copy = fn {src, si, len, dst, di} =>
      Word8ArraySlice.copy {src = Word8ArraySlice.slice (src, si, len),
                            dst = dst, di = di}
    val extract = fn (a, i, sz) =>
      Word8ArraySlice.vector (Word8ArraySlice.slice (a, i, sz))
in

val i2w = Word8.fromInt;

val w127 = i2w 127;

val a = fromList (map i2w [0,1,2,3,4,5,6]);
val b = fromList (map i2w [44,55,66]);
val c = fromList (map i2w [0,1,2,3,4,5,6]);

val test1:unit = tst' "test1" (fn () => a<>c);
val test2:unit = tst' "test2" 
  (fn () => 
           array(0, w127) <> array0
           andalso array(0, w127) <> tabulate(0, fn _ => w127)
           andalso tabulate(0, fn _ => w127) <> fromList []
           andalso array(0, w127) <> array(0, w127)
           andalso tabulate(0, fn _ => w127) <> tabulate(0, fn _ => w127)
           andalso fromList [] <> fromList [])

val d = tabulate(100, fn i => i2w (i mod 7))

val test3:unit = tst' "test3" (fn () => d sub 27 = i2w 6)

val test4a:unit = tst0 "test4a" ((tabulate(maxLen+1, i2w) seq "WRONG")
                            handle Overflow => "OK" | Size => "OK" | _ => "WRONG")

val test4b:unit = tst0 "test4b" ((tabulate(~1, i2w)       seq "WRONG")
                            handle Size => "OK" | _ => "WRONG")

val test4c:unit = 
    tst' "test4c" (fn () => length (tabulate(0, fn i => i2w (i div 0))) = 0);

val test5a:unit = tst' "test5a" (fn () => length (fromList []) = 0 andalso length a = 7);
val test5b:unit = tst' "test5b" (fn () => length array0 = 0);

val test6a:unit = tst0 "test6a" ((c sub ~1 seq "WRONG") handle Subscript => "OK" | _ => "WRONG")
val test6b:unit = tst0 "test6b" ((c sub 7  seq "WRONG") handle Subscript => "OK" | _ => "WRONG")
val test6c:unit = tst' "test6c" (fn () => c sub 0 = i2w 0);

val e = array(203, i2w 0);
val _ = (copy{src=d, si=0, dst=e, di=0,        len=NONE}; 
         copy{src=b, si=0, dst=e, di=length d, len=NONE};
         copy{src=d, si=0, dst=e, di=length d + length b, len=NONE});
         
fun a2v a = extract(a, 0, NONE);
val ev = Word8Vector.concat [a2v d, a2v b, a2v d];

val test7:unit = tst' "test7" (fn () => length e = 203);

val test8a:unit = tst0 "test8a" ((update(e, ~1, w127); "WRONG")
                            handle Subscript => "OK" | _ => "WRONG")
val test8b:unit = tst0 "test8b" ((update(e, length e, w127); "WRONG")
                            handle Subscript => "OK" | _ => "WRONG")

val f = extract (e, 100, SOME 3);

fun equal (v, v') =
   let
      val n = Word8Vector.length v
      val n' = Word8Vector.length v'
      fun loop i =
         i = n
         orelse (Word8Vector.sub (v, i) = Word8Vector.sub (v', i)
                 andalso loop (i + 1))
   in
      n = n' andalso loop 0
   end
   
val test9:unit = tst' "test9" (fn () => equal (f, a2v b));

val test9a:unit = tst' "test9a" (fn () => equal (ev, extract(e, 0, NONE))
                                 andalso equal (ev, extract(e, 0, SOME (length e))));
val test9b:unit = 
    tst' "test9b" (fn () => equal (Word8Vector.fromList [],
                                   extract(e, 100, SOME 0)));
val test9c:unit = tst0 "test9c" ((extract(e, ~1, SOME (length e))  seq "WRONG") 
                            handle Subscript => "OK" | _ => "WRONG")
val test9d:unit = tst0 "test9d" ((extract(e, length e+1, SOME 0) seq "WRONG") 
                            handle Subscript => "OK" | _ => "WRONG")
val test9e:unit = tst0 "test9e" ((extract(e, 0, SOME (length e+1)) seq "WRONG") 
                            handle Subscript => "OK" | _ => "WRONG")
val test9f:unit = tst0 "test9f" ((extract(e, 20, SOME ~1)        seq "WRONG") 
                            handle Subscript => "OK" | _ => "WRONG")
val test9g:unit = tst0 "test9g" ((extract(e, ~1, NONE)  seq "WRONG") 
                            handle Subscript => "OK" | _ => "WRONG")
val test9h:unit = tst0 "test9h" ((extract(e, length e+1, NONE) seq "WRONG") 
                            handle Subscript => "OK" | _ => "WRONG")
val test9i:unit = 
    tst' "test9i" (fn () => equal (a2v (fromList []),
                                   extract(e, length e, SOME 0))
                   andalso equal (a2v (fromList []),
                                  extract(e, length e, NONE)));

val _ = copy{src=e, si=0, dst=e, di=0, len=NONE};
val g = array(203, w127);
val _ = copy{src=e, si=0, dst=g, di=0, len=NONE};

val test10a:unit = tst' "test10a" (fn () => equal (ev, extract(e, 0, NONE))
                              andalso equal (ev, extract(e, 0, SOME (length e))));
val test10b:unit = tst' "test10b" (fn () => equal (ev, extract(g, 0, NONE))
                              andalso equal (ev, extract(g, 0, SOME (length g))));

val _ = copy{src=g, si=203, dst=g, di=0, len=SOME 0};
val test10c:unit = tst' "test10c" (fn () => equal (ev, extract(g, 0, NONE)));

val _ = copy{src=g, si=0, dst=g, di=203, len=SOME 0};
val test10d:unit = tst' "test10d" (fn () => equal (ev, extract(g, 0, NONE)));

val _ = copy{src=g, si=0, dst=g, di=1, len=SOME (length g-1)};
val test10e:unit = tst' "test10e" (fn () => equal (a2v b, extract(g, 101, SOME 3)));

val _ = copy{src=g, si=1, dst=g, di=0, len=SOME(length g-1)};
val test10f:unit = tst' "test10f" (fn () => equal (a2v b, extract(g, 100, SOME 3)));

val _ = copy{src=g, si=202, dst=g, di=202, len=SOME 1};
val test10g:unit = tst' "test10g" (fn () => g sub 202 = i2w ((202-1-103) mod 7));
val test10h:unit = tst' "test10h" (fn () =>
                              (copy{src=array0, si=0, dst=array0, di=0, len=NONE}; 
                               array0 <> array(0, w127)));
val test10i:unit = tst' "test10i" (fn () =>
                               (copy{src=array0, si=0, dst=array0, di=0, len=SOME 0}; 
                                array0 <> array(0, w127)));

val test11a:unit = tst0 "test11a" ((copy{src=g, si= ~1, dst=g, di=0, len=NONE}; "WRONG") 
                              handle Subscript => "OK" | _ => "WRONG")
val test11b:unit = tst0 "test11b" ((copy{src=g, si=0, dst=g, di= ~1, len=NONE}; "WRONG") 
                              handle Subscript => "OK" | _ => "WRONG")
val test11c:unit = tst0 "test11c" ((copy{src=g, si=1, dst=g, di=0, len=NONE}; "OK") 
                              handle _ => "WRONG")
val test11d:unit = tst0 "test11d" ((copy{src=g, si=0, dst=g, di=1, len=NONE}; "WRONG") 
                              handle Subscript => "OK" | _ => "WRONG")
val test11e:unit = tst0 "test11e" ((copy{src=g, si=203, dst=g, di=0, len=NONE}; "OK") 
                              handle _ => "WRONG")

val test11f:unit = tst0 "test11f" ((copy{src=g, si= ~1, dst=g, di=0, len=SOME (length g)}; "WRONG") 
                              handle Subscript => "OK" | _ => "WRONG")
val test11g:unit = tst0 "test11g" ((copy{src=g, si=0, dst=g, di= ~1, len=SOME (length g)}; "WRONG") 
                              handle Subscript => "OK" | _ => "WRONG")
val test11h:unit = tst0 "test11h" ((copy{src=g, si=1, dst=g, di=0, len=SOME (length g)}; "WRONG") 
                              handle Subscript => "OK" | _ => "WRONG")
val test11i:unit = tst0 "test11i" ((copy{src=g, si=0, dst=g, di=1, len=SOME (length g)}; "WRONG") 
                              handle Subscript => "OK" | _ => "WRONG")
val test11j:unit = tst0 "test11j" ((copy{src=g, si=0, dst=g, di=0, len=SOME (length g+1)}; "WRONG") 
                              handle Subscript => "OK" | _ => "WRONG")
val test11k:unit = tst0 "test11k" ((copy{src=g, si=203, dst=g, di=0, len=SOME 1}; "WRONG") 
                              handle Subscript => "OK" | _ => "WRONG")

end;