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