]> gitweb.factorcode.org Git - factor.git/blob - core/sequences/sequences-tests.factor
Fix conflict in images vocab
[factor.git] / core / sequences / sequences-tests.factor
1 USING: arrays kernel math 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 <slice> >vector ] unit-test
10 [ 3 ] [ 1 4 dup <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 <slice> subseq >vector ] unit-test
15 [ V{ 3 4 } ] [ 0 2 2 4 1 10 dup <slice> <slice> subseq >vector ] unit-test
16 [ "cba" ] [ "abcdef" 3 head-slice reverse ] unit-test
17
18 [ 5040 ] [ [ 1 2 3 4 5 6 7 ] 1 [ * ] reduce ] unit-test
19
20 [ 5040 { 1 1 2 6 24 120 720 } ]
21 [ { 1 2 3 4 5 6 7 } 1 [ * ] accumulate ] unit-test
22
23 [ f f ] [ [ ] [ ] find ] unit-test
24 [ 0 1 ] [ [ 1 ] [ ] find ] unit-test
25 [ 1 "world" ] [ [ "hello" "world" ] [ "world" = ] find ] unit-test
26 [ 2 3 ] [ [ 1 2 3 ] [ 2 > ] find ] unit-test
27 [ f f ] [ [ 1 2 3 ] [ 10 > ] find ] unit-test
28
29 [ 1 CHAR: e ]
30 [ "hello world" "aeiou" [ member? ] curry find ] unit-test
31
32 [ 4 CHAR: o ]
33 [ 3 "hello world" "aeiou" [ member? ] curry find-from ] unit-test
34
35 [ f ] [ 3 [ ]     member? ] unit-test
36 [ f ] [ 3 [ 1 2 ] member? ] unit-test
37 [ t ] [ 1 [ 1 2 ] member? ] unit-test
38 [ t ] [ 2 [ 1 2 ] member? ] unit-test
39
40 [ t ]
41 [ [ "hello" "world" ] [ second ] keep memq? ] unit-test
42
43 [ 4 ] [ CHAR: x "tuvwxyz" >vector index ] unit-test 
44
45 [ f ] [ CHAR: x 5 "tuvwxyz" >vector index-from ] unit-test 
46
47 [ f ] [ CHAR: a 0 "tuvwxyz" >vector index-from ] unit-test
48
49 [ f ] [ [ "Hello" { } 0.75 ] [ string? ] all? ] unit-test
50 [ t ] [ [ ] [ ] all? ] unit-test
51 [ t ] [ [ "hi" t 0.5 ] [ ] all? ] unit-test
52
53 [ [ 1 2 3 ] ] [ [ 1 4 2 5 3 6 ] [ 4 < ] filter ] unit-test
54 [ { 4 2 6 } ] [ { 1 4 2 5 3 6 } [ 2 mod 0 = ] filter ] unit-test
55
56 [ [ 3 ] ] [ [ 1 2 3 ] 2 [ swap < ] curry filter ] unit-test
57
58 [ V{ 1 2 3 } ] [ V{ 1 4 2 5 3 6 } clone [ [ 4 < ] filter-here ] keep ] unit-test
59 [ V{ 4 2 6 } ] [ V{ 1 4 2 5 3 6 } clone [ [ 2 mod 0 = ] filter-here ] keep ] unit-test
60
61 [ V{ 3 } ] [ V{ 1 2 3 } clone [ 2 [ swap < ] curry filter-here ] keep ] unit-test
62
63 [ "hello world how are you" ]
64 [ { "hello" "world" "how" "are" "you" } " " join ]
65 unit-test
66
67 [ "" ] [ { } "" join ] unit-test
68
69 [ { } ] [ { } flip ] unit-test
70
71 [ { "b" "e" } ] [ 1 { { "a" "b" "c" } { "d" "e" "f" } } flip nth ] unit-test
72
73 [ { { 1 4 } { 2 5 } { 3 6 } } ]
74 [ { { 1 2 3 } { 4 5 6 } } flip ] unit-test
75
76 [ [ 2 3 4 ] ] [ [ 1 2 3 ] 1 [ + ] curry map ] unit-test
77
78 [ 1 ] [ 0 [ 1 2 ] nth ] unit-test
79 [ 2 ] [ 1 [ 1 2 ] nth ] unit-test
80
81 [ [ ]           ] [ [ ]   [ ]       append ] unit-test
82 [ [ 1 ]         ] [ [ 1 ] [ ]       append ] unit-test
83 [ [ 2 ]         ] [ [ ] [ 2 ]       append ] unit-test
84 [ [ 1 2 3 4 ]   ] [ [ 1 2 3 ] [ 4 ] append ] unit-test
85 [ [ 1 2 3 4 ]   ] [ [ 1 2 3 ] { 4 } append ] unit-test
86
87 [ "a" -1 append ] must-fail
88 [ -1 "a" append ] must-fail
89
90 [ [ ]       ] [ 1 [ ]           remove ] unit-test
91 [ [ ]       ] [ 1 [ 1 ]         remove ] unit-test
92 [ [ 3 1 1 ] ] [ 2 [ 3 2 1 2 1 ] remove ] unit-test
93
94 [ [ ]       ] [ [ ]       reverse ] unit-test
95 [ [ 1 ]     ] [ [ 1 ]     reverse ] unit-test
96 [ [ 3 2 1 ] ] [ [ 1 2 3 ] reverse ] unit-test
97
98 [ f ] [ f 0 head ] unit-test
99 [ [ ] ] [ [ 1 ] 0 head ] unit-test
100 [ [ 1 2 3 ] ] [ [ 1 2 3 4 ] 3 head ] unit-test
101 [ [ ] ] [ [ 1 2 3 ] 3 tail ] unit-test
102 [ [ 3 ] ] [ [ 1 2 3 ] 2 tail ] unit-test
103
104 [ "blah" ] [ "blahxx" 2 head* ] unit-test
105
106 [ "xx" ] [ "blahxx" 2 tail* ] unit-test
107
108 [ t ] [ "xxfoo" 2 head-slice "xxbar" 2 head-slice = ] unit-test
109 [ t ] [ "xxfoo" 2 head-slice "xxbar" 2 head-slice [ hashcode ] bi@ = ] unit-test
110
111 [ t ] [ "xxfoo" 2 head-slice SBUF" barxx" 2 tail-slice* = ] unit-test
112 [ t ] [ "xxfoo" 2 head-slice SBUF" barxx" 2 tail-slice* [ hashcode ] bi@ = ] unit-test
113
114 [ t ] [ [ 1 2 3 ] [ 1 2 3 ] sequence= ] unit-test
115 [ t ] [ [ 1 2 3 ] { 1 2 3 } sequence= ] unit-test
116 [ t ] [ { 1 2 3 } [ 1 2 3 ] sequence= ] unit-test
117 [ f ] [ [ ] [ 1 2 3 ] sequence= ] unit-test
118
119 [ { 1 3 2 4 } ] [ { 1 2 3 4 } clone 1 2 pick exchange ] unit-test
120
121 [ { "" "a" "aa" "aaa" } ]
122 [ 4 [ CHAR: a <string> ] map ]
123 unit-test
124
125 [ V{ } ] [ "f" V{ } clone [ delete ] keep ] unit-test
126 [ V{ } ] [ "f" V{ "f" } clone [ delete ] keep ] unit-test
127 [ V{ } ] [ "f" V{ "f" "f" } clone [ delete ] keep ] unit-test
128 [ V{ "x" } ] [ "f" V{ "f" "x" "f" } clone [ delete ] keep ] unit-test
129 [ V{ "y" "x" } ] [ "f" V{ "y" "f" "x" "f" } clone [ delete ] keep ] unit-test
130
131 [ V{ 0 1 4 5 } ] [ 6 >vector 2 4 pick delete-slice ] unit-test
132
133 [ 6 >vector 2 8 pick delete-slice ] must-fail
134
135 [ V{ } ] [ 6 >vector 0 6 pick delete-slice ] unit-test
136
137 [ { 1 2 "a" "b" 5 6 7 } ] [
138     { "a" "b" } 2 4 { 1 2 3 4 5 6 7 }
139     replace-slice
140 ] unit-test
141
142 [ { 1 2 "a" "b" 6 7 } ] [
143     { "a" "b" } 2 5 { 1 2 3 4 5 6 7 }
144     replace-slice
145 ] unit-test
146
147 [ { 1 2 "a" "b" 4 5 6 7 } ] [
148     { "a" "b" } 2 3 { 1 2 3 4 5 6 7 }
149     replace-slice
150 ] unit-test
151
152 [ { 1 2 3 4 5 6 7 "a" "b" } ] [
153     { "a" "b" } 7 7 { 1 2 3 4 5 6 7 }
154     replace-slice
155 ] unit-test
156
157 [ { "a" 3 } ] [
158     { "a" } 0 2 { 1 2 3 } replace-slice
159 ] unit-test
160
161 [ { 1 4 9 } ] [ { 1 2 3 } clone dup [ sq ] change-each ] unit-test
162
163 [ 5 ] [ 1 >bignum { 1 5 7 } nth-unsafe ] unit-test
164 [ 5 ] [ 1 >bignum { 1 5 7 } nth-unsafe ] unit-test
165 [ 5 ] [ 1 >bignum "\u000001\u000005\u000007" nth-unsafe ] unit-test
166
167 [ SBUF" before&after" ] [
168     "&" 6 11 SBUF" before and after" replace-slice
169 ] unit-test
170
171 [ 3 "a" ] [ { "a" "b" "c" "a" "d" } [ "a" = ] find-last ] unit-test
172
173 [ f f ] [ 100 { 1 2 3 } [ 1 = ] find-from ] unit-test
174 [ f f ] [ 100 { 1 2 3 } [ 1 = ] find-last-from ] unit-test
175 [ f f ] [ -1 { 1 2 3 } [ 1 = ] find-from ] unit-test
176
177 [ 0 ] [ { "a" "b" "c" } { "A" "B" "C" } mismatch ] unit-test
178
179 [ 1 ] [ { "a" "b" "c" } { "a" "B" "C" } mismatch ] unit-test
180
181 [ f ] [ { "a" "b" "c" } { "a" "b" "c" } mismatch ] unit-test
182
183 [ V{ } V{ } ] [ { "a" "b" } { "a" "b" } drop-prefix [ >vector ] bi@ ] unit-test
184
185 [ V{ "C" } V{ "c" } ] [ { "a" "b" "C" } { "a" "b" "c" } drop-prefix [ >vector ] bi@ ] unit-test
186
187 [ -1 1 "abc" <slice> ] must-fail
188
189 [ V{ "a" "b" } V{ } ] [ { "X" "a" "b" } { "X" } drop-prefix [ >vector ] bi@ ] unit-test
190
191 [ 1 ] [ 0.5 { 1 2 3 } nth ] unit-test
192
193 ! Pathological case
194 [ "ihbye" ] [ "hi" <reversed> "bye" append ] unit-test
195
196 [ t ] [ "hi" <reversed> SBUF" hi" <reversed> = ] unit-test
197
198 [ t ] [ "hi" <reversed> SBUF" hi" <reversed> = ] unit-test
199
200 [ t ] [ "hi" <reversed> SBUF" hi" <reversed> [ hashcode ] bi@ = ] unit-test
201
202 [ -10 "hi" "bye" copy ] must-fail
203 [ 10 "hi" "bye" copy ] must-fail
204
205 [ V{ 1 2 3 5 6 } ] [
206     3 V{ 1 2 3 4 5 6 } clone [ delete-nth ] keep
207 ] unit-test
208
209 ! erg's random tester found this one
210 [ SBUF" 12341234" ] [
211     9 <sbuf> dup "1234" swap push-all dup dup swap push-all
212 ] unit-test
213
214 [ f ] [ f V{ } like f V{ } like eq? ] unit-test
215
216 [ V{ f f f } ] [ 3 V{ } new-sequence ] unit-test
217 [ SBUF" \0\0\0" ] [ 3 SBUF" " new-sequence ] unit-test
218
219 [ 0 ] [ f length ] unit-test
220 [ f first ] must-fail
221 [ 3 ] [ 3 10 nth ] unit-test
222 [ 3 ] [ 3 10 nth-unsafe ] unit-test
223 [ -3 10 nth ] must-fail
224 [ 11 10 nth ] must-fail
225
226 [ -1./0. 0 delete-nth ] must-fail
227 [ "" ] [ "" [ CHAR: \s = ] trim ] unit-test
228 [ "" ] [ "" [ CHAR: \s = ] trim-head ] unit-test
229 [ "" ] [ "" [ CHAR: \s = ] trim-tail ] unit-test
230 [ "" ] [ "  " [ CHAR: \s = ] trim-head ] unit-test
231 [ "" ] [ "  " [ CHAR: \s = ] trim-tail ] unit-test
232 [ "asdf" ] [ " asdf " [ CHAR: \s = ] trim ] unit-test
233 [ "asdf " ] [ " asdf " [ CHAR: \s = ] trim-head ] unit-test
234 [ " asdf" ] [ " asdf " [ CHAR: \s = ] trim-tail ] unit-test
235
236 [ 328350 ] [ 100 [ sq ] sigma ] unit-test
237
238 [ 50 ] [ 100 [ even? ] count ] unit-test
239 [ 50 ] [ 100 [ odd?  ] count ] unit-test
240
241 [ { "b" "d" } ] [ { 1 3 } { "a" "b" "c" "d" } nths ] unit-test
242 [ { "a" "b" "c" "d" } ] [ { 0 1 2 3 } { "a" "b" "c" "d" } nths ] unit-test
243 [ { "d" "c" "b" "a" } ] [ { 3 2 1 0 } { "a" "b" "c" "d" } nths ] unit-test
244 [ { "d" "a" "b" "c" } ] [ { 3 0 1 2 } { "a" "b" "c" "d" } nths ] unit-test
245                           
246 TUPLE: bogus-hashcode ;
247
248 M: bogus-hashcode hashcode* 2drop 0 >bignum ;
249
250 [ 0 ] [ { T{ bogus-hashcode } } hashcode ] unit-test
251
252 [ { 2 4 6 } { 1 3 5 7 } ] [ { 1 2 3 4 5 6 7 } [ even? ] partition ] unit-test
253
254 [ { 1 3 7 } ] [ 2 { 1 3 5 7 } remove-nth ] unit-test
255
256 [ { 1 3 "X" 5 7 } ] [ "X" 2 { 1 3 5 7 } insert-nth ] unit-test
257
258 [ V{ 0 2 } ] [ "a" { "a" "b" "a" } indices ] unit-test
259
260 [ "a,b" ] [ "a" "b" "," glue ] unit-test
261 [ "(abc)" ] [ "abc" "(" ")" surround ] unit-test
262
263 [ "HELLO" ] [
264     "HELLO" { -1 -1 -1 -1 -1 } { 2 2 2 2 2 2 }
265     [ * 2 + + ] 3map
266 ] unit-test
267
268 { 3 1 } [ [ 3array ] 3map ] must-infer-as
269
270 { 3 0 } [ [ 3drop ] 3each ] must-infer-as
271
272 [ V{ 0 3 } ] [ "A" { "A" "B" "C" "A" "D" } indices ] unit-test
273
274 [ "asdf" iota ] must-fail
275 [ T{ iota { n 10 } } ] [ 10 iota ] unit-test
276 [ 0 ] [ 10 iota first ] unit-test
277
278 [ "hi" 3 ] [
279     { 1 2 3 4 5 6 7 8 } [ H{ { 3 "hi" } } at ] map-find
280 ] unit-test
281
282 [ f f ] [
283     { 1 2 3 4 5 6 7 8 } [ H{ { 11 "hi" } } at ] map-find
284 ] unit-test