aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/math/number/frac.lux
blob: 7a7f1c6bf6d88a12282b30388b42c35db24cc8bd (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

(.require
 [library
  [lux (.except)
   ["[0]" ffi]
   [abstract
    [monad (.only do)]
    ["[0]" hash
     ["[1]T" \\test]]
    ["[0]" codec
     ["[1]T" \\test]]
    ["[0]" equivalence
     ["[1]T" \\test]]
    ["[0]" order
     ["[1]T" \\test]]
    ["[0]" monoid
     ["[1]T" \\test]]]
   [data
    ["[0]" bit (.use "[1]#[0]" equivalence)]]
   [math
    ["[0]" random (.only Random)]
    ["[0]" arithmetic
     ["[1]T" \\test]]]
   [meta
    [macro
     ["[0]" template]]
    [compiler
     ["@" target]]]
   [test
    ["_" property (.only Test)]]]]
 [\\library
  ["[0]" / (.only)
   [// (.except)
    ["n" nat]
    ["i" int]
    ["r" rev]
    ["[0]" i64]]]])

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

(def constant
  Test
  (do random.monad
    [sample random.safe_frac]
    (all _.and
         (_.coverage [/.biggest]
           (/.<= /.biggest sample))
         (_.coverage [/.positive_infinity]
           (/.< /.positive_infinity sample))
         (_.coverage [/.smallest]
           (bit#= (/.positive? sample)
                  (/.>= /.smallest sample)))
         (_.coverage [/.negative_infinity]
           (/.> /.negative_infinity sample))
         (_.coverage [/.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 (of ! each /.abs ..random)]
    (all _.and
         (_.coverage [/.negative?]
           (bit#= (/.negative? sample)
                  (/.< +0.0 sample)))
         (_.coverage [/.positive?]
           (bit#= (/.positive? sample)
                  (/.> +0.0 sample)))
         (_.coverage [/.zero?]
           (bit#= (/.zero? sample)
                  (/.= +0.0 sample)))
         (_.coverage [/.approximately?]
           (and (/.approximately? /.smallest sample sample)
                (/.approximately? (/.+ +1.0 shift) sample (/.+ shift sample))))
         (_.coverage [/.number?]
           (and (not (/.number? /.not_a_number))
                (not (/.number? /.positive_infinity))
                (not (/.number? /.negative_infinity))
                (/.number? sample)))
         )))

(def conversion
  Test
  (all _.and
       (do [! random.monad]
         [expected (of ! each (n.% 1,000,000) random.nat)]
         (_.coverage [/.nat]
           (|> expected n.frac /.nat (n.= expected))))
       (do [! random.monad]
         [expected (of ! each (i.% +1,000,000) random.int)]
         (_.coverage [/.int]
           (|> expected i.frac /.int (i.= expected))))
       (do [! random.monad]
         [expected (of ! each (|>> (i64.left_shifted 52) .rev)
                       random.nat)]
         (_.coverage [/.rev]
           (|> expected r.frac /.rev (r.= expected))))
       ))

(def signature
  Test
  (`` (all _.and
           (_.for [/.equivalence /.=]
                  (equivalenceT.spec /.equivalence random.safe_frac))
           (_.for [/.hash]
                  (hashT.spec /.hash random.frac))
           (_.for [/.order /.<]
                  (orderT.spec /.order random.safe_frac))
           (,, (with_template [<composite> <monoid>]
                 [(_.for [<monoid> <composite>]
                         (monoidT.spec /.equivalence <monoid> ..random))]

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

                 [/.min /.minimum]
                 [/.max /.maximum]
                 ))
           (,, (with_template [<codec>]
                 [(_.for [<codec>]
                         (codecT.spec /.equivalence <codec> random.safe_frac))]

                 [/.binary] [/.octal] [/.decimal] [/.hex]
                 ))
           (_.for [/.arithmetic]
                  (arithmeticT.spec /.equivalence /.arithmetic random.safe_frac))
           )))

(with_expansions [<jvm> (these (ffi.import java/lang/Double
                                 "[1]::[0]"
                                 ("static" doubleToRawLongBits [double] long)
                                 ("static" longBitsToDouble [long] double)))]
  (for @.old (these <jvm>)
       @.jvm (these <jvm>)
       (these)))

(def margin_of_error
  +0.0000001)

(def (trigonometric_symmetry forward backward angle)
  (-> (-> Frac Frac) (-> Frac Frac) Frac Bit)
  (let [normal (|> angle forward backward)]
    (|> normal forward backward (/.approximately? ..margin_of_error normal))))

(def test|math
  Test
  (all _.and
       (do [! random.monad]
         [.let [~= (/.approximately? ..margin_of_error)]
          angle (|> random.safe_frac (of ! each (/.* /.tau)))]
         (all _.and
              (_.coverage [/.sin /.asin]
                (trigonometric_symmetry /.sin /.asin angle))
              (_.coverage [/.cos /.acos]
                (trigonometric_symmetry /.cos /.acos angle))
              (_.coverage [/.tan /.atan]
                (trigonometric_symmetry /.tan /.atan angle))
              (_.coverage [/.tau]
                (and (and (~= +0.0 (/.sin /.tau))
                          (~= +1.0 (/.cos /.tau)))
                     (and (~= +0.0 (/.sin (/./ +2.0 /.tau)))
                          (~= -1.0 (/.cos (/./ +2.0 /.tau))))
                     (and (~= +1.0 (/.sin (/./ +4.0 /.tau)))
                          (~= +0.0 (/.cos (/./ +4.0 /.tau))))
                     (and (~= -1.0 (/.sin (/.* +3.0 (/./ +4.0 /.tau))))
                          (~= +0.0 (/.cos (/.* +3.0 (/./ +4.0 /.tau)))))
                     (let [x2+y2 (/.+ (/.pow +2.0 (/.sin angle))
                                      (/.pow +2.0 (/.cos angle)))]
                       (~= +1.0 x2+y2))))
              (_.coverage [/.pi]
                (~= (/./ +2.0 /.tau) /.pi))
              ))
       (do [! random.monad]
         [sample (|> random.safe_frac (of ! each (/.* +1000.0)))]
         (all _.and
              (_.coverage [/.ceil]
                (let [ceil'd (/.ceil sample)]
                  (and (|> ceil'd /.int i.frac (/.= ceil'd))
                       (/.>= sample ceil'd)
                       (/.<= +1.0 (/.- sample ceil'd)))))
              (_.coverage [/.floor]
                (let [floor'd (/.floor sample)]
                  (and (|> floor'd /.int i.frac (/.= floor'd))
                       (/.<= sample floor'd)
                       (/.<= +1.0 (/.- floor'd sample)))))
              (_.coverage [/.round]
                (let [round'd (/.round sample)]
                  (and (|> round'd /.int i.frac (/.= round'd))
                       (/.<= +1.0 (/.abs (/.- sample round'd))))))
              (_.coverage [/.root_2]
                (let [sample (/.abs sample)]
                  (|> sample
                      /.root_2
                      (/.pow +2.0)
                      (/.approximately? ..margin_of_error sample))))
              (_.coverage [/.root_3]
                (|> sample
                    /.root_3
                    (/.pow +3.0)
                    (/.approximately? ..margin_of_error sample)))
              ))
       (do [! random.monad]
         [.let [~= (/.approximately? ..margin_of_error)]
          sample (of ! each (/.* +10.0) random.safe_frac)
          power (of ! each (|>> (n.% 10) ++ n.frac) random.nat)]
         (all _.and
              (_.coverage [/.exp /.log]
                (|> sample /.exp /.log (/.approximately? +0.000000000000001 sample)))
              (_.coverage [/.e]
                (~= +1.0 (/.log /.e)))
              (_.coverage [/.pow /.log_by]
                (let [sample (/.abs sample)]
                  (|> sample
                      (/.pow power)
                      (/.log_by sample)
                      (~= power))))
              ))
       (do [! random.monad]
         [.let [~= (/.approximately? ..margin_of_error)]
          angle (of ! each (/.* /.tau) random.safe_frac)
          sample (of ! each /.abs random.safe_frac)
          big (of ! each (/.* +1,000,000,000.00) random.safe_frac)]
         (template.let [(odd! <function>)
                        [(_.coverage [<function>]
                           (~= (/.opposite (<function> angle))
                               (<function> (/.opposite angle))))]

                        (even! <function>)
                        [(_.coverage [<function>]
                           (~= (<function> angle)
                               (<function> (/.opposite angle))))]

                        (inverse! <left> <right> <input>)
                        [(_.coverage [<left> <right>]
                           (~= (<right> <input>)
                               (<left> (/./ <input> +1.0))))]]
           (all _.and
                (odd! /.sinh)
                (even! /.cosh)
                (odd! /.tanh)
                (odd! /.coth)
                (even! /.sech)
                (odd! /.csch)

                (inverse! /.acosh /.asech sample)
                (inverse! /.asinh /.acsch sample)
                (inverse! /.atanh /.acoth big)
                )))
       (do [! random.monad]
         [x (of ! each (|>> (/.* +10.0) /.abs) random.safe_frac)
          y (of ! each (|>> (/.* +10.0) /.abs) random.safe_frac)]
         (_.coverage [/.hypotenuse]
           (let [h (/.hypotenuse x y)]
             (and (/.>= x h)
                  (/.>= y h)))))
       (do [! random.monad]
         [.let [~= (/.approximately? ..margin_of_error)
                tau/4 (/./ +4.0 /.tau)]
          x (of ! each (/.* tau/4) random.safe_frac)
          y (of ! each (/.* tau/4) random.safe_frac)]
         (_.coverage [/.atan_2]
           (let [expected (/.atan_2 x y)
                 actual (if (/.> +0.0 x)
                          (/.atan (/./ x y))
                          (if (/.< +0.0 y)
                            (/.- /.pi (/.atan (/./ x y)))
                            (/.+ /.pi (/.atan (/./ x y)))))]
             (and (~= expected actual)
                  (~= tau/4 (/.atan_2 +0.0 (/.abs y)))
                  (~= (/.opposite tau/4) (/.atan_2 +0.0 (/.opposite (/.abs y))))
                  (/.not_a_number? (/.atan_2 +0.0 +0.0))))))
       (do [! random.monad]
         [of (of ! each (|>> (n.% 10) ++) random.nat)]
         (_.coverage [/.factorial]
           (and (n.= 1 (/.factorial 0))
                (|> (/.factorial of) (n.% of) (n.= 0)))))
       ))

(def .public test
  Test
  (<| (_.covering /._)
      (_.for [.Frac .F64 .Double])
      (all _.and
           (do random.monad
             [left random.safe_frac
              right random.safe_frac]
             (all _.and
                  (_.coverage [/.>]
                    (bit#= (/.> left right)
                           (/.< right left)))
                  (_.coverage [/.<= /.>=]
                    (bit#= (/.<= left right)
                           (/.>= right left)))
                  ))
           (do random.monad
             [sample random.safe_frac]
             (all _.and
                  (_.coverage [/.-]
                    (and (/.= +0.0 (/.- sample sample))
                         (/.= sample (/.- +0.0 sample))
                         (/.= (/.opposite sample)
                              (/.- sample +0.0))))
                  (_.coverage [/./]
                    (and (/.= +1.0 (/./ sample sample))
                         (/.= sample (/./ +1.0 sample))))
                  (_.coverage [/.abs]
                    (bit#= (/.> sample (/.abs sample))
                           (/.negative? sample)))
                  (_.coverage [/.signum]
                    (/.= (/.abs sample)
                         (/.* (/.signum sample) sample)))
                  ))
           (do random.monad
             [left (random.only (|>> (/.= +0.0) not)
                                ..random)
              right ..random]
             (all _.and
                  (_.coverage [/.%]
                    (let [rem (/.% left right)
                          div (|> right (/.- rem) (/./ left))]
                      (/.= right
                           (|> div (/.* left) (/.+ rem)))))
                  (_.coverage [/./%]
                    (let [[div rem] (/./% left right)]
                      (and (/.= div (/./ left right))
                           (/.= rem (/.% left right)))))
                  (_.coverage [/.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> (all _.and
                                        (let [test (is (-> Frac Bit)
                                                       (function (_ value)
                                                         (n.= (.nat (ffi.of_long (java/lang/Double::doubleToRawLongBits (ffi.as_double value))))
                                                              (/.bits value))))]
                                          (do random.monad
                                            [sample random.frac]
                                            (_.coverage [/.bits]
                                              (and (test sample)
                                                   (test /.biggest)
                                                   (test /.smallest)
                                                   (test /.not_a_number)
                                                   (test /.positive_infinity)
                                                   (test /.negative_infinity)))))
                                        (do random.monad
                                          [sample random.i64]
                                          (_.coverage [/.of_bits]
                                            (let [expected (ffi.of_double (java/lang/Double::longBitsToDouble (ffi.as_long sample)))
                                                  actual (/.of_bits sample)]
                                              (or (/.= expected actual)
                                                  (and (/.not_a_number? expected)
                                                       (/.not_a_number? actual))))))
                                        )]
             (for @.old <jvm>
                  @.jvm <jvm>
                  (let [test (is (-> 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]
                      (_.coverage [/.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]
             (_.coverage [/.opposite]
               (let [subtraction!
                     (/.= +0.0 (/.+ (/.opposite expected) expected))

                     inverse!
                     (|> expected /.opposite /.opposite (/.= expected))]
                 (and subtraction!
                      inverse!))))
           (do [! random.monad]
             [it (of ! each /.abs random.safe_frac)]
             (_.coverage [/.degree]
               (/.> it
                    (/.degree it))))
           (do [! random.monad]
             [it (of ! each /.abs random.safe_frac)]
             (_.coverage [/.percentage /.permille /.permyriad]
               (and (/.> it
                         (/.percentage it))
                    (/.> (/.percentage it)
                         (/.permille it))
                    (/.> (/.permille it)
                         (/.permyriad it)))))

           ..constant
           ..predicate
           ..conversion
           ..signature
           ..test|math
           )))