aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/math/number/frac.lux
blob: 09993f0337f9c6ef7fcb0ebb76ffbaa5c76125cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
(.module:
  [library
   [lux #*
    ["_" test (#+ Test)]
    ["@" target]
    ["." ffi]
    [abstract
     [monad (#+ do)]
     [\\specification
      ["$." equivalence]
      ["$." hash]
      ["$." order]
      ["$." monoid]
      ["$." codec]]]
    [data
     ["." bit ("#\." equivalence)]]
    [math
     ["." random (#+ Random)]]]]
  [\\library
   ["." /
    [// #*
     ["n" nat]
     ["i" int]
     ["r" rev]
     ["." i64]]]])

(def: random
  (Random Frac)
  (\ random.monad each (|>> (i.% +1,000,000) i.frac) random.int))

(def: constant
  Test
  (do random.monad
    [sample random.safe_frac]
    ($_ _.and
        (_.cover [/.biggest]
                 (/.<= /.biggest sample))
        (_.cover [/.positive_infinity]
                 (/.< /.positive_infinity sample))
        (_.cover [/.smallest]
                 (bit\= (/.positive? sample)
                        (/.>= /.smallest sample)))
        (_.cover [/.negative_infinity]
                 (/.> /.negative_infinity sample))
        (_.cover [/.not_a_number /.not_a_number?]
                 (and (/.not_a_number? /.not_a_number)
                      (not (or (/.= /.not_a_number sample)
                               (/.not_a_number? sample)))))
        )))

(def: predicate
  Test
  (do {! random.monad}
    [sample ..random
     shift (\ ! each /.abs ..random)]
    ($_ _.and
        (_.cover [/.negative?]
                 (bit\= (/.negative? sample)
                        (/.< +0.0 sample)))
        (_.cover [/.positive?]
                 (bit\= (/.positive? sample)
                        (/.> +0.0 sample)))
        (_.cover [/.zero?]
                 (bit\= (/.zero? sample)
                        (/.= +0.0 sample)))
        (_.cover [/.approximately?]
                 (and (/.approximately? /.smallest sample sample)
                      (/.approximately? (/.+ +1.0 shift) sample (/.+ shift sample))))
        (_.cover [/.number?]
                 (and (not (/.number? /.not_a_number))
                      (not (/.number? /.positive_infinity))
                      (not (/.number? /.negative_infinity))
                      (/.number? sample)))
        )))

(def: conversion
  Test
  ($_ _.and
      (do {! random.monad}
        [expected (\ ! each (n.% 1,000,000) random.nat)]
        (_.cover [/.nat]
                 (|> expected n.frac /.nat (n.= expected))))
      (do {! random.monad}
        [expected (\ ! each (i.% +1,000,000) random.int)]
        (_.cover [/.int]
                 (|> expected i.frac /.int (i.= expected))))
      (do {! random.monad}
        [expected (\ ! each (|>> (i64.left_shifted 52) .rev)
                     random.nat)]
        (_.cover [/.rev]
                 (|> expected r.frac /.rev (r.= expected))))
      ))

(def: signature
  Test
  (`` ($_ _.and
          (_.for [/.equivalence /.=]
                 ($equivalence.spec /.equivalence random.safe_frac))
          (_.for [/.hash]
                 ($hash.spec /.hash random.frac))
          (_.for [/.order /.<]
                 ($order.spec /.order random.safe_frac))
          (~~ (template [<composite> <monoid>]
                [(_.for [<monoid> <composite>]
                        ($monoid.spec /.equivalence <monoid> ..random))]

                [/.+ /.addition]
                [/.* /.multiplication]

                [/.min /.minimum]
                [/.max /.maximum]
                ))
          (~~ (template [<codec>]
                [(_.for [<codec>]
                        ($codec.spec /.equivalence <codec> random.safe_frac))]

                [/.binary] [/.octal] [/.decimal] [/.hex]
                ))
          )))

(with_expansions [<jvm> (as_is (ffi.import: java/lang/Double
                                 ["#::."
                                  ("static" doubleToRawLongBits [double] long)
                                  ("static" longBitsToDouble [long] double)]))]
  (for {@.old (as_is <jvm>)
        @.jvm (as_is <jvm>)}
       (as_is)))

(def: .public test
  Test
  (<| (_.covering /._)
      (_.for [.Frac])
      ($_ _.and
          (do random.monad
            [left random.safe_frac
             right random.safe_frac]
            ($_ _.and
                (_.cover [/.>]
                         (bit\= (/.> left right)
                                (/.< right left)))
                (_.cover [/.<= /.>=]
                         (bit\= (/.<= left right)
                                (/.>= right left)))
                ))
          (do random.monad
            [sample random.safe_frac]
            ($_ _.and
                (_.cover [/.-]
                         (and (/.= +0.0 (/.- sample sample))
                              (/.= sample (/.- +0.0 sample))
                              (/.= (/.opposite sample)
                                   (/.- sample +0.0))))
                (_.cover [/./]
                         (and (/.= +1.0 (/./ sample sample))
                              (/.= sample (/./ +1.0 sample))))
                (_.cover [/.abs]
                         (bit\= (/.> sample (/.abs sample))
                                (/.negative? sample)))
                (_.cover [/.signum]
                         (/.= (/.abs sample)
                              (/.* (/.signum sample) sample)))
                ))
          (do random.monad
            [left (random.only (|>> (/.= +0.0) not)
                               ..random)
             right ..random]
            ($_ _.and
                (_.cover [/.%]
                         (let [rem (/.% left right)
                               div (|> right (/.- rem) (/./ left))]
                           (/.= right
                                (|> div (/.* left) (/.+ rem)))))
                (_.cover [/./%]
                         (let [[div rem] (/./% left right)]
                           (and (/.= div (/./ left right))
                                (/.= rem (/.% left right)))))
                (_.cover [/.mod]
                         (or (and (/.= +0.0 (/.% left right))
                                  (/.= +0.0 (/.mod left right)))
                             (and (/.= (/.signum left)
                                       (/.signum (/.mod left right)))
                                  (/.= (/.signum right)
                                       (/.signum (/.% left right)))
                                  (if (/.= (/.signum left) (/.signum right))
                                    (/.= (/.% left right)
                                         (/.mod left right))
                                    (/.= (/.+ left (/.% left right))
                                         (/.mod left right))))))
                ))
          (with_expansions [<jvm> ($_ _.and
                                      (let [test (: (-> Frac Bit)
                                                    (function (_ value)
                                                      (n.= (.nat (java/lang/Double::doubleToRawLongBits value))
                                                           (/.bits value))))]
                                        (do random.monad
                                          [sample random.frac]
                                          (_.cover [/.bits]
                                                   (and (test sample)
                                                        (test /.biggest)
                                                        (test /.smallest)
                                                        (test /.not_a_number)
                                                        (test /.positive_infinity)
                                                        (test /.negative_infinity)))))
                                      (do random.monad
                                        [sample random.i64]
                                        (_.cover [/.of_bits]
                                                 (let [expected (java/lang/Double::longBitsToDouble sample)
                                                       actual (/.of_bits sample)]
                                                   (or (/.= expected actual)
                                                       (and (/.not_a_number? expected)
                                                            (/.not_a_number? actual))))))
                                      )]
            (for {@.old <jvm>
                  @.jvm <jvm>}
                 (let [test (: (-> Frac Bit)
                               (function (_ expected)
                                 (let [actual (|> expected /.bits /.of_bits)]
                                   (or (/.= expected actual)
                                       (and (/.not_a_number? expected)
                                            (/.not_a_number? actual))))))]
                   (do random.monad
                     [sample random.frac]
                     (_.cover [/.bits /.of_bits]
                              (and (test sample)
                                   (test /.biggest)
                                   (test /.smallest)
                                   (test /.not_a_number)
                                   (test /.positive_infinity)
                                   (test /.negative_infinity)))))))
          (do random.monad
            [expected random.safe_frac]
            (_.cover [/.opposite]
                     (let [subtraction!
                           (/.= +0.0 (/.+ (/.opposite expected) expected))

                           inverse!
                           (|> expected /.opposite /.opposite (/.= expected))]
                       (and subtraction!
                            inverse!))))

          ..constant
          ..predicate
          ..conversion
          ..signature
          )))