aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/math/number/nat.lux
blob: 733b6445e0b07c38f9a88b21df3eee1d97739e70 (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
(.using
 [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 (.open: "[1]#[0]" equivalence)]]
   [math
    ["[0]" random]]]]
 [\\library
  ["[0]" / (.only)
   [//
    ["f" frac]]]])

(def: signature
  Test
  (`` (all _.and
           (_.for [/.equivalence /.=]
                  ($equivalence.spec /.equivalence random.nat))
           (_.for [/.hash]
                  ($hash.spec /.hash random.nat))
           (_.for [/.order /.<]
                  ($order.spec /.order random.nat))
           (_.for [/.enum]
                  ($enum.spec /.enum random.nat))
           (_.for [/.interval]
                  ($interval.spec /.interval random.nat))
           (~~ (template [<composite> <monoid>]
                 [(_.for [<monoid> <composite>]
                         ($monoid.spec /.equivalence <monoid> random.nat))]
                 
                 [/.+ /.addition]
                 [/.* /.multiplication]

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

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

(def: predicate
  Test
  (do [! random.monad]
    [sample random.nat]
    (all _.and
         (_.coverage [/.even? /.odd?]
           (bit#= (/.even? sample)
                  (not (/.odd? sample))))
         )))

(def: .public test
  Test
  (<| (_.covering /._)
      (_.for [.Nat])
      (all _.and
           (do random.monad
             [sample random.nat]
             (all _.and
                  (_.coverage [/.-]
                    (and (/.= 0 (/.- sample sample))
                         (/.= sample (/.- 0 sample))))
                  (_.coverage [/./]
                    (and (/.= 1 (/./ sample sample))
                         (/.= sample (/./ 1 sample))))
                  ))
           (do random.monad
             [left random.nat
              right random.nat]
             (all _.and
                  (_.coverage [/.>]
                    (bit#= (/.> left right)
                           (/.< right left)))
                  (_.coverage [/.<= /.>=]
                    (bit#= (/.<= left right)
                           (/.>= right left)))
                  ))
           (do random.monad
             [left (random.only (|>> (/.= 0) not)
                                random.nat)
              right random.nat]
             (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)))))
                  ))
           (do [! random.monad]
             [.let [random (# ! each (|>> (/.% 1,000) ++) random.nat)]
              left random
              right random]
             (all _.and
                  (_.coverage [/.gcd]
                    (let [gcd (/.gcd left right)]
                      (and (/.= 0 (/.% gcd left))
                           (/.= 0 (/.% gcd right)))))
                  (_.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 (# ! each (/.% 1,000,000) random.nat)
              sample random.nat]
             (_.coverage [/.frac]
               (and (|> expected /.frac f.nat (/.= expected))
                    (f.number? (/.frac sample)))))

           ..predicate
           ..signature
           )))