aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/test/test/luxc/lang/translation/common.lux
blob: be552f1f0b318b47770ab811371d57fbbe76f0ac (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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
(.module:
  lux
  (lux [io #+ IO]
       (control [monad #+ do]
                pipe)
       (data text/format
             [bit]
             ["e" error]
             [bool "bool/" Eq<Bool>]
             [text "text/" Eq<Text>]
             [number "nat/" Interval<Nat> "int/" Number<Int> Interval<Int> "frac/" Number<Frac> Interval<Frac> "deg/" Interval<Deg>]
             (coll ["a" array]
                   [list]))
       ["r" math/random]
       [macro]
       (macro [code])
       [host]
       test)
  (luxc [lang]
        (lang [synthesis #+ Synthesis]))
  (test/luxc common))

(def: (bit-spec run)
  (-> Runner Test)
  (do r.Monad<Random>
    [param r.nat
     subject r.nat]
    (with-expansions [<binary> (do-template [<name> <reference> <param-expr>]
                                 [(test <name>
                                        (|> (run (` (<name> (~ (code.nat subject))
                                                            (~ (code.nat param)))))
                                            (case> (#e.Success valueT)
                                                   (n/= (<reference> param subject) (:! Nat valueT))

                                                   (#e.Error error)
                                                   (exec (log! error)
                                                     false))
                                            (let [param <param-expr>])))]

                                 ["lux bit and"                  bit.and param]
                                 ["lux bit or"                   bit.or param]
                                 ["lux bit xor"                  bit.xor param]
                                 ["lux bit shift-left"           bit.shift-left (n/% +64 param)]
                                 ["lux bit unsigned-shift-right" bit.shift-right (n/% +64 param)]
                                 )]
      ($_ seq
          <binary>
          (test "lux bit shift-right"
                (|> (run (` ("lux bit shift-right"
                             (~ (code.int (nat-to-int subject)))
                             (~ (code.nat param)))))
                    (case> (#e.Success valueT)
                           (i/= (bit.signed-shift-right param (nat-to-int subject))
                                (:! Int valueT))

                           (#e.Error error)
                           (exec (log! error)
                             false))
                    (let [param (n/% +64 param)])))
          ))))

(def: (int-spec run)
  (-> Runner Test)
  (do r.Monad<Random>
    [param (|> r.int (r.filter (|>> (i/= 0) not)))
     subject r.int
     #let [_ (log! (format "  param = " (%i param) "\n"
                           "subject = " (%i subject) "\n"))]]
    (`` ($_ seq
            (~~ (do-template [<name> <reference>]
                  [(test <name>
                         (|> (run (` (<name>)))
                             (case> (#e.Success valueT)
                                    (i/= <reference> (:! Int valueT))

                                    (#e.Error error)
                                    (exec (log! error)
                                      false))))]

                  ["lux int min" int/bottom]
                  ["lux int max" int/top]
                  ))
            (~~ (do-template [<name> <type> <prepare> <comp> <subject-expr>]
                  [(test <name>
                         (|> (run (` (<name> (~ (code.int subject)))))
                             (case> (#e.Success valueT)
                                    (<comp> (<prepare> subject) (:! <type> valueT))

                                    (#e.Error error)
                                    (exec (log! error)
                                      false))
                             (let [subject <subject-expr>])))]

                  ["lux int to-frac" Frac int-to-frac f/= subject]
                  ["lux int char"    Text (|>> (:! Nat) text.from-code) text/= (|> subject
                                                                                   (:! Nat)
                                                                                   (n/% (bit.shift-left +8 +1))
                                                                                   (:! Int))]
                  ))
            (~~ (do-template [<name> <reference> <outputT> <comp>]
                  [(test <name>
                         (exec (log! <name>)
                           (|> (run (` (<name> (~ (code.int subject)) (~ (code.int param)))))
                               (case> (#e.Success valueT)
                                      (<comp> (<reference> param subject) (:! <outputT> valueT))

                                      (#e.Error error)
                                      (exec (log! error)
                                        false)))))]

                  ["lux int +" i/+ Int  i/=]
                  ["lux int -" i/- Int  i/=]
                  ["lux int *" i/* Int  i/=]
                  ["lux int /" i// Int  i/=]
                  ["lux int %" i/% Int  i/=]
                  ["lux int =" i/= Bool bool/=]
                  ["lux int <" i/< Bool bool/=]
                  ))
            ))))

(def: (frac-spec|0 run)
  (-> Runner Test)
  (do r.Monad<Random>
    [param (|> r.frac (r.filter (|>> (f/= 0.0) not)))
     subject r.frac]
    (with-expansions [<binary> (do-template [<name> <reference> <outputT> <comp>]
                                 [(test <name>
                                        (|> (run (` (<name> (~ (code.frac subject)) (~ (code.frac param)))))
                                            (case> (#e.Success valueT)
                                                   (<comp> (<reference> param subject) (:! <outputT> valueT))

                                                   _
                                                   false)))]

                                 ["lux frac +" f/+ Frac f/=]
                                 ["lux frac -" f/- Frac f/=]
                                 ["lux frac *" f/* Frac f/=]
                                 ["lux frac /" f// Frac f/=]
                                 ["lux frac %" f/% Frac f/=]
                                 ["lux frac =" f/= Bool bool/=]
                                 ["lux frac <" f/< Bool bool/=]
                                 )]
      ($_ seq
          <binary>
          ))))

(def: (frac-spec|1 run)
  (-> Runner Test)
  (do r.Monad<Random>
    [subject r.frac]
    (`` ($_ seq
            (~~ (do-template [<name> <test>]
                  [(test <name>
                         (|> (run (` (<name>)))
                             (case> (#e.Success valueT)
                                    (<test> (:! Frac valueT))

                                    _
                                    false)))]

                  ["lux frac min" (f/= frac/bottom)]
                  ["lux frac max" (f/= frac/top)]
                  ["lux frac not-a-number" number.not-a-number?]
                  ["lux frac positive-infinity" (f/= number.positive-infinity)]
                  ["lux frac negative-infinity" (f/= number.negative-infinity)]
                  ["lux frac smallest" (f/= ("lux frac smallest"))]
                  ))
            (~~ (do-template [<forward> <backward> <test>]
                  [(test <forward>
                         (|> (run (` (<backward> (<forward> (~ (code.frac subject))))))
                             (case> (#e.Success valueT)
                                    (|> valueT (:! Frac) (f/- subject) frac/abs <test>)

                                    (#e.Error error)
                                    (exec (log! error)
                                      false))))]

                  ["lux frac to-int" "lux int to-frac" (f/< 1.0)]))
            ))))

(def: (frac-spec run)
  (-> Runner Test)
  ($_ seq
      (frac-spec|0 run)
      (frac-spec|1 run)))

(def: lower-alpha
  (r.Random Nat)
  (|> r.nat (:: r.Functor<Random> map (|>> (n/% +26) (n/+ +97)))))

(def: upper-alpha
  (r.Random Nat)
  (|> r.nat (:: r.Functor<Random> map (|>> (n/% +26) (n/+ +65)))))

(def: alpha
  (r.Random Nat)
  (r.either lower-alpha
            upper-alpha))

(def: (text-spec run)
  (-> Runner Test)
  (do r.Monad<Random>
    [sample-size (|> r.nat (:: @ map (|>> (n/% +10) (n/max +1))))
     sample0 (r.text' lower-alpha sample-size)
     sample1 (r.text' upper-alpha sample-size)
     sample2 (|> (r.text' alpha sample-size)
                 (r.filter (|>> (text/= sample1) not)))
     char-idx (|> r.nat (:: @ map (n/% sample-size)))
     #let [sample0S (code.text sample0)
           sample1S (code.text sample1)
           sample2S (code.text sample2)
           concatenatedS (` ("lux text concat" (~ sample0S) (~ sample1S)))
           pre-rep-once (format sample0 sample1)
           post-rep-once (format sample0 sample2)
           pre-rep-all (|> (list.repeat sample-size sample0) (text.join-with sample1))
           post-rep-all (|> (list.repeat sample-size sample0) (text.join-with sample2))]]
    ($_ seq
        (test "Can compare texts for equality."
              (and (|> (run (` ("lux text =" (~ sample0S) (~ sample0S))))
                       (case> (#e.Success valueV)
                              (:! Bool valueV)

                              _
                              false))
                   (|> (run (` ("lux text =" (~ sample0S) (~ sample1S))))
                       (case> (#e.Success valueV)
                              (not (:! Bool valueV))

                              _
                              false))))
        (test "Can compare texts for order."
              (|> (run (` ("lux text <" (~ sample1S) (~ sample0S))))
                  (case> (#e.Success valueV)
                         (:! Bool valueV)

                         _
                         false)))
        (test "Can change case of text."
              (and (|> (run (` ("lux text =" (~ sample0S) ("lux text upper" (~ sample0S)))))
                       (case> (#e.Success valueV)
                              (not (:! Bool valueV))

                              _
                              false))
                   (|> (run (` ("lux text =" (~ sample0S) ("lux text lower" ("lux text upper" (~ sample0S))))))
                       (case> (#e.Success valueV)
                              (:! Bool valueV)

                              _
                              false))))
        (test "Can get length of text."
              (|> (run (` ("lux text size" (~ sample0S))))
                  (case> (#e.Success valueV)
                         (n/= sample-size (:! Nat valueV))

                         _
                         false)))
        (test "Can concatenate text."
              (|> (run (` ("lux text size" (~ concatenatedS))))
                  (case> (#e.Success valueV)
                         (n/= (n/* +2 sample-size) (:! Nat valueV))

                         _
                         false)))
        (test "Can find index of sub-text."
              (and (|> (run (` ("lux text index" (~ concatenatedS) (~ sample0S) +0)))
                       (case> (^multi (#e.Success valueV)
                                      [(:! (Maybe Nat) valueV) (#.Some valueV)])
                              (n/= +0 valueV)

                              _
                              false))
                   (|> (run (` ("lux text index" (~ concatenatedS) (~ sample1S) +0)))
                       (case> (^multi (#e.Success valueV)
                                      [(:! (Maybe Nat) valueV) (#.Some valueV)])
                              (n/= sample-size valueV)

                              _
                              false))))
        (test "Text hashing is consistent."
              (|> (run (` ("lux int ="
                           ("lux text hash" (~ sample0S))
                           ("lux text hash" (~ sample0S)))))
                  (case> (#e.Success valueV)
                         (:! Bool valueV)
                         
                         (#e.Error error)
                         (exec (log! error)
                           false))))
        (test "Can replace sub-text once."
              (|> (run (` ("lux text ="
                           (~ (code.text post-rep-once))
                           ("lux text replace-once"
                            (~ (code.text pre-rep-once))
                            (~ sample1S)
                            (~ sample2S)))))
                  (case> (#e.Success valueV)
                         (:! Bool valueV)
                         
                         (#e.Error error)
                         (exec (log! error)
                           false))))
        (test "Can replace sub-text all times."
              (|> (run (` ("lux text ="
                           (~ (code.text post-rep-all))
                           ("lux text replace-all"
                            (~ (code.text pre-rep-all))
                            (~ sample1S)
                            (~ sample2S)))))
                  (case> (#e.Success valueV)
                         (:! Bool valueV)
                         
                         (#e.Error error)
                         (exec (log! error)
                           false))))
        (let [test-clip (function (_ from to expected)
                          (|> (run (` ("lux text clip"
                                       (~ concatenatedS)
                                       (~ (code.nat from))
                                       (~ (code.nat to)))))
                              (case> (^multi (#e.Success valueV)
                                             [(:! (Maybe Text) valueV) (#.Some valueV)])
                                     (text/= expected valueV)

                                     _
                                     false)))]
          (test "Can clip text to extract sub-text."
                (and (test-clip +0 sample-size sample0)
                     (test-clip sample-size (n/* +2 sample-size) sample1))))
        (test "Can extract individual characters from text."
              (|> (run (` ("lux text char"
                           (~ sample0S)
                           (~ (code.nat char-idx)))))
                  (case> (^multi (#e.Success valueV)
                                 [(:! (Maybe Nat) valueV) (#.Some valueV)])
                         (text.contains? ("lux nat char" valueV)
                                         sample0)

                         _
                         false)))
        )))

(def: (array-spec run)
  (-> Runner Test)
  (do r.Monad<Random>
    [size (|> r.nat (:: @ map (|>> (n/% +10) (n/max +1))))
     idx (|> r.nat (:: @ map (n/% size)))
     value r.nat
     #let [array0S (` ("lux array new" (~ (code.nat size))))
           array1S (` ("lux array put" (~ array0S) (~ (code.nat idx)) (~ (code.nat value))))]]
    ($_ seq
        (test "Can get size of array."
              (|> (run (` ("lux array size" (~ array0S))))
                  (case> (#e.Success valueV)
                         (n/= size (:! Nat valueV))

                         (#e.Error error)
                         (exec (log! error)
                           false))))
        (test "Can get element from array (if it exists)."
              (and (|> (run (` ("lux array get" (~ array0S) (~ (code.nat idx)))))
                       (case> (^multi (#e.Success valueV)
                                      [(:! (Maybe Nat) valueV) #.None])
                              true

                              _
                              false))
                   (|> (run (` ("lux array get" (~ array1S) (~ (code.nat idx)))))
                       (case> (^multi (#e.Success valueV)
                                      [(:! (Maybe Nat) valueV) (#.Some valueV)])
                              (n/= value valueV)

                              _
                              false))))
        (test "Can remove element from array."
              (|> (run (` ("lux array get"
                           ("lux array remove" (~ array1S)
                            (~ (code.nat idx)))
                           (~ (code.nat idx)))))
                  (case> (^multi (#e.Success valueV)
                                 [(:! (Maybe Nat) valueV) #.None])
                         true

                         _
                         false)))
        )))

(def: (math-spec run)
  (-> Runner Test)
  (do r.Monad<Random>
    [subject r.frac
     param r.frac]
    (`` ($_ seq
            (~~ (do-template [<name>]
                  [(test (format "Can apply '" <name> "' procedure.")
                         (|> (run (` (<name> (~ (code.frac subject)))))
                             (case> (#e.Success valueV)
                                    true

                                    (#e.Error error)
                                    (exec (log! error)
                                      false))))]

                  ["lux math cos"]
                  ["lux math sin"]
                  ["lux math tan"]
                  ["lux math acos"]
                  ["lux math asin"]
                  ["lux math atan"]
                  ["lux math exp"]
                  ["lux math log"]
                  ["lux math ceil"]
                  ["lux math floor"]))
            (~~ (do-template [<name>]
                  [(test (format "Can apply '" <name> "' procedure.")
                         (|> (run (` (<name> (~ (code.frac subject)) (~ (code.frac param)))))
                             (case> (#e.Success valueV)
                                    true

                                    (#e.Error error)
                                    (exec (log! error)
                                      false))))]

                  ["lux math pow"]))
            ))))

(def: (io-spec run)
  (-> Runner Test)
  (do r.Monad<Random>
    [message (r.text' alpha +5)]
    ($_ seq
        (test "Can log messages."
              (|> (run (` ("lux io log" (~ (code.text (format "LOG: " message))))))
                  (case> (#e.Success valueV)
                         true

                         (#e.Error error)
                         (exec (log! error)
                           false))))
        (test "Can throw runtime errors."
              (and (|> (run (` ("lux try" ("lux function" +1 []
                                           ("lux io error" (~ (code.text message)))))))
                       (case> (^multi (#e.Success valueV)
                                      [(:! (e.Error Text) valueV) (#e.Error error)])
                              (text.contains? message error)

                              _
                              false))
                   (|> (run (` ("lux try" ("lux function" +1 []
                                           (~ (code.text message))))))
                       (case> (^multi (#e.Success valueV)
                                      [(:! (e.Error Text) valueV) (#e.Success valueV)])
                              (text/= message valueV)

                              _
                              false))))
        (test "Can obtain current time in milli-seconds."
              (|> (run (` [("lux io current-time") ("lux io current-time")]))
                  (case> (#e.Success valueV)
                         (let [[pre post] (:! [Nat Nat] valueV)]
                           (n/>= pre post))

                         (#e.Error error)
                         (exec (log! error)
                           false))))
        )))

(def: (atom-spec run)
  (-> Runner Test)
  (do r.Monad<Random>
    [pre r.nat
     post (|> r.nat (r.filter (|>> (n/= pre) not)))
     #let [preS (code.nat pre)
           postS (code.nat post)
           atomS (` ("lux atom new" (~ preS)))]]
    ($_ seq
        (test "Can read atoms."
              (|> (run (` ("lux atom read" (~ atomS))))
                  (case> (#e.Success valueV)
                         (n/= pre (:! Nat valueV))

                         (#e.Error error)
                         (exec (log! error)
                           false))))
        (test "Can compare-and-swap atoms."
              (and (|> (run (` ("lux let" +0 (~ preS)
                                ("lux let" +1 ("lux atom new" (0))
                                 [("lux atom compare-and-swap" (1) (0) (~ postS))
                                  ("lux atom read" (1))]))))
                       (case> (#e.Success valueV)
                              (let [[swapped? current-value] (:! [Bool Nat] valueV)]
                                (and swapped?
                                     (n/= post current-value)))

                              (#e.Error error)
                              (exec (log! error)
                                false)))
                   (|> (run (` ("lux let" +0 (~ preS)
                                ("lux let" +1 ("lux atom new" (0))
                                 [("lux atom compare-and-swap" (1) (~ postS) (~ postS))
                                  ("lux atom read" (1))]))))
                       (case> (#e.Success valueV)
                              (let [[swapped? current-value] (:! [Bool Nat] valueV)]
                                (and (not swapped?)
                                     (n/= pre current-value)))

                              (#e.Error error)
                              (exec (log! error)
                                false)))))
        )))

(def: (box-spec run)
  (-> Runner Test)
  (do r.Monad<Random>
    [pre r.nat
     post (|> r.nat (r.filter (|>> (n/= pre) not)))
     #let [preS (code.nat pre)
           postS (code.nat post)
           boxS (` ("lux box new" (~ preS)))]]
    ($_ seq
        (test "Can read boxes."
              (|> (run (` ("lux box read" (~ boxS))))
                  (case> (#e.Success valueV)
                         (n/= pre (:! Nat valueV))

                         (#e.Error error)
                         (exec (log! error)
                           false))))
        (test "Can write boxes."
              (|> (run (` ("lux let" +0 (~ boxS)
                           ("lux let" +1 ("lux box write" (~ postS) (0))
                            ("lux box read" (0))))))
                  (case> (#e.Success valueV)
                         (n/= post (:! Nat valueV))

                         (#e.Error error)
                         (exec (log! error)
                           false))))
        )))

(def: (process-spec run)
  (-> Runner Test)
  ($_ seq
      (test "Can query the concurrency level of the machine."
            (|> (run (` ("lux process concurrency-level")))
                (case> (#e.Success valueV)
                       (n/>= +1 (:! Nat valueV))

                       (#e.Error error)
                       (exec (log! error)
                         false))))
      (do r.Monad<Random>
        [delay (|> r.nat (:: @ map (n/% +10)))
         message (r.text +5)]
        (test "Can schedule I/O operations for future execution."
              (|> (run (` ("lux process schedule"
                           (~ (code.nat delay))
                           ("lux function" +1 []
                            ("lux io log" (~ (code.text (format "SCHEDULE: " message))))))))
                  (case> (#e.Success valueV)
                         true

                         (#e.Error error)
                         (exec (log! error)
                           false)))))
      ))

(def: (all-specs run)
  (-> Runner Test)
  ($_ seq
      (bit-spec run)
      (int-spec run)
      (frac-spec run)
      (text-spec run)
      (array-spec run)
      (math-spec run)
      (io-spec run)
      (atom-spec run)
      (box-spec run)
      (process-spec run)
      ))

(context: "[JVM] Common procedures."
  (<| (times +100)
      (all-specs run-jvm)))

(context: "[JS] Common procedures."
  (<| (times +100)
      (all-specs run-js)))

(context: "[Lua] Common procedures."
  (<| (times +100)
      (all-specs run-lua)))

(context: "[Ruby] Common procedures."
  (<| (times +100)
      (all-specs run-ruby)))

(context: "[Python] Common procedures."
  (<| (times +100)
      (all-specs run-python)))

(context: "[R] Common procedures."
  (<| (times +100)
      (all-specs run-r)))

(context: "[Scheme] Common procedures."
  (<| (times +100)
      (all-specs run-scheme)))

(context: "[Common Lisp] Common procedures."
  (<| (times +100)
      (all-specs run-common-lisp)))

(context: "[PHP] Common procedures."
  (<| (times +100)
      (all-specs run-php)))