1 USING: compiler.test compiler.units tools.test kernel kernel.private
2 sequences.private math.private math combinators strings alien
3 arrays memory vocabs parser eval quotations compiler.errors
4 definitions generic.single ;
5 IN: compiler.tests.simple
8 [ ] [ [ ] compile-call ] unit-test
11 [ 1 ] [ [ 1 ] compile-call ] unit-test
12 [ 31 ] [ [ 31 ] compile-call ] unit-test
13 [ 255 ] [ [ 255 ] compile-call ] unit-test
14 [ -1 ] [ [ -1 ] compile-call ] unit-test
15 [ 65536 ] [ [ 65536 ] compile-call ] unit-test
16 [ -65536 ] [ [ -65536 ] compile-call ] unit-test
17 [ "hey" ] [ [ "hey" ] compile-call ] unit-test
22 [ ] [ [ no-op ] compile-call ] unit-test
23 [ 3 ] [ [ no-op 3 ] compile-call ] unit-test
24 [ 3 ] [ [ 3 no-op ] compile-call ] unit-test
26 : bar ( -- value ) 4 ;
28 [ 4 ] [ [ bar no-op ] compile-call ] unit-test
29 [ 4 3 ] [ [ no-op bar 3 ] compile-call ] unit-test
30 [ 3 4 ] [ [ 3 no-op bar ] compile-call ] unit-test
32 [ ] [ no-op ] unit-test
36 [ 1 ] [ t [ [ 1 ] [ 2 ] if ] compile-call ] unit-test
37 [ 2 ] [ f [ [ 1 ] [ 2 ] if ] compile-call ] unit-test
38 [ 1 3 ] [ t [ [ 1 ] [ 2 ] if 3 ] compile-call ] unit-test
39 [ 2 3 ] [ f [ [ 1 ] [ 2 ] if 3 ] compile-call ] unit-test
41 [ "hi" ] [ 0 [ { [ "hi" ] [ "bye" ] } dispatch ] compile-call ] unit-test
42 [ "bye" ] [ 1 [ { [ "hi" ] [ "bye" ] } dispatch ] compile-call ] unit-test
44 [ "hi" 3 ] [ 0 [ { [ "hi" ] [ "bye" ] } dispatch 3 ] compile-call ] unit-test
45 [ "bye" 3 ] [ 1 [ { [ "hi" ] [ "bye" ] } dispatch 3 ] compile-call ] unit-test
47 [ 4 1 ] [ 0 [ { [ bar 1 ] [ 3 1 ] } dispatch ] compile-call ] unit-test
48 [ 3 1 ] [ 1 [ { [ bar 1 ] [ 3 1 ] } dispatch ] compile-call ] unit-test
49 [ 4 1 3 ] [ 0 [ { [ bar 1 ] [ 3 1 ] } dispatch 3 ] compile-call ] unit-test
50 [ 3 1 3 ] [ 1 [ { [ bar 1 ] [ 3 1 ] } dispatch 3 ] compile-call ] unit-test
52 [ 2 3 ] [ 1 [ { [ gc 1 ] [ gc 2 ] } dispatch 3 ] compile-call ] unit-test
56 : recursive-test ( ? -- ) [ f recursive-test ] when ; inline recursive
58 [ ] [ t [ recursive-test ] compile-call ] unit-test
60 [ ] [ t recursive-test ] unit-test
62 ! Make sure error reporting works
64 ! [ [ dup ] compile-call ] must-fail
65 ! [ [ drop ] compile-call ] must-fail
69 [ ] [ [ callstack ] compile-call drop ] unit-test
75 [ "b" ] [ 1 [ empty { [ "a" ] [ "b" ] } dispatch ] compile-call ] unit-test
77 : dummy-if-1 ( -- ) t [ ] [ ] if ;
79 [ ] [ dummy-if-1 ] unit-test
81 : dummy-if-2 ( -- ) f [ ] [ ] if ;
83 [ ] [ dummy-if-2 ] unit-test
85 : dummy-if-3 ( -- n ) t [ 1 ] [ 2 ] if ;
87 [ 1 ] [ dummy-if-3 ] unit-test
89 : dummy-if-4 ( -- n ) f [ 1 ] [ 2 ] if ;
91 [ 2 ] [ dummy-if-4 ] unit-test
93 : dummy-if-5 ( -- n ) 0 dup 1 fixnum<= [ drop 1 ] [ ] if ;
95 [ 1 ] [ dummy-if-5 ] unit-test
97 : dummy-if-6 ( n -- n )
101 1 fixnum- dup 1 fixnum- fixnum+
104 [ 17 ] [ 10 dummy-if-6 ] unit-test
106 : dead-code-rec ( -- obj )
113 [ 3.2 ] [ dead-code-rec ] unit-test
115 : one-rec ( ? -- obj ) [ f one-rec ] [ "hi" ] if ;
117 [ "hi" ] [ t one-rec ] unit-test
119 : after-if-test ( -- n )
122 [ 5 ] [ after-if-test ] unit-test
126 : countdown-a ( n -- ) dup 0 eq? [ drop ] [ 1 fixnum- countdown-b ] if ;
127 : countdown-b ( n -- ) dup 0 eq? [ drop ] [ 1 fixnum- countdown-a ] if ;
129 [ ] [ 10 countdown-b ] unit-test
131 : dummy-when-1 ( -- ) t [ ] when ;
133 [ ] [ dummy-when-1 ] unit-test
135 : dummy-when-2 ( -- ) f [ ] when ;
137 [ ] [ dummy-when-2 ] unit-test
139 : dummy-when-3 ( a -- b ) dup [ dup fixnum* ] when ;
141 [ 16 ] [ 4 dummy-when-3 ] unit-test
142 [ f ] [ f dummy-when-3 ] unit-test
144 : dummy-when-4 ( a b -- a b ) dup [ dup dup fixnum* fixnum* ] when swap ;
146 [ 64 f ] [ f 4 dummy-when-4 ] unit-test
147 [ f t ] [ t f dummy-when-4 ] unit-test
149 : dummy-when-5 ( a -- b ) f [ dup fixnum* ] when ;
151 [ f ] [ f dummy-when-5 ] unit-test
153 : dummy-unless-1 ( -- ) t [ ] unless ;
155 [ ] [ dummy-unless-1 ] unit-test
157 : dummy-unless-2 ( -- ) f [ ] unless ;
159 [ ] [ dummy-unless-2 ] unit-test
161 : dummy-unless-3 ( a -- b ) dup [ drop 3 ] unless ;
163 [ 3 ] [ f dummy-unless-3 ] unit-test
164 [ 4 ] [ 4 dummy-unless-3 ] unit-test
166 ! Test cond expansion
170 { [ dup 2 mod 0 = ] [ drop "even" ] }
171 { [ dup 2 mod 1 = ] [ drop "odd" ] }
179 { [ dup 2 mod 0 = ] [ drop "even" ] }
180 { [ dup 2 mod 1 = ] [ drop "odd" ] }
188 { [ dup string? ] [ drop "string" ] }
189 { [ dup float? ] [ drop "float" ] }
190 { [ dup alien? ] [ drop "alien" ] }
199 { [ dup fixnum? ] [ ] }
205 GENERIC: single-combination-test ( obj1 obj2 -- obj )
207 M: object single-combination-test drop ;
208 M: f single-combination-test nip ;
209 M: array single-combination-test drop ;
210 M: integer single-combination-test drop ;
212 [ 2 3 ] [ 2 3 t single-combination-test ] unit-test
213 [ 2 3 ] [ 2 3 4 single-combination-test ] unit-test
214 [ 2 f ] [ 2 3 f single-combination-test ] unit-test
216 DEFER: single-combination-test-2
218 : single-combination-test-4 ( obj -- obj )
219 dup [ single-combination-test-2 ] when ;
221 : single-combination-test-3 ( obj -- obj )
224 GENERIC: single-combination-test-2 ( obj -- obj )
225 M: object single-combination-test-2 single-combination-test-3 ;
226 M: f single-combination-test-2 single-combination-test-4 ;
228 [ 3 ] [ t single-combination-test-2 ] unit-test
229 [ 3 ] [ 3 single-combination-test-2 ] unit-test
230 [ f ] [ f single-combination-test-2 ] unit-test
233 [ 100 ] [ [ 100 [ [ ] times ] keep ] compile-call ] unit-test
237 [ "compiler.tests.foo" forget-vocab ] with-compilation-unit
239 "USING: prettyprint words accessors ;
240 IN: compiler.tests.foo
241 : (recursive) ( -- ) (recursive) (recursive) ; inline recursive
242 : recursive ( -- ) (recursive) ;
243 \\ (recursive) word-optimized?" eval( -- obj )
247 ! This should not compile
248 GENERIC: bad-effect-test ( a -- )
249 M: quotation bad-effect-test call ; inline
250 : bad-effect-test* ( -- ) [ 1 2 3 ] bad-effect-test ;
252 [ bad-effect-test* ] [ not-compiled? ] must-fail-with
254 ! Don't want compiler error to stick around
255 [ ] [ [ M\ quotation bad-effect-test forget ] with-compilation-unit ] unit-test
257 ! Make sure time bombs literalize
258 [ [ \ + call ] compile-call ] [ no-method? ] must-fail-with