aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/region.lux
blob: 477f42a476771a9842f139449dc66025ce014075 (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
(.module:
  [library
   [lux #*
    [type (#+ :sharing)]
    ["_" test (#+ Test)]
    [abstract
     [equivalence (#+ Equivalence)]
     [functor (#+ Functor)]
     [apply (#+ Apply)]
     ["." monad (#+ Monad do)]
     ["." enum]
     [\\specification
      ["$." functor (#+ Injection Comparison)]
      ["$." apply]
      ["$." monad]]]
    [control
     ["." try (#+ Try)]]
    [data
     [collection
      ["." list]]]
    [math
     ["." random]
     [number
      ["n" nat]]]]]
  [\\library
   ["." / (#+ Region)
    [//
     ["." thread (#+ Thread)]
     ["." exception (#+ 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]
                     (Equivalence a)
                     ==
                     
                     (Try a)
                     (thread.run (:assume (/.run thread.monad left))))
           (:sharing [a]
                     (Equivalence a)
                     ==
                     
                     (Try a)
                     (thread.run (:assume (/.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 (\ ! map (|>> (n.% 100) (n.max 1))))]
        ($_ _.and
            (_.for [/.functor]
                   ($functor.spec ..injection ..comparison (: (All [! r]
                                                                (Functor (Region r (thread.Thread !))))
                                                              (/.functor thread.functor))))
            (_.for [/.apply]
                   ($apply.spec ..injection ..comparison (: (All [! r]
                                                              (Apply (Region r (thread.Thread !))))
                                                            (/.apply thread.monad))))
            (_.for [/.monad]
                   ($monad.spec ..injection ..comparison (: (All [! r]
                                                              (Monad (Region r (thread.Thread !))))
                                                            (/.monad thread.monad))))
            
            (_.cover [/.run]
                     (thread.run
                      (do {! thread.monad}
                        [clean_up_counter (thread.box 0)
                         .let [//@ !
                               count_clean_up (function (_ value)
                                                (do !
                                                  [_ (thread.update inc clean_up_counter)]
                                                  (in (#try.Success []))))]
                         outcome (/.run !
                                        (do {! (/.monad !)}
                                          [_ (monad.map ! (/.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))))))
            (_.cover [/.failure]
                     (thread.run
                      (do {! thread.monad}
                        [clean_up_counter (thread.box 0)
                         .let [//@ !
                               count_clean_up (function (_ value)
                                                (do !
                                                  [_ (thread.update inc clean_up_counter)]
                                                  (in (#try.Success []))))]
                         outcome (/.run !
                                        (do {! (/.monad !)}
                                          [_ (monad.map ! (/.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))))))
            (_.cover [/.except]
                     (thread.run
                      (do {! thread.monad}
                        [clean_up_counter (thread.box 0)
                         .let [//@ !
                               count_clean_up (function (_ value)
                                                (do !
                                                  [_ (thread.update inc clean_up_counter)]
                                                  (in (#try.Success []))))]
                         outcome (/.run !
                                        (do {! (/.monad !)}
                                          [_ (monad.map ! (/.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))))))
            (_.cover [/.acquire /.clean_up_error]
                     (thread.run
                      (do {! thread.monad}
                        [clean_up_counter (thread.box 0)
                         .let [//@ !
                               count_clean_up (function (_ value)
                                                (do !
                                                  [_ (thread.update inc clean_up_counter)]
                                                  (in (: (Try Any)
                                                         (exception.except ..oops [])))))]
                         outcome (/.run !
                                        (do {! (/.monad !)}
                                          [_ (monad.map ! (/.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))))))
            (_.cover [/.lift]
                     (thread.run
                      (do {! thread.monad}
                        [clean_up_counter (thread.box 0)
                         .let [//@ !]
                         outcome (/.run !
                                        (do (/.monad !)
                                          [_ (/.lift //@ (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))))))
            ))))