aboutsummaryrefslogtreecommitdiff
path: root/test/run_tests.cpp
blob: 3e665dbef5941bf4db781cb1e92c542d6f25c328 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
/*
 * Copyright (c) 2015 Andrew Kelley
 *
 * This file is part of zig, which is MIT licensed.
 * See http://opensource.org/licenses/MIT
 */

#include "list.hpp"
#include "buffer.hpp"
#include "os.hpp"
#include "error.hpp"

#include <stdio.h>
#include <stdarg.h>

struct TestSourceFile {
    const char *relative_path;
    const char *source_code;
};

struct TestCase {
    const char *case_name;
    const char *output;
    ZigList<TestSourceFile> source_files;
    ZigList<const char *> compile_errors;
    ZigList<const char *> compiler_args;
    ZigList<const char *> program_args;
    bool is_parseh;
    bool is_self_hosted;
};

static ZigList<TestCase*> test_cases = {0};
static const char *tmp_source_path = ".tmp_source.zig";
static const char *tmp_h_path = ".tmp_header.h";

#if defined(_WIN32)
static const char *tmp_exe_path = "./.tmp_exe.exe";
static const char *zig_exe = "./zig.exe";
#define NL "\r\n"
#else
static const char *tmp_exe_path = "./.tmp_exe";
static const char *zig_exe = "./zig";
#define NL "\n"
#endif

static void add_source_file(TestCase *test_case, const char *path, const char *source) {
    test_case->source_files.add_one();
    test_case->source_files.last().relative_path = path;
    test_case->source_files.last().source_code = source;
}

static TestCase *add_simple_case(const char *case_name, const char *source, const char *output) {
    TestCase *test_case = allocate<TestCase>(1);
    test_case->case_name = case_name;
    test_case->output = output;

    test_case->source_files.resize(1);
    test_case->source_files.at(0).relative_path = tmp_source_path;
    test_case->source_files.at(0).source_code = source;

    test_case->compiler_args.append("build");
    test_case->compiler_args.append(tmp_source_path);
    test_case->compiler_args.append("--export");
    test_case->compiler_args.append("exe");
    test_case->compiler_args.append("--name");
    test_case->compiler_args.append("test");
    test_case->compiler_args.append("--output");
    test_case->compiler_args.append(tmp_exe_path);
    test_case->compiler_args.append("--release");
    test_case->compiler_args.append("--strip");
    test_case->compiler_args.append("--color");
    test_case->compiler_args.append("on");
    test_case->compiler_args.append("--check-unused");

    test_cases.append(test_case);

    return test_case;
}

static TestCase *add_simple_case_libc(const char *case_name, const char *source, const char *output) {
    TestCase *tc = add_simple_case(case_name, source, output);
    tc->compiler_args.append("--library");
    tc->compiler_args.append("c");
    return tc;
}

static TestCase *add_compile_fail_case(const char *case_name, const char *source, int count, ...) {
    va_list ap;
    va_start(ap, count);

    TestCase *test_case = allocate<TestCase>(1);
    test_case->case_name = case_name;
    test_case->source_files.resize(1);
    test_case->source_files.at(0).relative_path = tmp_source_path;
    test_case->source_files.at(0).source_code = source;

    for (int i = 0; i < count; i += 1) {
        const char *arg = va_arg(ap, const char *);
        test_case->compile_errors.append(arg);
    }

    test_case->compiler_args.append("build");
    test_case->compiler_args.append(tmp_source_path);

    test_case->compiler_args.append("--name");
    test_case->compiler_args.append("test");

    test_case->compiler_args.append("--export");
    test_case->compiler_args.append("obj");

    test_case->compiler_args.append("--output");
    test_case->compiler_args.append(tmp_exe_path);

    test_case->compiler_args.append("--release");
    test_case->compiler_args.append("--strip");
    test_case->compiler_args.append("--check-unused");

    test_cases.append(test_case);

    va_end(ap);

    return test_case;
}

static TestCase *add_parseh_case(const char *case_name, const char *source, int count, ...) {
    va_list ap;
    va_start(ap, count);

    TestCase *test_case = allocate<TestCase>(1);
    test_case->case_name = case_name;
    test_case->is_parseh = true;

    test_case->source_files.resize(1);
    test_case->source_files.at(0).relative_path = tmp_h_path;
    test_case->source_files.at(0).source_code = source;

    for (int i = 0; i < count; i += 1) {
        const char *arg = va_arg(ap, const char *);
        test_case->compile_errors.append(arg);
    }

    test_case->compiler_args.append("parseh");
    test_case->compiler_args.append(tmp_h_path);
    test_case->compiler_args.append("--verbose");

    test_cases.append(test_case);

    va_end(ap);
    return test_case;
}

