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