aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/test/test/luxc/lang/analysis/procedure/common.lux
blob: 9cd456f5dd2aef73e2820c8596ec1d98eeca829d (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
(.module:
  lux
  (lux [io]
       (control [monad #+ do]
                pipe)
       (concurrency [atom])
       (data text/format
             ["e" error]
             [product]
             (coll [array]))
       ["r" math/random "r/" Monad<Random>]
       [macro #+ Monad<Meta>]
       (macro [code])
       (lang [type "type/" Eq<Type>])
       test)
  (luxc ["&" lang]
        (lang ["&." scope]
              ["&." module]
              ["~" analysis]
              (analysis [".A" expression]
                        ["@." common])
              [".L" eval]))
  (/// common)
  (test/luxc common))

(do-template [<name> <success> <failure>]
  [(def: (<name> procedure params output-type)
     (-> Text (List Code) Type Bool)
     (|> (&.with-scope
           (&.with-type output-type
             (analyse (` ((~ (code.text procedure)) (~+ params))))))
         (macro.run (io.run init-jvm))
         (case> (#e.Success _)
                <success>

                (#e.Error error)
                <failure>)))]

  [check-success+ true false]
  [check-failure+ false true]
  )

(context: "Lux procedures"
  (<| (times +100)
      (do @
        [[primT primC] gen-primitive
         [antiT antiC] (|> gen-primitive
                           (r.filter (|>> product.left (type/= primT) not)))]
        ($_ seq
            (test "Can test for reference equality."
                  (check-success+ "lux is" (list primC primC) Bool))
            (test "Reference equality must be done with elements of the same type."
                  (check-failure+ "lux is" (list primC antiC) Bool))
            (test "Can 'try' risky IO computations."
                  (check-success+ "lux try"
                                  (list (` ("lux function" (~' _) (~' _) (~ primC))))
                                  (type (Either Text primT))))
            ))))

(context: "Bit procedures"
  (<| (times +100)
      (do @
        [subjectC (|> r.nat (:: @ map code.nat))
         signedC (|> r.int (:: @ map code.int))
         paramC (|> r.nat (:: @ map code.nat))]
        ($_ seq
            (test "Can count the number of 1 bits in a bit pattern."
                  (check-success+ "lux bit count" (list subjectC) Nat))
            (test "Can perform bit 'and'."
                  (check-success+ "lux bit and" (list subjectC paramC) Nat))
            (test "Can perform bit 'or'."
                  (check-success+ "lux bit or" (list subjectC paramC) Nat))
            (test "Can perform bit 'xor'."
                  (check-success+ "lux bit xor" (list subjectC paramC) Nat))
            (test "Can shift bit pattern to the left."
                  (check-success+ "lux bit left-shift" (list subjectC paramC) Nat))
            (test "Can shift bit pattern to the right."
                  (check-success+ "lux bit logical-right-shift" (list subjectC paramC) Nat))
            (test "Can shift signed bit pattern to the right."
                  (check-success+ "lux bit arithmetic-right-shift" (list signedC paramC) Int))
            ))))

(context: "Nat procedures"
  (<| (times +100)
      (do @
        [subjectC (|> r.nat (:: @ map code.nat))
         paramC (|> r.nat (:: @ map code.nat))]
        ($_ seq
            (test "Can add natural numbers."
                  (check-success+ "lux nat +" (list subjectC paramC) Nat))
            (test "Can subtract natural numbers."
                  (check-success+ "lux nat -" (list subjectC paramC) Nat))
            (test "Can multiply natural numbers."
                  (check-success+ "lux nat *" (list subjectC paramC) Nat))
            (test "Can divide natural numbers."
                  (check-success+ "lux nat /" (list subjectC paramC) Nat))
            (test "Can calculate remainder of natural numbers."
                  (check-success+ "lux nat %" (list subjectC paramC) Nat))
            (test "Can test equality of natural numbers."
                  (check-success+ "lux nat =" (list subjectC paramC) Bool))
            (test "Can compare natural numbers."
                  (check-success+ "lux nat <" (list subjectC paramC) Bool))
            (test "Can obtain minimum natural number."
                  (check-success+ "lux nat min" (list) Nat))
            (test "Can obtain maximum natural number."
                  (check-success+ "lux nat max" (list) Nat))
            (test "Can convert natural number to integer."
                  (check-success+ "lux nat to-int" (list subjectC) Int))
            (test "Can convert natural number to text."
                  (check-success+ "lux nat char" (list subjectC) Text))
            ))))

(context: "Int procedures"
  (<| (times +100)
      (do @
        [subjectC (|> r.int (:: @ map code.int))
         paramC (|> r.int (:: @ map code.int))]
        ($_ seq
            (test "Can add integers."
                  (check-success+ "lux int +" (list subjectC paramC) Int))
            (test "Can subtract integers."
                  (check-success+ "lux int -" (list subjectC paramC) Int))
            (test "Can multiply integers."
                  (check-success+ "lux int *" (list subjectC paramC) Int))
            (test "Can divide integers."
                  (check-success+ "lux int /" (list subjectC paramC) Int))
            (test "Can calculate remainder of integers."
                  (check-success+ "lux int %" (list subjectC paramC) Int))
            (test "Can test equality of integers."
                  (check-success+ "lux int =" (list subjectC paramC) Bool))
            (test "Can compare integers."
                  (check-success+ "lux int <" (list subjectC paramC) Bool))
            (test "Can obtain minimum integer."
                  (check-success+ "lux int min" (list) Int))
            (test "Can obtain maximum integer."
                  (check-success+ "lux int max" (list) Int))
            (test "Can convert integer to natural number."
                  (check-success+ "lux int to-nat" (list subjectC) Nat))
            (test "Can convert integer to frac number."
                  (check-success+ "lux int to-frac" (list subjectC) Frac))
            ))))

(context: "Deg procedures"
  (<| (times +100)
      (do @
        [subjectC (|> r.deg (:: @ map code.deg))
         paramC (|> r.deg (:: @ map code.deg))
         natC (|> r.nat (:: @ map code.nat))]
        ($_ seq
            (test "Can add degrees."
                  (check-success+ "lux deg +" (list subjectC paramC) Deg))
            (test "Can subtract degrees."
                  (check-success+ "lux deg -" (list subjectC paramC) Deg))
            (test "Can multiply degrees."
                  (check-success+ "lux deg *" (list subjectC paramC) Deg))
            (test "Can divide degrees."
                  (check-success+ "lux deg /" (list subjectC paramC) Deg))
            (test "Can calculate remainder of degrees."
                  (check-success+ "lux deg %" (list subjectC paramC) Deg))
            (test "Can test equality of degrees."
                  (check-success+ "lux deg =" (list subjectC paramC) Bool))
            (test "Can compare degrees."
                  (check-success+ "lux deg <" (list subjectC paramC) Bool))
            (test "Can obtain minimum degree."
                  (check-success+ "lux deg min" (list) Deg))
            (test "Can obtain maximum degree."
                  (check-success+ "lux deg max" (list) Deg))
            (test "Can convert degree to frac number."
                  (check-success+ "lux deg to-frac" (list subjectC) Frac))
            (test "Can scale degree."
                  (check-success+ "lux deg scale" (list subjectC natC) Deg))
            (test "Can calculate the reciprocal of a natural number."
                  (check-success+ "lux deg reciprocal" (list subjectC natC) Deg))
            ))))

(context: "Frac procedures"
  (<| (times +100)
      (do @
        [subjectC (|> r.frac (:: @ map code.frac))
         paramC (|> r.frac (:: @ map code.frac))
         encodedC (|> (r.text +5) (:: @ map code.text))]
        ($_ seq
            (test "Can add frac numbers."
                  (check-success+ "lux frac +" (list subjectC paramC) Frac))
            (test "Can subtract frac numbers."
                  (check-success+ "lux frac -" (list subjectC paramC) Frac))
            (test "Can multiply frac numbers."
                  (check-success+ "lux frac *" (list subjectC paramC) Frac))
            (test "Can divide frac numbers."
                  (check-success+ "lux frac /" (list subjectC paramC) Frac))
            (test "Can calculate remainder of frac numbers."
                  (check-success+ "lux frac %" (list subjectC paramC) Frac))
            (test "Can test equality of frac numbers."
                  (check-success+ "lux frac =" (list subjectC paramC) Bool))
            (test "Can compare frac numbers."
                  (check-success+ "lux frac <" (list subjectC paramC) Bool))
            (test "Can obtain minimum frac number."
                  (check-success+ "lux frac min" (list) Frac))
            (test "Can obtain maximum frac number."
                  (check-success+ "lux frac max" (list) Frac))
            (test "Can obtain smallest frac number."
                  (check-success+ "lux frac smallest" (list) Frac))
            (test "Can obtain not-a-number."
                  (check-success+ "lux frac not-a-number" (list) Frac))
            (test "Can obtain positive infinity."
                  (check-success+ "lux frac positive-infinity" (list) Frac))
            (test "Can obtain negative infinity."
                  (check-success+ "lux frac negative-infinity" (list) Frac))
            (test "Can convert frac number to integer."
                  (check-success+ "lux frac to-int" (list subjectC) Int))
            (test "Can convert frac number to degree."
                  (check-success+ "lux frac to-deg" (list subjectC) Deg))
            (test "Can convert frac number to text."
                  (check-success+ "lux frac encode" (list subjectC) Text))
            (test "Can convert text to frac number."
                  (check-success+ "lux frac decode" (list encodedC) (type (Maybe Frac))))
            ))))

(context: "Text procedures"
  (<| (times +100)
      (do @
        [subjectC (|> (r.text +5) (:: @ map code.text))
         paramC (|> (r.text +5) (:: @ map code.text))
         replacementC (|> (r.text +5) (:: @ map code.text))
         fromC (|> r.nat (:: @ map code.nat))
         toC (|> r.nat (:: @ map code.nat))]
        ($_ seq
            (test "Can test text equality."
                  (check-success+ "lux text =" (list subjectC paramC) Bool))
            (test "Compare texts in lexicographical order."
                  (check-success+ "lux text <" (list subjectC paramC) Bool))
            (test "Can concatenate one text to another."
                  (check-success+ "lux text concat" (list subjectC paramC) Text))
            (test "Can find the index of a piece of text inside a larger one that (may) contain it."
                  (check-success+ "lux text index" (list subjectC paramC fromC) (type (Maybe Nat))))
            (test "Can query the size/length of a text."
                  (check-success+ "lux text size" (list subjectC) Nat))
            (test "Can calculate a hash code for text."
                  (check-success+ "lux text hash" (list subjectC) Nat))
            (test "Can replace a text inside of a larger one (once)."
                  (check-success+ "lux text replace-once" (list subjectC paramC replacementC) Text))
            (test "Can replace a text inside of a larger one (all times)."
                  (check-success+ "lux text replace-all" (list subjectC paramC replacementC) Text))
            (test "Can obtain the character code of a text at a given index."
                  (check-success+ "lux text char" (list subjectC fromC) (type (Maybe Nat))))
            (test "Can clip a piece of text between 2 indices."
                  (check-success+ "lux text clip" (list subjectC fromC toC) (type (Maybe Text))))
            ))))

(context: "Array procedures"
  (<| (times +100)
      (do @
        [[elemT elemC] gen-primitive
         sizeC (|> r.nat (:: @ map code.nat))
         idxC (|> r.nat (:: @ map code.nat))
         var-name (r.text +5)
         #let [arrayT (type (Array elemT))
               g!array (code.local-symbol var-name)
               array-operation (function (_ output-type code)
                                 (|> (&scope.with-scope ""
                                       (&scope.with-local [var-name arrayT]
                                         (&.with-type output-type
                                           (analyse code))))
                                     (macro.run (io.run init-jvm))
                                     (case> (#e.Success _)
                                            true

                                            (#e.Error error)
                                            false)))]]
        ($_ seq
            (test "Can create arrays."
                  (check-success+ "lux array new" (list sizeC) arrayT))
            (test "Can get a value inside an array."
                  (array-operation (type (Maybe elemT))
                                   (` ("lux array get" (~ g!array) (~ idxC)))))
            (test "Can put a value inside an array."
                  (array-operation arrayT
                                   (` ("lux array put" (~ g!array) (~ idxC) (~ elemC)))))
            (test "Can remove a value from an array."
                  (array-operation arrayT
                                   (` ("lux array remove" (~ g!array) (~ idxC)))))
            (test "Can query the size of an array."
                  (array-operation Nat
                                   (` ("lux array size" (~ g!array)))))
            ))))

(context: "Math procedures"
  (<| (times +100)
      (do @
        [subjectC (|> r.frac (:: @ map code.frac))
         paramC (|> r.frac (:: @ map code.frac))]
        (with-expansions [<unary> (do-template [<proc> <desc>]
                                    [(test (format "Can calculate " <desc> ".")
                                           (check-success+ <proc> (list subjectC) Frac))]

                                    ["lux math cos" "cosine"]
                                    ["lux math sin" "sine"]
                                    ["lux math tan" "tangent"]
                                    ["lux math acos" "inverse/arc cosine"]
                                    ["lux math asin" "inverse/arc sine"]
                                    ["lux math atan" "inverse/arc tangent"]
                                    ["lux math cosh" "hyperbolic cosine"]
                                    ["lux math sinh" "hyperbolic sine"]
                                    ["lux math tanh" "hyperbolic tangent"]
                                    ["lux math exp" "exponentiation"]
                                    ["lux math log" "logarithm"]
                                    ["lux math ceil" "ceiling"]
                                    ["lux math floor" "floor"]
                                    ["lux math round" "rounding"])
                          <binary> (do-template [<proc> <desc>]
                                     [(test (format "Can calculate " <desc> ".")
                                            (check-success+ <proc> (list subjectC paramC) Frac))]

                                     ["lux math atan2" "inverse/arc tangent (with 2 arguments)"]
                                     ["lux math pow" "power"])]
          ($_ seq
              <unary>
              <binary>)))))

(context: "Atom procedures"
  (<| (times +100)
      (do @
        [[elemT elemC] gen-primitive
         sizeC (|> r.nat (:: @ map code.nat))
         idxC (|> r.nat (:: @ map code.nat))
         var-name (r.text +5)
         #let [atomT (type (atom.Atom elemT))]]
        ($_ seq
            (test "Can create atomic reference."
                  (check-success+ "lux atom new" (list elemC) atomT))
            (test "Can read the value of an atomic reference."
                  (|> (&scope.with-scope ""
                        (&scope.with-local [var-name atomT]
                          (&.with-type elemT
                            (analyse (` ("lux atom read" (~ (code.symbol ["" var-name]))))))))
                      (macro.run (io.run init-jvm))
                      (case> (#e.Success _)
                             true

                             (#e.Error _)
                             false)))
            (test "Can swap the value of an atomic reference."
                  (|> (&scope.with-scope ""
                        (&scope.with-local [var-name atomT]
                          (&.with-type Bool
                            (analyse (` ("lux atom compare-and-swap"
                                         (~ (code.symbol ["" var-name]))
                                         (~ elemC)
                                         (~ elemC)))))))
                      (macro.run (io.run init-jvm))
                      (case> (#e.Success _)
                             true

                             (#e.Error _)
                             false)))
            ))))

(context: "Process procedures"
  (<| (times +100)
      (do @
        [[primT primC] gen-primitive
         timeC (|> r.nat (:: @ map code.nat))]
        ($_ seq
            (test "Can query the level of concurrency."
                  (check-success+ "lux process concurrency-level" (list) Nat))
            (test "Can run an IO computation concurrently."
                  (check-success+ "lux process future"
                                  (list (` ("lux function" (~' _) (~' _) (~ primC))))
                                  Top))
            (test "Can schedule an IO computation to run concurrently at some future time."
                  (check-success+ "lux process schedule"
                                  (list timeC
                                        (` ("lux function" (~' _) (~' _) (~ primC))))
                                  Top))
            ))))

(context: "IO procedures"
  (<| (times +100)
      (do @
        [logC (|> (r.text +5) (:: @ map code.text))
         exitC (|> r.int (:: @ map code.int))]
        ($_ seq
            (test "Can log messages to standard output."
                  (check-success+ "lux io log" (list logC) Top))
            (test "Can throw a run-time error."
                  (check-success+ "lux io error" (list logC) Bottom))
            (test "Can exit the program."
                  (check-success+ "lux io exit" (list exitC) Bottom))
            (test "Can query the current time (as milliseconds since epoch)."
                  (check-success+ "lux io current-time" (list) Int))
            ))))