]> gitweb.factorcode.org Git - factor.git/blob - core/test/collections/sequences.factor
more sql changes
[factor.git] / core / test / collections / sequences.factor
1 IN: temporary
2 USING: arrays kernel math namespaces sequences
3 sequences-internals strings test vectors ;
4
5 [ V{ 1 2 3 4 } ] [ 1 5 dup <slice> >vector ] unit-test
6 [ 3 ] [ 1 4 dup <slice> length ] unit-test
7 [ 2 ] [ 1 3 { 1 2 3 4 } <slice> length ] unit-test
8 [ V{ 2 3 } ] [ 1 3 { 1 2 3 4 } <slice> >vector ] unit-test
9 [ V{ 4 5 } ] [ { 1 2 3 4 5 } 2 tail-slice* >vector ] unit-test
10 [ V{ 3 4 } ] [ 2 4 1 10 dup <slice> subseq >vector ] unit-test
11 [ V{ 3 4 } ] [ 0 2 2 4 1 10 dup <slice> <slice> subseq >vector ] unit-test
12 [ "cba" ] [ "abcdef" 3 head-slice reverse ] unit-test
13
14 [ 5040 ] [ [ 1 2 3 4 5 6 7 ] 1 [ * ] reduce ] unit-test
15
16 [ 5040 [ 1 1 2 6 24 120 720 ] ]
17 [ [ 1 2 3 4 5 6 7 ] 1 [ * ] accumulate ] unit-test
18
19 [ -1 f ] [ [ ] [ ] find ] unit-test
20 [ 0 1 ] [ [ 1 ] [ ] find ] unit-test
21 [ 1 "world" ] [ [ "hello" "world" ] [ "world" = ] find ] unit-test
22 [ 2 3 ] [ [ 1 2 3 ] [ 2 > ] find ] unit-test
23 [ -1 f ] [ [ 1 2 3 ] [ 10 > ] find ] unit-test
24
25 [ 1 CHAR: e ]
26 [ "aeiou" "hello world" [ swap member? ] find-with ] unit-test
27
28 [ 4 CHAR: o ]
29 [ "aeiou" 3 "hello world" [ swap member? ] find-with* ] unit-test
30
31 [ f         ] [ 3 [ ]     member? ] unit-test
32 [ f         ] [ 3 [ 1 2 ] member? ] unit-test
33 [ t ] [ 1 [ 1 2 ] member? ] unit-test
34 [ t ] [ 2 [ 1 2 ] member? ] unit-test
35
36 [ t ]
37 [ [ "hello" "world" ] [ second ] keep memq? ] unit-test
38
39 [ 4 ] [ CHAR: x "tuvwxyz" >vector index ] unit-test 
40
41 [ -1 ] [ CHAR: x 5 "tuvwxyz" >vector index* ] unit-test 
42
43 [ -1 ] [ CHAR: a 0 "tuvwxyz" >vector index* ] unit-test
44
45 [ f ] [ [ "Hello" { } 4/3 ] [ string? ] all? ] unit-test
46 [ t ] [ [ ] [ ] all? ] unit-test
47 [ t ] [ [ "hi" t 1/2 ] [ ] all? ] unit-test
48
49 [ [ 1 2 3 ] ] [ [ 1 4 2 5 3 6 ] [ 4 < ] subset ] unit-test
50 [ { 4 2 6 } ] [ { 1 4 2 5 3 6 } [ 2 mod 0 = ] subset ] unit-test
51
52 [ [ 3 ] ] [ 2 [ 1 2 3 ] [ < ] subset-with ] unit-test
53
54 [ "hello world how are you" ]
55 [ { "hello" "world" "how" "are" "you" } " " join ]
56 unit-test
57
58 [ "" ] [ { } "" join ] unit-test
59
60 [ { } ] [ { } flip ] unit-test
61
62 [ { "b" "e" } ] [ 1 { { "a" "b" "c" } { "d" "e" "f" } } flip nth ] unit-test
63
64 [ { { 1 4 } { 2 5 } { 3 6 } } ]
65 [ { { 1 2 3 } { 4 5 6 } } flip ] unit-test
66
67 [ f ] [ [ { } { } "Hello" ] all-equal? ] unit-test
68 [ f ] [ [ { 2 } { } { } ] all-equal? ] unit-test
69 [ t ] [ [ ] all-equal? ] unit-test
70 [ t ] [ [ 1/2 ] all-equal? ] unit-test
71 [ t ] [ [ 1.0 10/10 1 ] all-equal? ] unit-test
72 [ t ] [ { 1 2 3 4 } [ < ] monotonic? ] unit-test
73 [ f ] [ { 1 2 3 4 } [ > ] monotonic? ] unit-test
74 [ [ 2 3 4 ] ] [ 1 [ 1 2 3 ] [ + ] map-with ] unit-test
75
76 [ 1 ] [ 0 [ 1 2 ] nth ] unit-test
77 [ 2 ] [ 1 [ 1 2 ] nth ] unit-test
78
79 [ [ ]           ] [ [ ]   [ ]       append ] unit-test
80 [ [ 1 ]         ] [ [ 1 ] [ ]       append ] unit-test
81 [ [ 2 ]         ] [ [ ] [ 2 ]       append ] unit-test
82 [ [ 1 2 3 4 ]   ] [ [ 1 2 3 ] [ 4 ] append ] unit-test
83 [ [ 1 2 3 4 ]   ] [ [ 1 2 3 ] { 4 } append ] unit-test
84
85 [ "a" -1 append ] unit-test-fails
86 [ -1 "a" append ] unit-test-fails
87
88 [ [ ]       ] [ 1 [ ]           remove ] unit-test
89 [ [ ]       ] [ 1 [ 1 ]         remove ] unit-test
90 [ [ 3 1 1 ] ] [ 2 [ 3 2 1 2 1 ] remove ] unit-test
91
92 [ [ ]       ] [ [ ]       reverse ] unit-test
93 [ [ 1 ]     ] [ [ 1 ]     reverse ] unit-test
94 [ [ 3 2 1 ] ] [ [ 1 2 3 ] reverse ] unit-test
95
96 [ f ] [ f 0 head ] unit-test
97 [ [ ] ] [ [ 1 ] 0 head ] unit-test
98 [ [ 1 2 3 ] ] [ [ 1 2 3 4 ] 3 head ] unit-test
99 [ [ ] ] [ [ 1 2 3 ] 3 tail ] unit-test
100 [ [ 3 ] ] [ [ 1 2 3 ] 2 tail ] unit-test
101
102 [ t ] [ [ 1 2 3 ] [ 1 2 3 ] sequence= ] unit-test
103 [ t ] [ [ 1 2 3 ] { 1 2 3 } sequence= ] unit-test
104 [ t ] [ { 1 2 3 } [ 1 2 3 ] sequence= ] unit-test
105 [ f ] [ [ ] [ 1 2 3 ] sequence= ] unit-test
106
107 [ { 1 3 2 4 } ] [ { 1 2 3 4 } clone 1 2 pick exchange ] unit-test
108
109 [ 3 ] [ { 1 2 3 4 } midpoint ] unit-test
110
111 [ -1 ] [ 3 { } [ - ] binsearch ] unit-test
112 [ 0 ] [ 3 { 3 } [ - ] binsearch ] unit-test
113 [ 1 ] [ 2 { 1 2 3 } [ - ] binsearch ] unit-test
114 [ 3 ] [ 4 { 1 2 3 4 5 6 } [ - ] binsearch ] unit-test
115 [ 1 ] [ 3.5 { 1 2 3 4 5 6 7 8 } [ - ] binsearch ] unit-test
116 [ 3 ] [ 5.5 { 1 2 3 4 5 6 7 8 } [ - ] binsearch ] unit-test
117 [ 10 ] [ 10 20 >vector [ - ] binsearch ] unit-test
118
119 : seq-sorter 0 over length 1- <sorter> ;
120
121 [ { 4 2 3 1 } ]
122 [ { 1 2 3 4 } clone dup seq-sorter sorter-exchange ] unit-test
123
124 [ -1 ] [ [ - ] { 1 2 3 4 } seq-sorter 1 compare ] unit-test
125
126 [ 1 ] [ [ - ] { -5 4 -3 5 } seq-sorter 2dup sort-up sorter-start nip ] unit-test
127
128 [ 3 ] [ [ - ] { -5 4 -3 -6 5 } seq-sorter 2dup sort-down sorter-end nip ] unit-test
129
130 [ { 1 2 3 4 5 6 7 8 9 } ] [
131     [ - ] { 9 8 7 6 5 4 3 2 1 } clone seq-sorter 2dup sort-step
132     sorter-seq >array nip
133 ] unit-test
134
135 [ { 1 2 3 4 5 6 7 8 9 } ] [
136     [ - ] { 1 2 3 4 5 6 7 8 9 } clone seq-sorter 2dup sort-step
137     sorter-seq >array nip
138 ] unit-test
139
140 [ [ ] ] [ [ ] natural-sort ] unit-test
141
142 [ t ] [
143     100 [
144         drop
145         100 [ drop 20 random-int [ drop 1000 random-int ] map ] map natural-sort [ <=> 0 <= ] monotonic?
146     ] all?
147 ] unit-test
148
149 [ { "" "a" "aa" "aaa" } ]
150 [ 4 [ CHAR: a <string> ] map ]
151 unit-test
152
153 [ V{ } ] [ "f" V{ } clone [ delete ] keep ] unit-test
154 [ V{ } ] [ "f" V{ "f" } clone [ delete ] keep ] unit-test
155 [ V{ } ] [ "f" V{ "f" "f" } clone [ delete ] keep ] unit-test
156 [ V{ "x" } ] [ "f" V{ "f" "x" "f" } clone [ delete ] keep ] unit-test
157 [ V{ "y" "x" } ] [ "f" V{ "y" "f" "x" "f" } clone [ delete ] keep ] unit-test
158
159 [ { 1 4 9 } ] [ { 1 2 3 } clone dup [ sq ] inject ] unit-test
160
161 [ { "one" "two" "three" 4 5 6 } ]
162 [
163     { "one" "two" "three" }
164     { 1 2 3 } { 1 2 3 4 5 6 } clone [ subst ] keep
165 ] unit-test
166
167 [ ] [ { 1 2 } [ 2drop 1 ] sort drop ] unit-test
168
169 [ 5 ] [ 1 >bignum { 1 5 7 } nth-unsafe ] unit-test
170 [ 5 ] [ 1 >bignum { 1 5 7 } nth-unsafe ] unit-test
171 [ 5 ] [ 1 >bignum "\u0001\u0005\u0007" nth-unsafe ] unit-test
172
173 [ "before&after" ] [ "&" 6 11 "before and after" replace-slice ] unit-test
174
175 [ 3 "a" ] [ { "a" "b" "c" "a" "d" } [ "a" = ] find-last ] unit-test
176
177 [ -1 f ] [ -1 { 1 2 3 } [ 1 = ] find* ] 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 [ -1 ] [ { "a" "b" "c" } { "a" "b" "c" } mismatch ] unit-test
184
185 [ V{ } V{ } ] [ { "a" "b" } { "a" "b" } drop-prefix [ >vector ] 2apply ] unit-test
186
187 [ V{ "C" } V{ "c" } ] [ { "a" "b" "C" } { "a" "b" "c" } drop-prefix [ >vector ] 2apply ] unit-test
188
189 [ -1 1 "abc" <slice> ] unit-test-fails
190
191 [ V{ "a" "b" } V{ } ] [ { "X" "a" "b" } { "X" } drop-prefix [ >vector ] 2apply ] unit-test
192
193 [ -1 ] [ "ab" "abc" <=> ] unit-test
194 [ 1 ] [ "abc" "ab" <=> ] unit-test
195
196 [ 1 4 9 16 16 V{ f 1 4 9 16 } ] [
197     V{ } clone "cache-test" set
198     1 "cache-test" get [ sq ] cache-nth
199     2 "cache-test" get [ sq ] cache-nth
200     3 "cache-test" get [ sq ] cache-nth
201     4 "cache-test" get [ sq ] cache-nth
202     4 "cache-test" get [ "wrong" ] cache-nth
203     "cache-test" get
204 ] unit-test
205
206 [ 1 ] [ 1/2 { 1 2 3 } nth ] unit-test
207
208 ! Pathological case
209 [ "ihbye" ] [ "hi" <reversed> "bye" append ] unit-test
210
211 [ 10 "hi" "bye" copy-into ] unit-test-fails
212
213 [ { 1 2 3 5 6 } ] [ 3 { 1 2 3 4 5 6 } remove-nth ] unit-test
214
215 [ V{ 1 2 3 } ]
216 [ 3 V{ 1 2 } clone [ push-new ] keep ] unit-test
217
218 [ V{ 1 2 3 } ]
219 [ 3 V{ 1 3 2 } clone [ push-new ] keep ] unit-test
220
221 ! Columns
222 { { 1 2 3 } { 4 5 6 } { 7 8 9 } } [ clone ] map "seq" set
223
224 [ { 1 4 7 } ] [ "seq" get 0 <column> >array ] unit-test
225 [ ] [ "seq" get 1 <column> [ sq ] inject ] unit-test
226 [ { 4 25 64 } ] [ "seq" get 1 <column> >array ] unit-test
227
228 [ { 1 2 3 } 0 group ] unit-test-fails