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