]> gitweb.factorcode.org Git - factor.git/blob - extra/sequences/extras/extras-tests.factor
23100570db780b35718c39ff7d5eee51456b7212
[factor.git] / extra / sequences / extras / extras-tests.factor
1 USING: accessors arrays ascii io io.streams.string kernel make
2 math math.vectors random sequences sequences.extras strings
3 tools.test vectors vocabs ;
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 { t } [ { 1 2 3 4 5 } 1 first= ] unit-test
167 { t } [ { 1 2 3 4 5 } 2 second= ] unit-test
168 { t } [ { 1 2 3 4 5 } 3 third= ] unit-test
169 { t } [ { 1 2 3 4 5 } 4 fourth= ] unit-test
170 { t } [ { 1 2 3 4 5 } 5 last= ] unit-test
171 { t } [ 4 { 1 2 3 4 5 } 5 nth= ] unit-test
172
173 { t } [ { 1 2 3 4 5 } [ 1 = ] first? ] unit-test
174 { t } [ { 1 2 3 4 5 } [ 2 = ] second? ] unit-test
175 { t } [ { 1 2 3 4 5 } [ 3 = ] third? ] unit-test
176 { t } [ { 1 2 3 4 5 } [ 4 = ] fourth? ] unit-test
177 { t } [ { 1 2 3 4 5 } [ 5 = ] last? ] unit-test
178 { t } [ 4 { 1 2 3 4 5 } [ 5 = ] nth? ] unit-test
179
180 { { 97 115 100 102 } } [
181     "asdf" [ [ read1 ] loop>array ] with-string-reader
182 ] unit-test
183
184 { V{ 97 115 100 102 } } [
185     "asdf" [ [ read1 ] V{ } loop>sequence ] with-string-reader
186 ] unit-test
187
188 { "" } [ { } "" reverse-as ] unit-test
189 { "ABC" } [ { 67 66 65 } "" reverse-as ] unit-test
190
191 { V{ 1 } } [ 1 0 V{ } [ insert-nth! ] keep ] unit-test
192 { V{ 1 2 3 4 } } [ 2 1 V{ 1 3 4 } [ insert-nth! ] keep ] unit-test
193
194 { "abc" } [ B{ 97 98 99 100 101 102 103 } 3 "" head-as ] unit-test
195 { "abcd" } [ B{ 97 98 99 100 101 102 103 } 3 "" head*-as ] unit-test
196 { "defg" } [ B{ 97 98 99 100 101 102 103 } 3 "" tail-as ] unit-test
197 { "efg" } [ B{ 97 98 99 100 101 102 103 } 3 "" tail*-as ] unit-test
198
199 { { 1 0 0 1 0 0 0 1 0 0 } }
200 [ 1 { 0 3 7 } 10 0 <array> [ set-nths ] keep ] unit-test
201
202 { { 1 0 0 1 0 0 0 1 0 0 } }
203 [ 1 { 0 3 7 } 10 0 <array> [ set-nths-unsafe ] keep ] unit-test
204
205 { t 3 3 } [ 10 <iota> [ [ odd? ] [ 1 > ] bi* and ] map-find-index ] unit-test
206 { f f f } [ 10 <iota> [ [ odd? ] [ 9 > ] bi* and ] map-find-index ] unit-test
207
208 { "abcdef" } [ f f "abcdef" subseq* ] unit-test
209 { "abcdef" } [ 0 f "abcdef" subseq* ] unit-test
210 { "ab" } [ f 2 "abcdef" subseq* ] unit-test
211 { "cdef" } [ 2 f "abcdef" subseq* ] unit-test
212 { "cd" } [ -4 -2 "abcdef" subseq* ] unit-test
213
214 { "foo" "" } [ "foo" [ ascii:blank? ] cut-when ] unit-test
215 { "foo" " " } [ "foo " [ ascii:blank? ] cut-when ] unit-test
216 { "" " foo" } [ " foo" [ ascii:blank? ] cut-when ] unit-test
217 { "foo" " bar" } [ "foo bar" [ ascii:blank? ] cut-when ] unit-test
218
219 { { 4 0 3 1 2 } } [ { 0 4 1 3 2 } 5 <iota> [ nth* ] curry map ] unit-test
220
221 { 1 "beef" } [ { "chicken" "beef" "moose" } [ length ] infimum-by* ] unit-test
222 { 0 "chicken" } [ { "chicken" "beef" "moose" } [ length ] supremum-by* ] unit-test
223 { 2 "moose" } [ { "chicken" "beef" "moose" } [ first ] supremum-by* ] unit-test
224 { f } [ f ?supremum ] unit-test
225 { f } [ { } ?supremum ] unit-test
226 { f } [ { f } ?supremum ] unit-test
227 { 3 } [ { 1 f 3 2 } ?supremum ] unit-test
228 { 3 } [ { 1 3 2 } ?supremum ] unit-test
229 { f } [ f ?infimum ] unit-test
230 { f } [ { } ?infimum ] unit-test
231 { f } [ { f } ?infimum ] unit-test
232 { 1 } [ { 1 f 3 2 } ?infimum ] unit-test
233 { 1 } [ { 1 3 2 } ?infimum ] unit-test
234
235 { 3/10 } [ 10 <iota> [ 3 < ] count* ] unit-test
236
237 { { 0 } } [ "ABABA" "ABA" start-all ] unit-test
238 { { 0 2 } } [ "ABABA" "ABA" start-all* ] unit-test
239 { { 0 3 } } [ "ABAABA" "ABA" start-all ] unit-test
240 { 1 } [ "ABABA" "ABA" count-subseq ] unit-test
241 { 2 } [ "ABABA" "ABA" count-subseq* ] unit-test
242
243 { 120000 } [ { 10 20 30 40 50 60 } 1 [ * ] 3 reduce-from ] unit-test
244
245 { { 0 1 2 3 } } [ 8 <iota> [ 4 < ] take-while >array ] unit-test
246 { { } } [ { 15 16 } [ 4 < ] take-while >array ] unit-test
247 { { 0 1 2 } } [ 3 <iota> [ 4 < ] take-while >array ] unit-test
248
249 { { 4 5 6 7 } } [ 8 <iota> [ 4 < ] drop-while >array ] unit-test
250 { { 15 16 } } [ { 15 16 } [ 4 < ] drop-while >array ] unit-test
251 { { } } [ 3 <iota> [ 4 < ] drop-while >array ] unit-test
252
253 { { } } [ { } ", " interleaved ] unit-test
254 { { 1 } } [ { 1 } ", " interleaved ] unit-test
255 { { 1 ", " 2 } } [ { 1 2 } ", " interleaved ] unit-test
256 { "" } [ "" CHAR: _ interleaved ] unit-test
257 { "a" } [ "a" CHAR: _ interleaved ] unit-test
258 { "a_b" } [ "ab" CHAR: _ interleaved ] unit-test
259 { "a_b_c" } [ "abc" CHAR: _ interleaved ] unit-test
260 { "a_b_c_d" } [ "abcd" CHAR: _ interleaved ] unit-test
261
262 { 0 } [ { 1 2 3 4 } [ 5 > ] count-head ] unit-test
263 { 2 } [ { 1 2 3 4 } [ 3 < ] count-head ] unit-test
264 { 4 } [ { 1 2 3 4 } [ 5 < ] count-head ] unit-test
265
266 { 0 } [ { 1 2 3 4 } [ 5 > ] count-tail ] unit-test
267 { 2 } [ { 1 2 3 4 } [ 2 > ] count-tail ] unit-test
268 { 4 } [ { 1 2 3 4 } [ 5 < ] count-tail ] unit-test
269
270 { SBUF" aco" SBUF" ftr"  } [ SBUF" factor" dup [ even? ] extract! ] unit-test
271
272 { 25 5 1 } [ { 4 5 6 } [ sq ] [ 20 > ] find-pred ] unit-test
273 { f f f } [ { 4 5 6 } [ sq ] [ 200 > ] find-pred ] unit-test
274
275 { -1/0. } [ { } max-subarray-sum ] unit-test
276 { -2 } [ { -3 -2 } max-subarray-sum ] unit-test
277 { 7 } [ { 1 2 3 -4 5 } max-subarray-sum ] unit-test
278 { 6 } [ { 1 2 3 -4 1 1 } max-subarray-sum ] unit-test
279
280 { { 9 7 5 } } [ -1 -6 -2 10 <iota> <step-slice> >array ] unit-test
281 { { 9 7 } } [ -1 -5 -2 10 <iota> <step-slice> >array ] unit-test
282 { { 9 7 } } [ -1 -4 -2 10 <iota> <step-slice> >array ] unit-test
283 { { 9 } } [ -1 -3 -2 10 <iota> <step-slice> >array ] unit-test
284 { { } } [ -4 10 -2 10 <iota> <step-slice> >array ] unit-test
285 { { 6 8 } } [ -4 15 2 10 <iota> <step-slice> >array ] unit-test
286 { { 1 3 } } [ 1 4 2 10 <iota> <step-slice> >array ] unit-test
287 { { 1 3 } } [ 1 5 2 10 <iota> <step-slice> >array ] unit-test
288 { { 1 3 5 } } [ 1 6 2 10 <iota> <step-slice> >array ] unit-test
289
290 { { 102 306 1530 } } [
291     { 2 3 5 } [ swap [ * ] [ 100 + ] if* ] map-with-previous
292 ] unit-test
293
294 { { } } [
295     { } [ nip ] map-with-previous
296 ] unit-test
297
298 { { -1 2 -3 4 -5 } } [ { 1 2 3 4 5 } [ odd? ] [ neg ] map-if ] unit-test