aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/type.lux
blob: 436dc4e85609faf70d1cda85f9be90ec7c8c3afe (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
(.require
 [library
  [lux (.except symbol)
   ["_" test (.only Test)]
   [abstract
    ["[0]" monad (.only do)]
    [\\specification
     ["$[0]" equivalence]]]
   [control
    ["[0]" pipe]
    ["[0]" maybe]]
   [data
    ["[0]" bit (.use "[1]#[0]" equivalence)]
    ["[0]" text (.use "[1]#[0]" equivalence)]
    [collection
     ["[0]" list]
     ["[0]" array]]]
   [macro
    ["^" pattern]
    ["[0]" code (.use "[1]#[0]" equivalence)]]
   [math
    ["[0]" random (.only Random) (.use "[1]#[0]" monad)]
    [number
     ["n" nat]]]]]
 [\\library
  ["[0]" / (.use "[1]#[0]" equivalence)]]
 ["[0]" /
  ["[1][0]" primitive]
  ["[1][0]" check]
  ["[1][0]" dynamic]
  ["[1][0]" implicit]
  ["[1][0]" quotient]
  ["[1][0]" refinement]
  ["[1][0]" resource]
  ["[1][0]" unit]
  ["[1][0]" variance]])

(def short
  (Random Text)
  (do [! random.monad]
    [size (|> random.nat (at ! each (n.% 10)))]
    (random.unicode size)))

(def symbol
  (Random Symbol)
  (random.and ..short ..short))

(def (random' parameters)
  (-> Nat (Random Type))
  (random.rec
   (function (_ again)
     (let [pairG (random.and again again)
           un_parameterized (is (Random Type)
                                (all random.either
                                     (random#each (|>> {.#Primitive}) (random.and ..short (random.list 0 again)))
                                     (random#each (|>> {.#Primitive}) (random.and ..short (random.list 1 again)))
                                     (random#each (|>> {.#Primitive}) (random.and ..short (random.list 2 again)))
                                     (random#each (|>> {.#Sum}) pairG)
                                     (random#each (|>> {.#Product}) pairG)
                                     (random#each (|>> {.#Function}) pairG)
                                     ))]
       (case parameters
         0 un_parameterized
         _ (|> random.nat
               (random#each (|>> (n.% parameters) {.#Parameter}))
               (random.either un_parameterized)))))))

(def .public (random parameters)
  (-> Nat (Random Type))
  (all random.either
       (random#each (/.univ_q parameters) (random' parameters))
       (random#each (/.ex_q parameters) (random' parameters))
       ))

(def .public test
  Test
  (<| (_.covering /._)
      (all _.and
           (_.for [/.equivalence]
                  ($equivalence.spec /.equivalence (..random 0)))
           
           (do [! random.monad]
             [anonymousT (random.only (|>> (pipe.case {.#Named _ _} false
                                             _ true))
                                      (..random 0))
              symbol/0 ..symbol
              symbol/1 ..symbol
              .let [namedT {.#Named symbol/0 anonymousT}
                    aliasedT {.#Named symbol/1 namedT}]]
             (all _.and
                  (_.coverage [/.de_aliased]
                    (at /.equivalence = namedT (/.de_aliased aliasedT)))
                  (_.coverage [/.anonymous]
                    (at /.equivalence = anonymousT (/.anonymous aliasedT)))))
           (do [! random.monad]
             [size (|> random.nat (at ! each (n.% 3)))
              members (|> (..random 0)
                          (random.only (function (_ type)
                                         (case type
                                           (^.or {.#Sum _} {.#Product _})
                                           #0

                                           _
                                           #1)))
                          (list.repeated size)
                          (monad.all !))
              .let [(open "/#[0]") /.equivalence
                    (open "list#[0]") (list.equivalence /.equivalence)]]
             (`` (all _.and
                      (~~ (with_template [<ctor> <dtor> <unit>]
                            [(_.coverage [<ctor> <dtor>]
                               (let [flat (|> members <ctor> <dtor>)]
                                 (or (list#= members flat)
                                     (and (list#= (list) members)
                                          (list#= (list <unit>) flat)))))]

                            [/.variant /.flat_variant Nothing]
                            [/.tuple   /.flat_tuple   Any]
                            ))
                      )))
           (_.coverage [/.applied]
             (and (<| (maybe.else #0)
                      (do maybe.monad
                        [partial (/.applied (list Bit) Ann)
                         full (/.applied (list Int) partial)]
                        (in (at /.equivalence = full {.#Product Bit Int}))))
                  (|> (/.applied (list Bit) Text)
                      (pipe.case {.#None} #1 _ #0))))
           (do [! random.monad]
             [size (|> random.nat (at ! each (n.% 3)))
              members (monad.all ! (list.repeated size (..random 0)))
              extra (|> (..random 0)
                        (random.only (function (_ type)
                                       (case type
                                         (^.or {.#Function _} {.#Apply _})
                                         #0

                                         _
                                         #1))))
              .let [(open "/#[0]") /.equivalence
                    (open "list#[0]") (list.equivalence /.equivalence)]]
             (all _.and
                  (_.coverage [/.function /.flat_function]
                    (let [[inputs output] (|> (/.function members extra) /.flat_function)]
                      (and (list#= members inputs)
                           (/#= extra output))))
                  (_.coverage [/.application /.flat_application]
                    (let [[tfunc tparams] (|> extra (/.application members) /.flat_application)]
                      (n.= (list.size members) (list.size tparams))))
                  ))
           (do [! random.monad]
             [size (|> random.nat (at ! each (|>> (n.% 3) ++)))
              body_type (|> (..random 0)
                            (random.only (function (_ type)
                                           (case type
                                             (^.or {.#UnivQ _} {.#ExQ _})
                                             #0

                                             _
                                             #1))))
              .let [(open "/#[0]") /.equivalence]]
             (`` (all _.and
                      (~~ (with_template [<ctor> <dtor>]
                            [(_.coverage [<ctor> <dtor>]
                               (let [[flat_size flat_body] (|> body_type (<ctor> size) <dtor>)]
                                 (and (n.= size flat_size)
                                      (/#= body_type flat_body))))]

                            [/.univ_q /.flat_univ_q]
                            [/.ex_q   /.flat_ex_q]
                            ))
                      (_.coverage [/.quantified?]
                        (and (not (/.quantified? body_type))
                             (|> body_type (/.univ_q size) /.quantified?)
                             (|> body_type (/.ex_q size) /.quantified?)))
                      )))
           (do [! random.monad]
             [depth (|> random.nat (at ! each (|>> (n.% 3) ++)))
              element_type (|> (..random 0)
                               (random.only (function (_ type)
                                              (case type
                                                (pattern {.#Primitive name (list element_type)})
                                                (not (text#= array.type_name name))

                                                _
                                                #1))))
              .let [(open "/#[0]") /.equivalence]]
             (all _.and
                  (_.coverage [/.array /.flat_array]
                    (let [[flat_depth flat_element] (|> element_type (/.array depth) /.flat_array)]
                      (and (n.= depth flat_depth)
                           (/#= element_type flat_element))))
                  (_.coverage [/.array?]
                    (and (not (/.array? element_type))
                         (/.array? (/.array depth element_type))))
                  ))
           (_.coverage [/.by_example]
             (let [example (is (Maybe Nat)
                               {.#None})]
               (/#= (.type_literal (List Nat))
                    (/.by_example [a]
                      (is (Maybe a)
                          example)
                      (List a)))))
           (do random.monad
             [sample random.nat]
             (_.coverage [/.log!]
               (exec
                 (/.log! sample)
                 true)))
           (do random.monad
             [left random.nat
              right (random.lower_case 1)
              .let [left,right [left right]]]
             (_.coverage [/.as]
               (|> left,right
                   (/.as [l r] (And l r) (Or l r))
                   (/.as [l r] (Or l r) (And l r))
                   (same? left,right))))
           (do random.monad
             [expected random.nat]
             (_.coverage [/.sharing]
               (n.= expected
                    (/.sharing [a]
                      (is (I64 a)
                          expected)
                      (is (I64 a)
                          (.i64 expected))))))
           (do random.monad
             [expected_left random.nat
              expected_right random.nat]
             (_.coverage [/.let]
               (let [[actual_left actual_right]
                     (is (/.let [side /.Nat]
                           [side side])
                         [expected_left expected_right])]
                 (and (same? expected_left actual_left)
                      (same? expected_right actual_right)))))
           (do random.monad
             [.let [(open "/#[0]") /.equivalence]
              left (..random 0)
              right (..random 0)]
             (all _.and
                  (_.coverage [/.code]
                    (bit#= (/#= left right)
                           (code#= (/.code left) (/.code right))))
                  (_.coverage [/.format]
                    (bit#= (/#= left right)
                           (text#= (/.format left) (/.format right))))
                  ))

           /primitive.test
           /check.test
           /dynamic.test
           /implicit.test
           /quotient.test
           /refinement.test
           /resource.test
           /unit.test
           /variance.test
           )))