aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/state.lux
blob: d1817279c7b5a51faefe1bc947475c7ffa9668cd (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
(.require
 [library
  [lux (.except)
   [abstract
    [monad (.only do)]
    [\\specification
     ["$[0]" functor (.only Injection Comparison)]
     ["$[0]" apply]
     ["$[0]" monad]]]
   [control
    ["[0]" pipe]
    ["[0]" io]]
   [data
    ["[0]" product]]
   [math
    ["[0]" random]
    [number
     ["n" nat]]]
   [test
    ["_" property (.only Test)]]]]
 [\\library
  ["[0]" / (.only State)]])

(def (with_conditions [state output] computation)
  (-> [Nat Nat] (State Nat Nat) Bit)
  (|> computation
      (/.result state)
      product.right
      (n.= output)))

(def basics
  (do random.monad
    [state random.nat
     value random.nat]
    (all _.and
         (_.coverage [/.State /.get]
           (with_conditions [state state]
             /.get))
         (_.coverage [/.put]
           (with_conditions [state value]
             (do /.monad
               [_ (/.put value)]
               /.get)))
         (_.coverage [/.update]
           (with_conditions [state (n.* value state)]
             (do /.monad
               [_ (/.update (n.* value))]
               /.get)))
         (_.coverage [/.use]
           (with_conditions [state (++ state)]
             (/.use ++)))
         (_.coverage [/.local]
           (with_conditions [state (n.* value state)]
             (/.local (n.* value)
                      /.get)))
         )))

(def (injection value)
  (All (_ s) (Injection (State s)))
  (function (_ state)
    [state value]))

(def (comparison init)
  (All (_ s) (-> s (Comparison (State s))))
  (function (_ == left right)
    (== (product.right (/.result init left))
        (product.right (/.result init right)))))

(def structures
  Test
  (do random.monad
    [state random.nat]
    (all _.and
         (_.for [/.functor]
                ($functor.spec ..injection (..comparison state) /.functor))
         (_.for [/.apply]
                ($apply.spec ..injection (..comparison state) /.apply))
         (_.for [/.monad]
                ($monad.spec ..injection (..comparison state) /.monad))
         )))

(def loops
  Test
  (do [! random.monad]
    [limit (|> random.nat (at ! each (n.% 10)))
     .let [condition (do /.monad
                       [state /.get]
                       (in (n.< limit state)))]]
    (all _.and
         (_.coverage [/.while /.result]
           (|> (/.while condition (/.update ++))
               (/.result 0)
               (pipe.let [state' output']
                 (n.= limit state'))))
         (_.coverage [/.do_while]
           (|> (/.do_while condition (/.update ++))
               (/.result 0)
               (pipe.let [state' output']
                 (or (n.= limit state')
                     (and (n.= 0 limit)
                          (n.= 1 state'))))))
         )))

(def monad_transformer
  Test
  (do random.monad
    [state random.nat
     left random.nat
     right random.nat]
    (let [(open "io#[0]") io.monad]
      (_.coverage [/.+State /.with /.lifted /.result']
        (|> (is (/.+State io.IO Nat Nat)
                (do (/.with io.monad)
                  [a (/.lifted io.monad (io#in left))
                   b (in right)]
                  (in (n.+ a b))))
            (/.result' state)
            io.run!
            (pipe.let [state' output']
              (and (n.= state state')
                   (n.= (n.+ left right) output')))))
      )))

(def .public test
  Test
  (<| (_.covering /._)
      (all _.and
           ..basics
           ..structures
           ..loops
           ..monad_transformer)))