1 USING: alien strings kernel math tools.test io prettyprint
2 namespaces combinators words classes sequences accessors
7 : cond-test-1 ( obj -- str )
9 { [ dup 2 mod 0 = ] [ drop "even" ] }
10 { [ dup 2 mod 1 = ] [ drop "odd" ] }
13 \ cond-test-1 must-infer
15 [ "even" ] [ 2 cond-test-1 ] unit-test
16 [ "odd" ] [ 3 cond-test-1 ] unit-test
18 : cond-test-2 ( obj -- str )
20 { [ dup t = ] [ drop "true" ] }
21 { [ dup f = ] [ drop "false" ] }
22 [ drop "something else" ]
25 \ cond-test-2 must-infer
27 [ "true" ] [ t cond-test-2 ] unit-test
28 [ "false" ] [ f cond-test-2 ] unit-test
29 [ "something else" ] [ "ohio" cond-test-2 ] unit-test
31 : cond-test-3 ( obj -- str )
33 [ drop "something else" ]
34 { [ dup t = ] [ drop "true" ] }
35 { [ dup f = ] [ drop "false" ] }
38 \ cond-test-3 must-infer
40 [ "something else" ] [ t cond-test-3 ] unit-test
41 [ "something else" ] [ f cond-test-3 ] unit-test
42 [ "something else" ] [ "ohio" cond-test-3 ] unit-test
48 \ cond-test-4 must-infer
50 [ cond-test-4 ] [ class \ no-cond = ] must-fail-with
55 { [ dup 2 mod 0 = ] [ drop "even" ] }
56 { [ dup 2 mod 1 = ] [ drop "odd" ] }
62 { [ dup 2 mod 0 = ] [ drop "even" ] }
63 { [ dup 2 mod 1 = ] [ drop "odd" ] }
69 { [ dup string? ] [ drop "string" ] }
70 { [ dup float? ] [ drop "float" ] }
71 { [ dup alien? ] [ drop "alien" ] }
78 { [ dup string? ] [ drop "string" ] }
79 { [ dup float? ] [ drop "float" ] }
80 { [ dup alien? ] [ drop "alien" ] }
87 { [ dup string? ] [ drop "string" ] }
88 { [ dup float? ] [ drop "float" ] }
89 { [ dup alien? ] [ drop "alien" ] }
96 { [ dup 2 mod 1 = ] [ drop "odd" ] }
98 { [ dup 2 mod 0 = ] [ drop "even" ] }
104 [ drop "really early" ]
105 { [ dup 2 mod 1 = ] [ drop "odd" ] }
106 { [ dup 2 mod 0 = ] [ drop "even" ] }
110 [ { } cond ] [ class \ no-cond = ] must-fail-with
114 { [ dup 2 mod 1 = ] [ drop "odd" ] }
116 { [ dup 2 mod 0 = ] [ drop "even" ] }
122 [ drop "really early" ]
123 { [ dup 2 mod 1 = ] [ drop "odd" ] }
124 { [ dup 2 mod 0 = ] [ drop "even" ] }
128 [ { } cond ] [ class \ no-cond = ] must-fail-with
131 : case-test-1 ( obj -- obj' )
139 \ case-test-1 must-infer
141 [ "two" ] [ 2 case-test-1 ] unit-test
144 [ "two" ] [ 2 \ case-test-1 def>> call ] unit-test
146 [ "x" case-test-1 ] must-fail
148 : case-test-2 ( obj -- obj' )
157 \ case-test-2 must-infer
159 [ 25 ] [ 5 case-test-2 ] unit-test
162 [ 25 ] [ 5 \ case-test-2 def>> call ] unit-test
164 : case-test-3 ( obj -- obj' )
170 { H{ } [ "a hashtable" ] }
171 { { 1 2 3 } [ "an array" ] }
175 \ case-test-3 must-infer
177 [ "an array" ] [ { 1 2 3 } case-test-3 ] unit-test
180 : case-const-2 2 ; inline
183 : case-test-4 ( obj -- str )
185 { case-const-1 [ "uno" ] }
186 { case-const-2 [ "dos" ] }
193 \ case-test-4 must-infer
195 [ "uno" ] [ 1 case-test-4 ] unit-test
196 [ "dos" ] [ 2 case-test-4 ] unit-test
197 [ "tres" ] [ 3 case-test-4 ] unit-test
198 [ "demasiado" ] [ 100 case-test-4 ] unit-test
200 : case-test-5 ( obj -- )
202 { case-const-1 [ "uno" print ] }
203 { case-const-2 [ "dos" print ] }
204 { 3 [ "tres" print ] }
205 { 4 [ "cuatro" print ] }
206 { 5 [ "cinco" print ] }
207 [ drop "demasiado" print ]
210 \ case-test-5 must-infer
212 [ ] [ 1 case-test-5 ] unit-test
217 { case-const-1 [ "uno" ] }
218 { case-const-2 [ "dos" ] }
228 { case-const-1 [ "uno" ] }
229 { case-const-2 [ "dos" ] }
239 { case-const-1 [ "uno" ] }
240 { case-const-2 [ "dos" ] }
250 { case-const-1 [ "uno" ] }
251 { case-const-2 [ "dos" ] }
259 : do-not-call "do not call" throw ;
261 : test-case-6 ( obj -- value )
263 { \ do-not-call [ "do-not-call" ] }
267 \ test-case-6 must-infer
269 [ "three" ] [ 3 test-case-6 ] unit-test
270 [ "do-not-call" ] [ \ do-not-call test-case-6 ] unit-test
274 { \ do-not-call [ "do-not-call" ] }
280 [ do-not-call ] first {
281 { \ do-not-call [ "do-not-call" ] }
288 { \ do-not-call [ "do-not-call" ] }
294 [ "a hashtable" ] [ H{ } \ case-test-3 def>> call ] unit-test
296 [ t ] [ { 1 3 2 } contiguous-range? ] unit-test
297 [ f ] [ { 1 2 2 4 } contiguous-range? ] unit-test
298 [ f ] [ { + 3 2 } contiguous-range? ] unit-test
299 [ f ] [ { 1 0 7 } contiguous-range? ] unit-test
300 [ f ] [ { 1 1 3 7 } contiguous-range? ] unit-test
301 [ t ] [ { 7 6 4 8 5 } contiguous-range? ] unit-test
303 : test-case-7 ( obj -- str )
314 \ test-case-7 must-infer
316 [ "plus" ] [ \ + test-case-7 ] unit-test