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