2 USING: arrays kernel math namespaces sequences
3 sequences-internals strings test vectors ;
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
14 [ 5040 ] [ [ 1 2 3 4 5 6 7 ] 1 [ * ] reduce ] unit-test
16 [ 5040 [ 1 1 2 6 24 120 720 ] ]
17 [ [ 1 2 3 4 5 6 7 ] 1 [ * ] accumulate ] unit-test
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
26 [ "aeiou" "hello world" [ swap member? ] find-with ] unit-test
29 [ "aeiou" 3 "hello world" [ swap member? ] find-with* ] unit-test
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
37 [ [ "hello" "world" ] [ second ] keep memq? ] unit-test
39 [ 4 ] [ CHAR: x "tuvwxyz" >vector index ] unit-test
41 [ -1 ] [ CHAR: x 5 "tuvwxyz" >vector index* ] unit-test
43 [ -1 ] [ CHAR: a 0 "tuvwxyz" >vector index* ] unit-test
45 [ f ] [ [ "Hello" { } 4/3 ] [ string? ] all? ] unit-test
46 [ t ] [ [ ] [ ] all? ] unit-test
47 [ t ] [ [ "hi" t 1/2 ] [ ] all? ] unit-test
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
52 [ [ 3 ] ] [ 2 [ 1 2 3 ] [ < ] subset-with ] unit-test
54 [ "hello world how are you" ]
55 [ { "hello" "world" "how" "are" "you" } " " join ]
58 [ "" ] [ { } "" join ] unit-test
60 [ { } ] [ { } flip ] unit-test
62 [ { "b" "e" } ] [ 1 { { "a" "b" "c" } { "d" "e" "f" } } flip nth ] unit-test
64 [ { { 1 4 } { 2 5 } { 3 6 } } ]
65 [ { { 1 2 3 } { 4 5 6 } } flip ] unit-test
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
76 [ 1 ] [ 0 [ 1 2 ] nth ] unit-test
77 [ 2 ] [ 1 [ 1 2 ] nth ] unit-test
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
85 [ "a" -1 append ] unit-test-fails
86 [ -1 "a" append ] unit-test-fails
88 [ [ ] ] [ 1 [ ] remove ] unit-test
89 [ [ ] ] [ 1 [ 1 ] remove ] unit-test
90 [ [ 3 1 1 ] ] [ 2 [ 3 2 1 2 1 ] remove ] unit-test
92 [ [ ] ] [ [ ] reverse ] unit-test
93 [ [ 1 ] ] [ [ 1 ] reverse ] unit-test
94 [ [ 3 2 1 ] ] [ [ 1 2 3 ] reverse ] unit-test
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
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
107 [ { 1 3 2 4 } ] [ { 1 2 3 4 } clone 1 2 pick exchange ] unit-test
109 [ 3 ] [ { 1 2 3 4 } midpoint ] unit-test
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
119 : seq-sorter 0 over length 1- <sorter> ;
122 [ { 1 2 3 4 } clone dup seq-sorter sorter-exchange ] unit-test
124 [ -1 ] [ [ - ] { 1 2 3 4 } seq-sorter 1 compare ] unit-test
126 [ 1 ] [ [ - ] { -5 4 -3 5 } seq-sorter 2dup sort-up sorter-start nip ] unit-test
128 [ 3 ] [ [ - ] { -5 4 -3 -6 5 } seq-sorter 2dup sort-down sorter-end nip ] unit-test
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
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
140 [ [ ] ] [ [ ] natural-sort ] unit-test
145 100 [ drop 20 random-int [ drop 1000 random-int ] map ] map natural-sort [ <=> 0 <= ] monotonic?
149 [ { "" "a" "aa" "aaa" } ]
150 [ 4 [ CHAR: a <string> ] map ]
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
159 [ { 1 4 9 } ] [ { 1 2 3 } clone dup [ sq ] inject ] unit-test
161 [ { "one" "two" "three" 4 5 6 } ]
163 { "one" "two" "three" }
164 { 1 2 3 } { 1 2 3 4 5 6 } clone [ subst ] keep
167 [ ] [ { 1 2 } [ 2drop 1 ] sort drop ] unit-test
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
173 [ "before&after" ] [ "&" 6 11 "before and after" replace-slice ] unit-test
175 [ 3 "a" ] [ { "a" "b" "c" "a" "d" } [ "a" = ] find-last ] unit-test
177 [ -1 f ] [ -1 { 1 2 3 } [ 1 = ] find* ] unit-test
179 [ 0 ] [ { "a" "b" "c" } { "A" "B" "C" } mismatch ] unit-test
181 [ 1 ] [ { "a" "b" "c" } { "a" "B" "C" } mismatch ] unit-test
183 [ -1 ] [ { "a" "b" "c" } { "a" "b" "c" } mismatch ] unit-test
185 [ V{ } V{ } ] [ { "a" "b" } { "a" "b" } drop-prefix [ >vector ] 2apply ] unit-test
187 [ V{ "C" } V{ "c" } ] [ { "a" "b" "C" } { "a" "b" "c" } drop-prefix [ >vector ] 2apply ] unit-test
189 [ -1 1 "abc" <slice> ] unit-test-fails
191 [ V{ "a" "b" } V{ } ] [ { "X" "a" "b" } { "X" } drop-prefix [ >vector ] 2apply ] unit-test
193 [ -1 ] [ "ab" "abc" <=> ] unit-test
194 [ 1 ] [ "abc" "ab" <=> ] unit-test
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
206 [ 1 ] [ 1/2 { 1 2 3 } nth ] unit-test
209 [ "ihbye" ] [ "hi" <reversed> "bye" append ] unit-test
211 [ 10 "hi" "bye" copy-into ] unit-test-fails
213 [ { 1 2 3 5 6 } ] [ 3 { 1 2 3 4 5 6 } remove-nth ] unit-test
216 [ 3 V{ 1 2 } clone [ push-new ] keep ] unit-test
219 [ 3 V{ 1 3 2 } clone [ push-new ] keep ] unit-test
222 { { 1 2 3 } { 4 5 6 } { 7 8 9 } } [ clone ] map "seq" set
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
228 [ { 1 2 3 } 0 group ] unit-test-fails