]> gitweb.factorcode.org Git - factor.git/blob - core/sequences/sequences-tests.factor
Fixes #2966
[factor.git] / core / sequences / sequences-tests.factor
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 ;
4 IN: sequences.tests
5
6 { "empty" } [ { } [ "empty" ] [ "not empty" ] if-empty ] unit-test
7 { { 1 } "not empty" } [ { 1 } [ "empty" ] [ "not empty" ] if-empty ] unit-test
8
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
19
20 { "cba" } [ "abcdef" 3 head-slice reverse ] unit-test
21
22 { 5040 5040 } [
23     [ 1 2 3 4 5 6 7 ] dup rest-slice
24     [ 1 [ * ] reduce ] bi@
25 ] unit-test
26
27 { 10079 6459 } [
28     [ 1 2 3 4 5 6 7 ] dup rest-slice
29     [ 1 [ [ * ] [ + ] bi* ] reduce-index ] bi@
30 ] unit-test
31
32 { 5040 { 1 1 2 6 24 120 720 } }
33 [ { 1 2 3 4 5 6 7 } 1 [ * ] accumulate ] unit-test
34
35 { 64 B{ 1 2 4 16 } }
36 [ B{ 2 2 4 4 } 1 [ * ] accumulate ] unit-test
37
38 { 5040 { 1 1 2 6 24 120 720 } }
39 [ { 1 2 3 4 5 6 7 } 1 [ * ] accumulate! ] unit-test
40
41 { t }
42 [ { 1 2 3 4 5 6 7 } dup 1 [ * ] accumulate! nip eq? ] unit-test
43
44 { { 1 2 6 24 120 720 5040 } }
45 [ { 1 2 3 4 5 6 7 } 1 [ * ] accumulate* ] unit-test
46
47 { B{ 2 4 16 64 } }
48 [ B{ 2 2 4 4 } 1 [ * ] accumulate* ] unit-test
49
50 { { 1 2 6 24 120 720 5040 } }
51 [ { 1 2 3 4 5 6 7 } 1 [ * ] accumulate*! ] unit-test
52
53 { t }
54 [ { 1 2 3 4 5 6 7 } dup 1 [ * ] accumulate*! eq? ] unit-test
55
56 { f f } [ [ ] [ ] find ] unit-test
57 { 0 1 } [ [ 1 ] [ ] find ] unit-test
58 { 1 "world" } [ [ "hello" "world" ] [ "world" = ] find ] unit-test
59 { 2 3 } [ [ 1 2 3 ] [ 2 > ] find ] unit-test
60 { f f } [ [ 1 2 3 ] [ 10 > ] find ] unit-test
61
62 { 1 CHAR: e }
63 [ "hello world" "aeiou" [ member? ] curry find ] unit-test
64
65 { 4 CHAR: o }
66 [ 3 "hello world" "aeiou" [ member? ] curry find-from ] unit-test
67
68 { f f } [ "abcd" [ 10 > nip ] find-index ] unit-test
69 { f f } [ "abcd" [ drop CHAR: e = ] find-index ] unit-test
70 { 3 CHAR: d } [ "abcdefg" [ 3 = nip ] find-index ] unit-test
71 { 3 CHAR: d } [ "abcdefg" [ drop CHAR: d = ] find-index ] unit-test
72
73 { 0 CHAR: a } [ 0 "abcdef" [ drop CHAR: a >= ] find-index-from ] unit-test
74 { 1 CHAR: b } [ 0 "abcdef" [ drop CHAR: a > ] find-index-from ] unit-test
75 { 2 CHAR: c } [ 1 "abcdef" [ drop CHAR: b > ] find-index-from ] unit-test
76
77 { f } [ 3 [ ]     member? ] unit-test
78 { f } [ 3 [ 1 2 ] member? ] unit-test
79 { t } [ 1 [ 1 2 ] member? ] unit-test
80 { t } [ 2 [ 1 2 ] member? ] unit-test
81
82 { t }
83 [ [ "hello" "world" ] [ second ] keep member-eq? ] unit-test
84
85 { 4 } [ CHAR: x "tuvwxyz" >vector index ] unit-test
86
87 { f } [ CHAR: x 5 "tuvwxyz" >vector index-from ] unit-test
88
89 { f } [ CHAR: a 0 "tuvwxyz" >vector index-from ] unit-test
90
91 { f } [ [ "Hello" { } 0.75 ] [ string? ] all? ] unit-test
92 { t } [ [ ] [ ] all? ] unit-test
93 { t } [ [ "hi" t 0.5 ] [ ] all? ] unit-test
94
95 { [ 1 2 3 ] } [ [ 1 4 2 5 3 6 ] [ 4 < ] filter ] unit-test
96 { { 4 2 6 } } [ { 1 4 2 5 3 6 } [ 2 mod 0 = ] filter ] unit-test
97
98 { [ 3 ] } [ [ 1 2 3 ] 2 [ swap < ] curry filter ] unit-test
99
100 { V{ 1 2 3 } } [ V{ 1 4 2 5 3 6 } clone [ 4 < ] filter! ] unit-test
101 { V{ 4 2 6 } } [ V{ 1 4 2 5 3 6 } clone [ 2 mod 0 = ] filter! ] unit-test
102
103 { V{ 3 } } [ V{ 1 2 3 } clone 2 [ swap < ] curry filter! ] unit-test
104 { 4 V{ 1 2 3 } } [ 4 V{ 1 4 2 5 3 6 } [ dupd > ] filter! ] unit-test
105
106 { "hello world how are you" }
107 [ { "hello" "world" "how" "are" "you" } " " join ] unit-test
108
109 { "hello world how are you" }
110 [ { "hello" "world" "how" "are" "you" } " " "" join-as ] unit-test
111
112 { "" } [ { } "" join ] unit-test
113
114 { "" } [ { } "" "" join-as ] unit-test
115
116 { { } } [ { } flip ] unit-test
117
118 { { "b" "e" } } [ 1 { { "a" "b" "c" } { "d" "e" "f" } } flip nth ] unit-test
119
120 { { { 1 4 } { 2 5 } { 3 6 } } }
121 [ { { 1 2 3 } { 4 5 6 } } flip ] unit-test
122
123 { [ 2 3 4 ] } [ [ 1 2 3 ] 1 [ + ] curry map ] unit-test
124
125 { 1 } [ 0 [ 1 2 ] nth ] unit-test
126 { 2 } [ 1 [ 1 2 ] nth ] unit-test
127
128 { [ ]           } [ [ ]   [ ]       append ] unit-test
129 { [ 1 ]         } [ [ 1 ] [ ]       append ] unit-test
130 { [ 2 ]         } [ [ ] [ 2 ]       append ] unit-test
131 { [ 1 2 3 4 ]   } [ [ 1 2 3 ] [ 4 ] append ] unit-test
132 { [ 1 2 3 4 ]   } [ [ 1 2 3 ] { 4 } append ] unit-test
133
134 [ "a" -1 append ] must-fail
135 [ -1 "a" append ] must-fail
136
137 { t } [ B{ 0 } { 1 } append byte-array? ] unit-test
138 { t } [ B{ 0 } { 1 } prepend byte-array? ] unit-test
139
140 { "0123456789" } [ 58 <iota> [ 48 < ] "" reject-as ] unit-test
141
142 { [ ]       } [ 1 [ ]           remove ] unit-test
143 { [ ]       } [ 1 [ 1 ]         remove ] unit-test
144 { [ 3 1 1 ] } [ 2 [ 3 2 1 2 1 ] remove ] unit-test
145
146 { [ ]       } [ [ ]       reverse ] unit-test
147 { [ 1 ]     } [ [ 1 ]     reverse ] unit-test
148 { [ 3 2 1 ] } [ [ 1 2 3 ] reverse ] unit-test
149
150 { f } [ f 0 head ] unit-test
151 { [ ] } [ [ 1 ] 0 head ] unit-test
152 { [ 1 2 3 ] } [ [ 1 2 3 4 ] 3 head ] unit-test
153 { [ ] } [ [ 1 2 3 ] 3 tail ] unit-test
154 { [ 3 ] } [ [ 1 2 3 ] 2 tail ] unit-test
155
156 { "blah" } [ "blahxx" 2 head* ] unit-test
157
158 { "xx" } [ "blahxx" 2 tail* ] unit-test
159
160 { t } [ "xxfoo" 2 head-slice "xxbar" 2 head-slice = ] unit-test
161 { t } [ "xxfoo" 2 head-slice "xxbar" 2 head-slice [ hashcode ] same? ] unit-test
162
163 { t } [ "xxfoo" 2 head-slice SBUF" barxx" 2 tail-slice* = ] unit-test
164 { t } [ "xxfoo" 2 head-slice SBUF" barxx" 2 tail-slice* [ hashcode ] same? ] unit-test
165
166 { t } [ [ 1 2 3 ] [ 1 2 3 ] sequence= ] unit-test
167 { t } [ [ 1 2 3 ] { 1 2 3 } sequence= ] unit-test
168 { t } [ { 1 2 3 } [ 1 2 3 ] sequence= ] unit-test
169 { f } [ [ ] [ 1 2 3 ] sequence= ] unit-test
170
171 { { 1 3 2 4 } } [ { 1 2 3 4 } clone 1 2 pick exchange ] unit-test
172
173 { { "" "a" "aa" "aaa" } }
174 [ 4 [ CHAR: a <string> ] map-integers ]
175 unit-test
176
177 { V{ 1 3 5 7 9 } } [ 10 <iota> >vector [ even? ] reject! ] unit-test
178
179 { V{ } } [ "f" V{ } clone remove! ] unit-test
180 { V{ } } [ "f" V{ "f" } clone remove! ] unit-test
181 { V{ } } [ "f" V{ "f" "f" } clone remove! ] unit-test
182 { V{ "x" } } [ "f" V{ "f" "x" "f" } clone remove! ] unit-test
183 { V{ "y" "x" } } [ "f" V{ "y" "f" "x" "f" } clone remove! ] unit-test
184
185 { V{ 0 1 4 5 } } [ 6 <iota> >vector 2 4 pick delete-slice ] unit-test
186
187 [ 6 >vector 2 8 pick delete-slice ] must-fail
188
189 { V{ } } [ 6 <iota> >vector 0 6 pick delete-slice ] unit-test
190
191 { { 1 2 "a" "b" 5 6 7 } } [
192     { "a" "b" } 2 4 { 1 2 3 4 5 6 7 }
193     replace-slice
194 ] unit-test
195
196 { { 1 2 "a" "b" 6 7 } } [
197     { "a" "b" } 2 5 { 1 2 3 4 5 6 7 }
198     replace-slice
199 ] unit-test
200
201 { { 1 2 "a" "b" 4 5 6 7 } } [
202     { "a" "b" } 2 3 { 1 2 3 4 5 6 7 }
203     replace-slice
204 ] unit-test
205
206 { { 1 2 3 4 5 6 7 "a" "b" } } [
207     { "a" "b" } 7 7 { 1 2 3 4 5 6 7 }
208     replace-slice
209 ] unit-test
210
211 { { "a" 3 } } [
212     { "a" } 0 2 { 1 2 3 } replace-slice
213 ] unit-test
214
215 { { 1 4 9 } } [ { 1 2 3 } clone [ sq ] map! ] unit-test
216
217 { 5 } [ 1 >bignum { 1 5 7 } nth-unsafe ] unit-test
218 { 5 } [ 1 >bignum { 1 5 7 } nth-unsafe ] unit-test
219 { 5 } [ 1 >bignum "\u000001\u000005\u000007" nth-unsafe ] unit-test
220
221 { SBUF" before&after" } [
222     "&" 6 11 SBUF" before and after" replace-slice
223 ] unit-test
224
225 { 3 "a" } [ { "a" "b" "c" "a" "d" } [ "a" = ] find-last ] unit-test
226
227 { f f } [ 100 { 1 2 3 } [ 1 = ] find-from ] unit-test
228 { f f } [ 100 { 1 2 3 } [ 1 = ] find-last-from ] unit-test
229 { f f } [ -1 { 1 2 3 } [ 1 = ] find-from ] unit-test
230
231 { 0 } [ { "a" "b" "c" } { "A" "B" "C" } mismatch ] unit-test
232
233 { 1 } [ { "a" "b" "c" } { "a" "B" "C" } mismatch ] unit-test
234
235 { f } [ { "a" "b" "c" } { "a" "b" "c" } mismatch ] unit-test
236
237 { V{ } V{ } } [ { "a" "b" } { "a" "b" } drop-prefix [ >vector ] bi@ ] unit-test
238
239 { V{ "C" } V{ "c" } } [ { "a" "b" "C" } { "a" "b" "c" } drop-prefix [ >vector ] bi@ ] unit-test
240
241 [ -1 1 "abc" <slice> ] must-fail
242
243 { V{ "a" "b" } V{ } } [ { "X" "a" "b" } { "X" } drop-prefix [ >vector ] bi@ ] unit-test
244
245 [ 0.5 { 1 2 3 } nth ] [ no-method? ] must-fail-with
246 [ 0.5 "asdfasdf" nth ] [ no-method? ] must-fail-with
247
248 ! Pathological case
249 { "ihbye" } [ "hi" <reversed> "bye" append ] unit-test
250
251 { t } [ "hi" <reversed> SBUF" hi" <reversed> = ] unit-test
252
253 { t } [ "hi" <reversed> SBUF" hi" <reversed> = ] unit-test
254
255 { t } [ "hi" <reversed> SBUF" hi" <reversed> [ hashcode ] same? ] unit-test
256
257 [ -10 "hi" "bye" copy ] must-fail
258 [ 10 "hi" "bye" copy ] must-fail
259
260 { V{ 1 2 3 5 6 } } [
261     3 V{ 1 2 3 4 5 6 } clone remove-nth!
262 ] unit-test
263
264 ! erg's random tester found this one
265 { SBUF" 12341234" } [
266     9 <sbuf> dup "1234" swap push-all dup dup swap push-all
267 ] unit-test
268
269 { f } [ f V{ } like f V{ } like eq? ] unit-test
270
271 { V{ f f f } } [ 3 V{ } new-sequence ] unit-test
272 { SBUF" \0\0\0" } [ 3 SBUF" " new-sequence ] unit-test
273
274 { 0 } [ f length ] unit-test
275 [ f first ] must-fail
276 { 3 } [ 3 10 <iota> nth ] unit-test
277 { 3 } [ 3 10 <iota> nth-unsafe ] unit-test
278 [ -3 10 <iota> nth ] must-fail
279 [ 11 10 <iota> nth ] must-fail
280
281 { f } [ f ?first ] unit-test
282 { f } [ { } ?first ] unit-test
283 { 0 } [ 10 <iota> ?first ] unit-test
284
285 { f } [ f ?last ] unit-test
286 { f } [ { } ?last ] unit-test
287 { 9 } [ 10 <iota> ?last ] unit-test
288
289 [ -1/0. 0 remove-nth! ] must-fail
290 { "" } [ "" [ CHAR: \s = ] trim ] unit-test
291 { "" } [ "" [ CHAR: \s = ] trim-head ] unit-test
292 { "" } [ "" [ CHAR: \s = ] trim-tail ] unit-test
293 { "" } [ "  " [ CHAR: \s = ] trim-head ] unit-test
294 { "" } [ "  " [ CHAR: \s = ] trim-tail ] unit-test
295 { "asdf" } [ " asdf " [ CHAR: \s = ] trim ] unit-test
296 { "asdf " } [ " asdf " [ CHAR: \s = ] trim-head ] unit-test
297 { " asdf" } [ " asdf " [ CHAR: \s = ] trim-tail ] unit-test
298
299 { 328350 } [ 100 <iota> [ sq ] map-sum ] unit-test
300
301 { 50 } [ 100 <iota> [ even? ] count ] unit-test
302 { 50 } [ 100 <iota> [ odd?  ] count ] unit-test
303
304 { { "b" "d" } } [ { 1 3 } { "a" "b" "c" "d" } nths ] unit-test
305 { { "a" "b" "c" "d" } } [ { 0 1 2 3 } { "a" "b" "c" "d" } nths ] unit-test
306 { { "d" "c" "b" "a" } } [ { 3 2 1 0 } { "a" "b" "c" "d" } nths ] unit-test
307 { { "d" "a" "b" "c" } } [ { 3 0 1 2 } { "a" "b" "c" "d" } nths ] unit-test
308
309 { "dac" } [ { 3 0 2 } "abcd" nths ] unit-test
310
311 TUPLE: bogus-hashcode ;
312
313 M: bogus-hashcode hashcode* 2drop 0 >bignum ;
314
315 { 0 } [ { T{ bogus-hashcode } } hashcode ] unit-test
316
317 { { 2 4 6 } { 1 3 5 7 } } [ { 1 2 3 4 5 6 7 } [ even? ] partition ] unit-test
318
319 { { 1 3 7 } } [ 2 { 1 3 5 7 } remove-nth ] unit-test
320
321 { { 1 3 "X" 5 7 } } [ "X" 2 { 1 3 5 7 } insert-nth ] unit-test
322
323 { V{ 0 2 } } [ "a" { "a" "b" "a" } indices ] unit-test
324
325 { "a,b" } [ "a" "b" "," glue ] unit-test
326 { "(abc)" } [ "abc" "(" ")" surround ] unit-test
327
328 { "HELLO" } [
329     "HELLO" { -1 -1 -1 -1 -1 } { 2 2 2 2 2 2 }
330     [ * 2 + + ] 3map
331 ] unit-test
332
333 { 3 1 } [ [ 3array ] 3map ] must-infer-as
334
335 { 3 0 } [ [ 3drop ] 3each ] must-infer-as
336
337 { V{ 0 3 } } [ "A" { "A" "B" "C" "A" "D" } indices ] unit-test
338
339 [ "asdf" <iota> ] must-fail
340 [ -1 <iota> ] must-fail
341 { T{ iota { n 10 } } } [ 10 <iota> ] unit-test
342 { 0 } [ 10 <iota> first ] unit-test
343 { 0 } [ 0 <iota> sum ] unit-test
344 { 0 } [ 1 <iota> sum ] unit-test
345 { 10 } [ 5 <iota> sum ] unit-test
346 { 15 } [ 6 <iota> sum ] unit-test
347
348 { "hi" 3 } [
349     { 1 2 3 4 5 6 7 8 } [ H{ { 3 "hi" } } at ] map-find
350 ] unit-test
351
352 { f f } [
353     { 1 2 3 4 5 6 7 8 } [ H{ { 11 "hi" } } at ] map-find
354 ] unit-test
355
356 { { "a" 1 "b" 1 "c" } }
357 [ 1 { "a" "b" "c" } [ [ dup , ] [ , ] interleave drop ] { } make ] unit-test
358
359 { t } [ 0 array-capacity? ] unit-test
360 { f } [ -1 array-capacity? ] unit-test
361
362 { +lt+ } [ { 0 0 0 } { 1 1 1 } <=> ] unit-test
363 { +lt+ } [ { 0 0 0 } { 0 1 1 } <=> ] unit-test
364 { +lt+ } [ { 0 0 0 } { 0 0 0 0 } <=> ] unit-test
365 { +gt+ } [ { 1 1 1 } { 0 0 0 } <=> ] unit-test
366 { +gt+ } [ { 0 1 1 } { 0 0 0 } <=> ] unit-test
367 { +gt+ } [ { 0 0 0 0 } { 0 0 0 } <=> ] unit-test
368 { +eq+ } [ { } { } <=> ] unit-test
369 { +eq+ } [ { 1 2 3 } { 1 2 3 } <=> ] unit-test
370
371 { { { { 1 "a" } { 1 "b" } } { { 2 "a" } { 2 "b" } } } }
372 [ { 1 2 } { "a" "b" } cartesian-product ] unit-test
373
374 { 2 4 } [ { 1 2 3 } { 4 5 6 } [ [ even? ] both? ] cartesian-find ] unit-test
375 { f f } [ { 1 2 3 } { 4 5 6 } [ [ 10 > ] both? ] cartesian-find ] unit-test
376
377 [ { } [ string>digits sum ] [ + ] map-reduce ] must-infer
378 [ { } [ ] [ + ] map-reduce ] must-fail
379 { 4 } [ { 1 1 } [ 1 + ] [ + ] map-reduce ] unit-test
380 { 2 18 } [ 2 { 3 3 3 } [ dupd * ] [ + ] map-reduce ] unit-test
381
382 [ { } { } [ [ string>digits product ] bi@ + ] [ + ] 2map-reduce ] must-infer
383 [ { } { } [ + ] [ + ] 2map-reduce ] must-fail
384 { 24 } [ { 1 2 } { 3 4 } [ + ] [ * ] 2map-reduce ] unit-test
385 { 2 96 } [ 2 { 3 3 3 3 } { 4 4 4 4 } [ [ dup ] 2dip * * ] [ + ] 2map-reduce ] unit-test
386
387 { 4 } [ 5 <iota> [ ] supremum-by ] unit-test
388 { 0 } [ 5 <iota> [ ] infimum-by ] unit-test
389 { "bar" } [ { "bar" "baz" "qux" } [ length ] supremum-by ] unit-test
390 { "bar" } [ { "bar" "baz" "qux" } [ length ] infimum-by ] unit-test
391 { { "foo" } } [ { { "foo" } { "bar" } } [ first ] supremum-by ] unit-test
392 { { "bar" } } [ { { "foo" } { "bar" } } [ first ] infimum-by ] unit-test
393 { -2 1 } [ -2 { 1 2 3 } [ over ^ ] supremum-by ] unit-test
394 { -2 3 } [ -2 { 1 2 3 } [ over ^ ] infimum-by ] unit-test
395
396 { { 0 0 255 } } [
397     {
398         { 0 0 0 }
399         { 95 255 95 }
400         { 215 95 95 }
401         { 95 135 255 }
402         { 135 95 135 }
403         { 135 255 255 }
404         { 0 0 255 }
405         { 0 95 95 }
406         { 0 255 215 }
407         { 135 0 95 }
408         { 255 0 175 }
409     } [ { 0 0 255 } distance ] infimum-by
410 ] unit-test
411
412 [ -1 0 <repetition> ] must-fail
413 { { } } [ 0 0 <repetition> >array ] unit-test
414 { { 0 } } [ 1 0 <repetition> >array ] unit-test
415 { { 0 0 0 } } [ 3 0 <repetition> >array ] unit-test
416
417 { "ABCD" } [ "AAAA" [ + ] map-index ] unit-test
418
419 {
420     { 11 23 35 }
421 } [ { 11 22 33 } [ + ] map-index ] unit-test
422
423 {
424     V{ 11 23 35 }
425 } [ { 11 22 33 } [ + ] V{ } map-index-as ] unit-test
426
427 {
428     B{ 11 23 35 }
429 } [ { 11 22 33 } [ + ] B{ } map-index-as ] unit-test
430
431 {
432     BV{ 11 23 35 }
433 } [ { 11 22 33 } [ + ] BV{ } map-index-as ] unit-test
434
435 { t } [ { } { 99 88 } [ <= ] 2all? ] unit-test
436
437 { f } [ { } { 99 88 } [ <= ] 2any? ] unit-test
438 { t } [ { 2 4 5 8 } { 2 4 6 8 } [ < ] 2any? ] unit-test
439 { f } [ { 2 4 6 8 } { 2 4 6 8 } [ < ] 2any? ] unit-test
440
441 { "ollo" } [ "ll" "o" 1surround ] unit-test
442 { "ollo" } [ { CHAR: l CHAR: l } "o" 1surround ] unit-test
443
444 { { CHAR: o CHAR: l CHAR: l CHAR: o } }
445 [ { CHAR: l CHAR: l } "o" { } 1surround-as ] unit-test
446
447 { "ollo" }
448 [ { CHAR: l CHAR: l } "o" "" 1surround-as ] unit-test