static void add_compiling_test_cases(void) {
    add_simple_case_libc("hello world with libc", R"SOURCE(
const c = @c_import(@c_include("stdio.h"));
export fn main(argc: c_int, argv: &&u8) -> c_int {
    c.puts(c"Hello, world!");
    return 0;
}
    )SOURCE", "Hello, world!" NL);

    {
        TestCase *tc = add_simple_case("multiple files with private function", R"SOURCE(
use @import("std").io;
use @import("foo.zig");

pub fn main(args: [][]u8) -> %void {
    private_function();
    %%stdout.printf("OK 2\n");
}

fn private_function() {
    print_text();
}
        )SOURCE", "OK 1\nOK 2\n");

        add_source_file(tc, "foo.zig", R"SOURCE(
use @import("std").io;

// purposefully conflicting function with main.zig
// but it's private so it should be OK
fn private_function() {
    %%stdout.printf("OK 1\n");
}

pub fn print_text() {
    private_function();
}
        )SOURCE");
    }

    {
        TestCase *tc = add_simple_case("import segregation", R"SOURCE(
use @import("foo.zig");
use @import("bar.zig");

pub fn main(args: [][]u8) -> %void {
    foo_function();
    bar_function();
}
        )SOURCE", "OK\nOK\n");

        add_source_file(tc, "foo.zig", R"SOURCE(
use @import("std").io;
pub fn foo_function() {
    %%stdout.printf("OK\n");
}
        )SOURCE");

        add_source_file(tc, "bar.zig", R"SOURCE(
use @import("other.zig");
use @import("std").io;

pub fn bar_function() {
    if (foo_function()) {
        %%stdout.printf("OK\n");
    }
}
        )SOURCE");

        add_source_file(tc, "other.zig", R"SOURCE(
#static_eval_enable(false)
pub fn foo_function() -> bool {
    // this one conflicts with the one from foo
    return true;
}
        )SOURCE");
    }

    {
        TestCase *tc = add_simple_case("two files use import each other", R"SOURCE(
use @import("a.zig");

pub fn main(args: [][]u8) -> %void {
    ok();
}
        )SOURCE", "OK\n");

        add_source_file(tc, "a.zig", R"SOURCE(
use @import("b.zig");
const io = @import("std").io;

pub const a_text = "OK\n";

pub fn ok() {
    %%io.stdout.printf(b_text);
}
        )SOURCE");

        add_source_file(tc, "b.zig", R"SOURCE(
use @import("a.zig");

pub const b_text = a_text;
        )SOURCE");
    }



    add_simple_case("hello world without libc", R"SOURCE(
const io = @import("std").io;

pub fn main(args: [][]u8) -> %void {
    %%io.stdout.printf("Hello, world!\n");
}
    )SOURCE", "Hello, world!\n");


    add_simple_case_libc("number literals", R"SOURCE(
const c = @c_import(@c_include("stdio.h"));

export fn main(argc: c_int, argv: &&u8) -> c_int {
    c.printf(c"\n");

    c.printf(c"0: %llu\n",
             u64(0));
    c.printf(c"320402575052271: %llu\n",
         u64(320402575052271));
    c.printf(c"0x01236789abcdef: %llu\n",
         u64(0x01236789abcdef));
    c.printf(c"0xffffffffffffffff: %llu\n",
         u64(0xffffffffffffffff));
    c.printf(c"0x000000ffffffffffffffff: %llu\n",
         u64(0x000000ffffffffffffffff));
    c.printf(c"0o1777777777777777777777: %llu\n",
         u64(0o1777777777777777777777));
    c.printf(c"0o0000001777777777777777777777: %llu\n",
         u64(0o0000001777777777777777777777));
    c.printf(c"0b1111111111111111111111111111111111111111111111111111111111111111: %llu\n",
         u64(0b1111111111111111111111111111111111111111111111111111111111111111));
    c.printf(c"0b0000001111111111111111111111111111111111111111111111111111111111111111: %llu\n",
         u64(0b0000001111111111111111111111111111111111111111111111111111111111111111));

    c.printf(c"\n");

    c.printf(c"0.0: %a\n",
         f64(0.0));
    c.printf(c"0e0: %a\n",
         f64(0e0));
    c.printf(c"0.0e0: %a\n",
         f64(0.0e0));
    c.printf(c"000000000000000000000000000000000000000000000000000000000.0e0: %a\n",
         f64(000000000000000000000000000000000000000000000000000000000.0e0));
    c.printf(c"0.000000000000000000000000000000000000000000000000000000000e0: %a\n",
         f64(0.000000000000000000000000000000000000000000000000000000000e0));
    c.printf(c"0.0e000000000000000000000000000000000000000000000000000000000: %a\n",
         f64(0.0e000000000000000000000000000000000000000000000000000000000));
    c.printf(c"1.0: %a\n",
         f64(1.0));
    c.printf(c"10.0: %a\n",
         f64(10.0));
    c.printf(c"10.5: %a\n",
         f64(10.5));
    c.printf(c"10.5e5: %a\n",
         f64(10.5e5));
    c.printf(c"10.5e+5: %a\n",
         f64(10.5e+5));
    c.printf(c"50.0e-2: %a\n",
         f64(50.0e-2));
    c.printf(c"50e-2: %a\n",
         f64(50e-2));

    c.printf(c"\n");

    c.printf(c"0x1.0: %a\n",
         f64(0x1.0));
    c.printf(c"0x10.0: %a\n",
         f64(0x10.0));
    c.printf(c"0x100.0: %a\n",
         f64(0x100.0));
    c.printf(c"0x103.0: %a\n",
         f64(0x103.0));
    c.printf(c"0x103.7: %a\n",
         f64(0x103.7));
    c.printf(c"0x103.70: %a\n",
         f64(0x103.70));
    c.printf(c"0x103.70p4: %a\n",
         f64(0x103.70p4));
    c.printf(c"0x103.70p5: %a\n",
         f64(0x103.70p5));
    c.printf(c"0x103.70p+5: %a\n",
         f64(0x103.70p+5));
    c.printf(c"0x103.70p-5: %a\n",
         f64(0x103.70p-5));

    c.printf(c"\n");

    c.printf(c"0b10100.00010e0: %a\n",
         f64(0b10100.00010e0));
    c.printf(c"0o10700.00010e0: %a\n",
         f64(0o10700.00010e0));

    return 0;
}
    )SOURCE", R"OUTPUT(
0: 0
320402575052271: 320402575052271
0x01236789abcdef: 320402575052271
0xffffffffffffffff: 18446744073709551615
0x000000ffffffffffffffff: 18446744073709551615
0o1777777777777777777777: 18446744073709551615
0o0000001777777777777777777777: 18446744073709551615
0b1111111111111111111111111111111111111111111111111111111111111111: 18446744073709551615
0b0000001111111111111111111111111111111111111111111111111111111111111111: 18446744073709551615

0.0: 0x0p+0
0e0: 0x0p+0
0.0e0: 0x0p+0
000000000000000000000000000000000000000000000000000000000.0e0: 0x0p+0
0.000000000000000000000000000000000000000000000000000000000e0: 0x0p+0
0.0e000000000000000000000000000000000000000000000000000000000: 0x0p+0
1.0: 0x1p+0
10.0: 0x1.4p+3
10.5: 0x1.5p+3
10.5e5: 0x1.0059p+20
10.5e+5: 0x1.0059p+20
50.0e-2: 0x1p-1
50e-2: 0x1p-1

0x1.0: 0x1p+0
0x10.0: 0x1p+4
0x100.0: 0x1p+8
0x103.0: 0x1.03p+8
0x103.7: 0x1.037p+8
0x103.70: 0x1.037p+8
0x103.70p4: 0x1.037p+12
0x103.70p5: 0x1.037p+13
0x103.70p+5: 0x1.037p+13
0x103.70p-5: 0x1.037p+3

0b10100.00010e0: 0x1.41p+4
0o10700.00010e0: 0x1.1c0001p+12
)OUTPUT");

    add_simple_case("order-independent declarations", R"SOURCE(
const io = @import("std").io;
const z = io.stdin_fileno;
const x : @typeof(y) = 1234;
const y : u16 = 5678;
pub fn main(args: [][]u8) -> %void {
    var x_local : i32 = print_ok(x);
}
fn print_ok(val: @typeof(x)) -> @typeof(foo) {
    %%io.stdout.printf("OK\n");
    return 0;
}
const foo : i32 = 0;
    )SOURCE", "OK\n");

    add_simple_case("for loops", R"SOURCE(
const io = @import("std").io;

pub fn main(args: [][]u8) -> %void {
    const array = []u8 {9, 8, 7, 6};
    for (array) |item| {
        %%io.stdout.print_u64(item);
        %%io.stdout.printf("\n");
    }
    for (array) |item, index| {
        %%io.stdout.print_i64(index);
        %%io.stdout.printf("\n");
    }
    const unknown_size: []u8 = array;
    for (unknown_size) |item| {
        %%io.stdout.print_u64(item);
        %%io.stdout.printf("\n");
    }
    for (unknown_size) |item, index| {
        %%io.stdout.print_i64(index);
        %%io.stdout.printf("\n");
    }
}
    )SOURCE", "9\n8\n7\n6\n0\n1\n2\n3\n9\n8\n7\n6\n0\n1\n2\n3\n");

    add_simple_case_libc("expose function pointer to C land", R"SOURCE(
const c = @c_import(@c_include("stdlib.h"));

export fn compare_fn(a: ?&const c_void, b: ?&const c_void) -> c_int {
    const a_int = (&i32)(a ?? unreachable{});
    const b_int = (&i32)(b ?? unreachable{});
    if (*a_int < *b_int) {
        -1
    } else if (*a_int > *b_int) {
        1
    } else {
        0
    }
}

export fn main(args: c_int, argv: &&u8) -> c_int {
    var array = []i32 { 1, 7, 3, 2, 0, 9, 4, 8, 6, 5 };

    c.qsort((&c_void)(&array[0]), c_ulong(array.len), @sizeof(i32), compare_fn);

    for (array) |item, i| {
        if (item != i) {
            c.abort();
        }
    }

    return 0;
}
    )SOURCE", "");



    add_simple_case_libc("casting between float and integer types", R"SOURCE(
const c = @c_import(@c_include("stdio.h"));
export fn main(argc: c_int, argv: &&u8) -> c_int {
    const small: f32 = 3.25;
    const x: f64 = small;
    const y = i32(x);
    const z = f64(y);
    c.printf(c"%.2f\n%d\n%.2f\n%.2f\n", x, y, z, f64(-0.4));
    return 0;
}
    )SOURCE", "3.25\n3\n3.00\n-0.40\n");


    add_simple_case("incomplete struct parameter top level decl", R"SOURCE(
const io = @import("std").io;
struct A {
    b: B,
}

struct B {
    c: C,
}

struct C {
    x: i32,

    fn d(c: C) {
        %%io.stdout.printf("OK\n");
    }
}

fn foo(a: A) {
    a.b.c.d();
}

pub fn main(args: [][]u8) -> %void {
    const a = A {
        .b = B {
            .c = C {
                .x = 13,
            },
        },
    };
    foo(a);
}

    )SOURCE", "OK\n");


    add_simple_case("same named methods in incomplete struct", R"SOURCE(
const io = @import("std").io;

struct Foo {
    field1: Bar,

    fn method(a: &Foo) -> bool { true }
}

struct Bar {
    field2: i32,

    fn method(b: &Bar) -> bool { true }
}

pub fn main(args: [][]u8) -> %void {
    const bar = Bar {.field2 = 13,};
    const foo = Foo {.field1 = bar,};
    if (!foo.method()) {
        %%io.stdout.printf("BAD\n");
    }
    if (!bar.method()) {
        %%io.stdout.printf("BAD\n");
    }
    %%io.stdout.printf("OK\n");
}
    )SOURCE", "OK\n");


    add_simple_case("defer with only fallthrough", R"SOURCE(
const io = @import("std").io;
pub fn main(args: [][]u8) -> %void {
    %%io.stdout.printf("before\n");
    defer %%io.stdout.printf("defer1\n");
    defer %%io.stdout.printf("defer2\n");
    defer %%io.stdout.printf("defer3\n");
    %%io.stdout.printf("after\n");
}
    )SOURCE", "before\nafter\ndefer3\ndefer2\ndefer1\n");


    add_simple_case("defer with return", R"SOURCE(
const io = @import("std").io;
pub fn main(args: [][]u8) -> %void {
    %%io.stdout.printf("before\n");
    defer %%io.stdout.printf("defer1\n");
    defer %%io.stdout.printf("defer2\n");
    if (args.len == 1) return;
    defer %%io.stdout.printf("defer3\n");
    %%io.stdout.printf("after\n");
}
    )SOURCE", "before\ndefer2\ndefer1\n");


    add_simple_case("%defer and it fails", R"SOURCE(
const io = @import("std").io;
pub fn main(args: [][]u8) -> %void {
    do_test() %% return;
}
fn do_test() -> %void {
    %%io.stdout.printf("before\n");
    defer %%io.stdout.printf("defer1\n");
    %defer %%io.stdout.printf("deferErr\n");
    %return its_gonna_fail();
    defer %%io.stdout.printf("defer3\n");
    %%io.stdout.printf("after\n");
}
error IToldYouItWouldFail;
fn its_gonna_fail() -> %void {
    return error.IToldYouItWouldFail;
}
    )SOURCE", "before\ndeferErr\ndefer1\n");


    add_simple_case("%defer and it passes", R"SOURCE(
const io = @import("std").io;
pub fn main(args: [][]u8) -> %void {
    do_test() %% return;
}
fn do_test() -> %void {
    %%io.stdout.printf("before\n");
    defer %%io.stdout.printf("defer1\n");
    %defer %%io.stdout.printf("deferErr\n");
    %return its_gonna_pass();
    defer %%io.stdout.printf("defer3\n");
    %%io.stdout.printf("after\n");
}
fn its_gonna_pass() -> %void { }
    )SOURCE", "before\nafter\ndefer3\ndefer1\n");


    {
        TestCase *tc = add_simple_case("@embed_file", R"SOURCE(
const foo_txt = @embed_file("foo.txt");
const io = @import("std").io;

pub fn main(args: [][]u8) -> %void {
    %%io.stdout.printf(foo_txt);
}
        )SOURCE", "1234\nabcd\n");

        add_source_file(tc, "foo.txt", "1234\nabcd\n");
    }
}


