blob: 94225be792f0a24d5e47f6220ce8759123c1dfbb (
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
(.module:
[lux (#- primitive)
["_" test (#+ Test)]
[abstract
[equivalence (#+ Equivalence)]
[predicate (#+ Predicate)]
[monad (#+ do)]]
[control
[pipe (#+ case>)]
["." try]
["." exception]
["<>" parser]]
[data
["." binary]
["." sum]
["." maybe]
["." bit]
["." name]
["." text ("#\." equivalence)
["." encoding]
["%" format (#+ format)]]
["." format #_
["#" binary]]
[number
["." i64]
["n" nat]
["." int]
["." rev]
["." frac]]
[collection
["." list]
["." row]
["." set]]]
[macro
["." code]]
["." type]
[math
["." random (#+ Random)]]]
{1
["." /]})
(template: (!expect <expectation> <computation>)
(case <computation>
<expectation>
true
_
false))
(def: segment-size 10)
(def: (utf8-conversion-does-not-alter? value)
(Predicate Text)
(|> value
encoding.to-utf8
encoding.from-utf8
(case> (#try.Success converted)
(text\= value converted)
(#try.Failure error)
false)))
(def: random-text
(Random Text)
(random.filter ..utf8-conversion-does-not-alter?
(random.unicode ..segment-size)))
(def: random-name
(Random Name)
(random.and ..random-text ..random-text))
(structure: location-equivalence
(Equivalence Location)
(def: (= [expected-module expected-line expected-column]
[sample-module sample-line sample-column])
(and (text\= expected-module sample-module)
(n.= expected-line sample-line)
(n.= expected-column sample-column))))
(def: random-location
(Random Location)
($_ random.and
..random-text
random.nat
random.nat))
(def: random-code
(Random Code)
(random.rec
(function (_ recur)
(let [random-sequence (do {! random.monad}
[size (\ ! map (n.% 2) random.nat)]
(random.list size recur))]
($_ random.and
..random-location
(: (Random (Code' (Ann Location)))
($_ random.or
random.bit
random.nat
random.int
random.rev
random.safe-frac
..random-text
..random-name
..random-name
random-sequence
random-sequence
(do {! random.monad}
[size (\ ! map (n.% 2) random.nat)]
(random.list size (random.and recur recur)))
)))))))
(def: random-type
(Random Type)
(let [(^open ".") random.monad]
($_ random.either
(wrap .Nat)
(wrap .List)
(wrap .Code)
(wrap .Type))))
(def: size
Test
(<| (_.with-cover [/.Size])
(`` ($_ _.and
(~~ (template [<size> <parser> <format>]
[(do {! random.monad}
[expected (\ ! map (i64.and (i64.mask <size>))
random.nat)]
(_.cover [<size> <parser> <format>]
(|> (format.run <format> expected)
(/.run <parser>)
(!expect (^multi (#try.Success actual)
(n.= (.nat expected)
(.nat actual)))))))]
[/.size/8 /.bits/8 format.bits/8]
[/.size/16 /.bits/16 format.bits/16]
[/.size/32 /.bits/32 format.bits/32]
[/.size/64 /.bits/64 format.bits/64]
))))))
(def: binary
Test
(`` ($_ _.and
(~~ (template [<parser> <format>]
[(do {! random.monad}
[expected (\ ! map encoding.to-utf8 (random.ascii ..segment-size))]
(_.cover [<parser> <format>]
(|> (format.run <format> expected)
(/.run <parser>)
(!expect (^multi (#try.Success actual)
(\ binary.equivalence = expected actual))))))]
[/.binary/8 format.binary/8]
[/.binary/16 format.binary/16]
[/.binary/32 format.binary/32]
[/.binary/64 format.binary/64]
)))))
(def: utf8
Test
(`` ($_ _.and
(~~ (template [<parser> <format>]
[(do {! random.monad}
[expected (random.ascii ..segment-size)]
(_.cover [<parser> <format>]
(|> (format.run <format> expected)
(/.run <parser>)
(!expect (^multi (#try.Success actual)
(\ text.equivalence = expected actual))))))]
[/.utf8/8 format.utf8/8]
[/.utf8/16 format.utf8/16]
[/.utf8/32 format.utf8/32]
[/.utf8/64 format.utf8/64]
[/.text format.text]
)))))
(def: row
Test
(`` ($_ _.and
(~~ (template [<parser> <format>]
[(do {! random.monad}
[expected (random.row ..segment-size random.nat)]
(_.cover [<parser> <format>]
(|> expected
(format.run (<format> format.nat))
(/.run (<parser> /.nat))
(!expect (^multi (#try.Success actual)
(\ (row.equivalence n.equivalence) = expected actual))))))]
[/.row/8 format.row/8]
[/.row/16 format.row/16]
[/.row/32 format.row/32]
[/.row/64 format.row/64]
)))))
(def: simple
Test
(`` ($_ _.and
(~~ (template [<parser> <format> <random> <equivalence>]
[(do {! random.monad}
[expected <random>]
(_.cover [<parser> <format>]
(|> expected
(format.run <format>)
(/.run <parser>)
(!expect (^multi (#try.Success actual)
(\ <equivalence> = expected actual))))))]
[/.bit format.bit random.bit bit.equivalence]
[/.nat format.nat random.nat n.equivalence]
[/.int format.int random.int int.equivalence]
[/.rev format.rev random.rev rev.equivalence]))
(do {! random.monad}
[expected random.frac]
(_.cover [/.frac format.frac]
(|> expected
(format.run format.frac)
(/.run /.frac)
(!expect (^multi (#try.Success actual)
(or (\ frac.equivalence = expected actual)
(and (frac.not-a-number? expected)
(frac.not-a-number? actual))))))))
(do {! random.monad}
[expected (\ ! map (|>> (i64.and (i64.mask /.size/8))
(n.max 2))
random.nat)]
(_.cover [/.not-a-bit]
(|> expected
(format.run format.bits/8)
(/.run /.bit)
(!expect (^multi (#try.Failure error)
(exception.match? /.not-a-bit error))))))
)))
(def: complex
Test
(`` ($_ _.and
(~~ (template [<parser> <format> <random> <equivalence>]
[(do {! random.monad}
[expected <random>]
(_.cover [<parser> <format>]
(|> expected
(format.run <format>)
(/.run <parser>)
(!expect (^multi (#try.Success actual)
(\ <equivalence> = expected actual))))))]
[/.location format.location random-location location-equivalence]
[/.code format.code random-code code.equivalence]
[/.type format.type random-type type.equivalence]
))
(~~ (template [<parser-coverage> <parser> <format-coverage> <format> <random> <equivalence>]
[(do {! random.monad}
[expected <random>]
(_.cover [<parser-coverage> <format-coverage>]
(|> expected
(format.run <format>)
(/.run <parser>)
(!expect (^multi (#try.Success actual)
(\ <equivalence> = expected actual))))))]
[/.maybe (/.maybe /.nat) format.maybe (format.maybe format.nat) (random.maybe random.nat) (maybe.equivalence n.equivalence)]
[/.list (/.list /.nat) format.list (format.list format.nat) (random.list ..segment-size random.nat) (list.equivalence n.equivalence)]
[/.set (/.set n.hash /.nat) format.set (format.set format.nat) (random.set n.hash ..segment-size random.nat) set.equivalence]
[/.name /.name format.name format.name ..random-name name.equivalence]))
(do {! random.monad}
[expected (\ ! map (list.repeat ..segment-size) random.nat)]
(_.cover [/.set-elements-are-not-unique]
(|> expected
(format.run (format.list format.nat))
(/.run (/.set n.hash /.nat))
(!expect (^multi (#try.Failure error)
(exception.match? /.set-elements-are-not-unique error))))))
(do {! random.monad}
[expected (random.or random.bit random.nat)]
(_.cover [/.or format.or]
(|> expected
(format.run (format.or format.bit format.nat))
(/.run (: (/.Parser (Either Bit Nat))
(/.or /.bit /.nat)))
(!expect (^multi (#try.Success actual)
(\ (sum.equivalence bit.equivalence n.equivalence) =
expected
actual))))))
(do {! random.monad}
[tag (\ ! map (|>> (i64.and (i64.mask /.size/8))
(n.max 2))
random.nat)
value random.bit]
(_.cover [/.invalid-tag]
(|> [tag value]
(format.run (format.and format.bits/8 format.bit))
(/.run (: (/.Parser (Either Bit Nat))
(/.or /.bit /.nat)))
(!expect (^multi (#try.Failure error)
(exception.match? /.invalid-tag error))))))
(do {! random.monad}
[expected (random.list ..segment-size random.nat)]
(_.cover [/.rec format.rec format.and format.any]
(|> expected
(format.run (format.rec (|>> (format.and format.nat)
(format.or format.any))))
(/.run (: (/.Parser (List Nat))
(/.rec
(function (_ recur)
(/.or /.any
(<>.and /.nat
recur))))))
(!expect (^multi (#try.Success actual)
(\ (list.equivalence n.equivalence) =
expected
actual))))))
)))
(def: #export test
Test
(<| (_.covering /._)
(_.with-cover [/.Parser])
(`` ($_ _.and
(_.cover [/.run /.any
format.no-op format.instance]
(|> (format.instance format.no-op)
(/.run /.any)
(!expect (#try.Success _))))
(do {! random.monad}
[data (\ ! map encoding.to-utf8 (random.ascii ..segment-size))]
(_.cover [/.binary-was-not-fully-read]
(|> data
(/.run /.any)
(!expect (^multi (#try.Failure error)
(exception.match? /.binary-was-not-fully-read error))))))
(do {! random.monad}
[expected (\ ! map encoding.to-utf8 (random.ascii ..segment-size))]
(_.cover [/.segment format.segment format.run]
(|> expected
(format.run (format.segment ..segment-size))
(/.run (/.segment ..segment-size))
(!expect (^multi (#try.Success actual)
(\ binary.equivalence = expected actual))))))
(do {! random.monad}
[data (\ ! map encoding.to-utf8 (random.ascii ..segment-size))]
(_.cover [/.end?]
(|> data
(/.run (do <>.monad
[pre /.end?
_ (/.segment ..segment-size)
post /.end?]
(wrap (and (not pre)
post))))
(!expect (#try.Success #1)))))
(do {! random.monad}
[to-read (\ ! map (n.% (inc ..segment-size)) random.nat)
data (\ ! map encoding.to-utf8 (random.ascii ..segment-size))]
(_.cover [/.Offset /.offset]
(|> data
(/.run (do <>.monad
[start /.offset
_ (/.segment to-read)
offset /.offset
_ (/.segment (n.- to-read ..segment-size))
nothing-left /.offset]
(wrap (and (n.= 0 start)
(n.= to-read offset)
(n.= ..segment-size nothing-left)))))
(!expect (#try.Success #1)))))
(do {! random.monad}
[to-read (\ ! map (n.% (inc ..segment-size)) random.nat)
data (\ ! map encoding.to-utf8 (random.ascii ..segment-size))]
(_.cover [/.remaining]
(|> data
(/.run (do <>.monad
[_ (/.segment to-read)
remaining /.remaining
_ (/.segment (n.- to-read ..segment-size))
nothing-left /.remaining]
(wrap (and (n.= ..segment-size
(n.+ to-read remaining))
(n.= 0 nothing-left)))))
(!expect (#try.Success #1)))))
..size
..binary
..utf8
..row
..simple
..complex
))))
|