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# -1 ( a -- b )" eval( -- ) ]
370 [ error>> bad-dispatch-position? ]
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
377 ! Moving a method from one vocab to another didn't always work
378 GENERIC: move-method-generic ( a -- b )
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
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
384 { } [ "IN: generic.standard.tests.a" <string-reader> "move-method-test-1" parse-stream drop ] unit-test
386 { { string } } [ \ move-method-generic order ] unit-test
388 ! FORGET: on method wrappers
389 GENERIC: forget-test ( a -- b )
391 M: integer forget-test 3 + ;
393 { } [ "IN: generic.standard.tests USE: math FORGET: M\\ integer forget-test" eval( -- ) ] unit-test
396 \ + effect-dependencies-of keys [ method? ] filter
397 [ "method-generic" word-prop \ forget-test eq? ] filter
400 [ 10 forget-test ] [ no-method? ] must-fail-with
402 ! Declarations on methods
403 GENERIC: flushable-generic ( a -- b ) flushable
404 M: integer flushable-generic ;
406 { t } [ \ flushable-generic flushable? ] unit-test
407 { t } [ M\ integer flushable-generic flushable? ] unit-test
409 GENERIC: non-flushable-generic ( a -- b )
410 M: integer non-flushable-generic ; flushable
412 { f } [ \ non-flushable-generic flushable? ] unit-test
413 { t } [ M\ integer non-flushable-generic flushable? ] unit-test
415 ! method-for-object, method-for-class, effective-method
416 GENERIC: foozul ( a -- b )
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
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
429 ! Ensure dynamic and static dispatch match in ambiguous cases
430 UNION: amb-union-1a integer float ;
431 UNION: amb-union-1b float string ;
433 GENERIC: amb-generic-1 ( a -- b )
435 M: amb-union-1a amb-generic-1 drop "a" ;
436 M: amb-union-1b amb-generic-1 drop "b" ;
440 5.0 \ amb-generic-1 effective-method execute( a -- b ) assert=
445 5.0 float \ amb-generic-1 method-for-class execute( a -- b ) assert=
448 UNION: amb-union-2a float string ;
449 UNION: amb-union-2b integer float ;
451 GENERIC: amb-generic-2 ( a -- b )
453 M: amb-union-2a amb-generic-2 drop "a" ;
454 M: amb-union-2b amb-generic-2 drop "b" ;
458 5.0 \ amb-generic-1 effective-method execute( a -- b ) assert=
463 5.0 float \ amb-generic-1 method-for-class execute( a -- b ) assert=
466 TUPLE: amb-tuple-a x ;
467 TUPLE: amb-tuple-b < amb-tuple-a ;
468 PREDICATE: amb-tuple-c < amb-tuple-a x>> 3 = ;
470 GENERIC: amb-generic-3 ( a -- b )
472 M: amb-tuple-b amb-generic-3 drop "b" ;
473 M: amb-tuple-c amb-generic-3 drop "c" ;
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=
481 UNION: amb-union-4 amb-tuple-a amb-tuple-d ;
483 GENERIC: amb-generic-4 ( a -- b )
485 M: amb-tuple-b amb-generic-4 drop "b" ;
486 M: amb-union-4 amb-generic-4 drop "4" ;
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=
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=
499 INSTANCE: amb-tuple-a amb-mixin-5
500 INSTANCE: amb-tuple-d amb-mixin-5
502 GENERIC: amb-generic-5 ( a -- b )
504 M: amb-tuple-b amb-generic-5 drop "b" ;
505 M: amb-mixin-5 amb-generic-5 drop "5" ;
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=
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=
517 UNION: amb-union-6 amb-tuple-b amb-tuple-d ;
519 GENERIC: amb-generic-6 ( a -- b )
521 M: amb-tuple-a amb-generic-6 drop "a" ;
522 M: amb-union-6 amb-generic-6 drop "6" ;
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=
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=
535 INSTANCE: amb-tuple-b amb-mixin-7
536 INSTANCE: amb-tuple-d amb-mixin-7
538 GENERIC: amb-generic-7 ( a -- b )
540 M: amb-tuple-a amb-generic-7 drop "a" ;
541 M: amb-mixin-7 amb-generic-7 drop "7" ;
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=
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=
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 = ;
557 UNION: amb-union-8 amb-predicate-b string ;
559 GENERIC: amb-generic-8 ( a -- b )
561 M: amb-union-8 amb-generic-8 drop "8" ;
562 M: amb-predicate-a amb-generic-8 drop "a" ;
566 4 \ amb-generic-8 effective-method execute( a -- b ) assert=