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