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