]> gitweb.factorcode.org Git - factor.git/blob - core/generic/standard/standard-tests.factor
9a4a4d6016a036195540aab9729210308fa17d38
[factor.git] / core / generic / standard / standard-tests.factor
1 USING: accessors arrays assocs bit-arrays bit-vectors
2 byte-arrays classes.tuple classes.union compiler.crossref
3 compiler.units definitions eval generic generic.single
4 generic.standard io.streams.string kernel make math
5 math.constants math.functions namespaces parser quotations
6 sequences specialized-vectors strings tools.test words ;
7 QUALIFIED-WITH: alien.c-types c
8 FROM: namespaces => set ;
9 SPECIALIZED-VECTOR: c:double
10 IN: generic.standard.tests
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 GENERIC: foobar ( x -- y )
22 M: object foobar drop "Hello world" ;
23 M: fixnum foobar drop "Goodbye cruel world" ;
24
25 [ "Hello world" ] [ 4 foobar foobar ] unit-test
26 [ "Goodbye cruel world" ] [ 4 foobar ] unit-test
27
28 GENERIC: lo-tag-test ( obj -- obj' )
29
30 M: integer lo-tag-test 3 + ;
31 M: float lo-tag-test 4 - ;
32 M: rational lo-tag-test 2 - ;
33 M: complex lo-tag-test sq ;
34
35 [ 8 ] [ 5 >bignum lo-tag-test ] unit-test
36 [ 0.0 ] [ 4.0 lo-tag-test ] unit-test
37 [ -1/2 ] [ 1+1/2 lo-tag-test ] unit-test
38 [ -16 ] [ C{ 0 4 } lo-tag-test ] unit-test
39
40 GENERIC: hi-tag-test ( obj -- obj' )
41
42 M: string hi-tag-test ", in bed" append ;
43 M: integer hi-tag-test 3 + ;
44 M: array hi-tag-test [ hi-tag-test ] map ;
45 M: sequence hi-tag-test reverse ;
46
47 [ B{ 3 2 1 } ] [ B{ 1 2 3 } hi-tag-test ] unit-test
48
49 [ { 6 9 12 } ] [ { 3 6 9 } hi-tag-test ] unit-test
50
51 [ "i like monkeys, in bed" ] [ "i like monkeys" hi-tag-test ] unit-test
52
53 UNION: funnies quotation float complex ;
54
55 GENERIC: funny ( x -- y )
56 M: funnies funny drop 2 ;
57 M: object funny drop 0 ;
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 [ 2 ] [ [ { } ] funny ] unit-test
67 [ 0 ] [ { } funny ] unit-test
68
69 TUPLE: shape ;
70
71 TUPLE: abstract-rectangle < shape width height ;
72
73 TUPLE: rectangle < abstract-rectangle ;
74
75 C: <rectangle> rectangle
76
77 TUPLE: parallelogram < abstract-rectangle skew ;
78
79 C: <parallelogram> parallelogram
80
81 TUPLE: circle < shape radius ;
82
83 C: <circle> circle
84
85 GENERIC: area ( shape -- n )
86
87 M: abstract-rectangle area [ width>> ] [ height>> ] bi * ;
88
89 M: circle area radius>> sq pi * ;
90
91 [ 12 ] [ 4 3 <rectangle> area ] unit-test
92 [ 12 ] [ 4 3 2 <parallelogram> area ] unit-test
93 [ t ] [ 2 <circle> area 4 pi * = ] unit-test
94
95 GENERIC: perimeter ( shape -- n )
96
97 : rectangle-perimeter ( l w -- n ) + 2 * ;
98
99 M: rectangle perimeter
100     [ width>> ] [ height>> ] bi
101     rectangle-perimeter ;
102
103 : hypotenuse ( a b -- c ) [ sq ] bi@ + sqrt ;
104
105 M: parallelogram perimeter
106     [ width>> ]
107     [ [ height>> ] [ skew>> ] bi hypotenuse ] bi
108     rectangle-perimeter ;
109
110 M: circle perimeter 2 * pi * ;
111
112 [ 14 ] [ 4 3 <rectangle> perimeter ] unit-test
113 [ 30.0 ] [ 10 4 3 <parallelogram> perimeter ] unit-test
114
115 PREDICATE: very-funny < funnies number? ;
116
117 GENERIC: gooey ( x -- y )
118 M: very-funny gooey sq ;
119
120 [ 0.25 ] [ 0.5 gooey ] unit-test
121
122 GENERIC: empty-method-test ( x -- y )
123 M: object empty-method-test ;
124 TUPLE: for-arguments-sake ;
125 C: <for-arguments-sake> for-arguments-sake
126
127 M: for-arguments-sake empty-method-test drop "Hi" ;
128
129 TUPLE: another-one ;
130 C: <another-one> another-one
131
132 [ "Hi" ] [ <for-arguments-sake> empty-method-test empty-method-test ] unit-test
133 [ T{ another-one f } ] [ <another-one> empty-method-test ] unit-test
134
135 GENERIC: big-mix-test ( obj -- obj' )
136
137 M: object big-mix-test drop "object" ;
138
139 M: tuple big-mix-test drop "tuple" ;
140
141 M: integer big-mix-test drop "integer" ;
142
143 M: float big-mix-test drop "float" ;
144
145 M: complex big-mix-test drop "complex" ;
146
147 M: string big-mix-test drop "string" ;
148
149 M: array big-mix-test drop "array" ;
150
151 M: sequence big-mix-test drop "sequence" ;
152
153 M: rectangle big-mix-test drop "rectangle" ;
154
155 M: parallelogram big-mix-test drop "parallelogram" ;
156
157 M: circle big-mix-test drop "circle" ;
158
159 [ "integer" ] [ 3 big-mix-test ] unit-test
160 [ "float" ] [ 5.0 big-mix-test ] unit-test
161 [ "complex" ] [ -1 sqrt big-mix-test ] unit-test
162 [ "sequence" ] [ B{ 1 2 3 } big-mix-test ] unit-test
163 [ "sequence" ] [ ?{ t f t } big-mix-test ] unit-test
164 [ "sequence" ] [ SBUF" hello world" big-mix-test ] unit-test
165 [ "sequence" ] [ V{ "a" "b" } big-mix-test ] unit-test
166 [ "sequence" ] [ BV{ 1 2 } big-mix-test ] unit-test
167 [ "sequence" ] [ ?V{ t t f f } big-mix-test ] unit-test
168 [ "string" ] [ "hello" big-mix-test ] unit-test
169 [ "rectangle" ] [ 1 2 <rectangle> big-mix-test ] unit-test
170 [ "parallelogram" ] [ 10 4 3 <parallelogram> big-mix-test ] unit-test
171 [ "circle" ] [ 100 <circle> big-mix-test ] unit-test
172 [ "tuple" ] [ H{ } big-mix-test ] unit-test
173 [ "object" ] [ \ + big-mix-test ] unit-test
174
175 GENERIC: small-lo-tag ( obj -- obj )
176
177 M: fixnum small-lo-tag drop "fixnum" ;
178
179 M: string small-lo-tag drop "string" ;
180
181 M: array small-lo-tag drop "array" ;
182
183 M: double-array small-lo-tag drop "double-array" ;
184
185 M: byte-array small-lo-tag drop "byte-array" ;
186
187 [ "fixnum" ] [ 3 small-lo-tag ] unit-test
188
189 [ "double-array" ] [ double-array{ 1.0 } small-lo-tag ] unit-test
190
191 ! Testing recovery from bad method definitions
192 "IN: generic.standard.tests GENERIC: unhappy ( x -- x )" eval( -- )
193 [
194     "IN: generic.standard.tests M: dictionary unhappy ;" eval( -- )
195 ] must-fail
196 [ ] [ "IN: generic.standard.tests GENERIC: unhappy ( x -- x )" eval( -- ) ] unit-test
197
198 GENERIC# complex-combination 1 ( a b -- c )
199 M: string complex-combination drop ;
200 M: object complex-combination nip ;
201
202 [ "hi" ] [ "hi" 3 complex-combination ] unit-test
203 [ "hi" ] [ 3 "hi" complex-combination ] unit-test
204
205 ! Regression
206 TUPLE: first-one ;
207 TUPLE: second-one ;
208 UNION: both first-one union-class ;
209
210 GENERIC: wii ( x -- y )
211 M: both wii drop 3 ;
212 M: second-one wii drop 4 ;
213 M: tuple-class wii drop 5 ;
214 M: integer wii drop 6 ;
215
216 [ 3 ] [ T{ first-one } wii ] unit-test
217
218 GENERIC: tag-and-f ( x -- x x )
219
220 M: fixnum tag-and-f 1 ;
221
222 M: bignum tag-and-f 2 ;
223
224 M: float tag-and-f 3 ;
225
226 M: f tag-and-f 4 ;
227
228 [ f 4 ] [ f tag-and-f ] unit-test
229
230 [ 3.4 3 ] [ 3.4 tag-and-f ] unit-test
231
232 ! Issues with forget
233 GENERIC: generic-forget-test ( a -- b )
234
235 M: f generic-forget-test ;
236
237 [ ] [ \ f \ generic-forget-test lookup-method "m" set ] unit-test
238
239 [ ] [ [ "m" get forget ] with-compilation-unit ] unit-test
240
241 [ ] [ "IN: generic.standard.tests M: f generic-forget-test ;" eval( -- ) ] unit-test
242
243 [ ] [ [ "m" get forget ] with-compilation-unit ] unit-test
244
245 [ f ] [ f generic-forget-test ] unit-test
246
247 ! erg's regression
248 [ ] [
249     """IN: generic.standard.tests
250
251     GENERIC: jeah ( a -- b )
252     TUPLE: boii ;
253     M: boii jeah ;
254     GENERIC: jeah* ( a -- b )
255     M: boii jeah* jeah ;""" eval( -- )
256
257     """IN: generic.standard.tests
258     FORGET: boii""" eval( -- )
259     
260     """IN: generic.standard.tests
261     TUPLE: boii ;
262     M: boii jeah ;""" eval( -- )
263 ] unit-test
264
265 ! Testing next-method
266 TUPLE: person ;
267
268 TUPLE: intern < person ;
269
270 TUPLE: employee < person ;
271
272 TUPLE: tape-monkey < employee ;
273
274 TUPLE: manager < employee ;
275
276 TUPLE: junior-manager < manager ;
277
278 TUPLE: middle-manager < manager ;
279
280 TUPLE: senior-manager < manager ;
281
282 TUPLE: executive < senior-manager ;
283
284 TUPLE: ceo < executive ;
285
286 GENERIC: salary ( person -- n )
287
288 M: intern salary
289     #! Intentional mistake.
290     call-next-method ;
291
292 M: employee salary drop 24000 ;
293
294 M: manager salary call-next-method 12000 + ;
295
296 M: middle-manager salary call-next-method 5000 + ;
297
298 M: senior-manager salary call-next-method 15000 + ;
299
300 M: executive salary call-next-method 2 * ;
301
302 M: ceo salary
303     #! Intentional error.
304     drop 5 call-next-method 3 * ;
305
306 [ salary ] must-infer
307
308 [ 24000 ] [ employee boa salary ] unit-test
309
310 [ 24000 ] [ tape-monkey boa salary ] unit-test
311
312 [ 36000 ] [ junior-manager boa salary ] unit-test
313
314 [ 41000 ] [ middle-manager boa salary ] unit-test
315
316 [ 51000 ] [ senior-manager boa salary ] unit-test
317
318 [ 102000 ] [ executive boa salary ] unit-test
319
320 [ ceo boa salary ]
321 [ T{ inconsistent-next-method f ceo salary } = ] must-fail-with
322
323 [ intern boa salary ]
324 [ no-next-method? ] must-fail-with
325
326 ! Weird shit
327 TUPLE: a ;
328 TUPLE: b ;
329 TUPLE: c ;
330
331 UNION: x a b ;
332 UNION: y a c ;
333
334 UNION: z x y ;
335
336 GENERIC: funky* ( obj -- )
337
338 M: z funky* "z" , drop ;
339
340 M: x funky* "x" , call-next-method ;
341
342 M: y funky* "y" , call-next-method ;
343
344 M: a funky* "a" , call-next-method ;
345
346 M: b funky* "b" , call-next-method ;
347
348 M: c funky* "c" , call-next-method ;
349
350 : funky ( obj -- seq ) [ funky* ] { } make ;
351
352 [ { "b" "x" "z" } ] [ T{ b } funky ] unit-test
353
354 [ { "c" "y" "z" } ] [ T{ c } funky ] unit-test
355
356 [ t ] [
357     T{ a } funky
358     { { "a" "x" "z" } { "a" "y" "z" } } member?
359 ] unit-test
360
361 ! Changing method combination should not fail
362 [ ] [ "IN: generic.standard.tests GENERIC: xyz ( a -- b )" eval( -- ) ] unit-test
363 [ ] [ "IN: generic.standard.tests MATH: xyz ( a b -- c )" eval( -- ) ] unit-test
364
365 [ f ] [ "xyz" "generic.standard.tests" lookup-word pic-def>> ] unit-test
366 [ f ] [ "xyz" "generic.standard.tests" lookup-word "decision-tree" word-prop ] unit-test
367
368 ! Corner case
369 [ "IN: generic.standard.tests GENERIC# broken-generic# -1 ( a -- b )" eval( -- ) ]
370 [ error>> bad-dispatch-position? ]
371 must-fail-with
372
373 ! Generic words cannot be inlined
374 [ ] [ "IN: generic.standard.tests GENERIC: foo ( -- x )" eval( -- ) ] unit-test
375 [ "IN: generic.standard.tests GENERIC: foo ( -- x ) inline" eval( -- ) ] must-fail
376
377 ! Moving a method from one vocab to another didn't always work
378 GENERIC: move-method-generic ( a -- b )
379
380 [ ] [ "IN: generic.standard.tests.a USE: strings USE: generic.standard.tests M: string move-method-generic ;" <string-reader> "move-method-test-1" parse-stream drop ] unit-test
381
382 [ ] [ "IN: generic.standard.tests.b USE: strings USE: generic.standard.tests M: string move-method-generic ;" <string-reader> "move-method-test-2" parse-stream drop ] unit-test
383
384 [ ] [ "IN: generic.standard.tests.a" <string-reader> "move-method-test-1" parse-stream drop ] unit-test
385
386 [ { string } ] [ \ move-method-generic order ] unit-test
387
388 ! FORGET: on method wrappers
389 GENERIC: forget-test ( a -- b )
390
391 M: integer forget-test 3 + ;
392
393 [ ] [ "IN: generic.standard.tests USE: math FORGET: M\\ integer forget-test" eval( -- ) ] unit-test
394
395 [ { } ] [
396     \ + effect-dependencies-of keys [ method? ] filter
397     [ "method-generic" word-prop \ forget-test eq? ] filter
398 ] unit-test
399
400 [ 10 forget-test ] [ no-method? ] must-fail-with
401
402 ! Declarations on methods
403 GENERIC: flushable-generic ( a -- b ) flushable
404 M: integer flushable-generic ;
405
406 [ t ] [ \ flushable-generic flushable? ] unit-test
407 [ t ] [ M\ integer flushable-generic flushable? ] unit-test
408
409 GENERIC: non-flushable-generic ( a -- b )
410 M: integer non-flushable-generic ; flushable
411
412 [ f ] [ \ non-flushable-generic flushable? ] unit-test
413 [ t ] [ M\ integer non-flushable-generic flushable? ] unit-test
414
415 ! method-for-object, method-for-class, effective-method
416 GENERIC: foozul ( a -- b )
417 M: reversed foozul ;
418 M: integer foozul ;
419 M: slice foozul ;
420
421 [ ] [ reversed \ foozul method-for-class M\ reversed foozul assert= ] unit-test
422 [ ] [ { 1 2 3 } <reversed> \ foozul method-for-object M\ reversed foozul assert= ] unit-test
423 [ ] [ { 1 2 3 } <reversed> \ foozul effective-method M\ reversed foozul assert= drop ] unit-test
424
425 [ ] [ fixnum \ foozul method-for-class M\ integer foozul assert= ] unit-test
426 [ ] [ 13 \ foozul method-for-object M\ integer foozul assert= ] unit-test
427 [ ] [ 13 \ foozul effective-method M\ integer foozul assert= drop ] unit-test
428
429 ! Ensure dynamic and static dispatch match in ambiguous cases
430 UNION: amb-union-1a integer float ;
431 UNION: amb-union-1b float string ;
432
433 GENERIC: amb-generic-1 ( a -- b )
434
435 M: amb-union-1a amb-generic-1 drop "a" ;
436 M: amb-union-1b amb-generic-1 drop "b" ;
437
438 [ ] [
439     5.0 amb-generic-1
440     5.0 \ amb-generic-1 effective-method execute( a -- b ) assert=
441 ] unit-test
442
443 [ ] [
444     5.0 amb-generic-1
445     5.0 float \ amb-generic-1 method-for-class execute( a -- b ) assert=
446 ] unit-test
447
448 UNION: amb-union-2a float string ;
449 UNION: amb-union-2b integer float ;
450
451 GENERIC: amb-generic-2 ( a -- b )
452
453 M: amb-union-2a amb-generic-2 drop "a" ;
454 M: amb-union-2b amb-generic-2 drop "b" ;
455
456 [ ] [
457     5.0 amb-generic-1
458     5.0 \ amb-generic-1 effective-method execute( a -- b ) assert=
459 ] unit-test
460
461 [ ] [
462     5.0 amb-generic-1
463     5.0 float \ amb-generic-1 method-for-class execute( a -- b ) assert=
464 ] unit-test
465
466 TUPLE: amb-tuple-a x ;
467 TUPLE: amb-tuple-b < amb-tuple-a ;
468 PREDICATE: amb-tuple-c < amb-tuple-a x>> 3 = ;
469
470 GENERIC: amb-generic-3 ( a -- b )
471
472 M: amb-tuple-b amb-generic-3 drop "b" ;
473 M: amb-tuple-c amb-generic-3 drop "c" ;
474
475 [ ] [
476     T{ amb-tuple-b f 3 } amb-generic-3
477     T{ amb-tuple-b f 3 } \ amb-generic-3 effective-method execute( a -- b ) assert=
478 ] unit-test
479
480 TUPLE: amb-tuple-d ;
481 UNION: amb-union-4 amb-tuple-a amb-tuple-d ;
482
483 GENERIC: amb-generic-4 ( a -- b )
484
485 M: amb-tuple-b amb-generic-4 drop "b" ;
486 M: amb-union-4 amb-generic-4 drop "4" ;
487
488 [ ] [
489     T{ amb-tuple-b f 3 } amb-generic-4
490     T{ amb-tuple-b f 3 } \ amb-generic-4 effective-method execute( a -- b ) assert=
491 ] unit-test
492
493 [ ] [
494     T{ amb-tuple-b f 3 } amb-generic-4
495     T{ amb-tuple-b f 3 } amb-tuple-b \ amb-generic-4 method-for-class execute( a -- b ) assert=
496 ] unit-test
497
498 MIXIN: amb-mixin-5
499 INSTANCE: amb-tuple-a amb-mixin-5
500 INSTANCE: amb-tuple-d amb-mixin-5
501
502 GENERIC: amb-generic-5 ( a -- b )
503
504 M: amb-tuple-b amb-generic-5 drop "b" ;
505 M: amb-mixin-5 amb-generic-5 drop "5" ;
506
507 [ ] [
508     T{ amb-tuple-b f 3 } amb-generic-5
509     T{ amb-tuple-b f 3 } \ amb-generic-5 effective-method execute( a -- b ) assert=
510 ] unit-test
511
512 [ ] [
513     T{ amb-tuple-b f 3 } amb-generic-5
514     T{ amb-tuple-b f 3 } amb-tuple-b \ amb-generic-5 method-for-class execute( a -- b ) assert=
515 ] unit-test
516
517 UNION: amb-union-6 amb-tuple-b amb-tuple-d ;
518
519 GENERIC: amb-generic-6 ( a -- b )
520
521 M: amb-tuple-a amb-generic-6 drop "a" ;
522 M: amb-union-6 amb-generic-6 drop "6" ;
523
524 [ ] [
525     T{ amb-tuple-b f 3 } amb-generic-6
526     T{ amb-tuple-b f 3 } \ amb-generic-6 effective-method execute( a -- b ) assert=
527 ] unit-test
528
529 [ ] [
530     T{ amb-tuple-b f 3 } amb-generic-6
531     T{ amb-tuple-b f 3 } amb-tuple-b \ amb-generic-6 method-for-class execute( a -- b ) assert=
532 ] unit-test
533
534 MIXIN: amb-mixin-7
535 INSTANCE: amb-tuple-b amb-mixin-7
536 INSTANCE: amb-tuple-d amb-mixin-7
537
538 GENERIC: amb-generic-7 ( a -- b )
539
540 M: amb-tuple-a amb-generic-7 drop "a" ;
541 M: amb-mixin-7 amb-generic-7 drop "7" ;
542
543 [ ] [
544     T{ amb-tuple-b f 3 } amb-generic-7
545     T{ amb-tuple-b f 3 } \ amb-generic-7 effective-method execute( a -- b ) assert=
546 ] unit-test
547
548 [ ] [
549     T{ amb-tuple-b f 3 } amb-generic-7
550     T{ amb-tuple-b f 3 } amb-tuple-b \ amb-generic-7 method-for-class execute( a -- b ) assert=
551 ] unit-test
552
553 ! Same thing as above but with predicate classes
554 PREDICATE: amb-predicate-a < integer 10 mod even? ;
555 PREDICATE: amb-predicate-b < amb-predicate-a 10 mod 4 = ;
556
557 UNION: amb-union-8 amb-predicate-b string ;
558
559 GENERIC: amb-generic-8 ( a -- b )
560
561 M: amb-union-8 amb-generic-8 drop "8" ;
562 M: amb-predicate-a amb-generic-8 drop "a" ;
563
564 [ ] [
565     4 amb-generic-8
566     4 \ amb-generic-8 effective-method execute( a -- b ) assert=
567 ] unit-test