aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/region.lux
blob: 37969d317d82e6247ecdaa5e7db6e8cbf0839a3d (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
(.require
 [library
  [lux (.except)
   [type (.only sharing)]
   ["_" test (.only Test)]
   [abstract
    [equivalence (.only Equivalence)]
    [functor (.only Functor)]
    [apply (.only Apply)]
    ["[0]" monad (.only Monad do)]
    ["[0]" enum]
    [\\specification
     ["$[0]" functor (.only Injection Comparison)]
     ["$[0]" apply]
     ["$[0]" monad]]]
   [control
    ["[0]" try (.only Try)]]
   [data
    [collection
     ["[0]" list]]]
   [math
    ["[0]" random]
    [number
     ["n" nat]]]]]
 [\\library
  ["[0]" / (.only Region)
   [//
    ["[0]" thread (.only Thread)]
    ["[0]" exception (.only Exception exception:)]]]])

(exception: oops)

(def (success? result)
  (All (_ a) (-> (Try a) Bit))
  (case result
    {try.#Success _}
    true
    
    {try.#Failure _}
    false))

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

(def (injection value)
  (Injection (All (_ a) (All (_ ! r) (Region r (Thread !) a))))
  (function (_ [region scope])
    (function (_ !)
      [scope
       {try.#Success value}])))

(def comparison
  (Comparison (All (_ a) (All (_ ! r) (Region r (Thread !) a))))
  (function (_ == left right)
    (case [(sharing [a]
             (is (Equivalence a)
                 ==)
             (is (Try a)
                 (thread.result (as_expected (/.run! thread.monad left)))))
           (sharing [a]
             (is (Equivalence a)
                 ==)
             (is (Try a)
                 (thread.result (as_expected (/.run! thread.monad right)))))]
      [{try.#Success left} {try.#Success right}]
      (== left right)

      _
      false)))

(def .public test
  Test
  (<| (_.covering /._)
      (_.for [/.Region])
      (do [! random.monad]
        [expected_clean_ups (|> random.nat (at ! each (|>> (n.% 100) (n.max 1))))]
        (all _.and
             (_.for [/.functor]
                    ($functor.spec ..injection ..comparison (is (All (_ ! r)
                                                                  (Functor (Region r (thread.Thread !))))
                                                                (/.functor thread.functor))))
             (_.for [/.apply]
                    ($apply.spec ..injection ..comparison (is (All (_ ! r)
                                                                (Apply (Region r (thread.Thread !))))
                                                              (/.apply thread.monad))))
             (_.for [/.monad]
                    ($monad.spec ..injection ..comparison (is (All (_ ! r)
                                                                (Monad (Region r (thread.Thread !))))
                                                              (/.monad thread.monad))))
             
             (_.coverage [/.run!]
               (thread.result
                (do [! thread.monad]
                  [clean_up_counter (thread.box 0)
                   .let [//@ !
                         count_clean_up (function (_ value)
                                          (do !
                                            [_ (thread.update! ++ clean_up_counter)]
                                            (in {try.#Success []})))]
                   outcome (/.run! !
                                   (do [! (/.monad !)]
                                     [_ (monad.each ! (/.acquire! //@ count_clean_up)
                                                    (enum.range n.enum 1 expected_clean_ups))]
                                     (in [])))
                   actual_clean_ups (thread.read! clean_up_counter)]
                  (in (and (..success? outcome)
                           (n.= expected_clean_ups
                                actual_clean_ups))))))
             (_.coverage [/.failure]
               (thread.result
                (do [! thread.monad]
                  [clean_up_counter (thread.box 0)
                   .let [//@ !
                         count_clean_up (function (_ value)
                                          (do !
                                            [_ (thread.update! ++ clean_up_counter)]
                                            (in {try.#Success []})))]
                   outcome (/.run! !
                                   (do [! (/.monad !)]
                                     [_ (monad.each ! (/.acquire! //@ count_clean_up)
                                                    (enum.range n.enum 1 expected_clean_ups))
                                      _ (/.failure //@ (exception.error ..oops []))]
                                     (in [])))
                   actual_clean_ups (thread.read! clean_up_counter)]
                  (in (and (..throws? ..oops outcome)
                           (n.= expected_clean_ups
                                actual_clean_ups))))))
             (_.coverage [/.except]
               (thread.result
                (do [! thread.monad]
                  [clean_up_counter (thread.box 0)
                   .let [//@ !
                         count_clean_up (function (_ value)
                                          (do !
                                            [_ (thread.update! ++ clean_up_counter)]
                                            (in {try.#Success []})))]
                   outcome (/.run! !
                                   (do [! (/.monad !)]
                                     [_ (monad.each ! (/.acquire! //@ count_clean_up)
                                                    (enum.range n.enum 1 expected_clean_ups))
                                      _ (/.except //@ ..oops [])]
                                     (in [])))
                   actual_clean_ups (thread.read! clean_up_counter)]
                  (in (and (..throws? ..oops outcome)
                           (n.= expected_clean_ups
                                actual_clean_ups))))))
             (_.coverage [/.acquire! /.clean_up_error]
               (thread.result
                (do [! thread.monad]
                  [clean_up_counter (thread.box 0)
                   .let [//@ !
                         count_clean_up (function (_ value)
                                          (do !
                                            [_ (thread.update! ++ clean_up_counter)]
                                            (in (is (Try Any)
                                                    (exception.except ..oops [])))))]
                   outcome (/.run! !
                                   (do [! (/.monad !)]
                                     [_ (monad.each ! (/.acquire! //@ count_clean_up)
                                                    (enum.range n.enum 1 expected_clean_ups))]
                                     (in [])))
                   actual_clean_ups (thread.read! clean_up_counter)]
                  (in (and (or (n.= 0 expected_clean_ups)
                               (..throws? /.clean_up_error outcome))
                           (n.= expected_clean_ups
                                actual_clean_ups))))))
             (_.coverage [/.lifted]
               (thread.result
                (do [! thread.monad]
                  [clean_up_counter (thread.box 0)
                   .let [//@ !]
                   outcome (/.run! !
                                   (do (/.monad !)
                                     [_ (/.lifted //@ (thread.write! expected_clean_ups clean_up_counter))]
                                     (in [])))
                   actual_clean_ups (thread.read! clean_up_counter)]
                  (in (and (..success? outcome)
                           (n.= expected_clean_ups
                                actual_clean_ups))))))
             ))))