File: test_builtins.cmd

package info (click to toggle)
wine 5.0.3-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 220,300 kB
  • sloc: ansic: 3,035,623; perl: 19,098; yacc: 16,358; makefile: 10,088; javascript: 9,150; objc: 6,590; lex: 5,734; python: 1,914; cpp: 1,042; sh: 759; java: 749; xml: 557; awk: 69; cs: 17
file content (3369 lines) | stat: -rw-r--r-- 93,660 bytes parent folder | download
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
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
echo Tests for cmd's builtin commands

@echo on
echo ------------ Testing 'echo' [ON] ------------
echo word
echo 'singlequotedword'
echo "doublequotedword"
@echo at-echoed-word
echo "/?"
echo.
echo .
echo.word
echo .word
echo:
echo :
echo:word
echo :word
echo/
echo /
echo/word
echo /word
echo off now
echo word@space@
echo word@space@@space@
 echo word
echo@tab@word
echo@tab@word @tab@
echo@tab@word@tab@@space@
@tab@echo word
echo @tab@word
echo  @tab@word
echo@tab@@tab@word
echo @tab@ on @space@
@echo --- @ with chains and brackets
(echo the @ character chains until&&@echo we leave the current depth||(
echo hidden
@echo hidden
))&&echo and can hide brackets||(@echo command hidden)||@(echo brackets hidden)
@echo ---

@echo off
echo off@tab@@space@
@echo noecho1
 @echo noecho2
@@@@@echo echo3
echo ------------ Testing 'echo' [OFF] ------------
echo word
echo 'singlequotedword'
echo "doublequotedword"
@echo at-echoed-word
echo "/?"
echo.
echo .
echo.word
echo .word
echo:
echo :
echo:word
echo :word
echo/
echo /
echo/word
echo /word
echo on again
echo word@space@
echo word@space@@space@
 echo word
echo@tab@word
echo@tab@word @tab@
echo@tab@word@tab@@space@
@tab@echo word
echo @tab@word
echo  @tab@word
echo@tab@@tab@word

echo ------------ Testing mixed echo modes ------------
echo @echo on> mixedEchoModes.cmd
echo if 1==1 echo foo>> mixedEchoModes.cmd
echo if 1==1 @echo bar>> mixedEchoModes.cmd
echo @echo off>> mixedEchoModes.cmd
echo if 1==1 echo foo2>> mixedEchoModes.cmd
echo if 1==1 @echo bar2>> mixedEchoModes.cmd
type mixedEchoModes.cmd
cmd /c mixedEchoModes.cmd
del mixedEchoModes.cmd

