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