1 USING: accessors alien arrays assocs classes classes.algebra
2 classes.tuple classes.union compiler.units continuations
3 definitions eval generic generic.math generic.standard
4 hashtables io io.streams.string kernel layouts math math.order
5 namespaces parser prettyprint quotations sequences sorting
6 strings tools.test vectors words generic.single
10 GENERIC: foobar ( x -- y )
11 M: object foobar drop "Hello world" ;
12 M: fixnum foobar drop "Goodbye cruel world" ;
14 GENERIC: class-of ( x -- y )
16 M: fixnum class-of drop "fixnum" ;
17 M: word class-of drop "word" ;
19 [ "fixnum" ] [ 5 class-of ] unit-test
20 [ "word" ] [ \ class-of class-of ] unit-test
21 [ 3.4 class-of ] must-fail
23 [ "Hello world" ] [ 4 foobar foobar ] unit-test
24 [ "Goodbye cruel world" ] [ 4 foobar ] unit-test
27 UNION: funnies quotation float complex ;
29 GENERIC: funny ( x -- y )
30 M: funnies funny drop 2 ;
31 M: object funny drop 0 ;
33 [ 2 ] [ [ { } ] funny ] unit-test
34 [ 0 ] [ { } funny ] unit-test
36 PREDICATE: very-funny < funnies number? ;
38 GENERIC: gooey ( x -- y )
39 M: very-funny gooey sq ;
41 [ 0.25 ] [ 0.5 gooey ] unit-test
43 GENERIC: empty-method-test ( x -- y )
44 M: object empty-method-test ;
45 TUPLE: for-arguments-sake ;
46 C: <for-arguments-sake> for-arguments-sake
48 M: for-arguments-sake empty-method-test drop "Hi" ;
51 C: <another-one> another-one
53 [ "Hi" ] [ <for-arguments-sake> empty-method-test empty-method-test ] unit-test
54 [ T{ another-one f } ] [ <another-one> empty-method-test ] unit-test
57 GENERIC: stack-underflow ( x y -- )
58 M: object stack-underflow 2drop ;
59 M: word stack-underflow 2drop ;
61 GENERIC: union-containment ( x -- y )
62 M: integer union-containment drop 1 ;
63 M: number union-containment drop 2 ;
65 [ 1 ] [ 1 union-containment ] unit-test
66 [ 2 ] [ 1.0 union-containment ] unit-test
68 ! Testing recovery from bad method definitions
69 "IN: generic.tests GENERIC: unhappy ( x -- x )" eval( -- )
71 "IN: generic.tests M: dictionary unhappy ;" eval( -- )
73 [ ] [ "IN: generic.tests GENERIC: unhappy ( x -- x )" eval( -- ) ] unit-test
75 GENERIC# complex-combination 1 ( a b -- c )
76 M: string complex-combination drop ;
77 M: object complex-combination nip ;
79 [ "hi" ] [ "hi" 3 complex-combination ] unit-test
80 [ "hi" ] [ 3 "hi" complex-combination ] unit-test
84 M: shit complex-combination 2array ;
85 [ { T{ shit f } 5 } ] [ T{ shit f } 5 complex-combination ] unit-test
87 [ t ] [ \ complex-combination generic? >boolean ] unit-test
89 GENERIC: big-generic-test ( x -- x y )
90 M: fixnum big-generic-test "fixnum" ;
91 M: bignum big-generic-test "bignum" ;
92 M: ratio big-generic-test "ratio" ;
93 M: string big-generic-test "string" ;
94 M: shit big-generic-test "shit" ;
96 [ T{ shit f } "shit" ] [ T{ shit f } big-generic-test ] unit-test
98 [ t ] [ \ + math-generic? ] unit-test
103 UNION: both first-one union-class ;
105 GENERIC: wii ( x -- y )
107 M: second-one wii drop 4 ;
108 M: tuple-class wii drop 5 ;
109 M: integer wii drop 6 ;
111 [ 3 ] [ T{ first-one } wii ] unit-test
113 GENERIC: tag-and-f ( x -- x x )
115 M: fixnum tag-and-f 1 ;
117 M: bignum tag-and-f 2 ;
119 M: float tag-and-f 3 ;
123 [ f 4 ] [ f tag-and-f ] unit-test
125 [ 3.4 3 ] [ 3.4 tag-and-f ] unit-test
128 GENERIC: generic-forget-test ( a -- b )
130 M: f generic-forget-test ;
132 [ ] [ \ f \ generic-forget-test method "m" set ] unit-test
134 [ ] [ [ "m" get forget ] with-compilation-unit ] unit-test
136 [ ] [ "IN: generic.tests M: f generic-forget-test ;" eval( -- ) ] unit-test
138 [ ] [ [ "m" get forget ] with-compilation-unit ] unit-test
140 [ f ] [ f generic-forget-test ] unit-test
144 """IN: compiler.tests
146 GENERIC: jeah ( a -- b )
149 GENERIC: jeah* ( a -- b )
150 M: boii jeah* jeah ;""" eval( -- )
152 """IN: compiler.tests
153 FORGET: boii""" eval( -- )
155 """IN: compiler.tests
157 M: boii jeah ;""" eval( -- )
160 ! call-next-method cache test
161 GENERIC: c-n-m-cache ( a -- b )
163 ! Force it to be unoptimized
164 M: fixnum c-n-m-cache { } [ ] like call( -- ) call-next-method ;
165 M: integer c-n-m-cache 1 + ;
166 M: number c-n-m-cache ;
168 [ 3 ] [ 2 c-n-m-cache ] unit-test
170 [ ] [ [ M\ integer c-n-m-cache forget ] with-compilation-unit ] unit-test
172 [ 2 ] [ 2 c-n-m-cache ] unit-test
174 ! Moving a method from one vocab to another doesn't always work
175 GENERIC: move-method-generic ( a -- b )
177 [ ] [ "IN: generic.tests.a USE: strings USE: generic.tests M: string move-method-generic ;" <string-reader> "move-method-test-1" parse-stream drop ] unit-test
179 [ ] [ "IN: generic.tests.b USE: strings USE: generic.tests M: string move-method-generic ;" <string-reader> "move-method-test-2" parse-stream drop ] unit-test
181 [ ] [ "IN: generic.tests.a" <string-reader> "move-method-test-1" parse-stream drop ] unit-test
183 [ { string } ] [ \ move-method-generic order ] unit-test
185 GENERIC: foozul ( a -- b )
191 reversed \ foozul method-for-class
192 reversed \ foozul method
197 fixnum \ <=> method-for-class
202 ! FORGET: on method wrappers
203 GENERIC: forget-test ( a -- b )
205 M: integer forget-test 3 + ;
207 [ ] [ "IN: generic.tests USE: math FORGET: M\\ integer forget-test" eval( -- ) ] unit-test
210 \ + compiled-usage keys
211 [ method-body? ] filter
212 [ "method-generic" word-prop \ forget-test eq? ] filter
215 [ 10 forget-test ] [ no-method? ] must-fail-with