]> gitweb.factorcode.org Git - factor.git/blob - extra/decimals/decimals-tests.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / extra / decimals / decimals-tests.factor
1 ! Copyright (C) 2009 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: continuations decimals grouping kernel locals math
4 math.functions math.order math.ratios prettyprint random
5 sequences tools.test ;
6 IN: decimals.tests
7
8 [ t ] [
9     D: 12.34 D: 00012.34000 =
10 ] unit-test
11
12 : random-test-int ( -- n )
13     10 random 2 random 0 = [ neg ] when ;
14
15 : random-test-decimal ( -- decimal )
16     random-test-int random-test-int <decimal> ;
17
18 ERROR: decimal-test-failure D1 D2 quot ;
19
20 :: (test-decimal-op) ( D1 D2 quot1 quot2 -- ? )
21     D1 D2
22     quot1 [ decimal>ratio >float ] compose
23     [ [ decimal>ratio ] bi@ quot2 call( obj obj -- obj ) >float ] 2bi -.1 ~
24     [ t ] [ D1 D2 quot1 decimal-test-failure ] if ; inline
25
26 : test-decimal-op ( quot1 quot2 -- ? )
27     [ random-test-decimal random-test-decimal ] 2dip (test-decimal-op) ; inline
28
29 [ t ] [ 1000 [ drop [ D+ ] [ + ] test-decimal-op ] all? ] unit-test
30 [ t ] [ 1000 [ drop [ D- ] [ - ] test-decimal-op ] all? ] unit-test
31 [ t ] [ 1000 [ drop [ D* ] [ * ] test-decimal-op ] all? ] unit-test
32 [ t ] [
33     1000 [
34         drop
35         [ [ 100 D/ ] [ /f ] test-decimal-op ]
36         [ { "kernel-error" 4 f f } = ] recover
37     ] all?
38 ] unit-test
39
40 [ t ] [ 
41     { D: 0. D: .0 D: 0.0 D: 00.00 D: . } all-equal?
42 ] unit-test
43
44 [ t ] [ T{ decimal f 90 0 } T{ decimal f 9 1 } = ] unit-test
45
46 [ t ] [ D: 1 D: 2 before? ] unit-test
47 [ f ] [ D: 2 D: 2 before? ] unit-test
48 [ f ] [ D: 3 D: 2 before? ] unit-test
49 [ f ] [ D: -1 D: -2 before? ] unit-test
50 [ f ] [ D: -2 D: -2 before? ] unit-test
51 [ t ] [ D: -3 D: -2 before? ] unit-test
52 [ t ] [ D: .5 D: 0 D: 1.0 between? ] unit-test