]> gitweb.factorcode.org Git - factor.git/blob - basis/math/floats/env/env-tests.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / basis / math / floats / env / env-tests.factor
1 USING: kernel math math.floats.env math.floats.env.private
2 math.functions math.libm sets tools.test ;
3 IN: math.floats.env.tests
4
5 : set-default-fp-env ( -- )
6     { } { } +round-nearest+ +denormal-keep+ set-fp-env ;
7
8 ! In case the tests screw up the FP env because of bugs in math.floats.env
9 set-default-fp-env
10
11 [ t ] [
12     [ 1.0 0.0 / drop ] collect-fp-exceptions
13     { +fp-zero-divide+ } set= 
14 ] unit-test
15
16 [ t ] [
17     [ 1.0 3.0 / drop ] collect-fp-exceptions
18     { +fp-inexact+ } set= 
19 ] unit-test
20
21 [ t ] [
22     [ 2.0 100,000.0 ^ drop ] collect-fp-exceptions
23     { +fp-inexact+ +fp-overflow+ } set= 
24 ] unit-test
25
26 [ t ] [
27     [ 2.0 -100,000.0 ^ drop ] collect-fp-exceptions
28     { +fp-inexact+ +fp-underflow+ } set= 
29 ] unit-test
30
31 [ t ] [
32     [ -1.0 fsqrt drop ] collect-fp-exceptions
33     { +fp-invalid-operation+ } set= 
34 ] unit-test
35
36 [
37     HEX: 3fd5,5555,5555,5555
38     HEX: 3fc9,9999,9999,999a
39     HEX: bfc9,9999,9999,999a
40     HEX: bfd5,5555,5555,5555
41 ] [
42     +round-nearest+ [
43          1.0 3.0 /f double>bits
44          1.0 5.0 /f double>bits
45         -1.0 5.0 /f double>bits
46         -1.0 3.0 /f double>bits
47     ] with-rounding-mode
48 ] unit-test
49
50 [
51     HEX: 3fd5,5555,5555,5555
52     HEX: 3fc9,9999,9999,9999
53     HEX: bfc9,9999,9999,999a
54     HEX: bfd5,5555,5555,5556
55 ] [
56     +round-down+ [
57          1.0 3.0 /f double>bits
58          1.0 5.0 /f double>bits
59         -1.0 5.0 /f double>bits
60         -1.0 3.0 /f double>bits
61     ] with-rounding-mode
62 ] unit-test
63
64 [
65     HEX: 3fd5,5555,5555,5556
66     HEX: 3fc9,9999,9999,999a
67     HEX: bfc9,9999,9999,9999
68     HEX: bfd5,5555,5555,5555
69 ] [
70     +round-up+ [
71          1.0 3.0 /f double>bits
72          1.0 5.0 /f double>bits
73         -1.0 5.0 /f double>bits
74         -1.0 3.0 /f double>bits
75     ] with-rounding-mode
76 ] unit-test
77
78 [
79     HEX: 3fd5,5555,5555,5555
80     HEX: 3fc9,9999,9999,9999
81     HEX: bfc9,9999,9999,9999
82     HEX: bfd5,5555,5555,5555
83 ] [
84     +round-zero+ [
85          1.0 3.0 /f double>bits
86          1.0 5.0 /f double>bits
87         -1.0 5.0 /f double>bits
88         -1.0 3.0 /f double>bits
89     ] with-rounding-mode
90 ] unit-test
91
92 ! ensure rounding mode is restored to +round-nearest+
93 [
94     HEX: 3fd5,5555,5555,5555
95     HEX: 3fc9,9999,9999,999a
96     HEX: bfc9,9999,9999,999a
97     HEX: bfd5,5555,5555,5555
98 ] [
99      1.0 3.0 /f double>bits
100      1.0 5.0 /f double>bits
101     -1.0 5.0 /f double>bits
102     -1.0 3.0 /f double>bits
103 ] unit-test
104
105 [ { +fp-zero-divide+ }       [ 1.0 0.0 /f ] with-fp-traps ] must-fail
106 [ { +fp-inexact+ }           [ 1.0 3.0 /f ] with-fp-traps ] must-fail
107 [ { +fp-invalid-operation+ } [ -1.0 fsqrt ] with-fp-traps ] must-fail
108 [ { +fp-overflow+ }          [ 2.0  100,000.0 ^ ] with-fp-traps ] must-fail
109 [ { +fp-underflow+ }         [ 2.0 -100,000.0 ^ ] with-fp-traps ] must-fail
110
111 ! Ensure traps get cleared
112 [ 1/0. ] [ 1.0 0.0 /f ] unit-test
113
114 ! Ensure state is back to normal
115 [ +round-nearest+ ] [ rounding-mode ] unit-test
116 [ +denormal-keep+ ] [ denormal-mode ] unit-test
117 [ { } ] [ fp-traps ] unit-test
118
119 ! In case the tests screw up the FP env because of bugs in math.floats.env
120 set-default-fp-env
121