]> gitweb.factorcode.org Git - factor.git/blob - extra/sequences/extras/extras-tests.factor
3cc68c9515ae683b1212911024becb5dec9e1b5d
[factor.git] / extra / sequences / extras / extras-tests.factor
1 USING: accessors arrays ascii grouping io io.streams.string
2 kernel make math prettyprint ranges sequences sequences.extras
3 strings tools.test ;
4
5 { V{ { 0 104 } { 2 108 } { 3 108 } } } [ "hello" [ even? ] find-all ] unit-test
6
7 { { "a" "b" "c" "d" "ab" "bc" "cd" "abc" "bcd" "abcd" } } [ "abcd" all-subseqs ] unit-test
8
9 { { "a" "ab" "abc" "abcd" "b" "bc" "bcd" "c" "cd" "d" } }
10 [ [ "abcd" [ , ] each-subseq ] { } make ] unit-test
11
12 { B{ 115 } } [ 1 2 "asdf" B{ } subseq-as ] unit-test
13
14 { "" } [ "abc" "def" longest-subseq ] unit-test
15 { "abcd" } [ "abcd" "abcde" longest-subseq ] unit-test
16 { "foo" } [ "foo" "foobar" longest-subseq ] unit-test
17 { "foo" } [ "foobar" "foo" longest-subseq ] unit-test
18
19 { "" "" } [ "" "" CHAR: ? pad-longest ] unit-test
20 { "abc" "def" } [ "abc" "def" CHAR: ? pad-longest ] unit-test
21 { "   " "abc" } [ "" "abc" CHAR: \s pad-longest ] unit-test
22 { "abc" "   " } [ "abc" "" CHAR: \s pad-longest ] unit-test
23 { "abc..." "foobar" } [ "abc" "foobar" CHAR: . pad-longest ] unit-test
24
25 {
26     {
27         "ABC"
28         "ABC"
29         "ABC"
30         "ABC"
31         "ABC-"
32         "-ABC-"
33         "-ABC--"
34         "--ABC--"
35     }
36 } [
37     "ABC" 8 <iota> [ CHAR: - pad-center ] with map
38 ] unit-test
39
40 { { 0 1 0 1 } } [
41     { 0 0 0 0 } { 1 3 } over [ 1 + ] change-nths
42 ] unit-test
43
44 { V{ f t f } } [
45     { 1 2 3 } [ even? ] selector* [ each ] dip
46 ] unit-test
47
48 { { 1 3 5 } } [ { 1 2 3 4 5 6 } [ nip even? ] filter-index ] unit-test
49
50 { V{ 1 3 5 } } [ { 1 2 3 4 5 6 } [ nip even? ] V{ } filter-index-as ] unit-test
51
52 { { 1 3 5 } } [ { 1 2 3 4 5 6 } even-indices ] unit-test
53
54 { { 2 4 6 } } [ { 1 2 3 4 5 6 } odd-indices ] unit-test
55
56 { "a b c d e" }
57 [ "a      b  \t \n \r  c   d \n    e   " [ ascii:blank? ] " " compact ] unit-test
58
59 { " a b c d e " }
60 [ " a      b  c   d    e   " [ ascii:blank? ] " " collapse ] unit-test
61
62 { { "hello," " " "world!" " " " " } }
63 [ "hello, world!  " [ ascii:blank? ] slice-when [ >string ] map ] unit-test
64
65 { t }
66 [ "abc" sequence>slice slice? ] unit-test
67
68 { "abc" }
69 [ "abc" sequence>slice >string ] unit-test
70
71 { t } [ "abcdef" [ 0 3 rot <slice> ] [ 2 4 rot <slice> ] bi slices-overlap? ] unit-test
72 { t } [ "abcdef" [ 0 3 rot <slice> ] [ 1 2 rot <slice> ] bi slices-overlap? ] unit-test
73 { f } [ "abcdef" [ 0 3 rot <slice> ] [ 3 6 rot <slice> ] bi slices-overlap? ] unit-test
74 { t } [ "abcdef" [ 0 3 rot <slice> ] [ 2 4 rot <slice> ] bi slices-touch? ] unit-test
75 { t } [ "abcdef" [ 0 3 rot <slice> ] [ 1 2 rot <slice> ] bi slices-touch? ] unit-test
76 { t } [ "abcdef" [ 0 3 rot <slice> ] [ 3 6 rot <slice> ] bi slices-touch? ] unit-test
77 { f } [ "abcdef" [ 0 3 rot <slice> ] [ 4 6 rot <slice> ] bi slices-touch? ] unit-test
78
79 { "abcdef" } [
80     "abcdef" [ 0 3 rot <slice> ] [ 3 6 rot <slice> ] bi merge-slices >string
81 ] unit-test
82
83 { "abcdef" } [
84     "abcdef" [ 3 6 rot <slice> ] [ 0 3 rot <slice> ] bi merge-slices >string
85 ] unit-test
86
87 { "abc" } [
88     "abcdef" [ 0 3 rot <slice> ] [ 0 3 rot <slice> ] bi merge-slices >string
89 ] unit-test
90
91
92 { "hello" "hello" } [ "hello" dup 0 rotate ] unit-test
93 { "hello" "llohe" } [ "hello" dup 2 rotate ] unit-test
94 { "hello" "lohel" } [ "hello" dup 13 rotate ] unit-test
95 { "hello" "ohell" } [ "hello" dup -1 rotate ] unit-test
96 { "hello" "lohel" } [ "hello" dup -12 rotate ] unit-test
97
98 { "hello" } [ "hello" dup 0 rotate! ] unit-test
99 { "llohe" } [ "hello" dup 2 rotate! ] unit-test
100 { "lohel" } [ "hello" dup 13 rotate! ] unit-test
101 { "ohell" } [ "hello" dup -1 rotate! ] unit-test
102 { "lohel" } [ "hello" dup -12 rotate! ] unit-test
103
104 { { } } [ { } [ ] map-concat ] unit-test
105 { V{ 0 0 1 0 1 2 } } [ 4 <iota> [ <iota> ] map-concat ] unit-test
106 { "abc" } [ "abc" [ 1string ] map-concat ] unit-test
107 { "abc" } [ { 97 98 99 } [ 1string ] map-concat ] unit-test
108 { { 97 98 99 } } [ "abc" [ 1string ] { } map-concat-as ] unit-test
109 { "baz" { "foobaz" "barbaz" } }
110 [ "baz" { { "foo" } { "bar" } } [ [ over append ] map ] map-concat ] unit-test
111
112 { { } } [ { } [ ] [ even? ] map-filter ] unit-test
113 { "bcde" } [ "abcd" [ 1 + ] [ drop t ] map-filter ] unit-test
114 { { 0 4 16 36 64 } } [ 10 <iota> [ sq ] [ even? ] { } map-filter-as ] unit-test
115
116 { V{ 0 4 16 36 64 } } [ 10 <iota> [ even? ] [ sq ] filter-map ] unit-test
117 { { 2 6 10 14 18 } } [ 10 <iota> [ odd? ] [ 2 * ] { } filter-map-as ] unit-test
118
119 { 8 } [ 3 <iota> dup [ 1 + * ] 2map-sum ] unit-test
120 { 4 } [ "hello" "jello" [ = ] 2count ] unit-test
121
122 { { } } [ { } round-robin ] unit-test
123 { "ADEBFC" } [ { "ABC" "D" "EF" } round-robin >string ] unit-test
124
125 { { } } [ "ABC" [ ] { } trim-as ] unit-test
126 { "ABC" } [ { 32 65 66 67 32 } [ ascii:blank? ] "" trim-as ] unit-test
127
128 { t } [ "ABC" dup [ ascii:blank? ] ?trim [ identity-hashcode ] same? ] unit-test
129 { "ABC" } [ " ABC " [ ascii:blank? ] ?trim ] unit-test
130
131 { t } [ "ABC" dup [ ascii:blank? ] ?trim-head [ identity-hashcode ] same? ] unit-test
132 { t } [ "ABC" dup [ ascii:blank? ] ?trim-tail [ identity-hashcode ] same? ] unit-test
133 { "ABC " } [ " ABC " [ ascii:blank? ] ?trim-head ] unit-test
134 { " ABC" } [ " ABC " [ ascii:blank? ] ?trim-tail ] unit-test
135
136 { "" } [ "" "" "" unsurround ] unit-test
137 { "" } [ "  " " " " " unsurround ] unit-test
138 { "foo.com" } [ "http://foo.com" "http://" "/" unsurround ] unit-test
139
140 { t } [ { 1 3 5 7 } [ even? ] none? ] unit-test
141 { f } [ { 1 2 3 4 } [ even? ] none? ] unit-test
142 { t } [ { } [ even? ] none? ] unit-test
143
144 { f } [ { 1 2 3 4 } [ even? ] one? ] unit-test
145 { t } [ { 1 2 3 } [ even? ] one? ] unit-test
146 { f } [ { } [ even? ] one? ] unit-test
147
148 { { { 5 8 0 } { 6 9 1 } { 7 10 2 } } } [ { 5 6 7 } { 8 9 10 } [ 3array ] 2map-index ] unit-test
149
150 { { } } [ { } <evens> >array ] unit-test
151 { { 0 2 } } [ 4 <iota> <evens> >array ] unit-test
152 { { 0 2 4 } } [ 5 <iota> <evens> >array ] unit-test
153 { "bbddff" } [ "abcdef" <evens> [ 1 + ] map! seq>> ] unit-test
154
155 { { } } [ { } <odds> >array ] unit-test
156 { { 1 3 } } [ 5 <iota> <odds> >array ] unit-test
157 { { 1 3 5 } } [ 6 <iota> <odds> >array ] unit-test
158 { "acceeg" } [ "abcdef" <odds> [ 1 + ] map! seq>> ] unit-test
159
160 { 1 } [ { 1 7 3 7 6 3 7 } arg-max ] unit-test
161 { 2 } [ { 0 1 99 } arg-max ] unit-test
162 { 0 } [ { 1 7 3 7 6 3 7 } arg-min ] unit-test
163 { V{ 0 4 } } [ { 5 3 2 10 5 } [ 5 = ] arg-where ] unit-test
164 { { 2 1 0 4 3 } } [ { 5 3 2 10 5 } arg-sort ] unit-test
165
166 { 10 } [ { 4 3 2 1 } [ 10 * ] map-infimum ] unit-test
167 { 40 } [ { 4 3 2 1 } [ 10 * ] map-supremum ] unit-test
168
169 { t } [ { 1 2 3 4 5 } 1 first= ] unit-test
170 { t } [ { 1 2 3 4 5 } 2 second= ] unit-test
171 { t } [ { 1 2 3 4 5 } 3 third= ] unit-test
172 { t } [ { 1 2 3 4 5 } 4 fourth= ] unit-test
173 { t } [ { 1 2 3 4 5 } 5 last= ] unit-test
174 { t } [ 4 { 1 2 3 4 5 } 5 nth= ] unit-test
175
176 { t } [ { 1 2 3 4 5 } [ 1 = ] first? ] unit-test
177 { t } [ { 1 2 3 4 5 } [ 2 = ] second? ] unit-test
178 { t } [ { 1 2 3 4 5 } [ 3 = ] third? ] unit-test
179 { t } [ { 1 2 3 4 5 } [ 4 = ] fourth? ] unit-test
180 { t } [ { 1 2 3 4 5 } [ 5 = ] last? ] unit-test
181 { t } [ 4 { 1 2 3 4 5 } [ 5 = ] nth? ] unit-test
182
183 { { 97 115 100 102 } } [
184     "asdf" [ [ read1 ] loop>array ] with-string-reader
185 ] unit-test
186
187 { V{ 97 115 100 102 } } [
188     "asdf" [ [ read1 ] V{ } loop>sequence ] with-string-reader
189 ] unit-test
190
191 { "" } [ { } "" reverse-as ] unit-test
192 { "ABC" } [ { 67 66 65 } "" reverse-as ] unit-test
193
194 { V{ 1 } } [ 1 0 V{ } [ insert-nth! ] keep ] unit-test
195 { V{ 1 2 3 4 } } [ 2 1 V{ 1 3 4 } [ insert-nth! ] keep ] unit-test
196
197 { "abc" } [ B{ 97 98 99 100 101 102 103 } 3 "" head-as ] unit-test
198 { "abcd" } [ B{ 97 98 99 100 101 102 103 } 3 "" head*-as ] unit-test
199 { "defg" } [ B{ 97 98 99 100 101 102 103 } 3 "" tail-as ] unit-test
200 { "efg" } [ B{ 97 98 99 100 101 102 103 } 3 "" tail*-as ] unit-test
201
202 { { 1 0 0 1 0 0 0 1 0 0 } }
203 [ 1 { 0 3 7 } 10 0 <array> [ set-nths ] keep ] unit-test
204
205 { { 1 0 0 1 0 0 0 1 0 0 } }
206 [ 1 { 0 3 7 } 10 0 <array> [ set-nths-unsafe ] keep ] unit-test
207
208 { t 3 3 } [ 10 <iota> [ [ odd? ] [ 1 > ] bi* and ] map-find-index ] unit-test
209 { f f f } [ 10 <iota> [ [ odd? ] [ 9 > ] bi* and ] map-find-index ] unit-test
210
211 { "abcdef" } [ f f "abcdef" subseq* ] unit-test
212 { "abcdef" } [ 0 f "abcdef" subseq* ] unit-test
213 { "ab" } [ f 2 "abcdef" subseq* ] unit-test
214 { "cdef" } [ 2 f "abcdef" subseq* ] unit-test
215 { "cd" } [ -4 -2 "abcdef" subseq* ] unit-test
216
217 { "foo" "" } [ "foo" [ ascii:blank? ] cut-when ] unit-test
218 { "foo" " " } [ "foo " [ ascii:blank? ] cut-when ] unit-test
219 { "" " foo" } [ " foo" [ ascii:blank? ] cut-when ] unit-test
220 { "foo" " bar" } [ "foo bar" [ ascii:blank? ] cut-when ] unit-test
221
222 { { 4 0 3 1 2 } } [ { 0 4 1 3 2 } 5 <iota> [ nth* ] curry map ] unit-test
223
224 { 1 "beef" } [ { "chicken" "beef" "moose" } [ length ] infimum-by* ] unit-test
225 { 0 "chicken" } [ { "chicken" "beef" "moose" } [ length ] supremum-by* ] unit-test
226 { 2 "moose" } [ { "chicken" "beef" "moose" } [ first ] supremum-by* ] unit-test
227 { f } [ f ?supremum ] unit-test
228 { f } [ { } ?supremum ] unit-test
229 { f } [ { f } ?supremum ] unit-test
230 { 3 } [ { 1 f 3 2 } ?supremum ] unit-test
231 { 3 } [ { 1 3 2 } ?supremum ] unit-test
232 { f } [ f ?infimum ] unit-test
233 { f } [ { } ?infimum ] unit-test
234 { f } [ { f } ?infimum ] unit-test
235 { 1 } [ { 1 f 3 2 } ?infimum ] unit-test
236 { 1 } [ { 1 3 2 } ?infimum ] unit-test
237
238 { 3/10 } [ 10 <iota> [ 3 < ] count* ] unit-test
239
240 { { 0 } } [ "ABABA" "ABA" start-all ] unit-test
241 { { 0 2 } } [ "ABABA" "ABA" start-all* ] unit-test
242 { { 0 3 } } [ "ABAABA" "ABA" start-all ] unit-test
243 { 1 } [ "ABABA" "ABA" count-subseq ] unit-test
244 { 2 } [ "ABABA" "ABA" count-subseq* ] unit-test
245
246 { 120000 } [ { 10 20 30 40 50 60 } 1 [ * ] 3 reduce-from ] unit-test
247
248 { 0 } [ { } [ + ] 0reduce ] unit-test
249 { 107 } [ { 100 1 2 4 } [ + ] 0reduce ] unit-test
250
251 { { } } [ { } [ + ] 0accumulate ] unit-test
252 { { 100 101 103 107 } } [ { 100 1 2 4 } [ + ] 0accumulate ] unit-test
253
254 {
255     H{ { t 6 } { f 5 } }
256     { 0 0 1 1 2 3 4 2 3 4 5 }
257 } [
258     { 2 7 1 8 1 7 1 8 2 8 4 } [ even? ] occurrence-count-by
259 ] unit-test
260
261 {
262     H{ { 8 3 } { 1 3 } { 2 2 } { 4 1 } { 7 2 } }
263     { 0 0 0 0 1 1 2 1 1 2 0 }
264 } [
265     { 2 7 1 8 1 7 1 8 2 8 4 } occurrence-count
266 ] unit-test
267
268 {
269     H{ { 8 3 } { 1 3 } { 2 2 } { 4 1 } { 7 2 } }
270     { 0 0 0 0 1 1 2 1 1 2 0 }
271 } [
272     { 2 7 1 8 1 7 1 8 2 8 4 } [ ] occurrence-count-by
273 ] unit-test
274
275 {
276     H{
277         { 97 1 } { 98 1 } { 99 1 } { 100 1 } { 101 1 } { 102 1 }
278         { 103 1 } { 104 1 } { 105 1 } { 106 1 } { 107 1 } { 108 1 }
279         { 109 1 } { 110 1 } { 111 1 } { 112 1 }
280     }
281     { 1 2 0 3 3 3 3 3 3 3 3 3 3 3 3 3 }
282  } [
283     "cab" "abcdefghijklmnop" progressive-index
284 ] unit-test
285
286 { H{ { 97 5 } } { 0 1 2 3 3 } } [
287     "aaa" "aaaaa" progressive-index
288 ] unit-test
289
290 { H{ { 97 5 } { 98 5 } } { 0 3 1 4 2 5 5 5 5 5 } } [
291     "aaabb" "ababababab" progressive-index
292 ] unit-test
293
294 { { 0 1 2 3 } } [ 8 <iota> [ 4 < ] take-while >array ] unit-test
295 { { } } [ { 15 16 } [ 4 < ] take-while >array ] unit-test
296 { { 0 1 2 } } [ 3 <iota> [ 4 < ] take-while >array ] unit-test
297
298 { { 4 5 6 7 } } [ 8 <iota> [ 4 < ] drop-while >array ] unit-test
299 { { 15 16 } } [ { 15 16 } [ 4 < ] drop-while >array ] unit-test
300 { { } } [ 3 <iota> [ 4 < ] drop-while >array ] unit-test
301
302 { { } } [ { } ", " interleaved ] unit-test
303 { { 1 } } [ { 1 } ", " interleaved ] unit-test
304 { { 1 ", " 2 } } [ { 1 2 } ", " interleaved ] unit-test
305 { "" } [ "" CHAR: _ interleaved ] unit-test
306 { "a" } [ "a" CHAR: _ interleaved ] unit-test
307 { "a_b" } [ "ab" CHAR: _ interleaved ] unit-test
308 { "a_b_c" } [ "abc" CHAR: _ interleaved ] unit-test
309 { "a_b_c_d" } [ "abcd" CHAR: _ interleaved ] unit-test
310
311 { 0 } [ { 1 2 3 4 } [ 5 > ] count-head ] unit-test
312 { 2 } [ { 1 2 3 4 } [ 3 < ] count-head ] unit-test
313 { 4 } [ { 1 2 3 4 } [ 5 < ] count-head ] unit-test
314
315 { 0 } [ { 1 2 3 4 } [ 5 > ] count-tail ] unit-test
316 { 2 } [ { 1 2 3 4 } [ 2 > ] count-tail ] unit-test
317 { 4 } [ { 1 2 3 4 } [ 5 < ] count-tail ] unit-test
318
319 { SBUF" aco" SBUF" ftr"  } [ SBUF" factor" dup [ even? ] extract! ] unit-test
320
321 { 25 5 1 } [ { 4 5 6 } [ sq ] [ 20 > ] find-pred ] unit-test
322 { f f f } [ { 4 5 6 } [ sq ] [ 200 > ] find-pred ] unit-test
323
324 { -1/0. } [ { } max-subarray-sum ] unit-test
325 { -2 } [ { -3 -2 } max-subarray-sum ] unit-test
326 { 7 } [ { 1 2 3 -4 5 } max-subarray-sum ] unit-test
327 { 6 } [ { 1 2 3 -4 1 1 } max-subarray-sum ] unit-test
328
329 { { 9 7 5 } } [ -1 -6 -2 10 <iota> <step-slice> >array ] unit-test
330 { { 9 7 } } [ -1 -5 -2 10 <iota> <step-slice> >array ] unit-test
331 { { 9 7 } } [ -1 -4 -2 10 <iota> <step-slice> >array ] unit-test
332 { { 9 } } [ -1 -3 -2 10 <iota> <step-slice> >array ] unit-test
333 { { } } [ -4 10 -2 10 <iota> <step-slice> >array ] unit-test
334 { { 6 8 } } [ -4 15 2 10 <iota> <step-slice> >array ] unit-test
335 { { 1 3 } } [ 1 4 2 10 <iota> <step-slice> >array ] unit-test
336 { { 1 3 } } [ 1 5 2 10 <iota> <step-slice> >array ] unit-test
337 { { 1 3 5 } } [ 1 6 2 10 <iota> <step-slice> >array ] unit-test
338
339 { { 102 306 1530 } } [
340     { 2 3 5 } [ swap [ * ] [ 100 + ] if* ] map-with-previous
341 ] unit-test
342
343 { { } } [
344     { } [ nip ] map-with-previous
345 ] unit-test
346
347 { { -1 2 -3 4 -5 } } [ { 1 2 3 4 5 } [ odd? ] [ neg ] map-if ] unit-test
348
349 { { { 100 0 } { 200 1 } { 300 2 } { 400 3 } } } [
350     { 100 200 300 400 } <zip-index> >array
351 ] unit-test
352
353 { } [
354     { } [ - . ] each-prior
355 ] unit-test
356
357 { } [
358     1000 { } [ - . ] each-prior-from
359 ] unit-test
360
361 { } [
362     { 5 16 42 103 } [ - . ] each-prior
363 ] unit-test
364
365 { } [
366     1 { 5 16 42 103 } [ - . ] each-prior-from
367 ] unit-test
368
369
370 { { } } [
371     { } [ - ] map-prior
372 ] unit-test
373
374 { V{ 5 11 26 61 } } [
375     V{ 5 16 42 103 } [ - ] map-prior
376 ] unit-test
377
378 { V{ f f 26 61 } } [
379     2 V{ 5 16 42 103 } [ - ] map-prior-from
380 ] unit-test
381
382 { V{ f f 26 61 } } [
383     2 { 5 16 42 103 } [ - ] V{ } map-prior-from-as
384 ] unit-test
385
386 { V{ } } [
387     { } [ - ] V{ } map-prior-as
388 ] unit-test
389
390 { { 5 11 26 61 } } [
391     V{ 5 16 42 103 } [ - ] { } map-prior-as
392 ] unit-test
393
394 { f } [ 0 CHAR: a "foo" nth-index ] unit-test
395 { 0 } [ 0 CHAR: a "abba" nth-index ] unit-test
396 { 3 } [ 1 CHAR: a "abba" nth-index ] unit-test
397 { f } [ 2 CHAR: a "abba" nth-index ] unit-test
398
399 { { -995 11 26 61 } } [
400     1000 V{ 5 16 42 103 } [ - ] { } map-prior-identity-as
401 ] unit-test
402
403 { V{ 1 4 9 } } [
404     { 1 2 3 } { 1 2 3 }
405     [ 2dup 2array all-eq? [ * ] [ 2drop f ] if ]
406     V{ } 2nested-filter-as
407 ] unit-test
408
409 { V{ 1 8 27 } } [
410     { 1 2 3 } { 1 2 3 } { 1 2 3 }
411     [ 3dup 3array all-eq? [ * * ] [ 3drop f ] if ]
412     V{ } 3nested-filter-as
413 ] unit-test
414
415 { V{ 0 2 0 3 6 4 12 0 5 10 15 20 } } [
416     6 [1..b)
417     [ [0..b) ]
418     [ 2dup [ odd? ] bi@ or [ * ] [ 2drop f ] if  ]
419     2nested-filter*
420 ] unit-test