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