-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLanguageTests.cpp
6702 lines (5132 loc) · 291 KB
/
LanguageTests.cpp
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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*=====================================================================
LanguageTests.cpp
-----------------
Copyright Glare Technologies Limited 2018 -
=====================================================================*/
#include "LanguageTests.h"
#include "LanguageTestUtils.h"
#include "FuzzTests.h"
#include "wnt_Lexer.h"
#include "wnt_ArrayLiteral.h"
#include "wnt_VectorLiteral.h"
#include "wnt_TupleLiteral.h"
#include "wnt_IfExpression.h"
#include "wnt_FunctionExpression.h"
#include "wnt_LetBlock.h"
#include "wnt_LetASTNode.h"
#include <utils/Timer.h>
#include <utils/Task.h>
#include <utils/TaskManager.h>
#include <utils/MemMappedFile.h>
#include <utils/FileUtils.h>
#include <utils/ConPrint.h>
#include <utils/StringUtils.h>
#include <utils/Vector.h>
#include <unordered_set>
#if BUILD_TESTS
namespace Winter
{
LanguageTests::LanguageTests()
{}
LanguageTests::~LanguageTests()
{}
#define testAssert(expr) (doTestAssert((expr), (#expr), (__LINE__), (__FILE__)))
void doTestAssert(bool expr, const char* test, long line, const char* file)
{
if(!expr)
{
stdErrPrint("Test assertion failed: " + std::string(file) + ", line " + toString((int)line) + ":\n" + std::string(test));
#if defined(_WIN32)
__debugbreak();
#endif
exit(1);
}
}
/*
def main(int x) int :
if x < 5 then
10
else
20
def main(int x) int :
if x < 5
10
else
20
def main(int x) int : if x < 5 then 10 else 20
def main(int x) int : if (x < 5) then 10 else 20
def main(int x) int : if (x < 5) then 10 else 20
def main(int x) int : if (x < 5, 10, 20)
parse 'if ('
parse expression 'x < 5'
if next token == ')', then it's if-then-else.
if next token == ',', then it's if(,,)
*/
/*
static Value* identity(Value* v)
{
return v;
}
static Value* h(Value* v, float x)
{
if(x < 0.5)
{
Value* ret = new IntValue(99);
ret->incRefCount(); // Set ref count to 1
return ret;
}
else
return v;
}
static int length(Value* v)
{
return v->toString().size();
}
// no ref counting on v done in function
static int f(Value* v)
{
return f(v);
}
static int bleh(float x)
{
Value* v = new IntValue(99);
v->incRefCount(); // Set v ref count to 1
Value* v2 = h(v, x); // If we don't know how h is defined, then we may have v2 = v, or we may have v2 = new value
// If it just returned v, we have one allocation in total. Otherwise we have two.
// There is no way of knowing at compile time.
const int len = length(v2);
delete v;
}
static int meh()
{
Value* v = new IntValue(99);
v->incRefCount(); // Set v ref count to 1
const int len = length(v);
delete v; // No ref count check required. Still need to delete though
}
// Function meh2 akes a ref-counted value, no reference counting code required for the argument though, as doesn't return a value of the same type.
static int meh2(Value* v)
{
const int len = length(v);
return len + 2;
}
// Can do another optimisation: Since meh3 doesn't take a Value as argument, we know that the return value must have refcount = 1.
static Value* meh3(int x)
{
Value* v = new IntValue(99);
v->incRefCount(); // Set v ref count to 1
return v;
}
// So take something like:
static int useKnownReturnRefCountOptimsiation(int x)
{
Value* v = meh3(x);
// We know rc(v) = 1
const int len = length(v);
// We still know rc(v) = 1
// At this point we can just delete v;
assert(v->getRefCount() == 1);
v->decRefCount();
delete v;
return len + 2;
}*/
void LanguageTests::doLLVMInit()
{
testMainFloatArg("def main(float x) float : sqrt(4)", 2.0f, 2.0f);
}
static void doCKeywordTests()
{
// ===================================================================
// Test using variable names etc.. that are OpenCL C keywords
// ===================================================================
testMainFloatArg("def main(float switch) float : switch", 10.0f, 10.f); // "switch" is an OpenCL C keyword
testMainFloatArg("def main(float float16) float : float16", 10.0f, 10.f);
// Test let var names
testMainFloatArg("def main(float x) float : let switch = x in switch", 10.0f, 10.f);
// Test function names. Actually because of type decoration, function names (always?) won't clash with OpenCL C keywords.
testMainFloatArg("def switch(float x) float : x def main(float x) : switch(x)", 10.0f, 10.f);
testMainFloatArg("def switch(float x) !noinline float : x def main(float x) : switch(x)", 10.0f, 10.f);
// Test structure names
testMainFloatArg(
"struct switch { float x } \n"
"def main(float x) float : switch(x).x", 10.0f, 10.f);
// Test use of 'complex' struct (complex is a reserved word in OpenCL and can't be used)
testMainFloatArg(
"struct complex { float re, float im } \n"
" def f(complex c) !noinline float : c.re \n"
" def main(float x) float : let c = complex(x, x + 1.0) in f(c)",
10.0f, 10.f);
}
static void doUnsignedIntegerTests()
{
// ===================================================================
// Test unsigned integers
// ===================================================================
testMainUInt32Arg("def main(uint32 x) : x", 1, 1);
testMainUInt32Arg("def main(uint32 x) : 10u", 1, 10);
testMainUInt32Arg("def main(uint32 x) : 4294967295u", 1, 4294967295u);
// TODO: this should fail to compile:
//testMainUInt32Arg("def main(uint32 x) : 429496729500000u", 1, 4294967295u);
// Test hex literals
testMainUInt32Arg("def main(uint32 x) : 0x12345678u", 1, 0x12345678u);
testMainUInt32Arg("def main(uint32 x) : 0x90abcdefu", 1, 0x90abcdefu);
testMainUInt32Arg("def main(uint32 x) : 0xABCDEF01u", 1, 0xABCDEF01u);
testMainUInt32Arg("def main(uint32 x) : 0xFFFFFFFFu", 1, 0xFFFFFFFFu);
testMainUInt32Arg("def main(uint32 x) : 10u32", 1, 10);
testMainUInt32Arg("def main(uint32 x) : x + 10u32", 1, 11);
testMainUInt32Arg("def main(uint32 x) : x + 1u32", 4294967294u, 4294967295u);
testMainUInt32Arg("def main(uint32 x) : x * 10u32", 2, 20);
}
static void doBitWiseOpTests()
{
// ===================================================================
// Test bitwise AND
// ===================================================================
testMainUInt32Arg("def main(uint32 x) : x & 7u", 0xFFFFFFFF, 7);
// ===================================================================
// Test bitwise OR
// ===================================================================
testMainUInt32Arg("def main(uint32 x) : x | 7u", 8, 15);
// ===================================================================
// Test bitwise XOR
// ===================================================================
testMainUInt32Arg("def main(uint32 x) : x ^ 15u", 7, 8);
// ===================================================================
// Test left shift
// ===================================================================
testMainUInt32Arg("def main(uint32 x) : x << 10u", 3, 3 << 10);
// ===================================================================
// Test right shift
// ===================================================================
testMainUInt32Arg("def main(uint32 x) : x >> 2u", 356, 356 >> 2);
}
static void doHexLiteralParsingTests()
{
// ===================================================================
// Test hex literal parsing (some more)
// ===================================================================
testMainIntegerArgInvalidProgram("def main(int x) : 0x");
testMainIntegerArgInvalidProgram("def main(int x) : 0xg");
testMainIntegerArgInvalidProgram("def main(int x) : 0xG");
testMainIntegerArgInvalidProgram("def main(int x) : 0x0x");
testMainIntegerArgInvalidProgram("def main(int x) : 0x-");
}
static void testVariableShadowing()
{
// ===================================================================
// Test variable shadowing
// ===================================================================
testMainFloatArg("def main(float x) float : \n\
let \n\
y = x + 1 \n\
in \n\
let \n\
y = x + 2 \n\
in \n\
y \n\
", 0.0f, 2.0f);
testMainFloatArg("def main(float x) float : \n\
let \n\
y = x \n\
in \n\
let \n\
y1 = y + 1 \n\
y = y1 + 2 \n\
in \n\
y \n\
", 0.0f, 3.0f);
testMainFloatArgInvalidProgram("def main(float x) float : \n\
let \n\
y = x + 1 \n\
in \n\
let \n\
y = y + 10 \n\
in \n\
y \n\
");
testMainFloatArgInvalidProgram("def main(float x) float : let x = x in x");
testMainFloatArgInvalidProgram("def main(float x) float : let x = x + 1 in x");
}
static void testFunctionInlining()
{
// ===================================================================
// Test function inlining
// ===================================================================
Winter::TestResults results;
// Start with something simple, test that f is inlined.
results = testMainIntegerArg("def f(int y) : y def main(int x) int : f(x)", 1, 1); // f should be inlined.
testAssert(results.maindef->body->nodeType() == ASTNode::VariableASTNodeType);
/*
Check that clamp is not inlined, since it duplicates an expensive arg.
max gets inlined into clmap, so we have
def clamp(real x, real lo, real hi) real : min(hi, if(x > lo, x, lo)) \n\
so arg x appears twice
*/
results = testMainFloatArg(
"def min(real a, real b) real : if(a < b, a, b) \n\
def max(real a, real b) real : if(a > b, a, b) \n\
def clamp(real x, real lo, real hi) real : min(hi, max(x, lo)) \n\
def main(float x) float : clamp(sin(x), 0.f, 1.f)", 0.3f, std::sin(0.3f));
testAssert(results.maindef->body->nodeType() == ASTNode::FunctionExpressionType);
testAssert(results.maindef->body.downcastToPtr<FunctionExpression>()->static_function_name == "clamp");
// A function that has a body which is just a function call should be inlined.
results = testMainFloatArg("def f1(float z) !noinline float : z*z \n\
def f2(float z) float : f1(z) \n\
def entryPoint2(float x) float : f1(x) + f2(x) \n\
def main(float x) float : f2(x)", 4.0f, 16.0f); // Call to f2 should be inlined, and replaced with the call to f1
testAssert(results.maindef->body->nodeType() == ASTNode::FunctionExpressionType);
testAssert(results.maindef->body.downcastToPtr<FunctionExpression>()->static_function_name == "f1");
// Test 'expensive' arg detection. Test that getfield is not considered expensive.
results = testMainFloatArg("struct S { float x } \n\
def f1(float x) !noinline float : x*x \n\
def f2(float x) float : f1(x) \n\
def entryPoint2(float x) float : f1(x) + f2(x) \n\
def main(float x) float : let s = S(x) in f2(s.x)", 4.0f, 16.0f); // Call to f should be inlined, even tho argument expression is a function.
testAssert(results.maindef->body->nodeType() == ASTNode::LetBlockType);
testAssert(results.maindef->body.downcastToPtr<LetBlock>()->expr->nodeType() == ASTNode::FunctionExpressionType);
testAssert(results.maindef->body.downcastToPtr<LetBlock>()->expr.downcastToPtr<FunctionExpression>()->static_function_name == "f1");
results = testMainIntegerArg("def f(int y) : y + 1 def main(int x) int : f(x)", 1, 2); // f should be inlined.
testAssert(results.maindef->body->nodeType() == ASTNode::AdditionExpressionType);
testMainFloatArg("def f(float b) float : let x = b in x \n\
def main(float x) float : f(x * 10) \n\
", 1.0f, 10.0f);
// Naive inlining of f() without variable renaming results in
// def main(float x) float : let x = x * 10 in x
// If we rename all let vars, we get something like:
testMainFloatArg("def f(float b) float : let x = b in x \n\
def main(float x) float : let x_new = x * 10 in x_new \n\
", 1.0f, 10.0f);
testMainFloatArg("def f(float b) float : let x = b in x \n\
def main(float x) float : f(x) \n\
", 1.0f, 1.0f);
testMainFloatArg("def f(float b) float : let x, y = (b, b) in x \n\
def main(float x) float : f(x * 10.0) \n\
", 1.0f, 10.0f);
testMainFloatArg("def f(float b) float : let y, x = (b, b) in x \n\
def main(float x) float : f(x * 10.0) \n\
", 1.0f, 10.0f);
// Test that new names are checked properly
testMainFloatArg("def f(float b) float : let x, x_0 = (b, b) in x \n\
def main(float x) float : f(x * 10) \n\
", 1.0f, 10.0f);
// Test that new names are checked properly
testMainFloatArg("def f(float b) float : let x = b in x \n\
def main(float x) float : let x_0 = 10 in f(x * x_0) \n\
", 1.0f, 10.0f);
// Test that new names are checked properly
testMainFloatArg("def f(float b) float : let x = b in x \n\
def main(float x) float : let x_0 = 10 x_1 = 11 x_2 = 12 x_3 = 13 x_4 = 14 x_5 = 15 x_6 = 16 x_7 = 17 x_8 = 18 x_9 = 19 x_10 = 20 in f(x * x_0) \n\
", 1.0f, 10.0f);
// Test with two functions being inlined that both have the same let var name (x), it should be assigned different names.
testMainFloatArg("def f(float b) float : let x = b in x \n\
def g(float b) float : let x = b in x \n\
def main(float x) float : f(x * 10) + g(x * 20) \n\
", 1.0f, 30.0f);
// Test with two functions being inlined that both have the same let var name (x), it should be assigned different names.
testMainFloatArg("def f(float b) float : let x = b in x \n\
def g(float b) float : let x = b in x \n\
def main(float x) float : let x_0 = 10 x_1 = 11 x_2 = 12 x_3 = 13 x_4 = 14 x_5 = 15 x_6 = 16 x_7 = 17 x_8 = 18 x_9 = 19 x_10 = 20 in f(x * 10) + g(x * 20) \n\
", 1.0f, 30.0f);
testMainIntegerArg("def f(int x) : x + 1 def main(int x) int : f(x)", 1, 2); // f should be inlined.
testMainIntegerArg("def f(int x) : x + 1 def main(int x) int : f(x) + f(x + 1)", 1, 5); // f should not be inlined, duplicate calls to it.
// Test inlining where the inlined function body contains a variable with the same name as the call location context.
testMainIntegerArg("def f(int x) : let a = 1 in x + a \n\
def main(int x) int : \n\
let \n\
a = f(x) # When f is inlined here, need to be careful with 'a' \n\
in \n\
a", 1, 2); // f should be inlined.
// Test inlining, where the argument expression is expensive to evaluate, e.g. sin(x) in this case.
// Because of that, we don't want to duplicate the argument expression.
// Naive inlining in this case would give
// def main(float) float : sin(x) + sin(x) + sin(x) + sin(x)
// Although common subexpression elimination (CSE) should in theory be able to remove the duplicate sin(x)'s,
// we don't want to rely on that.
// For example Winter doesn't do CSE itself, so generated OpenCL code would contain duplicate sin calls.
results = testMainFloatArg("def f(float x) : x + x + x + x \n\
def main(float x) float : f(sin(x))", 1, 4*sin(1.f)); // f should not be inlined.
testAssert(results.maindef->body->nodeType() == ASTNode::FunctionExpressionType);
testAssert(results.maindef->body.downcastToPtr<FunctionExpression>()->static_function_name == "f");
// Test inlining, where the argument expression is expensive to evaluate, e.g. sin(x) in this case.
// However, the function being called, f, only uses the argument once in the function body.
// So the expensive expression won't be duplicated, hence we can inline.
results = testMainFloatArg("def f(float x) : x + 1.0 \n\
def main(float x) float : f(sin(x))", 1, sin(1.f) + 1.0f);
testAssert(results.maindef->body->nodeType() == ASTNode::AdditionExpressionType);
testMainFloatArgAllowUnsafe("def f(varray<float> v) varray<float> : v def main(float x) float : elem(f([99.0]va), 0)", 1.f, 99.0f, INVALID_OPENCL);
}
static void testDeadCodeElimination()
{
// ===================================================================
// Test dead code elimination
// ===================================================================
testMainIntegerArg("def main(int x) int : \n\
let \n\
a = 1 + x # dead \n\
b = 3 + x # alive \n\
c = b + x # alive \n\
in \n\
c + x # c is alive \n\
", 1, 6);
testMainIntegerArg("def main(int x) int : \n\
let \n\
a = 1 + x # referred to by b, but still dead \n\
b = 3 + a # dead \n\
c = 4 + x # alive \n\
d = c + x # alive \n\
in \n\
d + x # d is alive \n\
", 1, 7);
// Test that a let block with no let vars is removed, and is replaced with the value expression (x var)
Winter::TestResults results = testMainIntegerArg("def main(int x) int : \n\
let \n\
a = 1 + x # dead \n\
in \n\
x \n\
", 4, 4);
testAssert(results.maindef->body->nodeType() == ASTNode::VariableASTNodeType);
}
static void testDeadFunctionElimination()
{
// ===================================================================
// Test dead function elimination
// ===================================================================
testMainInt64Arg("def func_1(float x) : x def func_2(float x) : x def main(int64 x) int64 : x", 1, 1);
try
{
const std::string src = "def func_1(int64 x) : x def func_2(int64 x) : x def main(int64 x) int64 : func_2(x)";
VMConstructionArgs vm_args;
vm_args.source_buffers.push_back(SourceBufferRef(new SourceBuffer("buffer", src)));
const FunctionSignature mainsig("main", std::vector<TypeVRef>(1, new Int(64)));
const FunctionSignature func_1_sig("func_1", std::vector<TypeVRef>(1, new Int(64)));
const FunctionSignature func_2_sig("func_2", std::vector<TypeVRef>(1, new Int(64)));
vm_args.entry_point_sigs.push_back(mainsig);
VirtualMachine vm(vm_args);
testAssert(vm.findMatchingFunction(mainsig).nonNull());
testAssert(vm.findMatchingFunction(func_1_sig).isNull()); // func_1 should be removed by dead code elim.
testAssert(vm.findMatchingFunction(func_2_sig).isNull()); // func_2 will be removed by inlining and then dead code elim.
}
catch(Winter::BaseException& e)
{
conPrint(e.what());
assert(0);
exit(1);
}
}
static void testArrays()
{
// ===================================================================
// Test compare-equal operators
// ===================================================================
// Test == with !noline to test LLVM codgen
testMainFloat("def eq(array<float, 2> a, array<float, 2> b) !noinline bool : a == b \n\
def main() float : eq([1.0, 2.0]a, [1.0, 3.0]a) ? 1.0 : 2.0",
2.0f);
testMainFloat("def eq(array<float, 2> a, array<float, 2> b) !noinline bool : a == b \n\
def main() float : eq([1.0, 2.0]a, [1.0, 2.0]a) ? 1.0 : 2.0",
1.0f);
// Test == without !noline to test winter interpreted execution/constant folding.
testMainFloat("def main() float : ([1.0, 2.0]a == [1.0, 3.0]a) ? 1.0 : 2.0", 2.0f);
testMainFloat("def main() float : ([1.0, 2.0]a == [3.0, 2.0]a) ? 1.0 : 2.0", 2.0f);
testMainFloat("def main() float : ([1.0, 2.0]a == [1.0, 2.0]a) ? 1.0 : 2.0", 1.0f);
// Test !=
testMainFloat("def neq(array<float, 2> a, array<float, 2> b) !noinline bool : a != b \n\
def main() float : neq([1.0, 2.0]a, [1.0, 3.0]a) ? 1.0 : 2.0",
1.0f);
testMainFloat("def neq(array<float, 2> a, array<float, 2> b) !noinline bool : a != b \n\
def main() float : neq([1.0, 2.0]a, [1.0, 2.0]a) ? 1.0 : 2.0",
2.0f);
// Test != without !noline to test winter interpreted execution/constant folding.
testMainFloat("def main() float : ([1.0, 2.0]a != [1.0, 3.0]a) ? 1.0 : 2.0", 1.0f);
testMainFloat("def main() float : ([1.0, 2.0]a != [3.0, 2.0]a) ? 1.0 : 2.0", 1.0f);
testMainFloat("def main() float : ([1.0, 2.0]a != [1.0, 2.0]a) ? 1.0 : 2.0", 2.0f);
}
static void testVArrays()
{
// ===================================================================
// Test makeVArray(elem, count) varray<T>
// ===================================================================
// Test makeVArray with constant count arg
testMainInt64Arg("def main(int64 x) int64 : length(makeVArray(1, 10i64))", 10, 10);
// Test makeVArray with runtime-variable, but bounded count arg
testMainInt64Arg("def main(int64 x) int64 : length(makeVArray(1, x > 10i64 ? 10i64 : x))", 10, 10, ALLOW_SPACE_BOUND_FAILURE | ALLOW_TIME_BOUND_FAILURE);
testMainInt64Arg("def main(int64 x) int64 : length(makeVArray(1, x))", 1, 1, ALLOW_SPACE_BOUND_FAILURE | ALLOW_TIME_BOUND_FAILURE);
testMainInt64Arg("def main(int64 x) int64 : length(makeVArray(1, x))", 4, 4, ALLOW_SPACE_BOUND_FAILURE | ALLOW_TIME_BOUND_FAILURE);
testMainInt64Arg("def main(int64 x) int64 : length(makeVArray(1, x))", 0, 0, ALLOW_SPACE_BOUND_FAILURE | ALLOW_TIME_BOUND_FAILURE);
testMainInt64Arg("def main(int64 x) int64 : makeVArray(x, x)[0]", 1, 1, ALLOW_UNSAFE | ALLOW_SPACE_BOUND_FAILURE | ALLOW_TIME_BOUND_FAILURE);
testMainInt64Arg("def main(int64 x) int64 : makeVArray(x, x)[0]", 2, 2, ALLOW_UNSAFE | ALLOW_SPACE_BOUND_FAILURE | ALLOW_TIME_BOUND_FAILURE);
testMainInt64Arg("def main(int64 x) int64 : makeVArray(x, x)[1]", 2, 2, ALLOW_UNSAFE | ALLOW_SPACE_BOUND_FAILURE | ALLOW_TIME_BOUND_FAILURE);
// testMainIntegerArg("def f(int i) varray<float> : [10]va def main(int x) int : if i > 0 && i < 4 then f(x)[i] else 0", 4, 4);
// testMainIntegerArg("def f(int i) varray<float> : [i, i, i, i]va def main(int x) int : if i > 0 && i < 4 then f(x)[i] else 0", 4, 4);
// testMainFloatArg("def f(float x) varray<float> : [x]va def main(float x) float : f(x)[0]", 4.0f, 4.0f);
testMainFloatArg("def main(float x) float : [4.0]va[0]", 0, 4.0f, INVALID_OPENCL);
// Test varrays with pass-by-value elements (e.g. structures)
testMainFloatArg("struct s { float x } def main(float x) float : [s(4.0)]va[0].x", 0, 4.0f, INVALID_OPENCL);
testMainFloatArg("struct s { float x } def main(float x) float : [s(10.0), s(11.0), s(12.0), s(13.0)]va[2].x", 0, 12.0f, INVALID_OPENCL);
// Test varrays with heap-allocated/ref counted elements
//testMainIntegerArg("struct s { float x } def main(float x) float : [\"abc\"]va[0].x", 0, 4.0f);
// In VArray literal
testMainFloatArgAllowUnsafe("struct s { float x } \n\
def op_add(s a, s b) : s(a.x + b.x) \n\
def main(float x) float : [s(1) + s(3)]va[0].x", 1.0f, 4.0f, INVALID_OPENCL);
// ===================================================================
// Test constant indices into varray literals.
// ===================================================================
// Test with literal indices
testMainIntegerArg("def main(int x) int : [10, 11, 12, 13]va[0]", 0, 10, INVALID_OPENCL);
testMainIntegerArg("def main(int x) int : [10, 11, 12, 13]va[1]", 0, 11, INVALID_OPENCL);
testMainIntegerArg("def main(int x) int : [10, 11, 12, 13]va[2]", 0, 12, INVALID_OPENCL);
testMainIntegerArg("def main(int x) int : [10, 11, 12, 13]va[3]", 0, 13, INVALID_OPENCL);
// Test with a let variable that refers to a literal
testMainIntegerArg("def main(int x) int : let v = [10, 11, 12, 13]va in v[0]", 0, 10, INVALID_OPENCL);
// Test with a let variable that refers to a named constant
testMainIntegerArg("V = [10, 11, 13, 13]va def main(int x) int : V[0]", 0, 10, INVALID_OPENCL);
testMainIntegerArgInvalidProgram("def main(int x) int : [10, 11, 12, 13]va[-1]");
testMainIntegerArgInvalidProgram("def main(int x) int : [10, 11, 12, 13]va[-10]");
testMainIntegerArgInvalidProgram("def main(int x) int : [10, 11, 12, 13]va[4]");
testMainIntegerArgInvalidProgram("def main(int x) int : [10, 11, 12, 13]va[10]");
// Test with runtime indices
testMainIntegerArg("def main(int x) int : if x >= 0 && x < 4 then [10, 11, 12, 13]va[x] else 0", 0, 10, INVALID_OPENCL);
//testMainIntegerArg("def main(int x) int : [10, 11, 12, 13]va[if x >= 0 && x < 4 then x else 0]", 0, 10);
testMainIntegerArg("def main(int x) int : if x >= 0 && x < 4 then [10, 11, 12, 13]va[x] else 0", 2, 12, INVALID_OPENCL);
testMainIntegerArg("def main(int x) int : [10, 11, 12, 13]va[2]", 2, 12, INVALID_OPENCL);
testMainIntegerArg("def main(int x) int : [10, 11, 12, 13]va[3]", 3, 13, INVALID_OPENCL);
testMainIntegerArgInvalidProgram("def main(int x) int : [10, 11, 12, 13]va[x]");
testMainIntegerArgInvalidProgram("def main(int x) int : if x >= -1 && x < 4 then [10, 11, 12, 13]va[x] else 0"); // Test with some incorrect bounding expressions.
testMainIntegerArgInvalidProgram("def main(int x) int : if x >= 0 && x <= 4 then [10, 11, 12, 13]va[x] else 0"); // Test with some incorrect bounding expressions.
testMainIntegerArgInvalidProgram("def main(int x) int : if x >= 0 && x < 5 then [10, 11, 12, 13]va[x] else 0"); // Test with some incorrect bounding expressions.
// ===================================================================
// Test VArrays
// ===================================================================
// Test VArray let variables.
testMainFloatArg("def main(float x) float : let v = [99]va in x", 1.f, 1.0f, INVALID_OPENCL);
testMainFloatArg("def main(float x) float : let v = [1.0, 2.0, 3.0]va in x", 1.f, 1.0f, INVALID_OPENCL);
testMainFloatArg("def main(float x) float : let v = [x + 1.0, x + 2.0, x + 3.0]va in x", 1.f, 1.0f, INVALID_OPENCL);
// Two VArray let variables
testMainFloatArg("def main(float x) float : let a = [1]va b = [2]va in x", 1.f, 1.0f, INVALID_OPENCL);
// Back-references in let variables
testMainFloatArg("def main(float x) float : let a = [1]va b = a in x", 1.f, 1.0f, INVALID_OPENCL);
// Test a reference to a let var from another let block
testMainFloatArg("def main(float x) float : \n\
let \n\
a = [1]va \n\
in \n\
let \n\
b = a \n\
in \n\
x", 1.f, 1.0f, INVALID_OPENCL);
// Return a varray from a function
testMainFloatArg("def f() varray<int> : [3]va def main(float x) float : let v = f() in x", 1.f, 1.0f, INVALID_OPENCL);
testMainFloatArg("def f(float x) !noinline varray<float> : [x, x, x, x]va def main(float x) float : (f(x))[2]", 1.f, 1.0f, INVALID_OPENCL | ALLOW_UNSAFE);
testMainFloatArg("def f(float x) varray<float> : [x, x, x, x]va def main(float x) float : (f(x))[2]", 1.f, 1.0f, INVALID_OPENCL | ALLOW_UNSAFE);
// Test a varray with access
//testMainFloatArgAllowUnsafe("def main(float x) float : [x + 1.0, x + 2.0, x + 3.0]va[1]", 1.f, 3.0f);
// Test a varray returned from a function with access
testMainFloatArgAllowUnsafe("def f(float x) varray<float> : [x]va def main(float x) float : elem(f(x), 0)", 1.f, 1.0f, INVALID_OPENCL);
testMainFloatArgAllowUnsafe("def f(float x) varray<float> : [x + 1.0, x + 2.0, x + 3.0]va def main(float x) float : f(x)[0]", 1.f, 2.0f, INVALID_OPENCL);
testMainFloatArgAllowUnsafe("def f(float x) varray<float> : [x + 1.0, x + 2.0, x + 3.0]va def main(float x) float : f(x)[2]", 1.f, 4.0f, INVALID_OPENCL);
// Test varray returned from an if expression
testMainFloatArgAllowUnsafe("def main(float x) float : let v = (if x < 2.0 then [x + 1.0]va else [x + 2.0]va) in elem(v, 0)", 1.f, 2.0f, INVALID_OPENCL);
testMainFloatArgAllowUnsafe("def main(float x) float : (if x < 2.0 then [x + 1.0]va else [x + 2.0]va)[0]", 1.f, 2.0f, INVALID_OPENCL);
// Test varray returned from an if expression in a function
testMainFloatArgAllowUnsafe("def f(float x) varray<float> : if x < 2.0 then [x + 1.0]va else [x + 2.0]va def main(float x) float : f(x)[0]", 1.f, 2.0f, INVALID_OPENCL);
testMainFloatArgAllowUnsafe("struct S { float x } def f(float x) S : S(x) def main(float x) float : f(x).x", 1.f, 1.0f, INVALID_OPENCL);
// Test varray in struct (just assigned to let var but not used)
testMainFloatArgAllowUnsafe("struct S { varray<float> v } def main(float x) float : let s = S([x]va) in x", 1.f, 1.0f, INVALID_OPENCL);
// Test varray in struct
testMainFloatArgAllowUnsafe("struct S { varray<float> v } def main(float x) float : S([x]va).v[0]", 1.f, 1.0f, INVALID_OPENCL);
// Test two varrays in struct
testMainFloatArgAllowUnsafe("struct S { varray<float> a, varray<float> b } def main(float x) float : S([x]va, [x]va).a[0]", 1.f, 1.0f, INVALID_OPENCL);
// Test varray in struct returned from function
testMainFloatArgAllowUnsafe("struct S { varray<float> v } def f(float x) S : S([x + 1.0]va) def main(float x) float : f(x).v[0]", 1.f, 2.0f, INVALID_OPENCL);
// Test two varrays in struct returned from function
testMainFloatArgAllowUnsafe("struct S { varray<float> a, varray<float> b } def f(float x) S : S([x + 1.0]va, [x + 2.0]va) def main(float x) float : f(x).a[0]", 1.f, 2.0f, INVALID_OPENCL);
//------------------------ VArrays as function arguments --------------------------
// Test a VArray returned from identity function
testMainFloatArgAllowUnsafe("def f(varray<float> v) varray<float> : v def main(float x) float : f([99.0]va)[0]", 1.f, 99.0f, INVALID_OPENCL);
testMainFloatArgAllowUnsafe("def f(varray<float> v) varray<float> : v def main(float x) float : let v = f([99.0]va) in x", 99.f, 99.0f, INVALID_OPENCL);
// Test a VArray returned from 'identity' function with two args
testMainFloatArgAllowUnsafe("def f(varray<float> v, varray<float> v2) varray<float> : v def main(float x) float : f([88.0]va, [99.0]va)[0]", 1.f, 88.0f, INVALID_OPENCL);
testMainFloatArgAllowUnsafe("def f(varray<float> v, varray<float> v2) varray<float> : v2 def main(float x) float : f([88.0]va, [99.0]va)[0]", 1.f, 99.0f, INVALID_OPENCL);
testMainFloatArgAllowUnsafe("def f(varray<float> v, varray<float> v2) varray<float> : v2 def main(float x) float : let v = [99.0]va in f(v, v)[0]", 1.f, 99.0f, INVALID_OPENCL);
// Test a VArray passed as a function argument (but not used)
testMainFloatArgAllowUnsafe("def f(varray<float> v) float : 1.0 def main(float x) float : f([99.0]va)", 1.f, 1.0f, INVALID_OPENCL);
// Test a VArray passed as a function argument, then indexed into.
testMainFloatArgAllowUnsafe("def f(varray<float> v) float : v[0] def main(float x) float : f([x]va)", 10.f, 10.0f, INVALID_OPENCL);
// Test two VArrays passed as function arguments, then indexed into.
testMainFloatArgAllowUnsafe("def f(varray<float> v_a, varray<float> v_b) float : v_a[0] + v_b[0] def main(float x) float : f([x]va, [1.0]va)", 10.f, 11.0f, INVALID_OPENCL);
// Test nested function call with varray
testMainFloatArgAllowUnsafe("def f(varray<float> v) float : v[0] def g(varray<float> v) float : f(v) def main(float x) float : g([x]va)", 10.f, 10.0f, INVALID_OPENCL);
testMainFloatArgAllowUnsafe("def f(varray<float> v) float : v[0] def g(varray<float> v) float : f(v) + 1.0 def main(float x) float : g([x]va)", 10.f, 11.0f, INVALID_OPENCL);
// Test varray function argument being referenced by let
testMainFloatArgAllowUnsafe("def f(varray<float> v) float : let v_2 = v in v_2[0] def main(float x) float : f([x]va)", 10.f, 10.0f, INVALID_OPENCL);
testMainFloatArgAllowUnsafe("def f(varray<float> v) float : let v_2 = v in v[0] def main(float x) float : f([x]va)", 10.f, 10.0f, INVALID_OPENCL);
// Test varray passed as function argument but returned in struct (tests return of argument via enclosing type)
testMainFloatArgAllowUnsafe("struct S { varray<float> v } def f(varray<float> arg) S : S(arg) def main(float x) float : f([x]va).v[0]", 10.f, 10.0f, INVALID_OPENCL);
testMainFloatArgAllowUnsafe("struct S { varray<float> a, varray<float> b } def f(varray<float> arg) S : S(arg, arg) def main(float x) float : f([x]va).a[0]", 10.f, 10.0f, INVALID_OPENCL);
testMainFloatArgAllowUnsafe("struct S { varray<float> a, varray<float> b } def f(varray<float> arg, varray<float> arg2) S : S(arg, arg2) def main(float x) float : f([x]va, [x]va).a[0]", 10.f, 10.0f, INVALID_OPENCL);
//------------------------ VArrays in tuples --------------------------
testMainFloatArgAllowUnsafe("def f(varray<float> arg) tuple<varray<float> > : [arg]t def main(float x) float : f([x]va)[0][0]", 10.f, 10.0f, INVALID_OPENCL);
testMainFloatArgAllowUnsafe("def f(varray<float> arg) tuple<varray<float>, varray<float> > : (arg, arg) def main(float x) float : f([x]va)[0][0]", 10.f, 10.0f, INVALID_OPENCL);
//------------------------ Test a VArray in a VArray --------------------------
testMainFloatArg("def main(float x) float : let a = [[99]va]va in x", 10.f, 10.0f, INVALID_OPENCL);
//------------------------ Test a string in a VArray --------------------------
testMainFloatArg("def main(float x) float : let a = [\"hello\"]va in x", 10.f, 10.0f, INVALID_OPENCL);
testMainFloatArg("def main(float x) float : let a = [[\"hello\"]va]va in x", 10.f, 10.0f, INVALID_OPENCL);
testMainInt64Arg("def main(int64 x) int64 : let a = [\"hello\"]va in length(a[0])", 0, 5, INVALID_OPENCL | ALLOW_TIME_BOUND_FAILURE);
//------------------------ Test a struct in a VArray, with access --------------------------
testMainFloatArg("struct s { float x } def main(float x) float : [s(4.0)]va[0].x", 0, 4.0f, INVALID_OPENCL);
testMainFloatArg("struct s { float x } def main(float x) float : [s(10.0), s(11.0), s(12.0), s(13.0)]va[2].x", 0, 12.0f, INVALID_OPENCL);
//------------------------ Test a struct, with a ref counted field, in a VArray --------------------------
testMainFloatArg("struct S { string str } def main(float x) float : let a = S(\"hello\") in x", 10.f, 10.0f, INVALID_OPENCL);
testMainFloatArg("struct S { string str } def main(float x) float : let a = [S(\"hello\")]va in x", 10.f, 10.0f, INVALID_OPENCL);
testMainFloatArg("struct S { string str } def main(float x) float : let a = [S(\"hello\"), S(\"world\")]va in x", 10.f, 10.0f, INVALID_OPENCL);
// Test a struct with two ref counted fields
testMainFloatArg("struct S { string s_a, string s_b } def main(float x) float : let a = S(\"hello\", \"world\") in x", 10.f, 10.0f, INVALID_OPENCL);
testMainFloatArg("struct S { string s_a, string s_b } def main(float x) float : let a = [S(\"hello\", \"world\")]va in x", 10.f, 10.0f, INVALID_OPENCL);
//------------------------ Test a tuple, with a ref counted field, in a VArray --------------------------
testMainFloatArg("struct S { string str } def main(float x) float : let a = S(\"hello\") in x", 10.f, 10.0f, INVALID_OPENCL);
testMainFloatArg("def main(float x) float : let a = [\"hello\"]t in x", 10.f, 10.0f, INVALID_OPENCL);
testMainFloatArg("def main(float x) float : let a = [[\"hello\"]t]va in x", 10.f, 10.0f, INVALID_OPENCL);
testMainFloatArg("def main(float x) float : let a = [[\"hello\"]t, [\"world\"]t]va in x", 10.f, 10.0f, INVALID_OPENCL);
// Test a tuple with two ref counted fields
testMainFloatArg("def main(float x) float : let a = (\"hello\", \"world\") in x", 10.f, 10.0f, INVALID_OPENCL);
testMainFloatArg("def main(float x) float : let a = [(\"hello\", \"world\")]va in x", 10.f, 10.0f, INVALID_OPENCL);
//------------------------ Test a VArray in a named constant --------------------------
testMainFloatArg("TEST = [99.0]va def main(float x) float : TEST[0]", 10.f, 99.0f, INVALID_OPENCL);
testMainFloatArg("TEST = [99.0]va def main(float x) float : TEST[0] + TEST[0]", 10.f, 198.0f, INVALID_OPENCL);
testMainFloatArg("TEST = [1.0 + 2.0]va def main(float x) float : TEST[0] + TEST[0]", 10.f, 6.0f, INVALID_OPENCL);
testMainFloatArgAllowUnsafe("def f(varray<float> v) : v TEST = f([3.0]va) def main(float x) float : TEST[0]", 10.f, 3.0f, INVALID_OPENCL);
testMainFloatArgAllowUnsafe("def f(varray<float> v) : v TEST = f([3.0]va) def main(float x) float : TEST[0] + TEST[0]", 10.f, 6.0f, INVALID_OPENCL);
// ===================================================================
// Test compare-equal operators
// ===================================================================
// Test == with !noline to test LLVM codgen
testMainFloat("def eq(varray<float> a, varray<float> b) !noinline bool : a == b \n\
def main() float : eq([1.0, 2.0]va, [1.0, 3.0]va) ? 1.0 : 0.0",
0.0f);
testMainFloat("def eq(varray<float> a, varray<float> b) !noinline bool : a == b \n\
def main() float : eq([1.0, 2.0]va, [1.0, 2.0, 3.0]va) ? 1.0 : 0.0",
0.0f);
testMainFloat("def eq(varray<float> a, varray<float> b) !noinline bool : a == b \n\
def main() float : eq([1.0, 2.0]va, [1.0, 2.0]va) ? 1.0 : 0.0",
1.0f);
// Test with a heap-allocated type as the varray elem type
testMainFloat("def eq(varray<string> a, varray<string> b) !noinline bool : a == b \n\
def main() float : eq([\"a\", \"b\"]va, [\"a\", \"b\"]va) ? 1.0 : 0.0",
1.0f);
testMainFloat("def eq(varray<string> a, varray<string> b) !noinline bool : a == b \n\
def main() float : eq([\"a\", \"b\"]va, [\"a\", \"c\"]va) ? 1.0 : 0.0",
0.0f);
// Test == without !noline to test winter interpreted execution/constant folding.
testMainFloat("def main() float : ([1.0, 2.0]va == [1.0, 3.0]va) ? 1.0 : 0.0", 0.0f);
testMainFloat("def main() float : ([1.0, 2.0]va == [3.0, 2.0]va) ? 1.0 : 0.0", 0.0f);
testMainFloat("def main() float : ([1.0, 2.0]va == [1.0, 2.0]va) ? 1.0 : 0.0", 1.0f);
// Test !=
testMainFloat("def neq(varray<float> a, varray<float> b) !noinline bool : a != b \n\
def main() float : neq([1.0, 2.0]va, [1.0, 3.0]va) ? 1.0 : 0.0",
1.0f);
testMainFloat("def neq(varray<float> a, varray<float> b) !noinline bool : a != b \n\
def main() float : neq([1.0, 2.0]va, [1.0, 2.0, 3.0]va) ? 1.0 : 0.0",
1.0f);
testMainFloat("def neq(varray<float> a, varray<float> b) !noinline bool : a != b \n\
def main() float : neq([1.0, 2.0]va, [1.0, 2.0]va) ? 1.0 : 0.0",
0.0f);
// Test != without !noline to test winter interpreted execution/constant folding.
testMainFloat("def main() float : ([1.0, 2.0]va != [1.0, 3.0]va) ? 1.0 : 0.0", 1.0f);
testMainFloat("def main() float : ([1.0, 2.0]va != [3.0, 2.0]va) ? 1.0 : 0.0", 1.0f);
testMainFloat("def main() float : ([1.0, 2.0]va != [1.0, 2.0]va) ? 1.0 : 0.0", 0.0f);
testMainFloat("def main() float : ([1.0, 2.0]va != [1.0]va) ? 1.0 : 0.0", 1.0f);
// ===================================================================
// Test allocation of large varray that is not captured or returned.
// ===================================================================
// Because this varray is not returned or captured from f(), it might have been allocated on the stack.
// We want to make sure it isn't because it is too large.
testMainInt64Arg("def f(int64 index) !noinline int64 : ([123i64]va1000000)[index] \n\
def main(int64 x) int64 : f(x)", 1, 123, ALLOW_UNSAFE);
// Test with smaller int suffix, should not emit a call to makeVArray()
testMainInt64Arg("def f(int64 index) !noinline int64 : ([123i64]va5)[index] \n\
def main(int64 x) int64 : f(x)", 1, 123, ALLOW_UNSAFE);
// Test the same as above, but with a structure in the varray
testMainInt64Arg("struct S { int64 x } \n\
def f(int64 index) !noinline int64 : ([S(123i64)]va1000000)[index].x \n\
def main(int64 x) int64 : f(x)", 1, 123, ALLOW_UNSAFE);
// Test with smaller int suffix, should not emit a call to makeVArray()
testMainInt64Arg("struct S { int64 x } \n\
def f(int64 index) !noinline int64 : ([S(123i64)]va5)[index].x \n\
def main(int64 x) int64 : f(x)", 1, 123, ALLOW_UNSAFE);
// TODO: test with refcounted heap-allocated types like string.
}
static void testToIntFunctions()
{
// ===================================================================
// Test toInt32
// ===================================================================
testMainIntegerArg("def main(int x) int : toInt32(0i64)", 0, 0);
testMainIntegerArg("def main(int x) int : toInt32(1i64)", 0, 1);
testMainIntegerArg("def main(int x) int : toInt32(-1i64)", 0, -1);
testMainIntegerArg("def main(int x) int : toInt32(12345i64)", 0, 12345);
testMainIntegerArg("def main(int x) int : toInt32(-12345i64)", 0, -12345);
testMainIntegerArg("def main(int x) int : toInt32(-2147483648i64)", 0, -2147483647 - 1);
testMainIntegerArg("def main(int x) int : toInt32(2147483647i64)", 0, 2147483647);
// Test some out-of domain values
testMainIntegerArgInvalidProgram("def main(int x) int : toInt32(-2147483649i64)");
testMainIntegerArgInvalidProgram("def main(int x) int : toInt32(-2147483650i64)");
testMainIntegerArgInvalidProgram("def main(int x) int : toInt32(-300147483650i64)");
testMainIntegerArgInvalidProgram("def main(int x) int : toInt32(-9223372036854775808i64)"); // -2^63
testMainIntegerArgInvalidProgram("def main(int x) int : toInt32(2147483648i64)");
testMainIntegerArgInvalidProgram("def main(int x) int : toInt32(2147483649i64)");
testMainIntegerArgInvalidProgram("def main(int x) int : toInt32(300147483650i64)");
testMainIntegerArgInvalidProgram("def main(int x) int : toInt32(9223372036854775807i64)"); // 2^63-1
// TODO: should be able to remove ALLOW_UNSAFE at some point.
testMainIntegerArg("def main(int x) int : toInt32(toInt64(x))", 0, 0, ALLOW_UNSAFE);
testMainIntegerArg("def main(int x) int : toInt32(toInt64(x))", 1, 1, ALLOW_UNSAFE);
testMainIntegerArg("def main(int x) int : toInt32(toInt64(x))", -1, -1, ALLOW_UNSAFE);
testMainIntegerArg("def main(int x) int : toInt32(toInt64(x))", -2147483647 - 1, -2147483647 - 1, ALLOW_UNSAFE);
testMainIntegerArg("def main(int x) int : toInt32(toInt64(x))", 2147483647, 2147483647, ALLOW_UNSAFE);
// ===================================================================
// Test toInt64
// ===================================================================
testMainInt64Arg("def main(int64 x) int64 : toInt64(0)", 0, 0);
testMainInt64Arg("def main(int64 x) int64 : toInt64(1)", 0, 1);
testMainInt64Arg("def main(int64 x) int64 : toInt64(-1)", 0, -1);
testMainInt64Arg("def main(int64 x) int64 : toInt64(-2147483648)", 0, -2147483648LL);
testMainInt64Arg("def main(int64 x) int64 : toInt64(2147483647)", 0, 2147483647);
}
static void testStringFunctions()
{
// ===================================================================
// Test string elem function (elem(string s, uint64 index) char)
// ===================================================================
// TODO: should be able to remove ALLOW_UNSAFE at some point.
testMainIntegerArg("def main(int x) int : codePoint(elem(\"a\", 0i64))", 0, (int)'a', ALLOW_UNSAFE | INVALID_OPENCL);
testMainIntegerArg("def main(int x) int : codePoint(elem(\"hello\", 0i64))", 0, (int)'h', ALLOW_UNSAFE | INVALID_OPENCL);
testMainIntegerArg("def main(int x) int : codePoint(elem(\"hello\", 1i64))", 0, (int)'e', ALLOW_UNSAFE | INVALID_OPENCL);
testMainIntegerArg("def main(int x) int : codePoint(elem(\"hello\", 4i64))", 0, (int)'o', ALLOW_UNSAFE | INVALID_OPENCL);
testMainInt64Arg("def main(int64 x) int64 : toInt64(codePoint(elem(\"abc\", x)))", 0, (int)'a', ALLOW_UNSAFE | INVALID_OPENCL | ALLOW_TIME_BOUND_FAILURE);
testMainInt64Arg("def main(int64 x) int64 : toInt64(codePoint(elem(\"abc\", x)))", 1, (int)'b', ALLOW_UNSAFE | INVALID_OPENCL | ALLOW_TIME_BOUND_FAILURE);
testMainInt64Arg("def main(int64 x) int64 : toInt64(codePoint(elem(\"abc\", x)))", 2, (int)'c', ALLOW_UNSAFE | INVALID_OPENCL | ALLOW_TIME_BOUND_FAILURE);
// with multi-byte chars:
testMainInt64Arg("def main(int64 x) int64 : toInt64(codePoint(elem(\"\\u{393}\", x)))", 0, 0x393, ALLOW_UNSAFE | INVALID_OPENCL | ALLOW_TIME_BOUND_FAILURE);
testMainInt64Arg("def main(int64 x) int64 : toInt64(codePoint(elem(\"\\u{393}bc\", x)))", 0, 0x393, ALLOW_UNSAFE | INVALID_OPENCL | ALLOW_TIME_BOUND_FAILURE);
testMainInt64Arg("def main(int64 x) int64 : toInt64(codePoint(elem(\"\\u{393}bc\", x)))", 1, (int)'b', ALLOW_UNSAFE | INVALID_OPENCL | ALLOW_TIME_BOUND_FAILURE);
testMainInt64Arg("def main(int64 x) int64 : toInt64(codePoint(elem(\"\\u{393}bc\", x)))", 2, (int)'c', ALLOW_UNSAFE | INVALID_OPENCL | ALLOW_TIME_BOUND_FAILURE);
testMainInt64Arg("def main(int64 x) int64 : toInt64(codePoint(elem(\"a\\u{393}c\", x)))", 0, (int)'a', ALLOW_UNSAFE | INVALID_OPENCL | ALLOW_TIME_BOUND_FAILURE);
testMainInt64Arg("def main(int64 x) int64 : toInt64(codePoint(elem(\"a\\u{393}c\", x)))", 1, 0x393, ALLOW_UNSAFE | INVALID_OPENCL | ALLOW_TIME_BOUND_FAILURE);
testMainInt64Arg("def main(int64 x) int64 : toInt64(codePoint(elem(\"a\\u{393}c\", x)))", 2, (int)'c', ALLOW_UNSAFE | INVALID_OPENCL | ALLOW_TIME_BOUND_FAILURE);
testMainInt64Arg("def main(int64 x) int64 : toInt64(codePoint(elem(\"ab\\u{393}\", x)))", 0, (int)'a', ALLOW_UNSAFE | INVALID_OPENCL | ALLOW_TIME_BOUND_FAILURE);
testMainInt64Arg("def main(int64 x) int64 : toInt64(codePoint(elem(\"ab\\u{393}\", x)))", 1, (int)'b', ALLOW_UNSAFE | INVALID_OPENCL | ALLOW_TIME_BOUND_FAILURE);
testMainInt64Arg("def main(int64 x) int64 : toInt64(codePoint(elem(\"ab\\u{393}\", x)))", 2, 0x393, ALLOW_UNSAFE | INVALID_OPENCL | ALLOW_TIME_BOUND_FAILURE);
// ===================================================================
// Test Escaped characters in string literals
// ===================================================================
const std::string SINGLE_QUOTE = "'";
const std::string DBL_QUOTE = "\"";
const std::string NEWLINE = "\n";
const std::string CRG_RTN = "\r";
const std::string TAB = "\t";
const std::string BACKSLASH = "\\";
testMainStringArg("def main(string s) string : " + DBL_QUOTE + NEWLINE + DBL_QUOTE, "hello", "\n"); // \n
testMainStringArg("def main(string s) string : " + DBL_QUOTE + CRG_RTN + DBL_QUOTE, "hello", "\r"); // \r
testMainStringArg("def main(string s) string : " + DBL_QUOTE + TAB + DBL_QUOTE, "hello", "\t"); // \t
testMainStringArg("def main(string s) string : " + DBL_QUOTE + BACKSLASH + BACKSLASH + DBL_QUOTE, "hello", "\\"); // test backslash escape
testMainStringArg("def main(string s) string : " + DBL_QUOTE + BACKSLASH + DBL_QUOTE + DBL_QUOTE, "hello", "\""); // test double quote escape
testMainIntegerArgInvalidProgram("def main(int x) int : length(" + DBL_QUOTE + BACKSLASH + "a" + DBL_QUOTE + ")");
testMainIntegerArgInvalidProgram("def main(int x) int : length(" + DBL_QUOTE + BACKSLASH + "a"); // EOF in middle of literal
testMainIntegerArgInvalidProgram("def main(int x) int : length(" + DBL_QUOTE + BACKSLASH + "n"); // EOF in middle of literal
testMainIntegerArgInvalidProgram("def main(int x) int : length(" + DBL_QUOTE + BACKSLASH); // EOF in middle of literal
testMainIntegerArgInvalidProgram("def main(int x) int : length(" + DBL_QUOTE); // EOF in middle of literal
testMainIntegerArgInvalidProgram("def main(int x) int : length(" + DBL_QUOTE + "\\u{0}"); // EOF immediately after literal
testMainIntegerArgInvalidProgram("def main(int x) int : length(" + DBL_QUOTE + "\\u{0"); // EOF in middle of literal
testMainIntegerArgInvalidProgram("def main(int x) int : length(" + DBL_QUOTE + "\\u{"); // EOF in middle of literal
testMainIntegerArgInvalidProgram("def main(int x) int : length(" + DBL_QUOTE + "\\u"); // EOF in middle of literal
testMainIntegerArgInvalidProgram("def main(int x) int : length(" + DBL_QUOTE + "\\"); // EOF in middle of literal
// Test Unicode code-point escape sequence
testMainInt64Arg("def main(int64 x) int64 : length(" + DBL_QUOTE + "\\u{0}" + DBL_QUOTE + ")", 2, 1, INVALID_OPENCL | ALLOW_TIME_BOUND_FAILURE);
testMainInt64Arg("def main(int64 x) int64 : length(" + DBL_QUOTE + "\\u{7FFF}" + DBL_QUOTE + ")", 2, 1, INVALID_OPENCL | ALLOW_TIME_BOUND_FAILURE);
testMainInt64Arg("def main(int64 x) int64 : length(" + DBL_QUOTE + "\\u{10FFFF}" + DBL_QUOTE + ")", 2, 1, INVALID_OPENCL | ALLOW_TIME_BOUND_FAILURE);
testMainStringArg("def main(string s) string : " + DBL_QUOTE + "\\u{393}" + DBL_QUOTE, "hello", "\xCE\x93"); // Greek capital letter gamma, U+393, http://unicodelookup.com/#gamma/1,
testMainStringArg("def main(string s) string : " + DBL_QUOTE + "\\u{20AC}" + DBL_QUOTE, "hello", "\xE2\x82\xAC"); // Euro sign, U+20AC