aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/math/number/int.lux
blob: 99738824aa4e112dac89f056b896e0398d295cb8 (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
(.module:
  [library
   [lux #*
    ["_" test (#+ Test)]
    [abstract
     [monad (#+ do)]
     [\\specification
      ["$." equivalence]
      ["$." hash]
      ["$." order]
      ["$." enum]
      ["$." interval]
      ["$." monoid]
      ["$." codec]]]
    [data
     ["." bit ("#\." equivalence)]]
    [math
     ["." random (#+ Random)]]]]
  [\\library
   ["." /
    [//
     ["n" nat]
     ["f" frac]
     ["." i64]]]])

(def: signature
  Test
  (`` ($_ _.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))
          (~~ (template [<compose> <monoid>]
                [(_.for [<monoid> <compose>]
                        ($monoid.spec /.equivalence <monoid> random.int))]
                
                [/.+ /.addition]
                [/.* /.multiplication]

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

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

(def: predicate
  Test
  (do {! random.monad}
    [sample random.int]
    ($_ _.and
        (_.cover [/.negative?]
                 (bit\= (/.negative? sample)
                        (/.< +0 sample)))
        (_.cover [/.positive?]
                 (bit\= (/.positive? sample)
                        (/.> +0 sample)))
        (_.cover [/.zero?]
                 (bit\= (/.zero? sample)
                        (/.= +0 sample)))
        (_.cover [/.even? /.odd?]
                 (bit\= (/.even? sample)
                        (not (/.odd? sample))))
        )))

(def: #export test
  Test
  (<| (_.covering /._)
      (_.for [.Int])
      ($_ _.and
          (do random.monad
            [sample random.int]
            ($_ _.and
                (_.cover [/.-]
                         (and (/.= +0 (/.- sample sample))
                              (/.= sample (/.- +0 sample))
                              (/.= (/.negate sample)
                                   (/.- sample +0))))
                (_.cover [/./]
                         (and (/.= +1 (/./ sample sample))
                              (/.= sample (/./ +1 sample))))
                (_.cover [/.abs]
                         (bit\= (/.> sample (/.abs sample))
                                (/.negative? sample)))
                (_.cover [/.signum]
                         (/.= (/.abs sample)
                              (/.* (/.signum sample) sample)))
                ))
          (do random.monad
            [left random.int
             right random.int]
            ($_ _.and
                (_.cover [/.>]
                         (bit\= (/.> left right)
                                (/.< right left)))
                (_.cover [/.<= /.>=]
                         (bit\= (/.<= left right)
                                (/.>= right left)))
                ))
          (do random.monad
            [left (random.only (|>> (/.= +0) not)
                               random.int)
             right random.int]
            ($_ _.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]
                         (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
                              (\ ! map (/.% +1,000))
                              (random.only (|>> (/.= +0) not)))]
             left random
             right random]
            ($_ _.and
                (_.cover [/.gcd]
                         (let [gcd (/.gcd left right)]
                           (and (/.= +0 (/.% gcd left))
                                (/.= +0 (/.% gcd right)))))
                (_.cover [/.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!)))
                (_.cover [/.co_prime?]
                         (bit\= (/.= +1 (/.gcd left right))
                                (/.co_prime? left right)))
                (_.cover [/.lcm]
                         (let [lcm (/.lcm left right)]
                           (and (/.= +0 (/.% left lcm))
                                (/.= +0 (/.% right lcm)))))
                ))
          (do random.monad
            [expected random.int]
            (_.cover [/.negate]
                     (let [subtraction!
                           (/.= +0 (/.+ (/.negate expected) expected))

                           inverse!
                           (|> expected /.negate /.negate (/.= expected))]
                       (and subtraction!
                            inverse!))))
          (do {! random.monad}
            [expected (\ ! map (/.% +1,000,000) random.int)]
            (_.cover [/.frac]
                     (|> expected /.frac f.int (/.= expected))))
          (do {! random.monad}
            [pattern random.int
             idx (\ ! map (n.% i64.width) random.nat)]
            (_.cover [/.right_shift]
                     (let [nullity!
                           (/.= pattern (/.right_shift 0 pattern))

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

                           sign_mask (i64.left_shift (dec i64.width) 1)
                           mantissa_mask (i64.not sign_mask)

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

                           mantissa_parity!
                           (/.= (i64.and mantissa_mask (i64.right_shift idx pattern))
                                (i64.and mantissa_mask (/.right_shift idx pattern)))]
                       (and nullity!
                            idempotency!
                            sign_preservation!
                            mantissa_parity!))))

          ..predicate
          ..signature
          )))