////////////////////////////////////////////////////////////////////////////////////

static void add_compile_failure_test_cases(void) {
    add_compile_fail_case("multiple function definitions", R"SOURCE(
fn a() {}
fn a() {}
    )SOURCE", 1, ".tmp_source.zig:3:1: error: redefinition of 'a'");

    add_compile_fail_case("bad directive", R"SOURCE(
#bogus1("")
extern fn b();
#bogus2("")
fn a() {}
    )SOURCE", 2, ".tmp_source.zig:2:1: error: invalid directive: 'bogus1'",
                 ".tmp_source.zig:4:1: error: invalid directive: 'bogus2'");

    add_compile_fail_case("unreachable with return", R"SOURCE(
fn a() -> unreachable {return;}
    )SOURCE", 1, ".tmp_source.zig:2:24: error: expected type 'unreachable', got 'void'");

    add_compile_fail_case("control reaches end of non-void function", R"SOURCE(
fn a() -> i32 {}
    )SOURCE", 1, ".tmp_source.zig:2:15: error: expected type 'i32', got 'void'");

    add_compile_fail_case("undefined function call", R"SOURCE(
fn a() {
    b();
}
    )SOURCE", 1, ".tmp_source.zig:3:5: error: use of undeclared identifier 'b'");

    add_compile_fail_case("wrong number of arguments", R"SOURCE(
fn a() {
    b(1);
}
fn b(a: i32, b: i32, c: i32) { }
    )SOURCE", 1, ".tmp_source.zig:3:6: error: expected 3 arguments, got 1");

    add_compile_fail_case("invalid type", R"SOURCE(
fn a() -> bogus {}
    )SOURCE", 1, ".tmp_source.zig:2:11: error: use of undeclared identifier 'bogus'");

    add_compile_fail_case("pointer to unreachable", R"SOURCE(
fn a() -> &unreachable {}
    )SOURCE", 1, ".tmp_source.zig:2:11: error: pointer to unreachable not allowed");

    add_compile_fail_case("unreachable code", R"SOURCE(
fn a() {
    return;
    b();
}

fn b() {}
    )SOURCE", 1, ".tmp_source.zig:4:5: error: unreachable code");

    add_compile_fail_case("bad import", R"SOURCE(
const bogus = @import("bogus-does-not-exist.zig");
    )SOURCE", 1, ".tmp_source.zig:2:15: error: unable to find 'bogus-does-not-exist.zig'");

    add_compile_fail_case("undeclared identifier", R"SOURCE(
fn a() {
    b +
    c
}
    )SOURCE", 2,
            ".tmp_source.zig:3:5: error: use of undeclared identifier 'b'",
            ".tmp_source.zig:4:5: error: use of undeclared identifier 'c'");

    add_compile_fail_case("parameter redeclaration", R"SOURCE(
fn f(a : i32, a : i32) {
}
    )SOURCE", 1, ".tmp_source.zig:2:15: error: redeclaration of variable 'a'");

    add_compile_fail_case("local variable redeclaration", R"SOURCE(
fn f() {
    const a : i32 = 0;
    const a = 0;
}
    )SOURCE", 1, ".tmp_source.zig:4:5: error: redeclaration of variable 'a'");

    add_compile_fail_case("local variable redeclares parameter", R"SOURCE(
fn f(a : i32) {
    const a = 0;
}
    )SOURCE", 1, ".tmp_source.zig:3:5: error: redeclaration of variable 'a'");

    add_compile_fail_case("variable has wrong type", R"SOURCE(
fn f() -> i32 {
    const a = c"a";
    a
}
    )SOURCE", 1, ".tmp_source.zig:4:5: error: expected type 'i32', got '&const u8'");

    add_compile_fail_case("if condition is bool, not int", R"SOURCE(
fn f() {
    if (0) {}
}
    )SOURCE", 1, ".tmp_source.zig:3:9: error: integer value 0 cannot be implicitly casted to type 'bool'");

    add_compile_fail_case("assign unreachable", R"SOURCE(
fn f() {
    const a = return;
}
    )SOURCE", 1, ".tmp_source.zig:3:5: error: variable initialization is unreachable");

    add_compile_fail_case("unreachable variable", R"SOURCE(
fn f() {
    const a : unreachable = return;
}
    )SOURCE", 1, ".tmp_source.zig:3:15: error: variable of type 'unreachable' not allowed");

    add_compile_fail_case("unreachable parameter", R"SOURCE(
fn f(a : unreachable) {}
    )SOURCE", 1, ".tmp_source.zig:2:10: error: parameter of type 'unreachable' not allowed");

    add_compile_fail_case("bad assignment target", R"SOURCE(
fn f() {
    3 = 3;
}
    )SOURCE", 1, ".tmp_source.zig:3:5: error: invalid assignment target");

    add_compile_fail_case("assign to constant variable", R"SOURCE(
fn f() {
    const a = 3;
    a = 4;
}
    )SOURCE", 1, ".tmp_source.zig:4:5: error: cannot assign to constant");

    add_compile_fail_case("use of undeclared identifier", R"SOURCE(
fn f() {
    b = 3;
}
    )SOURCE", 1, ".tmp_source.zig:3:5: error: use of undeclared identifier 'b'");

    add_compile_fail_case("const is a statement, not an expression", R"SOURCE(
fn f() {
    (const a = 0);
}
    )SOURCE", 1, ".tmp_source.zig:3:6: error: invalid token: 'const'");

    add_compile_fail_case("array access errors", R"SOURCE(
fn f() {
    var bad : bool = undefined;
    i[i] = i[i];
    bad[bad] = bad[bad];
}
    )SOURCE", 4, ".tmp_source.zig:4:5: error: use of undeclared identifier 'i'",
                 ".tmp_source.zig:4:7: error: use of undeclared identifier 'i'",
                 ".tmp_source.zig:5:8: error: array access of non-array",
                 ".tmp_source.zig:5:9: error: expected type 'isize', got 'bool'");

    add_compile_fail_case("variadic functions only allowed in extern", R"SOURCE(
fn f(...) {}
    )SOURCE", 1, ".tmp_source.zig:2:1: error: variadic arguments only allowed in extern functions");

    add_compile_fail_case("write to const global variable", R"SOURCE(
const x : i32 = 99;
fn f() {
    x = 1;
}
    )SOURCE", 1, ".tmp_source.zig:4:5: error: cannot assign to constant");


    add_compile_fail_case("missing else clause", R"SOURCE(
fn f() {
    const x : i32 = if (true) { 1 };
    const y = if (true) { i32(1) };
}
    )SOURCE", 2, ".tmp_source.zig:3:21: error: expected type 'i32', got 'void'",
                 ".tmp_source.zig:4:15: error: incompatible types: 'i32' and 'void'");

    add_compile_fail_case("direct struct loop", R"SOURCE(
struct A { a : A, }
    )SOURCE", 1, ".tmp_source.zig:2:1: error: 'A' depends on itself");

    add_compile_fail_case("indirect struct loop", R"SOURCE(
struct A { b : B, }
struct B { c : C, }
struct C { a : A, }
    )SOURCE", 1, ".tmp_source.zig:2:1: error: 'A' depends on itself");

    add_compile_fail_case("invalid struct field", R"SOURCE(
struct A { x : i32, }
fn f() {
    var a : A = undefined;
    a.foo = 1;
    const y = a.bar;
}
    )SOURCE", 2,
            ".tmp_source.zig:5:6: error: no member named 'foo' in 'A'",
            ".tmp_source.zig:6:16: error: no member named 'bar' in 'A'");

    add_compile_fail_case("redefinition of struct", R"SOURCE(
struct A { x : i32, }
struct A { y : i32, }
    )SOURCE", 1, ".tmp_source.zig:3:1: error: redefinition of 'A'");

    add_compile_fail_case("redefinition of enums", R"SOURCE(
enum A {}
enum A {}
    )SOURCE", 1, ".tmp_source.zig:3:1: error: redefinition of 'A'");

    add_compile_fail_case("redefinition of global variables", R"SOURCE(
var a : i32 = 1;
var a : i32 = 2;
    )SOURCE", 1, ".tmp_source.zig:3:1: error: redeclaration of variable 'a'");

    add_compile_fail_case("byvalue struct on exported functions", R"SOURCE(
struct A { x : i32, }
export fn f(a : A) {}
    )SOURCE", 1, ".tmp_source.zig:3:13: error: byvalue struct parameters not yet supported on extern functions");

    add_compile_fail_case("duplicate field in struct value expression", R"SOURCE(
struct A {
    x : i32,
    y : i32,
    z : i32,
}
fn f() {
    const a = A {
        .z = 1,
        .y = 2,
        .x = 3,
        .z = 4,
    };
}
    )SOURCE", 1, ".tmp_source.zig:12:9: error: duplicate field");

    add_compile_fail_case("missing field in struct value expression", R"SOURCE(
struct A {
    x : i32,
    y : i32,
    z : i32,
}
fn f() {
    // we want the error on the '{' not the 'A' because
    // the A could be a complicated expression
    const a = A {
        .z = 4,
        .y = 2,
    };
}
    )SOURCE", 1, ".tmp_source.zig:10:17: error: missing field: 'x'");

    add_compile_fail_case("invalid field in struct value expression", R"SOURCE(
struct A {
    x : i32,
    y : i32,
    z : i32,
}
fn f() {
    const a = A {
        .z = 4,
        .y = 2,
        .foo = 42,
    };
}
    )SOURCE", 1, ".tmp_source.zig:11:9: error: no member named 'foo' in 'A'");

    add_compile_fail_case("invalid break expression", R"SOURCE(
fn f() {
    break;
}
    )SOURCE", 1, ".tmp_source.zig:3:5: error: 'break' expression outside loop");

    add_compile_fail_case("invalid continue expression", R"SOURCE(
fn f() {
    continue;
}
    )SOURCE", 1, ".tmp_source.zig:3:5: error: 'continue' expression outside loop");

    add_compile_fail_case("invalid maybe type", R"SOURCE(
fn f() {
    if (const x ?= true) { }
}
    )SOURCE", 1, ".tmp_source.zig:3:20: error: expected maybe type");

    add_compile_fail_case("cast unreachable", R"SOURCE(
fn f() -> i32 {
    i32(return 1)
}
    )SOURCE", 1, ".tmp_source.zig:3:8: error: invalid cast from type 'unreachable' to 'i32'");

    add_compile_fail_case("invalid builtin fn", R"SOURCE(
fn f() -> @bogus(foo) {
}
    )SOURCE", 1, ".tmp_source.zig:2:11: error: invalid builtin function: 'bogus'");

    add_compile_fail_case("top level decl dependency loop", R"SOURCE(
const a : @typeof(b) = 0;
const b : @typeof(a) = 0;
    )SOURCE", 1, ".tmp_source.zig:2:1: error: 'a' depends on itself");

    add_compile_fail_case("noalias on non pointer param", R"SOURCE(
fn f(noalias x: i32) {}
    )SOURCE", 1, ".tmp_source.zig:2:6: error: noalias on non-pointer parameter");

    add_compile_fail_case("struct init syntax for array", R"SOURCE(
const foo = []u16{.x = 1024,};
    )SOURCE", 1, ".tmp_source.zig:2:18: error: type '[]u16' does not support struct initialization syntax");

    add_compile_fail_case("type variables must be constant", R"SOURCE(
var foo = u8;
    )SOURCE", 1, ".tmp_source.zig:2:1: error: variable of type 'type' must be constant");

    add_compile_fail_case("variables shadowing types", R"SOURCE(
struct Foo {}
struct Bar {}

fn f(Foo: i32) {
    var Bar : i32 = undefined;
}
    )SOURCE", 4,
            ".tmp_source.zig:5:6: error: redefinition of 'Foo'",
            ".tmp_source.zig:2:1: note: previous definition is here",
            ".tmp_source.zig:6:5: error: redefinition of 'Bar'",
            ".tmp_source.zig:3:1: note: previous definition is here");

    add_compile_fail_case("multiple else prongs in a switch", R"SOURCE(
fn f(x: u32) {
    const value: bool = switch (x) {
        1234 => false,
        else => true,
        else => true,
    };
}
    )SOURCE", 1, ".tmp_source.zig:6:9: error: multiple else prongs in switch expression");

    add_compile_fail_case("global variable initializer must be constant expression", R"SOURCE(
extern fn foo() -> i32;
const x = foo();
    )SOURCE", 1, ".tmp_source.zig:3:11: error: global variable initializer requires constant expression");

    add_compile_fail_case("non compile time string concatenation", R"SOURCE(
fn f(s: []u8) -> []u8 {
    s ++ "foo"
}
    )SOURCE", 1, ".tmp_source.zig:3:5: error: string concatenation requires constant expression");

    add_compile_fail_case("c_import with bogus include", R"SOURCE(
const c = @c_import(@c_include("bogus.h"));
    )SOURCE", 2, ".tmp_source.zig:2:11: error: C import failed",
                 ".h:1:10: note: 'bogus.h' file not found");

    add_compile_fail_case("address of number literal", R"SOURCE(
const x = 3;
const y = &x;
    )SOURCE", 1, ".tmp_source.zig:3:12: error: unable to get address of type '(integer literal)'");

    add_compile_fail_case("@typeof number literal", R"SOURCE(
const x = 3;
struct Foo {
    index: @typeof(x),
}
    )SOURCE", 1, ".tmp_source.zig:4:20: error: type '(integer literal)' not eligible for @typeof");

    add_compile_fail_case("integer overflow error", R"SOURCE(
const x : u8 = 300;
    )SOURCE", 1, ".tmp_source.zig:2:16: error: integer value 300 cannot be implicitly casted to type 'u8'");

    add_compile_fail_case("incompatible number literals", R"SOURCE(
const x = 2 == 2.0;
    )SOURCE", 1, ".tmp_source.zig:2:11: error: integer value 2 cannot be implicitly casted to type '(float literal)'");

    add_compile_fail_case("missing function call param", R"SOURCE(
struct Foo {
    a: i32,
    b: i32,

    fn member_a(foo: Foo) -> i32 {
        return foo.a;
    }
    fn member_b(foo: Foo) -> i32 {
        return foo.b;
    }
}

const member_fn_type = @typeof(Foo.member_a);
const members = []member_fn_type {
    Foo.member_a,
    Foo.member_b,
};

fn f(foo: Foo, index: i32) {
    const result = members[index]();
}
    )SOURCE", 1, ".tmp_source.zig:21:34: error: expected 1 arguments, got 0");

    add_compile_fail_case("missing function name and param name", R"SOURCE(
fn () {}
fn f(i32) {}
    )SOURCE", 2,
            ".tmp_source.zig:2:1: error: missing function name",
            ".tmp_source.zig:3:6: error: missing parameter name");

    add_compile_fail_case("wrong function type", R"SOURCE(
const fns = []fn(){ a, b, c };
fn a() -> i32 {0}
fn b() -> i32 {1}
fn c() -> i32 {2}
    )SOURCE", 3,
            ".tmp_source.zig:2:21: error: expected type 'fn()', got 'fn() -> i32'",
            ".tmp_source.zig:2:24: error: expected type 'fn()', got 'fn() -> i32'",
            ".tmp_source.zig:2:27: error: expected type 'fn()', got 'fn() -> i32'");

    add_compile_fail_case("extern function pointer mismatch", R"SOURCE(
const fns = [](fn(i32)->i32){ a, b, c };
pub fn a(x: i32) -> i32 {x + 0}
pub fn b(x: i32) -> i32 {x + 1}
export fn c(x: i32) -> i32 {x + 2}
    )SOURCE", 1, ".tmp_source.zig:2:37: error: expected type 'fn(i32) -> i32', got 'extern fn(i32) -> i32'");


    add_compile_fail_case("implicit cast from f64 to f32", R"SOURCE(
const x : f64 = 1.0;
const y : f32 = x;
    )SOURCE", 1, ".tmp_source.zig:3:17: error: expected type 'f32', got 'f64'");


    add_compile_fail_case("colliding invalid top level functions", R"SOURCE(
fn func() -> bogus {}
fn func() -> bogus {}
    )SOURCE", 2,
            ".tmp_source.zig:3:1: error: redefinition of 'func'",
            ".tmp_source.zig:2:14: error: use of undeclared identifier 'bogus'");


    add_compile_fail_case("bogus compile var", R"SOURCE(
const x = @compile_var("bogus");
    )SOURCE", 1, ".tmp_source.zig:2:24: error: unrecognized compile variable: 'bogus'");


    add_compile_fail_case("@const_eval", R"SOURCE(
fn a(x: i32) {
    const y = @const_eval(x);
}
    )SOURCE", 1, ".tmp_source.zig:3:27: error: unable to evaluate constant expression");

    add_compile_fail_case("non constant expression in array size outside function", R"SOURCE(
struct Foo {
    y: [get()]u8,
}
var global_var: isize = 1;
fn get() -> isize { global_var }
    )SOURCE", 1, ".tmp_source.zig:3:9: error: unable to evaluate constant expression");


    add_compile_fail_case("unnecessary if statement", R"SOURCE(
fn f() {
    if (true) { }
}
    )SOURCE", 1, ".tmp_source.zig:3:9: error: condition is always true; unnecessary if statement");


    add_compile_fail_case("addition with non numbers", R"SOURCE(
struct Foo {
    field: i32,
}
const x = Foo {.field = 1} + Foo {.field = 2};
    )SOURCE", 1, ".tmp_source.zig:5:28: error: invalid operands to binary expression: 'Foo' and 'Foo'");


    add_compile_fail_case("division by zero", R"SOURCE(
const lit_int_x = 1 / 0;
const lit_float_x = 1.0 / 0.0;
const int_x = i32(1) / i32(0);
const float_x = f32(1.0) / f32(0.0);
    )SOURCE", 4,
            ".tmp_source.zig:2:21: error: division by zero is undefined",
            ".tmp_source.zig:3:25: error: division by zero is undefined",
            ".tmp_source.zig:4:22: error: division by zero is undefined",
            ".tmp_source.zig:5:26: error: division by zero is undefined");


    add_compile_fail_case("missing switch prong", R"SOURCE(
enum Number {
    One,
    Two,
    Three,
    Four,
}
fn f(n: Number) -> i32 {
    switch (n) {
        One => 1,
        Two => 2,
        Three => 3,
    }
}
    )SOURCE", 1, ".tmp_source.zig:9:5: error: enumeration value 'Four' not handled in switch");

    add_compile_fail_case("import inside function body", R"SOURCE(
fn f() {
    const std = @import("std");
}
    )SOURCE", 1, ".tmp_source.zig:3:17: error: @import invalid inside function bodies");


    add_compile_fail_case("normal string with newline", R"SOURCE(
const foo = "a
b";
    )SOURCE", 1, ".tmp_source.zig:2:13: error: use raw string for multiline string literal");

    add_compile_fail_case("invalid comparison for function pointers", R"SOURCE(
fn foo() {}
const invalid = foo > foo;
    )SOURCE", 1, ".tmp_source.zig:3:21: error: operator not allowed for type 'fn()'");

    add_compile_fail_case("generic function instance with non-constant expression", R"SOURCE(
fn foo(x: i32)(y: i32) -> i32 { return x + y; }
fn test1(a: i32, b: i32) -> i32 {
    return foo(a)(b);
}
    )SOURCE", 1, ".tmp_source.zig:4:16: error: unable to evaluate constant expression");

    add_compile_fail_case("goto jumping into block", R"SOURCE(
fn f() {
    {
a_label:
    }
    goto a_label;
}
    )SOURCE", 2,
            ".tmp_source.zig:4:1: error: label 'a_label' defined but not used",
            ".tmp_source.zig:6:5: error: no label in scope named 'a_label'");

    add_compile_fail_case("goto jumping past a defer", R"SOURCE(
fn f(b: bool) {
    if (b) goto label;
    defer derp();
label:
}
fn derp(){}
    )SOURCE", 2,
            ".tmp_source.zig:3:12: error: no label in scope named 'label'",
            ".tmp_source.zig:5:1: error: label 'label' defined but not used");

    add_compile_fail_case("assign null to non-nullable pointer", R"SOURCE(
const a: &u8 = null;
    )SOURCE", 1, ".tmp_source.zig:2:16: error: expected maybe type, got '&u8'");

    add_compile_fail_case("indexing an array of size zero", R"SOURCE(
const array = []u8{};
fn foo() {
    const pointer = &array[0];
}
    )SOURCE", 1, ".tmp_source.zig:4:27: error: out of bounds array access");

    add_compile_fail_case("compile time division by zero", R"SOURCE(
const x = foo(0);
fn foo(x: i32) -> i32 {
    1 / x
}
    )SOURCE", 3,
            ".tmp_source.zig:3:1: error: function evaluation caused division by zero",
            ".tmp_source.zig:2:14: note: called from here",
            ".tmp_source.zig:4:7: note: division by zero here");

    add_compile_fail_case("branch on undefined value", R"SOURCE(
const x = if (undefined) true else false;
    )SOURCE", 1, ".tmp_source.zig:2:15: error: branch on undefined value");


    add_compile_fail_case("endless loop in function evaluation", R"SOURCE(
const seventh_fib_number = fibbonaci(7);
fn fibbonaci(x: i32) -> i32 {
    return fibbonaci(x - 1) + fibbonaci(x - 2);
}
    )SOURCE", 3,
            ".tmp_source.zig:3:1: error: function evaluation exceeded 1000 branches",
            ".tmp_source.zig:2:37: note: called from here",
            ".tmp_source.zig:4:40: note: quota exceeded here");

    add_compile_fail_case("@embed_file with bogus file", R"SOURCE(
const resource = @embed_file("bogus.txt");
    )SOURCE", 1, ".tmp_source.zig:2:18: error: unable to find './bogus.txt'");


    add_compile_fail_case("non-const expression in struct literal outside function", R"SOURCE(
struct Foo {
    x: i32,
}
const a = Foo {.x = get_it()};
extern fn get_it() -> i32;
    )SOURCE", 1, ".tmp_source.zig:5:27: error: unable to evaluate constant expression");

    add_compile_fail_case("non-const expression function call with struct return value outside function", R"SOURCE(
struct Foo {
    x: i32,
}
const a = get_it();
#static_eval_enable(false)
fn get_it() -> Foo { Foo {.x = 13} }
    )SOURCE", 1, ".tmp_source.zig:5:17: error: unable to evaluate constant expression");

    add_compile_fail_case("undeclared identifier error should mark fn as impure", R"SOURCE(
fn foo() {
    test_a_thing();
}
fn test_a_thing() {
    bad_fn_call();
}
    )SOURCE", 1, ".tmp_source.zig:6:5: error: use of undeclared identifier 'bad_fn_call'");
}

