aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/data/binary.lux
blob: 25877dfd7a4ca4cbd6a126178ec99fc307507bb2 (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
(.using
 [library
  [lux "*"
   [ffi {"+"}]
   ["_" test {"+" Test}]
   [abstract
    ["[0]" monad {"+" do}]
    ["[0]" enum]
    [\\specification
     ["$[0]" equivalence]
     ["$[0]" monoid]]]
   [control
    ["[0]" try {"+" Try}]
    ["[0]" exception {"+" Exception}]]
   [data
    [collection
     ["[0]" list ("[1]#[0]" functor)]
     [array
      [\\unsafe {"+"}]]]]
   [math
    ["[0]" random {"+" Random}]
    [number
     ["n" nat]
     ["[0]" i64]]]]]
 [\\library
  ["[0]" /
   ["!" \\unsafe]]])

(def: (succeed result)
  (-> (Try Bit) Bit)
  (case result
    {try.#Failure _}
    false

    {try.#Success output}
    output))

(def: .public (random size)
  (-> Nat (Random /.Binary))
  (let [output (/.empty size)]
    (loop (again [idx 0])
      (if (n.< size idx)
        (do random.monad
          [byte random.nat]
          (exec (try.trusted (/.has/1! idx byte output))
            (again (++ idx))))
        (# random.monad in output)))))

(def: (throws? exception try)
  (All (_ e a) (-> (Exception e) (Try a) Bit))
  (case try
    {try.#Failure error}
    (exception.match? exception error)

    {try.#Success _}
    false))

(def: (binary_io power read write value)
  (-> Nat (-> Nat /.Binary (Try Nat)) (-> Nat Nat /.Binary (Try Any)) Nat Bit)
  (let [bytes (i64.left_shifted power 1)
        binary (/.empty bytes)
        cap (case bytes
              8 (-- 0)
              _ (|> 1 (i64.left_shifted (n.* 8 bytes)) --))
        capped_value (i64.and cap value)]
    (and (..succeed
          (do try.monad
            [pre (read 0 binary)
             _ (write 0 value binary)
             post (read 0 binary)]
            (in (and (n.= 0 pre)
                     (n.= capped_value post)))))
         (throws? /.index_out_of_bounds (read 1 binary))
         (throws? /.index_out_of_bounds (write 1 value binary)))))

(def: as_list
  (-> /.Binary (List Nat))
  (/.aggregate (function (_ head tail)
                 {.#Item head tail})
               (list)))

(def: test|unsafe
  Test
  (<| (_.covering !._)
      (_.for [!.Binary])
      (do [! random.monad]
        [.let [gen_size (|> random.nat (# ! each (|>> (n.% 100) (n.max 8))))]
         size gen_size
         sample (..random size)
         value random.nat
         .let [gen_idx (|> random.nat (# ! each (n.% size)))]
         offset gen_idx
         length (# ! each (n.% (n.- offset size)) random.nat)]
        (`` ($_ _.and
                (_.for [!.=]
                       ($equivalence.spec (function (_ left right)
                                            (!.= left right))
                                          (..random size)))
                (_.cover [!.empty]
                         (!.= (!.empty size) (!.empty size)))
                (_.cover [!.size]
                         (|> (!.empty size) !.size (n.= size)))
                (~~ (template [<power> <bytes/?> <has/?>]
                      [(_.cover [<bytes/?> <has/?>]
                                (let [bytes (i64.left_shifted <power> 1)
                                      binary (!.empty bytes)
                                      cap (case bytes
                                            8 (-- 0)
                                            _ (|> 1 (i64.left_shifted (n.* 8 bytes)) --))
                                      capped_value (i64.and cap value)
                                      
                                      pre (<bytes/?> 0 binary)
                                      _ (<has/?> 0 value binary)
                                      post (<bytes/?> 0 binary)]
                                  (and (n.= 0 pre)
                                       (n.= capped_value post))))]

                      [0 !.bytes/1 !.has/1!]
                      [1 !.bytes/2 !.has/2!]
                      [2 !.bytes/4 !.has/4!]
                      [3 !.bytes/8 !.has/8!]))
                (_.cover [!.slice]
                         (let [random_slice (!.slice offset length sample)
                               idxs (is (List Nat)
                                        (case length
                                          0 (list)
                                          _ (enum.range n.enum 0 (-- length))))
                               reader (function (_ binary idx)
                                        (!.bytes/1 idx binary))]
                           (and (n.= length (!.size random_slice))
                                (# (list.equivalence n.equivalence) =
                                   (list#each (|>> (n.+ offset) (reader sample)) idxs)
                                   (list#each (reader random_slice) idxs)))))
                (_.cover [!.copy!]
                         (and (let [it (!.copy! size 0 sample 0 (!.empty size))]
                                (and (not (same? sample it))
                                     (!.= sample it)))
                              (let [sample/0 (!.bytes/1 0 sample)
                                    copy (!.copy! 1 0 sample 0 (!.empty 2))
                                    copy/0 (!.bytes/1 0 copy)
                                    copy/1 (!.bytes/1 1 copy)]
                                (and (n.= sample/0 copy/0)
                                     (n.= 0 copy/1)))))
                )))))

(def: .public test
  Test
  (<| (_.covering /._)
      (_.for [/.Binary])
      (do [! random.monad]
        [.let [gen_size (|> random.nat (# ! each (|>> (n.% 100) (n.max 8))))]
         size gen_size
         sample (..random size)
         value random.nat
         .let [gen_idx (|> random.nat (# ! each (n.% size)))]
         offset (# ! each (n.max 1) gen_idx)
         length (# ! each (n.% (n.- offset size)) random.nat)]
        ($_ _.and
            (_.for [/.equivalence]
                   ($equivalence.spec /.equivalence (..random size)))
            (_.for [/.monoid]
                   ($monoid.spec /.equivalence /.monoid (..random size)))
            (_.cover [/.aggregate]
                     (n.= (# list.mix mix n.+ 0 (..as_list sample))
                          (/.aggregate n.+ 0 sample)))
            
            (_.cover [/.empty]
                     (# /.equivalence =
                        (/.empty size)
                        (/.empty size)))
            (_.cover [/.size]
                     (|> (/.empty size) /.size (n.= size)))
            (_.for [/.index_out_of_bounds]
                   ($_ _.and
                       (_.cover [/.bytes/1 /.has/1!]
                                (..binary_io 0 /.bytes/1 /.has/1! value))
                       (_.cover [/.bytes/2 /.has/2!]
                                (..binary_io 1 /.bytes/2 /.has/2! value))
                       (_.cover [/.bytes/4 /.has/4!]
                                (..binary_io 2 /.bytes/4 /.has/4! value))
                       (_.cover [/.bytes/8 /.has/8!]
                                (..binary_io 3 /.bytes/8 /.has/8! value))))
            (_.cover [/.slice]
                     (let [random_slice (try.trusted (/.slice offset length sample))
                           idxs (is (List Nat)
                                    (case length
                                      0 (list)
                                      _ (enum.range n.enum 0 (-- length))))
                           reader (function (_ binary idx)
                                    (/.bytes/1 idx binary))]
                       (and (n.= length (/.size random_slice))
                            (case [(monad.each try.monad (|>> (n.+ offset) (reader sample)) idxs)
                                   (monad.each try.monad (reader random_slice) idxs)]
                              [{try.#Success binary_vals} {try.#Success slice_vals}]
                              (# (list.equivalence n.equivalence) = binary_vals slice_vals)

                              _
                              #0))))
            (_.cover [/.slice_out_of_bounds]
                     (and (throws? /.slice_out_of_bounds (/.slice size size sample))
                          (let [verdict (throws? /.slice_out_of_bounds (/.slice offset size sample))]
                            (case offset
                              0 (not verdict)
                              _ verdict))))
            (_.cover [/.after]
                     (and (# /.equivalence = sample (/.after 0 sample))
                          (# /.equivalence = (/.empty 0) (/.after size sample))
                          (n.= (n.- offset size) (/.size (/.after offset sample)))
                          (case (list.reversed (..as_list sample))
                            {.#End}
                            false

                            {.#Item head tail}
                            (n.= (list.mix n.+ 0 tail)
                                 (/.aggregate n.+ 0 (/.after 1 sample))))))
            (_.cover [/.copy!]
                     (and (case (/.copy! size 0 sample 0 (/.empty size))
                            {try.#Success output}
                            (and (not (same? sample output))
                                 (# /.equivalence = sample output))

                            {try.#Failure _}
                            false)
                          (succeed
                           (do try.monad
                             [sample/0 (/.bytes/1 0 sample)
                              copy (/.copy! 1 0 sample 0 (/.empty 2))
                              copy/0 (/.bytes/1 0 copy)
                              copy/1 (/.bytes/1 1 copy)]
                             (in (and (n.= sample/0 copy/0)
                                      (n.= 0 copy/1)))))))
            (_.cover [/.cannot_copy]
                     (and (not (throws? /.cannot_copy
                                        (/.copy! size 0 sample 0 (/.empty size))))
                          (throws? /.cannot_copy
                                   (/.copy! (n.+ offset size) 0 sample 0 (/.empty size)))
                          (throws? /.cannot_copy
                                   (/.copy! size offset sample 0 (/.empty size)))
                          (throws? /.cannot_copy
                                   (/.copy! size 0 sample offset (/.empty size)))))

            ..test|unsafe
            ))))