aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/parser/binary.lux
blob: 3bf3363c9fb00a75b8941cbe6bd0c10a66babaf3 (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:
  [library
   [lux "*"
    ["_" test {"+" Test}]
    ["[0]" type]
    [abstract
     [equivalence {"+" Equivalence}]
     [predicate {"+" Predicate}]
     [monad {"+" do}]]
    [control
     [pipe {"+" case>}]
     ["[0]" maybe]
     ["[0]" try]
     ["[0]" exception]
     ["<>" parser]]
    [data
     ["[0]" binary]
     ["[0]" sum]
     ["[0]" bit]
     ["[0]" text ("[1]#[0]" equivalence)
      ["%" format {"+" format}]
      [encoding
       ["[0]" utf8]]]
     ["[0]" format "_"
      ["[1]" binary]]
     [collection
      ["[0]" list]
      ["[0]" row]
      ["[0]" set]]]
    [macro
     ["[0]" code]]
    [math
     ["[0]" random {"+" 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)
      (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)
  ($_ random.and
      ..random_text
      random.nat
      random.nat))

(def: random_code
  (Random Code)
  (random.rec
   (function (_ recur)
     (let [random_sequence (do [! random.monad]
                             [size (# ! each (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_symbol
                  random_sequence
                  random_sequence
                  random_sequence
                  )))))))

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

(def: size
  Test
  (<| (_.for [/.Size])
      (`` ($_ _.and
              (~~ (template [<size> <parser> <format>]
                    [(do [! random.monad]
                       [expected (# ! each (i64.and (i64.mask <size>))
                                    random.nat)]
                       (_.cover [<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
  (`` ($_ _.and
          (~~ (template [<parser> <format>]
                [(do [! random.monad]
                   [expected (# ! each (# utf8.codec encoded) (random.ascii ..segment_size))]
                   (_.cover [<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
  (`` ($_ _.and
          (~~ (template [<parser> <format>]
                [(do [! random.monad]
                   [expected (random.ascii ..segment_size)]
                   (_.cover [<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: row
  Test
  (`` ($_ _.and
          (~~ (template [<parser> <format>]
                [(do [! random.monad]
                   [expected (random.row ..segment_size random.nat)]
                   (_.cover [<parser> <format>]
                            (|> expected
                                (format.result (<format> format.nat))
                                (/.result (<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.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]
            (_.cover [/.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)]
            (_.cover [/.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
  (`` ($_ _.and
          (~~ (template [<parser> <format> <random> <equivalence>]
                [(do [! random.monad]
                   [expected <random>]
                   (_.cover [<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>]
                   (_.cover [<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)]
            (_.cover [/.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)]
            (_.cover [/.or format.or]
                     (|> expected
                         (format.result (format.or format.bit format.nat))
                         (/.result (: (/.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]
            (_.cover [/.invalid_tag]
                     (|> [tag value]
                         (format.result (format.and format.bits/8 format.bit))
                         (/.result (: (/.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.result (format.rec (|>> (format.and format.nat)
                                                         (format.or format.any))))
                         (/.result (: (/.Parser (List Nat))
                                      (/.rec
                                       (function (_ recur)
                                         (/.or /.any
                                               (<>.and /.nat
                                                       recur))))))
                         (!expect (^multi {try.#Success actual}
                                          (# (list.equivalence n.equivalence) =
                                             expected
                                             actual))))))
          )))

(def: .public test
  Test
  (<| (_.covering /._)
      (_.for [/.Parser])
      (`` ($_ _.and
              (_.cover [/.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))]
                (_.cover [/.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))]
                (_.cover [/.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))]
                (_.cover [/.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))]
                (_.cover [/.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))]
                (_.cover [/.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
              ..row
              ..simple
              ..complex
              ))))