aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/data/binary.lux
blob: 331e051422290bbd21ea12d2d6070e069c840986 (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
(.module:
  [library
   [lux #*
    ["_" test (#+ Test)]
    [abstract
     ["." monad (#+ do)]
     ["." enum]
     [\\specification
      ["$." equivalence]
      ["$." monoid]]]
    [control
     ["." try (#+ Try)]
     ["." exception (#+ Exception)]]
    [data
     [collection
      ["." list]]]
    [math
     ["." random (#+ Random)]
     [number
      ["." i64]
      ["n" nat]]]]]
  [\\library
   ["." / (#+ Binary)]])

(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 [idx 0]
      (if (n.< size idx)
        (do random.monad
          [byte random.nat]
          (exec (try.trusted (/.write/8! idx byte output))
            (recur (++ 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: .public test
  Test
  (<| (_.covering /._)
      (do {! random.monad}
        [.let [gen_size (|> random.nat (\ ! map (|>> (n.% 100) (n.max 8))))]
         size gen_size
         sample (..random size)
         value random.nat
         .let [gen_idx (|> random.nat (\ ! map (n.% size)))]
         offset gen_idx
         length (\ ! map (n.% (n.- offset size)) random.nat)]
        (_.for [/.Binary]
               ($_ _.and
                   (_.for [/.equivalence]
                          ($equivalence.spec /.equivalence (..random size)))
                   (_.for [/.monoid]
                          ($monoid.spec /.equivalence /.monoid (..random size)))
                   (_.cover [/.aggregate]
                            (n.= (\ list.fold fold 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 [/.read/8! /.write/8!]
                                       (..binary_io 0 /.read/8! /.write/8! value))
                              (_.cover [/.read/16! /.write/16!]
                                       (..binary_io 1 /.read/16! /.write/16! value))
                              (_.cover [/.read/32! /.write/32!]
                                       (..binary_io 2 /.read/32! /.write/32! value))
                              (_.cover [/.read/64! /.write/64!]
                                       (..binary_io 3 /.read/64! /.write/64! value))))
                   (_.cover [/.slice]
                            (let [random_slice (try.trusted (/.slice offset length sample))
                                  idxs (: (List Nat)
                                          (case length
                                            0 (list)
                                            _ (enum.range n.enum 0 (-- length))))
                                  reader (function (_ binary idx)
                                           (/.read/8! idx binary))]
                              (and (n.= length (/.size random_slice))
                                   (case [(monad.map try.monad (|>> (n.+ offset) (reader sample)) idxs)
                                          (monad.map 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))
                                 (case (list.reversed (..as_list sample))
                                   #.End
                                   false

                                   (#.Item head tail)
                                   (n.= (list.fold 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 (/.read/8! 0 sample)
                                     copy (/.copy 1 0 sample 0 (/.empty 2))
                                     copy/0 (/.read/8! 0 copy)
                                     copy/1 (/.read/8! 1 copy)]
                                    (in (and (n.= sample/0 copy/0)
                                             (n.= 0 copy/1)))))))
                   )))))