aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/meta/type/resource.lux
blob: 65618e66a7f21bec21a089088f6bd05f7581b49d (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
(.require
 [library
  [lux (.except)
   ["_" test (.only Test)]
   [abstract
    ["[0]" monad (.only)
     [indexed (.only do)]]]
   [control
    ["[0]" io (.only IO)]
    ["[0]" try]
    ["[0]" exception (.only Exception)]
    [concurrency
     ["[0]" async (.only Async)]]]
   [data
    ["[0]" identity (.only Identity)]
    ["[0]" text (.use "[1]#[0]" equivalence)
     ["%" \\format (.only format)]]]
   [math
    ["[0]" random]]
   ["[0]" meta (.only)
    ["[0]" code (.only)
     ["<[1]>" \\parser]]
    [macro
     [syntax (.only syntax)]
     ["[0]" expansion]]]]]
 [\\library
  ["[0]" / (.only Res)]])

(def pure
  Test
  (monad.do [! random.monad]
    [pre (at ! each %.nat random.nat)
     post (at ! each %.nat random.nat)
     .let [! identity.monad]]
    (_.for [/.Linear /.run! /.monad]
           (`` (all _.and
                    (,, (with_template [<coverage> <bindings>]
                          [(_.coverage <coverage>
                             (<| (text#= (format pre post))
                                 (is (Identity Text))
                                 (/.run! !)
                                 (do (/.monad !)
                                   <bindings>
                                   (in (format left right)))))]
                          
                          [[/.Affine /.Key /.Res /.Ordered /.ordered
                            /.Relevant /.read]
                           [res|left (/.ordered ! pre)
                            res|right (/.ordered ! post)
                            right (/.read ! res|right)
                            left (/.read ! res|left)]]
                          [[/.Commutative /.commutative /.exchange]
                           [res|left (/.commutative ! pre)
                            res|right (/.commutative ! post)
                            _ ((/.exchange [1 0]) !)
                            left (/.read ! res|left)
                            right (/.read ! res|right)]]
                          [[/.group /.un_group]
                           [res|left (/.commutative ! pre)
                            res|right (/.commutative ! post)
                            _ ((/.group 2) !)
                            _ ((/.un_group 2) !)
                            right (/.read ! res|right)
                            left (/.read ! res|left)]]
                          [[/.lifted]
                           [left (/.lifted ! pre)
                            right (/.lifted ! post)]]
                          ))
                    )))))

(def sync
  Test
  (monad.do [! random.monad]
    [pre (at ! each %.nat random.nat)
     post (at ! each %.nat random.nat)
     .let [! io.monad]]
    (_.for [/.Linear /.run! /.monad]
           (`` (all _.and
                    (,, (with_template [<coverage> <bindings>]
                          [(_.coverage <coverage>
                             (<| (text#= (format pre post))
                                 io.run!
                                 (is (IO Text))
                                 (/.run! !)
                                 (do (/.monad !)
                                   <bindings>
                                   (in (format left right)))))]

                          [[/.Affine /.Key /.Res /.Ordered /.ordered
                            /.Relevant /.read]
                           [res|left (/.ordered ! pre)
                            res|right (/.ordered ! post)
                            right (/.read ! res|right)
                            left (/.read ! res|left)]]
                          [[/.Commutative /.commutative /.exchange]
                           [res|left (/.commutative ! pre)
                            res|right (/.commutative ! post)
                            _ ((/.exchange [1 0]) !)
                            left (/.read ! res|left)
                            right (/.read ! res|right)]]
                          [[/.group /.un_group]
                           [res|left (/.commutative ! pre)
                            res|right (/.commutative ! post)
                            _ ((/.group 2) !)
                            _ ((/.un_group 2) !)
                            right (/.read ! res|right)
                            left (/.read ! res|left)]]
                          [[/.lifted]
                           [left (/.lifted ! (io.io pre))
                            right (/.lifted ! (io.io post))]]
                          ))
                    )))))

(def async
  Test
  (monad.do [! random.monad]
    [pre (at ! each %.nat random.nat)
     post (at ! each %.nat random.nat)
     .let [! async.monad]]
    (_.for [/.Linear /.run! /.monad]
           (`` (all _.and
                    (,, (with_template [<coverage> <bindings>]
                          [(in (monad.do !
                                 [outcome (<| (is (Async Text))
                                              (/.run! !)
                                              (do (/.monad !)
                                                <bindings>
                                                (in (format left right))))]
                                 (_.coverage' <coverage>
                                   (text#= (format pre post)
                                           outcome))))]

                          [[/.Affine /.Key /.Res /.Ordered /.ordered
                            /.Relevant /.read]
                           [res|left (/.ordered ! pre)
                            res|right (/.ordered ! post)
                            right (/.read ! res|right)
                            left (/.read ! res|left)]]
                          [[/.Commutative /.commutative /.exchange]
                           [res|left (/.commutative ! pre)
                            res|right (/.commutative ! post)
                            _ ((/.exchange [1 0]) !)
                            left (/.read ! res|left)
                            right (/.read ! res|right)]]
                          [[/.group /.un_group]
                           [res|left (/.commutative ! pre)
                            res|right (/.commutative ! post)
                            _ ((/.group 2) !)
                            _ ((/.un_group 2) !)
                            right (/.read ! res|right)
                            left (/.read ! res|left)]]
                          [[/.lifted]
                           [left (/.lifted ! (async.resolved pre))
                            right (/.lifted ! (async.resolved post))]]
                          ))
                    )))))

(def with_error
  (syntax (_ [exception <code>.symbol
              to_expand <code>.any])
    (monad.do meta.monad
      [[_ _ exception] (meta.export exception)]
      (function (_ compiler)
        {.#Right [compiler
                  (list (code.bit (case ((expansion.single to_expand) compiler)
                                    {try.#Success _}
                                    false
                                    
                                    {try.#Failure error}
                                    true)))]}))))

(def .public test
  Test
  (<| (_.covering /._)
      (_.for [/.Procedure])
      (all _.and
           ..pure
           ..sync
           ..async

           (_.coverage [/.amount_cannot_be_zero]
             (`` (and (,, (with_template [<group|un_group>]
                            [(with_error /.amount_cannot_be_zero
                               (<group|un_group> 0))]

                            [/.group]
                            [/.un_group]
                            )))))
           (_.coverage [/.index_cannot_be_repeated]
             (with_error /.index_cannot_be_repeated
               (/.exchange [0 0])))
           )))