aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/math/number/int.lux
blob: 27f66d3a23a75d7ca4d15b402ba1ea3ad67b6a67 (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
(.require
 [library
  [lux (.except)
   ["_" test (.only Test)]
   [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 (.only Random)]]]]
 [\\library
  ["[0]" / (.only)
   [//
    ["n" nat]
    ["f" frac]
    ["[0]" i64]]]])

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

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

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

(def predicate
  Test
  (do [! random.monad]
    [sample random.int]
    (all _.and
         (_.coverage [/.negative?]
           (bit#= (/.negative? sample)
                  (/.< +0 sample)))
         (_.coverage [/.positive?]
           (bit#= (/.positive? sample)
                  (/.> +0 sample)))
         (_.coverage [/.zero?]
           (bit#= (/.zero? sample)
                  (/.= +0 sample)))
         (_.coverage [/.even? /.odd?]
           (bit#= (/.even? sample)
                  (not (/.odd? sample))))
         )))

(def .public test
  Test
  (<| (_.covering /._)
      (_.for [.Int])
      (let [(open "/#[0]") /.interval])
      (all _.and
           (do random.monad
             [sample random.int
              left random.int
              right random.int]
             (all _.and
                  (_.coverage [/.+]
                    (and (/.= (/.+ left right)
                              (/.+ right left))
                         (/.= sample (/.+ +0 sample))))
                  (_.coverage [/.-]
                    (and (/.= +0 (/.- sample sample))
                         (/.= sample (/.- +0 sample))
                         (/.= (/.opposite sample)
                              (/.- sample +0))
                         (/.= /#bottom
                              (/.- /#bottom +0))))
                  (_.coverage [/.*]
                    (and (/.= (/.* left right)
                              (/.* right left))
                         (/.= sample (/.* +1 sample))
                         (/.= /#bottom
                              (/.* -1 /#bottom))))
                  (_.coverage [/./]
                    (and (/.= +1 (/./ sample sample))
                         (/.= sample (/./ +1 sample))
                         (/.= /#bottom
                              (/./ -1 /#bottom))))
                  (_.coverage [/.abs]
                    (bit#= (/.> sample (/.abs sample))
                           (/.negative? sample)))
                  (_.coverage [/.signum]
                    (/.= (/.abs sample)
                         (/.* (/.signum sample) sample)))
                  (_.coverage [/.min]
                    (and (/.= (/.min left right)
                              (/.min right left))
                         (/.= sample
                              (/.min /#top sample))
                         (/.= /#bottom
                              (/.min /#bottom sample))))
                  (_.coverage [/.max]
                    (and (/.= (/.max left right)
                              (/.max right left))
                         (/.= /#top
                              (/.max /#top sample))
                         (/.= sample
                              (/.max /#bottom sample))))
                  ))
           (do random.monad
             [left random.int
              right random.int]
             (all _.and
                  (_.coverage [/.>]
                    (bit#= (/.> left right)
                           (/.< right left)))
                  (_.coverage [/.<= /.>=]
                    (bit#= (/.<= left right)
                           (/.>= right left)))
                  ))
           (do random.monad
             [left (random.only (|>> (/.= +0) not)
                                random.int)
              right random.int]
             (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]
                    (and (/.= (/.signum left)
                              (/.signum (/.mod left right)))
                         (/.= (/.signum right)
                              (/.signum (/.% left right)))
                         (if (/.= (/.signum left) (/.signum right))
                           (/.= (/.% left right)
                                (/.mod left right))
                           (or (and (/.= +0 (/.% left right))
                                    (/.= +0 (/.mod left right)))
                               (/.= (/.+ left (/.% left right))
                                    (/.mod left right))))))
                  ))
           (do [! random.monad]
             [.let [random (|> random.int
                               (at ! each (/.% +1,000))
                               (random.only (|>> (/.= +0) not)))]
              left random
              right random]
             (all _.and
                  (_.coverage [/.gcd]
                    (let [gcd (/.gcd left right)]
                      (and (/.= +0 (/.% gcd left))
                           (/.= +0 (/.% gcd right)))))
                  (_.coverage [/.extended_gcd]
                    (let [[[left_k right_k] gcd] (/.extended_gcd left right)

                          same_gcd!
                          (/.= gcd
                               (/.gcd left right))
                          
                          bezout_identity!
                          (/.= gcd
                               (/.+ (/.* left_k left)
                                    (/.* right_k right)))]
                      (and same_gcd!
                           bezout_identity!)))
                  (_.coverage [/.co_prime?]
                    (bit#= (/.= +1 (/.gcd left right))
                           (/.co_prime? left right)))
                  (_.coverage [/.lcm]
                    (let [lcm (/.lcm left right)]
                      (and (/.= +0 (/.% left lcm))
                           (/.= +0 (/.% right lcm)))))
                  ))
           (do random.monad
             [expected random.int]
             (_.coverage [/.opposite]
               (let [subtraction!
                     (/.= +0 (/.+ (/.opposite expected) expected))

                     inverse!
                     (|> expected /.opposite /.opposite (/.= expected))]
                 (and subtraction!
                      inverse!))))
           (do [! random.monad]
             [expected (at ! each (/.% +1,000,000) random.int)
              sample random.int]
             (_.coverage [/.frac]
               (and (|> expected /.frac f.int (/.= expected))
                    (f.number? (/.frac sample)))))
           (do [! random.monad]
             [pattern random.int
              idx (at ! each (n.% i64.width) random.nat)]
             (_.coverage [/.right_shifted]
               (let [nullity!
                     (/.= pattern (/.right_shifted 0 pattern))

                     idempotency!
                     (/.= pattern (/.right_shifted i64.width pattern))

                     sign_mask (i64.left_shifted (-- i64.width) 1)
                     mantissa_mask (-- (i64.left_shifted (n.- idx i64.width) 1))
                     co_mantissa_mask (i64.not mantissa_mask)

                     sign_preservation!
                     (/.= (i64.and sign_mask pattern)
                          (i64.and sign_mask (/.right_shifted idx pattern)))

                     mantissa_parity!
                     (/.= (i64.and mantissa_mask (i64.right_shifted idx pattern))
                          (i64.and mantissa_mask (/.right_shifted idx pattern)))

                     co_mantissa_disparity!
                     (or (n.= 0 idx)
                         (and (/.= +0 (i64.and co_mantissa_mask (i64.right_shifted idx pattern)))
                              (/.= (if (/.< +0 pattern)
                                     (.int co_mantissa_mask)
                                     +0)
                                   (i64.and co_mantissa_mask (/.right_shifted idx pattern)))))]
                 (and nullity!
                      idempotency!
                      sign_preservation!
                      mantissa_parity!
                      co_mantissa_disparity!
                      ))))

           ..predicate
           ..signature
           )))