aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/concurrency/atom.lux
blob: c8496c2108738db1fb4edb246f91d2c8db28402c (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
(.module:
  [lux #*
   ["_" test (#+ Test)]
   [abstract
    [monad (#+ do)]]
   [control
    ["." io]]
   [math
    ["." random]
    [number
     ["n" nat]]]]
  {1
   ["." /]})

(def: #export test
  Test
  (<| (_.covering /._)
      (do random.monad
        [value random.nat
         swap_value random.nat
         set_value random.nat
         #let [box (/.atom value)]]
        ($_ _.and
            (_.cover [/.Atom /.atom /.read]
                     (n.= value
                          (io.run (/.read box))))
            (_.cover [/.compare_and_swap]
                     (and (io.run (/.compare_and_swap value swap_value box))
                          (n.= swap_value
                               (io.run (/.read box)))))
            (_.cover [/.update]
                     (exec (io.run (/.update inc box))
                       (n.= (inc swap_value)
                            (io.run (/.read box)))))
            (_.cover [/.write]
                     (exec (io.run (/.write set_value box))
                       (n.= set_value
                            (io.run (/.read box)))))
            ))))