//////////////////////////////////////////////////////////////////////////////

static void add_parseh_test_cases(void) {
    add_parseh_case("simple data types", R"SOURCE(
#include <stdint.h>
int foo(char a, unsigned char b, signed char c);
int foo(char a, unsigned char b, signed char c); // test a duplicate prototype
void bar(uint8_t a, uint16_t b, uint32_t c, uint64_t d);
void baz(int8_t a, int16_t b, int32_t c, int64_t d);
    )SOURCE", 3,
            "pub extern fn foo(a: u8, b: u8, c: i8) -> c_int;",
            "pub extern fn bar(a: u8, b: u16, c: u32, d: u64);",
            "pub extern fn baz(a: i8, b: i16, c: i32, d: i64);");

    add_parseh_case("noreturn attribute", R"SOURCE(
void foo(void) __attribute__((noreturn));
    )SOURCE", 1, R"OUTPUT(pub extern fn foo() -> unreachable;)OUTPUT");

    add_parseh_case("enums", R"SOURCE(
enum Foo {
    FooA,
    FooB,
    Foo1,
};
    )SOURCE", 1, R"OUTPUT(export enum enum_Foo {
    A,
    B,
    _1,
}
pub const FooA = enum_Foo.A;
pub const FooB = enum_Foo.B;
pub const Foo1 = enum_Foo._1;)OUTPUT",
            R"OUTPUT(pub const Foo = enum_Foo;)OUTPUT");

    add_parseh_case("restrict -> noalias", R"SOURCE(
void foo(void *restrict bar, void *restrict);
    )SOURCE", 1, R"OUTPUT(pub extern fn foo(noalias bar: ?&c_void, noalias arg1: ?&c_void);)OUTPUT");

    add_parseh_case("simple struct", R"SOURCE(
struct Foo {
    int x;
    char *y;
};
    )SOURCE", 2,
            R"OUTPUT(export struct struct_Foo {
    x: c_int,
    y: ?&u8,
})OUTPUT", R"OUTPUT(pub const Foo = struct_Foo;)OUTPUT");

    add_parseh_case("qualified struct and enum", R"SOURCE(
struct Foo {
    int x;
    int y;
};
enum Bar {
    BarA,
    BarB,
};
void func(struct Foo *a, enum Bar **b);
    )SOURCE", 3, R"OUTPUT(export struct struct_Foo {
    x: c_int,
    y: c_int,
}
export enum enum_Bar {
    A,
    B,
}
pub const BarA = enum_Bar.A;
pub const BarB = enum_Bar.B;)OUTPUT",
            "pub extern fn func(a: ?&struct_Foo, b: ?&?&enum_Bar);",
    R"OUTPUT(pub const Foo = struct_Foo;
