]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/tree/propagation/call-effect/call-effect-tests.factor
Eliminate duplicate syntax for stack effects "(" no longer drops and is identical...
[factor.git] / basis / compiler / tree / propagation / call-effect / call-effect-tests.factor
1 ! Copyright (C) 2009 Slava Pestov, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: compiler.tree.propagation.call-effect tools.test fry math effects kernel
4 compiler.tree.builder compiler.tree.optimizer compiler.tree.debugger sequences
5 eval combinators ;
6 IN: compiler.tree.propagation.call-effect.tests
7
8 [ t ] [ \ + ( a b -- c ) execute-effect-unsafe? ] unit-test
9 [ t ] [ \ + ( a b c -- d e ) execute-effect-unsafe? ] unit-test
10 [ f ] [ \ + ( a b c -- d ) execute-effect-unsafe? ] unit-test
11 [ f ] [ \ call ( x -- ) execute-effect-unsafe? ] unit-test
12
13 [ t ] [ [ + ] cached-effect ( a b -- c ) effect= ] unit-test
14 [ t ] [ 5 [ + ] curry cached-effect ( a -- c ) effect= ] unit-test
15 [ t ] [ 5 [ ] curry cached-effect ( -- c ) effect= ] unit-test
16 [ t ] [ [ dup ] [ drop ] compose cached-effect ( a -- b ) effect= ] unit-test
17 [ t ] [ [ drop ] [ dup ] compose cached-effect ( a b -- c d ) effect= ] unit-test
18 [ t ] [ [ 2drop ] [ dup ] compose cached-effect ( a b c -- d e ) effect= ] unit-test
19 [ t ] [ [ 1 2 3 ] [ 2drop ] compose cached-effect ( -- a ) effect= ] unit-test
20 [ t ] [ [ 1 2 ] [ 3drop ] compose cached-effect ( a -- ) effect= ] unit-test
21
22 : optimized-quot ( quot -- quot' )
23     build-tree optimize-tree nodes>quot ;
24
25 : compiled-call2 ( a quot: ( a -- b ) -- b )
26     call( a -- b ) ;
27
28 : compiled-execute2 ( a b word: ( a b -- c ) -- c )
29     execute( a b -- c ) ;
30
31 [ [ 3 ] ] [ [ 1 2 \ + execute( a b -- c ) ] optimized-quot ] unit-test
32 [ [ 3 ] ] [ [ 1 2 [ + ] call( a b -- c ) ] optimized-quot ] unit-test
33 [ [ 3 ] ] [ [ 1 2 '[ _ + ] call( a -- b ) ] optimized-quot ] unit-test
34 [ [ 3 ] ] [ [ 1 2 '[ _ ] [ + ] compose call( a -- b ) ] optimized-quot ] unit-test
35
36 [ 1 2 { [ + ] } first compiled-call2 ] must-fail
37 [ 3 ] [ 1 2 { + } first compiled-execute2 ] unit-test
38 [ 3 ] [ 1 2 '[ _ + ] compiled-call2 ] unit-test
39 [ 3 ] [ 1 2 '[ _ ] [ + ] compose compiled-call2 ] unit-test
40 [ 3 ] [ 1 2 \ + compiled-execute2 ] unit-test
41
42 [ 3 ] [ 1 2 { [ + ] } first call( a b -- c ) ] unit-test
43 [ 3 ] [ 1 2 { + } first execute( a b -- c ) ] unit-test
44 [ 3 ] [ 1 2 '[ _ + ] call( a -- b ) ] unit-test
45 [ 3 ] [ 1 2 '[ _ ] [ + ] compose call( a -- b ) ] unit-test
46
47 [ t ] [ [ 2 '[ _ ] [ + ] compose ] final-info first infer-value ( object -- object ) effect= ] unit-test
48 [ t ] [ [ 2 '[ _ ] 1 '[ _ + ] compose ] final-info first infer-value ( -- object ) effect= ] unit-test
49 [ t ] [ [ 2 '[ _ + ] ] final-info first infer-value ( object -- object ) effect= ] unit-test
50 [ f ] [ [ [ [ ] [ 1 ] if ] ] final-info first infer-value ] unit-test
51 [ t ] [ [ [ 1 ] '[ @ ] ] final-info first infer-value ( -- object ) effect= ] unit-test
52 [ f ] [ [ dup drop ] final-info first infer-value ] unit-test
53
54 ! This should not hang
55 [ ] [ [ [ dup call( quot -- ) ] dup call( quot -- ) ] final-info drop ] unit-test
56 [ ] [ [ [ dup curry call( quot -- ) ] dup curry call( quot -- ) ] final-info drop ] unit-test
57
58 ! This should get inlined, because the parameter to the curry is literal even though
59 ! [ boa ] by itself doesn't infer
60 TUPLE: a-tuple x ;
61
62 [ V{ a-tuple } ] [ [ a-tuple '[ _ boa ] call( x -- tuple ) ] final-classes ] unit-test
63
64 ! See if redefinitions are handled correctly
65 : call(-redefine-test ( a -- b ) 1 + ;
66
67 : test-quotatation ( -- quot ) [ call(-redefine-test ] ;
68
69 [ t ] [ test-quotatation cached-effect ( a -- b ) effect<= ] unit-test
70
71 [ ] [ "IN: compiler.tree.propagation.call-effect.tests USE: math : call(-redefine-test ( a b -- c ) + ;" eval( -- ) ] unit-test
72
73 [ t ] [ test-quotatation cached-effect ( a b -- c ) effect<= ] unit-test
74
75 : inline-cache-invalidation-test ( a b c -- c ) call( a b -- c ) ;
76
77 [ 4 ] [ 1 3 test-quotatation inline-cache-invalidation-test ] unit-test
78
79 [ ] [ "IN: compiler.tree.propagation.call-effect.tests USE: math : call(-redefine-test ( a -- c ) 1 + ;" eval( -- ) ] unit-test
80
81 [ 1 3 test-quotatation inline-cache-invalidation-test ] [ T{ wrong-values f [ call(-redefine-test ] ( a b -- c ) } = ] must-fail-with
82
83 ! See if redefining a tuple class bumps effect counter
84 TUPLE: my-tuple a b c ;
85
86 : my-quot ( -- quot ) [ my-tuple boa ] ;
87
88 : my-word ( a b c q -- result ) call( a b c -- result ) ;
89
90 [ T{ my-tuple f 1 2 3 } ] [ 1 2 3 my-quot my-word ] unit-test
91
92 [ ] [ "IN: compiler.tree.propagation.call-effect.tests TUPLE: my-tuple a b ;" eval( -- ) ] unit-test
93
94 [ 1 2 3 my-quot my-word ] [ wrong-values? ] must-fail-with