aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/parser/binary.lux
blob: 41d2cec10bbe944ff5d1d521a5fd4bd7286b23e6 (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
391
392
(.using
 [library
  [lux (.except)
   ["_" test (.only Test)]
   ["[0]" type]
   [abstract
    [equivalence (.only Equivalence)]
    [predicate (.only Predicate)]
    [monad (.only do)]]
   [control
    ["[0]" pipe]
    ["[0]" maybe]
    ["[0]" try]
    ["[0]" exception]
    ["<>" parser]]
   [data
    ["[0]" binary]
    ["[0]" sum]
    ["[0]" bit]
    ["[0]" text (.open: "[1]#[0]" equivalence)
     ["%" format (.only format)]
     [encoding
      ["[0]" utf8]]]
    ["[0]" format
     ["[1]" binary]]
    [collection
     ["[0]" list]
     ["[0]" sequence]
     ["[0]" set]]]
   [macro
    ["^" pattern]
    ["[0]" code]]
   [math
    ["[0]" random (.only Random)]
    [number
     ["n" nat]
     ["[0]" i64]
     ["[0]" int]
     ["[0]" rev]
     ["[0]" frac]]]
   [meta
    ["[0]" symbol]]]]
 [\\library
  ["[0]" /]])

(template: (!expect <expectation> <computation>)
  [(case <computation>
     <expectation>
     true

     _
     false)])

(def: segment_size 10)

(def: (utf8_conversion_does_not_alter? value)
  (Predicate Text)
  (|> value
      (# utf8.codec encoded)
      (# utf8.codec decoded)
      (pipe.case
        {try.#Success converted}
        (text#= value converted)
        
        {try.#Failure error}
        false)))

(def: random_text
  (Random Text)
  (random.only ..utf8_conversion_does_not_alter?
               (random.unicode ..segment_size)))

(def: random_symbol
  (Random Symbol)
  (random.and ..random_text ..random_text))

(implementation: 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)
  (all random.and
       ..random_text
       random.nat
       random.nat))

(def: random_code
  (Random Code)
  (random.rec
   (function (_ again)
     (let [random_sequence (do [! random.monad]
                             [size (# ! each (n.% 2) random.nat)]
                             (random.list size again))]
       (all random.and
            ..random_location
            (is (Random (Code' (Ann Location)))
                (all random.or
                     random.bit
                     random.nat
                     random.int
                     random.rev
                     random.safe_frac
                     ..random_text
                     ..random_symbol
                     random_sequence
                     random_sequence
                     random_sequence
                     )))))))

(def: random_type
  (Random Type)
  (let [(open "[0]") random.monad]
    (all random.either
         (in .Nat)
         (in .List)
         (in .Code)
         (in .Type))))

(def: size
  Test
  (<| (_.for [/.Size])
      (`` (all _.and
               (~~ (template [<size> <parser> <format>]
                     [(do [! random.monad]
                        [expected (# ! each (i64.and (i64.mask <size>))
                                     random.nat)]
                        (_.coverage [<size> <parser> <format>]
                          (|> (format.result <format> expected)
                              (/.result <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
  (`` (all _.and
           (~~ (template [<parser> <format>]
                 [(do [! random.monad]
                    [expected (# ! each (# utf8.codec encoded) (random.ascii ..segment_size))]
                    (_.coverage [<parser> <format>]
                      (|> (format.result <format> expected)
                          (/.result <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
  (`` (all _.and
           (~~ (template [<parser> <format>]
                 [(do [! random.monad]
                    [expected (random.ascii ..segment_size)]
                    (_.coverage [<parser> <format>]
                      (|> (format.result <format> expected)
                          (/.result <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: sequence
  Test
  (`` (all _.and
           (~~ (template [<parser> <format>]
                 [(do [! random.monad]
                    [expected (random.sequence ..segment_size random.nat)]
                    (_.coverage [<parser> <format>]
                      (|> expected
                          (format.result (<format> format.nat))
                          (/.result (<parser> /.nat))
                          (!expect (^.multi {try.#Success actual}
                                            (# (sequence.equivalence n.equivalence) = expected actual))))))]

                 [/.sequence_8 format.sequence_8]
                 [/.sequence_16 format.sequence_16]
                 [/.sequence_32 format.sequence_32]
                 [/.sequence_64 format.sequence_64]
                 )))))

(def: simple
  Test
  (`` (all _.and
           (~~ (template [<parser> <format> <random> <equivalence>]
                 [(do [! random.monad]
                    [expected <random>]
                    (_.coverage [<parser> <format>]
                      (|> expected
                          (format.result <format>)
                          (/.result <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]
             (_.coverage [/.frac format.frac]
               (|> expected
                   (format.result format.frac)
                   (/.result /.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 (# ! each (|>> (i64.and (i64.mask /.size_8))
                                      (n.max 2))
                          random.nat)]
             (_.coverage [/.not_a_bit]
               (|> expected
                   (format.result format.bits_8)
                   (/.result /.bit)
                   (!expect (^.multi {try.#Failure error}
                                     (exception.match? /.not_a_bit error))))))
           )))

(def: complex
  Test
  (`` (all _.and
           (~~ (template [<parser> <format> <random> <equivalence>]
                 [(do [! random.monad]
                    [expected <random>]
                    (_.coverage [<parser> <format>]
                      (|> expected
                          (format.result <format>)
                          (/.result <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> <coverage_format> <format> <random> <equivalence>]
                 [(do [! random.monad]
                    [expected <random>]
                    (_.coverage [<parser_coverage> <coverage_format>]
                      (|> expected
                          (format.result <format>)
                          (/.result <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]
                 [/.symbol /.symbol format.symbol format.symbol ..random_symbol symbol.equivalence]))
           (do [! random.monad]
             [expected (# ! each (list.repeated ..segment_size) random.nat)]
             (_.coverage [/.set_elements_are_not_unique]
               (|> expected
                   (format.result (format.list format.nat))
                   (/.result (/.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)]
             (_.coverage [/.or format.or]
               (|> expected
                   (format.result (format.or format.bit format.nat))
                   (/.result (is (/.Parser (Either Bit Nat))
                                 (/.or /.bit /.nat)))
                   (!expect (^.multi {try.#Success actual}
                                     (# (sum.equivalence bit.equivalence n.equivalence) =
                                        expected
                                        actual))))))
           (do [! random.monad]
             [tag (# ! each (|>> (i64.and (i64.mask /.size_8))
                                 (n.max 2))
                     random.nat)
              value random.bit]
             (_.coverage [/.invalid_tag]
               (|> [tag value]
                   (format.result (format.and format.bits_8 format.bit))
                   (/.result (is (/.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)]
             (_.coverage [/.rec format.rec format.and format.any]
               (|> expected
                   (format.result (format.rec (|>> (format.and format.nat)
                                                   (format.or format.any))))
                   (/.result (is (/.Parser (List Nat))
                                 (/.rec
                                  (function (_ again)
                                    (/.or /.any
                                          (<>.and /.nat
                                                  again))))))
                   (!expect (^.multi {try.#Success actual}
                                     (# (list.equivalence n.equivalence) =
                                        expected
                                        actual))))))
           )))

(def: .public test
  Test
  (<| (_.covering /._)
      (_.for [/.Parser])
      (`` (all _.and
               (_.coverage [/.result /.any
                            format.no_op format.instance]
                 (|> (format.instance format.no_op)
                     (/.result /.any)
                     (!expect {try.#Success _})))
               (do [! random.monad]
                 [data (# ! each (# utf8.codec encoded) (random.ascii ..segment_size))]
                 (_.coverage [/.binary_was_not_fully_read]
                   (|> data
                       (/.result /.any)
                       (!expect (^.multi {try.#Failure error}
                                         (exception.match? /.binary_was_not_fully_read error))))))
               (do [! random.monad]
                 [expected (# ! each (# utf8.codec encoded) (random.ascii ..segment_size))]
                 (_.coverage [/.segment format.segment format.result]
                   (|> expected
                       (format.result (format.segment ..segment_size))
                       (/.result (/.segment ..segment_size))
                       (!expect (^.multi {try.#Success actual}
                                         (# binary.equivalence = expected actual))))))
               (do [! random.monad]
                 [data (# ! each (# utf8.codec encoded) (random.ascii ..segment_size))]
                 (_.coverage [/.end?]
                   (|> data
                       (/.result (do <>.monad
                                   [pre /.end?
                                    _ (/.segment ..segment_size)
                                    post /.end?]
                                   (in (and (not pre)
                                            post))))
                       (!expect {try.#Success #1}))))
               (do [! random.monad]
                 [to_read (# ! each (n.% (++ ..segment_size)) random.nat)
                  data (# ! each (# utf8.codec encoded) (random.ascii ..segment_size))]
                 (_.coverage [/.Offset /.offset]
                   (|> data
                       (/.result (do <>.monad
                                   [start /.offset
                                    _ (/.segment to_read)
                                    offset /.offset
                                    _ (/.segment (n.- to_read ..segment_size))
                                    nothing_left /.offset]
                                   (in (and (n.= 0 start)
                                            (n.= to_read offset)
                                            (n.= ..segment_size nothing_left)))))
                       (!expect {try.#Success #1}))))
               (do [! random.monad]
                 [to_read (# ! each (n.% (++ ..segment_size)) random.nat)
                  data (# ! each (# utf8.codec encoded) (random.ascii ..segment_size))]
                 (_.coverage [/.remaining]
                   (|> data
                       (/.result (do <>.monad
                                   [_ (/.segment to_read)
                                    remaining /.remaining
                                    _ (/.segment (n.- to_read ..segment_size))
                                    nothing_left /.remaining]
                                   (in (and (n.= ..segment_size
                                                 (n.+ to_read remaining))
                                            (n.= 0 nothing_left)))))
                       (!expect {try.#Success #1}))))
               ..size
               ..binary
               ..utf8
               ..sequence
               ..simple
               ..complex
               ))))