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
12 GENERIC: class-of ( x -- y )
14 M: fixnum class-of drop "fixnum" ;
15 M: word class-of drop "word" ;
17 { "fixnum" } [ 5 class-of ] unit-test
18 { "word" } [ \ class-of class-of ] unit-test
19 [ 3.4 class-of ] must-fail
21 GENERIC: foobar ( x -- y )
22 M: object foobar drop "Hello world" ;
23 M: fixnum foobar drop "Goodbye cruel world" ;
25 { "Hello world" } [ 4 foobar foobar ] unit-test
26 { "Goodbye cruel world" } [ 4 foobar ] unit-test
28 GENERIC: lo-tag-test ( obj -- obj' )
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 ;
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
40 GENERIC: hi-tag-test ( obj -- obj' )
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 ;
47 { B{ 3 2 1 } } [ B{ 1 2 3 } hi-tag-test ] unit-test
49 { { 6 9 12 } } [ { 3 6 9 } hi-tag-test ] unit-test
51 { "i like monkeys, in bed" } [ "i like monkeys" hi-tag-test ] unit-test
53 UNION: funnies quotation float complex ;
55 GENERIC: funny ( x -- y )
56 M: funnies funny drop 2 ;
57 M: object funny drop 0 ;
59 GENERIC: union-containment ( x -- y )
60 M: integer union-containment drop 1 ;
61 M: number union-containment drop 2 ;
63 { 1 } [ 1 union-containment ] unit-test
64 { 2 } [ 1.0 union-containment ] unit-test
66 { 2 } [ [ { } ] funny ] unit-test
67 { 0 } [ { } funny ] unit-test
71 TUPLE: abstract-rectangle < shape width height ;
73 TUPLE: rectangle < abstract-rectangle ;
75 C: <rectangle> rectangle
77 TUPLE: parallelogram < abstract-rectangle skew ;
79 C: <parallelogram> parallelogram
81 TUPLE: circle < shape radius ;
85 GENERIC: area ( shape -- n )
87 M: abstract-rectangle area [ width>> ] [ height>> ] bi * ;
89 M: circle area radius>> sq pi * ;
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
95 GENERIC: perimeter ( shape -- n )
97 : rectangle-perimeter ( l w -- n ) + 2 * ;
99 M: rectangle perimeter
100 [ width>> ] [ height>> ] bi
101 rectangle-perimeter ;
103 : hypotenuse ( a b -- c ) [ sq ] bi@ + sqrt ;
105 M: parallelogram perimeter
107 [ [ height>> ] [ skew>> ] bi hypotenuse ] bi
108 rectangle-perimeter ;
110 M: circle perimeter 2 * pi * ;
112 { 14 } [ 4 3 <rectangle> perimeter ] unit-test
113 { 30.0 } [ 10 4 3 <parallelogram> perimeter ] unit-test
115 PREDICATE: very-funny < funnies number? ;
117 GENERIC: gooey ( x -- y )
118 M: very-funny gooey sq ;
120 { 0.25 } [ 0.5 gooey ] unit-test
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
127 M: for-arguments-sake empty-method-test drop "Hi" ;
130 C: <another-one> another-one
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
135 GENERIC: big-mix-test ( obj -- obj' )
137 M: object big-mix-test drop "object" ;
139 M: tuple big-mix-test drop "tuple" ;
141 M: integer big-mix-test drop "integer" ;
143 M: float big-mix-test drop "float" ;
145 M: complex big-mix-test drop "complex" ;
147 M: string big-mix-test drop "string" ;
149 M: array big-mix-test drop "array" ;
151 M: sequence big-mix-test drop "sequence" ;
153 M: rectangle big-mix-test drop "rectangle" ;
155 M: parallelogram big-mix-test drop "parallelogram" ;
157 M: circle big-mix-test drop "circle" ;
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
175 GENERIC: small-lo-tag ( obj -- obj )
177 M: fixnum small-lo-tag drop "fixnum" ;
179 M: string small-lo-tag drop "string" ;
181 M: array small-lo-tag drop "array" ;
183 M: double-array small-lo-tag drop "double-array" ;
185 M: byte-array small-lo-tag drop "byte-array" ;
187 { "fixnum" } [ 3 small-lo-tag ] unit-test
189 { "double-array" } [ double-array{ 1.0 } small-lo-tag ] unit-test
191 ! Testing recovery from bad method definitions
192 "IN: generic.standard.tests GENERIC: unhappy ( x -- x )" eval( -- )
194 "IN: generic.standard.tests M: dictionary unhappy ;" eval( -- )
196 { } [ "IN: generic.standard.tests GENERIC: unhappy ( x -- x )" eval( -- ) ] unit-test
198 GENERIC# complex-combination 1 ( a b -- c )
199 M: string complex-combination drop ;
200 M: object complex-combination nip ;
202 { "hi" } [ "hi" 3 complex-combination ] unit-test
203 { "hi" } [ 3 "hi" complex-combination ] unit-test
208 UNION: both first-one union-class ;
210 GENERIC: wii ( x -- y )
212 M: second-one wii drop 4 ;
213 M: tuple-class wii drop 5 ;
214 M: integer wii drop 6 ;
216 { 3 } [ T{ first-one } wii ] unit-test
218 GENERIC: tag-and-f ( x -- x x )
220 M: fixnum tag-and-f 1 ;
222 M: bignum tag-and-f 2 ;
224 M: float tag-and-f 3 ;
228 { f 4 } [ f tag-and-f ] unit-test
230 { 3.4 3 } [ 3.4 tag-and-f ] unit-test
233 GENERIC: generic-forget-test ( a -- b )
235 M: f generic-forget-test ;
237 { } [ \ f \ generic-forget-test lookup-method "m" set ] unit-test
239 { } [ [ "m" get forget ] with-compilation-unit ] unit-test
241 { } [ "IN: generic.standard.tests M: f generic-forget-test ;" eval( -- ) ] unit-test
243 { } [ [ "m" get forget ] with-compilation-unit ] unit-test
245 { f } [ f generic-forget-test ] unit-test
249 "IN: generic.standard.tests
251 GENERIC: jeah ( a -- b )
254 GENERIC: jeah* ( a -- b )
255 M: boii jeah* jeah ;" eval( -- )
257 "IN: generic.standard.tests
258 FORGET: boii" eval( -- )
260 "IN: generic.standard.tests
262 M: boii jeah ;" eval( -- )
265 ! Testing next-method
268 TUPLE: intern < person ;
270 TUPLE: employee < person ;
272 TUPLE: tape-monkey < employee ;
274 TUPLE: manager < employee ;
276 TUPLE: junior-manager < manager ;
278 TUPLE: middle-manager < manager ;
280 TUPLE: senior-manager < manager ;
282 TUPLE: executive < senior-manager ;
284 TUPLE: ceo < executive ;
286 GENERIC: salary ( person -- n )
289 #! Intentional mistake.
292 M: employee salary drop 24000 ;
294 M: manager salary call-next-method 12000 + ;
296 M: middle-manager salary call-next-method 5000 + ;
298 M: senior-manager salary call-next-method 15000 + ;
300 M: executive salary call-next-method 2 * ;
303 #! Intentional error.
304 drop 5 call-next-method 3 * ;
306 [ salary ] must-infer
308 { 24000 } [ employee boa salary ] unit-test
310 { 24000 } [ tape-monkey boa salary ] unit-test
312 { 36000 } [ junior-manager boa salary ] unit-test
314 { 41000 } [ middle-manager boa salary ] unit-test
316 { 51000 } [ senior-manager boa salary ] unit-test
318 { 102000 } [ executive boa salary ] unit-test
321 [ T{ inconsistent-next-method f ceo salary } = ] must-fail-with
323 [ intern boa salary ]
324 [ no-next-method? ] must-fail-with
336 GENERIC: funky* ( obj -- )
338 M: z funky* "z" , drop ;
340 M: x funky* "x" , call-next-method ;
342 M: y funky* "y" , call-next-method ;
344 M: a funky* "a" , call-next-method ;
346 M: b funky* "b" , call-next-method ;
348 M: c funky* "c" , call-next-method ;
350 : funky ( obj -- seq ) [ funky* ] { } make ;
352 { { "b" "x" "z" } } [ T{ b } funky ] unit-test
354 { { "c" "y" "z" } } [ T{ c } funky ] unit-test
358 { { "a" "x" "z" } { "a" "y" "z" } } member?
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
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
369 [ "IN: generic.standard.tests GENERIC: broken-generic ( -- )" eval( -- ) ]
370 [ error>> bad-dispatch-position? ]
372 [ "IN: generic.standard.tests GENERIC# broken-generic# -1 ( a -- b )" eval( -- ) ]
373 [ error>> bad-dispatch-position? ]
375 [ "IN: generic.standard.tests GENERIC# broken-generic# 1 ( a -- b )" eval( -- ) ]
376 [ error>> bad-dispatch-position? ]
378 [ "IN: generic.standard.tests GENERIC# broken-generic# 2/3 ( a b c -- )" eval( -- ) ]
379 [ error>> bad-dispatch-position? ]
382 ! Generic words cannot be inlined
383 { } [ "IN: generic.standard.tests GENERIC: foo ( x -- x )" eval( -- ) ] unit-test
384 [ "IN: generic.standard.tests GENERIC: foo ( x -- x ) inline" eval( -- ) ] must-fail
386 ! Moving a method from one vocab to another didn't always work
387 GENERIC: move-method-generic ( a -- b )
389 { } [ "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
391 { } [ "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
393 { } [ "IN: generic.standard.tests.a" <string-reader> "move-method-test-1" parse-stream drop ] unit-test
395 { { string } } [ \ move-method-generic order ] unit-test
397 ! FORGET: on method wrappers
398 GENERIC: forget-test ( a -- b )
400 M: integer forget-test 3 + ;
402 { } [ "IN: generic.standard.tests USE: math FORGET: M\\ integer forget-test" eval( -- ) ] unit-test
405 \ + effect-dependencies-of keys [ method? ] filter
406 [ "method-generic" word-prop \ forget-test eq? ] filter
409 [ 10 forget-test ] [ no-method? ] must-fail-with
411 ! Declarations on methods
412 GENERIC: flushable-generic ( a -- b ) flushable
413 M: integer flushable-generic ;
415 { t } [ \ flushable-generic flushable? ] unit-test
416 { t } [ M\ integer flushable-generic flushable? ] unit-test
418 GENERIC: non-flushable-generic ( a -- b )
419 M: integer non-flushable-generic ; flushable
421 { f } [ \ non-flushable-generic flushable? ] unit-test
422 { t } [ M\ integer non-flushable-generic flushable? ] unit-test
424 ! method-for-object, method-for-class, effective-method
425 GENERIC: foozul ( a -- b )
430 { } [ reversed \ foozul method-for-class M\ reversed foozul assert= ] unit-test
431 { } [ { 1 2 3 } <reversed> \ foozul method-for-object M\ reversed foozul assert= ] unit-test
432 { } [ { 1 2 3 } <reversed> \ foozul effective-method M\ reversed foozul assert= drop ] unit-test
434 { } [ fixnum \ foozul method-for-class M\ integer foozul assert= ] unit-test
435 { } [ 13 \ foozul method-for-object M\ integer foozul assert= ] unit-test
436 { } [ 13 \ foozul effective-method M\ integer foozul assert= drop ] unit-test
438 ! Ensure dynamic and static dispatch match in ambiguous cases
439 UNION: amb-union-1a integer float ;
440 UNION: amb-union-1b float string ;
442 GENERIC: amb-generic-1 ( a -- b )
444 M: amb-union-1a amb-generic-1 drop "a" ;
445 M: amb-union-1b amb-generic-1 drop "b" ;
449 5.0 \ amb-generic-1 effective-method execute( a -- b ) assert=
454 5.0 float \ amb-generic-1 method-for-class execute( a -- b ) assert=
457 UNION: amb-union-2a float string ;
458 UNION: amb-union-2b integer float ;
460 GENERIC: amb-generic-2 ( a -- b )
462 M: amb-union-2a amb-generic-2 drop "a" ;
463 M: amb-union-2b amb-generic-2 drop "b" ;
467 5.0 \ amb-generic-1 effective-method execute( a -- b ) assert=
472 5.0 float \ amb-generic-1 method-for-class execute( a -- b ) assert=
475 TUPLE: amb-tuple-a x ;
476 TUPLE: amb-tuple-b < amb-tuple-a ;
477 PREDICATE: amb-tuple-c < amb-tuple-a x>> 3 = ;
479 GENERIC: amb-generic-3 ( a -- b )
481 M: amb-tuple-b amb-generic-3 drop "b" ;
482 M: amb-tuple-c amb-generic-3 drop "c" ;
485 T{ amb-tuple-b f 3 } amb-generic-3
486 T{ amb-tuple-b f 3 } \ amb-generic-3 effective-method execute( a -- b ) assert=
490 UNION: amb-union-4 amb-tuple-a amb-tuple-d ;
492 GENERIC: amb-generic-4 ( a -- b )
494 M: amb-tuple-b amb-generic-4 drop "b" ;
495 M: amb-union-4 amb-generic-4 drop "4" ;
498 T{ amb-tuple-b f 3 } amb-generic-4
499 T{ amb-tuple-b f 3 } \ amb-generic-4 effective-method execute( a -- b ) assert=
503 T{ amb-tuple-b f 3 } amb-generic-4
504 T{ amb-tuple-b f 3 } amb-tuple-b \ amb-generic-4 method-for-class execute( a -- b ) assert=
508 INSTANCE: amb-tuple-a amb-mixin-5
509 INSTANCE: amb-tuple-d amb-mixin-5
511 GENERIC: amb-generic-5 ( a -- b )
513 M: amb-tuple-b amb-generic-5 drop "b" ;
514 M: amb-mixin-5 amb-generic-5 drop "5" ;
517 T{ amb-tuple-b f 3 } amb-generic-5
518 T{ amb-tuple-b f 3 } \ amb-generic-5 effective-method execute( a -- b ) assert=
522 T{ amb-tuple-b f 3 } amb-generic-5
523 T{ amb-tuple-b f 3 } amb-tuple-b \ amb-generic-5 method-for-class execute( a -- b ) assert=
526 UNION: amb-union-6 amb-tuple-b amb-tuple-d ;
528 GENERIC: amb-generic-6 ( a -- b )
530 M: amb-tuple-a amb-generic-6 drop "a" ;
531 M: amb-union-6 amb-generic-6 drop "6" ;
534 T{ amb-tuple-b f 3 } amb-generic-6
535 T{ amb-tuple-b f 3 } \ amb-generic-6 effective-method execute( a -- b ) assert=
539 T{ amb-tuple-b f 3 } amb-generic-6
540 T{ amb-tuple-b f 3 } amb-tuple-b \ amb-generic-6 method-for-class execute( a -- b ) assert=
544 INSTANCE: amb-tuple-b amb-mixin-7
545 INSTANCE: amb-tuple-d amb-mixin-7
547 GENERIC: amb-generic-7 ( a -- b )
549 M: amb-tuple-a amb-generic-7 drop "a" ;
550 M: amb-mixin-7 amb-generic-7 drop "7" ;
553 T{ amb-tuple-b f 3 } amb-generic-7
554 T{ amb-tuple-b f 3 } \ amb-generic-7 effective-method execute( a -- b ) assert=
558 T{ amb-tuple-b f 3 } amb-generic-7
559 T{ amb-tuple-b f 3 } amb-tuple-b \ amb-generic-7 method-for-class execute( a -- b ) assert=
562 ! Same thing as above but with predicate classes
563 PREDICATE: amb-predicate-a < integer 10 mod even? ;
564 PREDICATE: amb-predicate-b < amb-predicate-a 10 mod 4 = ;
566 UNION: amb-union-8 amb-predicate-b string ;
568 GENERIC: amb-generic-8 ( a -- b )
570 M: amb-union-8 amb-generic-8 drop "8" ;
571 M: amb-predicate-a amb-generic-8 drop "a" ;
575 4 \ amb-generic-8 effective-method execute( a -- b ) assert=