blob: 021bd07583d2ccb533db0fb55a97b7d3b0a7d760 (
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
|
(.require
[library
[lux (.except)
["_" test (.only Test)]
[abstract
[monad (.only do)]]
[control
["[0]" try]
["[0]" exception]]
[math
["[0]" random (.only Random)]
[number
["i" int]]]
["[0]" meta (.only)
["[0]" code]
[macro
[syntax (.only syntax)]]]]]
[\\library
["[0]" /]])
(def |divisor|
(syntax (_ [])
(do meta.monad
[divisor meta.seed]
(in (list (code.int (case divisor
0 +1
_ (.int divisor))))))))
(def .public (random range)
(Ex (_ %) (-> Int (Random (/.Modulus %))))
(|> random.int
(at random.monad each (i.% range))
(random.one (|>> /.modulus try.maybe))))
(def .public test
Test
(<| (_.covering /._)
(_.for [/.Modulus])
(do random.monad
[divisor random.int
modulus (..random +1,000,000)
dividend random.int]
(all _.and
(_.coverage [/.modulus /.divisor]
(case (/.modulus divisor)
{try.#Success modulus}
(i.= divisor (/.divisor modulus))
{try.#Failure error}
(i.= +0 divisor)))
(_.coverage [/.zero_cannot_be_a_modulus]
(case (/.modulus +0)
{try.#Failure error}
(exception.match? /.zero_cannot_be_a_modulus error)
{try.#Success modulus}
false))
(_.coverage [/.literal]
(with_expansions [<divisor> (|divisor|)]
(i.= <divisor> (/.divisor (/.literal <divisor>)))))
(_.coverage [/.=]
(with_expansions [<divisor> (|divisor|)]
(/.= (/.literal <divisor>)
(/.literal <divisor>))))
(_.coverage [/.congruent?]
(and (/.congruent? modulus dividend dividend)
(or (not (/.congruent? modulus dividend (++ dividend)))
(i.= +1 (/.divisor modulus)))))
))))
|