]> gitweb.factorcode.org Git - factor.git/blob - core/sequences/sequences-tests.factor
sequences: adding reject/reject-as/reject!.
[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 { "0123456789" } [ 58 iota [ 48 < ] "" reject-as ] unit-test
121
122 [ [ ]       ] [ 1 [ ]           remove ] unit-test
123 [ [ ]       ] [ 1 [ 1 ]         remove ] unit-test
124 [ [ 3 1 1 ] ] [ 2 [ 3 2 1 2 1 ] remove ] unit-test
125
126 [ [ ]       ] [ [ ]       reverse ] unit-test
127 [ [ 1 ]     ] [ [ 1 ]     reverse ] unit-test
128 [ [ 3 2 1 ] ] [ [ 1 2 3 ] reverse ] unit-test
129
130 [ f ] [ f 0 head ] unit-test
131 [ [ ] ] [ [ 1 ] 0 head ] unit-test
132 [ [ 1 2 3 ] ] [ [ 1 2 3 4 ] 3 head ] unit-test
133 [ [ ] ] [ [ 1 2 3 ] 3 tail ] unit-test
134 [ [ 3 ] ] [ [ 1 2 3 ] 2 tail ] unit-test
135
136 [ "blah" ] [ "blahxx" 2 head* ] unit-test
137
138 [ "xx" ] [ "blahxx" 2 tail* ] unit-test
139
140 [ t ] [ "xxfoo" 2 head-slice "xxbar" 2 head-slice = ] unit-test
141 [ t ] [ "xxfoo" 2 head-slice "xxbar" 2 head-slice [ hashcode ] same? ] unit-test
142
143 [ t ] [ "xxfoo" 2 head-slice SBUF" barxx" 2 tail-slice* = ] unit-test
144 [ t ] [ "xxfoo" 2 head-slice SBUF" barxx" 2 tail-slice* [ hashcode ] same? ] unit-test
145
146 [ t ] [ [ 1 2 3 ] [ 1 2 3 ] sequence= ] unit-test
147 [ t ] [ [ 1 2 3 ] { 1 2 3 } sequence= ] unit-test
148 [ t ] [ { 1 2 3 } [ 1 2 3 ] sequence= ] unit-test
149 [ f ] [ [ ] [ 1 2 3 ] sequence= ] unit-test
150
151 [ { 1 3 2 4 } ] [ { 1 2 3 4 } clone 1 2 pick exchange ] unit-test
152
153 [ { "" "a" "aa" "aaa" } ]
154 [ 4 [ CHAR: a <string> ] { } map-integers ]
155 unit-test
156
157 { V{ 1 3 5 7 9 } } [ 10 iota >vector [ even? ] reject! ] unit-test
158
159 [ V{ } ] [ "f" V{ } clone remove! ] unit-test
160 [ V{ } ] [ "f" V{ "f" } clone remove! ] unit-test
161 [ V{ } ] [ "f" V{ "f" "f" } clone remove! ] unit-test
162 [ V{ "x" } ] [ "f" V{ "f" "x" "f" } clone remove! ] unit-test
163 [ V{ "y" "x" } ] [ "f" V{ "y" "f" "x" "f" } clone remove! ] unit-test
164
165 [ V{ 0 1 4 5 } ] [ 6 iota >vector 2 4 pick delete-slice ] unit-test
166
167 [ 6 >vector 2 8 pick delete-slice ] must-fail
168
169 [ V{ } ] [ 6 iota >vector 0 6 pick delete-slice ] unit-test
170
171 [ { 1 2 "a" "b" 5 6 7 } ] [
172     { "a" "b" } 2 4 { 1 2 3 4 5 6 7 }
173     replace-slice
174 ] unit-test
175
176 [ { 1 2 "a" "b" 6 7 } ] [
177     { "a" "b" } 2 5 { 1 2 3 4 5 6 7 }
178     replace-slice
179 ] unit-test
180
181 [ { 1 2 "a" "b" 4 5 6 7 } ] [
182     { "a" "b" } 2 3 { 1 2 3 4 5 6 7 }
183     replace-slice
184 ] unit-test
185
186 [ { 1 2 3 4 5 6 7 "a" "b" } ] [
187     { "a" "b" } 7 7 { 1 2 3 4 5 6 7 }
188     replace-slice
189 ] unit-test
190
191 [ { "a" 3 } ] [
192     { "a" } 0 2 { 1 2 3 } replace-slice
193 ] unit-test
194
195 [ { 1 4 9 } ] [ { 1 2 3 } clone [ sq ] map! ] unit-test
196
197 [ 5 ] [ 1 >bignum { 1 5 7 } nth-unsafe ] unit-test
198 [ 5 ] [ 1 >bignum { 1 5 7 } nth-unsafe ] unit-test
199 [ 5 ] [ 1 >bignum "\u000001\u000005\u000007" nth-unsafe ] unit-test
200
201 [ SBUF" before&after" ] [
202     "&" 6 11 SBUF" before and after" replace-slice
203 ] unit-test
204
205 [ 3 "a" ] [ { "a" "b" "c" "a" "d" } [ "a" = ] find-last ] unit-test
206
207 [ f f ] [ 100 { 1 2 3 } [ 1 = ] find-from ] unit-test
208 [ f f ] [ 100 { 1 2 3 } [ 1 = ] find-last-from ] unit-test
209 [ f f ] [ -1 { 1 2 3 } [ 1 = ] find-from ] unit-test
210
211 [ 0 ] [ { "a" "b" "c" } { "A" "B" "C" } mismatch ] unit-test
212
213 [ 1 ] [ { "a" "b" "c" } { "a" "B" "C" } mismatch ] unit-test
214
215 [ f ] [ { "a" "b" "c" } { "a" "b" "c" } mismatch ] unit-test
216
217 [ V{ } V{ } ] [ { "a" "b" } { "a" "b" } drop-prefix [ >vector ] bi@ ] unit-test
218
219 [ V{ "C" } V{ "c" } ] [ { "a" "b" "C" } { "a" "b" "c" } drop-prefix [ >vector ] bi@ ] unit-test
220
221 [ -1 1 "abc" <slice> ] must-fail
222
223 [ V{ "a" "b" } V{ } ] [ { "X" "a" "b" } { "X" } drop-prefix [ >vector ] bi@ ] unit-test
224
225 [ 0.5 { 1 2 3 } nth ] [ no-method? ] must-fail-with
226 [ 0.5 "asdfasdf" nth ] [ no-method? ] must-fail-with
227
228 ! Pathological case
229 [ "ihbye" ] [ "hi" <reversed> "bye" append ] unit-test
230
231 [ t ] [ "hi" <reversed> SBUF" hi" <reversed> = ] unit-test
232
233 [ t ] [ "hi" <reversed> SBUF" hi" <reversed> = ] unit-test
234
235 [ t ] [ "hi" <reversed> SBUF" hi" <reversed> [ hashcode ] same? ] unit-test
236
237 [ -10 "hi" "bye" copy ] must-fail
238 [ 10 "hi" "bye" copy ] must-fail
239
240 [ V{ 1 2 3 5 6 } ] [
241     3 V{ 1 2 3 4 5 6 } clone remove-nth!
242 ] unit-test
243
244 ! erg's random tester found this one
245 [ SBUF" 12341234" ] [
246     9 <sbuf> dup "1234" swap push-all dup dup swap push-all
247 ] unit-test
248
249 [ f ] [ f V{ } like f V{ } like eq? ] unit-test
250
251 [ V{ f f f } ] [ 3 V{ } new-sequence ] unit-test
252 [ SBUF" \0\0\0" ] [ 3 SBUF" " new-sequence ] unit-test
253
254 [ 0 ] [ f length ] unit-test
255 [ f first ] must-fail
256 [ 3 ] [ 3 10 iota nth ] unit-test
257 [ 3 ] [ 3 10 iota nth-unsafe ] unit-test
258 [ -3 10 iota nth ] must-fail
259 [ 11 10 iota nth ] must-fail
260
261 [ f ] [ f ?first ] unit-test
262 [ f ] [ { } ?first ] unit-test
263 [ 0 ] [ 10 iota ?first ] unit-test
264
265 [ f ] [ f ?last ] unit-test
266 [ f ] [ { } ?last ] unit-test
267 [ 9 ] [ 10 iota ?last ] unit-test
268
269 [ -1/0. 0 remove-nth! ] must-fail
270 [ "" ] [ "" [ CHAR: \s = ] trim ] unit-test
271 [ "" ] [ "" [ CHAR: \s = ] trim-head ] unit-test
272 [ "" ] [ "" [ CHAR: \s = ] trim-tail ] unit-test
273 [ "" ] [ "  " [ CHAR: \s = ] trim-head ] unit-test
274 [ "" ] [ "  " [ CHAR: \s = ] trim-tail ] unit-test
275 [ "asdf" ] [ " asdf " [ CHAR: \s = ] trim ] unit-test
276 [ "asdf " ] [ " asdf " [ CHAR: \s = ] trim-head ] unit-test
277 [ " asdf" ] [ " asdf " [ CHAR: \s = ] trim-tail ] unit-test
278
279 [ 328350 ] [ 100 iota [ sq ] map-sum ] unit-test
280
281 [ 50 ] [ 100 iota [ even? ] count ] unit-test
282 [ 50 ] [ 100 iota [ odd?  ] count ] unit-test
283
284 [ { "b" "d" } ] [ { 1 3 } { "a" "b" "c" "d" } nths ] unit-test
285 [ { "a" "b" "c" "d" } ] [ { 0 1 2 3 } { "a" "b" "c" "d" } nths ] unit-test
286 [ { "d" "c" "b" "a" } ] [ { 3 2 1 0 } { "a" "b" "c" "d" } nths ] unit-test
287 [ { "d" "a" "b" "c" } ] [ { 3 0 1 2 } { "a" "b" "c" "d" } nths ] unit-test
288
289 [ "dac" ] [ { 3 0 2 } "abcd" nths ] unit-test
290
291 TUPLE: bogus-hashcode ;
292
293 M: bogus-hashcode hashcode* 2drop 0 >bignum ;
294
295 [ 0 ] [ { T{ bogus-hashcode } } hashcode ] unit-test
296
297 [ { 2 4 6 } { 1 3 5 7 } ] [ { 1 2 3 4 5 6 7 } [ even? ] partition ] unit-test
298
299 [ { 1 3 7 } ] [ 2 { 1 3 5 7 } remove-nth ] unit-test
300
301 [ { 1 3 "X" 5 7 } ] [ "X" 2 { 1 3 5 7 } insert-nth ] unit-test
302
303 [ V{ 0 2 } ] [ "a" { "a" "b" "a" } indices ] unit-test
304
305 [ "a,b" ] [ "a" "b" "," glue ] unit-test
306 [ "(abc)" ] [ "abc" "(" ")" surround ] unit-test
307
308 [ "HELLO" ] [
309     "HELLO" { -1 -1 -1 -1 -1 } { 2 2 2 2 2 2 }
310     [ * 2 + + ] 3map
311 ] unit-test
312
313 { 3 1 } [ [ 3array ] 3map ] must-infer-as
314
315 { 3 0 } [ [ 3drop ] 3each ] must-infer-as
316
317 [ V{ 0 3 } ] [ "A" { "A" "B" "C" "A" "D" } indices ] unit-test
318
319 [ "asdf" iota ] must-fail
320 [ -1 iota ] must-fail
321 [ T{ iota-tuple { n 10 } } ] [ 10 iota ] unit-test
322 [ 0 ] [ 10 iota first ] unit-test
323 { 0 } [ 0 iota sum ] unit-test
324 { 0 } [ 1 iota sum ] unit-test
325 { 10 } [ 5 iota sum ] unit-test
326 { 15 } [ 6 iota sum ] unit-test
327
328 [ "hi" 3 ] [
329     { 1 2 3 4 5 6 7 8 } [ H{ { 3 "hi" } } at ] map-find
330 ] unit-test
331
332 [ f f ] [
333     { 1 2 3 4 5 6 7 8 } [ H{ { 11 "hi" } } at ] map-find
334 ] unit-test
335
336 USE: make
337
338 [ { "a" 1 "b" 1 "c" } ]
339 [ 1 { "a" "b" "c" } [ [ dup , ] [ , ] interleave drop ] { } make ] unit-test
340
341 [ t ] [ 0 array-capacity? ] unit-test
342 [ f ] [ -1 array-capacity? ] unit-test
343
344 [ +lt+ ] [ { 0 0 0 } { 1 1 1 } <=> ] unit-test
345 [ +lt+ ] [ { 0 0 0 } { 0 1 1 } <=> ] unit-test
346 [ +lt+ ] [ { 0 0 0 } { 0 0 0 0 } <=> ] unit-test
347 [ +gt+ ] [ { 1 1 1 } { 0 0 0 } <=> ] unit-test
348 [ +gt+ ] [ { 0 1 1 } { 0 0 0 } <=> ] unit-test
349 [ +gt+ ] [ { 0 0 0 0 } { 0 0 0 } <=> ] unit-test
350 [ +eq+ ] [ { } { } <=> ] unit-test
351 [ +eq+ ] [ { 1 2 3 } { 1 2 3 } <=> ] unit-test
352
353 [ { { { 1 "a" } { 1 "b" } } { { 2 "a" } { 2 "b" } } } ]
354 [ { 1 2 } { "a" "b" } cartesian-product ] unit-test
355
356 [ { } [ string>digits sum ] [ + ] map-reduce ] must-infer
357 [ { } [ ] [ + ] map-reduce ] must-fail
358 [ 4 ] [ { 1 1 } [ 1 + ] [ + ] map-reduce ] unit-test
359
360 [ { } { } [ [ string>digits product ] bi@ + ] [ + ] 2map-reduce ] must-infer
361 [ { } { } [ + ] [ + ] 2map-reduce ] must-fail
362 [ 24 ] [ { 1 2 } { 3 4 } [ + ] [ * ] 2map-reduce ] unit-test
363
364 [ 4 ] [ 5 iota [ ] supremum-by ] unit-test
365 [ 0 ] [ 5 iota [ ] infimum-by ] unit-test
366 { "bar" } [ { "bar" "baz" "qux" } [ length ] supremum-by ] unit-test
367 { "bar" } [ { "bar" "baz" "qux" } [ length ] infimum-by ] unit-test
368 [ { "foo" } ] [ { { "foo" } { "bar" } } [ first ] supremum-by ] unit-test
369 [ { "bar" } ] [ { { "foo" } { "bar" } } [ first ] infimum-by ] unit-test
370 { -2 1 } [ -2 { 1 2 3 } [ over ^ ] supremum-by ] unit-test
371 { -2 3 } [ -2 { 1 2 3 } [ over ^ ] infimum-by ] unit-test
372
373 [ { 0 0 255 } ] [
374     {
375         { 0 0 0 }
376         { 95 255 95 }
377         { 215 95 95 }
378         { 95 135 255 }
379         { 135 95 135 }
380         { 135 255 255 }
381         { 0 0 255 }
382         { 0 95 95 }
383         { 0 255 215 }
384         { 135 0 95 }
385         { 255 0 175 }
386     } [ { 0 0 255 } distance ] infimum-by
387 ] unit-test
388
389 [ -1 0 <repetition> ] must-fail
390 { { } } [ 0 0 <repetition> >array ] unit-test
391 { { 0 } } [ 1 0 <repetition> >array ] unit-test
392 { { 0 0 0 } } [ 3 0 <repetition> >array ] unit-test
393
394 {
395     { 11 23 35 }
396 } [ { 11 22 33 } [ + ] map-index ] unit-test
397
398 {
399     V{ 11 23 35 }
400 } [ { 11 22 33 } [ + ] V{ } map-index-as ] unit-test
401
402 {
403     B{ 11 23 35 }
404 } [ { 11 22 33 } [ + ] B{ } map-index-as ] unit-test
405
406 {
407     BV{ 11 23 35 }
408 } [ { 11 22 33 } [ + ] BV{ } map-index-as ] unit-test