aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/math/number/rev.lux
blob: 724d0112a266cafba11e846fbd7b352e6c6c8884 (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
(.require
 [library
  [lux (.except)
   [abstract
    [monad (.only do)]
    [\\specification
     ["$[0]" equivalence]
     ["$[0]" hash]
     ["$[0]" order]
     ["$[0]" enum]
     ["$[0]" interval]
     ["$[0]" monoid]
     ["$[0]" codec]]]
   [data
    ["[0]" bit (.use "[1]#[0]" equivalence)]]
   [math
    ["[0]" random]
    ["[0]" arithmetic
     ["[1]S" \\specification]]]
   [test
    ["_" property (.only Test)]]]]
 [\\library
  ["[0]" / (.only)
   [// (.only hex)
    ["n" nat]
    ["f" frac]
    ["[0]" i64 (.use "[1]#[0]" hash)]]]])

(def signature
  Test
  (`` (all _.and
           (_.for [/.equivalence /.=]
                  ($equivalence.spec /.equivalence random.rev))
           (_.for [/.hash]
                  ($hash.spec /.hash random.rev))
           (_.for [/.order /.<]
                  ($order.spec /.order random.rev))
           (_.for [/.enum]
                  ($enum.spec /.enum random.rev))
           (_.for [/.interval]
                  ($interval.spec /.interval random.rev))
           (,, (with_template [<composite> <monoid>]
                 [(_.for [<monoid> <composite>]
                         ($monoid.spec /.equivalence <monoid> random.rev))]

                 [/.+ /.addition]

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

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

(def .public test
  Test
  (<| (_.covering /._)
      (_.for [.Rev])
      (`` (all _.and
               (,, (with_template [<half> <whole>]
                     [(_.coverage [<half>]
                        (/.= <whole>
                             (/.+ <half> <half>)))]

                     [/./1 (-- /./1)]
                     [/./2 .0]
                     [/./4 /./2]
                     [/./8 /./4]
                     [/./16 /./8]
                     [/./32 /./16]
                     [/./64 /./32]
                     [/./128 /./64]
                     [/./256 /./128]
                     [/./512 /./256]
                     [/./1024 /./512]
                     [/./2048 /./1024]
                     [/./4096 /./2048]
                     ))
               (do random.monad
                 [sample random.rev]
                 (_.coverage [/.-]
                   (and (/.= .0 (/.- sample sample))
                        (/.= sample (/.- .0 sample)))))
               (do [! random.monad]
                 [left random.rev
                  right random.rev]
                 (_.coverage [/.*]
                   (and (/.< left (/.* left right))
                        (/.< right (/.* left right)))))
               (do [! random.monad]
                 [.let [dividend (at ! each (i64.and (hex "FFFF"))
                                     random.rev)
                        divisor (at ! each (|>> (i64.and (hex "F"))
                                                (i64.or (hex "1"))
                                                (i64.right_rotated 8)
                                                .rev)
                                    random.nat)]
                  dividend (random.only (/.> .0) dividend)
                  divisor/0 divisor
                  divisor/1 (random.only (|>> (/.= divisor/0) not)
                                         divisor)
                  scale (at ! each (|>> (n.% 10) ++)
                            random.nat)]
                 (all _.and
                      (_.coverage [/./]
                        (bit#= (/.< divisor/0 divisor/1)
                               (/.> (/./ divisor/0 dividend) (/./ divisor/1 dividend))))
                      (_.coverage [/.%]
                        (at i64.equivalence =
                            (.i64 (n.% (.nat divisor/0) (.nat dividend)))
                            (.i64 (/.% divisor/0 dividend))))
                      (_.coverage [/.up /.down]
                        (let [symmetry!
                              (|> dividend
                                  (/.up scale)
                                  (/.down scale)
                                  (/.= dividend))

                              discrete_division!
                              (/.= (/.% (.rev scale) dividend)
                                   (/.- (|> dividend
                                            (/.down scale)
                                            (/.up scale))
                                        dividend))]
                          (and symmetry!
                               discrete_division!)))
                      (_.coverage [/.ratio]
                        (|> dividend
                            (/.up scale)
                            (/.ratio dividend)
                            (n.= scale)))
                      ))
               (do [! random.monad]
                 [dividend random.rev
                  divisor (random.only (|>> (/.= .0) not)
                                       random.rev)]
                 (_.coverage [/./%]
                   (let [[quotient remainder] (/./% divisor dividend)]
                     (and (/.= (/./ divisor dividend) quotient)
                          (/.= (/.% divisor dividend) remainder)))))
               (do random.monad
                 [left random.rev
                  right random.rev]
                 (all _.and
                      (_.coverage [/.>]
                        (bit#= (/.> left right)
                               (/.< right left)))
                      (_.coverage [/.<= /.>=]
                        (bit#= (/.<= left right)
                               (/.>= right left)))
                      ))
               (do random.monad
                 [sample random.nat]
                 (_.coverage [/.reciprocal]
                   (/.= (/.reciprocal sample)
                        (|> sample /.reciprocal .nat /.reciprocal .nat /.reciprocal))))
               (do [! random.monad]
                 [expected (at ! each (|>> f.abs (f.% +1.0))
                               random.safe_frac)
                  sample random.rev]
                 (_.coverage [/.frac]
                   (and (|> expected f.rev /.frac (f.= expected))
                        (f.number? (/.frac sample)))))

               ..signature
               ))))