blob: 3c731ebc409711b383b259669fb124e7e070d734 (
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
|
(.module:
lux
lux/test
(lux (control ["M" monad #+ do Monad])
[math]
["r" math/random]
(data [maybe]
[bit]
[text "text/" Eq<Text>]
text/format)
[macro]
(macro ["s" syntax #+ syntax:])))
(context: "Value identity."
(<| (times +100)
(do @
[size (|> r.nat (:: @ map (|>> (n/% +100) (n/max +10))))
x (r.unicode size)
y (r.unicode size)]
($_ seq
(test "Every value is identical to itself, and the 'id' function doesn't change values in any way."
(and (is? x x)
(is? x (id x))))
(test "Values created separately can't be identical."
(not (is? x y)))
))))
(do-template [category rand-gen even? odd? = < >]
[(context: (format "[" category "] " "Moving up-down or down-up should result in same value.")
(<| (times +100)
(do @
[value rand-gen]
(test "" (and (|> value inc dec (= value))
(|> value dec inc (= value)))))))
(context: (format "[" category "] " "(x+1) > x && (x-1) < x")
(<| (times +100)
(do @
[value rand-gen]
(test "" (and (|> value inc (> value))
(|> value dec (< value)))))))
(context: (format "[" category "] " "Every odd/even number is surrounded by two of the other kind.")
(<| (times +100)
(do @
[value rand-gen]
(test ""
(if (even? value)
(and (|> value inc odd?)
(|> value dec odd?))
(and (|> value inc even?)
(|> value dec even?)))))))]
["Nat" r.nat n/even? n/odd? n/= n/< n/>]
["Int" r.int i/even? i/odd? i/= i/< i/>]
)
(do-template [category rand-gen = < > <= >= min max]
[(context: (format "[" category "] " "The symmetry of numerical comparisons.")
(<| (times +100)
(do @
[x rand-gen
y rand-gen]
(test ""
(or (= x y)
(if (< y x)
(> x y)
(< x y)))))))
(context: (format "[" category "] " "Minimums and maximums.")
(<| (times +100)
(do @
[x rand-gen
y rand-gen]
(test ""
(and (and (<= x (min x y))
(<= y (min x y)))
(and (>= x (max x y))
(>= y (max x y)))
)))))]
["Int" r.int i/= i/< i/> i/<= i/>= i/min i/max]
["Nat" r.nat n/= n/< n/> n/<= n/>= n/min n/max]
["Frac" r.frac f/= f/< f/> f/<= f/>= f/min f/max]
["Deg" r.deg d/= d/< d/> d/<= d/>= d/min d/max]
)
(do-template [category rand-gen = + - * / <%> > <0> <1> <factor> <cap> <prep>]
[(context: (format "[" category "] " "Additive identity")
(<| (times +100)
(do @
[x rand-gen]
(test ""
(and (|> x (+ <0>) (= x))
(|> x (- <0>) (= x)))))))
(context: (format "[" category "] " "Addition & Substraction")
(<| (times +100)
(do @
[x (:: @ map <prep> rand-gen)
y (:: @ map <prep> rand-gen)
#let [x (* <factor> x)
y (* <factor> y)]]
(test ""
(and (|> x (- y) (+ y) (= x))
(|> x (+ y) (- y) (= x)))))))
(context: (format "[" category "] " "Multiplicative identity")
(<| (times +100)
(do @
[x rand-gen]
(test ""
## Skip this test for Deg
## because Deg division loses the last
## 32 bits of precision.
(or (text/= "Deg" category)
(and (|> x (* <1>) (= x))
(|> x (/ <1>) (= x))))))))
(context: (format "[" category "] " "Multiplication & Division")
(<| (times +100)
(do @
[x (:: @ map <cap> rand-gen)
y (|> rand-gen
(:: @ map <cap>)
(r.filter (|>> (= <0>) not)))
#let [r (<%> y x)
x' (- r x)]]
(test ""
## Skip this test for Deg
## because Deg division loses the last
## 32 bits of precision.
(or (text/= "Deg" category)
(or (> x' y)
(|> x' (/ y) (* y) (= x'))))
))))]
["Nat" r.nat n/= n/+ n/- n/* n// n/% n/> +0 +1 +1_000_000 (n/% +1_000) id]
["Int" r.int i/= i/+ i/- i/* i// i/% i/> 0 1 1_000_000 (i/% 1_000) id]
["Frac" r.frac f/= f/+ f/- f/* f// f/% f/> 0.0 1.0 1_000_000.0 id math.floor]
["Deg" r.deg d/= d/+ d/- d/* d// d/% d/> .0 (.deg -1) (.deg -1) id id]
)
(def: frac-deg
(r.Random Deg)
(|> r.deg
(:: r.Functor<Random> map (|>> (:! Nat) (bit.left-shift +11) (bit.right-shift +11) (:! Deg)))))
(do-template [category rand-gen -> <- = <cap>]
[(context: (format "[" category "] " "Numeric conversions")
(<| (times +100)
(do @
[value rand-gen
#let [value (<cap> value)]]
(test ""
(|> value -> <- (= value))))))]
["Int->Nat" r.int .nat .int i/= (i/% 1_000_000)]
["Nat->Int" r.nat .int .nat n/= (n/% +1_000_000)]
["Int->Frac" r.int int-to-frac frac-to-int i/= (i/% 1_000_000)]
["Frac->Int" r.frac frac-to-int int-to-frac f/= math.floor]
["Deg->Frac" frac-deg deg-to-frac frac-to-deg d/= id]
)
(context: "Simple macros and constructs"
($_ seq
(test "Can write easy loops for iterative programming."
(i/= 1000
(loop [counter 0
value 1]
(if (i/< 3 counter)
(recur (inc counter) (i/* 10 value))
value))))
(test "Can create lists easily through macros."
(and (case (list 1 2 3)
(#.Cons 1 (#.Cons 2 (#.Cons 3 #.Nil)))
true
_
false)
(case (list& 1 2 3 (list 4 5 6))
(#.Cons 1 (#.Cons 2 (#.Cons 3 (#.Cons 4 (#.Cons 5 (#.Cons 6 #.Nil))))))
true
_
false)))
(test "Can have defaults for Maybe values."
(and (is? "yolo" (maybe.default "yolo"
#.None))
(is? "lol" (maybe.default "yolo"
(#.Some "lol")))))
))
(template: (hypotenuse x y)
(i/+ (i/* x x) (i/* y y)))
(context: "Templates."
(<| (times +100)
(do @
[x r.int
y r.int]
(test "Template application is a stand-in for the templated code."
(i/= (i/+ (i/* x x) (i/* y y))
(hypotenuse x y))))))
(context: "Cross-platform support."
($_ seq
(test "Can provide default in case there is no particular platform support."
(for {"" false}
true))
(test "Can pick code depending on the platform being targeted."
(for {"JVM" true
"JS" true}
false))))
|