]> gitweb.factorcode.org Git - factor.git/blob - core/sequences/sequences-tests.factor
core: Use integer>fixnum instead of >fixnum for ``nth`` and other sequence operations...
[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 ;
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 ]
88 unit-test
89
90 [ "" ] [ { } "" join ] unit-test
91
92 [ { } ] [ { } flip ] unit-test
93
94 [ { "b" "e" } ] [ 1 { { "a" "b" "c" } { "d" "e" "f" } } flip nth ] unit-test
95
96 [ { { 1 4 } { 2 5 } { 3 6 } } ]
97 [ { { 1 2 3 } { 4 5 6 } } flip ] unit-test
98
99 [ [ 2 3 4 ] ] [ [ 1 2 3 ] 1 [ + ] curry map ] unit-test
100
101 [ 1 ] [ 0 [ 1 2 ] nth ] unit-test
102 [ 2 ] [ 1 [ 1 2 ] nth ] unit-test
103
104 [ [ ]           ] [ [ ]   [ ]       append ] unit-test
105 [ [ 1 ]         ] [ [ 1 ] [ ]       append ] unit-test
106 [ [ 2 ]         ] [ [ ] [ 2 ]       append ] unit-test
107 [ [ 1 2 3 4 ]   ] [ [ 1 2 3 ] [ 4 ] append ] unit-test
108 [ [ 1 2 3 4 ]   ] [ [ 1 2 3 ] { 4 } append ] unit-test
109
110 [ "a" -1 append ] must-fail
111 [ -1 "a" append ] must-fail
112
113 { t } [ B{ 0 } { 1 } append byte-array? ] unit-test
114 { t } [ B{ 0 } { 1 } prepend byte-array? ] unit-test
115
116 [ [ ]       ] [ 1 [ ]           remove ] unit-test
117 [ [ ]       ] [ 1 [ 1 ]         remove ] unit-test
118 [ [ 3 1 1 ] ] [ 2 [ 3 2 1 2 1 ] remove ] unit-test
119
120 [ [ ]       ] [ [ ]       reverse ] unit-test
121 [ [ 1 ]     ] [ [ 1 ]     reverse ] unit-test
122 [ [ 3 2 1 ] ] [ [ 1 2 3 ] reverse ] unit-test
123
124 [ f ] [ f 0 head ] unit-test
125 [ [ ] ] [ [ 1 ] 0 head ] unit-test
126 [ [ 1 2 3 ] ] [ [ 1 2 3 4 ] 3 head ] unit-test
127 [ [ ] ] [ [ 1 2 3 ] 3 tail ] unit-test
128 [ [ 3 ] ] [ [ 1 2 3 ] 2 tail ] unit-test
129
130 [ "blah" ] [ "blahxx" 2 head* ] unit-test
131
132 [ "xx" ] [ "blahxx" 2 tail* ] unit-test
133
134 [ t ] [ "xxfoo" 2 head-slice "xxbar" 2 head-slice = ] unit-test
135 [ t ] [ "xxfoo" 2 head-slice "xxbar" 2 head-slice [ hashcode ] same? ] unit-test
136
137 [ t ] [ "xxfoo" 2 head-slice SBUF" barxx" 2 tail-slice* = ] unit-test
138 [ t ] [ "xxfoo" 2 head-slice SBUF" barxx" 2 tail-slice* [ hashcode ] same? ] unit-test
139
140 [ t ] [ [ 1 2 3 ] [ 1 2 3 ] sequence= ] unit-test
141 [ t ] [ [ 1 2 3 ] { 1 2 3 } sequence= ] unit-test
142 [ t ] [ { 1 2 3 } [ 1 2 3 ] sequence= ] unit-test
143 [ f ] [ [ ] [ 1 2 3 ] sequence= ] unit-test
144
145 [ { 1 3 2 4 } ] [ { 1 2 3 4 } clone 1 2 pick exchange ] unit-test
146
147 [ { "" "a" "aa" "aaa" } ]
148 [ 4 [ CHAR: a <string> ] { } map-integers ]
149 unit-test
150
151 [ V{ } ] [ "f" V{ } clone remove! ] unit-test
152 [ V{ } ] [ "f" V{ "f" } clone remove! ] unit-test
153 [ V{ } ] [ "f" V{ "f" "f" } clone remove! ] unit-test
154 [ V{ "x" } ] [ "f" V{ "f" "x" "f" } clone remove! ] unit-test
155 [ V{ "y" "x" } ] [ "f" V{ "y" "f" "x" "f" } clone remove! ] unit-test
156
157 [ V{ 0 1 4 5 } ] [ 6 iota >vector 2 4 pick delete-slice ] unit-test
158
159 [ 6 >vector 2 8 pick delete-slice ] must-fail
160
161 [ V{ } ] [ 6 iota >vector 0 6 pick delete-slice ] unit-test
162
163 [ { 1 2 "a" "b" 5 6 7 } ] [
164     { "a" "b" } 2 4 { 1 2 3 4 5 6 7 }
165     replace-slice
166 ] unit-test
167
168 [ { 1 2 "a" "b" 6 7 } ] [
169     { "a" "b" } 2 5 { 1 2 3 4 5 6 7 }
170     replace-slice
171 ] unit-test
172
173 [ { 1 2 "a" "b" 4 5 6 7 } ] [
174     { "a" "b" } 2 3 { 1 2 3 4 5 6 7 }
175     replace-slice
176 ] unit-test
177
178 [ { 1 2 3 4 5 6 7 "a" "b" } ] [
179     { "a" "b" } 7 7 { 1 2 3 4 5 6 7 }
180     replace-slice
181 ] unit-test
182
183 [ { "a" 3 } ] [
184     { "a" } 0 2 { 1 2 3 } replace-slice
185 ] unit-test
186
187 [ { 1 4 9 } ] [ { 1 2 3 } clone [ sq ] map! ] unit-test
188
189 [ 5 ] [ 1 >bignum { 1 5 7 } nth-unsafe ] unit-test
190 [ 5 ] [ 1 >bignum { 1 5 7 } nth-unsafe ] unit-test
191 [ 5 ] [ 1 >bignum "\u000001\u000005\u000007" nth-unsafe ] unit-test
192
193 [ SBUF" before&after" ] [
194     "&" 6 11 SBUF" before and after" replace-slice
195 ] unit-test
196
197 [ 3 "a" ] [ { "a" "b" "c" "a" "d" } [ "a" = ] find-last ] unit-test
198
199 [ f f ] [ 100 { 1 2 3 } [ 1 = ] find-from ] unit-test
200 [ f f ] [ 100 { 1 2 3 } [ 1 = ] find-last-from ] unit-test
201 [ f f ] [ -1 { 1 2 3 } [ 1 = ] find-from ] unit-test
202
203 [ 0 ] [ { "a" "b" "c" } { "A" "B" "C" } mismatch ] unit-test
204
205 [ 1 ] [ { "a" "b" "c" } { "a" "B" "C" } mismatch ] unit-test
206
207 [ f ] [ { "a" "b" "c" } { "a" "b" "c" } mismatch ] unit-test
208
209 [ V{ } V{ } ] [ { "a" "b" } { "a" "b" } drop-prefix [ >vector ] bi@ ] unit-test
210
211 [ V{ "C" } V{ "c" } ] [ { "a" "b" "C" } { "a" "b" "c" } drop-prefix [ >vector ] bi@ ] unit-test
212
213 [ -1 1 "abc" <slice> ] must-fail
214
215 [ V{ "a" "b" } V{ } ] [ { "X" "a" "b" } { "X" } drop-prefix [ >vector ] bi@ ] unit-test
216
217 [ 0.5 10 iota nth ] [ no-method? ] must-fail-with
218 [ 0.5 { 1 2 3 } nth ] [ no-method? ] must-fail-with
219 [ 0.5 "asdfasdf" nth ] [ no-method? ] must-fail-with
220
221 ! Pathological case
222 [ "ihbye" ] [ "hi" <reversed> "bye" append ] unit-test
223
224 [ t ] [ "hi" <reversed> SBUF" hi" <reversed> = ] unit-test
225
226 [ t ] [ "hi" <reversed> SBUF" hi" <reversed> = ] unit-test
227
228 [ t ] [ "hi" <reversed> SBUF" hi" <reversed> [ hashcode ] same? ] unit-test
229
230 [ -10 "hi" "bye" copy ] must-fail
231 [ 10 "hi" "bye" copy ] must-fail
232
233 [ V{ 1 2 3 5 6 } ] [
234     3 V{ 1 2 3 4 5 6 } clone remove-nth!
235 ] unit-test
236
237 ! erg's random tester found this one
238 [ SBUF" 12341234" ] [
239     9 <sbuf> dup "1234" swap push-all dup dup swap push-all
240 ] unit-test
241
242 [ f ] [ f V{ } like f V{ } like eq? ] unit-test
243
244 [ V{ f f f } ] [ 3 V{ } new-sequence ] unit-test
245 [ SBUF" \0\0\0" ] [ 3 SBUF" " new-sequence ] unit-test
246
247 [ 0 ] [ f length ] unit-test
248 [ f first ] must-fail
249 [ 3 ] [ 3 10 iota nth ] unit-test
250 [ 3 ] [ 3 10 iota nth-unsafe ] unit-test
251 [ -3 10 iota nth ] must-fail
252 [ 11 10 iota nth ] must-fail
253
254 [ f ] [ f ?first ] unit-test
255 [ f ] [ { } ?first ] unit-test
256 [ 0 ] [ 10 iota ?first ] unit-test
257
258 [ f ] [ f ?last ] unit-test
259 [ f ] [ { } ?last ] unit-test
260 [ 9 ] [ 10 iota ?last ] unit-test
261
262 [ -1/0. 0 remove-nth! ] must-fail
263 [ "" ] [ "" [ CHAR: \s = ] trim ] unit-test
264 [ "" ] [ "" [ CHAR: \s = ] trim-head ] unit-test
265 [ "" ] [ "" [ CHAR: \s = ] trim-tail ] unit-test
266 [ "" ] [ "  " [ CHAR: \s = ] trim-head ] unit-test
267 [ "" ] [ "  " [ CHAR: \s = ] trim-tail ] unit-test
268 [ "asdf" ] [ " asdf " [ CHAR: \s = ] trim ] unit-test
269 [ "asdf " ] [ " asdf " [ CHAR: \s = ] trim-head ] unit-test
270 [ " asdf" ] [ " asdf " [ CHAR: \s = ] trim-tail ] unit-test
271
272 [ 328350 ] [ 100 iota [ sq ] map-sum ] unit-test
273
274 [ 50 ] [ 100 iota [ even? ] count ] unit-test
275 [ 50 ] [ 100 iota [ odd?  ] count ] unit-test
276
277 [ { "b" "d" } ] [ { 1 3 } { "a" "b" "c" "d" } nths ] unit-test
278 [ { "a" "b" "c" "d" } ] [ { 0 1 2 3 } { "a" "b" "c" "d" } nths ] unit-test
279 [ { "d" "c" "b" "a" } ] [ { 3 2 1 0 } { "a" "b" "c" "d" } nths ] unit-test
280 [ { "d" "a" "b" "c" } ] [ { 3 0 1 2 } { "a" "b" "c" "d" } nths ] unit-test
281
282 [ "dac" ] [ { 3 0 2 } "abcd" nths ] unit-test
283                           
284 TUPLE: bogus-hashcode ;
285
286 M: bogus-hashcode hashcode* 2drop 0 >bignum ;
287
288 [ 0 ] [ { T{ bogus-hashcode } } hashcode ] unit-test
289
290 [ { 2 4 6 } { 1 3 5 7 } ] [ { 1 2 3 4 5 6 7 } [ even? ] partition ] unit-test
291
292 [ { 1 3 7 } ] [ 2 { 1 3 5 7 } remove-nth ] unit-test
293
294 [ { 1 3 "X" 5 7 } ] [ "X" 2 { 1 3 5 7 } insert-nth ] unit-test
295
296 [ V{ 0 2 } ] [ "a" { "a" "b" "a" } indices ] unit-test
297
298 [ "a,b" ] [ "a" "b" "," glue ] unit-test
299 [ "(abc)" ] [ "abc" "(" ")" surround ] unit-test
300
301 [ "HELLO" ] [
302     "HELLO" { -1 -1 -1 -1 -1 } { 2 2 2 2 2 2 }
303     [ * 2 + + ] 3map
304 ] unit-test
305
306 { 3 1 } [ [ 3array ] 3map ] must-infer-as
307
308 { 3 0 } [ [ 3drop ] 3each ] must-infer-as
309
310 [ V{ 0 3 } ] [ "A" { "A" "B" "C" "A" "D" } indices ] unit-test
311
312 [ "asdf" iota ] must-fail
313 [ T{ iota { n 10 } } ] [ 10 iota ] unit-test
314 [ 0 ] [ 10 iota first ] unit-test
315
316 [ "hi" 3 ] [
317     { 1 2 3 4 5 6 7 8 } [ H{ { 3 "hi" } } at ] map-find
318 ] unit-test
319
320 [ f f ] [
321     { 1 2 3 4 5 6 7 8 } [ H{ { 11 "hi" } } at ] map-find
322 ] unit-test
323
324 USE: make
325
326 [ { "a" 1 "b" 1 "c" } ]
327 [ 1 { "a" "b" "c" } [ [ dup , ] [ , ] interleave drop ] { } make ] unit-test
328
329 [ t ] [ 0 array-capacity? ] unit-test
330 [ f ] [ -1 array-capacity? ] unit-test
331
332 [ +lt+ ] [ { 0 0 0 } { 1 1 1 } <=> ] unit-test
333 [ +lt+ ] [ { 0 0 0 } { 0 1 1 } <=> ] unit-test
334 [ +lt+ ] [ { 0 0 0 } { 0 0 0 0 } <=> ] unit-test
335 [ +gt+ ] [ { 1 1 1 } { 0 0 0 } <=> ] unit-test
336 [ +gt+ ] [ { 0 1 1 } { 0 0 0 } <=> ] unit-test
337 [ +gt+ ] [ { 0 0 0 0 } { 0 0 0 } <=> ] unit-test
338 [ +eq+ ] [ { } { } <=> ] unit-test
339 [ +eq+ ] [ { 1 2 3 } { 1 2 3 } <=> ] unit-test
340
341 [ { { { 1 "a" } { 1 "b" } } { { 2 "a" } { 2 "b" } } } ]
342 [ { 1 2 } { "a" "b" } cartesian-product ] unit-test
343
344 [ { } [ string>digits sum ] [ + ] map-reduce ] must-infer
345 [ { } [ ] [ + ] map-reduce ] must-fail
346 [ 4 ] [ { 1 1 } [ 1 + ] [ + ] map-reduce ] unit-test
347
348 [ { } { } [ [ string>digits product ] bi@ + ] [ + ] 2map-reduce ] must-infer
349 [ { } { } [ + ] [ + ] 2map-reduce ] must-fail
350 [ 24 ] [ { 1 2 } { 3 4 } [ + ] [ * ] 2map-reduce ] unit-test