blob: d646852f3f4ed8e1a45636cb8ccd965d9cdf7390 (
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
|
(.module:
[lux (#- primitive)
["_" test (#+ Test)]
[abstract
[equivalence (#+ Equivalence)]
[monad (#+ do)]]
[control
["." try]
["." exception]
["<>" parser]]
[data
["." binary]
["." sum]
["." maybe]
["." bit]
["." name]
["." text ("#@." equivalence)
["." encoding]]
["." 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: random-name
(Random Name)
(random.and (random.unicode ..segment-size)
(random.unicode ..segment-size)))
(structure: cursor-equivalence
(Equivalence Cursor)
(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-cursor
(Random Cursor)
($_ random.and
(random.unicode ..segment-size)
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-cursor
(: (Random (Code' (Ann Cursor)))
($_ random.or
random.bit
random.nat
random.int
random.rev
random.frac
(random.unicode ..segment-size)
..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.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.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.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.utf8/64]
)))))
(def: row
Test
(`` ($_ _.and
(~~ (template [<parser> <format>]
[(do {@ random.monad}
[expected (random.row ..segment-size random.nat)]
(_.cover [<parser>]
(|> 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.run <format> expected)
(/.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]
[/.frac format.frac random.frac frac.equivalence]
))
(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.run <format> expected)
(/.run <parser>)
(!expect (^multi (#try.Success actual)
(:: <equivalence> = expected actual))))))]
[/.cursor format.cursor random-cursor cursor-equivalence]
[/.code format.code random-code code.equivalence]
[/.type format.type random-type type.equivalence]
))
(~~ (template [<cover> <parser> <format> <random> <equivalence>]
[(do {@ random.monad}
[expected <random>]
(_.cover [<cover>]
(|> (format.run <format> expected)
(/.run <parser>)
(!expect (^multi (#try.Success actual)
(:: <equivalence> = expected actual))))))]
[/.maybe (/.maybe /.nat) (format.maybe format.nat) (random.maybe random.nat) (maybe.equivalence n.equivalence)]
[/.list (/.list /.nat) (format.list format.nat) (random.list ..segment-size random.nat) (list.equivalence n.equivalence)]
[/.set (/.set n.hash /.nat) (format.set format.nat) (random.set n.hash ..segment-size random.nat) set.equivalence]
[/.name /.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]
(|> 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]
(|> expected
(format.run (format.list format.nat))
(/.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]
(|> (binary.create 0)
(/.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]
(|> expected
(/.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
))))
|