]> gitweb.factorcode.org Git - factor.git/blob - core/sequences/sequences-tests.factor
Move match to basis since compiler.tree.debugger uses it, fix conflict
[factor.git] / core / sequences / sequences-tests.factor
1 USING: arrays kernel math namespaces sequences kernel.private
2 sequences.private strings sbufs tools.test vectors
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 [ "hello world how are you" ]
59 [ { "hello" "world" "how" "are" "you" } " " join ]
60 unit-test
61
62 [ "" ] [ { } "" join ] unit-test
63
64 [ { } ] [ { } flip ] unit-test
65
66 [ { "b" "e" } ] [ 1 { { "a" "b" "c" } { "d" "e" "f" } } flip nth ] unit-test
67
68 [ { { 1 4 } { 2 5 } { 3 6 } } ]
69 [ { { 1 2 3 } { 4 5 6 } } flip ] unit-test
70
71 [ f ] [ [ { } { } "Hello" ] all-equal? ] unit-test
72 [ f ] [ [ { 2 } { } { } ] all-equal? ] unit-test
73 [ t ] [ [ ] all-equal? ] unit-test
74 [ t ] [ [ 1234 ] all-equal? ] unit-test
75 [ f ] [ [ 1.0 1 1 ] all-equal? ] unit-test
76 [ t ] [ { 1 2 3 4 } [ < ] monotonic? ] unit-test
77 [ f ] [ { 1 2 3 4 } [ > ] monotonic? ] unit-test
78 [ [ 2 3 4 ] ] [ [ 1 2 3 ] 1 [ + ] curry map ] unit-test
79
80 [ 1 ] [ 0 [ 1 2 ] nth ] unit-test
81 [ 2 ] [ 1 [ 1 2 ] nth ] unit-test
82
83 [ [ ]           ] [ [ ]   [ ]       append ] unit-test
84 [ [ 1 ]         ] [ [ 1 ] [ ]       append ] unit-test
85 [ [ 2 ]         ] [ [ ] [ 2 ]       append ] unit-test
86 [ [ 1 2 3 4 ]   ] [ [ 1 2 3 ] [ 4 ] append ] unit-test
87 [ [ 1 2 3 4 ]   ] [ [ 1 2 3 ] { 4 } append ] unit-test
88
89 [ "a" -1 append ] must-fail
90 [ -1 "a" append ] must-fail
91
92 [ [ ]       ] [ 1 [ ]           remove ] unit-test
93 [ [ ]       ] [ 1 [ 1 ]         remove ] unit-test
94 [ [ 3 1 1 ] ] [ 2 [ 3 2 1 2 1 ] remove ] unit-test
95
96 [ [ ]       ] [ [ ]       reverse ] unit-test
97 [ [ 1 ]     ] [ [ 1 ]     reverse ] unit-test
98 [ [ 3 2 1 ] ] [ [ 1 2 3 ] reverse ] unit-test
99
100 [ f ] [ f 0 head ] unit-test
101 [ [ ] ] [ [ 1 ] 0 head ] unit-test
102 [ [ 1 2 3 ] ] [ [ 1 2 3 4 ] 3 head ] unit-test
103 [ [ ] ] [ [ 1 2 3 ] 3 tail ] unit-test
104 [ [ 3 ] ] [ [ 1 2 3 ] 2 tail ] unit-test
105
106 [ "blah" ] [ "blahxx" 2 head* ] unit-test
107
108 [ "xx" ] [ "blahxx" 2 tail* ] unit-test
109
110 [ t ] [ "xxfoo" 2 head-slice "xxbar" 2 head-slice = ] unit-test
111 [ t ] [ "xxfoo" 2 head-slice "xxbar" 2 head-slice [ hashcode ] bi@ = ] unit-test
112
113 [ t ] [ "xxfoo" 2 head-slice SBUF" barxx" 2 tail-slice* = ] unit-test
114 [ t ] [ "xxfoo" 2 head-slice SBUF" barxx" 2 tail-slice* [ hashcode ] bi@ = ] unit-test
115
116 [ t ] [ [ 1 2 3 ] [ 1 2 3 ] sequence= ] unit-test
117 [ t ] [ [ 1 2 3 ] { 1 2 3 } sequence= ] unit-test
118 [ t ] [ { 1 2 3 } [ 1 2 3 ] sequence= ] unit-test
119 [ f ] [ [ ] [ 1 2 3 ] sequence= ] unit-test
120
121 [ { 1 3 2 4 } ] [ { 1 2 3 4 } clone 1 2 pick exchange ] unit-test
122
123 [ { "" "a" "aa" "aaa" } ]
124 [ 4 [ CHAR: a <string> ] map ]
125 unit-test
126
127 [ V{ } ] [ "f" V{ } clone [ delete ] keep ] unit-test
128 [ V{ } ] [ "f" V{ "f" } clone [ delete ] keep ] unit-test
129 [ V{ } ] [ "f" V{ "f" "f" } clone [ delete ] keep ] unit-test
130 [ V{ "x" } ] [ "f" V{ "f" "x" "f" } clone [ delete ] keep ] unit-test
131 [ V{ "y" "x" } ] [ "f" V{ "y" "f" "x" "f" } clone [ delete ] keep ] unit-test
132
133 [ V{ 0 1 4 5 } ] [ 6 >vector 2 4 pick delete-slice ] unit-test
134
135 [ 6 >vector 2 8 pick delete-slice ] must-fail
136
137 [ V{ } ] [ 6 >vector 0 6 pick delete-slice ] unit-test
138
139 [ V{ 1 2 "a" "b" 5 6 7 } ] [
140     { "a" "b" } 2 4 V{ 1 2 3 4 5 6 7 } clone
141     [ replace-slice ] keep
142 ] unit-test
143
144 [ V{ 1 2 "a" "b" 6 7 } ] [
145     { "a" "b" } 2 5 V{ 1 2 3 4 5 6 7 } clone
146     [ replace-slice ] keep
147 ] unit-test
148
149 [ V{ 1 2 "a" "b" 4 5 6 7 } ] [
150     { "a" "b" } 2 3 V{ 1 2 3 4 5 6 7 } clone
151     [ replace-slice ] keep
152 ] unit-test
153
154 [ V{ 1 2 3 4 5 6 7 "a" "b" } ] [
155     { "a" "b" } 7 7 V{ 1 2 3 4 5 6 7 } clone
156     [ replace-slice ] keep
157 ] unit-test
158
159 [ V{ "a" 3 } ] [
160     { "a" } 0 2 V{ 1 2 3 } clone [ replace-slice ] keep
161 ] unit-test
162
163 [ { 1 4 9 } ] [ { 1 2 3 } clone dup [ sq ] change-each ] unit-test
164
165 [ 5 ] [ 1 >bignum { 1 5 7 } nth-unsafe ] unit-test
166 [ 5 ] [ 1 >bignum { 1 5 7 } nth-unsafe ] unit-test
167 [ 5 ] [ 1 >bignum "\u000001\u000005\u000007" nth-unsafe ] unit-test
168
169 [ SBUF" before&after" ] [
170     "&" 6 11 SBUF" before and after" [ replace-slice ] keep
171 ] unit-test
172
173 [ 3 "a" ] [ { "a" "b" "c" "a" "d" } [ "a" = ] find-last ] unit-test
174
175 [ f f ] [ 100 { 1 2 3 } [ 1 = ] find-from ] unit-test
176 [ f f ] [ 100 { 1 2 3 } [ 1 = ] find-last-from ] unit-test
177 [ f f ] [ -1 { 1 2 3 } [ 1 = ] find-from ] unit-test
178
179 [ 0 ] [ { "a" "b" "c" } { "A" "B" "C" } mismatch ] unit-test
180
181 [ 1 ] [ { "a" "b" "c" } { "a" "B" "C" } mismatch ] unit-test
182
183 [ f ] [ { "a" "b" "c" } { "a" "b" "c" } mismatch ] unit-test
184
185 [ V{ } V{ } ] [ { "a" "b" } { "a" "b" } drop-prefix [ >vector ] bi@ ] unit-test
186
187 [ V{ "C" } V{ "c" } ] [ { "a" "b" "C" } { "a" "b" "c" } drop-prefix [ >vector ] bi@ ] unit-test
188
189 [ -1 1 "abc" <slice> ] must-fail
190
191 [ V{ "a" "b" } V{ } ] [ { "X" "a" "b" } { "X" } drop-prefix [ >vector ] bi@ ] unit-test
192
193 [ 1 4 9 16 16 V{ f 1 4 9 16 } ] [
194     V{ } clone "cache-test" set
195     1 "cache-test" get [ sq ] cache-nth
196     2 "cache-test" get [ sq ] cache-nth
197     3 "cache-test" get [ sq ] cache-nth
198     4 "cache-test" get [ sq ] cache-nth
199     4 "cache-test" get [ "wrong" ] cache-nth
200     "cache-test" get
201 ] unit-test
202
203 [ 1 ] [ 0.5 { 1 2 3 } nth ] unit-test
204
205 ! Pathological case
206 [ "ihbye" ] [ "hi" <reversed> "bye" append ] unit-test
207
208 [ t ] [ "hi" <reversed> SBUF" hi" <reversed> = ] unit-test
209
210 [ t ] [ "hi" <reversed> SBUF" hi" <reversed> = ] unit-test
211
212 [ t ] [ "hi" <reversed> SBUF" hi" <reversed> [ hashcode ] bi@ = ] unit-test
213
214 [ -10 "hi" "bye" copy ] must-fail
215 [ 10 "hi" "bye" copy ] must-fail
216
217 [ V{ 1 2 3 5 6 } ] [
218     3 V{ 1 2 3 4 5 6 } clone [ delete-nth ] keep
219 ] unit-test
220
221 ! erg's random tester found this one
222 [ SBUF" 12341234" ] [
223     9 <sbuf> dup "1234" swap push-all dup dup swap push-all
224 ] unit-test
225
226 [ f ] [ f V{ } like f V{ } like eq? ] unit-test
227
228 [ V{ f f f } ] [ 3 V{ } new-sequence ] unit-test
229 [ SBUF" \0\0\0" ] [ 3 SBUF" " new-sequence ] unit-test
230
231 [ 0 ] [ f length ] unit-test
232 [ f first ] must-fail
233 [ 3 ] [ 3 10 nth ] unit-test
234 [ 3 ] [ 3 10 nth-unsafe ] unit-test
235 [ -3 10 nth ] must-fail
236 [ 11 10 nth ] must-fail
237
238 [ -1./0. 0 delete-nth ] must-fail
239 [ "" ] [ "" [ CHAR: \s = ] trim ] unit-test
240 [ "" ] [ "" [ CHAR: \s = ] trim-left ] unit-test
241 [ "" ] [ "" [ CHAR: \s = ] trim-right ] unit-test
242 [ "" ] [ "  " [ CHAR: \s = ] trim-left ] unit-test
243 [ "" ] [ "  " [ CHAR: \s = ] trim-right ] unit-test
244 [ "asdf" ] [ " asdf " [ CHAR: \s = ] trim ] unit-test
245 [ "asdf " ] [ " asdf " [ CHAR: \s = ] trim-left ] unit-test
246 [ " asdf" ] [ " asdf " [ CHAR: \s = ] trim-right ] unit-test
247
248 [ 328350 ] [ 100 [ sq ] sigma ] unit-test
249
250 [ 50 ] [ 100 [ even? ] count ] unit-test
251 [ 50 ] [ 100 [ odd?  ] count ] unit-test
252
253 [ { "b" "d" } ] [ { "a" "b" "c" "d" } { 1 3 } nths ] unit-test
254 [ { "a" "b" "c" "d" } ] [ { "a" "b" "c" "d" } { 0 1 2 3 } nths ] unit-test
255 [ { "d" "c" "b" "a" } ] [ { "a" "b" "c" "d" } { 3 2 1 0 } nths ] unit-test
256 [ { "d" "a" "b" "c" } ] [ { "a" "b" "c" "d" } { 3 0 1 2 } nths ] unit-test
257
258 TUPLE: bogus-hashcode ;
259
260 M: bogus-hashcode hashcode* 2drop 0 >bignum ;
261
262 [ 0 ] [ { T{ bogus-hashcode } } hashcode ] unit-test
263
264 [ { 2 4 6 } { 1 3 5 7 } ] [ { 1 2 3 4 5 6 7 } [ even? ] partition ] unit-test
265
266 [ { 1 3 7 } ] [ 2 { 1 3 5 7 } remove-nth ] unit-test
267
268 [ { 1 3 "X" 5 7 } ] [ "X" 2 { 1 3 5 7 } insert-nth ] unit-test