]> gitweb.factorcode.org Git - factor.git/blob - core/generic/single/single-tests.factor
Delete empty unit tests files, remove 1- and 1+, reorder IN: lines in a lot of places...
[factor.git] / core / generic / single / single-tests.factor
1 USING: tools.test math math.functions math.constants generic.standard
2 generic.single strings sequences arrays kernel accessors words
3 specialized-arrays.double byte-arrays bit-arrays parser namespaces
4 make quotations stack-checker vectors growable hashtables sbufs
5 prettyprint byte-vectors bit-vectors specialized-vectors.double
6 definitions generic sets graphs assocs grouping see eval ;
7 IN: generic.single.tests
8
9 GENERIC: lo-tag-test ( obj -- obj' )
10
11 M: integer lo-tag-test 3 + ;
12
13 M: float lo-tag-test 4 - ;
14
15 M: rational lo-tag-test 2 - ;
16
17 M: complex lo-tag-test sq ;
18
19 [ 8 ] [ 5 >bignum lo-tag-test ] unit-test
20 [ 0.0 ] [ 4.0 lo-tag-test ] unit-test
21 [ -1/2 ] [ 1+1/2 lo-tag-test ] unit-test
22 [ -16 ] [ C{ 0 4 } lo-tag-test ] unit-test
23
24 GENERIC: hi-tag-test ( obj -- obj' )
25
26 M: string hi-tag-test ", in bed" append ;
27
28 M: integer hi-tag-test 3 + ;
29
30 M: array hi-tag-test [ hi-tag-test ] map ;
31
32 M: sequence hi-tag-test reverse ;
33
34 [ B{ 3 2 1 } ] [ B{ 1 2 3 } hi-tag-test ] unit-test
35
36 [ { 6 9 12 } ] [ { 3 6 9 } hi-tag-test ] unit-test
37
38 [ "i like monkeys, in bed" ] [ "i like monkeys" hi-tag-test ] unit-test
39
40 TUPLE: shape ;
41
42 TUPLE: abstract-rectangle < shape width height ;
43
44 TUPLE: rectangle < abstract-rectangle ;
45
46 C: <rectangle> rectangle
47
48 TUPLE: parallelogram < abstract-rectangle skew ;
49
50 C: <parallelogram> parallelogram
51
52 TUPLE: circle < shape radius ;
53
54 C: <circle> circle
55
56 GENERIC: area ( shape -- n )
57
58 M: abstract-rectangle area [ width>> ] [ height>> ] bi * ;
59
60 M: circle area radius>> sq pi * ;
61
62 [ 12 ] [ 4 3 <rectangle> area ] unit-test
63 [ 12 ] [ 4 3 2 <parallelogram> area ] unit-test
64 [ t ] [ 2 <circle> area 4 pi * = ] unit-test
65
66 GENERIC: perimiter ( shape -- n )
67
68 : rectangle-perimiter ( l w -- n ) + 2 * ;
69
70 M: rectangle perimiter
71     [ width>> ] [ height>> ] bi
72     rectangle-perimiter ;
73
74 : hypotenuse ( a b -- c ) [ sq ] bi@ + sqrt ;
75
76 M: parallelogram perimiter
77     [ width>> ]
78     [ [ height>> ] [ skew>> ] bi hypotenuse ] bi
79     rectangle-perimiter ;
80
81 M: circle perimiter 2 * pi * ;
82
83 [ 14 ] [ 4 3 <rectangle> perimiter ] unit-test
84 [ 30.0 ] [ 10 4 3 <parallelogram> perimiter ] unit-test
85
86 GENERIC: big-mix-test ( obj -- obj' )
87
88 M: object big-mix-test drop "object" ;
89
90 M: tuple big-mix-test drop "tuple" ;
91
92 M: integer big-mix-test drop "integer" ;
93
94 M: float big-mix-test drop "float" ;
95
96 M: complex big-mix-test drop "complex" ;
97
98 M: string big-mix-test drop "string" ;
99
100 M: array big-mix-test drop "array" ;
101
102 M: sequence big-mix-test drop "sequence" ;
103
104 M: rectangle big-mix-test drop "rectangle" ;
105
106 M: parallelogram big-mix-test drop "parallelogram" ;
107
108 M: circle big-mix-test drop "circle" ;
109
110 [ "integer" ] [ 3 big-mix-test ] unit-test
111 [ "float" ] [ 5.0 big-mix-test ] unit-test
112 [ "complex" ] [ -1 sqrt big-mix-test ] unit-test
113 [ "sequence" ] [ double-array{ 1.0 2.0 3.0 } big-mix-test ] unit-test
114 [ "sequence" ] [ B{ 1 2 3 } big-mix-test ] unit-test
115 [ "sequence" ] [ ?{ t f t } big-mix-test ] unit-test
116 [ "sequence" ] [ SBUF" hello world" big-mix-test ] unit-test
117 [ "sequence" ] [ V{ "a" "b" } big-mix-test ] unit-test
118 [ "sequence" ] [ BV{ 1 2 } big-mix-test ] unit-test
119 [ "sequence" ] [ ?V{ t t f f } big-mix-test ] unit-test
120 [ "sequence" ] [ double-vector{ -0.3 4.6 } big-mix-test ] unit-test
121 [ "string" ] [ "hello" big-mix-test ] unit-test
122 [ "rectangle" ] [ 1 2 <rectangle> big-mix-test ] unit-test
123 [ "parallelogram" ] [ 10 4 3 <parallelogram> big-mix-test ] unit-test
124 [ "circle" ] [ 100 <circle> big-mix-test ] unit-test
125 [ "tuple" ] [ H{ } big-mix-test ] unit-test
126 [ "object" ] [ \ + big-mix-test ] unit-test
127
128 GENERIC: small-lo-tag ( obj -- obj )
129
130 M: fixnum small-lo-tag drop "fixnum" ;
131
132 M: string small-lo-tag drop "string" ;
133
134 M: array small-lo-tag drop "array" ;
135
136 M: double-array small-lo-tag drop "double-array" ;
137
138 M: byte-array small-lo-tag drop "byte-array" ;
139
140 [ "fixnum" ] [ 3 small-lo-tag ] unit-test
141
142 [ "double-array" ] [ double-array{ 1.0 } small-lo-tag ] unit-test
143
144 ! Testing next-method
145 TUPLE: person ;
146
147 TUPLE: intern < person ;
148
149 TUPLE: employee < person ;
150
151 TUPLE: tape-monkey < employee ;
152
153 TUPLE: manager < employee ;
154
155 TUPLE: junior-manager < manager ;
156
157 TUPLE: middle-manager < manager ;
158
159 TUPLE: senior-manager < manager ;
160
161 TUPLE: executive < senior-manager ;
162
163 TUPLE: ceo < executive ;
164
165 GENERIC: salary ( person -- n )
166
167 M: intern salary
168     #! Intentional mistake.
169     call-next-method ;
170
171 M: employee salary drop 24000 ;
172
173 M: manager salary call-next-method 12000 + ;
174
175 M: middle-manager salary call-next-method 5000 + ;
176
177 M: senior-manager salary call-next-method 15000 + ;
178
179 M: executive salary call-next-method 2 * ;
180
181 M: ceo salary
182     #! Intentional error.
183     drop 5 call-next-method 3 * ;
184
185 [ salary ] must-infer
186
187 [ 24000 ] [ employee boa salary ] unit-test
188
189 [ 24000 ] [ tape-monkey boa salary ] unit-test
190
191 [ 36000 ] [ junior-manager boa salary ] unit-test
192
193 [ 41000 ] [ middle-manager boa salary ] unit-test
194
195 [ 51000 ] [ senior-manager boa salary ] unit-test
196
197 [ 102000 ] [ executive boa salary ] unit-test
198
199 [ ceo boa salary ]
200 [ T{ inconsistent-next-method f ceo salary } = ] must-fail-with
201
202 [ intern boa salary ]
203 [ no-next-method? ] must-fail-with
204
205 ! Weird shit
206 TUPLE: a ;
207 TUPLE: b ;
208 TUPLE: c ;
209
210 UNION: x a b ;
211 UNION: y a c ;
212
213 UNION: z x y ;
214
215 GENERIC: funky* ( obj -- )
216
217 M: z funky* "z" , drop ;
218
219 M: x funky* "x" , call-next-method ;
220
221 M: y funky* "y" , call-next-method ;
222
223 M: a funky* "a" , call-next-method ;
224
225 M: b funky* "b" , call-next-method ;
226
227 M: c funky* "c" , call-next-method ;
228
229 : funky ( obj -- seq ) [ funky* ] { } make ;
230
231 [ { "b" "x" "z" } ] [ T{ b } funky ] unit-test
232
233 [ { "c" "y" "z" } ] [ T{ c } funky ] unit-test
234
235 [ t ] [
236     T{ a } funky
237     { { "a" "x" "z" } { "a" "y" "z" } } member?
238 ] unit-test
239
240 ! Hooks
241 SYMBOL: my-var
242 HOOK: my-hook my-var ( -- x )
243
244 M: integer my-hook "an integer" ;
245 M: string my-hook "a string" ;
246
247 [ "an integer" ] [ 3 my-var set my-hook ] unit-test
248 [ "a string" ] [ my-hook my-var set my-hook ] unit-test
249 [ 1.0 my-var set my-hook ] [ T{ no-method f 1.0 my-hook } = ] must-fail-with
250
251 HOOK: call-next-hooker my-var ( -- x )
252
253 M: sequence call-next-hooker "sequence" ;
254
255 M: array call-next-hooker call-next-method "array " prepend ;
256
257 M: vector call-next-hooker call-next-method "vector " prepend ;
258
259 M: growable call-next-hooker call-next-method "growable " prepend ;
260
261 [ "vector growable sequence" ] [
262     V{ } my-var [ call-next-hooker ] with-variable
263 ] unit-test
264
265 [ t ] [
266     { } \ nth effective-method nip M\ sequence nth eq?
267 ] unit-test
268
269 [ t ] [
270     \ + \ nth effective-method nip dup \ nth "default-method" word-prop eq? and
271 ] unit-test
272
273 [ ] [ "IN: generic.single.tests GENERIC: xyz ( a -- b )" eval( -- ) ] unit-test
274 [ ] [ "IN: generic.single.tests MATH: xyz ( a b -- c )" eval( -- ) ] unit-test
275
276 [ f ] [ "xyz" "generic.single.tests" lookup pic-def>> ] unit-test
277 [ f ] [ "xyz" "generic.single.tests" lookup "decision-tree" word-prop ] unit-test
278
279 ! Corner case
280 [ "IN: generic.single.tests GENERIC# broken-generic# -1 ( a -- b )" eval( -- ) ]
281 [ error>> bad-dispatch-position? ]
282 must-fail-with