aboutsummaryrefslogtreecommitdiff
path: root/stdlib/test/test/lux.lux
blob: 4be4b753b49c1865f3c7cb8a9c82d23b25e675cd (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
(.module:
  [lux #*
   [control
    [monad (#+ do)]
    [predicate (#+ Predicate)]]
   [data
    ["." maybe]
    [number
     ["." i64]]]
   ["." math
    ["r" random (#+ Random) ("r/." Functor<Random>)]]
   ["_" test (#+ Test)]]
  [/
   ["/." cli]
   ["/." host
    ["/." jvm]]])

(def: identity
  Test
  (do r.Monad<Random>
    [self (r.unicode 1)]
    ($_ _.and
        (_.test "Every value is identical to itself."
                (is? self self))
        (_.test "The 'id' function doesn't change values in any way."
                (is? self (id self)))
        (do @
          [other (r.unicode 1)]
          (_.test "Values created separately can't be identical."
                  (not (is? self other))))
        )))

(def: increment-and-decrement
  Test
  (do r.Monad<Random>
    [value r.i64]
    ($_ _.and
        (_.test "'inc' and 'dec' are different."
                (not (n/= (inc value)
                          (dec value))))
        (_.test "'inc' and 'dec' are opposites."
                (and (|> value inc dec (n/= value))
                     (|> value dec inc (n/= value))))
        (_.test "'inc' and 'dec' shift the number by 1."
                (let [shift 1]
                  (and (n/= (n/+ shift value)
                            (inc value))
                       (n/= (n/- shift value)
                            (dec value))))))))

(def: (check-neighbors has-property? value)
  (All [a] (-> (Predicate (I64 a)) (I64 a) Bit))
  (and (|> value inc has-property?)
       (|> value dec has-property?)))

(def: (even-or-odd rand-gen even? odd?)
  (All [a] (-> (Random (I64 a)) (Predicate (I64 a)) (Predicate (I64 a)) Test))
  (do r.Monad<Random>
    [value rand-gen]
    ($_ _.and
        (_.test "Every number is either even or odd."
                (if (even? value)
                  (not (odd? value))
                  (odd? value)))
        (_.test "Every odd/even number is surrounded by two of the other kind."
                (if (even? value)
                  (check-neighbors odd? value)
                  (check-neighbors even? value))))))

(type: (Choice a)
  (-> a a a))

(type: (Order a)
  (-> a a Bit))

(type: (Equivalence a)
  (-> a a Bit))

(def: (choice rand-gen = [< choose])
  (All [a] (-> (Random a) (Equivalence a) [(Order a) (Choice a)] Test))
  (do r.Monad<Random>
    [left rand-gen
     right rand-gen
     #let [choice (choose left right)]]
    ($_ _.and
        (_.test "The choice between 2 values is one of them."
                (or (= left choice)
                    (= right choice)))
        (_.test "The choice between 2 values implies an order relationship between them."
                (if (= left choice)
                  (< right choice)
                  (< left choice))))))

(def: (minimum-and-maximum rand-gen = min' max')
  (All [a] (-> (Random a) (Equivalence a) [(Order a) (Choice a)] [(Order a) (Choice a)] Test))
  ($_ _.and
      (<| (_.context "Minimum.")
          (choice rand-gen = min'))
      (<| (_.context "Maximum.")
          (choice rand-gen = max'))))

(def: (conversion rand-gen forward backward =)
  (All [a b] (-> (Random a) (-> a b) (-> b a) (Equivalence a) Test))
  (do r.Monad<Random>
    [value rand-gen]
    (_.test "Can convert between types in a lossless way."
            (|> value forward backward (= value)))))

(def: frac-rev
  (r.Random Rev)
  (|> r.rev
      (:: r.Functor<Random> map (|>> (i64.left-shift 11) (i64.logical-right-shift 11)))))

(def: prelude-macros
  Test
  ($_ _.and
      (do r.Monad<Random>
        [factor (r/map (|>> (n/% 10) (n/max 1)) r.nat)
         iterations (r/map (n/% 100) r.nat)
         #let [expected (n/* factor iterations)]]
        (_.test "Can write loops."
                (n/= expected
                     (loop [counter 0
                            value 0]
                       (if (n/< iterations counter)
                         (recur (inc counter) (n/+ factor value))
                         value)))))

      (do r.Monad<Random>
        [first r.nat
         second r.nat
         third r.nat]
        (_.test "Can create lists easily through macros."
                (and (case (list first second third)
                       (#.Cons first' (#.Cons second' (#.Cons third' #.Nil)))
                       (and (n/= first first')
                            (n/= second second')
                            (n/= third third'))

                       _
                       false)
                     (case (list& first (list second third))
                       (#.Cons first' (#.Cons second' (#.Cons third' #.Nil)))
                       (and (n/= first first')
                            (n/= second second')
                            (n/= third third'))

                       _
                       false)
                     (case (list& first second (list third))
                       (#.Cons first' (#.Cons second' (#.Cons third' #.Nil)))
                       (and (n/= first first')
                            (n/= second second')
                            (n/= third third'))

                       _
                       false))))
      (do r.Monad<Random>
        [default r.nat
         maybe r.nat]
        (_.test "Can have defaults for Maybe values."
                (and (is? default (maybe.default default
                                                 #.None))

                     (is? maybe (maybe.default default
                                               (#.Some maybe))))))
      ))

(template: (hypotenuse cat0 cat1)
  (n/+ (n/* cat0 cat0) (n/* cat1 cat1)))

(def: template
  Test
  (do r.Monad<Random>
    [cat0 r.nat
     cat1 r.nat]
    (_.test "Template application is a stand-in for the templated code."
            (n/= (n/+ (n/* cat0 cat0) (n/* cat1 cat1))
                 (hypotenuse cat0 cat1)))))

(def: cross-platform-support
  Test
  (do r.Monad<Random>
    [on-default r.nat
     on-fake-host r.nat
     on-valid-host r.nat]
    ($_ _.and
        (_.test "Can provide default in case there is no particular host/platform support."
                (n/= on-default
                     (for {"" on-fake-host}
                          on-default)))
        (_.test "Can pick code depending on the host/platform being targeted."
                (n/= on-valid-host
                     (for {"JVM" on-valid-host
                           "JS" on-valid-host}
                          on-default))))))

(def: #export test
  ($_ _.and
      (<| (_.context "Identity.")
          ..identity)
      (<| (_.context "Increment & decrement.")
          ..increment-and-decrement)
      (<| (_.context "Even or odd.")
          ($_ _.and
              (<| (_.context "Natural numbers.")
                  (..even-or-odd r.nat n/even? n/odd?))
              (<| (_.context "Integers.")
                  (..even-or-odd r.int i/even? i/odd?))))
      (<| (_.context "Minimum and maximum.")
          (`` ($_ _.and
                  (~~ (do-template [<=> <lt> <min> <gt> <max> <gen> <context>]
                        [(<| (_.context <context>)
                             (..minimum-and-maximum <gen> <=> [<lt> <min>] [<gt> <max>]))]

                        [i/= i/< i/min i/> i/max r.int "Integers."]
                        [n/= n/< n/min n/> n/max r.nat "Natural numbers."]
                        [r/= r/< r/min r/> r/max r.rev "Revolutions."]
                        [f/= f/< f/min f/> f/max r.frac "Fractions."]
                        )))))
      (<| (_.context "Conversion.")
          (`` ($_ _.and
                  (~~ (do-template [<context> <=> <forward> <backward> <gen>]
                        [(<| (_.context <context>)
                             (..conversion <gen> <forward> <backward> <=>))]

                        ["Int -> Nat"
                         i/= .nat        .int        (r/map (i/%  +1_000_000) r.int)]
                        ["Nat -> Int"
                         n/= .int        .nat        (r/map (n/% 1_000_000) r.nat)]
                        ["Int -> Frac"
                         i/= int-to-frac frac-to-int (r/map (i/%  +1_000_000) r.int)]
                        ["Frac -> Int"
                         f/= frac-to-int int-to-frac (r/map math.floor r.frac)]
                        ["Rev -> Frac"
                         r/= rev-to-frac frac-to-rev frac-rev]
                        )))))
      (<| (_.context "Prelude macros.")
          ..prelude-macros)
      (<| (_.context "Templates.")
          ..template)
      (<| (_.context "Cross-platform support.")
          ..cross-platform-support)
      (<| (_.context "/cli")
          /cli.test)
      (<| (_.context "/host")
          ($_ _.and
              /host.test
              (<| (_.context "/jvm")
                  /jvm.test)))
      ))