-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_terraform.t
723 lines (637 loc) · 20.6 KB
/
test_terraform.t
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
-- SPDX-FileCopyrightText: 2024 René Hiemstra <[email protected]>
-- SPDX-FileCopyrightText: 2024 Torsten Keßler <[email protected]>
--
-- SPDX-License-Identifier: MIT
import "terraform"
local io = terralib.includec("stdio.h")
local base = require("base")
local concepts = require("concepts")
local alloc = require('alloc')
local dvector = require("dvector")
local size_t = uint64
local Real = concepts.Real
local Integer = concepts.Integer
local Float = concepts.Float
local Number = concepts.Number
import "terratest/terratest"
testenv "terraforming free functions" do
testset "concrete types" do
terraform foo(a : double, b : double)
return a * b + 2
end
terraform foo(a : float, b : double)
return a * b + 3
end
test foo(1., 2.) == 4.0
test foo(float(1), 2.) == 5.0
end
testset "global method dispatch parametric concrete types" do
terraform foo(a : T, b : T) where {T : Real}
return a * b
end
terraform foo(a : int, b : T) where {T : Real}
return a * b + 1
end
terraform foo(a : T, b : int) where {T : Real}
return a * b + 2
end
terraform foo(a : int, b : int)
return a * b + 3
end
--typical use-case in dispatch
test foo(1., 2.) == 2.0
test foo(1, 2.) == 3.0
test foo(1., 2) == 4.0
test foo(1, 2) == 5.0
end
testset "lobal method dispatch parametric concrete types" do
local terraform foo(a : T, b : T) where {T : Real}
return a * b
end
local terraform foo(a : int, b : T) where {T : Real}
return a * b + 1
end
local terraform foo(a : T, b : int) where {T : Real}
return a * b + 2
end
local terraform foo(a : int, b : int)
return a * b + 3
end
--typical use-case in dispatch
test foo(1., 2.) == 2.0
test foo(1, 2.) == 3.0
test foo(1., 2) == 4.0
test foo(1, 2) == 5.0
end
testset "global method dispatch parametric reference types" do
terraform foo(a : &T, b : &T) where {T : Real}
return @a * @b
end
terraform foo(a : &int, b : &T) where {T : Real}
return @a * @b + 1
end
terraform foo(a : &T, b : &int) where {T : Real}
return @a * @b + 2
end
terraform foo(a : &int, b : &int)
return @a * @b + 3
end
terracode
var x = 1.
var y = 2.
var i = 1
var j = 2
end
test foo(&x, &y) == 2.0
test foo(&i, &y) == 3.0
test foo(&x, &j) == 4.0
test foo(&i, &j) == 5.0
end
testset "global method dispatch mixed parametric concrete/reference types" do
terraform foo(a : T, b : &T) where {T : Real}
return a + @b
end
terraform foo(a : &T, b : T) where {T : Real}
return @a + b + 1
end
terracode
var x = 1.
var y = 2.
end
test foo(x, &y) == 3.0
test foo(&x, y) == 4.0
end
testset "local method dispatch parametric reference types" do
local terraform foo(a : &T, b : &T) where {T : Real}
return @a * @b
end
local terraform foo(a : &int, b : &T) where {T : Real}
return @a * @b + 1
end
local terraform foo(a : &T, b : &int) where {T : Real}
return @a * @b + 2
end
local terraform foo(a : &int, b : &int)
return @a * @b + 3
end
terracode
var x = 1.
var y = 2.
var i = 1
var j = 2
end
test foo(&x, &y) == 2.0
test foo(&i, &y) == 3.0
test foo(&x, &j) == 4.0
test foo(&i, &j) == 5.0
end
testset "nested reference types" do
terraform foo(a : &&T, b : &&T) where {T : Real}
return @(@a) * @(@b)
end
terraform foo(a : &&int, b : &&T) where {T : Real}
return @(@a) * @(@b) + 1
end
terraform foo(a : &&T, b : &&int) where {T : Real}
return @(@a) * @(@b) + 2
end
terraform foo(a : &&int, b : &&int)
return @(@a) * @(@b) + 3
end
terracode
var x, y, i, j = 1., 2., 1, 2
var rx, ry, ri, rj = &x, &y, &i, &j
end
test foo(&rx, &ry) == 2.0
test foo(&ri, &ry) == 3.0
test foo(&rx, &rj) == 4.0
test foo(&ri, &rj) == 5.0
end
testset "duck-typing" do
terraform foo(a, b)
return a * b + 1
end
terraform foo(a, b, c : T) where {T : Integer}
return a * b + c
end
test foo(2, 3) == 7
test foo(2, 3, 2) == 8
end
testset "varargs" do
terraform foo(a : int)
return a + 1
end
terraform foo(args ...)
var res = 1
escape
for i,v in ipairs(args.type.entries) do
local s = "_" .. tostring(i-1)
emit quote
res = res * args.[s]
end
end
end
return res
end
test foo(2) == 3
test foo(2, 3) == 6
test foo(2, 3, 4) == 24
end
testset "nearly ambiguous calls" do
--foo<T>
terraform foo(a : T, b : T) where {T : Float}
return a * b
end
--foo<T1,T2>
terraform foo(a : T1, b : T2) where {T1 : Float, T2 : Float}
return a * b + 1
end
--both of the following calls satisfy the concepts dispatch
--checks on all arguments.
test foo(1., 2.) == 2.0 --calling foo<T> and foo<T1,T2> are both valid. However,
--foo<T> is considered to be more specialized, so we pick this method.
test foo(float(1), 2.) == 3.0 --calling foo<T> would lead to a cast, which is
--not allowed, so we call foo<T1,T2>
end
testset "reference to local typealiases" do
terraform foo(a : size_t)
return a + 2
end
test foo(size_t(2)) == 4.0
end
testset "nested namespaces" do
local ns = {}
ns.Float = Float
ns.bar = {}
ns.bar.Float = Float
--foo<T>
terraform ns.bar.foo(a : T, b : T) where {T : ns.bar.Float}
return a * b
end
--foo<T1,T2>
terraform ns.bar.foo(a : T1, b : T2) where {T1 : ns.Float, T2 : ns.bar.Float}
return a * b + 1
end
test ns.bar.foo(1., 2.) == 2.0
test ns.bar.foo(float(1), 2.) == 3.0
end
testset "Access to parametric types in escape" do
terraform foo(a : T1, b : T2) where {T1 : Float, T2 : Float}
escape
assert(T1 == double and T2 == float)
end
return a * b + 1
end
test foo(2.0, float(3.0)) == 7
end
end
testenv "terraforming class methods" do
--dummy struct
local struct bar{
index : int
}
terracode
var mybar = bar{1}
end
testset "duck-typing" do
terraform bar:foo(a, b)
return a * b + self.index
end
terraform bar:foo(a, b, c : T) where {T : Integer}
return a * b + c + self.index
end
test mybar:foo(2, 3) == 7
test mybar:foo(2, 3, 2) == 9
end
testset "varargs" do
terraform bar:foo(a : int)
return a + self.index
end
terraform bar:foo(args ...)
var res = self.index
escape
for i,v in ipairs(args.type.entries) do
local s = "_" .. tostring(i-1)
emit quote
res = res * args.[s]
end
end
end
return res
end
test mybar:foo(2) == 3
test mybar:foo(2, 3) == 6
test mybar:foo(2, 3, 4) == 24
end
testset "static methods" do
terraform bar.sin(a : T) where {T : Real}
return a + 2
end
terraform bar:foo(a : T) where {T : Real}
return a + self.index
end
terraform bar.sin(a : T, b ...) where {T : Real}
return a + b._0 + 2
end
test bar.sin(2) == 4
test mybar:foo(2) == 3
test bar.sin(2,3) == 7
end
testset "concrete types" do
terraform bar:foo(a : double, b : double)
return a * b + self.index
end
terraform bar:foo(a : float, b : double)
return a * b + self.index + 1
end
test mybar:foo(1., 2.) == 3.0
test mybar:foo(float(1), 2.) == 4.0
end
testset "method dispatch parametric concrete types" do
terraform bar:foo(a : T, b : T) where {T : Real}
return a * b + self.index
end
terraform bar:foo(a : int, b : T) where {T : Real}
return a * b + 1 + self.index
end
terraform bar:foo(a : T, b : int) where {T : Real}
return a * b + 2 + self.index
end
terraform bar:foo(a : int, b : int)
return a * b + 3 + self.index
end
--typical use-case in dispatch
test mybar:foo(1., 2.) == 3.0
test mybar:foo(1, 2.) == 4.0
test mybar:foo(1., 2) == 5.0
test mybar:foo(1, 2) == 6.0
end
testset "method dispatch parametric reference types" do
terraform bar:foo(a : &T, b : &T) where {T : Real}
return @a * @b + self.index
end
terraform bar:foo(a : &int, b : &T) where {T : Real}
return @a * @b + 1 + self.index
end
terraform bar:foo(a : &T, b : &int) where {T : Real}
return @a * @b + 2 + self.index
end
terraform bar:foo(a : &int, b : &int)
return @a * @b + 3 + self.index
end
terracode
var x = 1.
var y = 2.
var i = 1
var j = 2
end
test mybar:foo(&x, &y) == 3.0
test mybar:foo(&i, &y) == 4.0
test mybar:foo(&x, &j) == 5.0
test mybar:foo(&i, &j) == 6.0
end
testset "method dispatch mixed parametric concrete/reference types" do
terraform bar:foo(a : T, b : &T) where {T : Real}
return a + @b + self.index
end
terraform bar:foo(a : &T, b : T) where {T : Real}
return @a + b + 1 + self.index
end
terracode
var x = 1.
var y = 2.
end
test mybar:foo(x, &y) == 4.0
test mybar:foo(&x, y) == 5.0
end
testset "nested reference types" do
terraform bar:foo(a : &&T, b : &&T) where {T : Real}
return @(@a) * @(@b) + self.index
end
terraform bar:foo(a : &&int, b : &&T) where {T : Real}
return @(@a) * @(@b) + 1 + self.index
end
terraform bar:foo(a : &&T, b : &&int) where {T : Real}
return @(@a) * @(@b) + 2 + self.index
end
terraform bar:foo(a : &&int, b : &&int)
return @(@a) * @(@b) + 3 + self.index
end
terracode
var x, y, i, j = 1., 2., 1, 2
var rx, ry, ri, rj = &x, &y, &i, &j
end
test mybar:foo(&rx, &ry) == 3.0
test mybar:foo(&ri, &ry) == 4.0
test mybar:foo(&rx, &rj) == 5.0
test mybar:foo(&ri, &rj) == 6.0
end
testset "nearly ambiguous calls" do
--foo<T>
terraform bar:foo(a : T, b : T) where {T : Float}
return a * b + self.index
end
--foo<T1,T2>
terraform bar:foo(a : T1, b : T2) where {T1 : Float, T2 : Float}
return a * b + 1 + self.index
end
--both of the following calls satisfy the concepts dispatch
--checks on all arguments.
test mybar:foo(1., 2.) == 3.0 --calling foo<T> and foo<T1,T2> are both valid. However,
--foo<T> is considered to be more specialized, so we pick this method.
test mybar:foo(float(1), 2.) == 4.0 --calling foo<T> would lead to a cast, which is
--not allowed, so we call foo<T1,T2>
end
testset "nested namespaces" do
local ns = {}
ns.Float = Float
ns.sin = {}
ns.sin.Float = Float
ns.sin.bar = bar
--foo<T>
terraform ns.sin.bar:foo(a : T, b : T) where {T : ns.sin.Float}
return a * b + self.index
end
--foo<T1,T2>
terraform ns.sin.bar:foo(a : T1, b : T2) where {T1 : ns.Float, T2 : ns.sin.Float}
return a * b + 1 + self.index
end
terracode
var mybar = ns.sin.bar{1}
end
test mybar:foo(1., 2.) == 3.0
test mybar:foo(float(1), 2.) == 4.0
end
testset "use in parametric class definition" do
local Bar = function(T)
local struct bar{
x : T
}
terraform bar:eval(x : S) where {S}
return x + self.x
end
return bar
end
local bbar = Bar(double)
terracode
var mybar = bbar{1}
end
test mybar:eval(1.) == 2
end
end
testenv "defining regular concepts" do
concept MyStack
Self.methods.length = {&Self} -> concepts.Integral
Self.methods.get = {&Self, concepts.Integral} -> Any
Self.methods.set = {&Self, concepts.Integral , Any} -> {}
end
test [concepts.isconcept(MyStack) == true]
concept VectorAny
local S = MyStack
Self:inherit(S)
Self.methods.swap = {&Self, &S} -> {}
Self.methods.copy = {&Self, &S} -> {}
end
test [concepts.isconcept(VectorAny) == true]
concept VectorNumber
Self:inherit(VectorAny)
local S = MyStack
Self.methods.fill = {&Self, Number} -> {}
Self.methods.clear = {&Self} -> {}
Self.methods.sum = {&Self} -> Number
Self.methods.axpy = {&Self, Number, &S} -> {}
Self.methods.dot = {&Self, &S} -> Number
end
test [concepts.isconcept(VectorNumber) == true]
concept VectorFloat
Self:inherit(VectorNumber)
Self.methods.norm = {&Self} -> Float
end
test [concepts.isconcept(VectorFloat) == true]
testset "inheritance and specialization" do
test [VectorAny(MyStack) == false]
test [concepts.is_specialized_over(&VectorAny, &MyStack)]
test [MyStack(VectorAny) == true]
test [MyStack(VectorNumber) == true]
test [MyStack(VectorFloat) == true]
test [VectorNumber(MyStack) == false]
test [VectorAny(VectorNumber) == true]
test [VectorNumber(VectorAny) == false]
test [concepts.is_specialized_over(&VectorNumber, &VectorAny)]
test [VectorFloat(MyStack) == false]
test [VectorAny(VectorNumber) == true]
test [VectorAny(VectorFloat) == true]
test [VectorAny(VectorNumber) == true]
test [VectorNumber(VectorFloat) == true]
test [VectorFloat(VectorNumber) == false]
test [VectorFloat(VectorAny) == false]
test [concepts.is_specialized_over(&VectorFloat, &VectorNumber)]
end
end
testenv "defining parametrized concepts" do
concept MyStack(T) where {T}
Self.methods.length = {&Self} -> concepts.Integral
Self.methods.get = {&Self, concepts.Integral} -> T
Self.methods.set = {&Self, concepts.Integral , T} -> {}
end
test [concepts.isparametrizedconcept(MyStack) == true]
concept Vector(T) where {T}
local S = MyStack(T)
Self:inherit(S)
Self.methods.swap = {&Self, &S} -> {}
Self.methods.copy = {&Self, &S} -> {}
end
concept Vector(T) where {T : Number}
local S = MyStack(T)
Self.methods.fill = {&Self, T} -> {}
Self.methods.clear = {&Self} -> {}
Self.methods.sum = {&Self} -> T
Self.methods.axpy = {&Self, T, &S} -> {}
Self.methods.dot = {&Self, &S} -> T
end
concept Vector(T) where {T : Float}
Self.methods.norm = {&Self} -> T
end
test [concepts.isparametrizedconcept(Vector) == true]
testset "Dispatch on Any" do
local S = MyStack(concepts.Any)
local V = Vector(concepts.Any)
test [S(V) == true]
test [V(S) == false]
test [concepts.is_specialized_over(&V, &S)]
end
testset "Dispatch on Integers" do
local S = MyStack(concepts.Integer)
local V1 = Vector(concepts.Any)
local V2 = Vector(concepts.Integer)
test [S(V2) == true]
test [S(V1) == false]
test [V2(S) == false]
test [V1(V2) == true]
test [V2(V1) == false]
test [concepts.is_specialized_over(&V2, &V1)]
end
testset "Dispatch on Float" do
local S = MyStack(concepts.Float)
local V1 = Vector(concepts.Any)
local V2 = Vector(concepts.Number)
local V3 = Vector(concepts.Float)
test [S(V1) == false]
test [S(V2) == false]
test [S(V3) == true]
test [V3(S) == false]
test [V1(V2) == true]
test [V1(V3) == true]
test [V1(V2) == true]
test [V2(V3) == true]
test [V3(V2) == false]
test [V3(V1) == false]
test [concepts.is_specialized_over(&V3, &V2)]
end
testset "Compile-time integer and string dispatch" do
local A = concepts.newconcept("A")
A.traits.isfoo = "A"
local B = concepts.newconcept("B")
B.traits.isfoo = "B"
local C = concepts.newconcept("C")
C.traits.isfoo = "C"
concept Foo(N) where {N : 1}
assert(N == 1)
Self:inherit(A)
end
concept Foo(N) where {N : 2}
assert(N == 2)
Self:inherit(B)
end
concept Foo(N) where {N : 3}
assert(N == 3)
Self:inherit(C)
end
concept Foo(S) where {S : "hello"}
assert(S == "hello")
Self.traits.isfoo = S
end
local Foo1 = Foo(1)
local Foo2 = Foo(2)
local Foo3 = Foo(3)
local Foo4 = Foo("hello")
test [A(Foo1) == true]
test [Foo1(A) == true]
test [B(Foo2) == true]
test [Foo2(B) == true]
test [A(B) == false]
test [B(A) == false]
test [C(Foo3) == true]
test [Foo3(C) == true]
test [C(A) == false]
test [C(B) == false]
test [A(C) == false]
test [B(C) == false]
test [Foo4.traits.isfoo == "hello"]
end
testset "Multiple Arguments" do
concept Matrix(T1, T2) where {T1 : Any, T2 : Any}
Self.methods.sum = {&Self} -> {}
end
concept Matrix(T, T) where {T : Any}
Self.methods.special_sum = {&Self} -> {}
end
local Generic = Matrix(concepts.Float, concepts.Integer)
local Special = Matrix(concepts.Float, concepts.Float)
test [Generic(Special) == true]
test [Special(Generic) == false]
end
testset "Multipe inheritance" do
local SVec = concepts.parametrizedconcept("SVec")
concept SVec()
Self.traits.length = concepts.traittag
Self.methods.length = &Self -> concepts.Integral
end
concept SVec(T) where {T : concepts.Number}
Self.methods.axpy = {&Self, T, &Self} -> {}
end
concept SVec(T, N) where {T: concepts.Float, N: concepts.Value}
assert(type(N) == "number")
Self.traits.length = N
end
concept SVec(T, N) where {T : concepts.Float, N : 3}
assert(N == 3)
Self.methods.cross = {&Self, &Self} -> T
end
local struct H(base.AbstractBase) {}
H.methods.length = &H -> int32
local struct G(base.AbstractBase) {}
G.traits.length = 2
G.methods.length = &G -> int64
local SVecAny = SVec()
test [SVecAny(H) == false]
test [SVecAny(G) == true]
local struct I(base.AbstractBase) {}
I.traits.length = 2
I.methods.length = &I -> uint64
I.methods.axpy = {&I, int32, &I} -> {}
local SVecInt = SVec(concepts.Integer)
test [SVecInt(G) == false]
test [SVecInt(I) == true]
local struct D(base.AbstractBase) {}
D.traits.length = 3
D.methods.length = &D -> uint64
D.methods.axpy = {&D, double, &D} -> {}
D.methods.cross = {&D, &D} -> {}
local struct D(base.AbstractBase) {}
D.traits.length = 3
D.methods.length = &D -> uint64
D.methods.axpy = {&D, double, &D} -> {}
D.methods.cross = {&D, &D} -> {}
local struct E(base.AbstractBase) {}
E.traits.length = 4
E.methods.length = &E -> uint64
E.methods.axpy = {&E, double, &E} -> {}
E.methods.cross = {&E, &E} -> {}
local SVec3D = SVec(concepts.Float, 3)
test [SVec3D(G) == false]
test [SVec3D(I) == false]
test [SVec3D(D) == true]
test [SVec3D(E) == false]
end
end