-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCompilingCodeTests.cs
More file actions
708 lines (614 loc) · 19.2 KB
/
Copy pathCompilingCodeTests.cs
File metadata and controls
708 lines (614 loc) · 19.2 KB
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
using System.Collections.Immutable;
using Draco.Compiler.Api.Syntax;
using static Draco.Compiler.Tests.TestUtilities;
namespace Draco.Compiler.Tests.EndToEnd;
public sealed class CompilingCodeTests : EndToEndTestsBase
{
[Fact]
public void Max()
{
var assembly = Compile("""
public func max(a: int32, b: int32): int32 = if (a > b) a else b;
""");
var inputs = new[] { (0, 0), (1, 0), (0, 1), (5, 0), (5, 4), (4, 5) };
foreach (var (a, b) in inputs)
{
var maxi = Invoke<int>(assembly, "max", a, b);
Assert.Equal(Math.Max(a, b), maxi);
}
}
[Fact]
public void Abs()
{
var assembly = Compile("""
public func abs(n: int32): int32 = if (n > 0) n else -n;
""");
var inputs = new[] { 0, 1, -1, 3, 8, -3, -5 };
foreach (var n in inputs)
{
var absi = Invoke<int>(assembly, "abs", n);
Assert.Equal(Math.Abs(n), absi);
}
}
[Fact]
public void Between()
{
var assembly = Compile("""
public func between(n: int32, a: int32, b: int32): bool = a <= n <= b;
""");
var trueInputs = new[] { (0, 0, 0), (0, 0, 1), (0, -1, 0), (0, 0, 5), (1, 0, 5), (4, 0, 5), (5, 0, 5) };
foreach (var (n, a, b) in trueInputs)
{
var isBetween = Invoke<bool>(assembly, "between", n, a, b);
Assert.True(isBetween);
}
var falseInputs = new[] { (-1, 0, 0), (2, 0, 1), (1, -1, 0), (6, 0, 5), (-1, 0, 5), (10, 0, 5), (7, 0, 5) };
foreach (var (n, a, b) in falseInputs)
{
var isBetween = Invoke<bool>(assembly, "between", n, a, b);
Assert.False(isBetween);
}
}
[Fact]
public void Negate()
{
var assembly = Compile("""
public func negate(n: int32): int32 = if (n < 0) n else -n;
""");
var inputs = new[] { 0, 1, -1, 3, 8, -3, -5 };
foreach (var n in inputs)
{
var neg = Invoke<int>(assembly, "negate", n);
Assert.Equal(n < 0 ? n : -n, neg);
}
}
[Fact]
public void Power()
{
var assembly = Compile("""
public func power(n: int32, exponent: int32): int32 = {
var i = 1;
var result = n;
while (i < exponent){
result *= n;
i += 1;
}
result
};
""");
var inputs = new[] { (1, 3), (5, 2), (-2, 4), (3, 3), (8, 2) };
foreach (var (n, exp) in inputs)
{
var pow = Invoke<int>(assembly, "power", n, exp);
Assert.Equal(Math.Pow(n, exp), pow);
}
}
[Fact]
public void PowerWithFloat64()
{
var assembly = Compile("""
public func power(n: float64, exponent: int32): float64 = {
var i = 1;
var result = n;
while (i < exponent){
result *= n;
i += 1;
}
result
};
""");
var inputs = new[] { (1.5, 3), (5.28, 2), (-2.5, 4), (3, 3), (8.847, 2) };
foreach (var (n, exp) in inputs)
{
var pow = Invoke<double>(assembly, "power", n, exp);
Assert.Equal(Math.Pow(n, exp), pow, 5);
}
}
[Fact]
public void LazyAnd()
{
var assembly = Compile("""
public func foo(nx2: bool, nx3: bool): int32 = {
var result = 1;
nx2 and { result *= 2; nx3 } and { result *= 3; false };
result
};
""");
var r1 = Invoke<int>(assembly, "foo", false, false);
var r2 = Invoke<int>(assembly, "foo", false, true);
var r3 = Invoke<int>(assembly, "foo", true, false);
var r4 = Invoke<int>(assembly, "foo", true, true);
Assert.Equal(1, r1);
Assert.Equal(1, r2);
Assert.Equal(2, r3);
Assert.Equal(6, r4);
}
[Fact]
public void LazyOr()
{
var assembly = Compile("""
public func foo(nx2: bool, nx3: bool): int32 = {
var result = 1;
nx2 or { result *= 2; nx3 } or { result *= 3; false };
result
};
""");
var r1 = Invoke<int>(assembly, "foo", false, false);
var r2 = Invoke<int>(assembly, "foo", false, true);
var r3 = Invoke<int>(assembly, "foo", true, false);
var r4 = Invoke<int>(assembly, "foo", true, true);
Assert.Equal(6, r1);
Assert.Equal(2, r2);
Assert.Equal(1, r3);
Assert.Equal(1, r4);
}
[Fact]
public void RecursiveFactorial()
{
var assembly = Compile("""
public func fact(n: int32): int32 =
if (n == 0) 1
else n * fact(n - 1);
""");
var results = new[] { 1, 1, 2, 6, 24, 120, 720 };
for (var i = 0; i < 7; ++i)
{
var facti = Invoke<int>(assembly, "fact", i);
Assert.Equal(results[i], facti);
}
}
[Fact]
public void RecursiveFibonacci()
{
var assembly = Compile("""
public func fib(n: int32): int32 =
if (n < 2) 1
else fib(n - 1) + fib(n - 2);
""");
var results = new[] { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 };
for (var i = 0; i < 10; ++i)
{
var fibi = Invoke<int>(assembly, "fib", i);
Assert.Equal(results[i], fibi);
}
}
[Fact]
public void IterativeSum()
{
var assembly = Compile("""
public func sum(start: int32, end: int32): int32 {
var i = start;
var s = 0;
while (i < end) {
i += 1;
s += i;
}
return s;
}
""");
var results = new[] { 0, 1, 3, 6, 10, 15, 21, 28, 36, 45 };
for (var i = 0; i < 10; ++i)
{
var sumi = Invoke<int>(assembly, "sum", 0, i);
Assert.Equal(results[i], sumi);
}
}
[Fact]
public void Globals()
{
var assembly = Compile("""
var x = 0;
func bar() { x += 1; }
public func foo(): int32 {
bar();
bar();
bar();
return x;
}
""");
var x = Invoke<int>(assembly, "foo");
Assert.Equal(3, x);
}
[Fact]
public void NonzeroGlobals()
{
var assembly = Compile("""
var x = 123;
func bar() { x += 1; }
public func foo(): int32 {
bar();
bar();
bar();
return x;
}
""");
var x = Invoke<int>(assembly, "foo");
Assert.Equal(126, x);
}
[Fact]
public void ComplexInitializerGlobals()
{
var assembly = Compile("""
public func foo(): int32 = x;
var x = add(1, 2) + 1 + 2 + 3;
func add(x: int32, y: int32): int32 = 2 * (x + y);
""");
var x = Invoke<int>(assembly, "foo");
Assert.Equal(12, x);
}
[Fact]
public void BreakAndContinue()
{
var assembly = Compile("""
public func foo(): int32 {
var s = 0;
var i = 0;
while (true) {
if (s > 30) goto break;
i += 1;
if (i mod 2 == 1) goto continue;
s += i;
}
return s;
}
""");
var x = Invoke<int>(assembly, "foo");
Assert.Equal(42, x);
}
[Fact]
public void MultiLineStringCutoff()
{
var assembly = Compile(""""
public func foo(): string{
return """
Hello
World!
""";
}
"""");
var x = Invoke<string>(assembly, "foo");
Assert.Equal("""
Hello
World!
""", x);
}
[Fact]
public void MultiLineStringInterpolation()
{
var assembly = Compile(""""
public func foo(): string{
return """
Hello \{1 + 2} World!
""";
}
"""");
var x = Invoke<string>(assembly, "foo");
Assert.Equal("Hello 3 World!", x);
}
[Fact]
public void MultiLineStringLineContinuation()
{
var assembly = Compile(""""
public func foo(): string{
return """
Hello\
World!
""";
}
"""");
var x = Invoke<string>(assembly, "foo");
Assert.Equal("Hello World!", x);
}
[Fact]
public void FunctionsWithExplicitGenerics()
{
var assembly = Compile(""""
func identity<T>(x: T): T = x;
func first<T, U>(a: T, b: U): T = identity<T>(a);
func second<T, U>(a: T, b: U): U = identity<U>(b);
public func foo(n: int32, m: int32): int32 =
first<int32, string>(n, "Hello") + second<bool, int32>(false, m);
"""");
var x = Invoke<int>(assembly, "foo", 2, 3);
Assert.Equal(5, x);
}
[Fact]
public void FunctionsWithImplicitGenerics()
{
var assembly = Compile(""""
func identity<T>(x: T): T = x;
func first<T, U>(a: T, b: U): T = identity(a);
func second<T, U>(a: T, b: U): U = identity(b);
public func foo(n: int32, m: int32): int32 =
first(n, "Hello") + second(false, m);
"""");
var x = Invoke<int>(assembly, "foo", 2, 3);
Assert.Equal(5, x);
}
[Fact]
public void ModuleFunctionCall()
{
var bar = SyntaxTree.Parse("""
public func bar(): int32{
return FooTest.foo();
}
""", ToPath("Tests", "bar.draco"));
var foo = SyntaxTree.Parse("""
internal func foo(): int32 = x;
val x = 5;
""", ToPath("Tests", "FooTest", "foo.draco"));
var assembly = Compile(ToPath("Tests"), bar, foo);
var x = Invoke<int>(assembly, "Tests", "bar");
Assert.Equal(5, x);
}
[Fact]
public void ModuleGlobalAccess()
{
var bar = SyntaxTree.Parse("""
public func bar(): int32{
return FooTest.x;
}
""", ToPath("Tests", "bar.draco"));
var foo = SyntaxTree.Parse("""
public val x = 5;
""", ToPath("Tests", "FooTest", "foo.draco"));
var assembly = Compile(ToPath("Tests"), bar, foo);
var x = Invoke<int>(assembly, "Tests", "bar");
Assert.Equal(5, x);
}
[Fact]
public void NestedModuleAccess()
{
var foo = SyntaxTree.Parse("""
public func foo(): int32 = 5;
""", ToPath("Tests", "FooTest", "foo.draco"));
var assembly = Compile(ToPath("Tests"), foo);
var x = Invoke<int>(assembly, "Tests.FooTest", "foo");
Assert.Equal(5, x);
}
[Fact]
public void GenericMemberMethodCall()
{
var csReference = CompileCSharpToStream("""
public class IdentityProvider
{
public T Identity<T>(T x) => x;
}
""");
var foo = SyntaxTree.Parse("""
public func foo(): int32 {
val provider = IdentityProvider();
return provider.Identity<int32>(2) + provider.Identity(123);
}
""");
var assembly = Compile(
root: null,
syntaxTrees: ImmutableArray.Create(foo),
additionalPeReferences: ImmutableArray.Create(("Test.dll", csReference)));
var x = Invoke<int>(assembly, "foo");
Assert.Equal(125, x);
}
[Fact]
public void PropertiesCompoundAssignment()
{
var csReference = CompileCSharpToStream("""
public class FooTest
{
public static int StaticProp { get; set; } = 5;
public int NonStaticProp { get; set; } = 4;
}
""");
var foo = SyntaxTree.Parse("""
public func foo(): int32 {
var test = FooTest();
test.NonStaticProp += 2;
FooTest.StaticProp += 3;
return test.NonStaticProp += FooTest.StaticProp;
}
""");
var assembly = Compile(
root: null,
syntaxTrees: ImmutableArray.Create(foo),
additionalPeReferences: ImmutableArray.Create(("Test.dll", csReference)));
var x = Invoke<int>(assembly, "foo");
Assert.Equal(14, x);
}
[Fact]
public void MemberFields()
{
var csReference = CompileCSharpToStream(
"""
public class FooTest
{
public int number = 3;
}
""");
var foo = SyntaxTree.Parse("""
public func foo(): int32 {
var test = FooTest();
test.number += 2;
return test.number;
}
""");
var assembly = Compile(
root: null,
syntaxTrees: ImmutableArray.Create(foo),
additionalPeReferences: ImmutableArray.Create(("Test.dll", csReference)));
var x = Invoke<int>(assembly, "foo");
Assert.Equal(5, x);
}
[Fact]
public void StaticFields()
{
var csReference = CompileCSharpToStream(
"""
public class FooTest
{
public static int number = 3;
}
""");
var foo = SyntaxTree.Parse("""
public func foo(): int32 {
FooTest.number += 2;
return FooTest.number;
}
""");
var assembly = Compile(
root: null,
syntaxTrees: ImmutableArray.Create(foo),
additionalPeReferences: ImmutableArray.Create(("Test.dll", csReference)));
var x = Invoke<int>(assembly, "foo");
Assert.Equal(5, x);
}
[Fact]
public void MergeArrays()
{
var assembly = Compile("""
func merge<T>(a: Array<T>, b: Array<T>): Array<T> {
val result = Array(a.Length + b.Length);
var offs = 0;
while (offs < a.Length) {
result[offs] = a[offs];
offs += 1;
}
while (offs - a.Length < b.Length) {
result[offs] = b[offs - a.Length];
offs += 1;
}
return result;
}
public func merge_int(a: Array<int32>, b: Array<int32>): Array<int32> = merge(a, b);
""");
var input1 = new[] { 1, 3 };
var input2 = new[] { 2, 4 };
var expectedOutput = new[] { 1, 3, 2, 4 };
var output = Invoke<int[]>(assembly, "merge_int", input1, input2);
Assert.True(output.SequenceEqual(expectedOutput));
}
[Fact]
public void BubbleSortArray()
{
var assembly = Compile("""
public func bubblesort(a: Array<int32>) {
var i = 1;
while (i < a.Length) {
var j = 0;
while (j < a.Length - i) {
if (a[j + 1] < a[j]) {
val tmp = a[j];
a[j] = a[j + 1];
a[j + 1] = tmp;
}
j += 1;
}
i += 1;
}
}
""");
var input = new[] { 17, 1, 12, 7, 10, 10, 6, 12, 2, 8 };
var output = new[] { 1, 2, 6, 7, 8, 10, 10, 12, 12, 17 };
Invoke<object?>(assembly, "bubblesort", input);
Assert.True(input.SequenceEqual(output));
}
[Fact]
public void InCodeModuleUsage()
{
var assembly = Compile(""""
public func foo(): string = FooModule.GetFoo();
module FooModule{
public func GetFoo(): string = "foo";
}
"""");
var x = Invoke<string>(assembly, "foo");
Assert.Equal("foo", x);
}
[Fact]
public void InCodeModuleUsageImportingInsideModule()
{
var assembly = Compile(""""
public func foo(): string = FooModule.Hello();
module FooModule {
import System.Text;
public func Hello(): string{
var sb = StringBuilder();
sb.Append("Hello, World!");
return sb.ToString();
}
}
"""");
var x = Invoke<string>(assembly, "foo");
Assert.Equal("Hello, World!", x);
}
[Fact]
public void VariadicArgsSum()
{
var assembly = Compile(""""
func sum(...ns: Array<int32>): int32 {
var result = 0;
var i = 0;
while (i < ns.Length) {
result += ns[i];
i += 1;
}
return result;
}
public func get_sum(): int32 = sum(1, 2, 3, 4, 5);
"""");
var x = Invoke<int>(assembly, "get_sum");
Assert.Equal(15, x);
}
[Fact]
public void MultidimensionalArrays()
{
var assembly = Compile(""""
func make(a: int32, b: int32, c: int32, d: int32): Array2D<int32> {
val res = Array2D(2, 2);
res[0, 0] = a;
res[1, 0] = b;
res[0, 1] = c;
res[1, 1] = d;
return res;
}
func add_to_main_diagonal(a: Array2D<int32>, n: int32) {
a[0, 0] += n;
a[1, 1] += n;
}
func sum(a: Array2D<int32>): int32 = a[0, 0] + a[1, 0] + a[0, 1] + a[1, 1];
public func get_result(): int32 {
val m = make(1, 2, 3, 4);
add_to_main_diagonal(m, 7);
return sum(m);
}
"""");
var x = Invoke<int>(assembly, "get_result");
Assert.Equal(24, x);
}
[Fact]
public void SingleInterpolatedElement()
{
var assembly = Compile(""""
public func stringify(a: int32): string = "\{a}";
"""");
var x = Invoke<string>(assembly, "stringify", 123);
Assert.Equal("123", x);
}
[Fact]
public void BasicMatchOnNumbers()
{
var assembly = Compile(""""
public func categorize(a: int32): string = match (a) {
1 -> "one";
2 -> "two";
_ if (a rem 2 == 0) -> "even";
_ -> "odd";
};
"""");
var input = new[] { 1, 2, 3, 4, 7, 8 };
var output = new[] { "one", "two", "odd", "even", "odd", "even" };
foreach (var (inp, expectedOut) in input.Zip(output))
{
var got = Invoke<string>(assembly, "categorize", inp);
Assert.Equal(expectedOut, got);
}
}
}