pub const Bar = enum_Bar;)OUTPUT");

    add_parseh_case("constant size array", R"SOURCE(
void func(int array[20]);
    )SOURCE", 1, "pub extern fn func(array: ?&c_int);");


    add_parseh_case("self referential struct with function pointer", R"SOURCE(
struct Foo {
    void (*derp)(struct Foo *foo);
};
    )SOURCE", 2, R"OUTPUT(export struct struct_Foo {
    derp: ?extern fn(?&struct_Foo),
})OUTPUT", R"OUTPUT(pub const Foo = struct_Foo;)OUTPUT");


    add_parseh_case("struct prototype used in func", R"SOURCE(
struct Foo;
struct Foo *some_func(struct Foo *foo, int x);
    )SOURCE", 2, R"OUTPUT(pub type struct_Foo = u8;
pub extern fn some_func(foo: ?&struct_Foo, x: c_int) -> ?&struct_Foo;)OUTPUT",
        R"OUTPUT(pub const Foo = struct_Foo;)OUTPUT");


    add_parseh_case("#define a char literal", R"SOURCE(
#define A_CHAR  'a'
    )SOURCE", 1, R"OUTPUT(pub const A_CHAR = 'a';)OUTPUT");


    add_parseh_case("#define an unsigned integer literal", R"SOURCE(
#define CHANNEL_COUNT 24
    )SOURCE", 1, R"OUTPUT(pub const CHANNEL_COUNT = 24;)OUTPUT");


    add_parseh_case("overide previous #define", R"SOURCE(
#define A_CHAR 'a'
#define A_CHAR 'b'
    )SOURCE", 1, "pub const A_CHAR = 'b';");


    add_parseh_case("#define referencing another #define", R"SOURCE(
#define THING2 THING1
#define THING1 1234
    )SOURCE", 2,
            "pub const THING1 = 1234;",
            "pub const THING2 = THING1;");


    add_parseh_case("variables", R"SOURCE(
extern int extern_var;
static const int int_var = 13;
    )SOURCE", 2,
            "pub extern var extern_var: c_int;",
            "pub const int_var: c_int = 13;");


    add_parseh_case("circular struct definitions", R"SOURCE(
struct Bar;

struct Foo {
    struct Bar *next;
};

struct Bar {
    struct Foo *next;
};
    )SOURCE", 2,
            R"SOURCE(export struct struct_Bar {
    next: ?&struct_Foo,
})SOURCE",
            R"SOURCE(export struct struct_Foo {
    next: ?&struct_Bar,
})SOURCE");


    add_parseh_case("typedef void", R"SOURCE(
typedef void Foo;
Foo fun(Foo *a);
    )SOURCE", 2,
            "pub const Foo = c_void;",
            "pub extern fn fun(a: ?&c_void);");

    add_parseh_case("generate inline func for #define global extern fn", R"SOURCE(
extern void (*fn_ptr)(void);
#define foo fn_ptr
    )SOURCE", 2,
            "pub extern var fn_ptr: ?extern fn();",
            R"SOURCE(pub inline fn foo() {
    (??fn_ptr)();
})SOURCE");


    add_parseh_case("#define string", R"SOURCE(
#define  foo  "a string"
    )SOURCE", 1, "pub const foo = c\"a string\";");

    add_parseh_case("__cdecl doesn't mess up function pointers", R"SOURCE(
void foo(void (__cdecl *fn_ptr)(void));
    )SOURCE", 1, "pub extern fn foo(fn_ptr: ?extern fn());");
}

