]> gitweb.factorcode.org Git - factor.git/blob - library/test/collections/sequences.factor
Factor 0.79
[factor.git] / library / test / collections / sequences.factor
1 IN: temporary
2 USING: arrays kernel lists math namespaces sequences
3 sequences-internals strings test vectors ;
4
5 [ V{ 1 2 3 4 } ] [ 1 5 <range> >vector ] unit-test
6 [ 3 ] [ 1 4 <range> 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 } ] [ 2 { 1 2 3 4 5 } tail-slice* >vector ] unit-test
10 [ V{ 3 4 } ] [ 2 4 1 10 <range> subseq >vector ] unit-test
11 [ V{ 3 4 } ] [ 0 2 2 4 1 10 <range> <slice> subseq >vector ] unit-test
12 [ "cba" ] [ 3 "abcdef" head-slice reverse ] unit-test
13
14 [ 5040 ] [ [ 1 2 3 4 5 6 7 ] 1 [ * ] reduce ] unit-test
15
16 [ [ 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 [ [ ]       ] [ 1 [ ]           remove ] unit-test
86 [ [ ]       ] [ 1 [ 1 ]         remove ] unit-test
87 [ [ 3 1 1 ] ] [ 2 [ 3 2 1 2 1 ] remove ] unit-test
88
89 [ [ ]       ] [ [ ]       reverse ] unit-test
90 [ [ 1 ]     ] [ [ 1 ]     reverse ] unit-test
91 [ [ 3 2 1 ] ] [ [ 1 2 3 ] reverse ] unit-test
92
93 [ f ] [ 0 f head ] unit-test
94 [ f ] [ 0 [ 1 ] head ] unit-test
95 [ [ 1 2 3 ] ] [ 3 [ 1 2 3 4 ] head ] unit-test
96 [ f ] [ 3 [ 1 2 3 ] tail ] unit-test
97 [ [ 3 ] ] [ 2 [ 1 2 3 ] tail ] unit-test
98
99 [ t ] [ [ 1 2 3 ] [ 1 2 3 ] sequence= ] unit-test
100 [ t ] [ [ 1 2 3 ] { 1 2 3 } sequence= ] unit-test
101 [ t ] [ { 1 2 3 } [ 1 2 3 ] sequence= ] unit-test
102 [ f ] [ [ ] [ 1 2 3 ] sequence= ] unit-test
103
104 [ { 1 3 2 4 } ] [ { 1 2 3 4 } clone 1 2 pick exchange ] unit-test
105
106 [ 3 ] [ { 1 2 3 4 } midpoint ] unit-test
107
108 [ -1 ] [ 3 { } [ - ] binsearch ] unit-test
109 [ 0 ] [ 3 { 3 } [ - ] binsearch ] unit-test
110 [ 1 ] [ 2 { 1 2 3 } [ - ] binsearch ] unit-test
111 [ 3 ] [ 4 { 1 2 3 4 5 6 } [ - ] binsearch ] unit-test
112 [ 1 ] [ 3.5 { 1 2 3 4 5 6 7 8 } [ - ] binsearch ] unit-test
113 [ 3 ] [ 5.5 { 1 2 3 4 5 6 7 8 } [ - ] binsearch ] unit-test
114 [ 10 ] [ 10 20 >vector [ - ] binsearch ] unit-test
115
116 : seq-sorter 0 over length 1- <sorter> ;
117
118 [ { 4 2 3 1 } ]
119 [ { 1 2 3 4 } clone dup seq-sorter sorter-exchange ] unit-test
120
121 [ -1 ] [ [ - ] { 1 2 3 4 } seq-sorter 1 compare ] unit-test
122
123 [ 1 ] [ [ - ] { -5 4 -3 5 } seq-sorter sort-up sorter-start nip ] unit-test
124
125 [ 3 ] [ [ - ] { -5 4 -3 -6 5 } seq-sorter sort-down sorter-end nip ] unit-test
126
127 [ { 1 2 3 4 5 6 7 8 9 } ] [
128     [ - ] { 9 8 7 6 5 4 3 2 1 } clone seq-sorter sort-step
129     sorter-seq >array nip
130 ] unit-test
131
132 [ { 1 2 3 4 5 6 7 8 9 } ] [
133     [ - ] { 1 2 3 4 5 6 7 8 9 } clone seq-sorter sort-step
134     sorter-seq >array nip
135 ] unit-test
136
137 [ [ ] ] [ [ ] number-sort ] unit-test
138
139 [ t ] [
140     100 [
141         drop
142         1000 [ drop 1000 random-int ] map number-sort [ <= ] monotonic?
143     ] all?
144 ] unit-test
145
146 [ { "" "a" "aa" "aaa" } ]
147 [ 4 [ CHAR: a fill ] map ]
148 unit-test
149
150 [ V{ } ] [ "f" V{ } clone [ delete ] keep ] unit-test
151 [ V{ } ] [ "f" V{ "f" } clone [ delete ] keep ] unit-test
152 [ V{ } ] [ "f" V{ "f" "f" } clone [ delete ] keep ] unit-test
153 [ V{ "x" } ] [ "f" V{ "f" "x" "f" } clone [ delete ] keep ] unit-test
154 [ V{ "y" "x" } ] [ "f" V{ "y" "f" "x" "f" } clone [ delete ] keep ] unit-test
155
156 [ { 1 4 9 } ] [ { 1 2 3 } clone dup [ sq ] inject ] unit-test
157
158 [ { 3 4 5 } ] [ 2 { 1 2 3 } clone [ [ + ] inject-with ] keep ] unit-test
159
160 [ { "one" "two" "three" 4 5 6 } ]
161 [
162     { "one" "two" "three" }
163     { 1 2 3 } { 1 2 3 4 5 6 } clone [ subst ] keep
164 ] unit-test
165
166 [ ] [ { 1 2 } [ 2drop 1 ] sort drop ] unit-test
167
168 [ 5 ] [ 1 >bignum { 1 5 7 } nth-unsafe ] unit-test
169 [ 5 ] [ 1 >bignum { 1 5 7 } nth-unsafe ] unit-test
170 [ 5 ] [ 1 >bignum "\u0001\u0005\u0007" nth-unsafe ] unit-test
171
172 [ "before&after" ] [ "&" 6 11 "before and after" replace-slice ] unit-test
173
174 [ 3 "a" ] [ { "a" "b" "c" "a" "d" } [ "a" = ] find-last ] unit-test
175
176 [ -1 f ] [ -1 { 1 2 3 } [ 1 = ] find* ] unit-test
177
178 [ { 1 2 3 } ] [ { 1 1 2 2 3 3 } prune ] unit-test
179
180 [ 0 ] [ { "a" "b" "c" } { "A" "B" "C" } mismatch ] unit-test
181
182 [ 1 ] [ { "a" "b" "c" } { "a" "B" "C" } mismatch ] unit-test
183
184 [ -1 ] [ { "a" "b" "c" } { "a" "b" "c" } mismatch ] unit-test
185
186 [ V{ } V{ } ] [ { "a" "b" } { "a" "b" } drop-prefix [ >vector ] 2apply ] unit-test
187
188 [ V{ "C" } V{ "c" } ] [ { "a" "b" "C" } { "a" "b" "c" } drop-prefix [ >vector ] 2apply ] unit-test
189
190 [ -1 1 "abc" <slice> ] unit-test-fails
191
192 [ V{ "a" "b" } V{ } ] [ { "X" "a" "b" } { "X" } drop-prefix [ >vector ] 2apply ] unit-test
193
194 [ -1 ] [ "ab" "abc" lexi ] unit-test
195 [ 1 ] [ "abc" "ab" lexi ] unit-test
196
197 [ 1 4 9 16 16 V{ f 1 4 9 16 } ] [
198     V{ } clone "cache-test" set
199     1 "cache-test" get [ sq ] cache-nth
200     2 "cache-test" get [ sq ] cache-nth
201     3 "cache-test" get [ sq ] cache-nth
202     4 "cache-test" get [ sq ] cache-nth
203     4 "cache-test" get [ "wrong" ] cache-nth
204     "cache-test" get
205 ] unit-test
206
207 [ 1 ] [ 1/2 { 1 2 3 } nth ] unit-test