1 USING: math.intervals kernel sequences words math math.order
2 arrays prettyprint tools.test random vocabs combinators
3 accessors math.constants ;
4 IN: math.intervals.tests
6 [ empty-interval ] [ 2 2 (a,b) ] unit-test
8 [ empty-interval ] [ 2 2 [a,b) ] unit-test
10 [ empty-interval ] [ 2 2 (a,b] ] unit-test
12 [ empty-interval ] [ 3 2 [a,b] ] unit-test
14 [ T{ interval f { 1 t } { 2 t } } ] [ 1 2 [a,b] ] unit-test
16 [ T{ interval f { 1 t } { 2 f } } ] [ 1 2 [a,b) ] unit-test
18 [ T{ interval f { 1 f } { 2 f } } ] [ 1 2 (a,b) ] unit-test
20 [ T{ interval f { 1 f } { 2 t } } ] [ 1 2 (a,b] ] unit-test
22 [ T{ interval f { 1 t } { 1 t } } ] [ 1 [a,a] ] unit-test
24 [ t ] [ { 3 t } { 3 f } endpoint< ] unit-test
25 [ t ] [ { 2 f } { 3 f } endpoint< ] unit-test
26 [ f ] [ { 3 f } { 3 t } endpoint< ] unit-test
27 [ t ] [ { 4 f } { 3 t } endpoint> ] unit-test
28 [ f ] [ { 3 f } { 3 t } endpoint> ] unit-test
30 [ empty-interval ] [ 1 2 [a,b] empty-interval interval+ ] unit-test
32 [ empty-interval ] [ empty-interval 1 2 [a,b] interval+ ] unit-test
35 1 2 [a,b] -3 3 [a,b] interval+ -2 5 [a,b] =
39 1 2 [a,b] -3 3 (a,b) interval+ -2 5 (a,b) =
42 [ empty-interval ] [ 1 2 [a,b] empty-interval interval- ] unit-test
44 [ empty-interval ] [ empty-interval 1 2 [a,b] interval- ] unit-test
47 1 2 [a,b] 0 1 [a,b] interval- 0 2 [a,b] =
50 [ empty-interval ] [ 1 2 [a,b] empty-interval interval* ] unit-test
52 [ empty-interval ] [ empty-interval 1 2 [a,b] interval* ] unit-test
55 1 2 [a,b] 0 4 [a,b] interval* 0 8 [a,b] =
59 1 2 [a,b] -4 4 [a,b] interval* -8 8 [a,b] =
63 1 2 [a,b] -0.5 0.5 [a,b] interval* -1.0 1.0 [a,b] =
67 1 2 [a,b] -0.5 0.5 (a,b] interval* -1.0 1.0 (a,b] =
71 -1 1 [a,b] -1 1 (a,b] interval* -1 1 [a,b] =
74 [ t ] [ 1 2 [a,b] dup empty-interval interval-union = ] unit-test
76 [ t ] [ empty-interval 1 2 [a,b] tuck interval-union = ] unit-test
79 0 1 (a,b) 0 1 [a,b] interval-union 0 1 [a,b] =
83 0 1 (a,b) 1 2 [a,b] interval-union 0 2 (a,b] =
87 0 1 (a,b) 0 1 [a,b] interval-intersect 0 1 (a,b) =
90 [ empty-interval ] [ 0 5 [a,b] -1 [a,a] interval-intersect ] unit-test
92 [ empty-interval ] [ 0 5 (a,b] 0 [a,a] interval-intersect ] unit-test
94 [ empty-interval ] [ empty-interval -1 [a,a] interval-intersect ] unit-test
96 [ empty-interval ] [ 0 5 (a,b] empty-interval interval-intersect ] unit-test
99 0 1 (a,b) full-interval interval-intersect 0 1 (a,b) =
103 empty-interval empty-interval interval-subset?
107 empty-interval 0 1 [a,b] interval-subset?
111 0 1 (a,b) 0 1 [a,b] interval-subset?
115 0 0 1 (a,b) interval-contains?
119 0.5 0 1 (a,b) interval-contains?
123 1 0 1 (a,b) interval-contains?
126 [ empty-interval ] [ -1 1 (a,b) empty-interval interval/ ] unit-test
128 [ t ] [ 0 0 331 [a,b) -1775 -953 (a,b) interval/ interval-contains? ] unit-test
130 [ t ] [ -1 1 (a,b) -1 1 (a,b) interval/ [-inf,inf] = ] unit-test
132 [ t ] [ -1 1 (a,b) 0 1 (a,b) interval/ [-inf,inf] = ] unit-test
134 "math.ratios.private" vocab [
136 -1 1 (a,b) 0.5 1 (a,b) interval/ -2.0 2.0 (a,b) =
140 [ f ] [ empty-interval interval-singleton? ] unit-test
142 [ t ] [ 1 [a,a] interval-singleton? ] unit-test
144 [ f ] [ 1 1 [a,b) interval-singleton? ] unit-test
146 [ f ] [ 1 3 [a,b) interval-singleton? ] unit-test
148 [ f ] [ 1 1 (a,b) interval-singleton? ] unit-test
150 [ 2 ] [ 1 3 [a,b) interval-length ] unit-test
152 [ 0 ] [ empty-interval interval-length ] unit-test
154 [ t ] [ 0 5 [a,b] 5 [a,a] interval<= ] unit-test
156 [ incomparable ] [ empty-interval 5 [a,a] interval< ] unit-test
158 [ incomparable ] [ 5 [a,a] empty-interval interval< ] unit-test
160 [ incomparable ] [ 0 5 [a,b] 5 [a,a] interval< ] unit-test
162 [ t ] [ 0 5 [a,b) 5 [a,a] interval< ] unit-test
164 [ f ] [ 0 5 [a,b] -1 [a,a] interval< ] unit-test
166 [ incomparable ] [ 0 5 [a,b] 1 [a,a] interval< ] unit-test
168 [ t ] [ -1 1 (a,b) -1 [a,a] interval> ] unit-test
170 [ t ] [ -1 1 (a,b) -1 [a,a] interval>= ] unit-test
172 [ f ] [ -1 1 (a,b) -1 [a,a] interval< ] unit-test
174 [ f ] [ -1 1 (a,b) -1 [a,a] interval<= ] unit-test
176 [ t ] [ -1 1 (a,b] 1 [a,a] interval<= ] unit-test
178 [ t ] [ -1 1 (a,b] 1 2 [a,b] interval<= ] unit-test
180 [ incomparable ] [ -1 1 (a,b] empty-interval interval>= ] unit-test
182 [ incomparable ] [ empty-interval -1 1 (a,b] interval>= ] unit-test
184 [ incomparable ] [ -1 1 (a,b] 1 2 [a,b] interval>= ] unit-test
186 [ incomparable ] [ -1 1 (a,b] 1 2 [a,b] interval> ] unit-test
188 [ t ] [ -1 1 (a,b] 1 2 (a,b] interval<= ] unit-test
190 [ f ] [ 0 10 [a,b] 0 [a,a] interval< ] unit-test
192 [ f ] [ 0 10 [a,b] 10 [a,a] interval> ] unit-test
194 [ incomparable ] [ 0 [a,a] 0 10 [a,b] interval< ] unit-test
196 [ incomparable ] [ 10 [a,a] 0 10 [a,b] interval> ] unit-test
198 [ t ] [ 0 [a,a] 0 10 [a,b] interval<= ] unit-test
200 [ incomparable ] [ 0 [a,a] 0 10 [a,b] interval>= ] unit-test
202 [ t ] [ 0 10 [a,b] 0 [a,a] interval>= ] unit-test
212 [ t ] [ 1 100 [a,b] -1 1 [a,b] interval/i [-inf,inf] = ] unit-test
214 ! Interval random tester
215 : random-element ( interval -- n )
216 dup full-interval eq? [
217 drop 32 random-bits 31 2^ -
219 dup to>> first over from>> first tuck - random +
220 2dup swap interval-contains? [
227 : random-interval ( -- interval )
228 10 random 0 = [ full-interval ] [
229 2000 random 1000 - dup 2 1000 random + +
230 1 random zero? [ [ neg ] bi@ swap ] when
239 : random-unary-op ( -- pair )
241 { bitnot interval-bitnot }
248 "math.ratios.private" vocab [
249 { recip interval-recip } suffix
253 : unary-test ( -- ? )
254 random-interval random-unary-op ! 2dup . .
255 0 pick interval-contains? over first \ recip eq? and [
258 [ [ random-element ] dip first execute ] 2keep
259 second execute interval-contains?
262 [ t ] [ 80000 [ drop unary-test ] all? ] unit-test
264 : random-binary-op ( -- pair )
272 { bitand interval-bitand }
273 { bitor interval-bitor }
274 { bitxor interval-bitxor }
275 ! { shift interval-shift }
279 "math.ratios.private" vocab [
280 { / interval/ } suffix
284 : binary-test ( -- ? )
285 random-interval random-interval random-binary-op ! 3dup . . .
286 0 pick interval-contains? over first { / /i mod rem } member? and [
289 [ [ [ random-element ] bi@ ] dip first execute ] 3keep
290 second execute interval-contains?
293 [ t ] [ 80000 [ drop binary-test ] all? ] unit-test
295 : random-comparison ( -- pair )
303 : comparison-test ( -- ? )
304 random-interval random-interval random-comparison
305 [ [ [ random-element ] bi@ ] dip first execute ] 3keep
306 second execute dup incomparable eq? [ 2drop t ] [ = ] if ;
308 [ t ] [ 40000 [ drop comparison-test ] all? ] unit-test
310 [ t ] [ -10 10 [a,b] 0 100 [a,b] assume> 0 10 (a,b] = ] unit-test
312 [ t ] [ -10 10 [a,b] 0 100 [a,b] assume>= 0 10 [a,b] = ] unit-test
314 [ t ] [ -10 10 [a,b] 0 100 [a,b] assume< -10 10 [a,b] = ] unit-test
316 [ t ] [ -10 10 [a,b] -100 0 [a,b] assume< -10 0 [a,b) = ] unit-test
318 [ t ] [ -10 10 [a,b] -100 0 [a,b] assume<= -10 0 [a,b] = ] unit-test
320 [ t ] [ -10 10 [a,b] 0 100 [a,b] assume<= -10 10 [a,b] = ] unit-test
322 [ t ] [ -10 10 [a,b] interval-abs 0 10 [a,b] = ] unit-test
324 ! Test that commutative interval ops really are
325 : random-interval-or-empty ( -- )
326 10 random 0 = [ empty-interval ] [ random-interval ] if ;
328 : random-commutative-op ( -- op )
331 interval-bitor interval-bitand interval-bitxor
332 interval-max interval-min
338 random-interval-or-empty random-interval-or-empty
339 random-commutative-op
340 [ execute ] [ swapd execute ] 3bi =