echo ------------ Testing parameterization ------------
call :TestParm a b c
call :TestParm "a b c"
call :TestParm "a b"\c
call :TestParm a=~`+,.{}!+b
call :TestParm a;b
call :TestParm "a;b"
call :TestParm a^;b
call :TestParm a[b]{c}(d)e
call :TestParm a&echo second line
call :TestParm a   b,,,c
call :TestParm a==b;;c
call :TestParm       a,,,  b
goto :TestRem

:TestParm
echo '%1', '%2', '%3'
goto :eof

:TestRem
echo ------------ Testing rem ------------
rem Hello
rem  Hello
rem   Hello || foo
rem echo lol
rem echo foo & echo bar
rem @tab@  Hello
rem@tab@  Hello
rem@tab@echo foo & echo bar
@echo on
rem Hello
rem  Hello
rem   Hello || foo
rem echo lol
rem echo foo & echo bar
rem @tab@  Hello
rem@tab@  Hello
rem@tab@echo foo & echo bar
@echo off

echo ------------ Testing redirection operators ------------
mkdir foobar & cd foobar
echo --- stdout redirection
echo foo>foo
type foo
echo foo 1> foo
type foo
echo foo@tab@1> foo
type foo
echo foo 1>@tab@foo
type foo
echo foo@tab@1>@tab@foo
type foo
echo foo7 7> foo
type foo
echo foo9 9> foo
type foo
echo foo1> foo
type foo
echo foo11> foo
type foo
echo foo12> foo
type foo
echo foo13>"foo"
type foo
echo foo14>."\foo"
type foo
echo foo15>."\f"oo
type foo
del foo
echo1>foo
type foo
echo --- stdout appending
echo foo>foo
echo foo >>foo
type foo
del foo
echo foob >> foo
type foo
echo fooc 1>>foo
type foo
echo food1>>foo
type foo
echo food2>>"foo"
type foo
del foo
echo food21>>foo
type foo
del foo
echo foo> foo
echo foo7 7>> foo || (echo not supported & del foo)
if exist foo (type foo) else echo not supported
echo --- redirections within IF statements
if 1==1 echo foo1>bar
type bar & del bar
if 1==1 echo foo2>>bar
type bar & del bar
echo -----
if 1==1 (echo foo2>bar) else echo baz2>bar
type bar & del bar
if 1==1 (echo foo3) else echo baz3>bar
type bar || echo file does not exist, ok
if 1==1 (echo foo4>bar) else echo baz4>bar
type bar & del bar
if 1==0 (echo foo5>bar) else echo baz5>bar
type bar & del bar
if 1==0 (echo foo6) else echo baz6 1>bar
type bar & del bar
if 1==0 (echo foo7 1>bar) else echo baz7>bar
type bar & del bar
if 1==0 (echo foo8 1>bar) else echo baz8>bak
type bak
if 1==1 (echo foo>bar & echo baz)
type bar
if 1==1 (
   echo foo>bar
   echo baz
)
type bar
(if 1==1 (echo A) else echo B) > C
type C
(if 1==0 (echo A) else echo B) > C
type C
(if 1==0 (echo A > B) else echo C)
cd .. & rd /s/q foobar

echo ------------ Testing circumflex escape character ------------
rem Using something like "echo foo^" asks for an additional char after a "More?" prompt on the following line; it's not possible to currently test that non-interactively
echo ^hell^o, world
echo hell^o, world
echo hell^^o, world
echo hell^^^o, world
echo hello^
world
echo hello^

world
echo hello^


echo finished
mkdir foobar
echo baz> foobar\baz
type foobar\baz
type foobar^\baz
rd /s/q foobar
echo foo ^| echo bar
echo foo ^& echo bar
call :setError 0
echo bak ^&& echo baz 2> nul
echo %ErrorLevel%
echo foo ^> foo
echo ^<> foo
type foo
del foo
set WINE_FOO=oof
echo ff^%WINE_FOO%
set WINE_FOO=bar ^| baz
set WINE_FOO
rem FIXME: echoing %WINE_FOO% gives an error (baz not recognized) but prematurely
rem exits the script on windows; redirecting stdout and/or stderr doesn't help
echo %ErrorLevel%
call :setError 0
set WINE_FOO=bar ^^^| baz
set WINE_FOO
echo %WINE_FOO%
echo %ErrorLevel%
set WINE_FOO=

echo ------------ Testing chains ------------
rem The chain operators have the following bottom-up precedence:
rem 'else' precedes nothing and matches the closest unmatched 'if' in the same bracket depth
rem '&' precedes 'else'
rem '||' precedes '&' and 'else'
rem '&&' precedes '||', '&' and 'else'
rem '|' precedes '&&', '||', '&' and 'else'
rem
rem Example: 'if 1==1 if 2==2 if 3==3 a | b && c || d & e else f else g' is interpreted as
rem          'if 1==1 (if 2==2 (if 3==3 ((((a | b) && c) || d) & e) else f) else g)'
goto :cfailend
:cfail
echo %1
call :setError 1
goto :eof
:cfailend
echo --- chain success
echo a1&echo a2
echo b1&&echo b2
echo c1||echo c2
echo ---
echo d1&echo d2&echo d3
echo e1&echo e2&&echo e3
echo f1&echo f2||echo f3
echo ---
echo g1&&echo g2&echo g3
echo h1&&echo h2&&echo h3
echo i1&&echo i2||echo i3
echo ---
echo j1||echo j2&echo j3
echo ---
echo k1||echo k2&&echo k3
echo ---
echo l1||echo l2||echo l3
echo ---
echo --- chain failure
call :cfail a1&call :cfail a2
call :cfail b1&&call :cfail b2
echo ---
call :cfail c1||call :cfail c2
call :cfail d1&call :cfail d2&call :cfail d3
call :cfail e1&call :cfail e2&&call :cfail e3
echo ---
call :cfail f1&call :cfail f2||call :cfail f3
call :cfail g1&&call :cfail g2&call :cfail g3
echo ---
call :cfail h1&&call :cfail h2&&call :cfail h3
echo ---
call :cfail i1&&call :cfail i2||call :cfail i3
echo ---
call :cfail j1||call :cfail j2&call :cfail j3
call :cfail k1||call :cfail k2&&call :cfail k3
echo ---
call :cfail l1||call :cfail l2||call :cfail l3
echo --- chain brackets
rem Brackets are like regular commands, they support redirections
rem and have the same precedence as regular commands.
echo a1&(echo a2&echo a3)
echo b1&(echo b2&&echo b3)
echo c1&(echo c2||echo c3)
echo ---
echo d1&&(echo d2&echo d3)
echo e1&&(echo e2&&echo e3)
echo f1&&(echo f2||echo f3)
echo ---
echo g1||(echo g2&echo g3)
echo ---
echo h1||(echo h2&&echo h3)
echo ---
echo i1||(echo i2||echo i3)
echo ---
call :cfail j1&(call :cfail j2&call :cfail j3)
call :cfail k1&(call :cfail k2&&call :cfail k3)
echo ---
call :cfail l1&(call :cfail l2||call :cfail l3)
call :cfail m1&&(call :cfail m2&call :cfail m3)
echo ---
call :cfail n1&&(call :cfail n2&&call :cfail n3)
echo ---
call :cfail o1&&(call :cfail o2||call :cfail o3)
echo ---
call :cfail p1||(call :cfail p2&call :cfail p3)
call :cfail q1||(call :cfail q2&&call :cfail q3)
echo ---
call :cfail r1||(call :cfail r2||call :cfail r3)
echo --- chain pipe
rem Piped commands run at the same time, so the print order varies.
rem Additionally, they don't run in the batch script context, as shown by
rem 'call :existing_label|echo read the error message'.
(echo a 1>&2|echo a 1>&2) 2>&1
echo ---
echo b1|echo b2
echo c1&&echo c2|echo c3
echo d1||echo d2|echo d3
echo ---
echo e1&echo e2|echo e3
echo f1|echo f2&&echo f3
echo g1|echo g2||echo g3
echo ---
echo h1|echo h2&echo h3
echo i1|echo i2|echo i3
echo --- chain pipe input
rem The output data of the left side of a pipe can disappear, probably
rem because it finished too fast and closed the pipe before it could be read,
rem which means we can get broken results for the tests of this section.
echo @echo off> tmp.cmd
echo set IN=X>> tmp.cmd
echo set /p IN=%%1:>> tmp.cmd
echo setlocal EnableDelayedExpansion>> tmp.cmd
echo echo [!IN!,%%1]>> tmp.cmd
echo endlocal>> tmp.cmd
echo set IN=>> tmp.cmd
echo a1|cmd /ctmp.cmd a2
echo b1|cmd /ctmp.cmd b2|cmd /ctmp.cmd b3
echo c1|cmd /ctmp.cmd c2|cmd /ctmp.cmd c3|cmd /ctmp.cmd c4
echo d1|call tmp.cmd d2
echo e1|call tmp.cmd e2|call tmp.cmd e3
echo f1|call tmp.cmd f2|call tmp.cmd f3|call tmp.cmd f4
rem FIXME these 3 tests cause "unexpected end of output"
rem test  : echo g1|tmp.cmd g2
rem result: g2:[g1,g2]
rem test  : echo h1|tmp.cmd h2|tmp.cmd h3
rem result: h3:[h2:[h1,h2],h3]@or_broken@h3:[h2:,h3]
rem test  : echo i1|tmp.cmd i2|tmp.cmd i3|tmp.cmd i4
rem result: i4:[i3:[i2:[i1,i2],i3],i4]@or_broken@i4:[i3:[i2:,i3],i4]@or_broken@i4:[i3:,i4]
del tmp.cmd
echo --- chain else
rem Command arguments are gready and eat up the 'else' unless terminated by
rem brackets, which means the 'else' can only be recognized when the
rem 'if true' command chain ends with brackets.
if 1==1 if 2==2 if 3==3 (echo a1) else (echo a2) else echo a3
if 1==1 if 2==2 if 3==0 (echo b1) else (echo b2) else echo b3
echo ---
if 1==1 if 2==0 if 3==3 (echo c1) else (echo c2) else echo c3
echo ---
if 1==1 if 2==0 if 3==0 (echo d1) else (echo d2) else echo d3
echo ---
if 1==0 if 2==2 if 3==3 (echo e1) else (echo e2) else echo e3
echo ---
if 1==0 if 2==2 if 3==0 (echo f1) else (echo f2) else echo f3
echo ---
if 1==0 if 2==0 if 3==3 (echo g1) else (echo g2) else echo g3
echo ---
if 1==0 if 2==0 if 3==0 (echo h1) else (echo h2) else echo h3
echo ---
echo --- chain else (if true)
if 1==1 echo a1 else echo a2
if 1==1 echo b1|echo b2 else echo b3
if 1==1 echo c1&&echo c2 else echo c3
if 1==1 echo d1||echo d2 else echo d3
echo ---
if 1==1 echo e1&echo e2 else echo e3
if 1==1 echo f1 else echo f2|echo f3
if 1==1 echo g1 else echo g2&&echo g3
if 1==1 echo h1 else echo h2||echo h3
echo ---
if 1==1 echo i1 else echo i2&echo i3
if 1==1 echo j1|(echo j2) else echo j3
echo ---
if 1==1 echo k1&&(echo k2) else echo k3
if 1==1 echo l1||(echo l2) else echo l3
echo ---
if 1==1 echo m1&(echo m2) else echo m3
if 1==1 (echo n1) else echo n2|echo n3
if 1==1 (echo o1) else echo o2&&echo o3
if 1==1 (echo p1) else echo p2||echo p3
if 1==1 (echo q1) else echo q2&echo q3
echo ---
echo --- chain else (if false)
if 1==0 echo a1 else echo a2
if 1==0 echo b1|echo b2 else echo b3
if 1==0 echo c1&&echo c2 else echo c3
if 1==0 echo d1||echo d2 else echo d3
if 1==0 echo e1&echo e2 else echo e3
if 1==0 echo f1 else echo f2|echo f3
if 1==0 echo g1 else echo g2&&echo g3
if 1==0 echo h1 else echo h2||echo h3
if 1==0 echo i1 else echo i2&echo i3
if 1==0 echo j1|(echo j2) else echo j3
echo ---
if 1==0 echo k1&&(echo k2) else echo k3
if 1==0 echo l1||(echo l2) else echo l3
if 1==0 echo m1&(echo m2) else echo m3
if 1==0 (echo n1) else echo n2|echo n3
if 1==0 (echo o1) else echo o2&&echo o3
if 1==0 (echo p1) else echo p2||echo p3
echo ---
if 1==0 (echo q1) else echo q2&echo q3
echo ------------ Testing 'set' ------------
call :setError 0
rem Remove any WINE_FOO* WINE_BA* environment variables from shell before proceeding
for /f "delims==" %%i in ('set WINE_ba') do set %%i=
for /f "delims==" %%i in ('set WINE_foo') do set %%i=
set WINE_FOOBAR 2> nul > nul
echo %ErrorLevel%
set WINE_FOOBAR =  baz
echo %ErrorLevel%
echo %WINE_FOOBAR%WINE_FOOBAR not defined
echo %WINE_FOOBAR %
set WINE_FOOBAR 2> nul
set WINE_FOOBAR =  baz2
echo %ErrorLevel%
echo %WINE_fOObAr %
set WINE_FOOBAR= bar
echo %ErrorLevel%
echo %WINE_FOOBAR%
set WINE_FOO
set WINE_FOOBAR=
set WINE_FOOB
echo %WINE_FOOBAR%WINE_FOOBAR not defined
set WINE_FOOBAR =
set WINE_FOOBA 2> nul > nul
echo %ErrorLevel%
set WINE_FOO=bar
echo %WINE_FOO%
set WINE_FOO=foo
set WINE_BAR=bar
echo %WINE_FOO%%WINE_BAR%
set WINE_BAR=
set WINE_FOO=
set WINE_FOO=%WINE_FOO%
echo %WINE_FOO%WINE_FOO not defined
set WINE_BAZ%=bazbaz
set WINE_BA
echo %WINE_BAZ%%
set WINE_BAZ%=
echo set "WINE_FOO=bar" should not include the quotes in the variable value
set "WINE_FOO=bar"
echo %WINE_FOO%
set@tab@WINE_FOO=foo
echo %WINE_FOO%
set@tab@WINE_FOO=
echo '%WINE_FOO%'
set WINE_FOO=foo@space@
echo '%WINE_FOO%'
set WINE_FOO=foo@tab@
echo '%WINE_FOO%'
rem Space symbol must appear in `var`
set WINE_FOO=value@space@
echo '%WINE_FOO%'
rem Space symbol must NOT appear in `var`
set "WINE_FOO=value"@space@
echo '%WINE_FOO%'
rem Mixed examples:
set WINE_FOO=jim fred
echo '%WINE_FOO%'
set WINE_FOO="jim" fred
echo '%WINE_FOO%'
set "WINE_FOO=jim fred"
echo '%WINE_FOO%'
set "WINE_FOO=jim" fred
echo '%WINE_FOO%'
rem Only the final quote ends the string
set "WINE_FOO=apple"banana"grape"orange
echo '%WINE_FOO%'
set WINE_FOO=
rem set PATH must work with quotes
set PATH_BACKUP=%PATH%
mkdir folder
mkdir "fol;der"
echo echo I'm here! > "fol;der\sub1.bat"
echo echo I'm here! > folder\sub1.bat
set PATH=nothing;"fol;der"
call sub1
set PATH="folder
call sub1
set PATH=folder"
call sub1
del "fol;der\sub1.bat"
del folder\sub1.bat
rmdir "fol;der"
rmdir folder
PATH=%PATH_BACKUP%

echo ------------ Testing 'choice' ------------

rem Windows XP and Windows 2000 do not come with choice
rem echo is used for @or_broken@ formatting
choice /C:ABC /M "Example message" /D A /T:0
if %ERRORLEVEL% EQU 9009 (
  echo choice unavailable
)
echo %ERRORLEVEL%
choice /C ABC "/M:Example message" /D:B /T 0
if %ERRORLEVEL% EQU 9009 (
  echo choice unavailable
)
echo %ERRORLEVEL%
choice /C def /D:f /T:0
if %ERRORLEVEL% EQU 9009 (
  echo choice unavailable
)
echo %ERRORLEVEL%
REM If a pipe fails due to a nonexistent command
REM it will stop the whole program's execution
if %ERRORLEVEL% NEQ 9009 (
  echo Y | choice /C ABCXYZ /D A /T 2
)
if %ERRORLEVEL% EQU 9009 (
  echo choice unavailable
)
echo %ERRORLEVEL%
choice /C ABC /N /D A /T 0
if %ERRORLEVEL% EQU 9009 (
  echo choice unavailable
)
echo %ERRORLEVEL%
choice /C abcABC /CS /D:A /T:0
if %ERRORLEVEL% EQU 9009 (
  echo choice unavailable
)
echo %ERRORLEVEL%
rem intentional error
choice /C abcABC /D:A /T:0 >NUL 2>NUL
echo %ERRORLEVEL%

echo ------------ Testing variable expansion ------------
call :setError 0
echo ~p0 should be path containing batch file
echo %~p0
mkdir dummydir
cd dummydir
echo %~p0
cd ..
rmdir dummydir
echo ~dp0 should be directory containing batch file
echo %~dp0
mkdir dummydir
cd dummydir
echo %~dp0
cd ..
rmdir dummydir
echo CD value %CD%
echo %%
echo P%
echo %P
echo %WINE_UNKNOWN%S
echo P%WINE_UNKNOWN%
echo P%WINE_UNKNOWN%S
echo %ERRORLEVEL
echo %ERRORLEVEL%
echo %ERRORLEVEL%%ERRORLEVEL%
echo %ERRORLEVEL%ERRORLEVEL%
echo %ERRORLEVEL%%
echo %ERRORLEVEL%%%
echo P%ERRORLEVEL%
echo %ERRORLEVEL%S
echo P%ERRORLEVEL%S

echo ------------ Testing variable substrings ------------
set WINE_VAR=qwerty
echo %WINE_VAR:~0,1%
echo %WINE_VAR:~0,3%
echo %WINE_VAR:~2,2%
echo '%WINE_VAR:~-2,3%'
echo '%WINE_VAR:~-2,1%'
echo %WINE_VAR:~2,-1%
echo %WINE_VAR:~2,-3%
echo '%WINE_VAR:~-2,-4%'
echo %WINE_VAR:~-3,-2%
set WINE_VAR=

echo ------------ Testing variable substitution ------------
echo --- in FOR variables
for %%i in ("A B" C) do echo %%i
rem check works when prefix with @
@for %%i in ("A B" C) do echo %%i
rem quotes removal
for %%i in ("A B" C) do echo '%%~i'
rem fully qualified path
for %%f in ("C D" E) do echo %%~ff
rem drive letter
for %%i in ("F G" H) do echo %%~di
rem path
for %%d in ("I J" K) do echo %%~pd
rem filename
for %%i in ("L M" N) do echo %%~ni
rem file extension
for %%i in ("O. P.OOL" Q.TABC hello) do echo '%%~xi'
rem path with short path names
for %%I in ("R S" T ABCDEFGHIJK.LMNOP) do echo '%%~sI'
rem file attribute
for %%i in ("U V" W) do echo '%%~ai'
echo foo> foo
for %%i in (foo) do echo '%%~ai'
for %%i in (foo) do echo '%%~zi'
del foo
rem file date/time
rem Not fully testable, until we can grep dir's output to get foo's creation time in an envvar...
for %%i in ("a b" c) do echo '%%~ti'
rem file size
rem Similar issues as above
for %%i in ("a b" c) do echo '%%~zi'
rem combined options
for %%i in ("d e" f) do echo %%~dpi
for %%i in ("g h" i) do echo %%~sdi
for %%i in ("g h" i) do echo %%~dsi
for %%i in ("j k" l.eh) do echo '%%~xsi'
for %%i in ("") do echo '%%~i,%%~fi,%%~di,%%~pi,%%~ni,%%~xi,%%~si,%%~ai,%%~ti,%%~zi'

echo --- in parameters
for %%i in ("A B" C) do call :echoFun %%i
rem quotes removal
for %%i in ("A B" C) do call :echoFunQ %%i
rem fully qualified path
for %%f in ("C D" E) do call :echoFunF %%f
rem drive letter
for %%i in ("F G" H) do call :echoFunD %%i
rem path
for %%d in ("I J" K) do call :echoFunP %%d
rem filename
for %%i in ("L M" N) do call :echoFunN %%i
rem file extension
for %%i in ("O. P.OOL" Q.TABC hello) do call :echoFunX %%i
rem path with short path names
for %%I in ("R S" T ABCDEFGHIJK.LMNOP) do call :echoFunS %%I
rem NT4 aborts whole script execution when encountering ~a, ~t and ~z substitutions, preventing full testing
rem combined options
for %%i in ("d e" f) do call :echoFunDP %%i
for %%i in ("g h" i) do call :echoFunSD %%i
for %%i in ("g h" i) do call :echoFunDS %%i
for %%i in ("j k" l.eh) do call :echoFunXS %%i

goto :endEchoFuns
:echoFun
echo %1
goto :eof

:echoFunQ
echo '%~1'
goto :eof

:echoFunF
echo %~f1
goto :eof

:echoFunD
echo %~d1
goto :eof

:echoFunP
echo %~p1
goto :eof

:echoFunN
echo %~n1
goto :eof

:echoFunX
echo '%~x1'
goto :eof

:echoFunS
rem some NT4 workaround
set WINE_VAR='%~s1'
echo %WINE_VAR%
set WINE_VAR=
goto :eof

:echoFunDP
echo %~dp1
goto :eof

:echoFunSD
echo %~sd1
goto :eof

:echoFunDS
echo %~ds1
goto :eof

:echoFunXS
echo '%~xs1'
goto :eof
:endEchoFuns

echo ------------ Testing parameter zero ------------
call :func parm1 parm2
goto :endParm0
:func
echo %~0 %~1
echo [%0] [%~d0] [%~p0] [%~n0] [%~x0] [%~s0]
goto :EOF
:endParm0

echo ------------ Testing variable delayed expansion ------------
rem NT4 doesn't support this
echo --- default mode (load-time expansion)
set WINE_FOO=foo
echo %WINE_FOO%
echo !WINE_FOO!
if %WINE_FOO% == foo (
    set WINE_FOO=bar
    if %WINE_FOO% == bar (echo bar) else echo foo
)

set WINE_FOO=foo
if %WINE_FOO% == foo (
    set WINE_FOO=bar
    if !WINE_FOO! == bar (echo bar) else echo foo
)

echo --- runtime (delayed) expansion mode
setlocal EnableDelayedExpansion
set WINE_FOO=foo
echo %WINE_FOO%
echo !WINE_FOO!
if %WINE_FOO% == foo (
    set WINE_FOO=bar
    if %WINE_FOO% == bar (echo bar) else echo foo
)

set WINE_FOO=foo
if %WINE_FOO% == foo (
    set WINE_FOO=bar
    if !WINE_FOO! == bar (echo bar) else echo foo
)
echo %ErrorLevel%
setlocal DisableDelayedExpansion
echo %ErrorLevel%
set WINE_FOO=foo
echo %WINE_FOO%
echo !WINE_FOO!
set WINE_FOO=
echo --- using /V cmd flag
echo @echo off> tmp.cmd
echo set WINE_FOO=foo>> tmp.cmd
echo echo %%WINE_FOO%%>> tmp.cmd
echo echo !WINE_FOO!>> tmp.cmd
echo set WINE_FOO=>> tmp.cmd
cmd /V:ON /C tmp.cmd
cmd /V:OfF /C tmp.cmd
del tmp.cmd

echo ------------ Testing conditional execution ------------
echo --- unconditional ampersand
call :setError 123 & echo foo1
echo bar2 & echo foo2
mkdir foobar & cd foobar
echo > foobazbar
cd .. & rd /s/q foobar
if exist foobazbar (
    echo foobar not deleted!
    cd ..
    rd /s/q foobar
) else echo foobar deleted
echo --- on success conditional and
call :setError 456 && echo foo3 > foo3
if exist foo3 (
    echo foo3 created
    del foo3
) else echo foo3 not created
echo bar4 && echo foo4
echo --- on failure conditional or
call :setError 789 || echo foo5
echo foo6 || echo bar6 > bar6
if exist bar6 (
    echo bar6 created
    del bar6
)

echo ------------ Testing cd ------------
mkdir foobar
cd foobar
echo blabla > singleFile
dir /b
echo Current dir: %CD%
cd
cd ..
cd
cd foobar@space@
cd
cd ..
cd
cd @space@foobar
cd
cd..
cd
cd foobar
cd..@space@
cd
if not exist foobar (cd ..)
cd foobar
cd@tab@..@tab@@space@@tab@
cd
if not exist foobar (cd ..)
cd foobar
mkdir "bar bak"
cd "bar bak"
cd
cd ..
cd ".\bar bak"
cd
cd ..
cd .\"bar bak"
cd
cd ..
cd bar bak
cd
cd "bar bak@space@"@tab@@space@
cd
cd ..\..
cd
rd /Q/s foobar
mkdir foobar
cd /d@tab@foobar
cd
cd ..
rd /q/s foobar

echo ------------ Testing type ------------
echo bar> foobaz
@echo on
type foobaz
echo ---
@echo off
type foobaz@tab@
echo ---1
type ."\foobaz"
echo ---2
type ".\foobaz"
echo ---3
echo foo> foobay
echo ---4
type foobaz foobay > foobax 2> foobaw
echo ---5
type foobax
echo ---6
type foobaw
echo ---7
del foobaz foobay foobax foobaw

echo ------------ Testing NUL ------------
md foobar & cd foobar
rem NUL file (non) creation + case insensitivity
rem Note: "if exist" does not work with NUL, so to check for file existence we use a kludgy workaround
echo > bar
echo foo > NUL
dir /b /a-d
echo foo > nul
dir /b /a-d
echo foo > NuL
@tab@dir /b@tab@/a-d
del bar
rem NUL not special everywhere
call :setError 123
echo NUL> foo
if not exist foo (echo foo should have been created) else (
    type foo
    del foo
)
rem Empty file creation
copy nul foo > nul
if exist foo (
    echo foo created
    del foo
    type foo
) else (
    echo ***
)
echo 1234 >a.a
copy a.a+NUL b.b >nul
call :CheckFileSize a.a 7 b.b 8
copy NUL+a.a b.b >nul
call :CheckFileSize a.a 7 b.b 8
mkdir subdir
copy a.a+NUL subdir\ >nul
call :CheckFileSize a.a 7 subdir\a.a 8
del subdir\a.a
cd subdir
copy ..\a.a NUL >nul
if exist a.a echo Failed
cd ..
rd subdir /s /q
del a.a b.b
cd .. & rd foobar /s /q

echo ------------ Testing if/else ------------
echo --- if/else should work with blocks
if 0 == 0 (
  echo if seems to work
) else (
  echo if seems to be broken
)
if 1 == 0 (
  echo else seems to be broken
) else (
  echo else seems to work
)
if /c==/c (
  echo if seems not to detect /c as parameter
) else (
  echo parameter detection seems to be broken
)
SET elseIF=0
if 1 == 1 (
  SET /a elseIF=%elseIF%+1
) else if 1 == 1 (
  SET /a elseIF=%elseIF%+2
) else (
  SET /a elseIF=%elseIF%+2
)
if %elseIF% == 1 (
  echo else if seems to work
) else (
  echo else if seems to be broken
)
SET elseIF=0
if 1 == 2 (
  SET /a elseIF=%elseIF%+2
) else if 1 == 1 (
  SET /a elseIF=%elseIF%+1
) else (
  SET /a elseIF=%elseIF%+2
)
if %elseIF% == 1 (
  echo else if seems to work
) else (
  echo else if seems to be broken
)
SET elseIF=0
if 1 == 2 (
  SET /a elseIF=%elseIF%+2
) else if 1 == 2 (
  SET /a elseIF=%elseIF%+2
) else (
  SET /a elseIF=%elseIF%+1
)
if %elseIF% == 1 (
  echo else if seems to work
) else (
  echo else if seems to be broken
)
if "x" == "a" (
  echo broken1
) else (
  echo expected1
  if "y" == "b" echo broken2
  echo expected post-embedded if
)
if ()==() (
  echo comparison operators surrounded by brackets seem to work
) else (
  echo comparison operators surrounded by brackets seem to be broken
)
if 1(==1( (
  echo comparison operators surrounded by brackets seem to work
) else (
  echo comparison operators surrounded by brackets seem to be broken
)
if )==) (
  echo comparison operators surrounded by brackets seem to work
) else (
  echo comparison operators surrounded by brackets seem to be broken
)
if /i not (a)==(b) (
  echo comparison operators surrounded by brackets seem to work
) else (
  echo comparison operators surrounded by brackets seem to be broken
)
echo --- case sensitivity with and without /i option
if bar==BAR echo if does not default to case sensitivity
if not bar==BAR echo if seems to default to case sensitivity
if /i foo==FOO echo if /i seems to work
if /i not foo==FOO echo if /i seems to be broken
if /I foo==FOO echo if /I seems to work
if /I not foo==FOO echo if /I seems to be broken

echo --- string comparisons
if abc == abc  (echo equal) else echo non equal
if abc =="abc" (echo equal) else echo non equal
if "abc"== abc (echo equal) else echo non equal
if "abc"== "abc" (echo equal) else echo non equal

echo --- tabs handling
if@tab@1==1 echo doom
if @tab@1==1 echo doom
if 1==1 (echo doom) else@tab@echo quake
if@tab@not @tab@1==@tab@0 @tab@echo lol
if 1==0@tab@(echo doom) else echo quake
if 1==0 (echo doom)@tab@else echo quake
if 1==0 (echo doom) else@tab@echo quake

echo --- comparison operators
rem NT4 misevaluates conditionals in for loops so we have to use subroutines as workarounds
echo ------ for strings
rem NT4 stops processing of the whole batch file as soon as it finds a
rem comparison operator non fully uppercased, such as lss instead of LSS, so we
rem can't test those here.
if LSS LSS LSSfoo (echo LSS string can be used as operand for LSS comparison)
if LSS LSS LSS (echo bar)
if 1.1 LSS 1.10 (echo floats are handled as strings)
if "9" LSS "10" (echo numbers in quotes recognized!) else echo numbers in quotes are handled as strings
if not "-1" LSS "1" (echo negative numbers as well) else echo NT4
if /i foo LSS FoOc echo if /i seems to work for LSS
if /I not foo LSS FOOb echo if /I seems to be broken for LSS
set WINE_STR_PARMS=A B AB BA AA
for %%i in (%WINE_STR_PARMS%) do (
    for %%j in (%WINE_STR_PARMS%) do (
        call :LSStest %%i %%j))
if b LSS B (echo b LSS B) else echo NT4
if /I b LSS B echo b LSS B insensitive
if b LSS A echo b LSS A
if /I b LSS A echo b LSS A insensitive
if a LSS B (echo a LSS B) else echo NT4
if /I a LSS B echo a LSS B insensitive
if A LSS b echo A LSS b
if /I A LSS b echo A LSS b insensitive
for %%i in (%WINE_STR_PARMS%) do (
    for %%j in (%WINE_STR_PARMS%) do (
        call :LEQtest %%i %%j))
if b LEQ B (echo b LEQ B) else echo NT4
if /I b LEQ B echo b LEQ B insensitive
if b LEQ A echo b LEQ A
if /I b LEQ A echo b LEQ A insensitive
if a LEQ B (echo a LEQ B) else echo NT4
if /I a LEQ B echo a LEQ B insensitive
if A LEQ b echo A LEQ b
if /I A LEQ b echo A LEQ b insensitive
for %%i in (%WINE_STR_PARMS%) do (
    for %%j in (%WINE_STR_PARMS%) do (
        call :EQUtest %%i %%j))
if /I A EQU a echo A EQU a insensitive
for %%i in (%WINE_STR_PARMS%) do (
    for %%j in (%WINE_STR_PARMS%) do (
        call :NEQtest %%i %%j))
for %%i in (%WINE_STR_PARMS%) do (
    for %%j in (%WINE_STR_PARMS%) do (
        call :GEQtest %%i %%j))
for %%i in (%WINE_STR_PARMS%) do (
    for %%j in (%WINE_STR_PARMS%) do (
        call :GTRtest %%i %%j))

echo ------------ Testing if/exist ------------
mkdir subdir
echo something>subdir\bar
echo something else>foo
if exist foo (
   echo exist explicit works
) else (
   echo ERROR exist explicit broken
)
if exist bar (
   echo ERROR exist explicit unknown file broken
) else (
   echo exist explicit unknown file works
)
if exist subdir\bar (
   echo exist explicit in subdir works
) else (
   echo ERROR exist explicit in subdir broken
)
if exist fo* (
   echo exist simple wildcard works
) else (
   echo ERROR exist simple wildcard broken
)
if exist subdir\ba* (
   echo exist wildcard works
) else (
   echo ERROR exist wildcard broken
)
if not exist subdir\ba* (
   echo ERROR negate exist wildcard broken
) else (
   echo negate exist wildcard works
)
if exist idontexist\ba* (
   echo ERROR exist wildcard bad subdir broken
) else (
   echo exist wildcard bad subdir broken works
)
if exist subdir (
   echo exist subdir ok
) else (
   echo ERROR exist subdir not working
)
if exist subdir\. (
   echo exist subdir with . ok
) else (
   echo ERROR exist subdir with . not working
)
if exist subdir\ (
   echo exist subdir with \ ok
) else (
   echo ERROR exist subdir with \ not working
)
if exist "subdir\" (
   echo exist subdir with \ and quotes ok
) else (
   echo ERROR exist subdir with \ and quotes not working
)
del foo subdir\bar
rd subdir

echo ------ for numbers
if -1 LSS 1 (echo negative numbers handled)
if not -1 LSS -10 (echo negative numbers handled)
if not 9 LSS 010 (echo octal handled)
if not -010 LSS -8 (echo also in negative form)
if 4 LSS 0x5 (echo hexa handled)
if not -1 LSS -0x1A (echo also in negative form)
if 11 LSS 101 (echo 11 LSS 101)
set WINE_INT_PARMS=0 1 10 9
for %%i in (%WINE_INT_PARMS%) do (
    for %%j in (%WINE_INT_PARMS%) do (
        call :LSStest %%i %%j))
for %%i in (%WINE_INT_PARMS%) do (
    for %%j in (%WINE_INT_PARMS%) do (
        call :LEQtest %%i %%j))
for %%i in (%WINE_INT_PARMS%) do (
    for %%j in (%WINE_INT_PARMS%) do (
        call :EQUtest %%i %%j))
if 011 EQU 9 (echo octal ok)
if 0xA1 EQU 161 (echo hexa ok)
if 0xA1 EQU "161" (echo hexa should be recognized) else (echo string/hexa compare ok)
if "0xA1" EQU 161 (echo hexa should be recognized) else (echo string/hexa compare ok)
for %%i in (%WINE_INT_PARMS%) do (
    for %%j in (%WINE_INT_PARMS%) do (
        call :NEQtest %%i %%j))
for %%i in (%WINE_INT_PARMS%) do (
    for %%j in (%WINE_INT_PARMS%) do (
        call :GEQtest %%i %%j))
for %%i in (%WINE_INT_PARMS%) do (
    for %%j in (%WINE_INT_PARMS%) do (
        call :GTRtest %%i %%j))
echo ------ for numbers and stringified numbers
if not "1" EQU 1 (echo strings and integers not equal) else echo foo
if not 1 EQU "1" (echo strings and integers not equal) else echo foo
if '1' EQU 1 echo '1' EQU 1
if 1 EQU '1' echo 1 EQU '1'
if not "1" GEQ 1 (echo foo) else echo bar
if "10" GEQ "1" echo "10" GEQ "1"
if '1' GEQ 1 (echo '1' GEQ 1) else echo NT4
if 1 GEQ "1" echo 1 GEQ "1"
if "1" GEQ "1" echo "1" GEQ "1"
if '1' GEQ "1" echo '1' GEQ "1"
if "10" GEQ "1" echo "10" GEQ "1"
if not 1 GEQ '1' (echo non NT4) else echo 1 GEQ '1'
for %%i in ("1" '1') do call :GEQtest %%i '1'
if "10" GEQ '1' (echo "10" GEQ '1') else echo foo
if 1 GEQ "10" (echo 1 GEQ "10") else echo foo
if "1" GEQ "10" (echo 1 GEQ "10") else echo foo
if '1' GEQ "10" (echo '1' GEQ "10") else echo foo
if "10" GEQ "10" (echo "10" GEQ "10")
echo --- unconditional ampersand after if one line
if "0"=="0" echo 1 & echo 2 & echo 3 else echo 4
echo ---
echo x & if "0"=="1" echo 1 & echo 2
echo ---
echo x & if "0"=="1" echo 1 & echo 2 & echo 3
echo ---
echo x & if "0"=="1" (echo 1 & echo 2 & echo 3)
echo ---
echo x & if "0"=="1" echo 1 & echo 2 & echo 3 else echo 4
echo ---
goto :endIfCompOpsSubroutines

rem IF subroutines helpers
:LSStest
if %1 LSS %2 echo %1 LSS %2
goto :eof
:LEQtest
if %1 LEQ %2 echo %1 LEQ %2
goto :eof
:EQUtest
if %1 EQU %2 echo %1 EQU %2
goto :eof
:NEQtest
if %1 NEQ %2 echo %1 NEQ %2
goto :eof
:GEQtest
if %1 GEQ %2 echo %1 GEQ %2
goto :eof
:GTRtest
if %1 GTR %2 echo %1 GTR %2
goto :eof

:endIfCompOpsSubroutines
set WINE_STR_PARMS=
set WINE_INT_PARMS=

echo ------------ Testing for ------------
echo --- plain FOR
for %%i in (A B C) do echo %%i
for %%i in (A B C) do echo %%I
for %%i in (A B C) do echo %%j
for %%i in (A B C) do call :forTestFun1 %%i
for %%i in (1,4,1) do echo %%i
for %%i in (A, B,C) do echo %%i
for %%i in  (X) do echo %%i
for@tab@%%i in  (X2) do echo %%i
for %%i in@tab@(X3) do echo %%i
for %%i in (@tab@ foo@tab@) do echo %%i
for@tab@ %%i in@tab@(@tab@M) do echo %%i
for %%i@tab@in (X)@tab@do@tab@echo %%i
for@tab@ %%j in@tab@(@tab@M, N, O@tab@) do echo %%j
for %%i in (`echo A B`) do echo %%i
for %%i in ('echo A B') do echo %%i
for %%i in ("echo A B") do echo %%i
for %%i in ("A B" C) do echo %%i
goto :endForTestFun1
:forTestFun1
echo %1
goto :eof
:endForTestFun1
echo --- imbricated FORs
for %%i in (X) do (
    for %%j in (Y) do (
        echo %%i %%j))
for %%i in (X) do (
    for %%I in (Y) do (
        echo %%i %%I))
for %%i in (A B) do (
    for %%j in (C D) do (
        echo %%i %%j))
for %%i in (A B) do (
    for %%j in (C D) do (
        call :forTestFun2 %%i %%j ))
goto :endForTestFun2
:forTestFun2
echo %1 %2
goto :eof
:endForTestFun2
mkdir foobar & cd foobar
mkdir foo
mkdir bar
mkdir baz
mkdir pop
echo > bazbaz
echo --- basic wildcards
for %%i in (ba*) do echo %%i
echo --- wildcards in subdirs
echo something>pop\bar1
echo something>pop\bar2.txt
echo something>pop\bar3
for %%f in (pop\ba*) do ( call echo %%f )
rmdir /s/q pop
echo --- for /d
for /d %%i in (baz foo bar) do echo %%i 2>&1
rem Confirm we don't match files:
for /d %%i in (bazb*) do echo %%i 2>&1
for /d %%i in (bazb2*) do echo %%i 2>&1
rem Show we pass through non wildcards
for /d %%i in (PASSED) do echo %%i
for /d %%i in (xxx) do (
  echo %%i - Should be xxx
  echo Expected second line
)
rem Show we issue no messages on failures
for /d %%i in (FAILED?) do echo %%i 2>&1
for /d %%i in (FAILED?) do (
  echo %%i - Unexpected!
  echo FAILED Unexpected second line
)
for /d %%i in (FAILED*) do echo %%i 2>&1
for /d %%i in (FAILED*) do (
  echo %%i - Unexpected!
  echo FAILED Unexpected second line
)
rem FIXME can't test wildcard expansion here since it's listed in directory
rem order, and not in alphabetic order.
rem Proper testing would need a currently missing "sort" program implementation.
rem for /d %%i in (ba*) do echo %%i>> tmp
rem sort < tmp
rem del tmp
rem for /d %%i in (?a*) do echo %%i>> tmp
rem sort < tmp
rem del tmp
rem for /d %%i in (*) do echo %%i>> tmp
rem sort < tmp
rem del tmp
echo > baz\bazbaz
goto :TestForR

:SetExpected
del temp.bat 2>nul
call :WriteLine set WINE_found=N
for /l %%i in (1,1,%WINE_expectedresults%) do (
  call :WriteLine if "%%%%WINE_expectedresults.%%i%%%%"=="%%%%~1" set WINE_found=Y
  call :WriteLine if "%%%%WINE_found%%%%"=="Y" set WINE_expectedresults.%%i=
  call :WriteLine if "%%%%WINE_found%%%%"=="Y" goto :eof
)
call :WriteLine echo Got unexpected result: "%%%%~1"
goto :eof

:WriteLine
echo %*>> temp.bat
goto :EOF

:ValidateExpected
del temp.bat 2>nul
for /l %%i in (1,1,%WINE_expectedresults%) do (
  call :WriteLine if not "%%%%WINE_expectedresults.%%i%%%%"=="" echo Found missing result: "%%%%WINE_expectedresults.%%i%%%%"
)
call temp.bat
del temp.bat 2>nul
goto :eof

:TestForR
rem %CD% does not work on NT4 so use the following workaround
for /d %%i in (.) do set WINE_CURDIR=%%~dpnxi

echo --- for /R
echo Plain directory enumeration
set WINE_expectedresults=4
set WINE_expectedresults.1=%WINE_CURDIR%\.
set WINE_expectedresults.2=%WINE_CURDIR%\bar\.
set WINE_expectedresults.3=%WINE_CURDIR%\baz\.
set WINE_expectedresults.4=%WINE_CURDIR%\foo\.
call :SetExpected
for /R %%i in (.) do call temp.bat "%%i"
call :ValidateExpected

echo Plain directory enumeration from provided root
set WINE_expectedresults=4
set WINE_expectedresults.1=%WINE_CURDIR%\.
set WINE_expectedresults.2=%WINE_CURDIR%\bar\.
set WINE_expectedresults.3=%WINE_CURDIR%\baz\.
set WINE_expectedresults.4=%WINE_CURDIR%\foo\.
if "%CD%"=="" goto :SkipBrokenNT4
call :SetExpected
for /R "%WINE_CURDIR%" %%i in (.) do call temp.bat "%%i"
call :ValidateExpected
:SkipBrokenNT4

echo File enumeration
set WINE_expectedresults=2
set WINE_expectedresults.1=%WINE_CURDIR%\baz\bazbaz
set WINE_expectedresults.2=%WINE_CURDIR%\bazbaz
call :SetExpected
for /R %%i in (baz*) do call temp.bat "%%i"
call :ValidateExpected

echo File enumeration from provided root
set WINE_expectedresults=2
set WINE_expectedresults.1=%WINE_CURDIR%\baz\bazbaz
set WINE_expectedresults.2=%WINE_CURDIR%\bazbaz
call :SetExpected
for /R %%i in (baz*) do call temp.bat "%%i"
call :ValidateExpected

echo Mixed enumeration
set WINE_expectedresults=6
set WINE_expectedresults.1=%WINE_CURDIR%\.
set WINE_expectedresults.2=%WINE_CURDIR%\bar\.
set WINE_expectedresults.3=%WINE_CURDIR%\baz\.
set WINE_expectedresults.4=%WINE_CURDIR%\baz\bazbaz
set WINE_expectedresults.5=%WINE_CURDIR%\bazbaz
set WINE_expectedresults.6=%WINE_CURDIR%\foo\.
call :SetExpected
for /R %%i in (. baz*) do call temp.bat "%%i"
call :ValidateExpected

echo Mixed enumeration from provided root
set WINE_expectedresults=6
set WINE_expectedresults.1=%WINE_CURDIR%\.
set WINE_expectedresults.2=%WINE_CURDIR%\bar\.
set WINE_expectedresults.3=%WINE_CURDIR%\baz\.
set WINE_expectedresults.4=%WINE_CURDIR%\baz\bazbaz
set WINE_expectedresults.5=%WINE_CURDIR%\bazbaz
set WINE_expectedresults.6=%WINE_CURDIR%\foo\.
call :SetExpected
for /R %%i in (. baz*) do call temp.bat "%%i"
call :ValidateExpected

echo With duplicates enumeration
set WINE_expectedresults=12
set WINE_expectedresults.1=%WINE_CURDIR%\bar\bazbaz
set WINE_expectedresults.2=%WINE_CURDIR%\bar\fred
set WINE_expectedresults.3=%WINE_CURDIR%\baz\bazbaz
set WINE_expectedresults.4=%WINE_CURDIR%\baz\bazbaz
set WINE_expectedresults.5=%WINE_CURDIR%\baz\bazbaz
set WINE_expectedresults.6=%WINE_CURDIR%\baz\fred
set WINE_expectedresults.7=%WINE_CURDIR%\bazbaz
set WINE_expectedresults.8=%WINE_CURDIR%\bazbaz
set WINE_expectedresults.9=%WINE_CURDIR%\bazbaz
set WINE_expectedresults.10=%WINE_CURDIR%\foo\bazbaz
set WINE_expectedresults.11=%WINE_CURDIR%\foo\fred
set WINE_expectedresults.12=%WINE_CURDIR%\fred
call :SetExpected
for /R %%i in (baz* bazbaz fred ba*) do call temp.bat "%%i"
call :ValidateExpected

echo Strip missing wildcards, keep unwildcarded names
set WINE_expectedresults=6
set WINE_expectedresults.1=%WINE_CURDIR%\bar\jim
set WINE_expectedresults.2=%WINE_CURDIR%\baz\bazbaz
set WINE_expectedresults.3=%WINE_CURDIR%\baz\jim
set WINE_expectedresults.4=%WINE_CURDIR%\bazbaz
set WINE_expectedresults.5=%WINE_CURDIR%\foo\jim
set WINE_expectedresults.6=%WINE_CURDIR%\jim
call :SetExpected
for /R %%i in (baz* fred* jim) do call temp.bat "%%i"
call :ValidateExpected

echo for /R passed
echo --- Complex wildcards unix and windows slash
cd ..
echo Windows slashes, valid path
for %%f in (foobar\baz\bazbaz) do echo ASIS: %%f
for %%f in (foobar\baz\*) do echo WC  : %%f
echo Windows slashes, invalid path
for %%f in (foobar\jim\bazbaz) do echo ASIS: %%f
for %%f in (foobar\jim\*) do echo WC  : %%f
echo Unix slashes, valid path
for %%f in (foobar/baz/bazbaz) do echo ASIS: %%f
for %%f in (foobar/baz/*) do echo WC  : %%f
echo Unix slashes, invalid path
for %%f in (foobar/jim/bazbaz) do echo ASIS: %%f
for %%f in (foobar/jim/*) do echo WC  : %%f
echo Done
rd /s/Q foobar
echo --- for /L
rem Some cases loop forever writing 0s, like e.g. (1,0,1), (1,a,3) or (a,b,c); those can't be tested here
for /L %%i in (1,2,0) do echo %%i
for@tab@/L %%i in (1,2,0) do echo %%i
for /L %%i in (1,2,6) do echo %%i
for /l %%i in (1 ,2,6) do echo %%i
for /L %%i in (a,2,3) do echo %%i
for /L %%i in (1,2,-1) do echo %%i
for /L %%i in (-4,-1,-1) do echo %%i
for /L %%i in (1,-2,-2) do echo %%i
for /L %%i in (1,2,a) do echo %%i
echo ErrorLevel %ErrorLevel%
for /L %%i in (1,a,b) do echo %%i
echo ErrorLevel %ErrorLevel%
rem Test boundaries
for /l %%i in (1,1,4) do echo %%i
for /l %%i in (1,2,4) do echo %%i
for /l %%i in (4,-1,1) do echo %%i
for /l %%i in (4,-2,1) do echo %%i
for /l %%i in (1,-1,4) do echo %%i
for /l %%i in (4,1,1) do echo %%i
for /L %%i in (a,2,b) do echo %%i
for /L %%i in (1,1,1) do echo %%i
for /L %%i in (1,-2,-1) do echo %%i
for /L %%i in (-1,-1,-1) do echo %%i
for /L %%i in (1,2, 3) do echo %%i
rem Test zero iteration skips the body of the for
for /L %%i in (2,2,1) do (
  echo %%i
  echo FAILED
)
echo --- rems inside for loops
for /f %%i IN ("hello") DO (
   REM foo|echo ERROR unexpected execution 1
   @REM foo|echo ERROR unexpected execution 2
   @     REM foo|echo ERROR unexpected execution 3
)
echo --- ifs inside for loops
for %%i in (test) do (
    echo a1
    if 1==1 (
        echo b1
    ) else (
        echo c1
    )
    echo d1
)
for %%i in (test) do (
    echo a2
    if 1==1 (
        echo b2
    ) else echo c2
    echo d2
)
for %%i in (test) do (
    echo a3
    if 1==0 (
        echo b3
    ) else echo c3
    echo d3
)
for %%i in (test) do (
    echo a4
    if 1==0 (
        echo b4
    ) else (
        echo c4
    )
    echo d4
)
echo --- set /a
goto :testseta

Rem Ideally for /f can be used rather than building a command to execute
rem but that does not work on NT4
:checkenvvars
if "%1"=="" goto :eof
call :executecmd set wine_result=%%%1%%
if "%wine_result%"=="%2" (
  echo %1 correctly %2
) else echo ERROR: %1 incorrectly %wine_result% [%2]
set %1=
shift
shift
rem shift
goto :checkenvvars
:executecmd
%*
goto :eof

:testseta
rem No output when using "set expr" syntax, unless in interactive mode
rem Need to use "set envvar=expr" to use in a batch script
echo ------ individual operations
set WINE_foo=0
set /a WINE_foo=1 +2 & call :checkenvvars WINE_foo 3
set /a WINE_foo=1 +-2 & call :checkenvvars WINE_foo -1
set /a WINE_foo=1 --2 & call :checkenvvars WINE_foo 3
set /a WINE_foo=2* 3 & call :checkenvvars WINE_foo 6
set /a WINE_foo=-2* -5 & call :checkenvvars WINE_foo 10
set /a WINE_foo=12/3 & call :checkenvvars WINE_foo 4
set /a WINE_foo=13/3 & call :checkenvvars WINE_foo 4
set /a WINE_foo=-13/3 & call :checkenvvars WINE_foo -4
rem FIXME Divide by zero should return an error, but error messages cannot be tested with current infrastructure
set /a WINE_foo=5 %% 5 & call :checkenvvars WINE_foo 0
set /a WINE_foo=5 %% 3 & call :checkenvvars WINE_foo 2
set /a WINE_foo=5 %% -3 & call :checkenvvars WINE_foo 2
set /a WINE_foo=-5 %% -3 & call :checkenvvars WINE_foo -2
set /a WINE_foo=1 ^<^< 0 & call :checkenvvars WINE_foo 1
set /a WINE_foo=1 ^<^< 2 & call :checkenvvars WINE_foo 4
set /a WINE_foo=1 ^<^< -2 & call :checkenvvars WINE_foo 0
set /a WINE_foo=-1 ^<^< -2 & call :checkenvvars WINE_foo 0
set /a WINE_foo=-1 ^<^< 2 & call :checkenvvars WINE_foo -4
set /a WINE_foo=9 ^>^> 0 & call :checkenvvars WINE_foo 9
set /a WINE_foo=9 ^>^> 2 & call :checkenvvars WINE_foo 2
set /a WINE_foo=9 ^>^> -2 & call :checkenvvars WINE_foo 0
set /a WINE_foo=-9 ^>^> -2 & call :checkenvvars WINE_foo -1
set /a WINE_foo=-9 ^>^> 2 & call :checkenvvars WINE_foo -3
set /a WINE_foo=5 ^& 0 & call :checkenvvars WINE_foo 0
set /a WINE_foo=5 ^& 1 & call :checkenvvars WINE_foo 1
set /a WINE_foo=5 ^& 3 & call :checkenvvars WINE_foo 1
set /a WINE_foo=5 ^& 4 & call :checkenvvars WINE_foo 4
set /a WINE_foo=5 ^& 1 & call :checkenvvars WINE_foo 1
set /a WINE_foo=5 ^| 0 & call :checkenvvars WINE_foo 5
set /a WINE_foo=5 ^| 1 & call :checkenvvars WINE_foo 5
set /a WINE_foo=5 ^| 3 & call :checkenvvars WINE_foo 7
set /a WINE_foo=5 ^| 4 & call :checkenvvars WINE_foo 5
set /a WINE_foo=5 ^| 1 & call :checkenvvars WINE_foo 5
set /a WINE_foo=5 ^^ 0 & call :checkenvvars WINE_foo 5
set /a WINE_foo=5 ^^ 1 & call :checkenvvars WINE_foo 4
set /a WINE_foo=5 ^^ 3 & call :checkenvvars WINE_foo 6
set /a WINE_foo=5 ^^ 4 & call :checkenvvars WINE_foo 1
set /a WINE_foo=5 ^^ 1 & call :checkenvvars WINE_foo 4
echo ------ precedence and grouping
set /a WINE_foo=4 + 2*3 & call :checkenvvars WINE_foo 10
set /a WINE_foo=(4+2)*3 & call :checkenvvars WINE_foo 18
set /a WINE_foo=4 * 3/5 & call :checkenvvars WINE_foo 2
set /a WINE_foo=(4 * 3)/5 & call :checkenvvars WINE_foo 2
set /a WINE_foo=4 * 5 %% 4 & call :checkenvvars WINE_foo 0
set /a WINE_foo=4 * (5 %% 4) & call :checkenvvars WINE_foo 4
set /a WINE_foo=3 %% (5 + 8 %% 3 ^^ 2) & call :checkenvvars WINE_foo 3
set /a WINE_foo=3 %% (5 + 8 %% 3 ^^ -2) & call :checkenvvars WINE_foo 3
echo ------ octal and hexadecimal
set /a WINE_foo=0xf + 3 & call :checkenvvars WINE_foo 18
set /a WINE_foo=0xF + 3 & call :checkenvvars WINE_foo 18
set /a WINE_foo=015 + 2 & call :checkenvvars WINE_foo 15
set /a WINE_foo=3, 8+3,0 & call :checkenvvars WINE_foo 3
echo ------ variables
set /a WINE_foo=WINE_bar=3, WINE_bar+1 & call :checkenvvars WINE_foo 3 WINE_bar 3
set /a WINE_foo=WINE_bar=3, WINE_bar+=1 & call :checkenvvars WINE_foo 3 WINE_bar 4
set /a WINE_foo=WINE_bar=3, WINE_baz=1, WINE_baz+=WINE_bar, WINE_baz & call :checkenvvars WINE_foo 3 WINE_bar 3 WINE_baz 4
set WINE_bar=3
set /a WINE_foo=WINE_bar*= WINE_bar & call :checkenvvars WINE_foo 9 WINE_bar 9
set /a WINE_foo=WINE_whateverNonExistingVar & call :checkenvvars WINE_foo 0
set WINE_bar=4
set /a WINE_foo=WINE_whateverNonExistingVar + WINE_bar & call :checkenvvars WINE_foo 4 WINE_bar 4
set WINE_bar=4
set /a WINE_foo=WINE_bar -= WINE_bar + 7 & call :checkenvvars WINE_foo -7 WINE_bar -7
set WINE_bar=-7
set /a WINE_foo=WINE_bar /= 3 + 2 & call :checkenvvars WINE_foo -1 WINE_bar -1
set /a WINE_foo=WINE_bar=5, WINE_bar %%=2 & call :checkenvvars WINE_foo 5 WINE_bar 1
set WINE_bar=1
set /a WINE_foo=WINE_bar ^<^<= 2 & call :checkenvvars WINE_foo 4 WINE_bar 4
set WINE_bar=4
set /a WINE_foo=WINE_bar ^>^>= 2 & call :checkenvvars WINE_foo 1 WINE_bar 1
set WINE_bar=1
set /a WINE_foo=WINE_bar ^&= 2 & call :checkenvvars WINE_foo 0 WINE_bar 0
set /a WINE_foo=WINE_bar=5, WINE_bar ^|= 2 & call :checkenvvars WINE_foo 5 WINE_bar 7
set /a WINE_foo=WINE_bar=5, WINE_bar ^^= 2 & call :checkenvvars WINE_foo 5 WINE_bar 7
set WINE_baz=4
set /a WINE_foo=WINE_bar=19, WINE_bar %%= 4 + (WINE_baz %%= 7) & call :checkenvvars WINE_foo 19 WINE_bar 3 WINE_baz 4
echo --- quotes
set /a WINE_foo=1
call :checkenvvars WINE_foo 1
set /a "WINE_foo=1"
call :checkenvvars WINE_foo 1
set /a WINE_foo=1,WINE_bar=2
call :checkenvvars WINE_foo 1 WINE_bar 2
set /a "WINE_foo=1,WINE_bar=2"
call :checkenvvars WINE_foo 1 WINE_bar 2
set /a "WINE_foo=1","WINE_bar=2"
call :checkenvvars WINE_foo 1 WINE_bar 2
set /a ""WINE_foo=1","WINE_bar=2""
call :checkenvvars WINE_foo 1 WINE_bar 2
set /a WINE_foo=1,WINE_bar=2,WINE_baz=3
call :checkenvvars WINE_foo 1 WINE_bar 2 WINE_baz 3
set /a "WINE_foo=1,WINE_bar=2,WINE_baz=3"
call :checkenvvars WINE_foo 1 WINE_bar 2 WINE_baz 3
set /a "WINE_foo=1","WINE_bar=2","WINE_baz=3"
call :checkenvvars WINE_foo 1 WINE_bar 2 WINE_baz 3
set /a ""WINE_foo=1","WINE_bar=2","WINE_baz=3""
call :checkenvvars WINE_foo 1 WINE_bar 2 WINE_baz 3
set /a ""WINE_foo=1","WINE_bar=2"","WINE_baz=3"
call :checkenvvars WINE_foo 1 WINE_bar 2 WINE_baz 3
set /a """"""WINE_foo=1""""""
call :checkenvvars WINE_foo 1
set /a """"""WINE_foo=1","WINE_bar=5""","WINE_baz=2""
call :checkenvvars WINE_foo 1 WINE_bar 5 WINE_baz 2
set /a WINE_foo="3"+"4"+"5+6"
call :checkenvvars WINE_foo 18
set WINE_foo=3
set /a WINE_bar="WINE_""foo"+4
call :checkenvvars WINE_foo 3 WINE_bar 7
echo --- whitespace are ignored between double char operators
set WINE_foo=4
set WINE_bar=5
set /a     WINE_foo   +    = 6
set /a     WINE_bar     *    = WINE_foo
call :checkenvvars WINE_foo 10 WINE_bar 50
set WINE_foo=4
set WINE_bar=5
set /a     WINE_foo   +    = "6  < < 7"
set /a     WINE_bar     *    = WINE_foo  +  WINE_foo
call :checkenvvars WINE_foo 772 WINE_bar 7720
set /a     WINE_foo=6 7
set /a     WINE_ var1=8
set WINE_foo=
echo --- invalid operator sequence
set WINE_foo=4
set /a =4
set /a *=4
set /a ^>=4"
set /a ^<=4"
set /a WINE_foo^>^<=4
echo %WINE_foo%
set /a WINE_foo^>^>^>=4
echo %WINE_foo%
echo ----- negative prefix
set /a WINE_foo=-1
call :checkenvvars WINE_foo -1
set /a WINE_foo=--1
call :checkenvvars WINE_foo 1
set /a WINE_foo=3--3
call :checkenvvars WINE_foo 6
set /a WINE_foo=3---3
call :checkenvvars WINE_foo 0
set /a WINE_foo=3----3
call :checkenvvars WINE_foo 6
set /a WINE_foo=-~1
call :checkenvvars WINE_foo 2
set /a WINE_foo=~-1
call :checkenvvars WINE_foo 0
set /a WINE_foo=3+-~1
call :checkenvvars WINE_foo 5
set /a WINE_foo=3+~-1
call :checkenvvars WINE_foo 3
echo ----- assignment tests involving the end destination
set WINE_foo=3
set /a WINE_foo+=3+(WINE_foo=4)
call :checkenvvars WINE_foo 11
set WINE_foo=2
set /a WINE_bar=3+(WINE_foo=6)
call :checkenvvars WINE_foo 6 WINE_bar 9
set WINE_foo=2
set /a WINE_bar=3+(WINE_foo=6,WINE_baz=7)
call :checkenvvars WINE_foo 6 WINE_bar 10 WINE_baz 7
set WINE_foo=2
set /a WINE_bar=WINE_foo=7
call :checkenvvars WINE_foo 7 WINE_bar 7
echo ----- equal precedence on stack
rem Unary - don't reduce if precedence is equal
set /a WINE_foo=!!1
call :checkenvvars WINE_foo 1
set /a WINE_foo=!!0
call :checkenvvars WINE_foo 0
set /a WINE_foo=~~1
call :checkenvvars WINE_foo 1
set /a WINE_foo=~~0
call :checkenvvars WINE_foo 0
set /a WINE_foo=--1
call :checkenvvars WINE_foo 1
set /a WINE_foo=+-1
call :checkenvvars WINE_foo -1
set /a WINE_foo=-+1
call :checkenvvars WINE_foo -1
set /a WINE_foo=++1
call :checkenvvars WINE_foo 1
set /a WINE_foo=!~1
call :checkenvvars WINE_foo 0
set /a WINE_foo=~!1
call :checkenvvars WINE_foo -1
set /a WINE_foo=!-1
call :checkenvvars WINE_foo 0
set /a WINE_foo=-!1
call :checkenvvars WINE_foo 0
set /a WINE_foo=!-0
call :checkenvvars WINE_foo 1
set /a WINE_foo=-!0
call :checkenvvars WINE_foo -1
rem Aritmatic - Reduce if precedence is equal
set /a WINE_foo=10*5/2
call :checkenvvars WINE_foo 25
set /a WINE_foo=5/2*10
call :checkenvvars WINE_foo 20
set /a WINE_foo=10/5/2
call :checkenvvars WINE_foo 1
set /a WINE_foo=5%%2*4
call :checkenvvars WINE_foo 4
set /a WINE_foo=10-5+2
call :checkenvvars WINE_foo 7
set /a WINE_foo=1^<^<4^>^>1
call :checkenvvars WINE_foo 8
rem Assignment - don't reduce if precedence is equal
set /a WINE_foo=5
set /a WINE_bar=WINE_foo=6
call :checkenvvars WINE_foo 6 WINE_bar 6

echo --- for /F
mkdir foobar & cd foobar
echo ------ string argument
rem NT4 does not support usebackq
for /F %%i in ("a b c") do echo %%i
for /F %%i in (  "a b c"    ) do echo X%%iX
for /f usebackq %%i in ('a b c') do echo %%i>output_file
if not exist output_file (echo no output) else (type output_file & del output_file)
for /f usebackq %%i in (   'a b c'   ) do echo X%%iX>output_file
if not exist output_file (echo no output) else (type output_file & del output_file)
for /f %%i in ("a ") do echo %%i
for /f usebackq %%i in ('a ') do echo %%i>output_file
if not exist output_file (echo no output) else (type output_file & del output_file)
for /f %%i in ("a") do echo %%i
for /f usebackq %%i in ('a') do echo %%i>output_file
if not exist output_file (echo no output) else (type output_file & del output_file)
fOr /f %%i in (" a") do echo %%i
for /f usebackq %%i in (' a') do echo %%i>output_file
if not exist output_file (echo no output) else (type output_file & del output_file)
for /f %%i in (" a ") do echo %%i
for /f usebackq %%i in (' a ') do echo %%i>output_file
if not exist output_file (echo no output) else (type output_file & del output_file)
echo ------ fileset argument
echo --------- basic blank handling
echo a b c>foo
for /f %%i in (foo) do echo %%i
echo a >foo
for /f %%i in (foo) do echo %%i
echo a>foo
for /f %%i in (foo) do echo %%i
echo  a>foo
for /f %%i in (foo) do echo %%i
echo  a >foo
for /f %%i in (foo) do echo %%i
echo. > foo
for /f %%i in (foo) do echo %%i
echo. >> foo
echo b > foo
for /f %%i in (foo) do echo %%i
echo --------- multi-line with empty lines
echo a Z f> foo
echo. >> foo
echo.>> foo
echo b bC>> foo
echo c>> foo
echo. >> foo
for /f %%b in (foo) do echo %%b
echo --------- multiple files
echo q w > bar
echo.>> bar
echo kkk>>bar
for /f %%k in (foo bar) do echo %%k
for /f %%k in (bar foo) do echo %%k
echo ------ command argument
rem Not implemented on NT4, need to skip it as no way to get output otherwise
if "%CD%"=="" goto :SkipFORFcmdNT4
for /f %%i in ('echo.Passed1') do echo %%i
for /f "usebackq" %%i in (`echo.Passed2`) do echo %%i
for /f usebackq %%i in (`echo.Passed3`) do echo %%i
for /f "usebackq" %%i in (`"c:\windows\system32\cmd.exe" /C echo Passed4`) do echo %%i
for /f "usebackq" %%i in (`""c:\windows\system32\cmd.exe" /C echo Passed5"`) do echo %%i
for /f %%i in (  'echo.Passed6'  ) do echo %%i
for /f "usebackq" %%i in (   `echo.Passed7` ) do echo %%i
goto :ContinueFORF
:SkipFORFcmdNT4
for /l %%i in (1,1,7) do echo Missing functionality - Broken%%i
:ContinueFORF
rem FIXME: Rest not testable right now in wine: not implemented and would need
rem preliminary grep-like program implementation (e.g. like findstr or fc) even
rem for a simple todo_wine test
rem (for /f "usebackq" %%i in (`echo z a b`) do echo %%i) || echo not supported
rem (for /f usebackq %%i in (`echo z a b`) do echo %%i) || echo not supported
echo ------ eol option
if "%CD%"=="" goto :SkipFORFeolNT4
echo Line one>foo
echo and Line two>>foo
echo Line three>>foo
for /f "eol=L" %%i in (foo) do echo %%i
for /f "eol=a" %%i in (foo) do echo %%i
del foo
goto :ContinueFORFeol
:SkipFORFeolNT4
for /l %%i in (1,1,3) do echo Broken NT4 functionality%%i
:ContinueFORFeol
for /f "eol=@" %%i in ("    ad") do echo %%i
for /f "eol=@" %%i in (" z@y") do echo %%i
for /f "eol=|" %%i in ("a|d") do echo %%i
for /f "eol=@" %%i in ("@y") do echo %%i > output_file
if not exist output_file (echo no output) else (del output_file)
for /f "eol==" %%i in ("=y") do echo %%i > output_file
if not exist output_file (echo no output) else (del output_file)
echo ------ delims option
for /f "delims=|" %%i in ("a|d") do echo %%i
for /f "delims=|" %%i in ("a |d") do echo %%i
for /f "delims=|" %%i in ("a d|") do echo %%i
for /f "delims=| " %%i in ("a d|") do echo %%i
for /f "delims==" %%i in ("C r=d|") do echo %%i
for /f "delims=" %%i in ("foo bar baz") do echo %%i
for /f "delims=" %%i in ("c:\foo bar baz\..") do echo %%~fi
echo ------ skip option
echo a > foo
echo b >> foo
echo c >> foo
for /f "skip=2" %%i in (foo) do echo %%i
for /f "skip=3" %%i in (foo) do echo %%i > output_file
if not exist output_file (echo no output) else (del output_file)
for /f "skip=4" %%i in (foo) do echo %%i > output_file
if not exist output_file (echo no output) else (del output_file)
for /f "skip=02" %%i in (foo) do echo %%i
for /f "skip=0x2" %%i in (foo) do echo %%i
for /f "skip=1" %%i in ("skipme") do echo %%i > output_file
if not exist output_file (echo no output) else (del output_file)
echo ------ tokens= option
rem Basic
for /f %%i in ("a b c d e f g") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m o=%%o
for /f "tokens=2" %%i in ("a b c d e f g") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m o=%%o
for /f "tokens=1,3,5-7" %%i in ("a b c d e f g") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m o=%%o
rem Show * means the rest
for /f "tokens=1,5*" %%i in ("a b c d e f g") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m o=%%o
for /f "tokens=6,9*" %%i in ("a b c d e f g h i j k l m n o p q r s t u v w x y z") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m o=%%o
rem Show * means the rest (not tokenized and rebuilt)
for /f "tokens=6,9*" %%i in ("a b c d e f g h i j k l m  n;;==  o p q r s t u v w x y z") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m o=%%o
rem Order is irrelevant
for /f "tokens=1,2,3*" %%i in ("a b c d e f g") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m n=%%n o=%%o
for /f "tokens=3,2,1*" %%i in ("a b c d e f g") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m n=%%n o=%%o
rem Duplicates are ignored
for /f "tokens=1,2,1*" %%i in ("a b c d e f g") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m n=%%n o=%%o
rem Large tokens are allowed
for /f "tokens=25,1,5*" %%i in ("a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m n=%%n o=%%o
rem Show tokens blanked in advance regardless of uniqueness of requested tokens
for /f "tokens=1,1,1,2*" %%i in ("a b c d e f g") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m n=%%n o=%%o
for /f "tokens=1-2,1-2,1-2" %%i in ("a b c d e f g") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m n=%%n o=%%o
rem Show No wrapping from z to A BUT wrapping sort of occurs Z to a occurs
for /f "tokens=1-20" %%u in ("a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z") do echo u=%%u v=%%v w=%%w x=%%x y=%%y z=%%z A=%%A a=%%a
for /f "tokens=1-20" %%U in ("a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z") do echo U=%%U V=%%V W=%%W X=%%X Y=%%Y Z=%%Z A=%%A a=%%a
rem Show negative ranges have no effect
for /f "tokens=1-3,5" %%i in ("a b c d e f g") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m o=%%o
for /f "tokens=3-1,5" %%i in ("a b c d e f g") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m o=%%o
rem Show duplicates stop * from working
for /f "tokens=1,2,3*" %%i in ("a b c d e f g") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m n=%%n o=%%o
for /f "tokens=1,1,3*" %%i in ("a b c d e f g") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m n=%%n o=%%o
for /f "tokens=2,2,3*" %%i in ("a b c d e f g") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m n=%%n o=%%o
for /f "tokens=3,2,3*" %%i in ("a b c d e f g") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m n=%%n o=%%o
rem Special case tokens=* or tokens=n,*
echo 3.14>testfile
FOR /F "tokens=*"  %%A IN (testfile) DO @echo 1:%%A,%%B
FOR /F "tokens=1*" %%A IN (testfile) DO @echo 2:%%A,%%B
FOR /F "tokens=2*" %%A IN (testfile) DO @echo 3:%%A,%%B
FOR /F "tokens=1,* delims=." %%A IN (testfile) DO @echo 4:%%A,%%B
del testfile
cd ..
rd /s/q foobar
echo ------ parameter splitting
echo forFParameterSplittingFunc "myparam1=myvalue1 myparam2=myparam2" mytest> foo
for /f "tokens=1 delims=;" %%i in (foo) do (call :%%i)
del foo
for /f "tokens=1 delims=;" %%i in ("forFParameterSplittingFunc "myparam1^=myvalue1 myparam2^=myparam2" mytest") do (call :%%i)
goto :forFParameterSplittingEnd
:forFParameterSplittingFunc
echo %~0 %~1 %~2 %~3 %~4 %~5
goto :eof
:forFParameterSplittingEnd
echo 3.14>testfile
FOR /F "delims=. tokens=*"  %%A IN (testfile) DO @echo 4:%%A,%%B
FOR /F "delims=. tokens=1*" %%A IN (testfile) DO @echo 5:%%A,%%B
FOR /F "delims=. tokens=2*" %%A IN (testfile) DO @echo 6:%%A,%%B
FOR /F "delims=. tokens=3*" %%A IN (testfile) DO @echo 7:%%A,%%B
del testfile

echo ------------ Testing del ------------
echo abc > file
echo deleting 'file'
del file
if errorlevel 0 (
    echo errorlevel is 0, good
) else (
    echo unexpected errorlevel, got %errorlevel%
)
if not exist file (
    echo successfully deleted 'file'
) else (
    echo error deleting 'file'
)
echo attempting to delete 'file', even though it is not present
del file
if errorlevel 0 (
    echo errorlevel is 0, good
) else (
    echo unexpected errorlevel, got %errorlevel%
)

echo ------------ Testing del /a ------------
del /f/q *.test > nul
echo r > r.test
attrib +r r.test
echo not-r > not-r.test

if not exist not-r.test echo not-r.test not found before delete, bad
del /a:-r *.test
if not exist not-r.test echo not-r.test not found after delete, good

if not exist r.test echo r.test not found before delete, bad
if exist r.test echo r.test found before delete, good
del /a:r *.test
if not exist r.test echo r.test not found after delete, good
if exist r.test echo r.test found after delete, bad

echo ------------ Testing del /q ------------
mkdir del_q_dir
cd del_q_dir
echo abc > file1
echo abc > file2.dat
rem If /q doesn't work, cmd will prompt and the test case should hang
del /q * > nul
for %%a in (1 2.dat) do if exist file%%a echo del /q * failed on file%%a
for %%a in (1 2.dat) do if not exist file%%a echo del /q * succeeded on file%%a
cd ..
rmdir del_q_dir

echo ------------ Testing del /s ------------
mkdir "foo bar"
cd "foo bar"
mkdir "foo:"
echo hi > file1.dat
echo there > file2.dat
echo bub > file3.dat
echo bye > "file with spaces.dat"
cd ..
del /s file1.dat > nul
del file2.dat /s > nul
del "file3.dat" /s > nul
del "file with spaces.dat" /s > nul
cd "foo bar"
for %%f in (1 2 3) do if exist file%%f.dat echo Del /s failed on file%%f
for %%f in (1 2 3) do if exist file%%f.dat del file%%f.dat
if exist "file with spaces.dat" echo Del /s failed on "file with spaces.dat"
if exist "file with spaces.dat" del "file with spaces.dat"
rmdir "foo:"
cd ..
rmdir "foo bar"

echo ------------ Testing rename ------------
mkdir foobar & cd foobar
echo --- ren and rename are synonymous
echo > foo
rename foo bar
if exist foo echo foo should be renamed!
if exist bar echo foo renamed to bar
ren bar foo
if exist bar echo bar should be renamed!
if exist foo echo bar renamed to foo
echo --- name collision
echo foo>foo
echo bar>bar
ren foo bar 2> nul
type foo
type bar
rem no-op
ren foo foo
mkdir baz
ren foo baz\abc
echo --- rename read-only files
echo > file1
attrib +r file1
ren file1 file2
if not exist file1 (
    if exist file2 (
        echo read-only file renamed
    )
) else (
    echo read-only file not renamed!
)
echo --- rename directories
mkdir rep1
ren rep1 rep2
if not exist rep1 (
    if exist rep2 (
        echo dir renamed
    )
)
attrib +r rep2
ren rep2 rep1
if not exist rep2 (
    if exist rep1 (
        echo read-only dir renamed
    )
)
echo --- rename in other directory
if not exist baz\abc (
    echo rename impossible in other directory
    if exist foo echo original file still present
) else (
    echo shouldn't rename in other directory!
    if not exist foo echo original file not present anymore
)
cd .. & rd /s/q foobar

echo ------------ Testing move ------------
mkdir foobar & cd foobar
echo --- file move
echo >foo
move foo bar > nul 2>&1
if not exist foo (
    if exist bar (
        echo file move succeeded
    )
)
echo bar>bar
echo baz> baz
move /Y bar baz > nul 2>&1
if not exist bar (
    if exist baz (
        echo file move with overwrite succeeded
    )
) else (
    echo file overwrite impossible!
    del bar
)
type baz

attrib +r baz
move baz bazro > nul 2>&1
if not exist baz (
    if exist bazro (
        echo read-only files are moveable
        move bazro baz > nul 2>&1
    )
) else (
    echo read-only file not moved!
)
attrib -r baz
mkdir rep
move baz rep > nul 2>&1
if not exist baz (
    if exist rep\baz (
        echo file moved in subdirectory
    )
)
call :setError 0
move rep\baz . > nul 2>&1
move /Y baz baz > nul 2>&1
if errorlevel 1 (
    echo moving a file to itself should be a no-op!
) else (
    echo moving a file to itself is a no-op
)
echo ErrorLevel: %ErrorLevel%
call :setError 0
del baz
echo --- directory move
mkdir foo\bar
mkdir baz
echo baz2>baz\baz2
move baz foo\bar > nul 2>&1
if not exist baz (
    if exist foo\bar\baz\baz2 (
        echo simple directory move succeeded
    )
)
call :setError 0
mkdir baz
move baz baz > nul 2>&1
echo moving a directory to itself gives error; errlevel %ErrorLevel%
echo ------ dir in dir move
rd /s/q foo
mkdir foo bar
echo foo2>foo\foo2
echo bar2>bar\bar2
move foo bar > nul 2>&1
if not exist foo (
    if exist bar (
        dir /b /ad bar
        dir /b /a-d bar
        dir /b bar\foo
    )
)
cd .. & rd /s/q foobar

echo ------------ Testing mkdir ------------
call :setError 0
echo --- md and mkdir are synonymous
mkdir foobar
echo %ErrorLevel%
rmdir foobar
md foobar
echo %ErrorLevel%
rmdir foobar
echo --- creating an already existing directory/file must fail
mkdir foobar
md foobar
echo %ErrorLevel%
rmdir foobar
echo > foobar
mkdir foobar
echo %ErrorLevel%
del foobar
echo --- multilevel path creation
mkdir foo
echo %ErrorLevel%
mkdir foo\bar\baz
echo %ErrorLevel%
cd foo
echo %ErrorLevel%
cd bar
echo %ErrorLevel%
cd baz
echo %ErrorLevel%
echo > ..\..\bar2
mkdir ..\..\..\foo\bar2
echo %ErrorLevel%
del ..\..\bar2
mkdir ..\..\..\foo\bar2
echo %ErrorLevel%
rmdir ..\..\..\foo\bar2
cd ..
rmdir baz
cd ..
rmdir bar
cd ..
rmdir foo
echo %ErrorLevel%
echo --- trailing backslashes
mkdir foo\\\\
echo %ErrorLevel%
if exist foo (rmdir foo & echo dir created
) else ( echo dir not created )
echo %ErrorLevel%
echo --- invalid chars
mkdir ?
echo mkdir ? gives errorlevel %ErrorLevel%
call :setError 0
mkdir ?\foo
echo mkdir ?\foo gives errorlevel %ErrorLevel%
call :setError 0
mkdir foo\?
echo mkdir foo\? gives errorlevel %ErrorLevel%
if exist foo (rmdir foo & echo ok, foo created
) else ( echo foo not created )
call :setError 0
mkdir foo\bar\?
echo mkdir foo\bar\? gives errorlevel %ErrorLevel%
call :setError 0
if not exist foo (
    echo bad, foo not created
) else (
    cd foo
    if exist bar (
        echo ok, foo\bar created
        rmdir bar
    )
    cd ..
    rmdir foo
)
echo --- multiple directories at once
mkdir foobaz & cd foobaz
mkdir foo bar\baz foobar "bazbaz" .\"zabzab"
if exist foo (echo foo created) else echo foo not created!
if exist bar (echo bar created) else echo bar not created!
if exist foobar (echo foobar created) else echo foobar not created!
if exist bar\baz (echo bar\baz created) else echo bar\baz not created!
if exist bazbaz (echo bazbaz created) else echo bazbaz not created!
if exist zabzab (echo zabzab created) else echo zabzab not created!
cd .. & rd /s/q foobaz
call :setError 0
mkdir foo\*
echo mkdir foo\* errorlevel %ErrorLevel%
if exist foo (rmdir foo & echo ok, foo created
) else ( echo bad, foo not created )

echo ------------ Testing rmdir ------------
call :setError 0
rem rd and rmdir are synonymous
mkdir foobar
rmdir foobar
echo %ErrorLevel%
if not exist foobar echo dir removed
mkdir foobar
rd foobar
echo %ErrorLevel%
if not exist foobar echo dir removed
rem Removing nonexistent directory
rmdir foobar
echo %ErrorLevel%
rem Removing single-level directories
echo > foo
rmdir foo
echo %ErrorLevel%
if exist foo echo file not removed
del foo
mkdir foo
echo > foo\bar
rmdir foo
echo %ErrorLevel%
if exist foo echo non-empty dir not removed
del foo\bar
mkdir foo\bar
rmdir foo
echo %ErrorLevel%
if exist foo echo non-empty dir not removed
rmdir foo\bar
rmdir foo
rem Recursive rmdir
mkdir foo\bar\baz
rmdir /s /Q foo
if not exist foo (
    echo recursive rmdir succeeded
) else (
    rd foo\bar\baz
    rd foo\bar
    rd foo
)
mkdir foo\bar\baz
echo foo > foo\bar\brol
rmdir /s /Q foo 2>&1
if not exist foo (
    echo recursive rmdir succeeded
) else (
    rd foo\bar\baz
    del foo\bar\brol
    rd foo\bar
    rd foo
)
rem multiples directories at once
mkdir foobaz & cd foobaz
mkdir foo
mkdir bar\baz
mkdir foobar
rd /s/q foo bar foobar
if not exist foo (echo foo removed) else echo foo not removed!
if not exist bar (echo bar removed) else echo bar not removed!
if not exist foobar (echo foobar removed) else echo foobar not removed!
if not exist bar\baz (echo bar\baz removed) else echo bar\baz not removed!
cd .. & rd /s/q foobaz

echo ------------ Testing pushd/popd ------------
cd
echo --- popd is no-op when dir stack is empty
popd
cd
echo --- pushing non-existing dir
pushd foobar
cd
echo --- basic behaviour
mkdir foobar\baz
pushd foobar
cd
popd
cd
pushd foobar
pushd baz
cd
popd
cd
pushd baz
popd
cd
popd
cd
pushd .
cd foobar\baz
pushd ..
cd
popd
popd
cd
rd /s/q foobar

echo ------------ Testing attrib ------------
rem FIXME Add tests for archive, hidden and system attributes + mixed attributes modifications
mkdir foobar & cd foobar
echo foo original contents> foo
attrib foo
echo > bar
echo --- read-only attribute
rem Read-only files cannot be altered or deleted, unless forced
attrib +R foo
attrib foo
dir /Ar /B
echo bar>> foo
type foo
del foo > NUL 2>&1
if exist foo (
    echo Read-only file not deleted
) else (
    echo Should not delete read-only file!
)
del /F foo
if not exist foo (
    echo Read-only file forcibly deleted
) else (
    echo Should delete read-only file with del /F!
    attrib -r foo
    del foo
)
cd .. & rd /s/q foobar
echo --- recursive behaviour
mkdir foobar\baz & cd foobar
echo > level1
echo > whatever
echo > baz\level2
attrib baz\level2
cd ..
attrib +R l*vel? /S > nul 2>&1
cd foobar
attrib level1
attrib baz\level2
echo > bar
attrib bar
cd .. & rd /s/q foobar
echo --- folders processing
mkdir foobar
attrib foobar
cd foobar
mkdir baz
echo toto> baz\toto
attrib +r baz /s /d > nul 2>&1
attrib baz
attrib baz\toto
echo lulu>>baz\toto
type baz\toto
echo > baz\lala
rem Oddly windows allows file creation in a read-only directory...
if exist baz\lala (echo file created in read-only dir) else echo file not created
cd .. & rd /s/q foobar

echo ------------ Testing assoc ------------
rem Modifying associations requires some privileges...
net session >nul 2>&1
if errorlevel 1 goto :SkipAssoc

rem FIXME Can't test error messages in the current test system, so we have to use some kludges
rem FIXME Revise once || conditional execution is fixed
mkdir foobar & cd foobar
echo --- setting association
assoc .foo > baz
type baz
echo ---

assoc .foo=bar
assoc .foo

rem association set system-wide
echo @echo off> tmp.cmd
echo echo +++>> tmp.cmd
echo assoc .foo>> tmp.cmd
cmd /c tmp.cmd

echo --- resetting association
assoc .foo=
assoc .foo > baz
type baz
echo ---

rem association removal set system-wide
cmd /c tmp.cmd > baz
type baz
echo ---
cd .. & rd /s/q foobar
goto ContinueFType
:SkipAssoc
echo --- setting association
echo ---
echo .foo=bar
echo .foo=bar
echo +++
echo .foo=bar
echo --- resetting association
echo ---
echo +++
echo ---


:ContinueFType
echo ------------ Testing ftype ------------
rem Modifying associations requires some privileges...
net session >nul 2>&1
if errorlevel 1 goto :SkipFType
rem FIXME Can't test error messages in the current test system, so we have to use some kludges
rem FIXME Revise once || conditional execution is fixed
mkdir foobar & cd foobar
echo --- setting association
ftype footype> baz
type baz
echo ---

ftype footype=foo_opencmd
assoc .foo=footype
ftype footype

rem association set system-wide
echo @echo off> tmp.cmd
echo echo +++>> tmp.cmd
echo ftype footype>> tmp.cmd
cmd /c tmp.cmd

echo --- resetting association
assoc .foo=

rem Removing a file type association doesn't work on XP due to a bug, so a workaround is needed
setlocal EnableDelayedExpansion
set WINE_FOO=original value
ftype footype=
ftype footype > baz
for /F %%i in ('type baz') do (set WINE_FOO=buggyXP)
rem Resetting actually works on wine/NT4, but is reported as failing due to the peculiar test (and non-support for EnabledDelayedExpansion)
rem FIXME Revisit once a grep-like program like ftype is implemented
rem (e.g. to check baz's size using dir /b instead)
echo !WINE_FOO!

rem cleanup registry
echo REGEDIT4> regCleanup.reg
echo.>> regCleanup.reg
echo [-HKEY_CLASSES_ROOT\footype]>> regCleanup.reg
regedit /s regCleanup.reg
set WINE_FOO=
endlocal
cd .. & rd /s/q foobar
goto ContinueCall
:SkipFType
echo --- setting association
echo ---
echo footype=foo_opencmd
echo .foo=footype
echo footype=foo_opencmd
echo +++
echo footype=foo_opencmd
echo --- resetting association
echo original value

:ContinueCall
echo ------------ Testing CALL ------------
mkdir foobar & cd foobar
echo --- external script
echo echo foo %%1> foo.cmd
call foo
call foo.cmd 8
echo echo %%1 %%2 > foo.cmd
call foo.cmd foo
call foo.cmd foo bar
call foo.cmd foo ""
call foo.cmd "" bar
call foo.cmd foo ''
call foo.cmd '' bar
del foo.cmd

echo --- internal routines
call :testRoutine :testRoutine
goto :endTestRoutine
:testRoutine
echo bar %1
goto :eof
:endTestRoutine

call :testRoutineArgs foo
call :testRoutineArgs foo bar
call :testRoutineArgs foo ""
call :testRoutineArgs ""  bar
call :testRoutineArgs foo ''
call :testRoutineArgs ''  bar
goto :endTestRoutineArgs
:testRoutineArgs
echo %1 %2
goto :eof
:endTestRoutineArgs

echo --- with builtins
call mkdir foo
echo %ErrorLevel%
if exist foo (echo foo created) else echo foo should exist!
rmdir foo
set WINE_FOOBAZ_VAR=foobaz
call echo Should expand %WINE_FOOBAZ_VAR%
set WINE_FOOBAZ_VAR=
echo>batfile
call dir /b
echo>robinfile
if 1==1 call del batfile
dir /b
if exist batfile echo batfile shouldn't exist
rem ... but not for 'if' or 'for'
call if 1==1 echo bar 2> nul
echo %ErrorLevel%
call :setError 0
call for %%i in (foo bar baz) do echo %%i 2> nul
echo %ErrorLevel%
rem First look for programs in the path before trying a builtin
echo echo non-builtin dir> dir.cmd
call dir /b
del dir.cmd
rem The below line equates to call (, which does nothing, then the
rem subsequent lines are executed.
call (
  echo Line one
  echo Line two
)
rem The below line equates to call if, which always fails, then the
rem subsequent lines are executed. Note cmd.exe swallows all lines
rem starting with )
call if 1==1 (
  echo Get if
) else (
  echo ... and else!
)
call call call echo passed
cd .. & rd /s/q foobar

echo ------------ Testing SHIFT ------------

call :shiftFun p1 p2 p3 p4 p5
goto :endShiftFun

:shiftFun
echo '%1' '%2' '%3' '%4' '%5'
shift
echo '%1' '%2' '%3' '%4' '%5'
shift@tab@ /1
echo '%1' '%2' '%3' '%4' '%5'
shift /2
echo '%1' '%2' '%3' '%4' '%5'
shift /-1
echo '%1' '%2' '%3' '%4' '%5'
shift /0
echo '%1' '%2' '%3' '%4' '%5'
goto :eof
:endShiftFun

echo ------------ Testing cmd invocation ------------
rem FIXME: only a stub ATM
echo --- a batch file can delete itself
echo del foo.cmd>foo.cmd
cmd /q /c foo.cmd
if not exist foo.cmd (
    echo file correctly deleted
) else (
    echo file should be deleted!
    del foo.cmd
)
echo --- a batch file can alter itself
echo echo bar^>foo.cmd>foo.cmd
cmd /q /c foo.cmd > NUL 2>&1
if exist foo.cmd (
    type foo.cmd
    del foo.cmd
) else (
    echo file not created!
)

echo ---------- Testing copy
md foobar2
cd foobar2
rem Note echo adds 0x0d 0x0a on the end of the line in the file
echo AAA> file1
echo BBBBBB> file2
echo CCCCCCCCC> file3
md dir1
goto :testcopy

:CheckExist
if exist "%1" (
  echo Passed: Found expected %1
) else (
  echo Failed: Did not find expected %1
)
del /q "%1" >nul 2>&1
shift
if not "%1"=="" goto :CheckExist
goto :eof

:CheckNotExist
if not exist "%1" (
  echo Passed: Did not find %1
) else (
  echo Failed: Unexpectedly found %1
  del /q "%1" >nul 2>&1
)
shift
if not "%1"=="" goto :CheckNotExist
goto :eof

rem Note: No way to check file size on NT4 so skip the test
:CheckFileSize
if not exist "%1" (
  echo Failed: File missing when requested filesize check [%2]
  goto :ContinueFileSizeChecks
)
for %%i in (%1) do set WINE_filesize=%%~zi
if "%WINE_filesize%"=="%2" (
    echo Passed: file size check on %1 [%WINE_filesize%]
) else (
  if "%WINE_filesize%"=="%%~zi" (
    echo Skipping file size check on NT4
  ) else (
    echo Failed: file size check on %1 [%WINE_filesize% != %2]
  )
)
:ContinueFileSizeChecks
shift
shift
if not "%1"=="" goto :CheckFileSize
goto :eof

:testcopy

rem -----------------------
rem Simple single file copy
rem -----------------------
rem Simple single file copy, normally used syntax
copy file1 dummy.file >nul 2>&1
if errorlevel 1 echo Incorrect errorlevel
call :CheckExist dummy.file

rem Simple single file copy, destination supplied as two forms of directory
copy file1 dir1 >nul 2>&1
if errorlevel 1 echo Incorrect errorlevel
call :CheckExist dir1\file1

copy file1 dir1\ >nul 2>&1
if errorlevel 1 echo Incorrect errorlevel
call :CheckExist dir1\file1

rem Simple single file copy, destination supplied as fully qualified destination
copy file1 dir1\file99 >nul 2>&1
if errorlevel 1 echo Incorrect errorlevel
call :CheckExist dir1\file99

rem Simple single file copy, destination not supplied
cd dir1
copy ..\file1 >nul 2>&1
if errorlevel 1 echo Incorrect errorlevel
call :CheckExist file1
cd ..

rem Simple single file copy, destination supplied as nonexistent directory
copy file1 dir2\ >nul 2>&1
if not errorlevel 1 echo Incorrect errorlevel
call :CheckNotExist dir2 dir2\file1

rem -----------------------
rem Wildcarded copy
rem -----------------------
rem Simple single file copy, destination supplied as two forms of directory
copy file? dir1 >nul 2>&1
if errorlevel 1 echo Incorrect errorlevel
call :CheckExist dir1\file1 dir1\file2 dir1\file3

copy file* dir1\ >nul 2>&1
if errorlevel 1 echo Incorrect errorlevel
call :CheckExist dir1\file1 dir1\file2 dir1\file3

rem Simple single file copy, destination not supplied
cd dir1
copy ..\file*.* >nul 2>&1
if errorlevel 1 echo Incorrect errorlevel
call :CheckExist file1 file2 file3
cd ..

rem Simple wildcarded file copy, destination supplied as nonexistent directory
copy file? dir2\ >nul 2>&1
if not errorlevel 1 echo Incorrect errorlevel
call :CheckNotExist dir2 dir2\file1 dir2\file2 dir2\file3

rem ------------------------------------------------
rem Confirm overwrite works (cannot test prompting!)
rem ------------------------------------------------
copy file1 testfile >nul 2>&1
copy /y file2 testfile >nul 2>&1
call :CheckExist testfile

rem ------------------------------------------------
rem Test concatenation
rem ------------------------------------------------
rem simple case, no wildcards
copy file1+file2 testfile >nul 2>&1
if errorlevel 1 echo Incorrect errorlevel
call :CheckExist testfile

rem simple case, wildcards, no concatenation
copy file* testfile >nul 2>&1
if errorlevel 1 echo Incorrect errorlevel
call :CheckExist testfile

rem simple case, wildcards, and concatenation
echo ddddd > fred
copy file*+fred testfile >nul 2>&1
if errorlevel 1 echo Incorrect errorlevel
call :CheckExist testfile

rem simple case, wildcards, and concatenation
copy fred+file* testfile >nul 2>&1
if errorlevel 1 echo Incorrect errorlevel
call :CheckExist testfile

rem Calculate destination name
copy fred+file* dir1 >nul 2>&1
if errorlevel 1 echo Incorrect errorlevel
call :CheckExist dir1\fred

rem Calculate destination name
copy fred+file* dir1\ >nul 2>&1
if errorlevel 1 echo Incorrect errorlevel
call :CheckExist dir1\fred

rem Calculate destination name (none supplied)
cd dir1
copy ..\fred+..\file* >nul 2>&1
if errorlevel 1 echo Incorrect errorlevel
call :CheckExist fred

copy ..\fr*+..\file1  >nul 2>&1
if errorlevel 1 echo Incorrect errorlevel
call :CheckExist fred
cd ..

rem ******************************************************************
rem ASCII and BINARY tests
rem Note: hard coded numbers deliberate because need to ensure whether
rem an additional EOF has been added or not. There is no way to handle
rem EOFs in batch, so assume if a single byte appears, it's an EOF!
rem ******************************************************************

rem Confirm original sizes of file1,2,3
call :CheckFileSize file1 5 file2 8 file3 11

cd dir1

rem ----------------------------------------------
rem Show concatenation defaults copy to ascii mode
rem ----------------------------------------------
rem Simple default copy source to destination (should not append EOF 5)
copy ..\file1 file1_default >nul 2>&1
call :CheckFileSize file1_default 5

rem Simple binary copy source to destination (should not append EOF 5)
copy /b ..\file1 file1_default2 >nul 2>&1
call :CheckFileSize file1_default2 5

rem Simple ascii copy source to destination (should append EOF 5+1, 8+1, 11+1)
copy /a ..\file1 file1_plus_eof >nul 2>&1
call :CheckFileSize file1_plus_eof 6
copy /a ..\file2 file2_plus_eof >nul 2>&1
call :CheckFileSize file2_plus_eof 9
copy /a ..\file3 file3_plus_eof >nul 2>&1
call :CheckFileSize file3_plus_eof 12

rem Concat 2 files, ascii mode - (only one EOF on the end 5+8+1)
copy /a ..\file1+..\file2 file12_plus_eof >nul 2>&1
call :CheckFileSize file12_plus_eof 14

rem Concat 2 files, binary mode - (no EOF on the end 5+8)
copy /b ..\file1+..\file2 file12_no_eof >nul 2>&1
call :CheckFileSize file12_no_eof 13

rem Concat 2 files, default mode - (one EOF on the end 5+8+1)
copy ..\file1+..\file2 file12_eof2 >nul 2>&1
call :CheckFileSize file12_eof2 14

rem Test copying when destination is one of the sources.
rem Concat file1+file2+file3 into file1, should produce file1+file2+file3 = 24
copy /y ..\file? .\ >nul 2>&1
copy /y /b file1+file2+file3 file1 >nul 2>&1
call :CheckFileSize file1 24

rem Concat file1+file2+file3 into file2, should produce file1+file3 = 16
copy /y ..\file? .\ >nul 2>&1
copy /y /b file1+file2+file3 file2 >nul 2>&1
call :CheckFileSize file2 16

rem Concat file1+file2+file3 into file3, should produce file1+file2 = 13
copy /y ..\file? .\ >nul 2>&1
copy /y /b file1+file2+file3 file3 >nul 2>&1
call :CheckFileSize file3 13

rem --------------------------------------------------------------
rem Show ascii source copy stops at first EOF, binary does the lot
rem --------------------------------------------------------------
copy file1_plus_eof /b file1_binary_srccopy /b >nul 2>&1
call :CheckFileSize file1_binary_srccopy 6

copy file1_plus_eof /a file1_ascii_srccopy /b >nul 2>&1
call :CheckFileSize file1_ascii_srccopy 5

rem --------------------------------------------------------------
rem Show results of concatenating files (ending in EOFs) and /a /b
rem --------------------------------------------------------------

rem Default and ascii copy reads as ascii, stripping EOFs, so 6-1 + 9-1 + 12-1 + 1
copy file1_plus_eof+file2_plus_eof+file3_plus_eof file123_default_copy >nul 2>&1
call :CheckFileSize file123_default_copy 25
copy /a file1_plus_eof+file2_plus_eof+file3_plus_eof file123_ascii_copy >nul 2>&1
call :CheckFileSize file123_ascii_copy 25

rem In binary mode, we get 3 eofs, so 6 + 9 + 12 = 27
copy /b file1_plus_eof + file2_plus_eof + file3_plus_eof file123_binary_copy >nul 2>&1
call :CheckFileSize file123_binary_copy 27

rem We can select which we want the eofs from by postfixing it with /a or /b
rem so here have first and third with eof, second as ascii 6 + 9-1 + 12
copy file1_plus_eof /b + file2_plus_eof /a + file3_plus_eof /b file123_mixed_copy1 >nul 2>&1
call :CheckFileSize file123_mixed_copy1 26

rem By postfixing the destination with /a, we ask for an ascii destination which appends EOF
rem so here have first and third with eof, second as ascii 6 + 9-1 + 12 + extra EOF
rem Note the delta between this and the previous one also shows that the destination
rem ascii/binary is inherited from the last /a or /b on the line
copy file1_plus_eof /b + file2_plus_eof /a + file3_plus_eof /b file123_mixed_copy2 /a >nul 2>&1
call :CheckFileSize file123_mixed_copy2 27

rem so here have second with eof, first and third as ascii 6-1 + 9 + 12-1
rem Note the delta between the next two also shows that the destination ascii/binary is
rem inherited from the last /a or /b on the line, so the first has an extra EOF
copy file1_plus_eof /a + file2_plus_eof /b + file3_plus_eof /a file123_mixed_copy3 >nul 2>&1
call :CheckFileSize file123_mixed_copy3 26
copy file1_plus_eof /a + file2_plus_eof /b + file3_plus_eof /a file123_mixed_copy4 /b >nul 2>&1
call :CheckFileSize file123_mixed_copy4 25

rem -------------------------------------------------------------------------------------------
rem This shows when concatenating, an ascii destination always adds on an EOF but when we
rem are not concatenating, it's a direct copy regardless of destination if being read as binary
rem -------------------------------------------------------------------------------------------

rem All 3 have eof's, plus an extra = 6 + 9 + 12 + eof
copy /b file1_plus_eof + file2_plus_eof + file3_plus_eof file123_mixed_copy5 /a >nul 2>&1
call :CheckFileSize file123_mixed_copy5 28

rem All 2 have eof's, plus an extra = 6 + 12 + eof
copy /b file1_plus_eof + file3_plus_eof file123_mixed_copy6 /a >nul 2>&1
call :CheckFileSize file123_mixed_copy6 19

rem One file has EOF, but doesn't get an extra one, i.e. 6
copy /b file1_plus_eof file123_mixed_copy7 /a >nul 2>&1
call :CheckFileSize file123_mixed_copy7 6

rem Syntax means concatenate so ascii destination kicks in
copy /b file1_plus_eof* file123_mixed_copy8 /a >nul 2>&1
call :CheckFileSize file123_mixed_copy8 7

del *.* /q
cd ..

rem ---------------------------------------
rem Error combinations
rem ---------------------------------------
rem Specify source directory but name is a file
call :setError 0
copy file1\ dir1\ >NUL 2>&1
if errorlevel 1 echo Passed: errorlevel invalid check 1
if not errorlevel 1 echo Failed: errorlevel invalid check 1
call :CheckNotExist dir1\file1

rem Overwrite same file
call :setError 0
copy file1 file1 >NUL 2>&1
if errorlevel 1 echo Passed: errorlevel invalid check 2
if not errorlevel 1 echo Failed: errorlevel invalid check 2

rem Supply same file identified as a directory
call :setError 0
copy file1 file1\ >NUL 2>&1
if errorlevel 1 echo Passed: errorlevel invalid check 3
if not errorlevel 1 echo Failed: errorlevel invalid check 3

cd ..
rd foobar2 /s /q

echo ------------ Testing setlocal/endlocal ------------
call :setError 0
rem Note: setlocal EnableDelayedExpansion already tested in the variable delayed expansion test section
mkdir foobar & cd foobar
echo --- enable/disable extensions
setlocal DisableEXTensions
echo ErrLev: %ErrorLevel%
endlocal
echo ErrLev: %ErrorLevel%
echo @echo off> tmp.cmd
echo echo ErrLev: %%ErrorLevel%%>> tmp.cmd
rem Enabled by default
cmd /C tmp.cmd
cmd /E:OfF /C tmp.cmd
cmd /e:oN /C tmp.cmd

rem FIXME: creating file before setting envvar value to prevent parsing-time evaluation (due to EnableDelayedExpansion not being implemented/available yet)
echo --- setlocal with corresponding endlocal
rem %CD% does not work on NT4 so use the following workaround
for /d %%i in (.) do set WINE_CURDIR=%%~dpnxi
echo @echo off> test.cmd
echo echo %%WINE_VAR%%>> test.cmd
echo setlocal>> test.cmd
echo set WINE_VAR=localval>> test.cmd
echo md foobar2>> test.cmd
echo cd foobar2>> test.cmd
echo echo %%WINE_VAR%%>> test.cmd
echo for /d %%%%i in (.) do echo %%%%~dpnxi>> test.cmd
echo endlocal>> test.cmd
echo echo %%WINE_VAR%%>> test.cmd
echo for /d %%%%i in (.) do echo %%%%~dpnxi>> test.cmd
set WINE_VAR=globalval
call test.cmd
echo %WINE_VAR%
for /d %%i in (.) do echo %%~dpnxi
cd /d %WINE_CURDIR%
rd foobar2
set WINE_VAR=
echo --- setlocal with no corresponding endlocal
echo @echo off> test.cmd
echo echo %%WINE_VAR%%>> test.cmd
echo setlocal>> test.cmd
echo set WINE_VAR=localval>> test.cmd
echo md foobar2>> test.cmd
echo cd foobar2>> test.cmd
echo echo %%WINE_VAR%%>> test.cmd
echo for /d %%%%i in (.) do echo %%%%~dpnxi>> test.cmd
set WINE_VAR=globalval
rem %CD% does not work on NT4 so use the following workaround
for /d %%i in (.) do set WINE_CURDIR=%%~dpnxi
call test.cmd
echo %WINE_VAR%
for /d %%i in (.) do echo %%~dpnxi
cd /d %WINE_CURDIR%
rd foobar2
set WINE_VAR=
echo --- setlocal within same batch program
set WINE_var1=one
set WINE_var2=
set WINE_var3=
rem %CD% does not work on NT4 so use the following workaround
for /d %%i in (.) do set WINE_CURDIR=%%~dpnxi
setlocal
set WINE_var2=two
mkdir foobar2
cd foobar2
setlocal
set WINE_var3=three
if "%WINE_var1%"=="one" echo Var1 ok 1
if "%WINE_var2%"=="two" echo Var2 ok 2
if "%WINE_var3%"=="three" echo Var3 ok 3
for /d %%i in (.) do set WINE_curdir2=%%~dpnxi
if "%WINE_curdir2%"=="%WINE_CURDIR%\foobar2" echo Directory is ok 1
endlocal
if "%WINE_var1%"=="one" echo Var1 ok 1
if "%WINE_var2%"=="two" echo Var2 ok 2
if "%WINE_var3%"=="" echo Var3 ok 3
for /d %%i in (.) do set WINE_curdir2=%%~dpnxi
if "%WINE_curdir2%"=="%WINE_CURDIR%\foobar2" echo Directory is ok 2
endlocal
if "%WINE_var1%"=="one" echo Var1 ok 1
if "%WINE_var2%"=="" echo Var2 ok 2
if "%WINE_var3%"=="" echo Var3 ok 3
for /d %%i in (.) do set WINE_curdir2=%%~dpnxi
if "%WINE_curdir2%"=="%WINE_CURDIR%" echo Directory is ok 3
rd foobar2 /s /q
set WINE_var1=

echo --- Mismatched set and end locals
mkdir foodir2 2>nul
mkdir foodir3 2>nul
mkdir foodir4 2>nul
rem %CD% does not work on NT4 so use the following workaround
for /d %%i in (.) do set WINE_curdir=%%~dpnxi

echo @echo off> 2set1end.cmd
echo echo %%WINE_var%%>> 2set1end.cmd
echo setlocal>> 2set1end.cmd
echo set WINE_VAR=2set1endvalue1>> 2set1end.cmd
echo cd ..\foodir3>> 2set1end.cmd
echo setlocal>> 2set1end.cmd
echo set WINE_VAR=2set1endvalue2>> 2set1end.cmd
echo cd ..\foodir4>> 2set1end.cmd
echo endlocal>> 2set1end.cmd
echo echo %%WINE_var%%>> 2set1end.cmd
echo for /d %%%%i in (.) do echo %%%%~dpnxi>> 2set1end.cmd

echo @echo off> 1set2end.cmd
echo echo %%WINE_var%%>> 1set2end.cmd
echo setlocal>> 1set2end.cmd
echo set WINE_VAR=1set2endvalue1>> 1set2end.cmd
echo cd ..\foodir3>> 1set2end.cmd
echo endlocal>> 1set2end.cmd
echo echo %%WINE_var%%>> 1set2end.cmd
echo for /d %%%%i in (.) do echo %%%%~dpnxi>> 1set2end.cmd
echo endlocal>> 1set2end.cmd
echo echo %%WINE_var%%>> 1set2end.cmd
echo for /d %%%%i in (.) do echo %%%%~dpnxi>> 1set2end.cmd

echo --- Extra setlocal in called batch
set WINE_VAR=value1
rem -- setlocal1 == this batch, should never be used inside a called routine
setlocal
set WINE_var=value2
cd foodir2
call "%WINE_CURDIR%\2set1end.cmd"
echo Finished:
echo %WINE_VAR%
for /d %%i in (.) do echo %%~dpnxi
endlocal
echo %WINE_VAR%
for /d %%i in (.) do echo %%~dpnxi
cd /d %WINE_CURDIR%

echo --- Extra endlocal in called batch
set WINE_VAR=value1
rem -- setlocal1 == this batch, should never be used inside a called routine
setlocal
set WINE_var=value2
cd foodir2
call "%WINE_CURDIR%\1set2end.cmd"
echo Finished:
echo %WINE_VAR%
for /d %%i in (.) do echo %%~dpnxi
endlocal
echo %WINE_VAR%
for /d %%i in (.) do echo %%~dpnxi
cd /d %WINE_CURDIR%

echo --- endlocal in called function rather than batch pgm is ineffective
@echo off
set WINE_var=1
set WINE_var2=1
setlocal
set WINE_var=2
call :endlocalroutine
echo %WINE_var%
endlocal
echo %WINE_var%
goto :endlocalfinished
:endlocalroutine
echo %WINE_var%
endlocal
echo %WINE_var%
setlocal
set WINE_var2=2
endlocal
echo %WINE_var2%
endlocal
echo %WINE_var%
echo %WINE_var2%
goto :eof
:endlocalfinished
echo %WINE_var%

set WINE_var=
set WINE_var2=
cd .. & rd /q/s foobar

echo ------------ Testing Errorlevel ------------
rem WARNING: Do *not* add tests using ErrorLevel after this section
should_not_exist 2> nul > nul
echo %ErrorLevel%
rem nt 4.0 doesn't really support a way of setting errorlevel, so this is weak
rem See http://www.robvanderwoude.com/exit.php
call :setError 1
echo %ErrorLevel%
if errorlevel 2 echo errorlevel too high, bad
if errorlevel 1 echo errorlevel just right, good
if errorlevel 01 echo errorlevel with leading zero just right, good
if errorlevel -1 echo errorlevel with negative number OK
if errorlevel 0x1 echo hexa should not be recognized!
if errorlevel 1a echo invalid error level recognized!
call :setError 0
echo abc%ErrorLevel%def
if errorlevel 1 echo errorlevel nonzero, bad
if not errorlevel 1 echo errorlevel zero, good
if not errorlevel 0x1 echo hexa should not be recognized!
if not errorlevel 1a echo invalid error level recognized!
rem Now verify that setting a real variable hides its magic variable
set errorlevel=7
echo %ErrorLevel% should be 7
if errorlevel 7 echo setting var worked too well, bad
call :setError 3
echo %ErrorLevel% should still be 7

echo ------------ Testing GOTO ------------
if a==a goto dest1
echo FAILURE at dest 1
:dest1
echo goto with no leading space worked
if a==a goto :dest1b
echo FAILURE at dest 1b
:dest1b
echo goto with colon and no leading space worked
if b==b goto dest2
echo FAILURE at dest 2
 :dest2
echo goto with a leading space worked
if c==c goto dest3
echo FAILURE at dest 3
	:dest3
echo goto with a leading tab worked
if d==d goto dest4
echo FAILURE at dest 4
:dest4@space@
echo goto with a following space worked
if e==e goto dest5
echo FAILURE at dest 5
:dest5&& echo FAILURE
echo goto with following amphersands worked

del failure.txt >nul 2>&1
if f==f goto dest6
echo FAILURE at dest 6
:dest6>FAILURE.TXT
if exist FAILURE.TXT echo FAILURE at dest 6 as file exists
echo goto with redirections worked
del FAILURE.TXT >nul 2>&1

:: some text that is ignored | dir >cmd_output | another test
if exist cmd_output echo FAILURE at dest 6 as file exists
echo Ignoring double colons worked
del cmd_output >nul 2>&1

rem goto a label which does not exist issues an error message and
rem acts the same as goto :EOF, and ensure ::label is never matched
del testgoto.bat >nul 2>&1
echo goto :dest7 ^>nul 2^>^&1 >> testgoto.bat
echo echo FAILURE at dest 7 - Should have not found label and issued an error plus ended the batch>> testgoto.bat
echo ::dest7>> testgoto.bat
echo echo FAILURE at dest 7 - Incorrectly went to label >> testgoto.bat
call testgoto.bat
del testgoto.bat >nul 2>&1

del testgoto.bat >nul 2>&1
echo goto ::dest8 ^>nul 2^>^&1 >> testgoto.bat
echo echo FAILURE at dest 8 - Should have not found label and issued an error plus ended the batch>> testgoto.bat
echo ::dest8>> testgoto.bat
echo echo FAILURE at dest 8 - Incorrectly went to label >> testgoto.bat
call testgoto.bat
del testgoto.bat >nul 2>&1

if g==g goto dest9
echo FAILURE at dest 9
:dest91
echo FAILURE at dest 91
@   :     dest9>rubbish
echo label with mixed whitespace and no echo worked

if h==h goto :dest10:this is ignored
echo FAILURE at dest 10
:dest10:this is also ignored
echo Correctly ignored trailing information

rem Testing which label is reached when there are many options
echo Begin:
set nextlabel=
call :sub
set nextlabel=middle
goto :sub

:sub
echo ..First sub
if not "%nextlabel%"=="" goto :%nextlabel%
goto :EOF

:sub
echo ..Second sub
if not "%nextlabel%"=="" goto :%nextlabel%
goto :EOF

:middle
echo Middle:
set nextlabel=
call :sub
set nextlabel=nearend
goto :sub

:sub
echo ..Third sub
if not "%nextlabel%"=="" goto :%nextlabel%
goto :EOF

:nearend
echo Near end:
set nextlabel=
call :sub
set nextlabel=end
goto :sub

:sub
echo ..Fourth sub
if not "%nextlabel%"=="" goto :%nextlabel%
goto :EOF

:end
echo At end:
set nextlabel=
call :sub
set nextlabel=done
goto :sub

:done
echo Finished

echo ------------ Testing PATH ------------
set WINE_backup_path=%path%
set path=original
path
path try2
path
path=try3
path
set path=%WINE_backup_path%
set WINE_backup_path=

echo ------------ Testing start /W ------------
echo start /W failed to wait>foobar.txt
start /W "" cmd /C "ping -n1 & echo start /W seems to really wait>foobar.txt"& type foobar.txt& del foobar.txt

echo ------------ Testing changing the drive letter ----------
pushd C:\

echo Normal:
call :setError 0
C:
if errorlevel 1 echo Normal drive change failed

echo Normal+space
call :setError 0
C:@space@
if errorlevel 1 echo Normal+space drive change failed

echo Normal+space+garbage
call :setError 0
C: garbage
if errorlevel 1 echo Normal+space+garbage drive change failed

call :setError 0
echo Quoted should fail
"C:"
if not errorlevel 1 echo quoted drive change unexpectedly worked

echo Normal+tab
call :setError 0
C:@tab@
if errorlevel 1 echo Normal+tab drive change failed

echo Normal+tab+garbage
call :setError 0
C:@tab@garbagetab
if errorlevel 1 echo Normal+tab+garbage drive change failed

popd

echo ------------ Testing combined CALLs/GOTOs ------------
echo @echo off>foo.cmd
echo goto :eof>>foot.cmd
echo :eof>>foot.cmd
echo echo world>>foo.cmd

echo @echo off>foot.cmd
echo echo cheball>>foot.cmd
echo.>>foot.cmd
echo call :bar>>foot.cmd
echo if "%%1"=="deleteMe" (del foot.cmd)>>foot.cmd
echo goto :eof>>foot.cmd
echo.>>foot.cmd
echo :bar>>foot.cmd
echo echo barbare>>foot.cmd
echo goto :eof>>foot.cmd

call foo.cmd
call foot
call :bar
del foo.cmd
rem Script execution stops after the following line
foot deleteMe
call :foo
call :foot
goto :endFuns

:foot
echo foot

:foo
echo foo
goto :eof

:endFuns

:bar
echo bar
call :foo

:baz
echo baz
goto :eof

echo Final message is not output since earlier 'foot' processing stops script execution
echo Do NOT add any tests below this line

echo ------------ Done, jumping to EOF -----------
goto :eof
rem Subroutine to set errorlevel and return
rem in windows nt 4.0, this always sets errorlevel 1, since /b isn't supported
:setError
exit /B %1
rem This line runs under cmd in windows NT 4, but not in more modern versions.