]> gitweb.factorcode.org Git - factor.git/blob - core/kernel/kernel-tests.factor
7c51468c45ec582266fd0c28963e931b691fcd61
[factor.git] / core / kernel / kernel-tests.factor
1 USING: accessors alien alien.accessors arrays assocs byte-arrays
2 continuations debugger grouping io.streams.string kernel
3 kernel.private literals locals.backend math memory namespaces
4 prettyprint sequences sequences.private tools.test vocabs.loader
5 words ;
6 IN: kernel.tests
7
8 { 0 } [ f size ] unit-test
9 { t } [ [ \ = \ = ] all-equal? ] unit-test
10
11 {
12     {
13         { 1 2 0 }
14         { 1 2 1 }
15         { 1 2 2 }
16         { 1 2 3 }
17         { 1 2 4 }
18         { 1 2 5 }
19         { 1 2 6 }
20         { 1 2 7 }
21         { 1 2 8 }
22         { 1 2 9 }
23     }
24 } [ 1 2 10 <iota> [ 3array ] 2with map ] unit-test
25
26
27 ! Don't leak extra roots if error is thrown
28 { } [ 1000 [ [ 3 throw ] ignore-errors ] times ] unit-test
29
30 [ -1 f <array> ] must-fail
31 { } [ 10 [ [ -1 f <array> ] ignore-errors ] times ] unit-test
32 ! { } [ 1000 [ [ -1 f <array> ] ignore-errors ] times ] unit-test ! Travis CI fails
33
34 ! Make sure we report the correct error on stack underflow
35 [ clear drop ] [
36     2 head ${ KERNEL-ERROR ERROR-DATASTACK-UNDERFLOW } =
37 ] must-fail-with
38
39 { } [ :c ] unit-test
40
41 [
42     3 [ { } set-retainstack ] dip ]
43     [ 2 head ${ KERNEL-ERROR ERROR-RETAINSTACK-UNDERFLOW } =
44 ] must-fail-with
45
46 { } [ :c ] unit-test
47
48 : overflow-d ( -- ) 3 overflow-d ;
49
50 : (overflow-d-alt) ( -- n ) 3 ;
51
52 : overflow-d-alt ( -- ) (overflow-d-alt) overflow-d-alt ;
53
54 : overflow-r ( -- ) 3 load-local overflow-r ;
55
56 <<
57 { overflow-d (overflow-d-alt) overflow-d-alt overflow-r }
58 [ t "no-compile" set-word-prop ] each
59 >>
60
61 [ overflow-d ] [
62     2 head ${ KERNEL-ERROR ERROR-DATASTACK-OVERFLOW } =
63 ] must-fail-with
64
65 { } [ :c ] unit-test
66
67 [ overflow-d-alt ] [
68     2 head ${ KERNEL-ERROR ERROR-DATASTACK-OVERFLOW } =
69 ] must-fail-with
70
71 { } [ [ :c ] with-string-writer drop ] unit-test
72
73 [ overflow-r ] [
74     2 head ${ KERNEL-ERROR ERROR-RETAINSTACK-OVERFLOW } =
75 ] must-fail-with
76
77 { } [ :c ] unit-test
78
79 [ -7 <byte-array> ] must-fail
80
81 { 3 } [ t 3 and ] unit-test
82 { f } [ f 3 and ] unit-test
83 { f } [ 3 f and ] unit-test
84 { 4 } [ 4 6 or ] unit-test
85 { 6 } [ f 6 or ] unit-test
86 { f } [ 1 2 xor ] unit-test
87 { 1 } [ 1 f xor ] unit-test
88 { 2 } [ f 2 xor ] unit-test
89 { f } [ f f xor ] unit-test
90
91 [ dip ] must-fail
92 { } [ :c ] unit-test
93
94 [ 1 [ call ] dip ] must-fail
95 { } [ :c ] unit-test
96
97 [ 1 2 [ call ] dip ] must-fail
98 { } [ :c ] unit-test
99
100 { 5 } [ 1 [ 2 2 + ] dip + ] unit-test
101
102 [ [ ] keep ] must-fail
103
104 { 6 } [ 2 [ sq ] keep + ] unit-test
105
106 [ [ ] 2keep ] must-fail
107 [ 1 [ ] 2keep ] must-fail
108 { 3 1 2 } [ 1 2 [ 2drop 3 ] 2keep ] unit-test
109
110 { 0 } [ f [ sq ] [ 0 ] if* ] unit-test
111 { 4 } [ 2 [ sq ] [ 0 ] if* ] unit-test
112
113 { 0 } [ f [ 0 ] unless* ] unit-test
114 { t } [ t [ "Hello" ] unless* ] unit-test
115
116 { "2\n" } [ [ 1 2 [ . ] [ sq . ] ?if ] with-string-writer ] unit-test
117 { "9\n" } [ [ 3 f [ . ] [ sq . ] ?if ] with-string-writer ] unit-test
118
119 { f } [ f (clone) ] unit-test
120 { -123 } [ -123 (clone) ] unit-test
121
122 { 6 2 } [ 1 2 [ 5 + ] dip ] unit-test
123
124 { } [ get-callstack set-callstack ] unit-test
125
126 [ 3drop get-datastack ] must-fail
127 { } [ :c ] unit-test
128
129 ! Doesn't compile; important
130 : foo ( a -- b ) ;
131
132 << \ foo t "no-compile" set-word-prop >>
133
134 [ drop foo ] must-fail
135 { } [ :c ] unit-test
136
137 ! Regression
138 : (loop) ( a b c d -- )
139     pickd swap pickd swap
140     < [ [ 1 + ] 3dip (loop) ] [ 4drop ] if ; inline recursive
141
142 : loop ( obj -- )
143     H{ } values swap [ dup length swap ] dip [ 0 ] 3dip (loop) ;
144
145 [ loop ] must-fail
146
147 { 1 1 2 2 3 3 } [ 1 2 3 [ dup ] tri@ ] unit-test
148 { 1 4 9 } [ 1 2 3 [ sq ] tri@ ] unit-test
149 [ [ sq ] tri@ ] must-infer
150
151 { 4 } [ 1 { [ 1 ] [ 2 ] } dispatch sq ] unit-test
152
153 ! Test traceback accuracy
154 : last-frame ( -- pair )
155     6 9 error-continuation get call>> callstack>array subseq ;
156
157 {
158     { [ 1 2 [ 3 throw ] call 4 ] [ 1 2 [ 3 throw ] call 4 ] 3 }
159 } [
160     [ [ 1 2 [ 3 throw ] call 4 ] call ] ignore-errors
161     last-frame
162 ] unit-test
163
164 {
165     { [ 1 2 [ 3 throw ] dip 4 ] [ 1 2 [ 3 throw ] dip 4 ] 3 }
166 } [
167     [ [ 1 2 [ 3 throw ] dip 4 ] call ] ignore-errors
168     last-frame
169 ] unit-test
170
171 {
172     { [ 1 2 3 throw [ ] call 4 ] [ 1 2 3 throw [ ] call 4 ] 3 }
173 } [
174     [ [ 1 2 3 throw [ ] call 4 ] call ] ignore-errors
175     last-frame
176 ] unit-test
177
178 {
179     { [ 1 2 3 throw [ ] dip 4 ] [ 1 2 3 throw [ ] dip 4 ] 3 }
180 } [
181     [ [ 1 2 3 throw [ ] dip 4 ] call ] ignore-errors
182     last-frame
183 ] unit-test
184
185 {
186     { [ 1 2 3 throw [ ] [ ] if 4 ] [ 1 2 3 throw [ ] [ ] if 4 ] 3 }
187 } [
188     [ [ 1 2 3 throw [ ] [ ] if 4 ] call ] ignore-errors
189     last-frame
190 ] unit-test
191
192 { 10 2 3 4 5 } [ 1 2 3 4 5 [ 10 * ] 4dip ] unit-test
193
194 { 3 -1 5/6 } [ 1 2 3 4 5 6 [ + ] [ - ] [ / ] 2tri* ] unit-test
195
196 { { 1 2 } { 3 4 } { 5 6 } } [ 1 2 3 4 5 6 [ 2array ] 2tri@ ] unit-test
197
198 { t } [ { } identity-hashcode fixnum? ] unit-test
199 { 123 } [ 123 identity-hashcode ] unit-test
200 { t } [ f identity-hashcode fixnum? ] unit-test
201
202 ! Make sure memory protection faults work
203 [ f 0 alien-unsigned-1 ] [ vm-error? ] must-fail-with
204 [ 1 <alien> 0 alien-unsigned-1 ] [ vm-error? ] must-fail-with
205
206 { 1 2 3 1 2 3 } [ 1 2 3 3dup ] unit-test
207 { 1 2 3 4 1 2 3 4 } [ 1 2 3 4 4dup ] unit-test
208
209 { 2 3 4 1 } [ 1 2 3 4 roll ] unit-test
210 { 1 2 3 4 } [ 2 3 4 1 -roll ] unit-test
211
212 { } [ "kernel" reload ] long-unit-test