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
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
|
// slang-test-main.cpp
#include "../../source/core/slang-io.h"
#include "../../source/core/slang-token-reader.h"
#include "../../source/core/slang-std-writers.h"
#include "../../source/core/slang-hex-dump-util.h"
#include "../../slang-com-helper.h"
#include "../../source/core/slang-string-util.h"
#include "../../source/core/slang-byte-encode-util.h"
using namespace Slang;
#include "os.h"
#include "../../source/core/slang-render-api-util.h"
#include "test-context.h"
#include "test-reporter.h"
#include "options.h"
#include "slangc-tool.h"
#include "../../source/core/slang-cpp-compiler.h"
#include "../../source/core/slang-process-util.h"
#define STB_IMAGE_IMPLEMENTATION
#include "external/stb/stb_image.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#define SLANG_PRELUDE_NAMESPACE CPPPrelude
#include "../../tests/cross-compile/slang-cpp-prelude.h"
// Options for a particular test
struct TestOptions
{
enum Type
{
Normal, ///< A regular test
Diagnostic, ///< Diagnostic tests will always run (as form of failure is being tested)
};
Type type = Type::Normal;
String command;
List<String> args;
// The categories that this test was assigned to
List<TestCategory*> categories;
bool isSynthesized = false;
};
struct TestDetails
{
TestDetails() {}
explicit TestDetails(const TestOptions& inOptions):
options(inOptions)
{}
TestOptions options; ///< The options for the test
TestRequirements requirements; ///< The requirements for the test to work
};
// Information on tests to run for a particular file
struct FileTestList
{
List<TestDetails> tests;
};
enum class SpawnType
{
UseExe,
UseSharedLibrary,
};
struct TestInput
{
// Path to the input file for the test
String filePath;
// Prefix for the path that test output should write to
// (usually the same as `filePath`, but will differ when
// we run multiple tests out of the same file)
String outputStem;
// Arguments for the test (usually to be interpreted
// as command line args)
TestOptions const* testOptions;
// Determines how the test will be spawned
SpawnType spawnType;
};
typedef TestResult(*TestCallback)(TestContext* context, TestInput& input);
// Globals
// Pre declare
static void _addRenderTestOptions(const Options& options, CommandLine& cmdLine);
/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!! Functions !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
bool match(char const** ioCursor, char const* expected)
{
char const* cursor = *ioCursor;
while(*expected && *cursor == *expected)
{
cursor++;
expected++;
}
if(*expected != 0) return false;
*ioCursor = cursor;
return true;
}
void skipHorizontalSpace(char const** ioCursor)
{
char const* cursor = *ioCursor;
for( ;;)
{
switch( *cursor )
{
case ' ':
case '\t':
cursor++;
continue;
default:
break;
}
break;
}
*ioCursor = cursor;
}
void skipToEndOfLine(char const** ioCursor)
{
char const* cursor = *ioCursor;
for( ;;)
{
int c = *cursor;
switch( c )
{
default:
cursor++;
continue;
case '\r': case '\n':
{
cursor++;
int d = *cursor;
if( (c ^ d) == ('\r' ^ '\n') )
{
cursor++;
}
}
; // fall through to:
case 0:
*ioCursor = cursor;
return;
}
}
}
String getString(char const* textBegin, char const* textEnd)
{
StringBuilder sb;
sb.Append(textBegin, textEnd - textBegin);
return sb.ProduceString();
}
String collectRestOfLine(char const** ioCursor)
{
char const* cursor = *ioCursor;
char const* textBegin = cursor;
skipToEndOfLine(&cursor);
char const* textEnd = cursor;
*ioCursor = cursor;
return getString(textBegin, textEnd);
}
static TestResult _gatherTestOptions(
TestCategorySet* categorySet,
char const** ioCursor,
TestOptions& outOptions)
{
char const* cursor = *ioCursor;
// Right after the `TEST` keyword, the user may specify
// one or more categories for the test.
if(*cursor == '(')
{
cursor++;
// optional test category
skipHorizontalSpace(&cursor);
char const* categoryStart = cursor;
for(;;)
{
switch( *cursor )
{
default:
cursor++;
continue;
case ',':
case ')':
{
char const* categoryEnd = cursor;
cursor++;
auto categoryName = getString(categoryStart, categoryEnd);
TestCategory* category = categorySet->find(categoryName);
if(!category)
{
return TestResult::Fail;
}
outOptions.categories.add(category);
if( *categoryEnd == ',' )
{
skipHorizontalSpace(&cursor);
categoryStart = cursor;
continue;
}
}
break;
case 0: case '\r': case '\n':
return TestResult::Fail;
}
break;
}
}
// If no categories were specified, then add the default category
if(outOptions.categories.getCount() == 0)
{
outOptions.categories.add(categorySet->defaultCategory);
}
if(*cursor == ':')
cursor++;
else
{
return TestResult::Fail;
}
// Next scan for a sub-command name
char const* commandStart = cursor;
for(;;)
{
switch(*cursor)
{
default:
cursor++;
continue;
case ':':
break;
case 0: case '\r': case '\n':
return TestResult::Fail;
}
break;
}
char const* commandEnd = cursor;
outOptions.command = getString(commandStart, commandEnd);
if(*cursor == ':')
cursor++;
else
{
return TestResult::Fail;
}
// Now scan for arguments. For now we just assume that
// any whitespace separation indicates a new argument
// (we don't support quoting)
for(;;)
{
skipHorizontalSpace(&cursor);
// End of line? then no more options.
switch( *cursor )
{
case 0: case '\r': case '\n':
skipToEndOfLine(&cursor);
return TestResult::Pass;
default:
break;
}
// Let's try to read one option
char const* argBegin = cursor;
for(;;)
{
switch( *cursor )
{
default:
cursor++;
continue;
case 0: case '\r': case '\n': case ' ': case '\t':
break;
}
break;
}
char const* argEnd = cursor;
assert(argBegin != argEnd);
outOptions.args.add(getString(argBegin, argEnd));
}
}
// Try to read command-line options from the test file itself
TestResult gatherTestsForFile(
TestCategorySet* categorySet,
String filePath,
FileTestList* testList)
{
String fileContents;
try
{
fileContents = Slang::File::readAllText(filePath);
}
catch (Slang::IOException)
{
return TestResult::Fail;
}
// Walk through the lines of the file, looking for test commands
char const* cursor = fileContents.begin();
while(*cursor)
{
// We are at the start of a line of input.
skipHorizontalSpace(&cursor);
// Look for a pattern that matches what we want
if(match(&cursor, "//TEST_IGNORE_FILE"))
{
return TestResult::Ignored;
}
else if(match(&cursor, "//TEST"))
{
TestDetails testDetails;
if(_gatherTestOptions(categorySet, &cursor, testDetails.options) != TestResult::Pass)
return TestResult::Fail;
testList->tests.add(testDetails);
}
else if (match(&cursor, "//DIAGNOSTIC_TEST"))
{
TestDetails testDetails;
if (_gatherTestOptions(categorySet, &cursor, testDetails.options) != TestResult::Pass)
return TestResult::Fail;
// Mark that it is a diagnostic test
testDetails.options.type = TestOptions::Type::Diagnostic;
testList->tests.add(testDetails);
}
else
{
skipToEndOfLine(&cursor);
}
}
return TestResult::Pass;
}
Result spawnAndWaitExe(TestContext* context, const String& testPath, const CommandLine& cmdLine, ExecuteResult& outRes)
{
const auto& options = context->options;
if (options.shouldBeVerbose)
{
String commandLine = ProcessUtil::getCommandLineString(cmdLine);
context->reporter->messageFormat(TestMessageType::Info, "%s\n", commandLine.begin());
}
Result res = ProcessUtil::execute(cmdLine, outRes);
if (SLANG_FAILED(res))
{
// fprintf(stderr, "failed to run test '%S'\n", testPath.ToWString());
context->reporter->messageFormat(TestMessageType::RunError, "failed to run test '%S'", testPath.toWString().begin());
}
return res;
}
Result spawnAndWaitSharedLibrary(TestContext* context, const String& testPath, const CommandLine& cmdLine, ExecuteResult& outRes)
{
const auto& options = context->options;
String exeName = Path::getFileNameWithoutExt(cmdLine.m_executable);
if (options.shouldBeVerbose)
{
CommandLine testCmdLine;
testCmdLine.setExecutableFilename("slang-test");
if (options.binDir.getLength())
{
testCmdLine.addArg("-bindir");
testCmdLine.addArg(options.binDir);
}
testCmdLine.addArg(exeName);
testCmdLine.m_args.addRange(cmdLine.m_args);
String testCmdLineString = ProcessUtil::getCommandLineString(testCmdLine);
context->reporter->messageFormat(TestMessageType::Info, "%s\n", testCmdLineString.getBuffer());
}
auto func = context->getInnerMainFunc(context->options.binDir, exeName);
if (func)
{
StringBuilder stdErrorString;
StringBuilder stdOutString;
// Say static so not released
StringWriter stdError(&stdErrorString, WriterFlag::IsConsole | WriterFlag::IsStatic);
StringWriter stdOut(&stdOutString, WriterFlag::IsConsole | WriterFlag::IsStatic);
StdWriters* prevStdWriters = StdWriters::getSingleton();
StdWriters stdWriters;
stdWriters.setWriter(SLANG_WRITER_CHANNEL_STD_ERROR, &stdError);
stdWriters.setWriter(SLANG_WRITER_CHANNEL_STD_OUTPUT, &stdOut);
if (exeName == "slangc")
{
stdWriters.setWriter(SLANG_WRITER_CHANNEL_DIAGNOSTIC, &stdError);
}
List<const char*> args;
args.add(exeName.getBuffer());
for (Index i = 0; i < cmdLine.m_args.getCount(); ++i)
{
args.add(cmdLine.m_args[i].value.getBuffer());
}
SlangResult res = func(&stdWriters, context->getSession(), int(args.getCount()), args.begin());
StdWriters::setSingleton(prevStdWriters);
outRes.standardError = stdErrorString;
outRes.standardOutput = stdOutString;
outRes.resultCode = (int)TestToolUtil::getReturnCode(res);
return SLANG_OK;
}
return SLANG_FAIL;
}
static SlangResult _extractArg(const CommandLine& cmdLine, const String& argName, String& outValue)
{
SLANG_ASSERT(argName.getLength() > 0 && argName[0] == '-');
Index index = cmdLine.findArgIndex(argName.getUnownedSlice());
if (index >= 0 && index < cmdLine.getArgCount() - 1)
{
outValue = cmdLine.m_args[index + 1].value;
return SLANG_OK;
}
return SLANG_FAIL;
}
static bool _hasOption(const List<String>& args, const String& argName)
{
return args.indexOf(argName) != Index(-1);
}
static SlangPassThrough _toPassThroughType(const UnownedStringSlice& slice)
{
if (slice == "dxc")
{
return SLANG_PASS_THROUGH_DXC;
}
else if (slice == "fxc")
{
return SLANG_PASS_THROUGH_FXC;
}
else if (slice == "glslang")
{
return SLANG_PASS_THROUGH_GLSLANG;
}
else if (slice == "c" || slice == "cpp")
{
return SLANG_PASS_THROUGH_GENERIC_C_CPP;
}
else if (slice == "clang")
{
return SLANG_PASS_THROUGH_CLANG;
}
else if (slice == "gcc")
{
return SLANG_PASS_THROUGH_GCC;
}
else if (slice == "vs" || slice == "visualstudio")
{
return SLANG_PASS_THROUGH_VISUAL_STUDIO;
}
return SLANG_PASS_THROUGH_NONE;
}
static PassThroughFlags _getPassThroughFlagsForTarget(SlangCompileTarget target)
{
switch (target)
{
case SLANG_TARGET_UNKNOWN:
case SLANG_HLSL:
case SLANG_GLSL:
case SLANG_C_SOURCE:
case SLANG_CPP_SOURCE:
{
return 0;
}
case SLANG_DXBC:
case SLANG_DXBC_ASM:
{
return PassThroughFlag::Fxc;
}
case SLANG_SPIRV:
case SLANG_SPIRV_ASM:
{
return PassThroughFlag::Glslang;
}
case SLANG_DXIL:
case SLANG_DXIL_ASM:
{
return PassThroughFlag::Dxc;
}
case SLANG_HOST_CALLABLE:
case SLANG_EXECUTABLE:
case SLANG_SHARED_LIBRARY:
{
return PassThroughFlag::Generic_C_CPP;
}
default:
{
SLANG_ASSERT(!"Unknown type");
return 0;
}
}
}
static SlangCompileTarget _getCompileTarget(const UnownedStringSlice& name)
{
#define CASE(NAME, TARGET) if(name == NAME) return SLANG_##TARGET;
CASE("hlsl", HLSL)
CASE("glsl", GLSL)
CASE("dxbc", DXBC)
CASE("dxbc-assembly", DXBC_ASM)
CASE("dxbc-asm", DXBC_ASM)
CASE("spirv", SPIRV)
CASE("spirv-assembly", SPIRV_ASM)
CASE("spirv-asm", SPIRV_ASM)
CASE("dxil", DXIL)
CASE("dxil-assembly", DXIL_ASM)
CASE("dxil-asm", DXIL_ASM)
CASE("c", C_SOURCE)
CASE("cpp", CPP_SOURCE)
CASE("exe", EXECUTABLE)
CASE("sharedlib", SHARED_LIBRARY)
CASE("dll", SHARED_LIBRARY)
CASE("callable", HOST_CALLABLE)
CASE("host-callable", HOST_CALLABLE)
#undef CASE
return SLANG_TARGET_UNKNOWN;
}
static SlangResult _extractRenderTestRequirements(const CommandLine& cmdLine, TestRequirements* ioRequirements)
{
const auto& args = cmdLine.m_args;
// TODO(JS):
// This is rather convoluted in that it has to work out from the command line parameters passed
// to render-test what renderer will be used.
// That a similar logic has to be kept inside the implementation of render-test and both this
// and render-test will have to be kept in sync.
bool useDxil = cmdLine.findArgIndex(UnownedStringSlice::fromLiteral("-use-dxil")) >= 0;
bool usePassthru = false;
// Work out what kind of render will be used
RenderApiType renderApiType;
{
RenderApiType foundRenderApiType = RenderApiType::Unknown;
RenderApiType foundLanguageRenderType = RenderApiType::Unknown;
for (const auto& arg: args)
{
Slang::UnownedStringSlice argSlice = arg.value.getUnownedSlice();
if (argSlice.size() && argSlice[0] == '-')
{
// Look up the rendering API if set
UnownedStringSlice argName = UnownedStringSlice(argSlice.begin() + 1, argSlice.end());
RenderApiType renderApiType = RenderApiUtil::findApiTypeByName(argName);
if (renderApiType != RenderApiType::Unknown)
{
foundRenderApiType = renderApiType;
// There should be only one explicit api
SLANG_ASSERT(ioRequirements->explicitRenderApi == RenderApiType::Unknown || ioRequirements->explicitRenderApi == renderApiType);
// Set the explicitly set render api
ioRequirements->explicitRenderApi = renderApiType;
continue;
}
// Lookup the target language type
RenderApiType languageRenderType = RenderApiUtil::findImplicitLanguageRenderApiType(argName);
if (languageRenderType != RenderApiType::Unknown)
{
foundLanguageRenderType = languageRenderType;
// Use the pass thru compiler if these are the sources
usePassthru |= (argName == "hlsl" || argName == "glsl");
continue;
}
}
}
// If a render option isn't set use defaultRenderType
renderApiType = (foundRenderApiType == RenderApiType::Unknown) ? foundLanguageRenderType : foundRenderApiType;
}
// The native language for the API
SlangSourceLanguage nativeLanguage = SLANG_SOURCE_LANGUAGE_UNKNOWN;
SlangCompileTarget target = SLANG_TARGET_NONE;
SlangPassThrough passThru = SLANG_PASS_THROUGH_NONE;
switch (renderApiType)
{
case RenderApiType::D3D11:
target = SLANG_DXBC;
nativeLanguage = SLANG_SOURCE_LANGUAGE_HLSL;
passThru = SLANG_PASS_THROUGH_FXC;
break;
case RenderApiType::D3D12:
target = SLANG_DXBC;
nativeLanguage = SLANG_SOURCE_LANGUAGE_HLSL;
passThru = SLANG_PASS_THROUGH_FXC;
if (useDxil)
{
target = SLANG_DXIL;
passThru = SLANG_PASS_THROUGH_DXC;
}
break;
case RenderApiType::OpenGl:
target = SLANG_GLSL;
nativeLanguage = SLANG_SOURCE_LANGUAGE_GLSL;
passThru = SLANG_PASS_THROUGH_GLSLANG;
break;
case RenderApiType::Vulkan:
target = SLANG_SPIRV;
nativeLanguage = SLANG_SOURCE_LANGUAGE_GLSL;
passThru = SLANG_PASS_THROUGH_GLSLANG;
break;
case RenderApiType::CPU:
target = SLANG_HOST_CALLABLE;
nativeLanguage = SLANG_SOURCE_LANGUAGE_CPP;
passThru = SLANG_PASS_THROUGH_GENERIC_C_CPP;
break;
}
SlangSourceLanguage sourceLanguage = nativeLanguage;
if (!usePassthru)
{
sourceLanguage = SLANG_SOURCE_LANGUAGE_SLANG;
passThru = SLANG_PASS_THROUGH_NONE;
}
if (passThru == SLANG_PASS_THROUGH_NONE)
{
// Work out backends needed based on the target
ioRequirements->addUsedBackends(_getPassThroughFlagsForTarget(target));
}
else
{
ioRequirements->addUsed(passThru);
}
// Add the render api used
ioRequirements->addUsed(renderApiType);
return SLANG_OK;
}
static SlangResult _extractSlangCTestRequirements(const CommandLine& cmdLine, TestRequirements* ioRequirements)
{
// This determines what the requirements are for a slangc like command line
// First check pass through
{
String passThrough;
if (SLANG_SUCCEEDED(_extractArg(cmdLine, "-pass-through", passThrough)))
{
ioRequirements->addUsed(_toPassThroughType(passThrough.getUnownedSlice()));
}
}
// The target if set will also imply a backend
{
String targetName;
if (SLANG_SUCCEEDED(_extractArg(cmdLine, "-target", targetName)))
{
const SlangCompileTarget target = _getCompileTarget(targetName.getUnownedSlice());
ioRequirements->addUsedBackends(_getPassThroughFlagsForTarget(target));
}
}
return SLANG_OK;
}
static SlangResult _extractReflectionTestRequirements(const CommandLine& cmdLine, TestRequirements* ioRequirements)
{
// There are no specialized constraints for a reflection test
return SLANG_OK;
}
static SlangResult _extractTestRequirements(const CommandLine& cmdLine, TestRequirements* ioInfo)
{
String exeName = Path::getFileNameWithoutExt(cmdLine.m_executable);
if (exeName == "render-test")
{
return _extractRenderTestRequirements(cmdLine, ioInfo);
}
else if (exeName == "slangc")
{
return _extractSlangCTestRequirements(cmdLine, ioInfo);
}
else if (exeName == "slang-reflection-test")
{
return _extractReflectionTestRequirements(cmdLine, ioInfo);
}
SLANG_ASSERT(!"Unknown tool type");
return SLANG_FAIL;
}
static RenderApiFlags _getAvailableRenderApiFlags(TestContext* context)
{
// Only evaluate if it hasn't already been evaluated (the actual evaluation is slow...)
if (!context->isAvailableRenderApiFlagsValid)
{
// Call the render-test tool asking it only to startup a specified render api
// (taking into account adapter options)
RenderApiFlags availableRenderApiFlags = 0;
for (int i = 0; i < int(RenderApiType::CountOf); ++i)
{
const RenderApiType apiType = RenderApiType(i);
if (apiType == RenderApiType::CPU)
{
// TODO(JS): Only enable CPU on Windows for now
#if SLANG_WINDOWS_FAMILY
availableRenderApiFlags |= RenderApiFlags(1) << int(apiType);
#endif
continue;
}
// See if it's possible the api is available
if (RenderApiUtil::calcHasApi(apiType))
{
// Try starting up the device
CommandLine cmdLine;
cmdLine.setExecutablePath(Path::combine(context->options.binDir, String("render-test") + ProcessUtil::getExecutableSuffix()));
_addRenderTestOptions(context->options, cmdLine);
// We just want to see if the device can be started up
cmdLine.addArg("-only-startup");
// Select what api to use
StringBuilder builder;
builder << "-" << RenderApiUtil::getApiName(apiType);
cmdLine.addArg(builder);
// Run the render-test tool and see if the device could startup
ExecuteResult exeRes;
if (SLANG_SUCCEEDED(spawnAndWaitSharedLibrary(context, "device-startup", cmdLine, exeRes))
&& TestToolUtil::getReturnCodeFromInt(exeRes.resultCode) == ToolReturnCode::Success)
{
availableRenderApiFlags |= RenderApiFlags(1) << int(apiType);
}
}
}
context->availableRenderApiFlags = availableRenderApiFlags;
context->isAvailableRenderApiFlagsValid = true;
}
return context->availableRenderApiFlags;
}
ToolReturnCode getReturnCode(const ExecuteResult& exeRes)
{
return TestToolUtil::getReturnCodeFromInt(exeRes.resultCode);
}
ToolReturnCode spawnAndWait(TestContext* context, const String& testPath, SpawnType spawnType, const CommandLine& cmdLine, ExecuteResult& outExeRes)
{
if (context->isCollectingRequirements())
{
// If we just want info... don't bother running anything
const SlangResult res = _extractTestRequirements(cmdLine, context->testRequirements);
// Keep compiler happy on release
SLANG_UNUSED(res);
SLANG_ASSERT(SLANG_SUCCEEDED(res));
return ToolReturnCode::Success;
}
const auto& options = context->options;
SlangResult spawnResult = SLANG_FAIL;
switch (spawnType)
{
case SpawnType::UseExe:
{
spawnResult = spawnAndWaitExe(context, testPath, cmdLine, outExeRes);
break;
}
case SpawnType::UseSharedLibrary:
{
spawnResult = spawnAndWaitSharedLibrary(context, testPath, cmdLine, outExeRes);
break;
}
default: break;
}
if (SLANG_FAILED(spawnResult))
{
return ToolReturnCode::FailedToRun;
}
return getReturnCode(outExeRes);
}
String getOutput(const ExecuteResult& exeRes)
{
ExecuteResult::ResultCode resultCode = exeRes.resultCode;
String standardOuptut = exeRes.standardOutput;
String standardError = exeRes.standardError;
// We construct a single output string that captures the results
StringBuilder actualOutputBuilder;
actualOutputBuilder.Append("result code = ");
actualOutputBuilder.Append(resultCode);
actualOutputBuilder.Append("\nstandard error = {\n");
actualOutputBuilder.Append(standardError);
actualOutputBuilder.Append("}\nstandard output = {\n");
actualOutputBuilder.Append(standardOuptut);
actualOutputBuilder.Append("}\n");
return actualOutputBuilder.ProduceString();
}
// Finds the specialized or default path for expected data for a test.
// If neither are found, will return an empty string
String findExpectedPath(const TestInput& input, const char* postFix)
{
StringBuilder specializedBuf;
// Try the specialized name first
specializedBuf << input.outputStem;
if (postFix)
{
specializedBuf << postFix;
}
if (File::exists(specializedBuf))
{
return specializedBuf;
}
// Try the default name
StringBuilder defaultBuf;
defaultBuf.Clear();
defaultBuf << input.filePath;
if (postFix)
{
defaultBuf << postFix;
}
if (File::exists(defaultBuf))
{
return defaultBuf;
}
// Couldn't find either
printf("referenceOutput '%s' or '%s' not found.\n", defaultBuf.getBuffer(), specializedBuf.getBuffer());
return "";
}
static void _initSlangCompiler(TestContext* context, CommandLine& ioCmdLine)
{
ioCmdLine.setExecutablePath(Path::combine(context->options.binDir, String("slangc") + ProcessUtil::getExecutableSuffix()));
if (context->options.verbosePaths)
{
ioCmdLine.addArg("-verbose-paths");
}
}
TestResult asTestResult(ToolReturnCode code)
{
switch (code)
{
case ToolReturnCode::Success: return TestResult::Pass;
case ToolReturnCode::Ignored: return TestResult::Ignored;
default: return TestResult::Fail;
}
}
#define TEST_RETURN_ON_DONE(x) \
{ \
const ToolReturnCode toolRet_ = x; \
if (TestToolUtil::isDone(toolRet_)) \
{ \
return asTestResult(toolRet_); \
} \
}
static SlangResult _executeBinary(const UnownedStringSlice& hexDump, ExecuteResult& outExeRes)
{
// We need to extract the binary
List<uint8_t> data;
SLANG_RETURN_ON_FAIL(HexDumpUtil::parseWithMarkers(hexDump, data));
// Need to write this off to a temporary file
String fileName;
SLANG_RETURN_ON_FAIL(File::generateTemporary(UnownedStringSlice("slang-test"), fileName));
fileName.append(ProcessUtil::getExecutableSuffix());
TemporaryFileSet temporaryFileSet;
temporaryFileSet.add(fileName);
{
ComPtr<ISlangWriter> writer;
SLANG_RETURN_ON_FAIL(FileWriter::createBinary(fileName.getBuffer(), 0, writer));
SLANG_RETURN_ON_FAIL(writer->write((const char*)data.getBuffer(), data.getCount()));
}
// Make executable... (for linux/unix like targets)
SLANG_RETURN_ON_FAIL(File::makeExecutable(fileName));
// Execute it
CommandLine cmdLine;
cmdLine.m_executable = fileName;
cmdLine.m_executableType = CommandLine::ExecutableType::Path;
return ProcessUtil::execute(cmdLine, outExeRes);
}
TestResult runSimpleTest(TestContext* context, TestInput& input)
{
// need to execute the stand-alone Slang compiler on the file, and compare its output to what we expect
auto outputStem = input.outputStem;
CommandLine cmdLine;
_initSlangCompiler(context, cmdLine);
cmdLine.addArg(input.filePath);
for( auto arg : input.testOptions->args )
{
cmdLine.addArg(arg);
}
ExecuteResult exeRes;
TEST_RETURN_ON_DONE(spawnAndWait(context, outputStem, input.spawnType, cmdLine, exeRes));
if (context->isCollectingRequirements())
{
return TestResult::Pass;
}
// See what kind of target it is
SlangCompileTarget target = SLANG_TARGET_UNKNOWN;
{
const auto& args = input.testOptions->args;
const Index targetIndex = args.indexOf("-target");
if (targetIndex != Index(-1) && targetIndex + 1 < args.getCount())
{
target = _getCompileTarget(args[targetIndex + 1].getUnownedSlice());
}
}
// If it's executable we run it and use it's output
if (target == SLANG_EXECUTABLE)
{
ExecuteResult runExeRes;
if (SLANG_FAILED(_executeBinary(exeRes.standardOutput.getUnownedSlice(), runExeRes)))
{
return TestResult::Fail;
}
exeRes = runExeRes;
}
String actualOutput = getOutput(exeRes);
String expectedOutputPath = outputStem + ".expected";
String expectedOutput;
try
{
expectedOutput = Slang::File::readAllText(expectedOutputPath);
}
catch (Slang::IOException)
{
}
// If no expected output file was found, then we
// expect everything to be empty
if (expectedOutput.getLength() == 0)
{
expectedOutput = "result code = 0\nstandard error = {\n}\nstandard output = {\n}\n";
}
TestResult result = TestResult::Pass;
// Otherwise we compare to the expected output
if (actualOutput != expectedOutput)
{
context->reporter->dumpOutputDifference(expectedOutput, actualOutput);
result = TestResult::Fail;
}
// If the test failed, then we write the actual output to a file
// so that we can easily diff it from the command line and
// diagnose the problem.
if (result == TestResult::Fail)
{
String actualOutputPath = outputStem + ".actual";
Slang::File::writeAllText(actualOutputPath, actualOutput);
context->reporter->dumpOutputDifference(expectedOutput, actualOutput);
}
return result;
}
static SlangResult _loadAsSharedLibrary(const UnownedStringSlice& hexDump, TemporaryFileSet& inOutTemporaryFileSet, SharedLibrary::Handle& outSharedLibrary)
{
// We need to extract the binary
List<uint8_t> data;
SLANG_RETURN_ON_FAIL(HexDumpUtil::parseWithMarkers(hexDump, data));
// Need to write this off to a temporary file
String fileName;
SLANG_RETURN_ON_FAIL(File::generateTemporary(UnownedStringSlice("slang-test"), fileName));
// Need to work out the dll name
String sharedLibraryName = SharedLibrary::calcPlatformPath(fileName.getUnownedSlice());
inOutTemporaryFileSet.add(sharedLibraryName);
{
ComPtr<ISlangWriter> writer;
SLANG_RETURN_ON_FAIL(FileWriter::createBinary(sharedLibraryName.getBuffer(), 0, writer));
SLANG_RETURN_ON_FAIL(writer->write((const char*)data.getBuffer(), data.getCount()));
}
// Make executable... (for linux/unix like targets)
//SLANG_RETURN_ON_FAIL(File::makeExecutable(fileName));
return SharedLibrary::loadWithPlatformPath(sharedLibraryName.getBuffer(), outSharedLibrary);
}
static void _writeBuffer(const CPPPrelude::RWStructuredBuffer<int32_t>& in, StringBuilder& out)
{
for (size_t i = 0; i < in.count; ++i)
{
if (i > 0)
{
out << ", ";
}
out << in[i];
}
out << "\n";
}
TestResult runCPUExecuteTest(TestContext* context, TestInput& input)
{
auto outputStem = input.outputStem;
CommandLine cmdLine;
_initSlangCompiler(context, cmdLine);
cmdLine.addArg(input.filePath);
for (auto arg : input.testOptions->args)
{
cmdLine.addArg(arg);
}
ExecuteResult exeRes;
TEST_RETURN_ON_DONE(spawnAndWait(context, outputStem, input.spawnType, cmdLine, exeRes));
if (context->isCollectingRequirements())
{
return TestResult::Pass;
}
TemporaryFileSet temporaryFileSet;
SharedLibrary::Handle sharedLibrary = SharedLibrary::Handle(0);
if (SLANG_FAILED(_loadAsSharedLibrary(exeRes.standardOutput.getUnownedSlice(), temporaryFileSet, sharedLibrary)))
{
return TestResult::Fail;
}
StringBuilder actualOutput;
// TODO(JS): For moment just assume function name/data/parameters
{
SharedLibrary::FuncPtr func = SharedLibrary::findFuncByName(sharedLibrary, "computeMain");
if (!func)
{
SharedLibrary::unload(sharedLibrary);
return TestResult::Fail;
}
struct UniformState
{
CPPPrelude::RWStructuredBuffer<int> buffer;
};
typedef void (*Func)(CPPPrelude::ComputeVaryingInput* varyingInput, UniformState* uniformState);
Func runFunc = Func(func);
int32_t data[4] = { 0, 0, 0, 0};
UniformState state;
state.buffer = CPPPrelude::RWStructuredBuffer<int32_t>{data, 4};
CPPPrelude::ComputeVaryingInput varyingInput = {};
for (Int i = 0; i < 4; ++i)
{
varyingInput.groupThreadID.x = uint32_t(i);
runFunc(&varyingInput, &state);
}
SharedLibrary::unload(sharedLibrary);
// Write the data
_writeBuffer(state.buffer, actualOutput);
}
String expectedOutputPath = outputStem + ".expected";
String expectedOutput;
try
{
expectedOutput = Slang::File::readAllText(expectedOutputPath);
}
catch (Slang::IOException)
{
}
TestResult result = TestResult::Pass;
// Otherwise we compare to the expected output
if (actualOutput != expectedOutput)
{
context->reporter->dumpOutputDifference(expectedOutput, actualOutput);
result = TestResult::Fail;
}
// If the test failed, then we write the actual output to a file
// so that we can easily diff it from the command line and
// diagnose the problem.
if (result == TestResult::Fail)
{
String actualOutputPath = outputStem + ".actual";
Slang::File::writeAllText(actualOutputPath, actualOutput);
context->reporter->dumpOutputDifference(expectedOutput, actualOutput);
}
return result;
}
TestResult runSimpleCompareCommandLineTest(TestContext* context, TestInput& input)
{
TestInput workInput(input);
// Use the original files input to compare with
workInput.outputStem = input.filePath;
// Force to using exes
workInput.spawnType = SpawnType::UseExe;
return runSimpleTest(context, workInput);
}
TestResult runReflectionTest(TestContext* context, TestInput& input)
{
const auto& options = context->options;
auto filePath = input.filePath;
auto outputStem = input.outputStem;
bool isCPUTest = input.testOptions->command.startsWith("CPU_");
CommandLine cmdLine;
cmdLine.setExecutablePath(Path::combine(options.binDir, String("slang-reflection-test") + ProcessUtil::getExecutableSuffix()));
cmdLine.addArg(filePath);
for( auto arg : input.testOptions->args )
{
cmdLine.addArg(arg);
}
ExecuteResult exeRes;
TEST_RETURN_ON_DONE(spawnAndWait(context, outputStem, input.spawnType, cmdLine, exeRes));
if (context->isCollectingRequirements())
{
return TestResult::Pass;
}
String actualOutput = getOutput(exeRes);
if (isCPUTest)
{
#if SLANG_PTR_IS_32
outputStem.append(".32");
#else
outputStem.append(".64");
#endif
}
String expectedOutputPath = outputStem + ".expected";
String expectedOutput;
try
{
expectedOutput = Slang::File::readAllText(expectedOutputPath);
}
catch (Slang::IOException)
{
}
// If no expected output file was found, then we
// expect everything to be empty
if (expectedOutput.getLength() == 0)
{
expectedOutput = "result code = 0\nstandard error = {\n}\nstandard output = {\n}\n";
}
TestResult result = TestResult::Pass;
// Otherwise we compare to the expected output
if (actualOutput != expectedOutput)
{
result = TestResult::Fail;
}
// If the test failed, then we write the actual output to a file
// so that we can easily diff it from the command line and
// diagnose the problem.
if (result == TestResult::Fail)
{
String actualOutputPath = outputStem + ".actual";
Slang::File::writeAllText(actualOutputPath, actualOutput);
context->reporter->dumpOutputDifference(expectedOutput, actualOutput);
}
return result;
}
String getExpectedOutput(String const& outputStem)
{
String expectedOutputPath = outputStem + ".expected";
String expectedOutput;
try
{
expectedOutput = Slang::File::readAllText(expectedOutputPath);
}
catch (Slang::IOException)
{
}
// If no expected output file was found, then we
// expect everything to be empty
if (expectedOutput.getLength() == 0)
{
expectedOutput = "result code = 0\nstandard error = {\n}\nstandard output = {\n}\n";
}
return expectedOutput;
}
static String _calcSummary(const CPPCompiler::Output& inOutput)
{
CPPCompiler::Output output(inOutput);
// We only want to analyse errors for now
output.removeByType(CPPCompiler::Diagnostic::Type::Info);
output.removeByType(CPPCompiler::Diagnostic::Type::Warning);
StringBuilder builder;
output.appendSimplifiedSummary(builder);
return builder;
}
static String _calcModulePath(const TestInput& input)
{
// Make the module name the same as the source file
auto filePath = input.filePath;
String directory = Path::getParentDirectory(input.outputStem);
String moduleName = Path::getFileNameWithoutExt(filePath);
return Path::combine(directory, moduleName);
}
static TestResult runCPPCompilerCompile(TestContext* context, TestInput& input)
{
CPPCompiler* compiler = context->getDefaultCPPCompiler();
if (!compiler)
{
return TestResult::Ignored;
}
// need to execute the stand-alone Slang compiler on the file, and compare its output to what we expect
auto outputStem = input.outputStem;
CommandLine cmdLine;
_initSlangCompiler(context, cmdLine);
cmdLine.addArg(input.filePath);
for (auto arg : input.testOptions->args)
{
cmdLine.addArg(arg);
}
ExecuteResult exeRes;
TEST_RETURN_ON_DONE(spawnAndWait(context, outputStem, input.spawnType, cmdLine, exeRes));
if (context->isCollectingRequirements())
{
return TestResult::Pass;
}
// Dump out what happened
{
String actualOutputPath = outputStem + ".actual";
Slang::File::writeAllText(actualOutputPath, getOutput(exeRes));
}
if (exeRes.resultCode != 0)
{
return TestResult::Fail;
}
String modulePath = _calcModulePath(input);
// Find the target
UnownedStringSlice targetExt = UnownedStringSlice::fromLiteral("c");
Index index = cmdLine.findArgIndex(UnownedStringSlice::fromLiteral("-target"));
if (index >= 0 && index + 1 < cmdLine.getArgCount())
{
targetExt = cmdLine.m_args[index + 1].value.getUnownedSlice();
}
// If output was C/C++ we should try compiling
if (targetExt == "c" || targetExt == "cpp")
{
CPPCompiler::CompileOptions options;
options.sourceType = (targetExt == "c") ? CPPCompiler::SourceType::C : CPPCompiler::SourceType::CPP;
options.includePaths.add("tests/cross-compile");
String actualOutput = exeRes.standardOutput;
// Create a filename to write this out to
String cppSource = modulePath + "." + targetExt;
Slang::File::writeAllText(cppSource, actualOutput);
// Okay we can now try compiling
// Compile this source
options.sourceFiles.add(cppSource);
options.modulePath = modulePath;
options.targetType = CPPCompiler::TargetType::SharedLibrary;
CPPCompiler::Output output;
if (SLANG_FAILED(compiler->compile(options, output)))
{
return TestResult::Fail;
}
if (output.getCountByType(CPPCompiler::Diagnostic::Type::Error) > 0)
{
return TestResult::Fail;
}
}
else
{
// Can only be callable
SLANG_ASSERT(targetExt == "callable" || targetExt == "host-callable");
}
return TestResult::Pass;
}
static TestResult runCPPCompilerSharedLibrary(TestContext* context, TestInput& input)
{
CPPCompiler* compiler = context->getDefaultCPPCompiler();
if (!compiler)
{
return TestResult::Ignored;
}
// If we are just collecting requirements, say it passed
if (context->isCollectingRequirements())
{
return TestResult::Pass;
}
auto outputStem = input.outputStem;
auto filePath = input.filePath;
String actualOutputPath = outputStem + ".actual";
File::remove(actualOutputPath);
// Make the module name the same as the source file
String modulePath = _calcModulePath(input);
String ext = Path::getFileExt(filePath);
// Remove the binary..
String sharedLibraryPath = SharedLibrary::calcPlatformPath(modulePath.getUnownedSlice());
File::remove(sharedLibraryPath);
// Set up the compilation options
CPPCompiler::CompileOptions options;
options.sourceType = (ext == "c") ? CPPCompiler::SourceType::C : CPPCompiler::SourceType::CPP;
// Build a shared library
options.targetType = CPPCompiler::TargetType::SharedLibrary;
// Compile this source
options.sourceFiles.add(filePath);
options.modulePath = modulePath;
options.includePaths.add(".");
CPPCompiler::Output output;
if (SLANG_FAILED(compiler->compile(options, output)))
{
return TestResult::Fail;
}
if (SLANG_FAILED(output.result))
{
// Compilation failed
String actualOutput = _calcSummary(output);
// Write the output
Slang::File::writeAllText(actualOutputPath, actualOutput);
// Check that they are the same
{
// Read the expected
String expectedOutput;
try
{
String expectedOutputPath = outputStem + ".expected";
expectedOutput = Slang::File::readAllText(expectedOutputPath);
}
catch (Slang::IOException)
{
}
// Compare if they are the same
if (!StringUtil::areLinesEqual(actualOutput.getUnownedSlice(), expectedOutput.getUnownedSlice()))
{
context->reporter->dumpOutputDifference(expectedOutput, actualOutput);
return TestResult::Fail;
}
}
}
else
{
SharedLibrary::Handle handle;
if (SLANG_FAILED(SharedLibrary::loadWithPlatformPath(sharedLibraryPath.getBuffer(), handle)))
{
return TestResult::Fail;
}
const int inValue = 10;
const char inBuffer[] = "Hello World!";
char buffer[128] = "";
int value = 0;
typedef int(*TestFunc)(int intValue, const char* textValue, char* outTextValue);
// We could capture output if we passed in a ISlangWriter - but for that to work we'd need a
TestFunc testFunc = (TestFunc)SharedLibrary::findFuncByName(handle, "test");
if (testFunc)
{
value = testFunc(inValue, inBuffer, buffer);
}
else
{
printf("Unable to access 'test' function\n");
}
SharedLibrary::unload(handle);
if (!(inValue == value && strcmp(inBuffer, buffer) == 0))
{
return TestResult::Fail;
}
}
return TestResult::Pass;
}
static TestResult runCPPCompilerExecute(TestContext* context, TestInput& input)
{
CPPCompiler* compiler = context->getDefaultCPPCompiler();
if (!compiler)
{
return TestResult::Ignored;
}
// If we are just collecting requirements, say it passed
if (context->isCollectingRequirements())
{
return TestResult::Pass;
}
auto filePath = input.filePath;
auto outputStem = input.outputStem;
String actualOutputPath = outputStem + ".actual";
File::remove(actualOutputPath);
// Make the module name the same as the source file
String ext = Path::getFileExt(filePath);
String modulePath = _calcModulePath(input);
// Remove the binary..
{
StringBuilder moduleExePath;
moduleExePath << modulePath;
moduleExePath << ProcessUtil::getExecutableSuffix();
File::remove(moduleExePath);
}
// Set up the compilation options
CPPCompiler::CompileOptions options;
options.sourceType = (ext == "c") ? CPPCompiler::SourceType::C : CPPCompiler::SourceType::CPP;
// Compile this source
options.sourceFiles.add(filePath);
options.modulePath = modulePath;
CPPCompiler::Output output;
if (SLANG_FAILED(compiler->compile(options, output)))
{
return TestResult::Fail;
}
String actualOutput;
// If the actual compilation failed, then the output will be
if (SLANG_FAILED(output.result))
{
actualOutput = _calcSummary(output);
}
else
{
// Execute the binary and see what we get
CommandLine cmdLine;
StringBuilder exePath;
exePath << modulePath << ProcessUtil::getExecutableSuffix();
cmdLine.setExecutablePath(exePath);
ExecuteResult exeRes;
if (SLANG_FAILED(ProcessUtil::execute(cmdLine, exeRes)))
{
return TestResult::Fail;
}
// Write the output, and compare to expected
actualOutput = getOutput(exeRes);
}
// Write the output
Slang::File::writeAllText(actualOutputPath, actualOutput);
// Check that they are the same
{
// Read the expected
String expectedOutput;
try
{
String expectedOutputPath = outputStem + ".expected";
expectedOutput = Slang::File::readAllText(expectedOutputPath);
}
catch (Slang::IOException)
{
}
// Compare if they are the same
if (!StringUtil::areLinesEqual(actualOutput.getUnownedSlice(), expectedOutput.getUnownedSlice()))
{
context->reporter->dumpOutputDifference(expectedOutput, actualOutput);
return TestResult::Fail;
}
}
return TestResult::Pass;
}
TestResult runCrossCompilerTest(TestContext* context, TestInput& input)
{
// need to execute the stand-alone Slang compiler on the file
// then on the same file + `.glsl` and compare output
auto filePath = input.filePath;
auto outputStem = input.outputStem;
CommandLine actualCmdLine;
CommandLine expectedCmdLine;
_initSlangCompiler(context, actualCmdLine);
_initSlangCompiler(context, expectedCmdLine);
actualCmdLine.addArg(filePath);
// TODO(JS): This should no longer be needed with TestInfo accumulated for a test
const auto& args = input.testOptions->args;
const Index targetIndex = args.indexOf("-target");
if (targetIndex != Index(-1) && targetIndex + 1 < args.getCount())
{
SlangCompileTarget target = _getCompileTarget(args[targetIndex + 1].getUnownedSlice());
// Check the session supports it. If not we ignore it
if (SLANG_FAILED(spSessionCheckCompileTargetSupport(context->getSession(), target)))
{
return TestResult::Ignored;
}
switch (target)
{
case SLANG_DXIL_ASM:
{
expectedCmdLine.addArg(filePath + ".hlsl");
expectedCmdLine.addArg("-pass-through");
expectedCmdLine.addArg("dxc");
break;
}
case SLANG_DXBC_ASM:
{
expectedCmdLine.addArg(filePath + ".hlsl");
expectedCmdLine.addArg("-pass-through");
expectedCmdLine.addArg("fxc");
break;
}
default:
{
expectedCmdLine.addArg(filePath + ".glsl");
expectedCmdLine.addArg("-pass-through");
expectedCmdLine.addArg("glslang");
break;
}
}
}
for( auto arg : input.testOptions->args )
{
actualCmdLine.addArg(arg);
expectedCmdLine.addArg(arg);
}
ExecuteResult expectedExeRes;
TEST_RETURN_ON_DONE(spawnAndWait(context, outputStem, input.spawnType, expectedCmdLine, expectedExeRes));
String expectedOutput;
if (context->isExecuting())
{
expectedOutput = getOutput(expectedExeRes);
String expectedOutputPath = outputStem + ".expected";
try
{
Slang::File::writeAllText(expectedOutputPath, expectedOutput);
}
catch (Slang::IOException)
{
return TestResult::Fail;
}
}
ExecuteResult actualExeRes;
TEST_RETURN_ON_DONE(spawnAndWait(context, outputStem, input.spawnType, actualCmdLine, actualExeRes));
if (context->isCollectingRequirements())
{
return TestResult::Pass;
}
String actualOutput = getOutput(actualExeRes);
TestResult result = TestResult::Pass;
// Otherwise we compare to the expected output
if (actualOutput != expectedOutput)
{
result = TestResult::Fail;
}
// Always fail if the compilation produced a failure, just
// to catch situations where, e.g., command-line options parsing
// caused the same error in both the Slang and glslang cases.
//
if(actualExeRes.resultCode != 0 )
{
result = TestResult::Fail;
}
// If the test failed, then we write the actual output to a file
// so that we can easily diff it from the command line and
// diagnose the problem.
if (result == TestResult::Fail)
{
String actualOutputPath = outputStem + ".actual";
Slang::File::writeAllText(actualOutputPath, actualOutput);
context->reporter->dumpOutputDifference(expectedOutput, actualOutput);
}
return result;
}
TestResult generateHLSLBaseline(TestContext* context, TestInput& input)
{
auto filePath999 = input.filePath;
auto outputStem = input.outputStem;
CommandLine cmdLine;
_initSlangCompiler(context, cmdLine);
cmdLine.addArg(filePath999);
for( auto arg : input.testOptions->args )
{
cmdLine.addArg(arg);
}
cmdLine.addArg("-target");
cmdLine.addArg("dxbc-assembly");
cmdLine.addArg("-pass-through");
cmdLine.addArg("fxc");
ExecuteResult exeRes;
TEST_RETURN_ON_DONE(spawnAndWait(context, outputStem, input.spawnType, cmdLine, exeRes));
if (context->isCollectingRequirements())
{
return TestResult::Pass;
}
String expectedOutput = getOutput(exeRes);
String expectedOutputPath = outputStem + ".expected";
try
{
Slang::File::writeAllText(expectedOutputPath, expectedOutput);
}
catch (Slang::IOException)
{
return TestResult::Fail;
}
return TestResult::Pass;
}
TestResult runHLSLComparisonTest(TestContext* context, TestInput& input)
{
auto filePath999 = input.filePath;
auto outputStem = input.outputStem;
// We will use the Microsoft compiler to generate out expected output here
String expectedOutputPath = outputStem + ".expected";
// Generate the expected output using standard HLSL compiler
generateHLSLBaseline(context, input);
// need to execute the stand-alone Slang compiler on the file, and compare its output to what we expect
CommandLine cmdLine;
_initSlangCompiler(context, cmdLine);
cmdLine.addArg(filePath999);
for( auto arg : input.testOptions->args )
{
cmdLine.addArg(arg);
}
// TODO: The compiler should probably define this automatically...
cmdLine.addArg("-D");
cmdLine.addArg("__SLANG__");
cmdLine.addArg("-target");
cmdLine.addArg("dxbc-assembly");
ExecuteResult exeRes;
TEST_RETURN_ON_DONE(spawnAndWait(context, outputStem, input.spawnType, cmdLine, exeRes));
if (context->isCollectingRequirements())
{
return TestResult::Pass;
}
// We ignore output to stdout, and only worry about what the compiler
// wrote to stderr.
ExecuteResult::ResultCode resultCode = exeRes.resultCode;
String standardOutput = exeRes.standardOutput;
String standardError = exeRes.standardError;
// We construct a single output string that captures the results
StringBuilder actualOutputBuilder;
actualOutputBuilder.Append("result code = ");
actualOutputBuilder.Append(resultCode);
actualOutputBuilder.Append("\nstandard error = {\n");
actualOutputBuilder.Append(standardError);
actualOutputBuilder.Append("}\nstandard output = {\n");
actualOutputBuilder.Append(standardOutput);
actualOutputBuilder.Append("}\n");
String actualOutput = actualOutputBuilder.ProduceString();
String expectedOutput;
try
{
expectedOutput = Slang::File::readAllText(expectedOutputPath);
}
catch (Slang::IOException)
{
}
TestResult result = TestResult::Pass;
// If no expected output file was found, then we
// expect everything to be empty
if (expectedOutput.getLength() == 0)
{
if (resultCode != 0) result = TestResult::Fail;
if (standardError.getLength() != 0) result = TestResult::Fail;
if (standardOutput.getLength() != 0) result = TestResult::Fail;
}
// Otherwise we compare to the expected output
else if (actualOutput != expectedOutput)
{
result = TestResult::Fail;
}
// Always fail if the compilation produced a failure, just
// to catch situations where, e.g., command-line options parsing
// caused the same error in both the Slang and fxc cases.
//
if( resultCode != 0 )
{
result = TestResult::Fail;
}
// If the test failed, then we write the actual output to a file
// so that we can easily diff it from the command line and
// diagnose the problem.
if (result == TestResult::Fail)
{
String actualOutputPath = outputStem + ".actual";
Slang::File::writeAllText(actualOutputPath, actualOutput);
context->reporter->dumpOutputDifference(expectedOutput, actualOutput);
}
return result;
}
TestResult doGLSLComparisonTestRun(TestContext* context,
TestInput& input,
char const* langDefine,
char const* passThrough,
char const* outputKind,
String* outOutput)
{
auto filePath999 = input.filePath;
auto outputStem = input.outputStem;
CommandLine cmdLine;
_initSlangCompiler(context, cmdLine);
cmdLine.addArg(filePath999);
if( langDefine )
{
cmdLine.addArg("-D");
cmdLine.addArg(langDefine);
}
if( passThrough )
{
cmdLine.addArg("-pass-through");
cmdLine.addArg(passThrough);
}
cmdLine.addArg("-target");
cmdLine.addArg("spirv-assembly");
for( auto arg : input.testOptions->args )
{
cmdLine.addArg(arg);
}
ExecuteResult exeRes;
TEST_RETURN_ON_DONE(spawnAndWait(context, outputStem, input.spawnType, cmdLine, exeRes));
if (context->isCollectingRequirements())
{
return TestResult::Pass;
}
ExecuteResult::ResultCode resultCode = exeRes.resultCode;
String standardOuptut = exeRes.standardOutput;
String standardError = exeRes.standardError;
// We construct a single output string that captures the results
StringBuilder outputBuilder;
outputBuilder.Append("result code = ");
outputBuilder.Append(resultCode);
outputBuilder.Append("\nstandard error = {\n");
outputBuilder.Append(standardError);
outputBuilder.Append("}\nstandard output = {\n");
outputBuilder.Append(standardOuptut);
outputBuilder.Append("}\n");
String outputPath = outputStem + outputKind;
String output = outputBuilder.ProduceString();
*outOutput = output;
return TestResult::Pass;
}
TestResult runGLSLComparisonTest(TestContext* context, TestInput& input)
{
auto filePath999 = input.filePath;
auto outputStem = input.outputStem;
String expectedOutput;
String actualOutput;
TestResult hlslResult = doGLSLComparisonTestRun(context, input, "__GLSL__", "glslang", ".expected", &expectedOutput);
TestResult slangResult = doGLSLComparisonTestRun(context, input, "__SLANG__", nullptr, ".actual", &actualOutput);
if (context->isCollectingRequirements())
{
return TestResult::Pass;
}
// If either is ignored, the whole test is
if (hlslResult == TestResult::Ignored ||
slangResult == TestResult::Ignored)
{
return TestResult::Ignored;
}
Slang::File::writeAllText(outputStem + ".expected", expectedOutput);
Slang::File::writeAllText(outputStem + ".actual", actualOutput);
if( hlslResult == TestResult::Fail ) return TestResult::Fail;
if( slangResult == TestResult::Fail ) return TestResult::Fail;
if (actualOutput != expectedOutput)
{
context->reporter->dumpOutputDifference(expectedOutput, actualOutput);
return TestResult::Fail;
}
return TestResult::Pass;
}
static void _addRenderTestOptions(const Options& options, CommandLine& ioCmdLine)
{
if (options.adapter.getLength())
{
ioCmdLine.addArg("-adapter");
ioCmdLine.addArg(options.adapter);
}
}
TestResult runComputeComparisonImpl(TestContext* context, TestInput& input, const char *const* langOpts, size_t numLangOpts)
{
// TODO: delete any existing files at the output path(s) to avoid stale outputs leading to a false pass
auto filePath999 = input.filePath;
auto outputStem = input.outputStem;
CommandLine cmdLine;
cmdLine.setExecutablePath(Path::combine(context->options.binDir, String("render-test") + ProcessUtil::getExecutableSuffix()));
cmdLine.addArg(filePath999);
_addRenderTestOptions(context->options, cmdLine);
for (auto arg : input.testOptions->args)
{
cmdLine.addArg(arg);
}
for (int i = 0; i < int(numLangOpts); ++i)
{
cmdLine.addArg(langOpts[i]);
}
cmdLine.addArg("-o");
auto actualOutputFile = outputStem + ".actual.txt";
cmdLine.addArg(actualOutputFile);
if (context->isExecuting())
{
// clear the stale actual output file first. This will allow us to detect error if render-test fails and outputs nothing.
File::writeAllText(actualOutputFile, "");
}
ExecuteResult exeRes;
TEST_RETURN_ON_DONE(spawnAndWait(context, outputStem, input.spawnType, cmdLine, exeRes));
if (context->isCollectingRequirements())
{
return TestResult::Pass;
}
const String referenceOutput = findExpectedPath(input, ".expected.txt");
if (referenceOutput.getLength() <= 0)
{
return TestResult::Fail;
}
auto actualOutput = getOutput(exeRes);
auto expectedOutput = getExpectedOutput(outputStem);
if (actualOutput != expectedOutput)
{
context->reporter->dumpOutputDifference(expectedOutput, actualOutput);
String actualOutputPath = outputStem + ".actual";
Slang::File::writeAllText(actualOutputPath, actualOutput);
return TestResult::Fail;
}
// check against reference output
if (!File::exists(actualOutputFile))
{
printf("render-test not producing expected outputs.\n");
printf("render-test output:\n%s\n", actualOutput.getBuffer());
return TestResult::Fail;
}
if (!File::exists(referenceOutput))
{
printf("referenceOutput %s not found.\n", referenceOutput.getBuffer());
return TestResult::Fail;
}
auto actualOutputContent = File::readAllText(actualOutputFile);
auto actualProgramOutput = Split(actualOutputContent, '\n');
auto referenceProgramOutput = Split(File::readAllText(referenceOutput), '\n');
auto printOutput = [&]()
{
context->reporter->messageFormat(TestMessageType::TestFailure, "output mismatch! actual output: {\n%s\n}, \n%s\n", actualOutputContent.getBuffer(), actualOutput.getBuffer());
};
if (actualProgramOutput.getCount() < referenceProgramOutput.getCount())
{
printOutput();
return TestResult::Fail;
}
for (Index i = 0; i < referenceProgramOutput.getCount(); i++)
{
auto reference = String(referenceProgramOutput[i].trim());
auto actual = String(actualProgramOutput[i].trim());
if (actual != reference)
{
printOutput();
return TestResult::Fail;
}
}
return TestResult::Pass;
}
TestResult runSlangComputeComparisonTest(TestContext* context, TestInput& input)
{
const char* langOpts[] = { "-slang", "-compute" };
return runComputeComparisonImpl(context, input, langOpts, SLANG_COUNT_OF(langOpts));
}
TestResult runSlangComputeComparisonTestEx(TestContext* context, TestInput& input)
{
return runComputeComparisonImpl(context, input, nullptr, 0);
}
TestResult runHLSLComputeTest(TestContext* context, TestInput& input)
{
const char* langOpts[] = { "--hlsl-rewrite", "-compute" };
return runComputeComparisonImpl(context, input, langOpts, SLANG_COUNT_OF(langOpts));
}
TestResult runSlangRenderComputeComparisonTest(TestContext* context, TestInput& input)
{
const char* langOpts[] = { "-slang", "-gcompute" };
return runComputeComparisonImpl(context, input, langOpts, SLANG_COUNT_OF(langOpts));
}
TestResult doRenderComparisonTestRun(TestContext* context, TestInput& input, char const* langOption, char const* outputKind, String* outOutput)
{
// TODO: delete any existing files at the output path(s) to avoid stale outputs leading to a false pass
auto filePath = input.filePath;
auto outputStem = input.outputStem;
CommandLine cmdLine;
cmdLine.setExecutablePath(Path::combine(context->options.binDir, String("render-test") + ProcessUtil::getExecutableSuffix()));
cmdLine.addArg(filePath);
_addRenderTestOptions(context->options, cmdLine);
for( auto arg : input.testOptions->args )
{
cmdLine.addArg(arg);
}
cmdLine.addArg(langOption);
cmdLine.addArg("-o");
cmdLine.addArg(outputStem + outputKind + ".png");
ExecuteResult exeRes;
TEST_RETURN_ON_DONE(spawnAndWait(context, outputStem, input.spawnType, cmdLine, exeRes));
if (context->isCollectingRequirements())
{
return TestResult::Pass;
}
ExecuteResult::ResultCode resultCode = exeRes.resultCode;
String standardOutput = exeRes.standardOutput;
String standardError = exeRes.standardError;
// We construct a single output string that captures the results
StringBuilder outputBuilder;
outputBuilder.Append("result code = ");
outputBuilder.Append(resultCode);
outputBuilder.Append("\nstandard error = {\n");
outputBuilder.Append(standardError);
outputBuilder.Append("}\nstandard output = {\n");
outputBuilder.Append(standardOutput);
outputBuilder.Append("}\n");
String outputPath = outputStem + outputKind;
String output = outputBuilder.ProduceString();
*outOutput = output;
return TestResult::Pass;
}
class STBImage
{
public:
typedef STBImage ThisType;
/// Reset back to default initialized state (frees any image set)
void reset();
/// True if rhs has same size and amount of channels
bool isComparable(const ThisType& rhs) const;
/// The width in pixels
int getWidth() const { return m_width; }
/// The height in pixels
int getHeight() const { return m_height; }
/// The number of channels (typically held as bytes in order)
int getNumChannels() const { return m_numChannels; }
/// Get the contained pixels, nullptr if nothing loaded
const unsigned char* getPixels() const { return m_pixels; }
unsigned char* getPixels() { return m_pixels; }
/// Read an image with filename. SLANG_OK on success
SlangResult read(const char* filename);
~STBImage() { reset(); }
int m_width = 0;
int m_height = 0;
int m_numChannels = 0;
unsigned char* m_pixels = nullptr;
};
void STBImage::reset()
{
if (m_pixels)
{
stbi_image_free(m_pixels);
m_pixels = nullptr;
}
m_width = 0;
m_height = 0;
m_numChannels = 0;
}
SlangResult STBImage::read(const char* filename)
{
reset();
m_pixels = stbi_load(filename, &m_width, &m_height, &m_numChannels, 0);
if (!m_pixels)
{
return SLANG_FAIL;
}
return SLANG_OK;
}
bool STBImage::isComparable(const ThisType& rhs) const
{
return (this == &rhs) ||
(m_width == rhs.m_width && m_height == rhs.m_height && m_numChannels == rhs.m_numChannels);
}
TestResult doImageComparison(TestContext* context, String const& filePath)
{
auto reporter = context->reporter;
// Allow a difference in the low bits of the 8-bit result, just to play it safe
static const int kAbsoluteDiffCutoff = 2;
// Allow a relative 1% difference
static const float kRelativeDiffCutoff = 0.01f;
String expectedPath = filePath + ".expected.png";
String actualPath = filePath + ".actual.png";
STBImage expectedImage;
if (SLANG_FAILED(expectedImage.read(expectedPath.getBuffer())))
{
reporter->messageFormat(TestMessageType::RunError, "Unable to load image ;%s'", expectedPath.getBuffer());
return TestResult::Fail;
}
STBImage actualImage;
if (SLANG_FAILED(actualImage.read(actualPath.getBuffer())))
{
reporter->messageFormat(TestMessageType::RunError, "Unable to load image ;%s'", actualPath.getBuffer());
return TestResult::Fail;
}
if (!expectedImage.isComparable(actualImage))
{
reporter->messageFormat(TestMessageType::TestFailure, "Images are different sizes '%s' '%s'", actualPath.getBuffer(), expectedPath.getBuffer());
return TestResult::Fail;
}
{
const unsigned char* expectedPixels = expectedImage.getPixels();
const unsigned char* actualPixels = actualImage.getPixels();
const int height = actualImage.getHeight();
const int width = actualImage.getWidth();
const int numChannels = actualImage.getNumChannels();
const int rowSize = width * numChannels;
for (int y = 0; y < height; ++y)
{
for (int i = 0; i < rowSize; ++i)
{
int expectedVal = expectedPixels[i];
int actualVal = actualPixels[i];
int absoluteDiff = actualVal - expectedVal;
if (absoluteDiff < 0) absoluteDiff = -absoluteDiff;
if (absoluteDiff < kAbsoluteDiffCutoff)
{
// There might be a difference, but we'll consider it to be inside tolerance
continue;
}
float relativeDiff = 0.0f;
if (expectedVal != 0)
{
relativeDiff = fabsf(float(actualVal) - float(expectedVal)) / float(expectedVal);
if (relativeDiff < kRelativeDiffCutoff)
{
// relative difference was small enough
continue;
}
}
// TODO: may need to do some local search sorts of things, to deal with
// cases where vertex shader results lead to rendering that is off
// by one pixel...
const int x = i / numChannels;
const int channelIndex = i % numChannels;
reporter->messageFormat(TestMessageType::TestFailure, "image compare failure at (%d,%d) channel %d. expected %d got %d (absolute error: %d, relative error: %f)\n",
x, y, channelIndex,
expectedVal,
actualVal,
absoluteDiff,
relativeDiff);
// There was a difference we couldn't excuse!
return TestResult::Fail;
}
expectedPixels += rowSize;
actualPixels += rowSize;
}
}
return TestResult::Pass;
}
TestResult runHLSLRenderComparisonTestImpl(
TestContext* context,
TestInput& input,
char const* expectedArg,
char const* actualArg)
{
auto filePath = input.filePath;
auto outputStem = input.outputStem;
String expectedOutput;
String actualOutput;
TestResult hlslResult = doRenderComparisonTestRun(context, input, expectedArg, ".expected", &expectedOutput);
if (hlslResult != TestResult::Pass)
{
return hlslResult;
}
TestResult slangResult = doRenderComparisonTestRun(context, input, actualArg, ".actual", &actualOutput);
if (slangResult != TestResult::Pass)
{
return slangResult;
}
if (context->isCollectingRequirements())
{
return TestResult::Pass;
}
Slang::File::writeAllText(outputStem + ".expected", expectedOutput);
Slang::File::writeAllText(outputStem + ".actual", actualOutput);
if( hlslResult == TestResult::Fail ) return TestResult::Fail;
if( slangResult == TestResult::Fail ) return TestResult::Fail;
if (actualOutput != expectedOutput)
{
context->reporter->dumpOutputDifference(expectedOutput, actualOutput);
return TestResult::Fail;
}
// Next do an image comparison on the expected output images!
TestResult imageCompareResult = doImageComparison(context, outputStem);
if(imageCompareResult != TestResult::Pass)
return imageCompareResult;
return TestResult::Pass;
}
TestResult runHLSLRenderComparisonTest(TestContext* context, TestInput& input)
{
return runHLSLRenderComparisonTestImpl(context, input, "-hlsl", "-slang");
}
TestResult runHLSLCrossCompileRenderComparisonTest(TestContext* context, TestInput& input)
{
return runHLSLRenderComparisonTestImpl(context, input, "-slang", "-glsl-cross");
}
TestResult runHLSLAndGLSLRenderComparisonTest(TestContext* context, TestInput& input)
{
return runHLSLRenderComparisonTestImpl(context, input, "-hlsl-rewrite", "-glsl-rewrite");
}
TestResult skipTest(TestContext* /* context */, TestInput& /*input*/)
{
return TestResult::Ignored;
}
// based on command name, dispatch to an appropriate callback
struct TestCommandInfo
{
char const* name;
TestCallback callback;
};
static const TestCommandInfo s_testCommandInfos[] =
{
{ "SIMPLE", &runSimpleTest},
{ "REFLECTION", &runReflectionTest},
{ "CPU_REFLECTION", &runReflectionTest},
{ "COMMAND_LINE_SIMPLE", &runSimpleCompareCommandLineTest},
{ "COMPARE_HLSL", &runHLSLComparisonTest},
{ "COMPARE_HLSL_RENDER", &runHLSLRenderComparisonTest},
{ "COMPARE_HLSL_CROSS_COMPILE_RENDER", &runHLSLCrossCompileRenderComparisonTest},
{ "COMPARE_HLSL_GLSL_RENDER", &runHLSLAndGLSLRenderComparisonTest},
{ "COMPARE_COMPUTE", &runSlangComputeComparisonTest},
{ "COMPARE_COMPUTE_EX", &runSlangComputeComparisonTestEx},
{ "HLSL_COMPUTE", &runHLSLComputeTest},
{ "COMPARE_RENDER_COMPUTE", &runSlangRenderComputeComparisonTest},
{ "COMPARE_GLSL", &runGLSLComparisonTest},
{ "CROSS_COMPILE", &runCrossCompilerTest},
{ "CPP_COMPILER_EXECUTE", &runCPPCompilerExecute},
{ "CPP_COMPILER_SHARED_LIBRARY", &runCPPCompilerSharedLibrary},
{ "CPP_COMPILER_COMPILE", &runCPPCompilerCompile},
{ "CPU_EXECUTE", &runCPUExecuteTest},
};
TestResult runTest(
TestContext* context,
String const& filePath,
String const& outputStem,
String const& testName,
TestOptions const& testOptions)
{
// If we are collecting requirements and it's diagnostic test, we always run
// (ie no requirements need to be captured - effectively it has 'no requirements')
if (context->isCollectingRequirements() && testOptions.type == TestOptions::Diagnostic)
{
return TestResult::Pass;
}
const SpawnType defaultSpawnType = context->options.useExes ? SpawnType::UseExe : SpawnType::UseSharedLibrary;
for( const auto& command : s_testCommandInfos)
{
if(testOptions.command != command.name)
continue;
TestInput testInput;
testInput.filePath = filePath;
testInput.outputStem = outputStem;
testInput.testOptions = &testOptions;
testInput.spawnType = defaultSpawnType;
return command.callback(context, testInput);
}
// No actual test runner found!
return TestResult::Fail;
}
bool testCategoryMatches(
TestCategory* sub,
TestCategory* sup)
{
auto ss = sub;
while(ss)
{
if(ss == sup)
return true;
ss = ss->parent;
}
return false;
}
bool testCategoryMatches(
TestCategory* categoryToMatch,
Dictionary<TestCategory*, TestCategory*> categorySet)
{
for( auto item : categorySet )
{
if(testCategoryMatches(categoryToMatch, item.Value))
return true;
}
return false;
}
bool testPassesCategoryMask(
TestContext* context,
TestOptions const& test)
{
// Don't include a test we should filter out
for( auto testCategory : test.categories )
{
if(testCategoryMatches(testCategory, context->options.excludeCategories))
return false;
}
// Otherwise include any test the user asked for
for( auto testCategory : test.categories )
{
if(testCategoryMatches(testCategory, context->options.includeCategories))
return true;
}
// skip by default
return false;
}
static void _calcSynthesizedTests(TestContext* context, RenderApiType synthRenderApiType, const List<TestDetails>& srcTests, List<TestDetails>& ioSynthTests)
{
// Add the explicit parameter
for (const auto& testDetails: srcTests)
{
const auto& requirements = testDetails.requirements;
// Render tests use renderApis...
// If it's an explicit test, we don't synth from it now
// TODO(JS): Arguably we should synthesize from explicit tests. In principal we can remove the explicit api apply another
// although that may not always work.
if (requirements.usedRenderApiFlags == 0 ||
requirements.explicitRenderApi != RenderApiType::Unknown)
{
continue;
}
TestDetails synthTestDetails(testDetails.options);
TestOptions& synthOptions = synthTestDetails.options;
// Mark as synthesized
synthOptions.isSynthesized = true;
StringBuilder builder;
builder << "-";
builder << RenderApiUtil::getApiName(synthRenderApiType);
synthOptions.args.add(builder);
// If the target is vulkan remove the -hlsl option
if (synthRenderApiType == RenderApiType::Vulkan)
{
Index index = synthOptions.args.indexOf("-hlsl");
if (index != Index(-1))
{
synthOptions.args.removeAt(index);
}
}
// Work out the info about this tests
context->testRequirements = &synthTestDetails.requirements;
runTest(context, "", "", "", synthOptions);
context->testRequirements = nullptr;
// It does set the explicit render target
SLANG_ASSERT(synthTestDetails.requirements.explicitRenderApi == synthRenderApiType);
// Add to the tests
ioSynthTests.add(synthTestDetails);
}
}
static bool _canIgnore(TestContext* context,
const TestRequirements& requirements)
{
// Work out what render api flags are available
const RenderApiFlags availableRenderApiFlags = requirements.usedRenderApiFlags ? _getAvailableRenderApiFlags(context) : 0;
// Are all the required backends available?
if (((requirements.usedBackendFlags & context->availableBackendFlags) != requirements.usedBackendFlags))
{
return true;
}
// Are all the required rendering apis available?
if ((requirements.usedRenderApiFlags & availableRenderApiFlags) != requirements.usedRenderApiFlags)
{
return true;
}
return false;
}
void runTestsOnFile(
TestContext* context,
String filePath)
{
// Gather a list of tests to run
FileTestList testList;
if( gatherTestsForFile(&context->categorySet, filePath, &testList) == TestResult::Ignored )
{
// Test was explicitly ignored
return;
}
// Note cases where a test file exists, but we found nothing to run
if( testList.tests.getCount() == 0 )
{
context->reporter->addTest(filePath, TestResult::Ignored);
return;
}
RenderApiFlags apiUsedFlags = 0;
RenderApiFlags explictUsedApiFlags = 0;
{
// We can get the test info for each of them
for (auto& testDetails : testList.tests)
{
auto& requirements = testDetails.requirements;
// Collect what the test needs (by setting restRequirements the test isn't actually run)
context->testRequirements = &requirements;
runTest(context, filePath, filePath, filePath, testDetails.options);
//
apiUsedFlags |= requirements.usedRenderApiFlags;
explictUsedApiFlags |= (requirements.explicitRenderApi != RenderApiType::Unknown) ? (RenderApiFlags(1) << int(requirements.explicitRenderApi)) : 0;
}
context->testRequirements = nullptr;
}
SLANG_ASSERT((apiUsedFlags & explictUsedApiFlags) == explictUsedApiFlags);
const RenderApiFlags availableRenderApiFlags = apiUsedFlags ? _getAvailableRenderApiFlags(context) : 0;
// If synthesized tests are wanted look into adding them
if (context->options.synthesizedTestApis && availableRenderApiFlags)
{
List<TestDetails> synthesizedTests;
// What render options do we want to synthesize
RenderApiFlags missingApis = (~apiUsedFlags) & (context->options.synthesizedTestApis & availableRenderApiFlags);
//const Index numInitialTests = testList.tests.getCount();
while (missingApis)
{
const int index = ByteEncodeUtil::calcMsb8(missingApis);
SLANG_ASSERT(index >= 0 && index <= int(RenderApiType::CountOf));
const RenderApiType synthRenderApiType = RenderApiType(index);
_calcSynthesizedTests(context, synthRenderApiType, testList.tests, synthesizedTests);
// Disable the bit
missingApis &= ~(RenderApiFlags(1) << index);
}
// Add all the synthesized tests
testList.tests.addRange(synthesizedTests);
}
// We have found a test to run!
int subTestCount = 0;
for( auto& testDetails : testList.tests )
{
int subTestIndex = subTestCount++;
// Check that the test passes our current category mask
if(!testPassesCategoryMask(context, testDetails.options))
{
continue;
}
// Work out the test stem
StringBuilder outputStem;
outputStem << filePath;
if (subTestIndex != 0)
{
outputStem << "." << subTestIndex;
}
// Work out the test name - taking into account render api / if synthesized
StringBuilder testName(outputStem);
if (testDetails.options.isSynthesized)
{
testName << " syn";
}
const auto& requirements = testDetails.requirements;
// Display list of used apis on render test
if (requirements.usedRenderApiFlags)
{
RenderApiFlags usedFlags = requirements.usedRenderApiFlags;
testName << " (";
bool isPrev = false;
while (usedFlags)
{
const int index = ByteEncodeUtil::calcMsb8(usedFlags);
const RenderApiType renderApiType = RenderApiType(index);
if (isPrev)
{
testName << ",";
}
testName << RenderApiUtil::getApiName(renderApiType);
// Disable bit
usedFlags &= ~(RenderApiFlags(1) << index);
isPrev = true;
}
testName << ")";
}
// Report the test and run/ignore
{
auto reporter = context->reporter;
TestReporter::TestScope scope(reporter, testName);
TestResult testResult = TestResult::Fail;
// If this test can be ignored
if (_canIgnore(context, testDetails.requirements))
{
testResult = TestResult::Ignored;
}
else
{
testResult = runTest(context, filePath, outputStem, testName, testDetails.options);
}
reporter->addResult(testResult);
// Could determine if to continue or not here... based on result
}
}
}
static bool endsWithAllowedExtension(
TestContext* /*context*/,
String filePath)
{
char const* allowedExtensions[] = {
".slang",
".hlsl",
".fx",
".glsl",
".vert",
".frag",
".geom",
".tesc",
".tese",
".comp",
".internal",
".ahit",
".chit",
".miss",
".rgen",
".c",
".cpp",
};
for( auto allowedExtension : allowedExtensions)
{
if(filePath.endsWith(allowedExtension))
return true;
}
return false;
}
static bool shouldRunTest(
TestContext* context,
String filePath)
{
if(!endsWithAllowedExtension(context, filePath))
return false;
if( context->options.testPrefix )
{
if( strncmp(context->options.testPrefix, filePath.begin(), strlen(context->options.testPrefix)) != 0 )
{
return false;
}
}
return true;
}
void runTestsInDirectory(
TestContext* context,
String directoryPath)
{
for (auto file : osFindFilesInDirectory(directoryPath))
{
if( shouldRunTest(context, file) )
{
// fprintf(stderr, "slang-test: found '%s'\n", file.getBuffer());
runTestsOnFile(context, file);
}
}
for (auto subdir : osFindChildDirectories(directoryPath))
{
runTestsInDirectory(context, subdir);
}
}
SlangResult innerMain(int argc, char** argv)
{
auto stdWriters = StdWriters::initDefaultSingleton();
// The context holds useful things used during testing
TestContext context;
SLANG_RETURN_ON_FAIL(SLANG_FAILED(context.init()))
auto& categorySet = context.categorySet;
// Set up our test categories here
auto fullTestCategory = categorySet.add("full", nullptr);
auto quickTestCategory = categorySet.add("quick", fullTestCategory);
/*auto smokeTestCategory = */categorySet.add("smoke", quickTestCategory);
auto renderTestCategory = categorySet.add("render", fullTestCategory);
/*auto computeTestCategory = */categorySet.add("compute", fullTestCategory);
auto vulkanTestCategory = categorySet.add("vulkan", fullTestCategory);
auto unitTestCatagory = categorySet.add("unit-test", fullTestCategory);
auto compatibilityIssueCategory = categorySet.add("compatibility-issue", fullTestCategory);
auto sharedLibraryCategory = categorySet.add("shared-library", fullTestCategory);
#if SLANG_WINDOWS_FAMILY
auto windowsCategory = categorySet.add("windows", fullTestCategory);
#endif
#if SLANG_UNIX_FAMILY
auto unixCatagory = categorySet.add("unix", fullTestCategory);
#endif
// An un-categorized test will always belong to the `full` category
categorySet.defaultCategory = fullTestCategory;
TestCategory* fxcCategory = nullptr;
TestCategory* dxcCategory = nullptr;
TestCategory* glslangCategory = nullptr;
// Work out what backends/pass-thrus are available
{
SlangSession* session = context.getSession();
for (int i = 0; i < SLANG_PASS_THROUGH_COUNT_OF; ++i)
{
SlangPassThrough passThru = SlangPassThrough(i);
if (SLANG_SUCCEEDED(spSessionCheckPassThroughSupport(session, passThru)))
{
context.availableBackendFlags |= PassThroughFlags(1) << int(i);
}
}
if (context.availableBackendFlags & PassThroughFlag::Fxc)
{
fxcCategory = categorySet.add("fxc", fullTestCategory);
}
if (context.availableBackendFlags & PassThroughFlag::Glslang)
{
glslangCategory = categorySet.add("glslang", fullTestCategory);
}
if (context.availableBackendFlags & PassThroughFlag::Dxc)
{
dxcCategory = categorySet.add("dxc", fullTestCategory);
}
}
// Working out what renderApis is worked on on demand through
// _getAvailableRenderApiFlags()
{
// We can set the slangc command line tool, to just use the function defined here
context.setInnerMainFunc("slangc", &SlangCTool::innerMain);
}
SLANG_RETURN_ON_FAIL(Options::parse(argc, argv, &categorySet, StdWriters::getError(), &context.options));
Options& options = context.options;
if (options.subCommand.getLength())
{
// Get the function from the tool
auto func = context.getInnerMainFunc(options.binDir, options.subCommand);
if (!func)
{
StdWriters::getError().print("error: Unable to launch tool '%s'\n", options.subCommand.getBuffer());
return SLANG_FAIL;
}
// Copy args to a char* list
const auto& srcArgs = options.subCommandArgs;
List<const char*> args;
args.setCount(srcArgs.getCount());
for (Index i = 0; i < srcArgs.getCount(); ++i)
{
args[i] = srcArgs[i].getBuffer();
}
return func(StdWriters::getSingleton(), context.getSession(), int(args.getCount()), args.getBuffer());
}
// On TeamCity CI there is an issue with unix/linux targets where test system may be different from the build system
// That when C/C++ code is compiled, it does so for the test systems arch not for the build system
// This leads to shared library not being loadable, so we need to disable such tests that have this requirement
if (options.outputMode == TestOutputMode::TeamCity)
{
#if SLANG_UNIX_FAMILY && SLANG_PROCESSOR_X86
// Disable shared library requiring tests
options.excludeCategories.Add(sharedLibraryCategory, sharedLibraryCategory);
#endif
}
if( options.includeCategories.Count() == 0 )
{
options.includeCategories.Add(fullTestCategory, fullTestCategory);
}
// Exclude rendering tests when building under AppVeyor.
//
// TODO: this is very ad hoc, and we should do something cleaner.
if( options.outputMode == TestOutputMode::AppVeyor )
{
options.excludeCategories.Add(renderTestCategory, renderTestCategory);
options.excludeCategories.Add(vulkanTestCategory, vulkanTestCategory);
}
{
// Setup the reporter
TestReporter reporter;
SLANG_RETURN_ON_FAIL(reporter.init(options.outputMode));
context.reporter = &reporter;
reporter.m_dumpOutputOnFailure = options.dumpOutputOnFailure;
reporter.m_isVerbose = options.shouldBeVerbose;
{
TestReporter::SuiteScope suiteScope(&reporter, "tests");
// Enumerate test files according to policy
// TODO: add more directories to this list
// TODO: allow for a command-line argument to select a particular directory
runTestsInDirectory(&context, "tests/");
}
// Run the unit tests (these are internal C++ tests - not specified via files in a directory)
// They are registered with SLANG_UNIT_TEST macro
{
TestReporter::SuiteScope suiteScope(&reporter, "unit tests");
TestReporter::set(&reporter);
// Run the unit tests
TestRegister* cur = TestRegister::s_first;
while (cur)
{
StringBuilder filePath;
filePath << "unit-tests/" << cur->m_name << ".internal";
TestOptions testOptions;
testOptions.categories.add(unitTestCatagory);
testOptions.command = filePath;
if (shouldRunTest(&context, testOptions.command))
{
if (testPassesCategoryMask(&context, testOptions))
{
reporter.startTest(testOptions.command);
// Run the test function
cur->m_func();
reporter.endTest();
}
else
{
reporter.addTest(testOptions.command, TestResult::Ignored);
}
}
// Next
cur = cur->m_next;
}
TestReporter::set(nullptr);
}
reporter.outputSummary();
return reporter.didAllSucceed() ? SLANG_OK : SLANG_FAIL;
}
}
int main(int argc, char** argv)
{
const SlangResult res = innerMain(argc, argv);
#ifdef _MSC_VER
_CrtDumpMemoryLeaks();
#endif
return SLANG_SUCCEEDED(res) ? 0 : 1;
}
|