1 USING: accessors effects eval kernel layouts math namespaces
2 quotations tools.test typed words words.symbol combinators.short-circuit
3 compiler.tree.debugger prettyprint definitions compiler.units sequences ;
6 TYPED: f+ ( a: float b: float -- c: float )
10 [ 2 1+1/2 f+ ] unit-test
12 TYPED: fix+ ( a: fixnum b: fixnum -- c: fixnum )
15 most-positive-fixnum neg 1 - 1quotation
16 [ most-positive-fixnum 1 fix+ ] unit-test
18 TUPLE: tweedle-dee ; final
19 TUPLE: tweedle-dum ; final
21 TYPED: dee ( x: tweedle-dee -- y )
24 TYPED: dum ( x: tweedle-dum -- y )
27 [ \ tweedle-dum new dee ]
28 [ { [ input-mismatch-error? ] [ expected-type>> tweedle-dee = ] [ value>> tweedle-dum? ] } 1&& ] must-fail-with
30 [ \ tweedle-dee new dum ]
31 [ { [ input-mismatch-error? ] [ expected-type>> tweedle-dum = ] [ value>> tweedle-dee? ] } 1&& ] must-fail-with
33 TYPED: dumdum ( x -- y: tweedle-dum )
34 drop \ tweedle-dee new ;
37 [ { [ output-mismatch-error? ] [ expected-type>> tweedle-dum = ] [ value>> tweedle-dee? ] } 1&& ] must-fail-with
39 TYPED:: f+locals ( a: float b: float -- c: float )
42 [ 3.5 ] [ 2 1+1/2 f+locals ] unit-test
45 { x fixnum read-only }
46 { y fixnum read-only } ; final
49 { u unboxable read-only }
50 { xy fixnum read-only } ; final
52 TYPED: unboxy ( in: unboxable -- out: unboxable2 )
53 dup [ x>> ] [ y>> ] bi - unboxable2 boa ;
55 [ (( in: fixnum in: fixnum -- out: fixnum out: fixnum out: fixnum )) ]
56 [ \ unboxy "typed-word" word-prop stack-effect ] unit-test
58 [ T{ unboxable2 { u T{ unboxable { x 12 } { y 3 } } } { xy 9 } } ]
59 [ T{ unboxable { x 12 } { y 3 } } unboxy ] unit-test
68 { x fixnum read-only }
69 { y fixnum read-only }
70 { z float read-only } ; final
74 USING: accessors kernel math ;
76 T{ unboxable f 12 3 4.0 } unboxy xy>>
80 TYPED: no-inputs ( -- out: integer )
83 [ 1 ] [ no-inputs ] unit-test
86 { x read-only } ; final
88 TYPED: no-inputs-unboxable-output ( -- out: unboxable3 )
91 [ T{ unboxable3 } ] [ no-inputs-unboxable-output ] unit-test
93 [ f ] [ no-inputs-unboxable-output no-inputs-unboxable-output eq? ] unit-test
97 TYPED: no-outputs ( x: integer -- )
100 [ 2 ] [ 2 no-outputs buh get ] unit-test
102 TYPED: no-outputs-unboxable-input ( x: unboxable3 -- )
105 [ T{ unboxable3 } ] [ T{ unboxable3 } no-outputs-unboxable-input buh get ] unit-test
108 T{ unboxable3 } no-outputs-unboxable-input buh get
109 T{ unboxable3 } no-outputs-unboxable-input buh get
113 ! Reported by littledan
114 TUPLE: superclass { x read-only } ;
115 TUPLE: subclass < superclass { y read-only } ; final
117 TYPED: unbox-fail ( a: superclass -- ? ) subclass? ;
119 [ t ] [ subclass new unbox-fail ] unit-test
121 ! If a final class becomes non-final, typed words need to be recompiled
122 TYPED: recompile-fail ( a: subclass -- ? ) buh get eq? ;
124 [ f ] [ subclass new [ buh set ] [ recompile-fail ] bi ] unit-test
126 [ ] [ "IN: typed.tests TUPLE: subclass < superclass { y read-only } ;" eval( -- ) ] unit-test
128 [ t ] [ subclass new [ buh set ] [ recompile-fail ] bi ] unit-test
130 ! Make sure that foldable and flushable work on typed words
131 TYPED: add ( a: integer b: integer -- c: integer ) + ; foldable
133 [ [ 3 ] ] [ [ 1 2 add ] cleaned-up-tree nodes>quot ] unit-test
135 TYPED: flush-test ( s: symbol -- ? ) on t ; flushable
137 : flush-print-1 ( symbol -- ) flush-test drop ;
138 : flush-print-2 ( symbol -- ) flush-test . ;
144 a-symbol flush-print-1
151 a-symbol flush-print-2
156 ! Forgetting an unboxed final class should work
157 TUPLE: forget-class { x read-only } ; final
159 TYPED: forget-fail ( a: forget-class -- ) drop ;
161 [ ] [ [ \ forget-class forget ] with-compilation-unit ] unit-test
163 [ ] [ [ \ forget-fail forget ] with-compilation-unit ] unit-test