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