]> gitweb.factorcode.org Git - factor.git/blob - extra/decimals/decimals-tests.factor
functors: inline the parts of interpolate this needs
[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
4 literals locals math math.functions math.order prettyprint
5 random tools.test ;
6 IN: decimals.tests
7
8 { t } [
9     DECIMAL: 12.34 DECIMAL: 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-integers? ] unit-test
30 { t } [ 1000 [ drop [ D- ] [ - ] test-decimal-op ] all-integers? ] unit-test
31 { t } [ 1000 [ drop [ D* ] [ * ] test-decimal-op ] all-integers? ] unit-test
32 { t } [
33     1000 [
34         drop
35         [ [ 100 D/ ] [ /f ] test-decimal-op ]
36         [ ${ KERNEL-ERROR ERROR-DIVIDE-BY-ZERO f f } = ] recover
37     ] all-integers?
38 ] unit-test
39
40 { t } [
41     { DECIMAL: 0. DECIMAL: .0 DECIMAL: 0.0 DECIMAL: 00.00 DECIMAL: . } all-equal?
42 ] unit-test
43
44 { t } [ T{ decimal f 90 0 } T{ decimal f 9 1 } = ] unit-test
45
46 { t } [ DECIMAL: 1 DECIMAL: 2 before? ] unit-test
47 { f } [ DECIMAL: 2 DECIMAL: 2 before? ] unit-test
48 { f } [ DECIMAL: 3 DECIMAL: 2 before? ] unit-test
49 { f } [ DECIMAL: -1 DECIMAL: -2 before? ] unit-test
50 { f } [ DECIMAL: -2 DECIMAL: -2 before? ] unit-test
51 { t } [ DECIMAL: -3 DECIMAL: -2 before? ] unit-test
52 { t } [ DECIMAL: .5 DECIMAL: 0 DECIMAL: 1.0 between? ] unit-test
53
54 { "DECIMAL: 0" } [ DECIMAL: 0 unparse ] unit-test
55 { "DECIMAL: 0.1" } [ DECIMAL: 0.1 unparse ] unit-test
56 { "DECIMAL: 1" } [ DECIMAL: 1.0 unparse ] unit-test
57 { "DECIMAL: 1.01" } [ DECIMAL: 1.01 unparse ] unit-test
58 { "DECIMAL: -0.1" } [ DECIMAL: -0.1 unparse ] unit-test
59 { "DECIMAL: -1" } [ DECIMAL: -1.0 unparse ] unit-test
60 { "DECIMAL: -1.01" } [ DECIMAL: -1.01 unparse ] unit-test