]> gitweb.factorcode.org Git - factor.git/blob - core/generic/generic-tests.factor
aae76184ff302b0b5b0246d7a96425b92528596e
[factor.git] / core / generic / generic-tests.factor
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 ;
6 IN: generic.tests
7
8 GENERIC: foobar ( x -- y )
9 M: object foobar drop "Hello world" ;
10 M: fixnum foobar drop "Goodbye cruel world" ;
11
12 GENERIC: class-of ( x -- y )
13
14 M: fixnum class-of drop "fixnum" ;
15 M: word   class-of drop "word"   ;
16
17 [ "fixnum" ] [ 5 class-of ] unit-test
18 [ "word" ] [ \ class-of class-of ] unit-test
19 [ 3.4 class-of ] must-fail
20
21 [ "Hello world" ] [ 4 foobar foobar ] unit-test
22 [ "Goodbye cruel world" ] [ 4 foobar ] unit-test
23
24 ! Testing unions
25 UNION: funnies quotation float complex ;
26
27 GENERIC: funny ( x -- y )
28 M: funnies funny drop 2 ;
29 M: object funny drop 0 ;
30
31 [ 2 ] [ [ { } ] funny ] unit-test
32 [ 0 ] [ { } funny ] unit-test
33
34 PREDICATE: very-funny < funnies number? ;
35
36 GENERIC: gooey ( x -- y )
37 M: very-funny gooey sq ;
38
39 [ 0.25 ] [ 0.5 gooey ] unit-test
40
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
45
46 M: for-arguments-sake empty-method-test drop "Hi" ;
47
48 TUPLE: another-one ;
49 C: <another-one> another-one
50
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
53
54 ! Weird bug
55 GENERIC: stack-underflow ( x y -- )
56 M: object stack-underflow 2drop ;
57 M: word stack-underflow 2drop ;
58
59 GENERIC: union-containment ( x -- y )
60 M: integer union-containment drop 1 ;
61 M: number union-containment drop 2 ;
62
63 [ 1 ] [ 1 union-containment ] unit-test
64 [ 2 ] [ 1.0 union-containment ] unit-test
65
66 ! Testing recovery from bad method definitions
67 "IN: generic.tests GENERIC: unhappy ( x -- x )" eval
68 [
69     "IN: generic.tests M: dictionary unhappy ;" eval
70 ] must-fail
71 [ ] [ "IN: generic.tests GENERIC: unhappy ( x -- x )" eval ] unit-test
72
73 GENERIC# complex-combination 1 ( a b -- c )
74 M: string complex-combination drop ;
75 M: object complex-combination nip ;
76
77 [ "hi" ] [ "hi" 3 complex-combination ] unit-test
78 [ "hi" ] [ 3 "hi" complex-combination ] unit-test
79
80 TUPLE: shit ;
81
82 M: shit complex-combination 2array ;
83 [ { T{ shit f } 5 } ] [ T{ shit f } 5 complex-combination ] unit-test
84
85 [ t ] [ \ complex-combination generic? >boolean ] unit-test
86
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" ;
93
94 [ T{ shit f } "shit" ] [ T{ shit f } big-generic-test ] unit-test
95
96 [ t ] [ \ + math-generic? ] unit-test
97
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
106
107 [ t ] [ { hashtable equal? } method-spec? ] unit-test
108 [ f ] [ { word = } method-spec? ] unit-test
109
110 ! Regression
111 TUPLE: first-one ;
112 TUPLE: second-one ;
113 UNION: both first-one union-class ;
114
115 GENERIC: wii ( x -- y )
116 M: both wii drop 3 ;
117 M: second-one wii drop 4 ;
118 M: tuple-class wii drop 5 ;
119 M: integer wii drop 6 ;
120
121 [ 3 ] [ T{ first-one } wii ] unit-test
122
123 GENERIC: tag-and-f ( x -- x x )
124
125 M: fixnum tag-and-f 1 ;
126
127 M: bignum tag-and-f 2 ;
128
129 M: float tag-and-f 3 ;
130
131 M: f tag-and-f 4 ;
132
133 [ f 4 ] [ f tag-and-f ] unit-test
134
135 [ 3.4 3 ] [ 3.4 tag-and-f ] unit-test
136
137 ! Issues with forget
138 GENERIC: generic-forget-test-1 ( a b -- c )
139
140 M: integer generic-forget-test-1 / ;
141
142 [ t ] [
143     \ / usage [ word? ] filter
144     [ name>> "integer=>generic-forget-test-1" = ] contains?
145 ] unit-test
146
147 [ ] [
148     [ \ generic-forget-test-1 forget ] with-compilation-unit
149 ] unit-test
150
151 [ f ] [
152     \ / usage [ word? ] filter
153     [ name>> "integer=>generic-forget-test-1" = ] contains?
154 ] unit-test
155
156 GENERIC: generic-forget-test-2 ( a b -- c )
157
158 M: sequence generic-forget-test-2 = ;
159
160 [ t ] [
161     \ = usage [ word? ] filter
162     [ name>> "sequence=>generic-forget-test-2" = ] contains?
163 ] unit-test
164
165 [ ] [
166     [ { sequence generic-forget-test-2 } forget ] with-compilation-unit
167 ] unit-test
168
169 [ f ] [
170     \ = usage [ word? ] filter
171     [ name>> "sequence=>generic-forget-test-2" = ] contains?
172 ] unit-test
173
174 GENERIC: generic-forget-test-3 ( a -- b )
175
176 M: f generic-forget-test-3 ;
177
178 [ ] [ \ f \ generic-forget-test-3 method "m" set ] unit-test
179
180 [ ] [ [ "m" get forget ] with-compilation-unit ] unit-test
181
182 [ ] [ "IN: generic.tests M: f generic-forget-test-3 ;" eval ] unit-test
183
184 [ ] [ [ "m" get forget ] with-compilation-unit ] unit-test
185
186 [ f ] [ f generic-forget-test-3 ] unit-test
187
188 : a-word ;
189
190 GENERIC: a-generic ( a -- b )
191
192 M: integer a-generic a-word ;
193
194 [ ] [ \ integer \ a-generic method "m" set ] unit-test
195
196 [ t ] [ "m" get \ a-word usage memq? ] unit-test
197
198 [ ] [ "IN: generic.tests : a-generic ;" eval ] unit-test
199
200 [ f ] [ "m" get \ a-word usage memq? ] unit-test
201
202 ! erg's regression
203 [ ] [
204     <"
205     IN: compiler.tests
206
207     GENERIC: jeah ( a -- b )
208     TUPLE: boii ;
209     M: boii jeah ;
210     GENERIC: jeah* ( a -- b )
211     M: boii jeah* jeah ;
212     "> eval
213
214     <"
215     IN: compiler.tests
216     FORGET: boii
217     "> eval
218     
219     <"
220     IN: compiler.tests
221     TUPLE: boii ;
222     M: boii jeah ;
223     "> eval
224 ] unit-test
225
226 ! call-next-method cache test
227 GENERIC: c-n-m-cache ( a -- b )
228
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 ;
233
234 [ 3 ] [ 2 c-n-m-cache ] unit-test
235
236 [ ] [ [ { integer c-n-m-cache } forget ] with-compilation-unit ] unit-test
237
238 [ 2 ] [ 2 c-n-m-cache ] unit-test