]> gitweb.factorcode.org Git - factor.git/blob - extra/decimals/decimals-tests.factor
4a9baced073fb1bf0a025db5c27f49d24b5d970b
[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 kernel.private literals
4 locals math math.functions math.order random tools.test ;
5 IN: decimals.tests
6
7 { t } [
8     DECIMAL: 12.34 DECIMAL: 00012.34000 =
9 ] unit-test
10
11 : random-test-int ( -- n )
12     10 random 2 random 0 = [ neg ] when ;
13
14 : random-test-decimal ( -- decimal )
15     random-test-int random-test-int <decimal> ;
16
17 ERROR: decimal-test-failure D1 D2 quot ;
18
19 :: (test-decimal-op) ( D1 D2 quot1 quot2 -- ? )
20     D1 D2
21     quot1 [ decimal>ratio >float ] compose
22     [ [ decimal>ratio ] bi@ quot2 call( obj obj -- obj ) >float ] 2bi -.1 ~
23     [ t ] [ D1 D2 quot1 decimal-test-failure ] if ; inline
24
25 : test-decimal-op ( quot1 quot2 -- ? )
26     [ random-test-decimal random-test-decimal ] 2dip (test-decimal-op) ; inline
27
28 { t } [ 1000 [ drop [ D+ ] [ + ] test-decimal-op ] all-integers? ] unit-test
29 { t } [ 1000 [ drop [ D- ] [ - ] test-decimal-op ] all-integers? ] unit-test
30 { t } [ 1000 [ drop [ D* ] [ * ] test-decimal-op ] all-integers? ] unit-test
31 { t } [
32     1000 [
33         drop
34         [ [ 100 D/ ] [ /f ] test-decimal-op ]
35         [ ${ KERNEL-ERROR ERROR-DIVIDE-BY-ZERO f f } = ] recover
36     ] all-integers?
37 ] unit-test
38
39 { t } [
40     { DECIMAL: 0. DECIMAL: .0 DECIMAL: 0.0 DECIMAL: 00.00 DECIMAL: . } all-equal?
41 ] unit-test
42
43 { t } [ T{ decimal f 90 0 } T{ decimal f 9 1 } = ] unit-test
44
45 { t } [ DECIMAL: 1 DECIMAL: 2 before? ] unit-test
46 { f } [ DECIMAL: 2 DECIMAL: 2 before? ] unit-test
47 { f } [ DECIMAL: 3 DECIMAL: 2 before? ] unit-test
48 { f } [ DECIMAL: -1 DECIMAL: -2 before? ] unit-test
49 { f } [ DECIMAL: -2 DECIMAL: -2 before? ] unit-test
50 { t } [ DECIMAL: -3 DECIMAL: -2 before? ] unit-test
51 { t } [ DECIMAL: .5 DECIMAL: 0 DECIMAL: 1.0 between? ] unit-test