1 USING: accessors alien arrays definitions generic generic.standard
2 generic.math assocs hashtables io kernel math namespaces parser
3 prettyprint sequences strings tools.test vectors words
4 quotations classes classes.algebra classes.tuple continuations
5 layouts classes.union sorting compiler.units eval multiline ;
8 GENERIC: foobar ( x -- y )
9 M: object foobar drop "Hello world" ;
10 M: fixnum foobar drop "Goodbye cruel world" ;
12 GENERIC: class-of ( x -- y )
14 M: fixnum class-of drop "fixnum" ;
15 M: word class-of drop "word" ;
17 [ "fixnum" ] [ 5 class-of ] unit-test
18 [ "word" ] [ \ class-of class-of ] unit-test
19 [ 3.4 class-of ] must-fail
21 [ "Hello world" ] [ 4 foobar foobar ] unit-test
22 [ "Goodbye cruel world" ] [ 4 foobar ] unit-test
25 UNION: funnies quotation float complex ;
27 GENERIC: funny ( x -- y )
28 M: funnies funny drop 2 ;
29 M: object funny drop 0 ;
31 [ 2 ] [ [ { } ] funny ] unit-test
32 [ 0 ] [ { } funny ] unit-test
34 PREDICATE: very-funny < funnies number? ;
36 GENERIC: gooey ( x -- y )
37 M: very-funny gooey sq ;
39 [ 0.25 ] [ 0.5 gooey ] unit-test
41 GENERIC: empty-method-test ( x -- y )
42 M: object empty-method-test ;
43 TUPLE: for-arguments-sake ;
44 C: <for-arguments-sake> for-arguments-sake
46 M: for-arguments-sake empty-method-test drop "Hi" ;
49 C: <another-one> another-one
51 [ "Hi" ] [ <for-arguments-sake> empty-method-test empty-method-test ] unit-test
52 [ T{ another-one f } ] [ <another-one> empty-method-test ] unit-test
55 GENERIC: stack-underflow ( x y -- )
56 M: object stack-underflow 2drop ;
57 M: word stack-underflow 2drop ;
59 GENERIC: union-containment ( x -- y )
60 M: integer union-containment drop 1 ;
61 M: number union-containment drop 2 ;
63 [ 1 ] [ 1 union-containment ] unit-test
64 [ 2 ] [ 1.0 union-containment ] unit-test
66 ! Testing recovery from bad method definitions
67 "IN: generic.tests GENERIC: unhappy ( x -- x )" eval
69 "IN: generic.tests M: dictionary unhappy ;" eval
71 [ ] [ "IN: generic.tests GENERIC: unhappy ( x -- x )" eval ] unit-test
73 GENERIC# complex-combination 1 ( a b -- c )
74 M: string complex-combination drop ;
75 M: object complex-combination nip ;
77 [ "hi" ] [ "hi" 3 complex-combination ] unit-test
78 [ "hi" ] [ 3 "hi" complex-combination ] unit-test
82 M: shit complex-combination 2array ;
83 [ { T{ shit f } 5 } ] [ T{ shit f } 5 complex-combination ] unit-test
85 [ t ] [ \ complex-combination generic? >boolean ] unit-test
87 GENERIC: big-generic-test ( x -- x y )
88 M: fixnum big-generic-test "fixnum" ;
89 M: bignum big-generic-test "bignum" ;
90 M: ratio big-generic-test "ratio" ;
91 M: string big-generic-test "string" ;
92 M: shit big-generic-test "shit" ;
94 [ T{ shit f } "shit" ] [ T{ shit f } big-generic-test ] unit-test
96 [ t ] [ \ + math-generic? ] unit-test
98 ! Test math-combination
99 [ [ [ >float ] dip ] ] [ \ real \ float math-upgrade ] unit-test
100 [ [ >float ] ] [ \ float \ real math-upgrade ] unit-test
101 [ [ [ >bignum ] dip ] ] [ \ fixnum \ bignum math-upgrade ] unit-test
102 [ [ >float ] ] [ \ float \ integer math-upgrade ] unit-test
103 [ number ] [ \ number \ float math-class-max ] unit-test
104 [ float ] [ \ real \ float math-class-max ] unit-test
105 [ fixnum ] [ \ fixnum \ null math-class-max ] unit-test
107 [ t ] [ { hashtable equal? } method-spec? ] unit-test
108 [ f ] [ { word = } method-spec? ] unit-test
113 UNION: both first-one union-class ;
115 GENERIC: wii ( x -- y )
117 M: second-one wii drop 4 ;
118 M: tuple-class wii drop 5 ;
119 M: integer wii drop 6 ;
121 [ 3 ] [ T{ first-one } wii ] unit-test
123 GENERIC: tag-and-f ( x -- x x )
125 M: fixnum tag-and-f 1 ;
127 M: bignum tag-and-f 2 ;
129 M: float tag-and-f 3 ;
133 [ f 4 ] [ f tag-and-f ] unit-test
135 [ 3.4 3 ] [ 3.4 tag-and-f ] unit-test
138 GENERIC: generic-forget-test-1 ( a b -- c )
140 M: integer generic-forget-test-1 / ;
143 \ / usage [ word? ] filter
144 [ name>> "integer=>generic-forget-test-1" = ] contains?
148 [ \ generic-forget-test-1 forget ] with-compilation-unit
152 \ / usage [ word? ] filter
153 [ name>> "integer=>generic-forget-test-1" = ] contains?
156 GENERIC: generic-forget-test-2 ( a b -- c )
158 M: sequence generic-forget-test-2 = ;
161 \ = usage [ word? ] filter
162 [ name>> "sequence=>generic-forget-test-2" = ] contains?
166 [ { sequence generic-forget-test-2 } forget ] with-compilation-unit
170 \ = usage [ word? ] filter
171 [ name>> "sequence=>generic-forget-test-2" = ] contains?
174 GENERIC: generic-forget-test-3 ( a -- b )
176 M: f generic-forget-test-3 ;
178 [ ] [ \ f \ generic-forget-test-3 method "m" set ] unit-test
180 [ ] [ [ "m" get forget ] with-compilation-unit ] unit-test
182 [ ] [ "IN: generic.tests M: f generic-forget-test-3 ;" eval ] unit-test
184 [ ] [ [ "m" get forget ] with-compilation-unit ] unit-test
186 [ f ] [ f generic-forget-test-3 ] unit-test
190 GENERIC: a-generic ( a -- b )
192 M: integer a-generic a-word ;
194 [ ] [ \ integer \ a-generic method "m" set ] unit-test
196 [ t ] [ "m" get \ a-word usage memq? ] unit-test
198 [ ] [ "IN: generic.tests : a-generic ;" eval ] unit-test
200 [ f ] [ "m" get \ a-word usage memq? ] unit-test
207 GENERIC: jeah ( a -- b )
210 GENERIC: jeah* ( a -- b )
226 ! call-next-method cache test
227 GENERIC: c-n-m-cache ( a -- b )
229 ! Force it to be unoptimized
230 M: fixnum c-n-m-cache { } [ ] like call call-next-method ;
231 M: integer c-n-m-cache 1 + ;
232 M: number c-n-m-cache ;
234 [ 3 ] [ 2 c-n-m-cache ] unit-test
236 [ ] [ [ { integer c-n-m-cache } forget ] with-compilation-unit ] unit-test
238 [ 2 ] [ 2 c-n-m-cache ] unit-test