]> gitweb.factorcode.org Git - factor.git/blob - core/generic/generic-tests.factor
cpu.ppc: fix load errors
[factor.git] / core / generic / generic-tests.factor
1 USING: accessors alien arrays definitions generic
2 generic.standard generic.math assocs hashtables io kernel math
3 math.order namespaces parser prettyprint sequences strings
4 tools.test vectors words quotations classes classes.algebra
5 classes.tuple continuations layouts classes.union sorting
6 compiler.units eval multiline 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 ! Regression
100 TUPLE: first-one ;
101 TUPLE: second-one ;
102 UNION: both first-one union-class ;
103
104 GENERIC: wii ( x -- y )
105 M: both wii drop 3 ;
106 M: second-one wii drop 4 ;
107 M: tuple-class wii drop 5 ;
108 M: integer wii drop 6 ;
109
110 [ 3 ] [ T{ first-one } wii ] unit-test
111
112 GENERIC: tag-and-f ( x -- x x )
113
114 M: fixnum tag-and-f 1 ;
115
116 M: bignum tag-and-f 2 ;
117
118 M: float tag-and-f 3 ;
119
120 M: f tag-and-f 4 ;
121
122 [ f 4 ] [ f tag-and-f ] unit-test
123
124 [ 3.4 3 ] [ 3.4 tag-and-f ] unit-test
125
126 ! Issues with forget
127 GENERIC: generic-forget-test ( a -- b )
128
129 M: f generic-forget-test ;
130
131 [ ] [ \ f \ generic-forget-test method "m" set ] unit-test
132
133 [ ] [ [ "m" get forget ] with-compilation-unit ] unit-test
134
135 [ ] [ "IN: generic.tests M: f generic-forget-test ;" eval( -- ) ] unit-test
136
137 [ ] [ [ "m" get forget ] with-compilation-unit ] unit-test
138
139 [ f ] [ f generic-forget-test ] unit-test
140
141 ! erg's regression
142 [ ] [
143     <"
144     IN: compiler.tests
145
146     GENERIC: jeah ( a -- b )
147     TUPLE: boii ;
148     M: boii jeah ;
149     GENERIC: jeah* ( a -- b )
150     M: boii jeah* jeah ;
151     "> eval( -- )
152
153     <"
154     IN: compiler.tests
155     FORGET: boii
156     "> eval( -- )
157     
158     <"
159     IN: compiler.tests
160     TUPLE: boii ;
161     M: boii jeah ;
162     "> eval( -- )
163 ] unit-test
164
165 ! call-next-method cache test
166 GENERIC: c-n-m-cache ( a -- b )
167
168 ! Force it to be unoptimized
169 M: fixnum c-n-m-cache { } [ ] like call( -- ) call-next-method ;
170 M: integer c-n-m-cache 1 + ;
171 M: number c-n-m-cache ;
172
173 [ 3 ] [ 2 c-n-m-cache ] unit-test
174
175 [ ] [ [ M\ integer c-n-m-cache forget ] with-compilation-unit ] unit-test
176
177 [ 2 ] [ 2 c-n-m-cache ] unit-test
178
179 ! Moving a method from one vocab to another doesn't always work
180 GENERIC: move-method-generic ( a -- b )
181
182 [ ] [ "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
183
184 [ ] [ "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
185
186 [ ] [ "IN: generic.tests.a" <string-reader> "move-method-test-1" parse-stream drop ] unit-test
187
188 [ { string } ] [ \ move-method-generic order ] unit-test
189
190 GENERIC: foozul ( a -- b )
191 M: reversed foozul ;
192 M: integer foozul ;
193 M: slice foozul ;
194
195 [ t ] [
196     reversed \ foozul method-for-class
197     reversed \ foozul method
198     eq?
199 ] unit-test
200
201 [ t ] [
202     fixnum \ <=> method-for-class
203     real \ <=> method
204     eq?
205 ] unit-test