aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/test/test/luxc/analyser/procedure/common.lux
blob: 3947a738e36e52fbda673b3f492744cccc520cb0 (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
(;module:
  lux
  (lux [io]
       (control [monad #+ do]
                pipe)
       (concurrency [atom])
       (data text/format
             ["R" result]
             [product]
             (coll [array]))
       ["r" math/random "r/" Monad<Random>]
       [type "Type/" Eq<Type>]
       [macro #+ Monad<Lux>]
       (macro [code])
       test)
  (luxc ["&" base]
        ["&;" scope]
        ["&;" module]
        (lang ["~" analysis])
        [analyser]
        (analyser ["@" procedure]
                  ["@;" common]))
  (../.. common)
  (test/luxc common))

(do-template [<name> <success> <failure>]
  [(def: (<name> procedure params output-type)
     (-> Text (List Code) Type Bool)
     (|> (&;with-expected-type output-type
           (@;analyse-procedure analyse procedure params))
         (macro;run (init-compiler []))
         (case> (#R;Success _)
                <success>

                (#R;Error _)
                <failure>)))]

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

(context: "Lux procedures"
  [[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"
  [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+ "bit count" (list subjectC) Nat))
      (test "Can perform bit 'and'."
            (check-success+ "bit and" (list subjectC paramC) Nat))
      (test "Can perform bit 'or'."
            (check-success+ "bit or" (list subjectC paramC) Nat))
      (test "Can perform bit 'xor'."
            (check-success+ "bit xor" (list subjectC paramC) Nat))
      (test "Can shift bit pattern to the left."
            (check-success+ "bit shift-left" (list subjectC paramC) Nat))
      (test "Can shift bit pattern to the right."
            (check-success+ "bit unsigned-shift-right" (list subjectC paramC) Nat))
      (test "Can shift signed bit pattern to the right."
            (check-success+ "bit shift-right" (list signedC paramC) Int))
      ))

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

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

(context: "Deg procedures"
  [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+ "deg +" (list subjectC paramC) Deg))
      (test "Can subtract degrees."
            (check-success+ "deg -" (list subjectC paramC) Deg))
      (test "Can multiply degrees."
            (check-success+ "deg *" (list subjectC paramC) Deg))
      (test "Can divide degrees."
            (check-success+ "deg /" (list subjectC paramC) Deg))
      (test "Can calculate remainder of degrees."
            (check-success+ "deg %" (list subjectC paramC) Deg))
      (test "Can test equality of degrees."
            (check-success+ "deg =" (list subjectC paramC) Bool))
      (test "Can compare degrees."
            (check-success+ "deg <" (list subjectC paramC) Bool))
      (test "Can obtain minimum degree."
            (check-success+ "deg min" (list) Deg))
      (test "Can obtain maximum degree."
            (check-success+ "deg max" (list) Deg))
      (test "Can convert degree to frac number."
            (check-success+ "deg to-frac" (list subjectC) Frac))
      (test "Can scale degree."
            (check-success+ "deg scale" (list subjectC natC) Deg))
      (test "Can calculate the reciprocal of a natural number."
            (check-success+ "deg reciprocal" (list natC) Deg))
      ))

(context: "Frac procedures"
  [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+ "frac +" (list subjectC paramC) Frac))
      (test "Can subtract frac numbers."
            (check-success+ "frac -" (list subjectC paramC) Frac))
      (test "Can multiply frac numbers."
            (check-success+ "frac *" (list subjectC paramC) Frac))
      (test "Can divide frac numbers."
            (check-success+ "frac /" (list subjectC paramC) Frac))
      (test "Can calculate remainder of frac numbers."
            (check-success+ "frac %" (list subjectC paramC) Frac))
      (test "Can test equality of frac numbers."
            (check-success+ "frac =" (list subjectC paramC) Bool))
      (test "Can compare frac numbers."
            (check-success+ "frac <" (list subjectC paramC) Bool))
      (test "Can obtain minimum frac number."
            (check-success+ "frac min" (list) Frac))
      (test "Can obtain maximum frac number."
            (check-success+ "frac max" (list) Frac))
      (test "Can obtain smallest frac number."
            (check-success+ "frac smallest" (list) Frac))
      (test "Can obtain not-a-number."
            (check-success+ "frac not-a-number" (list) Frac))
      (test "Can obtain positive infinity."
            (check-success+ "frac positive-infinity" (list) Frac))
      (test "Can obtain negative infinity."
            (check-success+ "frac negative-infinity" (list) Frac))
      (test "Can convert frac number to integer."
            (check-success+ "frac to-int" (list subjectC) Int))
      (test "Can convert frac number to degree."
            (check-success+ "frac to-deg" (list subjectC) Deg))
      (test "Can convert frac number to text."
            (check-success+ "frac encode" (list subjectC) Text))
      (test "Can convert text to frac number."
            (check-success+ "frac encode" (list encodedC) (type (Maybe Frac))))
      ))

(context: "Text procedures"
  [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+ "text =" (list subjectC paramC) Bool))
      (test "Compare texts in lexicographical order."
            (check-success+ "text <" (list subjectC paramC) Bool))
      (test "Can prepend one text to another."
            (check-success+ "text prepend" (list subjectC paramC) Text))
      (test "Can find the index of a piece of text inside a larger one that (may) contain it."
            (check-success+ "text index" (list subjectC paramC fromC) (type (Maybe Nat))))
      (test "Can query the size/length of a text."
            (check-success+ "text size" (list subjectC) Nat))
      (test "Can calculate a hash code for text."
            (check-success+ "text hash" (list subjectC) Nat))
      (test "Can replace a text inside of a larger one (once)."
            (check-success+ "text replace-once" (list subjectC paramC replacementC) Text))
      (test "Can replace a text inside of a larger one (all times)."
            (check-success+ "text replace-all" (list subjectC paramC replacementC) Text))
      (test "Can obtain the character code of a text at a given index."
            (check-success+ "text char" (list subjectC fromC) Nat))
      (test "Can clip a piece of text between 2 indices."
            (check-success+ "text clip" (list subjectC fromC toC) Text))
      ))

