-
Notifications
You must be signed in to change notification settings - Fork 211
/
extras-tests.factor
245 lines (190 loc) · 7.12 KB
/
extras-tests.factor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
! Copyright (C) 2013 Doug Coleman.
! See https://factorcode.org/license.txt for BSD license.
USING: alien.c-types arrays assocs combinators.extras io.files
kernel math modern.slices parser ranges sequences splitting
tools.test ;
IN: combinators.extras.tests
{ "a b" }
[ "a" "b" [ " " glue ] once ] unit-test
{ "a b c" }
[ "a" "b" "c" [ " " glue ] twice ] unit-test
{ "a b c d" }
[ "a" "b" "c" "d" [ " " glue ] thrice ] unit-test
{ { "negative" 0 "positive" } } [
{ -1 0 1 } [
{
{ [ 0 > ] [ "positive" ] }
{ [ 0 < ] [ "negative" ] }
[ ]
} cond-case
] map
] unit-test
<<
SYNTAX: ..= dup pop scan-object [a..b] suffix! ;
SYNTAX: ..< dup pop scan-object [a..b) suffix! ;
>>
<<
: describe-number ( n -- str )
{
{ 0 [ "no" ] }
{ 1 ..= 3 [ "a few" ] }
{ 4 ..= 9 [ "several" ] }
{ 12 [ "twelve" ] }
{ 10 ..= 99 [ "tens of" ] }
{ 100 ..= 999 [ "hundreds of" ] }
{ 1000 ..= 999,999 [ "thousands of" ] }
[ drop "millions and millions of" ]
} sequence-case ;
>>
{ "twelve" } [ 12 describe-number ] unit-test
{ "several" } [ 5 describe-number ] unit-test
{ "tens of" } [ 10 describe-number ] unit-test
{ "millions and millions of" } [ 1,000,000 describe-number ] unit-test
{ { 1 2 3 } } [ 1 { [ ] [ 1 + ] [ 2 + ] } cleave-array ] unit-test
{ 2 15 } [ 1 2 3 4 5 6 [ - - ] [ + + ] 3bi* ] unit-test
{ 2 5 } [ 1 2 3 4 5 6 [ - - ] 3bi@ ] unit-test
{ 3 1 } [ 1 2 [ + ] keepd ] unit-test
{ "1" "123" } [ "1" "123" [ length ] [ > ] swap-when ] unit-test
{ "123" "1" } [ "1" "123" [ length ] [ < ] swap-when ] unit-test
{ t } [ "resource:" [ file-exists? ] ?1arg >boolean ] unit-test
{ f } [ f [ file-exists? ] ?1arg ] unit-test
{ f } [ "/homeasdfasdf123123" [ file-exists? ] ?1arg ] unit-test
{ "hi " "there" } [
"hi there" {
{ [ "there" over subseq-start ] [ cut ] }
[ f ]
} cond*
] unit-test
{ "hi " "there" } [
"hi there" {
{ [ "foo" over subseq-start ] [ head f ] }
{ [ "there" over subseq-start ] [ cut ] }
[ f ]
} cond*
] unit-test
{ "hi there" f } [
"hi there" {
{ [ "foo" over subseq-start ] [ head f ] }
{ [ "bar" over subseq-start ] [ cut ] }
[ f ]
} cond*
] unit-test
{ "hi " "there" } [
"hi there" {
{ [ dup "there" subseq-index ] [ cut ] }
[ f ]
} cond*
] unit-test
{ "hi " "there" } [
"hi there" {
{ [ dup "foo" subseq-index ] [ head f ] }
{ [ dup "there" subseq-index ] [ cut ] }
[ f ]
} cond*
] unit-test
{ "hi there" f } [
"hi there" {
{ [ dup "foo" subseq-index ] [ head f ] }
{ [ dup "bar" subseq-index ] [ cut ] }
[ f ]
} cond*
] unit-test
{ f } [ f { } chain ] unit-test
{ 3 } [ H{ { 1 H{ { 2 3 } } } } { [ 1 of ] [ 2 of ] } chain ] unit-test
{ f } [ H{ { 1 H{ { 3 4 } } } } { [ 1 of ] [ 2 of ] } chain ] unit-test
{ f } [ H{ { 2 H{ { 3 4 } } } } { [ 1 of ] [ 2 of ] } chain ] unit-test
{ 5 } [
"hello factor!" { [ split-words ] [ first ] [ length ] } chain
] unit-test
{
{ 1 2 3 4 }
{ 1 2 3 4 }
{ 1 2 3 4 }
{ 1 2 3 4 }
} [
1 2 3 4
[ 4array ] [ 4array ] [ 4array ] [ 4array ] 4quad
] unit-test
{
{ 1 2 3 4 }
{ 5 6 7 8 }
{ 9 10 11 12 }
} [
1 2 3 4 5 6 7 8 9 10 11 12
[ 4array ] [ 4array ] [ 4array ] 4tri*
] unit-test
{
{ 1 2 3 4 }
{ 5 6 7 8 }
{ 9 10 11 12 }
} [
1 2 3 4 5 6 7 8 9 10 11 12
[ 4array ] 4tri@
] unit-test
{ 1 2 3 } [ 1 2 [ 3 ] dip-1up ] unit-test
{ 2 2 } [ 1 2 [ 1 + ] dip-1up ] unit-test
{ 20 11 } [ 10 20 [ 1 + ] dip-1up ] unit-test
{ 0 10 20 30 40 50 60 80 71 } [ 0 10 20 30 40 50 60 70 80 [ 1 + ] dip-1up ] unit-test
{ 0 10 20 30 40 50 70 80 61 } [ 0 10 20 30 40 50 60 70 80 [ 1 + ] 2dip-1up ] unit-test
{ 0 10 20 30 40 60 70 80 51 } [ 0 10 20 30 40 50 60 70 80 [ 1 + ] 3dip-1up ] unit-test
{ 0 10 20 30 40 50 80 61 71 } [ 0 10 20 30 40 50 60 70 80 [ [ 1 + ] bi@ ] dip-2up ] unit-test
{ 0 10 20 30 40 70 80 51 61 } [ 0 10 20 30 40 50 60 70 80 [ [ 1 + ] bi@ ] 2dip-2up ] unit-test
{ 0 10 20 30 60 70 80 41 51 } [ 0 10 20 30 40 50 60 70 80 [ [ 1 + ] bi@ ] 3dip-2up ] unit-test
{ 0 10 20 60 70 80 31 41 51 } [ 0 10 20 30 40 50 60 70 80 [ [ 1 + ] tri@ ] 3dip-3up ] unit-test
{ 4 "abcd" 97 98 99 100 } [
0 "abcd"
[ [ CHAR: a = ] accept1 ]
[ [ CHAR: b = ] accept1 ]
[ [ CHAR: c = ] accept1 ]
[ [ CHAR: d = ] accept1 ] 4craft-1up
] unit-test
{ 20 30 2500 } [ 20 30 [ + sq ] 2keep-1up ] unit-test
{ 10 1 } [ 10 [ drop 1 ] keep-1up ] unit-test
{ 10 20 1 } [ 10 20 [ 2drop 1 ] 2keep-1up ] unit-test
{ 10 20 30 1 } [ 10 20 30 [ 3drop 1 ] 3keep-1up ] unit-test
{ 10 1 } [ 10 [ drop 1 ] keep-1up ] unit-test
{ 10 20 1 } [ 10 20 [ 2drop 1 ] 2keep-1up ] unit-test
{ 10 20 30 1 } [ 10 20 30 [ 3drop 1 ] 3keep-1up ] unit-test
{ 10 1 2 } [ 10 [ drop 1 2 ] keep-2up ] unit-test
{ 10 20 1 2 } [ 10 20 [ 2drop 1 2 ] 2keep-2up ] unit-test
{ 10 20 30 1 2 } [ 10 20 30 [ 3drop 1 2 ] 3keep-2up ] unit-test
{ 10 1 2 3 } [ 10 [ drop 1 2 3 ] keep-3up ] unit-test
{ 10 20 1 2 3 } [ 10 20 [ 2drop 1 2 3 ] 2keep-3up ] unit-test
{ 10 20 30 1 2 3 } [ 10 20 30 [ 3drop 1 2 3 ] 3keep-3up ] unit-test
: test-keep-under ( -- a b c d e ) 1 [ [ 5 + ] call 10 20 30 ] keep-under ;
: test-2keep-under ( -- a b c d e f g ) 1 2 [ [ 5 + ] bi@ 10 20 30 ] 2keep-under ;
: test-3keep-under ( -- a b c d e f g h i ) 1 2 3 [ [ 5 + ] tri@ 10 20 30 ] 3keep-under ;
: test-4keep-under ( -- a b c d e f g h i j k l ) 1 2 3 4 [ [ 5 + ] quad@ 10 20 30 40 ] 4keep-under ;
{ 1 6 10 20 30 } [ test-keep-under ] unit-test
{ 1 2 6 7 10 20 30 } [ test-2keep-under ] unit-test
{ 1 2 3 6 7 8 10 20 30 } [ test-3keep-under ] unit-test
{ 1 2 3 4 6 7 8 9 10 20 30 40 } [ test-4keep-under ] unit-test
{ 1 2 3 4 1 2 3 4 5 } [ 1 2 3 4 [ 5 ] 4keep-under ] unit-test
{ 1 2 3 4 1 2 3 4 5 6 7 8 9 10 } [ 1 2 3 4 [ 5 6 7 8 9 10 ] 4keep-under ] unit-test
{ 3 { 1 2 3 } }
[ 0 { 1 2 3 } [ 1 + ] 1temp1d map ] unit-test
{ 3 { { 1 1 } { 2 2 } { 3 3 } } }
[ 0 { { 1 1 } { 2 2 } { 3 3 } } [ 1 + ] 1temp2d assoc-map ] unit-test
{ 103 203 { { 1 1 } { 2 2 } { 3 3 } } }
[ 100 200 { { 1 1 } { 2 2 } { 3 3 } } [ [ 1 + ] bi@ ] 2temp2d assoc-map ] unit-test
{ 111 112 113 114 } [ 10 100 [ 1 + + ] [ 2 + + ] [ 3 + + ] [ 4 + + ] 2quad ] unit-test
{ f } [ f ?[ 10 * ] ] unit-test
{ 20 } [ 2 ?[ 10 * ] ] unit-test
{ f } [ 1 [ even? ] [ ] [ ] ?1if ] unit-test
{ f } [ 1 [ even? ] [ ] ?1when ] unit-test
{ f } [ 1 [ even? ] [ ] ?1unless ] unit-test
{ 1 } [ 1 [ odd? ] [ ] [ ] ?1if ] unit-test
{ 1 } [ 1 [ odd? ] [ ] ?1when ] unit-test
{ 1 } [ 1 [ odd? ] [ ] ?1unless ] unit-test
{ f f } [ 1 2 [ + even? ] [ ] [ ] ?2if ] unit-test
{ f f } [ 1 2 [ + even? ] [ ] ?2when ] unit-test
{ f f } [ 1 2 [ + even? ] [ ] ?2unless ] unit-test
{ 1 2 } [ 1 2 [ + odd? ] [ ] [ ] ?2if ] unit-test
{ 1 2 } [ 1 2 [ + odd? ] [ ] ?2when ] unit-test
{ 1 2 } [ 1 2 [ + odd? ] [ ] ?2unless ] unit-test
{ 1 2 3 } [ 1 2 3 [ + + even? ] [ ] [ ] ?3if ] unit-test
{ 1 2 3 } [ 1 2 3 [ + + even? ] [ ] ?3when ] unit-test
{ 1 2 3 } [ 1 2 3 [ + + even? ] [ ] ?3unless ] unit-test
{ f f f } [ 1 2 3 [ + + odd? ] [ ] [ ] ?3if ] unit-test
{ f f f } [ 1 2 3 [ + + odd? ] [ ] ?3when ] unit-test
{ f f f } [ 1 2 3 [ + + odd? ] [ ] ?3unless ] unit-test