aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux.lux
blob: 14360da93523d9221ba135c00ece56f4603d1828 (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
(.with-expansions [<host-modules> (.as-is [runtime (#+)]
                                          [primitive (#+)]
                                          [structure (#+)]
                                          [reference (#+)]
                                          [case (#+)]
                                          [loop (#+)]
                                          [function (#+)]
                                          [extension (#+)])]
  (.module:
    ["/" lux #*
     [abstract
      [monad (#+ do)]
      [predicate (#+ Predicate)]]
     [control
      ["." io (#+ io)]
      [function
       [mixin (#+)]]
      [parser
       [cli (#+ program:)]]]
     [data
      ["." name]
      [number
       ["." i64]
       ["n" nat]
       ["i" int]
       ["r" rev]
       ["f" frac]]]
     ["." math]
     ["_" test (#+ Test)]
     ## These modules do not need to be tested.
     [type
      [variance (#+)]]
     [locale (#+)
      [language (#+)]
      [territory (#+)]]
     ["%" data/text/format (#+ format)]
     [math
      ["." random (#+ Random) ("#@." functor)]]
     ## TODO: Test these modules
     [data
      [format
       [css (#+)]
       [markdown (#+)]]]
     ["@" target
      [js (#+)]
      [python (#+)]
      [lua (#+)]
      [ruby (#+)]
      [php (#+)]
      [common-lisp (#+)]
      [scheme (#+)]]
     ## [tool
     ##  [compiler
     ##   [language
     ##    [lux
     ##     [phase
     ##      [generation
     ##       [jvm (#+)
     ##        <host-modules>]
     ##       [js (#+)
     ##        <host-modules>]
     ##       [python (#+)
     ##        <host-modules>]
     ##       [lua (#+)
     ##        <host-modules>]
     ##       [ruby (#+)
     ##        <host-modules>]
     ##       ## [php (#+)
     ##       ##  <host-modules>]
     ##       ## [common-lisp (#+)
     ##       ##  <host-modules>]
     ##       ## [scheme (#+)
     ##       ##  <host-modules>]
     ##       ]
     ##      [extension
     ##       [generation
     ##        [jvm (#+)]
     ##        [js (#+)]
     ##        [python (#+)]
     ##        [lua (#+)]
     ##        [ruby (#+)]]]]]]]]
     ## [control
     ##  ["._" predicate]
     ##  [function
     ##   ["._" contract]]
     ##  [monad
     ##   ["._" free]]
     ##  [parser
     ##   [type (#+)]]]
     ## [data
     ##  ["._" env]
     ##  ["._" trace]
     ##  ["._" store]
     ##  [format
     ##   ["._" context]
     ##   ["._" html]
     ##   ["._" css]
     ##   ["._" binary]]
     ##  [collection
     ##   [tree
     ##    [rose
     ##     ["._" parser]]]
     ##   [dictionary
     ##    ["._" plist]]
     ##   [set
     ##    ["._" multi]]]
     ##  [text
     ##   ["._" buffer]]]
     ## ["._" macro]
     ## [type
     ##  ["._" unit]
     ##  ["._" refinement]
     ##  ["._" quotient]]
     ## [world
     ##  ["._" environment]
     ##  ["._" console]]
     ## [compiler
     ##  ["._" cli]
     ##  ["._" default
     ##   ["._" evaluation]
     ##   [phase
     ##    ["._" generation]
     ##    [extension
     ##     ["._" directive]]]
     ##   ["._default" cache]]
     ##  [meta
     ##   ["._meta" io
     ##    ["._meta_io" context]
     ##    ["._meta_io" archive]]
     ##   ["._meta" archive]
     ##   ["._meta" cache]]]
     ## ["._" interpreter
     ##  ["._interpreter" type]]
     ]
    ## TODO: Must have 100% coverage on tests.
    ["." / #_
     ["#." abstract]
     ["#." control]
     ["#." data]
     ["#." macro]
     ["#." math]
     ["#." time]
     ## ["#." tool]
     ["#." type]
     ["#." world]
     ["#." host]
     ["#." extension]
     ["#." target #_
      ["#/." jvm]]]
    ))

## TODO: Get rid of this ASAP
(template: (!bundle body)
  (: Test
     (do random.monad
       [_ (wrap [])]
       body)))

(def: identity
  Test
  (do {@ random.monad}
    [self (random.unicode 1)]
    ($_ _.and
        (_.test "Every value is identical to itself."
                (is? self self))
        (do @
          [other (random.unicode 1)]
          (_.test "Values created separately can't be identical."
                  (not (is? self other))))
        )))

(def: increment-and-decrement
  Test
  (do random.monad
    [value random.i64]
    ($_ _.and
        (_.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."
                (and (|> (inc value) (n.- value) (n.= 1))
                     (|> value (n.- (dec value)) (n.= 1)))))))

(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 random.monad
    [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 random.monad
    [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 random.monad
    [value rand-gen]
    (_.test "Can convert between types in a lossless way."
            (|> value forward backward (= value)))))

(def: frac-rev
  (Random Rev)
  (let [bits-to-ignore 11]
    (:: random.functor map (i64.left-shift bits-to-ignore) random.rev)))

(def: prelude-macros
  Test
  ($_ _.and
      (do random.monad
        [factor (random@map (|>> (n.% 10) (n.max 1)) random.nat)
         iterations (random@map (n.% 100) random.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 random.monad
        [first random.nat
         second random.nat
         third random.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))))
      ))

(template: (quadrance cat0 cat1)
  (n.+ (n.* cat0 cat0) (n.* cat1 cat1)))

(def: templates
  Test
  (do random.monad
    [cat0 random.nat
     cat1 random.nat]
    (_.test "Template application is a stand-in for the templated code."
            (n.= (n.+ (n.* cat0 cat0) (n.* cat1 cat1))
                 (quadrance cat0 cat1)))))

(def: cross-platform-support
  Test
  (do random.monad
    [on-default random.nat
     on-fake-host random.nat
     on-valid-host random.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 {(~~ (static @.old)) on-valid-host
                               (~~ (static @.jvm)) on-valid-host
                               (~~ (static @.js)) on-valid-host}
                              on-default)))))))

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

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

                                         [i.= .nat   .int   (random@map (i.% +1,000,000) random.int)]
                                         [n.= .int   .nat   (random@map (n.%  1,000,000) random.nat)]
                                         [i.= i.frac f.int  (random@map (i.% +1,000,000) random.int)]
                                         [f.= f.int  i.frac (random@map (|>> (i.% +1,000,000) i.frac) random.int)]
                                         [r.= r.frac f.rev  frac-rev]
                                         )))))
                       (<| (_.context "Prelude macros.")
                           ..prelude-macros)
                       (<| (_.context "Templates.")
                           ..templates)
                       (<| (_.context "Cross-platform support.")
                           ..cross-platform-support)))
          (_.in-parallel (list /abstract.test
                               /control.test
                               /data.test
                               /macro.test
                               /math.test
                               /time.test
                               ## /tool.test
                               /type.test
                               /world.test
                               /host.test
                               /extension.test
                               /target/jvm.test))
          )))

(program: args
  (<| io
      _.run!
      (_.times 100)
      ..test))