]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/tree/propagation/call-effect/call-effect-tests.factor
change ERROR: words from throw-foo back to foo.
[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: accessors combinators combinators.private compiler.tree
4 compiler.tree.propagation.call-effect compiler.units math effects kernel
5 compiler.tree.builder compiler.tree.optimizer compiler.tree.debugger sequences
6 eval fry kernel.private tools.test ;
7 IN: compiler.tree.propagation.call-effect.tests
8
9 ! cached-effect
10 { t } [ [ + ] cached-effect ( a b -- c ) effect= ] unit-test
11 { t } [ 5 [ + ] curry cached-effect ( a -- c ) effect= ] unit-test
12 { t } [ 5 [ ] curry cached-effect ( -- c ) effect= ] unit-test
13 { t } [ [ dup ] [ drop ] compose cached-effect ( a -- b ) effect= ] unit-test
14 { t } [ [ drop ] [ dup ] compose cached-effect ( a b -- c d ) effect= ] unit-test
15 { t } [ [ 2drop ] [ dup ] compose cached-effect ( a b c -- d e ) effect= ] unit-test
16 { t } [ [ 1 2 3 ] [ 2drop ] compose cached-effect ( -- a ) effect= ] unit-test
17 { t } [ [ 1 2 ] [ 3drop ] compose cached-effect ( a -- ) effect= ] unit-test
18
19 ! call-effect>quot
20 {
21     [ drop ( a -- b ) T{ inline-cache } call-effect-ic ]
22 } [
23     ( a -- b ) call-effect>quot
24 ] unit-test
25
26 ! call-effect-slow>quot
27 { 10000 } [
28     100 [ sq ] ( a -- b ) call-effect-slow>quot call
29 ] unit-test
30
31 {
32     [
33         [
34             ( -- a b c )
35             2dup
36             [
37                 [ [ get-datastack ] dip dip ] dip dup terminated?>>
38                 [ 2drop f ] [
39                     dup in>> length swap out>> length
40                     check-datastack
41                 ] if
42             ]
43             2dip
44             rot
45             [ 2drop ]
46             [ wrong-values ]
47             if
48         ]
49         ( obj -- a b c )
50         call-effect-unsafe
51     ]
52 } [
53     ( -- a b c ) call-effect-slow>quot
54 ] unit-test
55
56 ! call-effect-unsafe?
57 { f t } [
58     [ ] ( m -- ) call-effect-unsafe?
59     [ ] ( x -- x ) call-effect-unsafe?
60 ] unit-test
61
62 ! call-inlining
63 {
64     [ drop f T{ inline-cache } call-effect-ic ]
65 } [
66     T{ #call
67        { word call-effect }
68        { in-d V{ 165186755 165186756 165186754 } }
69        { out-d { 165186757 } }
70     } call-inlining
71 ] unit-test
72
73 ! execute-effect-unsafe?
74 { t } [ \ + ( a b -- c ) execute-effect-unsafe? ] unit-test
75 { t } [ \ + ( a b c -- d e ) execute-effect-unsafe? ] unit-test
76 { f } [ \ + ( a b c -- d ) execute-effect-unsafe? ] unit-test
77 { f } [ \ call ( x -- ) execute-effect-unsafe? ] unit-test
78
79 ! update-inline-cache
80 { t } [
81     [ boa ] inline-cache new [ update-inline-cache ] keep
82     [ boa ] effect-counter inline-cache boa =
83 ] unit-test
84
85
86 : optimized-quot ( quot -- quot' )
87     build-tree optimize-tree nodes>quot ;
88
89 : compiled-call2 ( a quot: ( a -- b ) -- b )
90     call( a -- b ) ;
91
92 : compiled-execute2 ( a b word: ( a b -- c ) -- c )
93     execute( a b -- c ) ;
94
95 { [ 3 ] } [ [ 1 2 \ + execute( a b -- c ) ] optimized-quot ] unit-test
96 { [ 3 ] } [ [ 1 2 [ + ] call( a b -- c ) ] optimized-quot ] unit-test
97 { [ 3 ] } [ [ 1 2 '[ _ + ] call( a -- b ) ] optimized-quot ] unit-test
98 { [ 3 ] } [ [ 1 2 '[ _ ] [ + ] compose call( a -- b ) ] optimized-quot ] unit-test
99
100 [ 1 2 { [ + ] } first compiled-call2 ] must-fail
101 { 3 } [ 1 2 { + } first compiled-execute2 ] unit-test
102 { 3 } [ 1 2 '[ _ + ] compiled-call2 ] unit-test
103 { 3 } [ 1 2 '[ _ ] [ + ] compose compiled-call2 ] unit-test
104 { 3 } [ 1 2 \ + compiled-execute2 ] unit-test
105
106 { 3 } [ 1 2 { [ + ] } first call( a b -- c ) ] unit-test
107 { 3 } [ 1 2 { + } first execute( a b -- c ) ] unit-test
108 { 3 } [ 1 2 '[ _ + ] call( a -- b ) ] unit-test
109 { 3 } [ 1 2 '[ _ ] [ + ] compose call( a -- b ) ] unit-test
110
111 { t } [ [ 2 '[ _ ] [ + ] compose ] final-info first infer-value ( object -- object ) effect= ] unit-test
112 { t } [ [ 2 '[ _ ] 1 '[ _ + ] compose ] final-info first infer-value ( -- object ) effect= ] unit-test
113 { t } [ [ 2 '[ _ + ] ] final-info first infer-value ( object -- object ) effect= ] unit-test
114 { f } [ [ [ [ ] [ 1 ] if ] ] final-info first infer-value ] unit-test
115 { t } [ [ [ 1 ] '[ @ ] ] final-info first infer-value ( -- object ) effect= ] unit-test
116 { f } [ [ dup drop ] final-info first infer-value ] unit-test
117
118 ! This should not hang
119 { } [ [ [ dup call( quot -- ) ] dup call( quot -- ) ] final-info drop ] unit-test
120 { } [ [ [ dup curry call( quot -- ) ] dup curry call( quot -- ) ] final-info drop ] unit-test
121
122 ! This should get inlined, because the parameter to the curry is literal even though
123 ! [ boa ] by itself doesn't infer
124 TUPLE: a-tuple x ;
125
126 { V{ a-tuple } } [ [ a-tuple '[ _ boa ] call( x -- tuple ) ] final-classes ] unit-test
127
128 ! See if redefinitions are handled correctly
129 : call(-redefine-test ( a -- b ) 1 + ;
130
131 : test-quotatation ( -- quot ) [ call(-redefine-test ] ;
132
133 { t } [ test-quotatation cached-effect ( a -- b ) effect<= ] unit-test
134
135 { } [ "IN: compiler.tree.propagation.call-effect.tests USE: math : call(-redefine-test ( a b -- c ) + ;" eval( -- ) ] unit-test
136
137 { t } [ test-quotatation cached-effect ( a b -- c ) effect<= ] unit-test
138
139 : inline-cache-invalidation-test ( a b c -- c ) call( a b -- c ) ;
140
141 { 4 } [ 1 3 test-quotatation inline-cache-invalidation-test ] unit-test
142
143 { } [ "IN: compiler.tree.propagation.call-effect.tests USE: math : call(-redefine-test ( a -- c ) 1 + ;" eval( -- ) ] unit-test
144
145 [ 1 3 test-quotatation inline-cache-invalidation-test ] [ T{ wrong-values f [ call(-redefine-test ] ( a b -- c ) } = ] must-fail-with
146
147 ! See if redefining a tuple class bumps effect counter
148 TUPLE: my-tuple a b c ;
149
150 : my-quot ( -- quot ) [ my-tuple boa ] ;
151
152 : my-word ( a b c q -- result ) call( a b c -- result ) ;
153
154 { T{ my-tuple f 1 2 3 } } [ 1 2 3 my-quot my-word ] unit-test
155
156 { } [ "IN: compiler.tree.propagation.call-effect.tests TUPLE: my-tuple a b ;" eval( -- ) ] unit-test
157
158 [ 1 2 3 my-quot my-word ] [ wrong-values? ] must-fail-with