(context: "Array procedures"
  [[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;Array elemT))]]
  ($_ seq
      (test "Can create arrays."
            (check-success+ "array new" (list sizeC) arrayT))
      (test "Can get a value inside an array."
            (|> (&scope;with-scope ""
                  (&scope;with-local [var-name arrayT]
                    (&;with-expected-type elemT
                      (@;analyse-procedure analyse "array get"
                                           (list idxC
                                                 (code;symbol ["" var-name]))))))
                (macro;run (init-compiler []))
                (case> (#R;Success _)
                       true

                       (#R;Error _)
                       false)))
      (test "Can put a value inside an array."
            (|> (&scope;with-scope ""
                  (&scope;with-local [var-name arrayT]
                    (&;with-expected-type arrayT
                      (@;analyse-procedure analyse "array put"
                                           (list idxC
                                                 elemC
                                                 (code;symbol ["" var-name]))))))
                (macro;run (init-compiler []))
                (case> (#R;Success _)
                       true

                       (#R;Error _)
                       false)))
      (test "Can remove a value from an array."
            (|> (&scope;with-scope ""
                  (&scope;with-local [var-name arrayT]
                    (&;with-expected-type arrayT
                      (@;analyse-procedure analyse "array remove"
                                           (list idxC
                                                 (code;symbol ["" var-name]))))))
                (macro;run (init-compiler []))
                (case> (#R;Success _)
                       true

                       (#R;Error _)
                       false)))
      (test "Can query the size of an array."
            (|> (&scope;with-scope ""
                  (&scope;with-local [var-name arrayT]
                    (&;with-expected-type Nat
                      (@;analyse-procedure analyse "array size"
                                           (list (code;symbol ["" var-name]))))))
                (macro;run (init-compiler []))
                (case> (#R;Success _)
                       true

                       (#R;Error _)
                       false)))
      ))

(context: "Math procedures"
  [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))]

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

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

(context: "Atom procedures"
  [[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+ "atom new" (list elemC) atomT))
      (test "Can read the value of an atomic reference."
            (|> (&scope;with-scope ""
                  (&scope;with-local [var-name atomT]
                    (&;with-expected-type elemT
                      (@;analyse-procedure analyse "atom read"
                                           (list (code;symbol ["" var-name]))))))
                (macro;run (init-compiler []))
                (case> (#R;Success _)
                       true

                       (#R;Error _)
                       false)))
      (test "Can swap the value of an atomic reference."
            (|> (&scope;with-scope ""
                  (&scope;with-local [var-name atomT]
                    (&;with-expected-type Bool
                      (@;analyse-procedure analyse "atom compare-and-swap"
                                           (list elemC
                                                 elemC
                                                 (code;symbol ["" var-name]))))))
                (macro;run (init-compiler []))
                (case> (#R;Success _)
                       true

                       (#R;Error _)
                       false)))
      ))

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

(context: "IO procedures"
  [logC (|> (r;text +5) (:: @ map code;text))
   exitC (|> r;nat (:: @ map code;nat))]
  ($_ seq
      (test "Can log messages to standard output."
            (check-success+ "io log" (list logC) Unit))
      (test "Can log messages to standard output."
            (check-success+ "io error" (list logC) Bottom))
      (test "Can log messages to standard output."
            (check-success+ "io exit" (list exitC) Bottom))
      (test "Can query the current time (as milliseconds since epoch)."
            (check-success+ "io current-time" (list) Int))
      ))