static void run_self_hosted_test(void) {
    Buf zig_stderr = BUF_INIT;
    Buf zig_stdout = BUF_INIT;
    int return_code;
    ZigList<const char *> args = {0};
    args.append("test");
    args.append("../test/self_hosted.zig");
    os_exec_process(zig_exe, args, &return_code, &zig_stderr, &zig_stdout);

    if (return_code) {
        printf("\nSelf-hosted tests failed:\n");
        printf("./zig test ../test/self_hosted.zig\n");
        printf("%s\n", buf_ptr(&zig_stderr));
        exit(1);
    }
}

static void add_self_hosted_tests(void) {
    TestCase *test_case = allocate<TestCase>(1);
    test_case->case_name = "self hosted tests";
    test_case->is_self_hosted = true;
    test_cases.append(test_case);
}

static void print_compiler_invocation(TestCase *test_case) {
    printf("%s", zig_exe);
    for (int i = 0; i < test_case->compiler_args.length; i += 1) {
        printf(" %s", test_case->compiler_args.at(i));
    }
    printf("\n");
}

static void run_test(TestCase *test_case) {
    if (test_case->is_self_hosted) {
        return run_self_hosted_test();
    }

    for (int i = 0; i < test_case->source_files.length; i += 1) {
        TestSourceFile *test_source = &test_case->source_files.at(i);
        os_write_file(
                buf_create_from_str(test_source->relative_path),
                buf_create_from_str(test_source->source_code));
    }

    Buf zig_stderr = BUF_INIT;
    Buf zig_stdout = BUF_INIT;
    int return_code;
    int err;
    if ((err = os_exec_process(zig_exe, test_case->compiler_args, &return_code, &zig_stderr, &zig_stdout))) {
        fprintf(stderr, "Unable to exec %s: %s\n", zig_exe, err_str(err));
    }

    if (!test_case->is_parseh && test_case->compile_errors.length) {
        if (return_code) {
            for (int i = 0; i < test_case->compile_errors.length; i += 1) {
                const char *err_text = test_case->compile_errors.at(i);
                if (!strstr(buf_ptr(&zig_stderr), err_text)) {
                    printf("\n");
                    printf("========= Expected this compile error: =========\n");
                    printf("%s\n", err_text);
                    printf("================================================\n");
                    print_compiler_invocation(test_case);
                    printf("%s\n", buf_ptr(&zig_stderr));
                    exit(1);
                }
            }
            return; // success
        } else {
            printf("\nCompile failed with return code 0 (Expected failure):\n");
            print_compiler_invocation(test_case);
            printf("%s\n", buf_ptr(&zig_stderr));
            exit(1);
        }
    }

    if (return_code != 0) {
        printf("\nCompile failed with return code %d:\n", return_code);
        print_compiler_invocation(test_case);
        printf("%s\n", buf_ptr(&zig_stderr));
        exit(1);
    }

    if (test_case->is_parseh) {
        if (buf_len(&zig_stderr) > 0) {
            printf("\nparseh emitted warnings:\n");
            print_compiler_invocation(test_case);
            printf("%s\n", buf_ptr(&zig_stderr));
            exit(1);
        }

        for (int i = 0; i < test_case->compile_errors.length; i += 1) {
            const char *output = test_case->compile_errors.at(i);

            if (!strstr(buf_ptr(&zig_stdout), output)) {
                printf("\n");
                printf("========= Expected this output: =========\n");
                printf("%s\n", output);
                printf("================================================\n");
                print_compiler_invocation(test_case);
                printf("%s\n", buf_ptr(&zig_stdout));
                exit(1);
            }
        }
    } else {
        Buf program_stderr = BUF_INIT;
        Buf program_stdout = BUF_INIT;
        os_exec_process(tmp_exe_path, test_case->program_args, &return_code, &program_stderr, &program_stdout);

        if (return_code != 0) {
            printf("\nProgram exited with return code %d:\n", return_code);
            print_compiler_invocation(test_case);
            printf("%s", tmp_exe_path);
            for (int i = 0; i < test_case->program_args.length; i += 1) {
                printf(" %s", test_case->program_args.at(i));
            }
            printf("\n");
            printf("%s\n", buf_ptr(&program_stderr));
            exit(1);
        }

        if (!buf_eql_str(&program_stdout, test_case->output)) {
            printf("\n");
            print_compiler_invocation(test_case);
            printf("%s", tmp_exe_path);
            for (int i = 0; i < test_case->program_args.length; i += 1) {
                printf(" %s", test_case->program_args.at(i));
            }
            printf("\n");
            printf("==== Test failed. Expected output: ====\n");
            printf("%s\n", test_case->output);
            printf("========= Actual output: ==============\n");
            printf("%s\n", buf_ptr(&program_stdout));
            printf("=======================================\n");
            exit(1);
        }
    }

    for (int i = 0; i < test_case->source_files.length; i += 1) {
        TestSourceFile *test_source = &test_case->source_files.at(i);
        remove(test_source->relative_path);
    }
}

