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

(def: #export test
  Test
  (<| (_.context (%name (name-of /.Atom)))
      (do r.monad
        [value r.nat
         swap-value r.nat
         set-value r.nat
         #let [box (/.atom value)]]
        ($_ _.and
            (_.test "Can obtain the value of an atom."
                    (n/= value (io.run (/.read box))))

            (_.test "Can swap the value of an atom."
                    (and (io.run (/.compare-and-swap value swap-value box))
                         (n/= swap-value (io.run (/.read box)))))

            (_.test "Can update the value of an atom."
                    (exec (io.run (/.update inc box))
                      (n/= (inc swap-value) (io.run (/.read box)))))

            (_.test "Can immediately set the value of an atom."
                    (exec (io.run (/.write set-value box))
                      (n/= set-value (io.run (/.read box)))))
            ))))