aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/tool/compiler/phase.lux
blob: ced6edd4872ccecdbb3c003f9978f7b0997dce75 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
(.using
 [library
  [lux "*"
   ["_" test {"+" Test}]
   [abstract
    [monad {"+" do}]
    [\\specification
     ["$[0]" functor {"+" Injection Comparison}]
     ["$[0]" monad]]]
   [control
    ["[0]" pipe]
    ["[0]" try ("[1]#[0]" functor)]
    ["[0]" exception {"+" exception:}]]
   [data
    ["[0]" text ("[1]#[0]" equivalence)
     ["%" format {"+" format}]]]
   [math
    ["[0]" random]
    [number
     ["n" nat]
     ["i" int]]]]]
 [\\library
  ["[0]" /
   [//
    [meta
     ["[0]" archive]]]]])

(def: (injection value)
  (All (_ s) (Injection (/.Operation s)))
  (function (_ state)
    {try.#Success [state value]}))

(def: (comparison init)
  (All (_ s) (-> s (Comparison (/.Operation s))))
  (function (_ == left right)
    (case [(/.result init left)
           (/.result init right)]
      [{try.#Success left}
       {try.#Success right}]
      (== left right)

      _
      false)))

(exception: oops)

(def: test|error
  Test
  (do [! random.monad]
    [state random.nat
     expected random.int
     expected_error (random.ascii/lower 1)]
    ($_ _.and
        (_.cover [/.failure]
                 (|> (/.failure expected_error)
                     (/.result state)
                     (pipe.case {try.#Failure actual_error}
                       (same? expected_error actual_error)

                       _
                       false)))
        (_.cover [/.lifted]
                 (and (|> (/.lifted {try.#Failure expected_error})
                          (/.result state)
                          (pipe.case {try.#Failure actual_error}
                            (same? expected_error actual_error)

                            _
                            false))
                      (|> (/.lifted {try.#Success expected})
                          (# /.functor each (same? expected))
                          (/.result state)
                          (try.else false))))
        (_.cover [/.except]
                 (|> (/.except ..oops [])
                     (/.result state)
                     (pipe.case {try.#Failure error}
                       (exception.match? ..oops error)

                       _
                       false)))
        (_.cover [/.assertion]
                 (and (|> (/.assertion ..oops [] false)
                          (/.result state)
                          (pipe.case {try.#Failure error}
                            (exception.match? ..oops error)

                            _
                            false))
                      (|> (/.assertion ..oops [] true)
                          (/.result state)
                          (pipe.case {try.#Success _}
                            true

                            _
                            false))))
        )))

(def: test|state
  Test
  (do [! random.monad]
    [state random.nat
     dummy random.nat
     expected random.int]
    ($_ _.and
        (_.cover [/.state]
                 (|> /.state
                     (# /.functor each (same? state))
                     (/.result state)
                     (try.else false)))
        (_.cover [/.with]
                 (|> (do /.monad
                       [_ (/.with state)]
                       /.state)
                     (# /.functor each (same? state))
                     (/.result dummy)
                     (try.else false)))
        (_.cover [/.sub]
                 (|> (/.sub [(# n.hex encoded)
                             (function (_ new old)
                               (|> new (# n.hex decoded) (try.else dummy)))]
                            (do /.monad
                              [state/hex /.state]
                              (in (|> state
                                      (# n.hex encoded)
                                      (text#= state/hex)))))
                     (/.result' state)
                     (pipe.case {try.#Success [state' verdict]}
                       (and verdict
                            (n.= state state'))

                       _
                       false)))
        )))

(def: test|operation
  Test
  (do [! random.monad]
    [state random.nat
     expected random.int]
    ($_ _.and
        (_.for [/.functor]
               ($functor.spec ..injection (..comparison state) /.functor))
        (_.for [/.monad]
               ($monad.spec ..injection (..comparison state) /.monad))

        (_.cover [/.result]
                 (|> (# /.monad in expected)
                     (/.result state)
                     (pipe.case {try.#Success actual}
                       (same? expected actual)

                       _
                       false)))
        (_.cover [/.result']
                 (|> (# /.monad in expected)
                     (/.result' state)
                     (pipe.case {try.#Success [state' actual]}
                       (and (same? state state')
                            (same? expected actual))

                       _
                       false)))
        ..test|state
        ..test|error
        )))

(def: test|phase
  Test
  (do [! random.monad]
    [state/0 random.nat
     state/1 random.rev
     expected random.int]
    ($_ _.and
        (_.cover [/.identity]
                 (|> (/.identity archive.empty expected)
                     (/.result state/0)
                     (try#each (same? expected))
                     (try.else false)))
        (_.cover [/.composite]
                 (let [phase (/.composite (is (/.Phase Nat Int Frac)
                                              (function (_ archive input)
                                                (# /.monad in (i.frac input))))
                                          (is (/.Phase Rev Frac Text)
                                              (function (_ archive input)
                                                (# /.monad in (%.frac input)))))]
                   (|> (phase archive.empty expected)
                       (/.result' [state/0 state/1])
                       (pipe.case {try.#Success [[state/0' state/1'] actual]}
                         (and (text#= (%.frac (i.frac expected)) actual)
                              (same? state/0 state/0')
                              (same? state/1 state/1'))

                         _
                         false))))
        )))

(def: .public test
  Test
  (<| (_.covering /._)
      ($_ _.and
          (_.for [/.Operation]
                 ..test|operation)
          (_.for [/.Phase]
                 ..test|phase)
          )))