static void run_all_tests(bool reverse) {
    if (reverse) {
        for (int i = test_cases.length - 1; i >= 0; i -= 1) {
            TestCase *test_case = test_cases.at(i);
            printf("Test %d/%d %s...", i + 1, test_cases.length, test_case->case_name);
            run_test(test_case);
            printf("OK\n");
        }
    } else {
        for (int i = 0; i < test_cases.length; i += 1) {
            TestCase *test_case = test_cases.at(i);
            printf("Test %d/%d %s...", i + 1, test_cases.length, test_case->case_name);
            run_test(test_case);
            printf("OK\n");
        }
    }
    printf("%d tests passed.\n", test_cases.length);
}

static void cleanup(void) {
    remove(tmp_source_path);
    remove(tmp_h_path);
    remove(tmp_exe_path);
}

static int usage(const char *arg0) {
    fprintf(stderr, "Usage: %s [--reverse]\n", arg0);
    return 1;
}

int main(int argc, char **argv) {
    bool reverse = false;
    for (int i = 1; i < argc; i += 1) {
        if (strcmp(argv[i], "--reverse") == 0) {
            reverse = true;
        } else {
            return usage(argv[0]);
        }
    }
    add_compiling_test_cases();
    add_compile_failure_test_cases();
    add_parseh_test_cases();
    add_self_hosted_tests();
    run_all_tests(reverse);
    cleanup();
}