1 USING: accessors compiler.units effects eval kernel kernel.private layouts
2 literals math namespaces quotations tools.test typed words words.symbol
3 combinators.short-circuit compiler.tree.debugger prettyprint definitions
4 sequences classes.intersection strings classes.union ;
7 TYPED: f+ ( a: float b: float -- c: float )
11 [ 2 1+1/2 f+ ] unit-test
13 TYPED: fix+ ( a: fixnum b: fixnum -- c: fixnum )
16 ! XXX: As of .97, we don't require that the output is a fixnum.
17 ! most-positive-fixnum neg 1 - 1quotation
18 ! [ most-positive-fixnum 1 fix+ ] unit-test
20 ! XXX: Check that we throw an error. This used to underflow to the least-positive-fixnum.
21 [ most-positive-fixnum 1 fix+ ] [ ${ KERNEL-ERROR 7 } head? ] must-fail-with
23 TUPLE: tweedle-dee ; final
24 TUPLE: tweedle-dum ; final
26 TYPED: dee ( x: tweedle-dee -- y )
29 TYPED: dum ( x: tweedle-dum -- y )
32 [ \ tweedle-dum new dee ]
33 [ { [ input-mismatch-error? ] [ expected-type>> tweedle-dee = ] [ value>> tweedle-dum? ] } 1&& ] must-fail-with
35 [ \ tweedle-dee new dum ]
36 [ { [ input-mismatch-error? ] [ expected-type>> tweedle-dum = ] [ value>> tweedle-dee? ] } 1&& ] must-fail-with
38 TYPED: dumdum ( x -- y: tweedle-dum )
39 drop \ tweedle-dee new ;
42 [ { [ output-mismatch-error? ] [ expected-type>> tweedle-dum = ] [ value>> tweedle-dee? ] } 1&& ] must-fail-with
44 TYPED:: f+locals ( a: float b: float -- c: float )
47 { 3.5 } [ 2 1+1/2 f+locals ] unit-test
50 { x fixnum read-only }
51 { y fixnum read-only } ; final
54 { u unboxable read-only }
55 { xy fixnum read-only } ; final
57 TYPED: unboxy ( in: unboxable -- out: unboxable2 )
58 dup [ x>> ] [ y>> ] bi - unboxable2 boa ;
60 { ( in: fixnum in: fixnum -- out: fixnum out: fixnum out: fixnum ) }
61 [ \ unboxy "typed-word" word-prop stack-effect ] unit-test
63 { T{ unboxable2 { u T{ unboxable { x 12 } { y 3 } } } { xy 9 } } }
64 [ T{ unboxable { x 12 } { y 3 } } unboxy ] unit-test
73 { x fixnum read-only }
74 { y fixnum read-only }
75 { z float read-only } ; final
79 USING: accessors kernel math ;
81 T{ unboxable f 12 3 4.0 } unboxy xy>>
85 TYPED: no-inputs ( -- out: integer )
88 { 1 } [ no-inputs ] unit-test
91 { x read-only } ; final
93 TYPED: no-inputs-unboxable-output ( -- out: unboxable3 )
96 { T{ unboxable3 } } [ no-inputs-unboxable-output ] unit-test
98 { f } [ no-inputs-unboxable-output no-inputs-unboxable-output eq? ] unit-test
102 TYPED: no-outputs ( x: integer -- )
105 { 2 } [ 2 no-outputs buh get ] unit-test
107 TYPED: no-outputs-unboxable-input ( x: unboxable3 -- )
110 { T{ unboxable3 } } [ T{ unboxable3 } no-outputs-unboxable-input buh get ] unit-test
113 T{ unboxable3 } no-outputs-unboxable-input buh get
114 T{ unboxable3 } no-outputs-unboxable-input buh get
118 ! Reported by littledan
119 TUPLE: superclass { x read-only } ;
120 TUPLE: subclass < superclass { y read-only } ; final
122 TYPED: unbox-fail ( a: superclass -- ? ) subclass? ;
124 { t } [ subclass new unbox-fail ] unit-test
126 ! If a final class becomes non-final, typed words need to be recompiled
127 TYPED: recompile-fail ( a: subclass -- ? ) buh get eq? ;
129 { f } [ subclass new [ buh set ] [ recompile-fail ] bi ] unit-test
131 { } [ "IN: typed.tests TUPLE: subclass < superclass { y read-only } ;" eval( -- ) ] unit-test
133 { t } [ subclass new [ buh set ] [ recompile-fail ] bi ] unit-test
135 ! Make sure that foldable and flushable work on typed words
136 TYPED: add ( a: integer b: integer -- c: integer ) + ; foldable
138 { [ 3 ] } [ [ 1 2 add ] cleaned-up-tree nodes>quot ] unit-test
140 TYPED: flush-test ( s: symbol -- ? ) on t ; flushable
142 : flush-print-1 ( symbol -- ) flush-test drop ;
143 : flush-print-2 ( symbol -- ) flush-test . ;
149 a-symbol flush-print-1
156 a-symbol flush-print-2
161 ! Forgetting an unboxed final class should work
162 TUPLE: forget-class { x read-only } ; final
164 TYPED: forget-fail ( a: forget-class -- ) drop ;
166 { } [ [ \ forget-class forget ] with-compilation-unit ] unit-test
168 { } [ [ \ forget-fail forget ] with-compilation-unit ] unit-test
170 TYPED: typed-maybe ( x: maybe{ integer } -- ? ) >boolean ;
172 { f } [ f typed-maybe ] unit-test
173 { t } [ 30 typed-maybe ] unit-test
174 [ 30.0 typed-maybe ] [ input-mismatch-error? ] must-fail-with
176 TYPED: typed-union ( x: union{ integer string } -- ? ) >boolean ;
178 { t } [ 3 typed-union ] unit-test
179 { t } [ "asdf" typed-union ] unit-test
180 [ 3.3 typed-union ] [ input-mismatch-error? ] must-fail-with
182 TYPED: typed-intersection ( x: intersection{ integer bignum } -- ? ) >boolean ;
184 { t } [ 5555555555555555555555555555555555555555555555555555 typed-intersection ] unit-test
185 [ 0 typed-intersection ] [ input-mismatch-error? ] must-fail-with
188 "IN: test123 USE: typed TYPED: foo ( x -- y ) ;" eval( -- )
189 ] [ error>> no-types-specified? ] must-fail-with