-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
tests.cpp
3502 lines (3124 loc) · 101 KB
/
tests.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
#include "Util.h"
#include "List.h"
#include "Node.h"
#include "Wasp.h"
#include "Angle.h"
#include "String.h"
#include "Map.h"
#include "tests.h"
#include "Paint.h"
#pragma GCC diagnostic ignored "-Wdeprecated"
#import "test_angle.cpp"
#import "test_wast.cpp"
#import "test_wasm.cpp"
#pragma GCC diagnostic pop
#include "wasm_runner.h"
#include "WitReader.h"
#include "types/Number.h"
#define assert_parses(marka) result=assert_parsesx(marka);if(result==ERROR){printf("NOT PARSING %s\n",marka);backtrace_line();}
//void testDwarf();
//void testSourceMap();
void testInclude() {
// assert_emit("include test-include.wasp", 42);
// assert_emit("use test-include.wasm", 42);
assert_emit("include test/lib.wasp", 42);
// assert_emit("include test/lib.wast", 42);
assert_emit("use test/lib.wasm; test", 42);
// assert_emit("use https://pannous.com/files/lib.wasm; test", 42);
// assert_emit("use git://pannous/waps/test/lib.wasm; test", 42);
// assert_emit("use system:test/lib.wasm; test", 42); // ^^
}
void testExceptions() {
// assert_emit("(unclosed bracket",123);
assert_throws("x:int=1;x='ok'");
assert_throws("x:int=1;x=1.1");
// assert_emit("x:int=1;x=1.0",1); // might be cast by compiler
// assert_emit("x=1;x='ok';x=1", 1); // untyped x can be reassigned
assert_throws("'unclosed quote");
assert_throws("\"unclosed quote");
assert_throws("unclosed quote'");
assert_throws("unclosed quote\"");
assert_throws("unclosed bracket)");
assert_throws("(unclosed bracket");
}
void testNoBlock() { // fixed
assert_parses(R"(
#see math.wasp !
τ=π*2
#assert τ≈6.2831853
#τ≈6.2831853
#τ==6.2831853
)");
}
void testTypeConfusion() {
assert_throws("x=1;x='ok'");
assert_throws("x=1;x=1.0");
assert_throws("double:=it*2");// double is type i64!
// todo: get rid of stupid type name double, in C it's float64 OR int64 anyway
}
void testVectorShim() {
assert_emit("v=[1 2 3];w=[2 3 4];v*w", 2 + 6 + 12);
}
void testHtmlWasp() {
eval("html{bold{Hello}}"); // => <html><body><bold>Hello</bold></body></html> via appendChild bold to body
// eval("html{bold($myid style=red){Hello}}"); // => <bold id=myid style=red>Hello</bold>
}
void testJS() {
eval("js{alert('Hello')}"); // => <script>alert('Hello')</script>
}
void testInnerHtml() {
#if not WEBAPP and not MY_WASM
return;
#endif
auto html = parse("<html><bold>test</bold></html>");
check_is(html.kind, Kind::strings);
check(html.value.string);
check_is(*html.value.string, "<bold>test</bold>");
auto serialized = html.serialize();
check_is(serialized, "<html><bold>test</bold></html>");
// eval("<html><script>alert('ok')");
// eval("<html><script>alert('ok')</script></html>");
eval("<html><bold id=b ok=123>test</bold></html>");
assert_is("$b.ok", 123);
eval("<script>console.log('ok!')</script>");
eval("<script>alert('alert ok!')</script>");// // pop up window NOT supported by WebView, so we use print instead
// eval("$b.innerHTML='<i>ok</i>'");
// eval("<html><bold id='anchor'>…</bold></html>");
// eval("$anchor.innerHTML='<i>ok</i>'");
//
//// eval("x=<html><bold>test</bold></html>;$results.innerHTML=x");
// eval("$results.innerHTML='<bold>test</bold>'");
}
void testHtml() {
// testHtmlWasp();
// testJS();
testInnerHtml();
}
void testReplaceAll() {
String s = "abaabaa";
auto replaced = s.replaceAll("a", "ca");
// auto replaced = s.replaceAll('a', "ca");
check_is(replaced, "cabcacabcaca");
auto replaced2 = replaced.replaceAll("ca", "a");
check_is(replaced2, "abaabaa");
replaced2.replaceAllInPlace('b', 'p');
check_is(replaced2, "apaapaa");
}
void testFetch() {
// todo: use host fetch if available
auto string1 = fetch("https://pannous.com/files/test");
auto res = String(string1).trim();
if (res.contains("not available")) {
print("fetch not available. set CURL=1 in CMakelists.txt or use host function");
return;
}
check_eq(res, "test 2 5 3 7");
}
void testCanvas() {
result = analyze(parse("$canvas"));
assert_equals(result.kind, (int64) externref);
auto nod = eval(" ctx = $canvas.getContext('2d');\n"
" ctx.fillStyle = 'red';\n"
" ctx.fillRect(10, 10, 150, 100);");
print(nod);
}
void testDom() {
print("testDom");
preRegisterFunctions();
result = analyze(parse("getElementById('canvas')"));
assert_equals(result.kind, call);
result = eval("getElementById('canvas');");
// print(typeName(result.kind));
// assert_equals(result.kind, strings); // why?
// assert_equals(result.kind, longs); // todo: can't use smart pointers for elusive externref
// assert_equals(result.kind, bools); // todo: can't use smart pointers for elusive externref
print(typeName(30));
print(typeName(9));
// assert_equals(result.kind, 30);//
// assert_equals(result.kind,9);//
// assert_equals(result.kind, (int64) externref); // todo: can't use smart pointers for elusive externref
// result = eval("document.getElementById('canvas');");
// result = analyze(parse("$canvas"));
// assert_equals(result.kind, (int64) externref);
}
inline void print(Primitive l) {
print(typeName(l));
}
void testDomProperty() {
#ifndef WEBAPP
return;
#endif
result = eval("getExternRefPropertyValue($canvas,'width')"); // ok!!
check_is(result.value.longy, 300); // only works because String "300" gets converted to BigInt 300
// result = eval("width='width';$canvas.width");
result = eval("$canvas.width");
check_eq(result.value.longy, 300);
// return;
result = eval("$canvas.style");
check_is(result.kind, strings);
// check_is(result.kind, stringp);
if (result.value.string)
check_eq(*result.value.string, "dfsa");
// getExternRefPropertyValue OK [object HTMLCanvasElement] style [object CSSStyleDeclaration]
// ⚠️ But can't forward result as smarti or stringref: SyntaxError: Failed to parse String to BigInt
// todo : how to communicate new string as RETURN type of arbitrary function from js to wasp?
// call Webview.getString(); ?
// embedder.trace('canvas = document.getElementById("canvas");')
// print(nod);
}
void testTypes() {
result = analyze(parse("int a"));
check_is(result.kind, Kind::reference);
check_is(result.type, IntegerType);// IntegerType
check_is(result.name, "a");
result = analyze(parse("string b"));
check_is(result.kind, Kind::reference);
check_is(result.type, StringType);
check_is(result.name, "b");
result = analyze(parse("float a,string b"));
let result0 = result[0];
check_is(result0.kind, Kind::reference);
// check_is(result0.kind, Kind::declaration);
// todo at this stage it should be a declaration?
check_is(result0.type, DoubleType);
check_is(result0.name, "a");
let result1 = result[1];
check_is(result1.kind, Kind::reference);
check_is(result1.type, StringType);
check_is(result1.name, "b");
}
void testTypedFunctions() {
// todo name 'id' clashes with 'id' in preRegisterFunctions()
result = analyze(parse("int tee(float b, string c){b}"));
check_is(result.kind, Kind::declaration);
check_is(result.name, "tee");
auto signature_node = result["@signature"];
// auto signature_node = result.metas()["signature"];
if (not signature_node.value.data)error("no signature");
Signature &signature = *(Signature *) signature_node.value.data;
check_is(signature.functions.first()->name, "tee")
check_is(signature.parameters.size(), 2)
check_is(signature.parameters.first().name, "b")
check_is(signature.parameters.first().type, reals);// use real / number for float64 float32
check_is(signature.parameters.last().name, "c")
check_is(signature.parameters.last().type, strings);
let params = signature.parameters.map(+[](Arg f) { return f.name; });
check_is(params.first(), "b");
}
void testEmptyTypedFunctions() {
// todo int a(){} should be compiler error
// todo do we really want / need int a(); void a(){} ?
// if(ch=='{' and next=='}' and previous==')'){
// actual.setType(declaration, false);// a(){} => def a!
// proceed();
// proceed();
// break;
// }
result = analyze(parse("int a(){}"));
check_is(result.kind, Kind::declaration);
check_is(result.name, "a");
auto signature_node = result["@signature"];
Signature signature = *(Signature *) signature_node.value.data;
check_is(signature.functions.first()->name, "a")
let names2 = signature.functions.map(+[](Function *f) {
return f->name;
});
check_is(names2.size(), 1);
check_is(names2.first(), "a");
result = analyze(parse("int a();"));
check_is(result.kind, Kind::declaration);// header signature
check_is(result.type, IntegerType);
check_is(result.name, "a");
}
void testPolymorphism() {
// debug:
// auto debug_node = parse("string aaa(string a){return a};\nfloat bbb(float b){return b+1}");
// auto debug_fun = analyze(debug_node);
auto node = parse("string test(string a){return a};\nfloat test(float b){return b+1}");
auto fun = analyze(node);
auto function = functions["test"];
check_is(function.is_polymorphic, true);
check_is(function.variants.size(), 2);
check_is(function.variants[0].signature.size(), 1);
// check_is(function.variants[0].signature.parameters[0].type, (Type) strings); todo
check_is(function.variants[0].signature.parameters[0].type, (Type) stringp);
auto variant = function.variants[1];
check_is(variant.signature.size(), 1);
check_is(variant.signature.parameters[0].type, (Type) float32);
}
void testPolymorphism2() {
auto node = parse("fun test(string a){return a};\nfun test(float b){return b+1}");
auto fun = analyze(node);
auto function = functions["test"];
check_is(function.is_polymorphic, true);
check_is(function.variants.size(), 2);
check_is(function.variants[0].signature.size(), 1);
check_is(function.variants[0].signature.parameters[0].type, (Type) int32);
check_is(function.variants[1].signature.size(), 1);
check_is(function.variants[1].signature.parameters[0].type, (Type) float32);
}
//#import "pow.h"
//void testOwnPowerExponentialLogarithm() {
// check_is(exp(1), 2.718281828459045);
// check_is(exp(5.5), 244.69193226422033);
// auto x = powerd(1.5, 5.5);
// printf("1.5^5.5=%f", x);
// check_eq(x, 9.30040636712988);
// auto x1 = powerd(2.5, 1.5);
// printf("2.5^1.5=%f", x1);
// check_eq(x1, 3.952847075210474);
// auto x2 = powerd(2.5, 3.5);
// check_eq(x2, 24.705294220065465);
//}
void testGenerics() {
auto type = Type(Generics{.kind = array, .value_type = int16});
// auto header= type.value & array;
// auto header= type.value & 0xFFFF0000; // todo <<
auto header = type.value & 0x0000FFFF; //todo ??
check_eq(header, array);
}
void testNumbers() {
Number n = 1;// as comfortable BigInt Object used inside wasp
check(n == 1.0);
check(n / 2 == 0.5);
check(((n * 2) ^ 10) == 1024);
}
void testFunctionDeclaration() {
// auto node1 = analyze(parse("fn main(){}"));
// check(node1.kind==declaration);
// check(node1.name=="main");
clearAnalyzerContext();
auto node2 = analyze(parse("fun test(float a):int{return a*2}"));
check(node2.kind == declaration);
check(node2.name == "test");
check_is(functions["test"].signature.size(), 1);
check_is(functions["test"].signature.parameters[0].name, "a");
check_is(functions["test"].signature.parameters[0].type, (Type) float_type);
check(functions["test"].body);
check_is(*functions["test"].body, analyze(parse("return a*2")));
}
void testRenameWasmFunction() {
Module &module1 = loadModule("samples/test.wasm");
module1.functions.at(0).name = "test";
module1.save("samples/test2.wasm");
}
void testPower() {
assert_equals(powi(10, 1), 10l);
assert_equals(powi(10, 2), 100l);
assert_equals(powi(10, 3), 1000l);
assert_equals(powi(10, 4), 10000l);
assert_equals(parseLong("8e6"), 8000000l);
skip(
assert_equals(parseLong("8e-6"), 1.0 / 8000000l);
)
assert_equals(parseDouble("8.333e-3"), 0.008333l);
assert_equals(parseDouble("8.333e3"), 8333.0l);
assert_equals(parseDouble("8.333e-3"), 0.008333l);
// assert_equals(ftoa(8.33333333332248946124e-03), "0.0083");
assert_equals(powi(10, 1), 10l);
assert_equals(powi(10, 2), 100l);
assert_equals(powi(10, 4), 10000l);
assert_equals(powi(2, 2), 4l);
assert_equals(powi(2, 8), 256l);
skip(
assert_equals(powd(2, -2), 1 / 4.);
assert_equals(powd(2, -8), 1 / 256.);
assert_equals(powd(10, -2), 1 / 100.);
assert_equals(powd(10, -4), 1 / 10000.);
)
}
void testMaps0() {
Map<int, long> map;
check(map.values[0] == map[0]);
check(map.values == &(map[0]));
map[0] = 2;
check(map.values[0] == 2);
check(map.size() == 1);
map[2] = 4;
check(map.size() == 2);
check(map.values[1] == 4);
check(map.keys[1] == 2);
print((int) map[0]);
print((int) map[2]);
print(map[(size_t) 0]);
print(map[(size_t) 1]);
check(map[0] == 2);
check(map[2] == 4);
}
void testMapOfStrings() {
Map<String, chars> map;
map["a"] = "1";
check(map.size() == 1);
map["a"] = "1";
check(map.size() == 1);
check(map.keys[0] == "a");
check(map.values[0] == "1"s);
check(map["a"] == "1"s);
// check(!map.has("b"));
check(map.position("b") == -1);
map["b"] = "2";
check(map.size() == 2);
check(map.keys[1] == "b");
check(map.values[1] == "2"s);
check(map["b"] == "2"s);
}
void testMapOfStringValues() {
Map<chars, String> map;
map["a"] = "1";
check(map.size() == 1);
check(map.keys[0] == "a"s);
check(map.values[0] == "1");
check(map["a"] == "1");
map["b"] = "2";
check(map.size() == 2);
check(map.keys[1] == "b"s);
check(map.values[1] == "2");
check(map["b"] == "2");
}
void testMaps1() {
functions.clear();
functions.insert_or_assign("abcd", {.name="abcd"});
functions.insert_or_assign("efg", {.name="efg"});
check_is(functions.size(), 2);
check(functions["abcd"].name == "abcd");
check(functions["efg"].name == "efg");
}
void testMaps2() {
functions.clear();
Function abcd;
abcd.name = "abcd";
functions["abcd"] = abcd;
functions["efg"] = {.name="efg"};
functions["abcd"] = {.name="abcd"};
functions["efg"] = {.name="efg"};
check_is(functions.size(), 2);
print(functions["abcd"]);
print(functions["abcd"].name);
check(functions["abcd"].name == "abcd");
check(functions["efg"].name == "efg");
}
void testMaps() {
testMaps0();// ok
testMapOfStrings();
testMapOfStringValues();
testMaps1();
testMaps2();// now ok
}
void testHex() {
assert_equals(hex(18966001896603L), "0x113fddce4c9b");
assert_is("42", 42);
assert_is("0xFF", 255);
assert_is("0x100", 256);
assert_is("0xdce4c9b", 0xdce4c9b);
// assert_is("0x113fddce4c9b", 0x113fddce4c9bl); todo
// assert_is("0x113fddce4c9b", 0x113fddce4c9bL);
}
void test_fd_write() { // built-in wasi function
// assert_emit("x='hello';fd_write(1,20,1,8)", (int64) 0);// 20 = &x+4 {char*,len}
// assert_emit("puts 'ok';proc_exit(1)\nputs 'no'", (int64) 0);
// assert_emit("quit",0);
assert_emit("x='hello';fd_write(1,x,1,8)", (int64) 0);// &x+4 {char*,len}
// assert_run("len('123')", 3); // Map::len
// quit()
assert_emit("puts 'ok'", (int64) 0); // connect to wasi fd_write
loadModule("wasp");
assert_emit("puts 'ok'", (int64) 0);
assert_emit("puti 56", 56);
assert_emit("putl 56", 56);
// assert_emit("putx 56", 56);
assert_emit("putf 3.1", 0);
check(module_cache.has("wasp-runtime.wasm"s.hash()))
}
void testEnumConversion() {
#if not TRACE
Valtype yy = (Valtype) Primitive::charp;
int i = (int) Primitive::charp;
int i1 = (int) yy;// CRASHES in Trace mode WHY?
check_is(stackItemSize(Primitive::wasm_float64), 8);
check_is(i, i1);
check((Type) Primitive::charp == yy);
check((Type) yy == Primitive::charp);
check(Primitive::charp == (Type) yy);
check(yy == (Type) Primitive::charp);
check((int) yy == (int) Primitive::charp);
#endif
}
void bindgen(Node &n) {
// todo parserOptions => formatOptions => Format ! inheritable
// todo interface host-funcs {current-user: func() -> string}
print(n.serialize());
}
// https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md#item-use
void testUse() {
parse("use * from other-file");
parse("use { a, list, of, names } from another-file");
parse("use { name as other-name } from yet-another-file");
// MY SYNTAX :
parse("use other-file");// MY SYNTAX
parse("use all from other-file");// MY SYNTAX redundant
parse("use name from yet-another-file");// MY SYNTAX
parse("use name from yet-another-file as other-name");// MY SYNTAX
// parse("use name as other-name from yet-another-file");// MY SYNTAX
}
void testClass() {
analyze(parse("public data class Person(string FirstName, string LastName);"));
analyze(parse("public data class Student : Person { int ID; }"));
analyze(parse("var person = new Person('Scott', 'Hunter'); // positional construction"));
analyze(parse("otherPerson = person with { LastName = \"Hanselman\" };"));
// "var (f, l) = person; // positional deconstruction"
}
void testStruct() { // builtin with struct/record
assert_emit("struct a{x:int y:int z:int};a{1 3 4}.y", 3);
return;
assert_emit("struct a{x:int y:float};a{1 3.2}.y", 3.2);
assert_emit("struct a{x:int y:float};b a{1 .2};b.y", .2);
assert_emit("struct a{x:int y:float};b:a{1 .2};b.y", .2);
assert_emit("struct a{x:int y:float};b=a{1 .2};b.y", .2);
assert_emit("struct a{x:int y:float};a b{1 .2};b.y", .2);
assert_emit("record a{x:u32 y:float32};a b{1 .2};b.y", .2);
assert_emit(R"(
record person {
name: string,
age: u32,
has-lego-action-figure: bool,
}; x=person{age:22}; x.age)", 22); // todo require optional fields marked as such with '?'
}
void testStruct2() {
const char *code0 = "struct point{a:int b:int c:string}";
Node &node = parse(code0);
// assert_equals(node.kind, Kind::structs);
assert_equals(node.length, 3);
assert_equals(IntegerType, node[1].type);
// const char *code = "struct point{a:int b:int c:string};x=point(1,2,'ok');x.b";
// basal node_pointer act as structs
assert_emit("point{a:int b:int c:string};x=point(1,2,'ok');x.b", 2)
assert_emit("data=[1,2,3];struct point{a:int b:int c:string};x=data as struct;x.b", 2)
}
void test_c_numbers() {
// check(0x1000000000000000l==powi(2,60))
unsigned int x = -1;
unsigned int y = 0xFFFFFFFF;
// signed int biggest = 0x7FFFFFFF;
// signed int smallest = 0x80000000;// "implementation defined" so might not always pass
signed int z = -1;
check(x == y)
check(x == z)
check(z == y)
check((int) -1 == (unsigned int) 0xFFFFFFFF)
}
void testArraySize() { // todo!
// There should be one-- and preferably only one --obvious way to do it.
// requires struct lookup and aliases
assert_emit("pixel=[1 2 4];#pixel", 3);
// assert_emit("pixel=[1 2 4];pixel#", 3);
assert_emit("pixel=[1 2 4];pixel size", 3);
assert_emit("pixel=[1 2 4];pixel length", 3);
assert_emit("pixel=[1 2 4];pixel count", 3);
assert_emit("pixel=[1 2 4];pixel number", 3);// ambivalence with type number!
assert_emit("pixel=[1 2 4];pixel.size", 3);
assert_emit("pixel=[1 2 4];pixel.length", 3);
assert_emit("pixel=[1 2 4];pixel.count", 3);
assert_emit("pixel=[1 2 4];pixel.number", 3);// ambivalence cast
assert_emit("pixels=[1 2 4];number of pixels ", 3);
assert_emit("pixels=[1 2 4];size of pixels ", 3);
assert_emit("pixels=[1 2 4];length of pixels ", 3);
assert_emit("pixels=[1 2 4];count of pixels ", 3);
assert_emit("pixel=[1 2 3];pixel.add(5);#pixel", 4);
}
void testArrayOperations() { // todo!
testArraySize();
// todo 'do' notation to modify versus return different list!
assert_emit("pixel=[1 2 3];do add 4 to pixel; pixel", Node(1, 2, 3, 4, 0));
assert_emit("pixel=[1 2 3];y=pixel + 4; pixel", Node(1, 2, 3, 0));
// assert_throws("pixel=[1 2 3];pixel + 4;pixel");// unused non-mutating operation
assert_emit("pixels=[1 2 4];pixel#3", 4);// plural!
assert_emit("pixel=[1 2 3];pixel + [4]", Node(1, 2, 3, 4, 0));
assert_emit("pixel=[1 2 3];pixel + 4", Node(1, 2, 3, 4, 0));
assert_emit("pixel=[1 2 3];pixel<<4", Node(1, 2, 3, 4, 0));
assert_emit("pixel=[1 2 3];4>>pixel", Node(4, 1, 2, 3, 0));
assert_emit("pixel=[1 2 3];add(pixel, 4)", Node(1, 2, 3, 4, 0));// julia style
assert_emit("pixel=[1 2 3];add 4 to pixel", Node(1, 2, 3, 4, 0));
assert_emit("pixel=[1 2 3];pixel.add 4", Node(1, 2, 3, 4, 0));
assert_emit("pixel=[1 2 3];pixel add 4", Node(1, 2, 3, 4, 0));
assert_emit("pixel=[1 2 3];pixel.add(4)", Node(1, 2, 3, 4, 0));
assert_emit("pixel=[1 2 3];pixel.insert 4", Node(1, 2, 3, 4, 0));
assert_emit("pixel=[1 2 3];pixel insert 4", Node(1, 2, 3, 4, 0));
assert_emit("pixel=[1 2 3];pixel.insert(4)", Node(1, 2, 3, 4, 0));
assert_emit("pixel=[1 2 3];pixel.insert(4,-1)", Node(1, 2, 3, 4, 0));
assert_emit("pixel=[1 2 3];pixel.insert 4 at end", Node(1, 2, 3, 4, 0));
assert_emit("pixel=[1 2 3];pixel.insert 4 at -1", Node(1, 2, 3, 4, 0));
assert_emit("pixel=[1 2 3];insert 4 at end of pixel", Node(1, 2, 3, 4, 0));
assert_emit("pixel=[1 2 3];pixel.insert(4,0)", Node(4, 1, 2, 3, 0));
assert_emit("pixel=[1 2 3];pixel.insert 4 at 0", Node(4, 1, 2, 3, 0));
assert_emit("pixel=[1 2 3];pixel.insert 4 at start", Node(4, 1, 2, 3, 0));
assert_emit("pixel=[1 2 3];pixel.insert 4 at head", Node(4, 1, 2, 3, 0));
assert_emit("pixel=[1 2 3];pixel.insert 4 at beginning", Node(4, 1, 2, 3, 0));
assert_emit("pixels=[1 2 3];insert 4 at start of pixels", Node(4, 1, 2, 3, 0));
assert_emit("pixel=[1 2 3];pixel - [3]", Node(1, 2, 0));
assert_emit("pixel=[1 2 3];pixel - 3", Node(1, 2, 0));
assert_emit("pixel=[1 2 3];remove [3] from pixel", Node(1, 2, 0));
assert_emit("pixel=[1 2 3];remove 3 from pixel", Node(1, 2, 0));
assert_emit("pixel=[1 2 3];pixel.remove(3)", Node(1, 2, 0));
assert_emit("pixel=[1 2 3];pixel.remove 3", Node(1, 2, 0));
assert_emit("pixel=[1 2 3];pixel remove(3)", Node(1, 2, 0));
assert_emit("pixel=[1 2 3];pixel remove 3", Node(1, 2, 0));
assert_emit("pixel=[1 2 3];pixel.remove([3])", Node(1, 2, 0));
assert_emit("pixel=[1 2 3];pixel.remove [3]", Node(1, 2, 0));
assert_emit("pixel=[1 2 3];pixel remove([3])", Node(1, 2, 0));
assert_emit("pixel=[1 2 3];pixel remove [3]", Node(1, 2, 0));
assert_emit("pixel=[1 2 3 4];pixel.remove([3 4])", Node(1, 2, 0));
assert_emit("pixel=[1 2 3 4];pixel.remove [3 4]", Node(1, 2, 0));
assert_emit("pixel=[1 2 3 4];pixel remove([3 4])", Node(1, 2, 0));
assert_emit("pixel=[1 2 3 4];pixel remove [3 4]", Node(1, 2, 0));
assert_emit("pixel=[1 2 3 4];pixel remove 3 4", Node(1, 2, 0));
assert_emit("pixel=[1 2 3 4];pixel remove (3 4)", Node(1, 2, 0));
assert_emit("pixels=[1 2 3 4];pixels without (3 4)", Node(1, 2, 0));
}
void testArrayCreation() {
// skip(
// todo create empty array
assert_emit("pixel=[];pixel[1]=15;pixel[1]", 15);
assert_emit("pixel=();pixel#1=15;pixel#1", 15);// diadic ternary operator
assert_emit("pixel array;pixel#1=15;pixel#1", 15);
assert_emit("pixel:int[100];pixel[1]=15;pixel[1]", 15);
assert_emit("pixel=int[100];pixel[1]=15;pixel[1]", 15);// todo wasp can't distinguish type ':' from value '=' OK?
assert_emit("pixel: 100 int;pixel[1]=15;pixel[1]", 15);// number times type = typed array
}
void testIndexOffset() {
assert_emit("(2 4 3)[1]", 4);
assert_emit("(2 4 3)#2", 4);
assert_emit("y=(1 4 3)#2", 4);
assert_emit("y=(1 4 3)[1]", 4);
assert_is("x=(1 4 3);x#2=5;x#2", 5);
assert_is("x=(1 4 3);z=(9 8 7);x#2", 4);
assert_emit("x=(5 6 7);y=(1 4 3);y#2", 4);
assert_emit("x=(5 6 7);(1 4 3)#2", 4);
skip(
assert_emit("y=(1 4 3);y[1]", 4);// CAN NOT WORK in data_mode because y[1] ≈ y:1 setter
assert_emit("x=(5 6 7);y=(1 4 3);y[1]", 4);
)
assert_emit("(5 6 7);(2 4 3)[0]", 2);
assert_emit("x=(5 6 7);y=(1 4 3);y#2", 4);
assert_emit("(5 6 7);(1 4 3)#2", 4);
assert_emit("x=(5 6 7);(1 4 3)#2", 4);
skip(
assert_emit("puts('ok');(1 4 3)#2", 4);
)
assert_emit("x=0;while x++<11: nop;", 0);
assert_emit("i=10007;x=i%10000", 7);
assert_emit("k=(1,2,3);i=1;k#i=4;k#1", 4)
assert_emit("k=(1,2,3);i=1;k#i=4;k#1", 4)
assert_emit("maxi=3840*2160", 3840 * 2160);
assert_emit("i=10007;x=i%10000", 7);
assert_is("x=(1 4 3);x#2=5;x#2", 5);
assert_is("x=(1 4 3);x#2", 4);
}
void testFlagSafety() {
auto code = "flags empty_flags{}; empty_flags mine = data_mode | space_brace;";
assert_throws(code) // "data_mode not a member of empty_flags"s
assert_throws("enum cant_combine{a;b}; a+b;");
assert_throws("enum context_x{a;b};enum context_y{b;c};b;");
}
void testFlags2() {
// todo allow just parser-flags{…} in wasp > wit
auto code = R"(flags parser-flags{
data_mode
arrow
space_brace
}
parser-flags my_flags = data_mode + space_brace
)";
assert_emit(code, 5)// 1+4
clearAnalyzerContext();
Node &parsed = parse(code, {.kebab_case=true});
Node &node = analyze(parsed);
check(types.has("parser-flags"))
check(globals.has("data_mode"))
check(globals.has("parser-flags.data_mode")) //
Node &parserFlags = node.first();
// todo AddressSanitizer:DEADLYSIGNAL why? lldb does'nt fail here
check(parserFlags.name == "parser-flags")
check(parserFlags.kind == flags)
check(parserFlags.length == 3)
check(parserFlags[1].name == "arrow")
check(parserFlags[2].value.longy == 4)
Node &instance = node.last();
print(instance);
check(instance.name == "my_flags")
check(instance.type)
check(instance.type->name == "parser-flags") // deduced!
check(instance.kind == flags) // kind? not really type! todo?
Node my_flags = instance.interpret();
print(my_flags);
check(my_flags.value.longy == 5) // 1+4 bit internal detail!
skip(
check(my_flags.values().serialize() == "data_mode + space_brace")
)
// check(node.last().serialize() == "ParserOptions my_flags = data_mode | space_brace") // todo canonical type serialization!?
}
void testFlags() {
clearAnalyzerContext();
Node &parsed = parse("flags abc{a b c}");
Node &node = analyze(parsed);
check(node.name == "abc")
check(node.kind == flags)
check(node.length == 3);
check(node[0].name == "a");
check_is(typeName(node[0].kind), typeName(flag_entry));
check_is(node[0].kind, flag_entry);
check(node[0].kind == flag_entry);
check(node[0].value.longy == 1);
check(node[0].type);
check(node[0].type == node);
check(node[1].value.longy == 2);
check(node[2].value.longy == 4);
}
void testPattern() {
result = parse("y[1]", ParserOptions{.data_mode=false});
check(result[0].kind == patterns);
check(result[0][0].kind == longs);
check(result[0][0].value.longy == 1);
// assert_emit("(2 4 3)[0]", 2);
}
void testWitInterface() {
Node &mod = Node("host-funcs").setType(modul).add(Node("current-user").setType(functor).add(StringType));
assert_emit("interface host-funcs {current-user: func() -> string}", mod)
}
void testWitExport() {
const char *code = "struct point{x:int y:float}";
Node &node = parse(code);
bindgen(node);
}
void testWitFunction() {
// funcDeclaration
// a:b,c vs a:b, c:d
assert_emit("add: func(a: float32, b: float32) -> float32", 0);
Module &mod = read_wasm("test.wasm");
print(mod.import_count);
check_is(mod.import_count, 1)
check_is(Node().setType(longs).serialize(), "0")
check_is(mod.import_names, List<String>{"add"});// or export names?
}
void testWitImport() {
}
void testEqualsBinding() {
// colon closes with space, not semicolon !
parse("a = float32, b: float32");
check(result.length == 1);
check(result["a"] == "float32");
Node val;
val.add(Node("float32"));
val.add(Node("b").add(Node("float32")));
check_is(result[0], val);
}
void testColonImmediateBinding() {
// colon closes with space, not semicolon !
result = parse("a: float32, b: float32");
check(result.length == 2);
check(result["a"] == "float32");
check(result[0] == Node("a").add(Node("float32")));
check(result[1] == Node("b").add(Node("float32")));
}
void testWit() {
// testWitFunction();
// testWitInterface();
Node wit;
wit = (new WitReader())->read("test/merge/world.wit");
wit = (new WitReader())->read("samples/bug.wit");
wit = (new WitReader())->read("test/merge/example_dep/index.wit");
wit = (new WitReader())->read("test/merge/index.wit");
wit = (new WitReader())->read("samples/wit/typenames.wit");
wit = (new WitReader())->read("samples/wit/wasi_unstable.wit");
// check(wit.length > 0);
}
void testHyphenUnits() {
// const char *code = "1900 - 2000 AD";// (easy with units)
// assert_analyze(code,"{kind=range type=AD value=(1900,2000)}");
// todo how does Julia represent 10 ± 2 m/s ?
assert_is("1900 - 2000 AD == 1950 AD ± 50", true);
assert_is("1900 - 2000 cm == 1950 cm ± 50", true);
assert_is("1900 - 2000 cm == 1950 ± 50 cm ", true);
}
void testHypenVersusMinus() {
const char *code = "a=-1 b=2 b-a";
assert_emit(code, 3);
// kebab case
const char *data = "a-b:2 c-d:4 a-b";
assert_emit(data, 2);
// testHyphenUnits();
// Node &node = parse(data);
}
void testKebabCase() {
testHypenVersusMinus();
}
/*
0 0 1
64 40 2
8192 2000 3
1048576 100000 4
134217728 8000000 5
17179869184 400000000 6
2199023255552 20000000000 7
281474976710656 1000000000000 8
36028797018963968 80000000000000 9
0 0 1
-65 ffffffffffffffbf 2
-8193 ffffffffffffdfff 3
-1048577 ffffffffffefffff 4
-134217729 fffffffff7ffffff 5
-17179869185 fffffffbffffffff 6
-2199023255553 fffffdffffffffff 7
-281474976710657 fffeffffffffffff 8
-36028797018963969 ff7fffffffffffff 9
*/
// only test once, see lebByteSize for result
void testLebByteSize() {
check_eq(lebByteSize((int64) -17179869185 + 1), 5)
check_eq(lebByteSize((int64) -17179869185), 6)
check_eq(lebByteSize((int64) -17179869185 - 1), 6)
short last = 1;
for (int64 i = -63; i > -0x100000000000000l; --i) {
// for (int64 i = 0; i < 0x10000000000000l; ++i) {
// for (uint64 i = 0; i < 0x100000000000000; ++i) {
short size = lebByteSize(i);
if (size > last) {
// printf("%ld %lx %d\n", i, i, size);
last = size;
i = i * 128 + 129;
}
}
}
void testListGrow() {
// tested once, ok
return;
List<int> oh = {0, 1, 2, 3};
for (int i = 4; i < 1000000000; ++i) {
oh.add(i);
unsigned int ix = random() % i;
check_silent(oh[ix] == ix)
}
String aok = "ok";
List<String> ja;// = {ok};
ja.add(aok);
String &o1 = ja[0];
ja.grow();
String &o3 = ja[0];
check(o1.data == o3.data);
o3.data = (char *) "hu";
check(o1.data == o3.data);
}
void testWasmRunner() {
// int result = run_wasm("test/test42.wasm");
// assert_equals(result, 42);
}
void testLeaks() {
int reruns = 0;
// int reruns = 100000;
for (int i = 0; i < reruns; ++i) {//
printf("\n\n ===========================================\n%d\n\n\n", i);
// assert_emit("i=-9;√-i", 3);// SIGKILL after about 3000 emits OK'ish ;)
assert_run("i=-9;√-i", 3);// SIGKILL after about 120 runs … can be optimized ;)
}
}
void testWrong0Termination() {
#ifndef WASM
List<String> builtin_constants = {"pi", "π"};
assert_equals(builtin_constants.size(), 2);// todo
#endif
}
void testDeepColon() {
result = parse("current-user: func() -> string");
check_is(result.kind, key);
check_is(result.values().name, "func");
check_is(result.values().values().name, "string");
};
void testDeepColon2() {
result = parse("a:b:c:d");
check_is(result.kind, key);
check_is(result.values().name, "b");
check_is(result.values().values().values().name, "d");
};
void testStupidLongLong() {
// int a;
// int64 b;// 4 byte in wasm/windows grr
// int64 c;// 8 bytes everywhere (still not guaranteed grr)
float a;
double b;
long double c;// float128 16 byte in wasm wow, don't use anyways;)
print((int) sizeof(a));
print((int) sizeof(b));
print((int) sizeof(c));// what? 16 bytes!?
}
void testFloatReturnThroughMain() {
double x = 0.0000001;// 3e...
// double x=1000000000.1;// 4...
// double x=-1000000000.1;// c1…
// double x=9999999999999999.99999999;// 43…
// double x=-9999999999999999.99999999;// c3…
// double x=1.1;// 3ff199999999999a
// double x=-1.1;// bff199999999999a
int64 y = *(int64 *) &x;
#ifndef WASM
printf("%llx\n", y);
#endif
y = 0x00FF000000000000;// -> 0.000000 OK
x = *(double *) &y;
printf("%lf\n", x);
}
void testArrayS() {
auto node = analyze(parse("int"));
// assert_equals( node.type->kind, classe);
assert_equals(node.kind, clazz);
auto node2 = analyze(parse("ints"));
assert_equals(node2.kind, arrays);// type: array<int>
node = parse("ints x");
// assert_equals( node.kind, reference);
// assert_equals( node.kind, arrays);
assert_equals(node.kind, groups);
assert_equals(node.type, &DoubleType);
}
void testArrayInitialization() {// via Units
assert_emit("x : int[100]; x.length", 100)
assert_emit("x : u8 * 100; x.length", 100) // type times size operation!!
assert_emit("x : 100 * int; x.length", 100)
assert_emit("x : 100 * ints; x.length", 100)
assert_emit("x : 100 ints; x.length", 100) // implicit multiplication, no special case!
assert_emit("x : 100 int; x.length", 100)
assert_emit("x : 100 integers; x.length", 100)
assert_emit("x : 100 numbers; x.length", 100)
assert_emit("x is 100 times [0]; x.length", 100)
assert_emit("x is array of size 100; x.length", 100)
assert_emit("x is an 100 integer array; x.length", 100)
assert_emit("x is a 100 integer array; x.length", 100)
assert_emit("x is a 100 element array; x.length", 100)