1 USING: arrays assocs byte-arrays generic.single kernel make math
2 math.functions math.order math.parser math.vectors sbufs
3 sequences sequences.private strings tools.test vectors ;
6 { "empty" } [ { } [ "empty" ] [ "not empty" ] if-empty ] unit-test
7 { { 1 } "not empty" } [ { 1 } [ "empty" ] [ "not empty" ] if-empty ] unit-test
9 { V{ 1 2 3 4 } } [ 1 5 dup <iota> <slice> >vector ] unit-test
10 { 3 } [ 1 4 dup <iota> <slice> length ] unit-test
11 { 2 } [ 1 3 { 1 2 3 4 } <slice> length ] unit-test
12 { V{ 2 3 } } [ 1 3 { 1 2 3 4 } <slice> >vector ] unit-test
13 { V{ 4 5 } } [ { 1 2 3 4 5 } 2 tail-slice* >vector ] unit-test
14 { V{ 3 4 } } [ 2 4 1 10 dup <iota> <slice> subseq >vector ] unit-test
15 { V{ 3 4 } } [ 0 2 2 4 1 10 dup <iota> <slice> <slice> subseq >vector ] unit-test
16 [ 0 10 "hello" <slice> ] must-fail
17 [ -10 3 "hello" <slice> ] must-fail
18 [ 2 1 "hello" <slice> ] must-fail
20 { "cba" } [ "abcdef" 3 head-slice reverse ] unit-test
23 [ 1 2 3 4 5 6 7 ] dup rest-slice
24 [ 1 [ * ] reduce ] bi@
28 [ 1 2 3 4 5 6 7 ] dup rest-slice
29 [ 1 [ [ * ] [ + ] bi* ] reduce-index ] bi@
33 { 10 21 32 } { 500 600 700 } [ - sq ] [ - ] 2map-reduce
36 { 5040 { 1 1 2 6 24 120 720 } }
37 [ { 1 2 3 4 5 6 7 } 1 [ * ] accumulate ] unit-test
40 [ B{ 2 2 4 4 } 1 [ * ] accumulate ] unit-test
42 { 5040 { 1 1 2 6 24 120 720 } }
43 [ { 1 2 3 4 5 6 7 } 1 [ * ] accumulate! ] unit-test
46 [ { 1 2 3 4 5 6 7 } dup 1 [ * ] accumulate! nip eq? ] unit-test
48 { { 1 2 6 24 120 720 5040 } }
49 [ { 1 2 3 4 5 6 7 } 1 [ * ] accumulate* ] unit-test
52 [ B{ 2 2 4 4 } 1 [ * ] accumulate* ] unit-test
54 { { 1 2 6 24 120 720 5040 } }
55 [ { 1 2 3 4 5 6 7 } 1 [ * ] accumulate*! ] unit-test
58 [ { 1 2 3 4 5 6 7 } dup 1 [ * ] accumulate*! eq? ] unit-test
60 { f f } [ [ ] [ ] find ] unit-test
61 { 0 1 } [ [ 1 ] [ ] find ] unit-test
62 { 1 "world" } [ [ "hello" "world" ] [ "world" = ] find ] unit-test
63 { 2 3 } [ [ 1 2 3 ] [ 2 > ] find ] unit-test
64 { f f } [ [ 1 2 3 ] [ 10 > ] find ] unit-test
67 [ "hello world" "aeiou" [ member? ] curry find ] unit-test
70 [ 3 "hello world" "aeiou" [ member? ] curry find-from ] unit-test
72 { f f } [ "abcd" [ 10 > nip ] find-index ] unit-test
73 { f f } [ "abcd" [ drop CHAR: e = ] find-index ] unit-test
74 { 3 CHAR: d } [ "abcdefg" [ 3 = nip ] find-index ] unit-test
75 { 3 CHAR: d } [ "abcdefg" [ drop CHAR: d = ] find-index ] unit-test
77 { 0 CHAR: a } [ 0 "abcdef" [ drop CHAR: a >= ] find-index-from ] unit-test
78 { 1 CHAR: b } [ 0 "abcdef" [ drop CHAR: a > ] find-index-from ] unit-test
79 { 2 CHAR: c } [ 1 "abcdef" [ drop CHAR: b > ] find-index-from ] unit-test
81 { f } [ 3 [ ] member? ] unit-test
82 { f } [ 3 [ 1 2 ] member? ] unit-test
83 { t } [ 1 [ 1 2 ] member? ] unit-test
84 { t } [ 2 [ 1 2 ] member? ] unit-test
87 [ [ "hello" "world" ] [ second ] keep member-eq? ] unit-test
89 { 4 } [ CHAR: x "tuvwxyz" >vector index ] unit-test
91 { f } [ CHAR: x 5 "tuvwxyz" >vector index-from ] unit-test
93 { f } [ CHAR: a 0 "tuvwxyz" >vector index-from ] unit-test
95 { f } [ [ "Hello" { } 0.75 ] [ string? ] all? ] unit-test
96 { t } [ [ ] [ ] all? ] unit-test
97 { t } [ [ "hi" t 0.5 ] [ ] all? ] unit-test
99 { [ 1 2 3 ] } [ [ 1 4 2 5 3 6 ] [ 4 < ] filter ] unit-test
100 { { 4 2 6 } } [ { 1 4 2 5 3 6 } [ 2 mod 0 = ] filter ] unit-test
102 { [ 3 ] } [ [ 1 2 3 ] 2 [ swap < ] curry filter ] unit-test
104 { V{ 1 2 3 } } [ V{ 1 4 2 5 3 6 } clone [ 4 < ] filter! ] unit-test
105 { V{ 4 2 6 } } [ V{ 1 4 2 5 3 6 } clone [ 2 mod 0 = ] filter! ] unit-test
107 { V{ 3 } } [ V{ 1 2 3 } clone 2 [ swap < ] curry filter! ] unit-test
108 { 4 V{ 1 2 3 } } [ 4 V{ 1 4 2 5 3 6 } [ dupd > ] filter! ] unit-test
110 { "hello world how are you" }
111 [ { "hello" "world" "how" "are" "you" } " " join ] unit-test
113 { "hello world how are you" }
114 [ { "hello" "world" "how" "are" "you" } " " "" join-as ] unit-test
116 { "" } [ { } "" join ] unit-test
118 { "" } [ { } "" "" join-as ] unit-test
120 { { } } [ { } flip ] unit-test
122 { { "b" "e" } } [ 1 { { "a" "b" "c" } { "d" "e" "f" } } flip nth ] unit-test
124 { { { 1 4 } { 2 5 } { 3 6 } } }
125 [ { { 1 2 3 } { 4 5 6 } } flip ] unit-test
127 { [ 2 3 4 ] } [ [ 1 2 3 ] 1 [ + ] curry map ] unit-test
129 { 1 } [ 0 [ 1 2 ] nth ] unit-test
130 { 2 } [ 1 [ 1 2 ] nth ] unit-test
132 { [ ] } [ [ ] [ ] append ] unit-test
133 { [ 1 ] } [ [ 1 ] [ ] append ] unit-test
134 { [ 2 ] } [ [ ] [ 2 ] append ] unit-test
135 { [ 1 2 3 4 ] } [ [ 1 2 3 ] [ 4 ] append ] unit-test
136 { [ 1 2 3 4 ] } [ [ 1 2 3 ] { 4 } append ] unit-test
138 [ "a" -1 append ] must-fail
139 [ -1 "a" append ] must-fail
141 { t } [ B{ 0 } { 1 } append byte-array? ] unit-test
142 { t } [ B{ 0 } { 1 } prepend byte-array? ] unit-test
144 { "0123456789" } [ 58 <iota> [ 48 < ] "" reject-as ] unit-test
146 { [ ] } [ 1 [ ] remove ] unit-test
147 { [ ] } [ 1 [ 1 ] remove ] unit-test
148 { [ 3 1 1 ] } [ 2 [ 3 2 1 2 1 ] remove ] unit-test
150 { [ ] } [ [ ] reverse ] unit-test
151 { [ 1 ] } [ [ 1 ] reverse ] unit-test
152 { [ 3 2 1 ] } [ [ 1 2 3 ] reverse ] unit-test
154 { f } [ f 0 head ] unit-test
155 { [ ] } [ [ 1 ] 0 head ] unit-test
156 { [ 1 2 3 ] } [ [ 1 2 3 4 ] 3 head ] unit-test
157 { [ ] } [ [ 1 2 3 ] 3 tail ] unit-test
158 { [ 3 ] } [ [ 1 2 3 ] 2 tail ] unit-test
160 { "blah" } [ "blahxx" 2 head* ] unit-test
162 { "xx" } [ "blahxx" 2 tail* ] unit-test
164 { t } [ "xxfoo" 2 head-slice "xxbar" 2 head-slice = ] unit-test
165 { t } [ "xxfoo" 2 head-slice "xxbar" 2 head-slice [ hashcode ] same? ] unit-test
167 { t } [ "xxfoo" 2 head-slice SBUF" barxx" 2 tail-slice* = ] unit-test
168 { t } [ "xxfoo" 2 head-slice SBUF" barxx" 2 tail-slice* [ hashcode ] same? ] unit-test
170 { t } [ [ 1 2 3 ] [ 1 2 3 ] sequence= ] unit-test
171 { t } [ [ 1 2 3 ] { 1 2 3 } sequence= ] unit-test
172 { t } [ { 1 2 3 } [ 1 2 3 ] sequence= ] unit-test
173 { f } [ [ ] [ 1 2 3 ] sequence= ] unit-test
175 { { 1 3 2 4 } } [ { 1 2 3 4 } clone 1 2 pick exchange ] unit-test
177 { { "" "a" "aa" "aaa" } }
178 [ 4 [ CHAR: a <string> ] map-integers ]
181 { V{ 1 3 5 7 9 } } [ 10 <iota> >vector [ even? ] reject! ] unit-test
183 { V{ } } [ "f" V{ } clone remove! ] unit-test
184 { V{ } } [ "f" V{ "f" } clone remove! ] unit-test
185 { V{ } } [ "f" V{ "f" "f" } clone remove! ] unit-test
186 { V{ "x" } } [ "f" V{ "f" "x" "f" } clone remove! ] unit-test
187 { V{ "y" "x" } } [ "f" V{ "y" "f" "x" "f" } clone remove! ] unit-test
189 { V{ 0 1 4 5 } } [ 6 <iota> >vector 2 4 pick delete-slice ] unit-test
191 [ 6 >vector 2 8 pick delete-slice ] must-fail
193 { V{ } } [ 6 <iota> >vector 0 6 pick delete-slice ] unit-test
195 { { 1 2 "a" "b" 5 6 7 } } [
196 { "a" "b" } 2 4 { 1 2 3 4 5 6 7 }
200 { { 1 2 "a" "b" 6 7 } } [
201 { "a" "b" } 2 5 { 1 2 3 4 5 6 7 }
205 { { 1 2 "a" "b" 4 5 6 7 } } [
206 { "a" "b" } 2 3 { 1 2 3 4 5 6 7 }
210 { { 1 2 3 4 5 6 7 "a" "b" } } [
211 { "a" "b" } 7 7 { 1 2 3 4 5 6 7 }
216 { "a" } 0 2 { 1 2 3 } replace-slice
219 { { 1 4 9 } } [ { 1 2 3 } clone [ sq ] map! ] unit-test
221 { 5 } [ 1 >bignum { 1 5 7 } nth-unsafe ] unit-test
222 { 5 } [ 1 >bignum { 1 5 7 } nth-unsafe ] unit-test
223 { 5 } [ 1 >bignum "\u000001\u000005\u000007" nth-unsafe ] unit-test
225 { SBUF" before&after" } [
226 "&" 6 11 SBUF" before and after" replace-slice
229 { 3 "a" } [ { "a" "b" "c" "a" "d" } [ "a" = ] find-last ] unit-test
231 { f f } [ 100 { 1 2 3 } [ 1 = ] find-from ] unit-test
232 { f f } [ 100 { 1 2 3 } [ 1 = ] find-last-from ] unit-test
233 { f f } [ -1 { 1 2 3 } [ 1 = ] find-from ] unit-test
235 { 0 } [ { "a" "b" "c" } { "A" "B" "C" } mismatch ] unit-test
237 { 1 } [ { "a" "b" "c" } { "a" "B" "C" } mismatch ] unit-test
239 { f } [ { "a" "b" "c" } { "a" "b" "c" } mismatch ] unit-test
241 { V{ } V{ } } [ { "a" "b" } { "a" "b" } drop-prefix [ >vector ] bi@ ] unit-test
243 { V{ "C" } V{ "c" } } [ { "a" "b" "C" } { "a" "b" "c" } drop-prefix [ >vector ] bi@ ] unit-test
245 [ -1 1 "abc" <slice> ] must-fail
247 { V{ "a" "b" } V{ } } [ { "X" "a" "b" } { "X" } drop-prefix [ >vector ] bi@ ] unit-test
249 [ 0.5 { 1 2 3 } nth ] [ no-method? ] must-fail-with
250 [ 0.5 "asdfasdf" nth ] [ no-method? ] must-fail-with
253 { "ihbye" } [ "hi" <reversed> "bye" append ] unit-test
255 { t } [ "hi" <reversed> SBUF" hi" <reversed> = ] unit-test
257 { t } [ "hi" <reversed> SBUF" hi" <reversed> = ] unit-test
259 { t } [ "hi" <reversed> SBUF" hi" <reversed> [ hashcode ] same? ] unit-test
261 [ -10 "hi" "bye" copy ] must-fail
262 [ 10 "hi" "bye" copy ] must-fail
265 3 V{ 1 2 3 4 5 6 } clone remove-nth!
268 ! erg's random tester found this one
269 { SBUF" 12341234" } [
270 9 <sbuf> dup "1234" swap push-all dup dup swap push-all
273 { f } [ f V{ } like f V{ } like eq? ] unit-test
275 { V{ f f f } } [ 3 V{ } new-sequence ] unit-test
276 { SBUF" \0\0\0" } [ 3 SBUF" " new-sequence ] unit-test
278 { 0 } [ f length ] unit-test
279 [ f first ] must-fail
280 { 3 } [ 3 10 <iota> nth ] unit-test
281 { 3 } [ 3 10 <iota> nth-unsafe ] unit-test
282 [ -3 10 <iota> nth ] must-fail
283 [ 11 10 <iota> nth ] must-fail
285 { f } [ f ?first ] unit-test
286 { f } [ { } ?first ] unit-test
287 { 0 } [ 10 <iota> ?first ] unit-test
289 { f } [ f ?last ] unit-test
290 { f } [ { } ?last ] unit-test
291 { 9 } [ 10 <iota> ?last ] unit-test
293 [ -1/0. 0 remove-nth! ] must-fail
294 { "" } [ "" [ CHAR: \s = ] trim ] unit-test
295 { "" } [ "" [ CHAR: \s = ] trim-head ] unit-test
296 { "" } [ "" [ CHAR: \s = ] trim-tail ] unit-test
297 { "" } [ " " [ CHAR: \s = ] trim-head ] unit-test
298 { "" } [ " " [ CHAR: \s = ] trim-tail ] unit-test
299 { "asdf" } [ " asdf " [ CHAR: \s = ] trim ] unit-test
300 { "asdf " } [ " asdf " [ CHAR: \s = ] trim-head ] unit-test
301 { " asdf" } [ " asdf " [ CHAR: \s = ] trim-tail ] unit-test
303 { 328350 } [ 100 <iota> [ sq ] map-sum ] unit-test
305 { 5 } [ { 1 f 3 f 5 f 7 f 9 f } count ] unit-test
307 { 50 } [ 100 <iota> [ even? ] count-by ] unit-test
308 { 50 } [ 100 <iota> [ odd? ] count-by ] unit-test
310 { { "b" "d" } } [ { 1 3 } { "a" "b" "c" "d" } nths ] unit-test
311 { { "a" "b" "c" "d" } } [ { 0 1 2 3 } { "a" "b" "c" "d" } nths ] unit-test
312 { { "d" "c" "b" "a" } } [ { 3 2 1 0 } { "a" "b" "c" "d" } nths ] unit-test
313 { { "d" "a" "b" "c" } } [ { 3 0 1 2 } { "a" "b" "c" "d" } nths ] unit-test
315 { "dac" } [ { 3 0 2 } "abcd" nths ] unit-test
317 TUPLE: bogus-hashcode ;
319 M: bogus-hashcode hashcode* 2drop 0 >bignum ;
321 { 0 } [ { T{ bogus-hashcode } } hashcode ] unit-test
323 { { 2 4 6 } { 1 3 5 7 } } [ { 1 2 3 4 5 6 7 } [ even? ] partition ] unit-test
325 { { 1 3 7 } } [ 2 { 1 3 5 7 } remove-nth ] unit-test
327 { { 1 3 "X" 5 7 } } [ "X" 2 { 1 3 5 7 } insert-nth ] unit-test
329 { V{ 0 2 } } [ "a" { "a" "b" "a" } indices ] unit-test
331 { "a,b" } [ "a" "b" "," glue ] unit-test
332 { "(abc)" } [ "abc" "(" ")" surround ] unit-test
335 "HELLO" { -1 -1 -1 -1 -1 } { 2 2 2 2 2 2 }
339 { 3 1 } [ [ 3array ] 3map ] must-infer-as
341 { 3 0 } [ [ 3drop ] 3each ] must-infer-as
343 { V{ 0 3 } } [ "A" { "A" "B" "C" "A" "D" } indices ] unit-test
345 [ "asdf" <iota> ] must-fail
346 [ -1 <iota> ] must-fail
347 { T{ iota { n 10 } } } [ 10 <iota> ] unit-test
348 { 0 } [ 10 <iota> first ] unit-test
349 { 0 } [ 0 <iota> sum ] unit-test
350 { 0 } [ 1 <iota> sum ] unit-test
351 { 10 } [ 5 <iota> sum ] unit-test
352 { 15 } [ 6 <iota> sum ] unit-test
355 { 1 2 3 4 5 6 7 8 } [ H{ { 3 "hi" } } at ] map-find
359 { 1 2 3 4 5 6 7 8 } [ H{ { 11 "hi" } } at ] map-find
362 { { "a" 1 "b" 1 "c" } }
363 [ 1 { "a" "b" "c" } [ [ dup , ] [ , ] interleave drop ] { } make ] unit-test
365 { t } [ 0 array-capacity? ] unit-test
366 { f } [ -1 array-capacity? ] unit-test
368 { +lt+ } [ { 0 0 0 } { 1 1 1 } <=> ] unit-test
369 { +lt+ } [ { 0 0 0 } { 0 1 1 } <=> ] unit-test
370 { +lt+ } [ { 0 0 0 } { 0 0 0 0 } <=> ] unit-test
371 { +gt+ } [ { 1 1 1 } { 0 0 0 } <=> ] unit-test
372 { +gt+ } [ { 0 1 1 } { 0 0 0 } <=> ] unit-test
373 { +gt+ } [ { 0 0 0 0 } { 0 0 0 } <=> ] unit-test
374 { +eq+ } [ { } { } <=> ] unit-test
375 { +eq+ } [ { 1 2 3 } { 1 2 3 } <=> ] unit-test
377 { { { { 1 "a" } { 1 "b" } } { { 2 "a" } { 2 "b" } } } }
378 [ { 1 2 } { "a" "b" } cartesian-product ] unit-test
380 { 2 4 } [ { 1 2 3 } { 4 5 6 } [ [ even? ] both? ] cartesian-find ] unit-test
381 { f f } [ { 1 2 3 } { 4 5 6 } [ [ 10 > ] both? ] cartesian-find ] unit-test
383 [ { } [ string>digits sum ] [ + ] map-reduce ] must-infer
384 [ { } [ ] [ + ] map-reduce ] must-fail
385 { 4 } [ { 1 1 } [ 1 + ] [ + ] map-reduce ] unit-test
386 { 2 18 } [ 2 { 3 3 3 } [ dupd * ] [ + ] map-reduce ] unit-test
388 [ { } { } [ [ string>digits product ] bi@ + ] [ + ] 2map-reduce ] must-infer
389 [ { } { } [ + ] [ + ] 2map-reduce ] must-fail
390 { 24 } [ { 1 2 } { 3 4 } [ + ] [ * ] 2map-reduce ] unit-test
391 { 2 96 } [ 2 { 3 3 3 3 } { 4 4 4 4 } [ [ dup ] 2dip * * ] [ + ] 2map-reduce ] unit-test
393 { 4 } [ 5 <iota> [ ] supremum-by ] unit-test
394 { 0 } [ 5 <iota> [ ] infimum-by ] unit-test
395 { "bar" } [ { "bar" "baz" "qux" } [ length ] supremum-by ] unit-test
396 { "bar" } [ { "bar" "baz" "qux" } [ length ] infimum-by ] unit-test
397 { { "foo" } } [ { { "foo" } { "bar" } } [ first ] supremum-by ] unit-test
398 { { "bar" } } [ { { "foo" } { "bar" } } [ first ] infimum-by ] unit-test
399 { -2 1 } [ -2 { 1 2 3 } [ over ^ ] supremum-by ] unit-test
400 { -2 3 } [ -2 { 1 2 3 } [ over ^ ] infimum-by ] unit-test
415 } [ { 0 0 255 } distance ] infimum-by
418 [ -1 0 <repetition> ] must-fail
419 { { } } [ 0 0 <repetition> >array ] unit-test
420 { { 0 } } [ 1 0 <repetition> >array ] unit-test
421 { { 0 0 0 } } [ 3 0 <repetition> >array ] unit-test
423 { "ABCD" } [ "AAAA" [ + ] map-index ] unit-test
427 } [ { 11 22 33 } [ + ] map-index ] unit-test
431 } [ { 11 22 33 } [ + ] V{ } map-index-as ] unit-test
435 } [ { 11 22 33 } [ + ] B{ } map-index-as ] unit-test
439 } [ { 11 22 33 } [ + ] BV{ } map-index-as ] unit-test
441 { t } [ { } { 99 88 } [ <= ] 2all? ] unit-test
443 { f } [ { } { 99 88 } [ <= ] 2any? ] unit-test
444 { t } [ { 2 4 5 8 } { 2 4 6 8 } [ < ] 2any? ] unit-test
445 { f } [ { 2 4 6 8 } { 2 4 6 8 } [ < ] 2any? ] unit-test
447 { "ollo" } [ "ll" "o" 1surround ] unit-test
448 { "ollo" } [ { CHAR: l CHAR: l } "o" 1surround ] unit-test
450 { { CHAR: o CHAR: l CHAR: l CHAR: o } }
451 [ { CHAR: l CHAR: l } "o" { } 1surround-as ] unit-test
454 [ { CHAR: l CHAR: l } "o" "" 1surround-as ] unit-test