File: operations_test.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (3042 lines) | stat: -rw-r--r-- 102,570 bytes parent folder | download | duplicates (5)
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
//  Boost operations_test.cpp  ---------------------------------------------------------//

//  Copyright Beman Dawes 2002, 2009.

//  Distributed under the Boost Software License, Version 1.0.
//  See http://www.boost.org/LICENSE_1_0.txt

//  Library home page: http://www.boost.org/libs/filesystem

#include <boost/config/warning_disable.hpp>

//  See deprecated_test for tests of deprecated features
#ifndef BOOST_FILESYSTEM_NO_DEPRECATED
#define BOOST_FILESYSTEM_NO_DEPRECATED
#endif
#ifndef BOOST_SYSTEM_NO_DEPRECATED
#define BOOST_SYSTEM_NO_DEPRECATED
#endif

#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/directory.hpp>
#include <boost/filesystem/exception.hpp>
#include <boost/filesystem/file_status.hpp>
#include <boost/filesystem/fstream.hpp> // for BOOST_FILESYSTEM_C_STR

#include <boost/cerrno.hpp>
#include <boost/system/error_code.hpp>
#include <boost/system/system_error.hpp>
#include <boost/system/system_category.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/detail/lightweight_main.hpp>

namespace fs = boost::filesystem;
using boost::system::error_code;
using boost::system::system_category;
using boost::system::system_error;

#include <fstream>
#include <iostream>

using std::cout;
using std::endl;

#include <string>
#include <vector>
#include <algorithm>
#include <cstring> // for strncmp, etc.
#include <ctime>
#include <cstdlib> // for system(), getenv(), etc.
#ifdef BOOST_POSIX_API
#include <unistd.h>
#endif

#ifdef BOOST_WINDOWS_API
#include <windows.h>

inline std::wstring convert(const char* c)
{
    std::string s(c);

    return std::wstring(s.begin(), s.end());
}

//  Note: these three setenv* functions are not general solutions for the missing
//  setenv* problem on VC++. See Microsoft's _putenv for that need, and ticker #7018
//  for discussion and rationale for returning void for this test program, which needs
//  to work for both the MSVC Runtime and the Windows Runtime (which does not support
//  _putenv).

inline void setenv_(const char* name, const fs::path::value_type* val, int)
{
    SetEnvironmentVariableW(convert(name).c_str(), val);
}

inline void setenv_(const char* name, const char* val, int)
{
    SetEnvironmentVariableW(convert(name).c_str(), convert(val).c_str());
}

inline void unsetenv_(const char* name)
{
    SetEnvironmentVariableW(convert(name).c_str(), 0);
}

//! Sets read-only attribute on a file
inline void set_read_only(fs::path const& p)
{
    DWORD attrs = GetFileAttributesW(p.c_str());
    if (attrs == INVALID_FILE_ATTRIBUTES)
    {
        DWORD err = GetLastError();
        throw fs::filesystem_error("operations_test set_read_only: failed to get file attributes", p, error_code(err, system_category()));
    }

    attrs |= FILE_ATTRIBUTE_READONLY;
    if (!SetFileAttributesW(p.c_str(), attrs))
    {
        DWORD err = GetLastError();
        throw fs::filesystem_error("operations_test set_read_only: failed to set file attributes", p, error_code(err, system_category()));
    }
}

//! Converts path to a Windows long path
inline fs::path make_long_path(fs::path const& p)
{
    fs::path lp;
    // Long paths must be absolute, use preferred separators and not contain dot and dot-dot elements.
    // Also, UNC paths must use the "\\?\UNC\" prefix.
    fs::path np = p.lexically_normal();
    np.make_preferred();
    std::wstring rp = np.root_path().wstring();
    if (rp.size() > 4u && rp[0] == L'\\' && rp[1] == '\\' && (rp[2] == L'?' || rp[2] == L'.') && rp[3] == '\\')
        lp = rp;
    else if (rp.size() > 2u && rp[0] == L'\\' && rp[1] == '\\')
        lp = L"\\\\?\\UNC\\" + rp.substr(2);
    else
        lp = L"\\\\?\\" + rp;

    lp /= np.relative_path();

    return lp;
}

#else

#include <unistd.h> // sleep
#include <stdlib.h> // allow unqualifed calls to env funcs on SunOS

inline void setenv_(const char* name, const char* val, int ovw)
{
    setenv(name, val, ovw);
}

inline void unsetenv_(const char* name)
{
    unsetenv(name);
}

#endif

//! Converts root path of the argument to canonical form, i.e. "C:\" instead of "c:\" on Windows.
inline fs::path canonicalize_root_path(fs::path const& p)
{
    fs::path root_path = p.root_path();
    if (root_path.empty())
        return p;
    root_path = fs::canonical(root_path);
    fs::path rel_path = p.relative_path();
    if (!rel_path.empty())
        root_path.append(rel_path);
    return root_path;
}

#define CHECK_EXCEPTION(Functor, Expect) throws_fs_error(Functor, Expect, __LINE__)

namespace {
typedef int errno_t;
std::string platform(BOOST_PLATFORM);
bool report_throws = false;
bool cleanup = true;
bool skip_long_windows_tests = false;

fs::directory_iterator end_itr;
fs::path dir;
fs::path d1;
fs::path d2;
fs::path f0;
fs::path f1;
fs::path d1f1;

bool create_symlink_ok(true);

fs::path ng(" no-way, Jose");

const fs::path temp_dir(fs::unique_path("op-test-%%%%-%%%%"));

void create_file(const fs::path& ph, const std::string& contents = std::string())
{
    std::ofstream f(BOOST_FILESYSTEM_C_STR(ph));
    if (!f)
        throw fs::filesystem_error("operations_test create_file", ph, error_code(errno, system_category()));
    if (!contents.empty())
        f << contents;
}

void verify_file(const fs::path& ph, const std::string& expected)
{
    std::ifstream f(BOOST_FILESYSTEM_C_STR(ph));
    if (!f)
        throw fs::filesystem_error("operations_test verify_file", ph, error_code(errno, system_category()));
    std::string contents;
    f >> contents;
    if (contents != expected)
        throw fs::filesystem_error("operations_test verify_file contents \"" + contents + "\" != \"" + expected + "\"", ph, error_code());
}

template< typename F >
bool throws_fs_error(F func, errno_t en, int line)
{
    try
    {
        func();
    }
    catch (const fs::filesystem_error& ex)
    {
        if (report_throws)
        {
            // use the what() convenience function to display exceptions
            cout << "\n"
                 << ex.what() << "\n";
        }
        if (en == 0 || en == ex.code().default_error_condition().value())
            return true;
        cout
            << "\nWarning: line " << line
            << " exception reports default_error_condition().value() "
            << ex.code().default_error_condition().value()
            << ", should be " << en
            << "\n value() is " << ex.code().value()
            << endl;
        return true;
    }
    return false;
}

struct poison_category_impl : public boost::system::error_category
{
    char const* name() const noexcept { return "poison"; }
    std::string message(int) const { return "poison_category::message"; }
};

boost::system::error_category& poison_category()
{
    static poison_category_impl instance;
    return instance;
}

// compile-only two argument "do-the-right-thing" tests
//   verifies that all overload combinations compile without error
void do_the_right_thing_tests(bool call_ = false)
{
    if (call_)
    {
        fs::path p;
        std::string s;
        const char* a = 0;
        fs::copy_file(p, p);
        fs::copy_file(s, p);
        fs::copy_file(a, p);
        fs::copy_file(p, s);
        fs::copy_file(p, a);
        fs::copy_file(s, s);
        fs::copy_file(a, s);
        fs::copy_file(s, a);
        fs::copy_file(a, a);
    }
}

void bad_file_size()
{
    fs::file_size(" No way, Jose");
}

void bad_directory_size()
{
    fs::file_size(fs::current_path());
}

fs::path bad_create_directory_path;
void bad_create_directory()
{
    fs::create_directory(bad_create_directory_path);
}

void bad_equivalent()
{
    fs::equivalent("no-such-path", "another-not-present-path");
}

fs::path bad_remove_dir;
void bad_remove()
{
    fs::remove(bad_remove_dir);
}

void bad_space()
{
    fs::space("no-such-path");
}

class renamer
{
public:
    renamer(const fs::path& p1, const fs::path& p2) :
        from(p1), to(p2) {}
    void operator()()
    {
        fs::rename(from, to);
    }

private:
    fs::path from;
    fs::path to;
};

//------------------------------ debugging aids --------------------------------------//

//std::ostream& operator<<(std::ostream& os, const fs::file_status& s)
//{
//  if (s.type() == fs::status_error)        { os << "status_error"; }
//  else if (s.type() == fs::file_not_found) { os << "file_not_found"; }
//  else if (s.type() == fs::regular_file)   { os << "regular_file"; }
//  else if (s.type() == fs::directory_file) { os << "directory_file"; }
//  else if (s.type() == fs::symlink_file)   { os << "symlink_file"; }
//  else if (s.type() == fs::block_file)     { os << "block_file"; }
//  else if (s.type() == fs::character_file) { os << "character_file"; }
//  else if (s.type() == fs::fifo_file)      { os << "fifo_file"; }
//  else if (s.type() == fs::socket_file)    { os << "socket_file"; }
//  else if (s.type() == fs::reparse_file)   { os << "reparse_file"; }
//  else                                     { os << "type_unknown"; }
//  return os;
//}

//void dump_tree(const fs::path & root)
//{
//  cout << "dumping tree rooted at " << root << endl;
//  for (fs::recursive_directory_iterator it (root, fs::directory_options::follow_directory_symlink);
//       it != fs::recursive_directory_iterator();
//       ++it)
//  {
//    for (int i = 0; i <= it.level(); ++i)
//      cout << "  ";

//    cout << it->path();
//    if (fs::is_symlink(it->path()))
//    {
//      cout << " [symlink]" << endl;
//    }
//    else
//      cout << endl;
//  }
//}

//  exception_tests()  ---------------------------------------------------------------//

#if defined(BOOST_GCC) && BOOST_GCC >= 80000
#pragma GCC diagnostic push
// catching polymorphic type "X" by value - that's the intention of the test
#pragma GCC diagnostic ignored "-Wcatch-value"
#endif

void exception_tests()
{
    cout << "exception_tests..." << endl;
    bool exception_thrown;

    //  catch runtime_error by value

    cout << "  catch runtime_error by value" << endl;
    exception_thrown = false;
    try
    {
        fs::create_directory("no-such-dir/foo/bar");
    }
    catch (std::runtime_error x)
    {
        exception_thrown = true;
        cout << "    x.what() returns \"" << x.what() << "\"" << endl;
    }
    BOOST_TEST(exception_thrown);

    //  catch system_error by value

    cout << "  catch system_error by value" << endl;
    exception_thrown = false;
    try
    {
        fs::create_directory("no-such-dir/foo/bar");
    }
    catch (system_error x)
    {
        exception_thrown = true;
        cout << "    x.what() returns \"" << x.what() << "\"" << endl;
    }
    BOOST_TEST(exception_thrown);

    //  catch filesystem_error by value

    cout << "  catch filesystem_error by value" << endl;
    exception_thrown = false;
    try
    {
        fs::create_directory("no-such-dir/foo/bar");
    }
    catch (fs::filesystem_error x)
    {
        exception_thrown = true;
        cout << "    x.what() returns \"" << x.what() << "\"" << endl;
    }
    BOOST_TEST(exception_thrown);

    //  catch filesystem_error by const reference

    cout << "  catch filesystem_error by const reference" << endl;
    exception_thrown = false;
    try
    {
        fs::create_directory("no-such-dir/foo/bar");
    }
    catch (const fs::filesystem_error& x)
    {
        exception_thrown = true;
        cout << "    x.what() returns \"" << x.what() << "\"" << endl;
    }
    BOOST_TEST(exception_thrown);

    // the bound functions should throw, so CHECK_EXCEPTION() should return true

    BOOST_TEST(CHECK_EXCEPTION(bad_file_size, ENOENT));

    if (platform == "Windows")
        BOOST_TEST(CHECK_EXCEPTION(bad_directory_size, ENOENT));
    else
        BOOST_TEST(CHECK_EXCEPTION(bad_directory_size, 0));

    // test path::exception members
    try
    {
        fs::file_size(ng); // will throw
    }
    catch (fs::filesystem_error& ex)
    {
        BOOST_TEST(ex.path1().string() == " no-way, Jose");
    }

    cout << "  exception_tests complete" << endl;
}

#if defined(BOOST_GCC) && BOOST_GCC >= 80000
#pragma GCC diagnostic pop
#endif

// create a directory tree that can be used by subsequent tests  ---------------------//
//
//    dir
//      d1
//        d1f1       // an empty file
//      f0           // an empty file
//      f1           // a file containing "file-f1"

void create_tree()
{
    cout << "creating test directories and files in " << dir << endl;

    // create directory d1
    BOOST_TEST(!fs::create_directory(dir));
    BOOST_TEST(!fs::is_symlink(dir));
    BOOST_TEST(!fs::is_symlink("nosuchfileordirectory"));
    d1 = dir / "d1";
    BOOST_TEST(fs::create_directory(d1));
    BOOST_TEST(fs::exists(d1));
    BOOST_TEST(fs::is_directory(d1));
    BOOST_TEST(fs::is_empty(d1));

    // create an empty file named "d1f1"
    d1f1 = d1 / "d1f1";
    create_file(d1f1, "");
    BOOST_TEST(fs::exists(d1f1));
    BOOST_TEST(!fs::is_directory(d1f1));
    BOOST_TEST(fs::is_regular_file(d1f1));
    BOOST_TEST(fs::is_empty(d1f1));
    BOOST_TEST(fs::file_size(d1f1) == 0);
    BOOST_TEST(fs::hard_link_count(d1f1) == 1);

    // create an empty file named "f0"
    f0 = dir / "f0";
    create_file(f0, "");
    BOOST_TEST(fs::exists(f0));
    BOOST_TEST(!fs::is_directory(f0));
    BOOST_TEST(fs::is_regular_file(f0));
    BOOST_TEST(fs::is_empty(f0));
    BOOST_TEST(fs::file_size(f0) == 0);
    BOOST_TEST(fs::hard_link_count(f0) == 1);

    // create a file named "f1"
    f1 = dir / "f1";
    create_file(f1, "file-f1");
    BOOST_TEST(fs::exists(f1));
    BOOST_TEST(!fs::is_directory(f1));
    BOOST_TEST(fs::is_regular_file(f1));
    BOOST_TEST(fs::file_size(f1) == 7);
    verify_file(f1, "file-f1");
}

//  directory_iterator_tests  --------------------------------------------------------//

void directory_iterator_tests()
{
    cout << "directory_iterator_tests..." << endl;

    bool dir_itr_exception(false);
    try
    {
        fs::directory_iterator it("");
    }
    catch (const fs::filesystem_error&)
    {
        dir_itr_exception = true;
    }
    BOOST_TEST(dir_itr_exception);

    error_code ec;

    BOOST_TEST(!ec);
    fs::directory_iterator it("", ec);
    BOOST_TEST(ec);

    dir_itr_exception = false;
    try
    {
        fs::directory_iterator itx("nosuchdirectory");
    }
    catch (const fs::filesystem_error&)
    {
        dir_itr_exception = true;
    }
    BOOST_TEST(dir_itr_exception);

    ec.clear();
    fs::directory_iterator it2x("nosuchdirectory", ec);
    BOOST_TEST(ec);

    dir_itr_exception = false;
    try
    {
        error_code ecx;
        fs::directory_iterator itx("nosuchdirectory", ecx);
        BOOST_TEST(ecx);
        BOOST_TEST(ecx == boost::system::errc::no_such_file_or_directory);
    }
    catch (const fs::filesystem_error&)
    {
        dir_itr_exception = true;
    }
    BOOST_TEST(!dir_itr_exception);

    // create a second directory named d2
    d2 = dir / "d2";
    fs::create_directory(d2);
    BOOST_TEST(fs::exists(d2));
    BOOST_TEST(fs::is_directory(d2));

    // test the basic operation of directory_iterators, and test that
    // stepping one iterator doesn't affect a different iterator.
    {
        typedef std::vector< fs::directory_entry > vec_type;
        vec_type vec;

        fs::directory_iterator it1(dir);
        BOOST_TEST(it1 != fs::directory_iterator());
        BOOST_TEST(fs::exists(it1->status()));
        vec.push_back(*it1);
        BOOST_TEST(*it1 == vec[0]);

        fs::directory_iterator it2(dir);
        BOOST_TEST(it2 != fs::directory_iterator());
        BOOST_TEST(*it1 == *it2);

        ++it1;
        BOOST_TEST(it1 != fs::directory_iterator());
        BOOST_TEST(fs::exists(it1->status()));
        BOOST_TEST(it1 != it2);
        BOOST_TEST(*it1 != vec[0]);
        BOOST_TEST(*it2 == vec[0]);
        vec.push_back(*it1);

        ++it1;
        BOOST_TEST(it1 != fs::directory_iterator());
        BOOST_TEST(fs::exists(it1->status()));
        BOOST_TEST(it1 != it2);
        BOOST_TEST(*it2 == vec[0]);
        vec.push_back(*it1);

        ++it1;
        BOOST_TEST(it1 != fs::directory_iterator());
        BOOST_TEST(fs::exists(it1->status()));
        BOOST_TEST(it1 != it2);
        BOOST_TEST(*it2 == vec[0]);
        vec.push_back(*it1);

        ++it1;
        BOOST_TEST(it1 == fs::directory_iterator());

        BOOST_TEST(*it2 == vec[0]);
        ec.clear();
        it2.increment(ec);
        BOOST_TEST(!ec);
        BOOST_TEST(it2 != fs::directory_iterator());
        BOOST_TEST(it1 == fs::directory_iterator());
        BOOST_TEST(*it2 == vec[1]);
        ++it2;
        BOOST_TEST(*it2 == vec[2]);
        BOOST_TEST(it1 == fs::directory_iterator());
        ++it2;
        BOOST_TEST(*it2 == vec[3]);
        ++it2;
        BOOST_TEST(it1 == fs::directory_iterator());
        BOOST_TEST(it2 == fs::directory_iterator());

        // sort vec and check that the right directory entries were found
        std::sort(vec.begin(), vec.end());

        BOOST_TEST_EQ(vec[0].path().filename().string(), std::string("d1"));
        BOOST_TEST_EQ(vec[1].path().filename().string(), std::string("d2"));
        BOOST_TEST_EQ(vec[2].path().filename().string(), std::string("f0"));
        BOOST_TEST_EQ(vec[3].path().filename().string(), std::string("f1"));
    }

    // Test that querying a file status for a removed file referenced by the iterator
    // doesn't cause an error (i.e. the behavior is similar to standalone status/symlink_status).
    // https://github.com/boostorg/filesystem/issues/314
    {
        BOOST_TEST(fs::is_empty(d2));
        create_file(d2 / "file");
        fs::directory_iterator it(d2);
        fs::remove(d2 / "file");

        for (; it != fs::directory_iterator(); ++it)
        {
            it->symlink_status();
            it->status();
            it->refresh();
        }
    }

    { // *i++ must meet the standard's InputIterator requirements
        fs::directory_iterator dir_itr(dir);
        BOOST_TEST(dir_itr != fs::directory_iterator());
        fs::path p = dir_itr->path();
        BOOST_TEST((*dir_itr++).path() == p);
        BOOST_TEST(dir_itr != fs::directory_iterator());
        BOOST_TEST(dir_itr->path() != p);

        // test case reported in comment to SourceForge bug tracker [937606]
        // augmented to test single pass semantics of a copied iterator [#12578]
        fs::directory_iterator itx(dir);
        fs::directory_iterator itx2(itx);
        BOOST_TEST(itx == itx2);
        const fs::path p1 = (*itx++).path();
        BOOST_TEST(itx == itx2);
        BOOST_TEST(itx != fs::directory_iterator());
        const fs::path p2 = (*itx++).path();
        BOOST_TEST(itx == itx2);
        BOOST_TEST(p1 != p2);
        ++itx;
        BOOST_TEST(itx == itx2);
        ++itx;
        BOOST_TEST(itx == itx2);
        BOOST_TEST(itx == fs::directory_iterator());
        BOOST_TEST(itx2 == fs::directory_iterator());
    }

    //  Windows has a tricky special case when just the root-name is given,
    //  causing the rest of the path to default to the current directory.
    //  Reported as S/F bug [ 1259176 ]
    if (platform == "Windows")
    {
        fs::path root_name_path(fs::current_path().root_name());
        fs::directory_iterator itx(root_name_path);
        BOOST_TEST(itx != fs::directory_iterator());
        //      BOOST_TEST(fs::exists((*itx).path()));
        BOOST_TEST(fs::exists(itx->path()));
        BOOST_TEST(itx->path().parent_path() == root_name_path);
        bool found(false);
        do
        {
            if (itx->path().filename() == temp_dir.filename())
                found = true;
        } while (++itx != fs::directory_iterator());
        BOOST_TEST(found);
    }

    // there was an inital bug in directory_iterator that caused premature
    // close of an OS handle. This block will detect regression.
    {
        fs::directory_iterator di;
        {
            di = fs::directory_iterator(dir);
        }
        BOOST_TEST(++di != fs::directory_iterator());
    }

    cout << "  directory_iterator_tests complete" << endl;
}

//  recursive_directory_iterator_tests  ----------------------------------------------//

int walk_tree(bool recursive)
{
    //cout << "    walk_tree" << endl;
    error_code ec;
    int d1f1_count = 0;
    for (fs::recursive_directory_iterator it(dir, recursive ? (fs::directory_options::follow_directory_symlink | fs::directory_options::skip_dangling_symlinks) : fs::directory_options::none);
         it != fs::recursive_directory_iterator();
         it.increment(ec))
    {
        //cout << "      " << it->path() << " : " << ec << endl;
        if (it->path().filename() == "d1f1")
            ++d1f1_count;
    }
    //cout << "      last error : " << ec << endl;
    return d1f1_count;
}

void recursive_directory_iterator_tests()
{
    cout << "recursive_directory_iterator_tests..." << endl;
    BOOST_TEST_EQ(walk_tree(false), 1);
    if (create_symlink_ok)
        BOOST_TEST(walk_tree(true) > 1);

    //  test iterator increment with error_code argument
    cout << "  with error_code argument" << endl;
    boost::system::error_code ec;
    int d1f1_count = 0;
    fs::recursive_directory_iterator it(dir, fs::directory_options::none);
    fs::recursive_directory_iterator it2(it); // test single pass shallow copy semantics
    for (;
         it != fs::recursive_directory_iterator();
         it.increment(ec))
    {
        if (it->path().filename() == "d1f1")
            ++d1f1_count;
        BOOST_TEST(it == it2); // verify single pass shallow copy semantics
    }
    BOOST_TEST(!ec);
    BOOST_TEST_EQ(d1f1_count, 1);
    BOOST_TEST(it == it2); // verify single pass shallow copy semantics

    cout << "  recursive_directory_iterator_tests complete" << endl;
}

//  iterator_status_tests  -----------------------------------------------------------//

void iterator_status_tests()
{
    cout << "iterator_status_tests..." << endl;

    error_code ec;
    // harmless if these fail:
    fs::create_symlink(dir / "f0", dir / "f0_symlink", ec);
    fs::create_symlink(dir / "no such file", dir / "dangling_symlink", ec);
    fs::create_directory_symlink(dir / "d1", dir / "d1_symlink", ec);
    fs::create_directory_symlink(dir / "no such directory", dir / "dangling_directory_symlink", ec);

    for (fs::directory_iterator it(dir);
         it != fs::directory_iterator(); ++it)
    {
        BOOST_TEST(fs::status(it->path()).type() == it->status().type());
        BOOST_TEST(fs::symlink_status(it->path()).type() == it->symlink_status().type());
        if (it->path().filename() == "d1")
        {
            BOOST_TEST(fs::is_directory(it->status()));
            BOOST_TEST(fs::is_directory(it->symlink_status()));
        }
        else if (it->path().filename() == "d2")
        {
            BOOST_TEST(fs::is_directory(it->status()));
            BOOST_TEST(fs::is_directory(it->symlink_status()));
        }
        else if (it->path().filename() == "f0")
        {
            BOOST_TEST(fs::is_regular_file(it->status()));
            BOOST_TEST(fs::is_regular_file(it->symlink_status()));
        }
        else if (it->path().filename() == "f1")
        {
            BOOST_TEST(fs::is_regular_file(it->status()));
            BOOST_TEST(fs::is_regular_file(it->symlink_status()));
        }
        else if (it->path().filename() == "f0_symlink")
        {
            BOOST_TEST(fs::is_regular_file(it->status()));
            BOOST_TEST(fs::is_symlink(it->symlink_status()));
            BOOST_TEST(fs::is_symlink(*it));
        }
        else if (it->path().filename() == "dangling_symlink")
        {
            BOOST_TEST(it->status().type() == fs::file_not_found);
            BOOST_TEST(fs::is_symlink(it->symlink_status()));
        }
        else if (it->path().filename() == "d1_symlink")
        {
            BOOST_TEST(fs::is_directory(it->status()));
            BOOST_TEST(fs::is_symlink(it->symlink_status()));
        }
        else if (it->path().filename() == "dangling_directory_symlink")
        {
            BOOST_TEST(it->status().type() == fs::file_not_found);
            BOOST_TEST(fs::is_symlink(it->symlink_status()));
        }
        //else
        //  cout << "    Note: unexpected directory entry " << it->path().filename() << endl;
    }
}

//  recursive_iterator_status_tests  -------------------------------------------------//

void recursive_iterator_status_tests()
{
    cout << "recursive_iterator_status_tests..." << endl;
    for (fs::recursive_directory_iterator it(dir);
         it != fs::recursive_directory_iterator();
         ++it)
    {
        BOOST_TEST(fs::status(it->path()).type() == it->status().type());
        BOOST_TEST(fs::symlink_status(it->path()).type() == it->symlink_status().type());
    }
}

//  create_hard_link_tests  ----------------------------------------------------------//

void create_hard_link_tests()
{
    cout << "create_hard_link_tests..." << endl;

    fs::path from_ph(dir / "f3");
    fs::path f1x(dir / "f1");

    BOOST_TEST(!fs::exists(from_ph));
    BOOST_TEST(fs::exists(f1x));
    bool create_hard_link_ok(true);
    try
    {
        fs::create_hard_link(f1x, from_ph);
    }
    catch (const fs::filesystem_error& ex)
    {
        create_hard_link_ok = false;
        cout
            << "     *** For information only ***\n"
               "     create_hard_link() attempt failed\n"
               "     filesystem_error.what() reports: "
            << ex.what() << "\n"
                            "     create_hard_link() may not be supported on this file system\n";
    }

    if (create_hard_link_ok)
    {
        cout
            << "     *** For information only ***\n"
               "     create_hard_link() succeeded\n";
        BOOST_TEST(fs::exists(from_ph));
        BOOST_TEST(fs::exists(f1x));
        BOOST_TEST(fs::equivalent(from_ph, f1x));
        BOOST_TEST(fs::hard_link_count(from_ph) == 2);
        BOOST_TEST(fs::hard_link_count(f1x) == 2);
    }

    //  Although tests may be running on a FAT or other file system that does
    //  not support hard links, that is unusual enough that it is considered
    //  a test failure.
    BOOST_TEST(create_hard_link_ok);

    error_code ec;
    fs::create_hard_link(fs::path("doesnotexist"), fs::path("shouldnotwork"), ec);
    BOOST_TEST(ec);
}

//  create_symlink_tests  ------------------------------------------------------------//

void create_symlink_tests()
{
    cout << "create_symlink_tests..." << endl;

    fs::path from_ph(dir / "f4");
    fs::path f1x(dir / "f1");
    BOOST_TEST(!fs::exists(from_ph));
    BOOST_TEST(fs::exists(f1x));
    try
    {
        fs::create_symlink(f1x, from_ph);
    }
    catch (const fs::filesystem_error& ex)
    {
        create_symlink_ok = false;
        cout
            << "     *** For information only ***\n"
               "     create_symlink() attempt failed\n"
               "     filesystem_error.what() reports: "
            << ex.what() << "\n"
                            "     create_symlink() may not be supported on this operating system or file system\n";
    }

    if (create_symlink_ok)
    {
        cout
            << "     *** For information only ***\n"
               "     create_symlink() succeeded\n";
        BOOST_TEST(fs::exists(from_ph));
        BOOST_TEST(fs::is_symlink(from_ph));
        BOOST_TEST(fs::exists(f1x));
        BOOST_TEST(fs::equivalent(from_ph, f1x));
        BOOST_TEST(fs::read_symlink(from_ph) == f1x);

        fs::file_status stat = fs::symlink_status(from_ph);
        BOOST_TEST(fs::exists(stat));
        BOOST_TEST(!fs::is_directory(stat));
        BOOST_TEST(!fs::is_regular_file(stat));
        BOOST_TEST(!fs::is_other(stat));
        BOOST_TEST(fs::is_symlink(stat));

        stat = fs::status(from_ph);
        BOOST_TEST(fs::exists(stat));
        BOOST_TEST(!fs::is_directory(stat));
        BOOST_TEST(fs::is_regular_file(stat));
        BOOST_TEST(!fs::is_other(stat));
        BOOST_TEST(!fs::is_symlink(stat));

        // since create_symlink worked, copy_symlink should also work
        fs::path symlink2_ph(dir / "symlink2");
        fs::copy_symlink(from_ph, symlink2_ph);
        stat = fs::symlink_status(symlink2_ph);
        BOOST_TEST(fs::is_symlink(stat));
        BOOST_TEST(fs::exists(stat));
        BOOST_TEST(!fs::is_directory(stat));
        BOOST_TEST(!fs::is_regular_file(stat));
        BOOST_TEST(!fs::is_other(stat));
    }

    error_code ec = error_code();
    fs::create_symlink("doesnotexist", "", ec);
    BOOST_TEST(ec);
}

//  permissions_tests  ---------------------------------------------------------------//

void permissions_tests()
{
    cout << "permissions_tests..." << endl;

    fs::path p(dir / "permissions.txt");
    create_file(p);

    if (platform == "POSIX")
    {
        cout << "  fs::status(p).permissions() " << std::oct << fs::status(p).permissions()
             << std::dec << endl;
        BOOST_TEST((fs::status(p).permissions() & 0600) == 0600); // 0644, 0664 sometimes returned

        fs::permissions(p, fs::owner_all);
        BOOST_TEST(fs::status(p).permissions() == fs::owner_all);

        fs::permissions(p, fs::add_perms | fs::group_all);
        BOOST_TEST(fs::status(p).permissions() == (fs::owner_all | fs::group_all));

        fs::permissions(p, fs::remove_perms | fs::group_all);
        BOOST_TEST(fs::status(p).permissions() == fs::owner_all);

        // some POSIX platforms cache permissions during directory iteration, some don't
        // so test that iteration finds the correct permissions
        for (fs::directory_iterator itr(dir); itr != fs::directory_iterator(); ++itr)
            if (itr->path().filename() == fs::path("permissions.txt"))
                BOOST_TEST(itr->status().permissions() == fs::owner_all);

        if (create_symlink_ok) // only if symlinks supported
        {
            BOOST_TEST(fs::status(p).permissions() == fs::owner_all);
            fs::path p2(dir / "permissions-symlink.txt");
            fs::create_symlink(p, p2);
            cout << std::oct;
            cout << "   status(p).permissions() " << fs::status(p).permissions() << endl;
            cout << "  status(p2).permissions() " << fs::status(p).permissions() << endl;
            fs::permissions(p2, fs::add_perms | fs::others_read);
            cout << "   status(p).permissions(): " << fs::status(p).permissions() << endl;
            cout << "  status(p2).permissions(): " << fs::status(p2).permissions() << endl;
            cout << std::dec;
        }
    }
    else // Windows
    {
        BOOST_TEST(fs::status(p).permissions() == 0666);
        fs::permissions(p, fs::remove_perms | fs::group_write);
        BOOST_TEST(fs::status(p).permissions() == 0444);
        fs::permissions(p, fs::add_perms | fs::group_write);
        BOOST_TEST(fs::status(p).permissions() == 0666);
    }
}

//  rename_tests  --------------------------------------------------------------------//

void rename_tests()
{
    cout << "rename_tests..." << endl;

    fs::path f1x(dir / "f1");
    BOOST_TEST(fs::exists(f1x));

    // error: rename a non-existent old file
    BOOST_TEST(!fs::exists(d1 / "f99"));
    BOOST_TEST(!fs::exists(d1 / "f98"));
    renamer n1a(d1 / "f99", d1 / "f98");
    BOOST_TEST(CHECK_EXCEPTION(n1a, ENOENT));
    renamer n1b(fs::path(""), d1 / "f98");
    BOOST_TEST(CHECK_EXCEPTION(n1b, ENOENT));

    // error: rename an existing file to ""
    renamer n2(f1x, "");
    BOOST_TEST(CHECK_EXCEPTION(n2, ENOENT));

    // rename an existing file to an existent file
    create_file(dir / "ff1", "ff1");
    create_file(dir / "ff2", "ff2");
    fs::rename(dir / "ff2", dir / "ff1");
    BOOST_TEST(fs::exists(dir / "ff1"));
    verify_file(dir / "ff1", "ff2");
    BOOST_TEST(!fs::exists(dir / "ff2"));

    // rename an existing file to itself
    BOOST_TEST(fs::exists(dir / "f1"));
    fs::rename(dir / "f1", dir / "f1");
    BOOST_TEST(fs::exists(dir / "f1"));

    // error: rename an existing directory to an existing non-empty directory
    BOOST_TEST(fs::exists(dir / "f1"));
    BOOST_TEST(fs::exists(d1 / "f2"));
    // several POSIX implementations (cygwin, openBSD) report ENOENT instead of EEXIST,
    // so we don't verify error type on the following test.
    renamer n3b(dir, d1);
    BOOST_TEST(CHECK_EXCEPTION(n3b, 0));

    //  error: move existing file to a nonexistent parent directory
    BOOST_TEST(!fs::is_directory(dir / "f1"));
    BOOST_TEST(!fs::exists(dir / "d3/f3"));
    renamer n4a(dir / "f1", dir / "d3/f3");
    BOOST_TEST(CHECK_EXCEPTION(n4a, ENOENT));

    // rename existing file in same directory
    BOOST_TEST(fs::exists(d1 / "f2"));
    BOOST_TEST(!fs::exists(d1 / "f50"));
    fs::rename(d1 / "f2", d1 / "f50");
    BOOST_TEST(!fs::exists(d1 / "f2"));
    BOOST_TEST(fs::exists(d1 / "f50"));
    fs::rename(d1 / "f50", d1 / "f2");
    BOOST_TEST(fs::exists(d1 / "f2"));
    BOOST_TEST(!fs::exists(d1 / "f50"));

    // move and rename an existing file to a different directory
    fs::rename(d1 / "f2", d2 / "f3");
    BOOST_TEST(!fs::exists(d1 / "f2"));
    BOOST_TEST(!fs::exists(d2 / "f2"));
    BOOST_TEST(fs::exists(d2 / "f3"));
    BOOST_TEST(!fs::is_directory(d2 / "f3"));
    verify_file(d2 / "f3", "file-f1");
    fs::rename(d2 / "f3", d1 / "f2");
    BOOST_TEST(fs::exists(d1 / "f2"));

    // error: move existing directory to nonexistent parent directory
    BOOST_TEST(fs::exists(d1));
    BOOST_TEST(!fs::exists(dir / "d3/d5"));
    BOOST_TEST(!fs::exists(dir / "d3"));
    renamer n5a(d1, dir / "d3/d5");
    BOOST_TEST(CHECK_EXCEPTION(n5a, ENOENT));

    // rename existing directory
    fs::path d3(dir / "d3");
    BOOST_TEST(fs::exists(d1));
    BOOST_TEST(fs::exists(d1 / "f2"));
    BOOST_TEST(!fs::exists(d3));
    fs::rename(d1, d3);
    BOOST_TEST(!fs::exists(d1));
    BOOST_TEST(fs::exists(d3));
    BOOST_TEST(fs::is_directory(d3));
    BOOST_TEST(!fs::exists(d1 / "f2"));
    BOOST_TEST(fs::exists(d3 / "f2"));
    fs::rename(d3, d1);
    BOOST_TEST(fs::exists(d1));
    BOOST_TEST(fs::exists(d1 / "f2"));
    BOOST_TEST(!fs::exists(d3));

    // rename and move d1 to d2 / "d20"
    BOOST_TEST(fs::exists(d1));
    BOOST_TEST(!fs::exists(d2 / "d20"));
    BOOST_TEST(fs::exists(d1 / "f2"));
    fs::rename(d1, d2 / "d20");
    BOOST_TEST(!fs::exists(d1));
    BOOST_TEST(fs::exists(d2 / "d20"));
    BOOST_TEST(fs::exists(d2 / "d20" / "f2"));
    fs::rename(d2 / "d20", d1);
    BOOST_TEST(fs::exists(d1));
    BOOST_TEST(!fs::exists(d2 / "d20"));
    BOOST_TEST(fs::exists(d1 / "f2"));
}

//  predicate_and_status_tests  ------------------------------------------------------//

void predicate_and_status_tests()
{
    cout << "predicate_and_status_tests..." << endl;

    BOOST_TEST(!fs::exists(ng));
    BOOST_TEST(!fs::is_directory(ng));
    BOOST_TEST(!fs::is_regular_file(ng));
    BOOST_TEST(!fs::is_symlink(ng));
    fs::file_status stat(fs::status(ng));
    BOOST_TEST(fs::type_present(stat));
    BOOST_TEST(fs::permissions_present(stat));
    BOOST_TEST(fs::status_known(stat));
    BOOST_TEST(!fs::exists(stat));
    BOOST_TEST(!fs::is_directory(stat));
    BOOST_TEST(!fs::is_regular_file(stat));
    BOOST_TEST(!fs::is_other(stat));
    BOOST_TEST(!fs::is_symlink(stat));
    stat = fs::status("");
    BOOST_TEST(fs::type_present(stat));
    BOOST_TEST(fs::permissions_present(stat));
    BOOST_TEST(fs::status_known(stat));
    BOOST_TEST(!fs::exists(stat));
    BOOST_TEST(!fs::is_directory(stat));
    BOOST_TEST(!fs::is_regular_file(stat));
    BOOST_TEST(!fs::is_other(stat));
    BOOST_TEST(!fs::is_symlink(stat));

#ifdef BOOST_WINDOWS_API
    stat = fs::status(L"\\System Volume Information");
    BOOST_TEST(fs::type_present(stat));
    BOOST_TEST(fs::permissions_present(stat));
    BOOST_TEST(fs::status_known(stat));
    BOOST_TEST(fs::exists(stat));
    BOOST_TEST(fs::is_directory(stat));
    BOOST_TEST(!fs::is_regular_file(stat));
    BOOST_TEST(!fs::is_other(stat));
    BOOST_TEST(!fs::is_symlink(stat));
#endif // BOOST_WINDOWS_API
}

//  create_directory_tests  ----------------------------------------------------------//

void create_directory_tests()
{
    cout << "create_directory_tests..." << endl;

    error_code ec;
    BOOST_TEST(!fs::create_directory("", ec));
    BOOST_TEST(ec);

#ifdef BOOST_WINDOWS_API
    ec.clear();
    BOOST_TEST(!fs::create_directory(" ", ec)); // OK on Linux
    BOOST_TEST(ec);
#endif

    ec.clear();
    BOOST_TEST(!fs::create_directory("/", ec));
    BOOST_TEST(!ec);
    BOOST_TEST(fs::is_directory("/")); // this is a post-condition

    ec.clear();
    BOOST_TEST(!fs::create_directory(".", ec));
    BOOST_TEST(!ec);

    ec.clear();
    BOOST_TEST(!fs::create_directory("..", ec));
    BOOST_TEST(!ec);

    // create a directory, then check it for consistency
    //   take extra care to report problems, since if this fails
    //   many subsequent tests will fail
    try
    {
        fs::create_directory(dir);
    }

    catch (const fs::filesystem_error& x)
    {
        cout << x.what() << "\n\n"
                            "***** Creating directory "
             << dir << " failed.   *****\n"
                       "***** This is a serious error that will prevent further tests    *****\n"
                       "***** from returning useful results. Further testing is aborted. *****\n\n";
        std::exit(1);
    }

    catch (...)
    {
        cout << "\n\n"
                "***** Creating directory "
             << dir << " failed.   *****\n"
                       "***** This is a serious error that will prevent further tests    *****\n"
                       "***** from returning useful results. Further testing is aborted. *****\n\n";
        std::exit(1);
    }

    BOOST_TEST(fs::exists(dir));
    BOOST_TEST(fs::is_empty(dir));
    BOOST_TEST(fs::is_directory(dir));
    BOOST_TEST(!fs::is_regular_file(dir));
    BOOST_TEST(!fs::is_other(dir));
    BOOST_TEST(!fs::is_symlink(dir));
    fs::file_status stat = fs::status(dir);
    BOOST_TEST(fs::exists(stat));
    BOOST_TEST(fs::is_directory(stat));
    BOOST_TEST(!fs::is_regular_file(stat));
    BOOST_TEST(!fs::is_other(stat));
    BOOST_TEST(!fs::is_symlink(stat));

    cout << "  create_directory_tests complete" << endl;
}

//  current_directory_tests  ---------------------------------------------------------//

void current_directory_tests()
{
    cout << "current_directory_tests..." << endl;

    // set the current directory, then check it for consistency
    fs::path original_dir = fs::current_path();
    BOOST_TEST(dir != original_dir);
    fs::current_path(dir);
    BOOST_TEST(fs::current_path() == dir);
    BOOST_TEST(fs::current_path() != original_dir);
    fs::current_path(original_dir);
    BOOST_TEST(fs::current_path() == original_dir);
    BOOST_TEST(fs::current_path() != dir);

    // make sure the overloads work
    fs::current_path(dir.c_str());
    BOOST_TEST(fs::current_path() == dir);
    BOOST_TEST(fs::current_path() != original_dir);
    fs::current_path(original_dir.string());
    BOOST_TEST(fs::current_path() == original_dir);
    BOOST_TEST(fs::current_path() != dir);
}

//  create_directories_tests  --------------------------------------------------------//

void create_directories_tests()
{
    cout << "create_directories_tests..." << endl;

    error_code ec;
    BOOST_TEST(!fs::create_directories("", ec));
    BOOST_TEST(ec);

#ifdef BOOST_WINDOWS_API
    // Windows only test, since " " is OK on Linux as a directory name
    ec.clear();
    BOOST_TEST(!fs::create_directories(" ", ec));
    BOOST_TEST(ec);
#endif

    ec.clear();
    BOOST_TEST(!fs::create_directories("/", ec));
    BOOST_TEST(!ec);

    ec.clear();
    BOOST_TEST(!fs::create_directories(".", ec));
    BOOST_TEST(!ec);

    ec.clear();
    BOOST_TEST(!fs::create_directories("..", ec));
    BOOST_TEST(!ec);

#ifdef BOOST_POSIX_API
    if (access("/", W_OK) != 0)
    {
        ec.clear();
        BOOST_TEST(!fs::create_directories("/foo", ec)); // may be OK on Windows
                                                         //  but unlikely to be OK on POSIX, unless running as root
        BOOST_TEST(ec);
    }
#endif

    fs::path p = dir / "level1/." / "level2/./.." / "level3/";
    // trailing "/.", "/./..", and "/" in the above elements test ticket #7258 and
    // related issues

    cout << "    p is " << p << endl;
    BOOST_TEST(!fs::exists(p));
    BOOST_TEST(fs::create_directories(p));
    BOOST_TEST(fs::exists(p));
    BOOST_TEST(fs::is_directory(p));

    if (fs::exists("/permissions_test"))
    {
        BOOST_TEST(!fs::create_directories("/permissions_test", ec));
        BOOST_TEST(!fs::create_directories("/permissions_test/another_directory", ec));
        BOOST_TEST(ec);
    }
}

//  resize_file_tests  ---------------------------------------------------------------//

void resize_file_tests()
{
    cout << "resize_file_tests..." << endl;

    fs::path p(dir / "resize_file_test.txt");

    fs::remove(p);
    create_file(p, "1234567890");

    BOOST_TEST(fs::exists(p));
    BOOST_TEST_EQ(fs::file_size(p), 10U);
    fs::resize_file(p, 5);
    BOOST_TEST(fs::exists(p));
    BOOST_TEST_EQ(fs::file_size(p), 5U);
    fs::resize_file(p, 15);
    BOOST_TEST(fs::exists(p));
    BOOST_TEST_EQ(fs::file_size(p), 15U);

    error_code ec;
    fs::resize_file("no such file", 15, ec);
    BOOST_TEST(ec);
}

//  status_of_nonexistent_tests  -----------------------------------------------------//

void status_of_nonexistent_tests()
{
    cout << "status_of_nonexistent_tests..." << endl;
    fs::path p("nosuch");
    BOOST_TEST(!fs::exists(p));
    BOOST_TEST(!fs::is_regular_file(p));
    BOOST_TEST(!fs::is_directory(p));
    BOOST_TEST(!fs::is_symlink(p));
    BOOST_TEST(!fs::is_other(p));

    fs::file_status s = fs::status(p);
    BOOST_TEST(!fs::exists(s));
    BOOST_TEST_EQ(s.type(), fs::file_not_found);
    BOOST_TEST(fs::type_present(s));
    BOOST_TEST(!fs::is_regular_file(s));
    BOOST_TEST(!fs::is_directory(s));
    BOOST_TEST(!fs::is_symlink(s));
    BOOST_TEST(!fs::is_other(s));

    // ticket #12574 was just user confusion, but are the tests are worth keeping
    error_code ec;
    BOOST_TEST(!fs::is_directory(dir / "no-such-directory", ec));
    BOOST_TEST(ec);
    //cout << "error_code value: " << ec.value() << endl;
    ec.clear();
    BOOST_TEST(!fs::is_directory(dir / "no-such-directory" / "bar", ec));
    BOOST_TEST(ec);
    //cout << "error_code value: " << ec.value() << endl;
}

//  status_error_reporting_tests  ----------------------------------------------------//

void status_error_reporting_tests()
{
    cout << "status_error_reporting_tests..." << endl;

    error_code ec;

    // test status, ec, for existing file
    ec.assign(-1, poison_category());
    BOOST_TEST(ec.value() == -1);
    BOOST_TEST(&ec.category() == &poison_category());
    fs::file_status s = fs::status(".", ec);
    BOOST_TEST(ec.value() == 0);
    BOOST_TEST(ec.category() == system_category());
    BOOST_TEST(fs::exists(s));
    BOOST_TEST(fs::is_directory(s));

    // test status, ec, for non-existing file
    fs::path p("nosuch");
    ec.assign(-1, poison_category());
    s = fs::status(p, ec);
    BOOST_TEST(ec.value() != 0);
    BOOST_TEST(ec.category() == system_category());

    BOOST_TEST(!fs::exists(s));
    BOOST_TEST_EQ(s.type(), fs::file_not_found);
    BOOST_TEST(fs::type_present(s));
    BOOST_TEST(!fs::is_regular_file(s));
    BOOST_TEST(!fs::is_directory(s));
    BOOST_TEST(!fs::is_symlink(s));
    BOOST_TEST(!fs::is_other(s));

    // test queries, ec, for existing file
    ec.assign(-1, poison_category());
    BOOST_TEST(fs::exists(".", ec));
    BOOST_TEST(ec.value() == 0);
    BOOST_TEST(ec.category() == system_category());
    ec.assign(-1, poison_category());
    BOOST_TEST(!fs::is_regular_file(".", ec));
    BOOST_TEST(ec.value() == 0);
    BOOST_TEST(ec.category() == system_category());
    ec.assign(-1, poison_category());
    BOOST_TEST(fs::is_directory(".", ec));
    BOOST_TEST(ec.value() == 0);
    BOOST_TEST(ec.category() == system_category());

    // test queries, ec, for non-existing file
    ec.assign(-1, poison_category());
    BOOST_TEST(!fs::exists(p, ec));
    BOOST_TEST(ec.value() != 0);
    BOOST_TEST(ec.category() == system_category());
    ec.assign(-1, poison_category());
    BOOST_TEST(!fs::is_regular_file(p, ec));
    BOOST_TEST(ec.value() != 0);
    BOOST_TEST(ec.category() == system_category());
    ec.assign(-1, poison_category());
    BOOST_TEST(!fs::is_directory(p, ec));
    BOOST_TEST(ec.value() != 0);
    BOOST_TEST(ec.category() == system_category());
}

//  remove_tests  --------------------------------------------------------------------//

void remove_tests(const fs::path& dirx)
{
    cout << "remove_tests..." << endl;

    // remove() file
    fs::path f1x = dirx / "shortlife";
    BOOST_TEST(!fs::exists(f1x));
    create_file(f1x, "");
    BOOST_TEST(fs::exists(f1x));
    BOOST_TEST(!fs::is_directory(f1x));
    BOOST_TEST(fs::remove(f1x));
    BOOST_TEST(!fs::exists(f1x));
    BOOST_TEST(!fs::remove("no-such-file"));
    BOOST_TEST(!fs::remove("no-such-directory/no-such-file"));

#if defined(BOOST_WINDOWS_API)
    // remove() read-only file
    BOOST_TEST(!fs::exists(f1x));
    create_file(f1x, "");
    BOOST_TEST(fs::exists(f1x));
    BOOST_TEST(!fs::is_directory(f1x));
    set_read_only(f1x);
    BOOST_TEST(fs::remove(f1x));
    BOOST_TEST(!fs::exists(f1x));
#endif // defined(BOOST_WINDOWS_API)

    // remove() directory
    fs::path d1x = dirx / "shortlife_dir";
    BOOST_TEST(!fs::exists(d1x));
    fs::create_directory(d1x);
    BOOST_TEST(fs::exists(d1x));
    BOOST_TEST(fs::is_directory(d1x));
    BOOST_TEST(fs::is_empty(d1x));
    bad_remove_dir = dirx;
    BOOST_TEST(CHECK_EXCEPTION(bad_remove, ENOTEMPTY));
    BOOST_TEST(fs::remove(d1x));
    BOOST_TEST(!fs::exists(d1x));
}

//  remove_symlink_tests  ------------------------------------------------------------//

void remove_symlink_tests()
{
    cout << "remove_symlink_tests..." << endl;

    // remove() dangling symbolic link
    fs::path link = dir / "dangling_link";
    fs::remove(link); // remove any residue from past tests
    BOOST_TEST(!fs::is_symlink(link));
    BOOST_TEST(!fs::exists(link));
    fs::create_symlink("nowhere", link);
    BOOST_TEST(!fs::exists(link));
    BOOST_TEST(fs::is_symlink(link));
    BOOST_TEST(fs::remove(link));
    BOOST_TEST(!fs::is_symlink(link));

    // remove() self-refering symbolic link
    link = dir / "link_to_self";
    fs::remove(link); // remove any residue from past tests
    BOOST_TEST(!fs::is_symlink(link));
    BOOST_TEST(!fs::exists(link));
    fs::create_symlink(link, link);
    BOOST_TEST(fs::remove(link));
    BOOST_TEST(!fs::exists(link));
    BOOST_TEST(!fs::is_symlink(link));

    // remove() cyclic symbolic link
    link = dir / "link_to_a";
    fs::path link2 = dir / "link_to_b";
    fs::remove(link);  // remove any residue from past tests
    fs::remove(link2); // remove any residue from past tests
    BOOST_TEST(!fs::is_symlink(link));
    BOOST_TEST(!fs::exists(link));
    fs::create_symlink(link, link2);
    fs::create_symlink(link2, link);
    BOOST_TEST(fs::remove(link));
    BOOST_TEST(fs::remove(link2));
    BOOST_TEST(!fs::exists(link));
    BOOST_TEST(!fs::exists(link2));
    BOOST_TEST(!fs::is_symlink(link));

    // remove() symbolic link to file
    fs::path f1x = dir / "link_target";
    fs::remove(f1x); // remove any residue from past tests
    BOOST_TEST(!fs::exists(f1x));
    create_file(f1x, "");
    BOOST_TEST(fs::exists(f1x));
    BOOST_TEST(!fs::is_directory(f1x));
    BOOST_TEST(fs::is_regular_file(f1x));
    link = dir / "non_dangling_link";
    fs::create_symlink(f1x, link);
    BOOST_TEST(fs::exists(link));
    BOOST_TEST(!fs::is_directory(link));
    BOOST_TEST(fs::is_regular_file(link));
    BOOST_TEST(fs::is_symlink(link));
    BOOST_TEST(fs::remove(link));
    BOOST_TEST(fs::exists(f1x));
    BOOST_TEST(!fs::exists(link));
    BOOST_TEST(!fs::is_symlink(link));
    BOOST_TEST(fs::remove(f1x));
    BOOST_TEST(!fs::exists(f1x));
}

//  remove_all_tests  ----------------------------------------------------------------//

void remove_all_tests(const fs::path& dirx)
{
    cout << "remove_all_tests..." << endl;

    // remove_all() file
    {
        fs::path f1x = dirx / "shortlife";
        BOOST_TEST(!fs::exists(f1x));
        create_file(f1x, "");
        BOOST_TEST(fs::exists(f1x));
        BOOST_TEST(!fs::is_directory(f1x));
        BOOST_TEST_EQ(fs::remove_all(f1x), 1u);
        BOOST_TEST(!fs::exists(f1x));
        BOOST_TEST_EQ(fs::remove_all("no-such-file"), 0u);
        BOOST_TEST_EQ(fs::remove_all("no-such-directory/no-such-file"), 0u);
    }

    // remove_all() directory tree
    {
        unsigned int created_count = 0u;
        fs::path d1x = dirx / "shortlife_dir";
        BOOST_TEST(!fs::exists(d1x));
        fs::create_directory(d1x);
        ++created_count;
        BOOST_TEST(fs::exists(d1x));
        BOOST_TEST(fs::is_directory(d1x));

        fs::path d2x = d1x / "nested_dir";
        BOOST_TEST(!fs::exists(d2x));
        fs::create_directory(d2x);
        ++created_count;
        BOOST_TEST(fs::exists(d2x));
        BOOST_TEST(fs::is_directory(d2x));

        fs::path f1x = d1x / "shortlife";
        BOOST_TEST(!fs::exists(f1x));
        create_file(f1x, "");
        ++created_count;
        BOOST_TEST(fs::exists(f1x));
        BOOST_TEST(!fs::is_directory(f1x));

#if defined(BOOST_WINDOWS_API)
        // read-only file
        fs::path f2x = d1x / "shortlife_ro";
        BOOST_TEST(!fs::exists(f2x));
        create_file(f2x, "");
        ++created_count;
        BOOST_TEST(fs::exists(f2x));
        BOOST_TEST(!fs::is_directory(f2x));
        set_read_only(f2x);
#endif // defined(BOOST_WINDOWS_API)

        boost::uintmax_t removed_count = fs::remove_all(d1x);
        BOOST_TEST_EQ(removed_count, created_count);

        BOOST_TEST(!fs::exists(d1x));
    }
}

//  remove_all_symlink_tests  --------------------------------------------------------//

void remove_all_symlink_tests(const fs::path& dirx)
{
    cout << "remove_all_symlink_tests..." << endl;

    // External directory tree
    fs::path d1x = dirx / "shortlife_dir1";
    BOOST_TEST(!fs::exists(d1x));
    fs::create_directory(d1x);
    BOOST_TEST(fs::exists(d1x));
    BOOST_TEST(fs::is_directory(d1x));

    fs::path f1x = d1x / "shortlife1";
    BOOST_TEST(!fs::exists(f1x));
    create_file(f1x, "");
    BOOST_TEST(fs::exists(f1x));
    BOOST_TEST(!fs::is_directory(f1x));

    fs::path f2x = d1x / "shortlife2";
    BOOST_TEST(!fs::exists(f2x));
    create_file(f2x, "");
    BOOST_TEST(fs::exists(f2x));
    BOOST_TEST(!fs::is_directory(f2x));

    // remove_all() directory tree that has symlinks to external directories
    unsigned int created_count = 0u;
    fs::path d2x = dirx / "shortlife_dir2";
    BOOST_TEST(!fs::exists(d2x));
    fs::create_directory(d2x);
    ++created_count;
    BOOST_TEST(fs::exists(d2x));
    BOOST_TEST(fs::is_directory(d2x));

    fs::path f3x = d2x / "shortlife";
    BOOST_TEST(!fs::exists(f3x));
    create_file(f3x, "");
    ++created_count;
    BOOST_TEST(fs::exists(f3x));
    BOOST_TEST(!fs::is_directory(f3x));

    fs::path d3x = d2x / "symlink_dir";
    BOOST_TEST(!fs::exists(d3x));
    fs::create_directory_symlink(d1x, d3x);
    ++created_count;
    BOOST_TEST(fs::exists(d3x));
    BOOST_TEST(fs::is_symlink(d3x));

#if defined(BOOST_FILESYSTEM_HAS_MKLINK)
    fs::path junc = d2x / "junc";
    fs::path cur_path(fs::current_path());
    fs::current_path(d2x);
    BOOST_TEST(std::system("mklink /J junc ..\\shortlife_dir1") == 0);
    fs::current_path(cur_path);
    ++created_count;
    BOOST_TEST(fs::exists(junc));
#endif

    fs::path f4x = d2x / "symlink";
    BOOST_TEST(!fs::exists(f4x));
    fs::create_symlink(f1x, f4x);
    ++created_count;
    BOOST_TEST(fs::exists(f4x));
    BOOST_TEST(fs::is_symlink(f4x));

    fs::path f5x = d2x / "hardlink";
    BOOST_TEST(!fs::exists(f5x));
    fs::create_hard_link(f2x, f5x);
    ++created_count;
    BOOST_TEST(fs::exists(f5x));
    BOOST_TEST(!fs::is_directory(f5x));

    boost::uintmax_t removed_count = fs::remove_all(d2x);
    BOOST_TEST_EQ(removed_count, created_count);

    BOOST_TEST(!fs::exists(d2x));

    // Check that external directory and file are intact
    BOOST_TEST(fs::exists(d1x));
    BOOST_TEST(fs::is_directory(d1x));
    BOOST_TEST(fs::exists(f1x));
    BOOST_TEST(!fs::is_directory(f1x));
    BOOST_TEST(fs::exists(f2x));
    BOOST_TEST(!fs::is_directory(f2x));

    // Cleanup
    fs::remove_all(d1x);
}

//  absolute_tests  -----------------------------------------------------------------//

void absolute_tests()
{
    cout << "absolute_tests..." << endl;

#if BOOST_FILESYSTEM_VERSION == 3
    BOOST_TEST_EQ(fs::absolute(""), fs::current_path());
    BOOST_TEST_EQ(fs::absolute("", ""), fs::current_path());
#else
    BOOST_TEST_EQ(fs::absolute(""), fs::current_path() / fs::path());
    BOOST_TEST_EQ(fs::absolute("", ""), fs::current_path() / fs::path());
#endif
    BOOST_TEST_EQ(fs::absolute(fs::current_path() / "foo/bar"), fs::current_path() / "foo/bar");
    BOOST_TEST_EQ(fs::absolute("foo"), fs::current_path() / "foo");
    BOOST_TEST_EQ(fs::absolute("foo", fs::current_path()), fs::current_path() / "foo");
    BOOST_TEST_EQ(fs::absolute("bar", "foo"), fs::current_path() / "foo" / "bar");
    BOOST_TEST_EQ(fs::absolute("/foo"), fs::current_path().root_path().string() + "foo");

#ifdef BOOST_WINDOWS_API
    BOOST_TEST_EQ(fs::absolute("a:foo", "b:/bar"), fs::path(L"a:/bar/foo"));
#endif

    // these tests were moved from elsewhere, so may duplicate some of the above tests

    // p.empty()
#if BOOST_FILESYSTEM_VERSION == 3
    BOOST_TEST_EQ(fs::absolute(fs::path(), "//foo/bar"), fs::path("//foo/bar"));
    if (platform == "Windows")
    {
        BOOST_TEST_EQ(fs::absolute(fs::path(), "a:/bar"), fs::path("a:/bar"));
    }
#else
    BOOST_TEST_EQ(fs::absolute(fs::path(), "//foo/bar"), fs::path("//foo/bar/"));
    if (platform == "Windows")
    {
        BOOST_TEST_EQ(fs::absolute(fs::path(), "a:/bar"), fs::path("a:/bar/"));
    }
#endif

    // p.has_root_name()
    //   p.has_root_directory()
    BOOST_TEST_EQ(fs::absolute(fs::path("//foo/bar"), "//uvw/xyz"), fs::path("//foo/bar"));
    if (platform == "Windows")
    {
        BOOST_TEST_EQ(fs::absolute(fs::path("a:/bar"), "b:/xyz"), fs::path("a:/bar"));
    }
    //   !p.has_root_directory()
    BOOST_TEST_EQ(fs::absolute(fs::path("//net"), "//xyz/"), fs::path("//net/"));
#if BOOST_FILESYSTEM_VERSION == 3
    BOOST_TEST_EQ(fs::absolute(fs::path("//net"), "//xyz/abc"), fs::path("//net/abc"));
    BOOST_TEST_EQ(fs::absolute(fs::path("//net"), "//xyz/abc/def"), fs::path("//net/abc/def"));
#else
    BOOST_TEST_EQ(fs::absolute(fs::path("//net"), "//xyz/abc"), fs::path("//net/abc/"));
    BOOST_TEST_EQ(fs::absolute(fs::path("//net"), "//xyz/abc/def"), fs::path("//net/abc/def/"));
#endif
    if (platform == "Windows")
    {
        BOOST_TEST_EQ(fs::absolute(fs::path("a:"), "b:/"), fs::path("a:/"));
#if BOOST_FILESYSTEM_VERSION == 3
        BOOST_TEST_EQ(fs::absolute(fs::path("a:"), "b:/abc"), fs::path("a:/abc"));
        BOOST_TEST_EQ(fs::absolute(fs::path("a:"), "b:/abc/def"), fs::path("a:/abc/def"));
#else
        BOOST_TEST_EQ(fs::absolute(fs::path("a:"), "b:/abc"), fs::path("a:/abc\\"));
        BOOST_TEST_EQ(fs::absolute(fs::path("a:"), "b:/abc/def"), fs::path("a:/abc/def\\"));
#endif
        BOOST_TEST_EQ(fs::absolute(fs::path("a:foo"), "b:/"), fs::path("a:/foo"));
        BOOST_TEST_EQ(fs::absolute(fs::path("a:foo"), "b:/abc"), fs::path("a:/abc/foo"));
        BOOST_TEST_EQ(fs::absolute(fs::path("a:foo"), "b:/abc/def"), fs::path("a:/abc/def/foo"));
        BOOST_TEST_EQ(fs::absolute(fs::path("a:foo/bar"), "b:/"), fs::path("a:/foo/bar"));
        BOOST_TEST_EQ(fs::absolute(fs::path("a:foo/bar"), "b:/abc"), fs::path("a:/abc/foo/bar"));
        BOOST_TEST_EQ(fs::absolute(fs::path("a:foo/bar"), "b:/abc/def"), fs::path("a:/abc/def/foo/bar"));
        BOOST_TEST_EQ(fs::absolute(fs::path("\\\\net\\share\\folder"), "c:\\"), fs::path("\\\\net\\share\\folder"));
    }
    // !p.has_root_name()
    //   p.has_root_directory()
#ifdef BOOST_WINDOWS_API
    BOOST_TEST_EQ(fs::absolute(fs::path("/"), "//xyz/"), fs::path("//xyz/"));
    BOOST_TEST_EQ(fs::absolute(fs::path("/"), "//xyz/abc"), fs::path("//xyz/"));
    BOOST_TEST_EQ(fs::absolute(fs::path("/foo"), "//xyz/"), fs::path("//xyz/foo"));
    BOOST_TEST_EQ(fs::absolute(fs::path("/foo"), "//xyz/abc"), fs::path("//xyz/foo"));
#else
    BOOST_TEST_EQ(fs::absolute(fs::path("/"), "//xyz/"), fs::path("/"));
    BOOST_TEST_EQ(fs::absolute(fs::path("/"), "//xyz/abc"), fs::path("/"));
    BOOST_TEST_EQ(fs::absolute(fs::path("/foo"), "//xyz/"), fs::path("/foo"));
    BOOST_TEST_EQ(fs::absolute(fs::path("/foo"), "//xyz/abc"), fs::path("/foo"));
#endif
    //   !p.has_root_directory()
    BOOST_TEST_EQ(fs::absolute(fs::path("foo"), "//xyz/abc"), fs::path("//xyz/abc/foo"));
    BOOST_TEST_EQ(fs::absolute(fs::path("foo/bar"), "//xyz/abc"), fs::path("//xyz/abc/foo/bar"));
    BOOST_TEST_EQ(fs::absolute(fs::path("."), "//xyz/abc"), fs::path("//xyz/abc/."));
    BOOST_TEST_EQ(fs::absolute(fs::path(".."), "//xyz/abc"), fs::path("//xyz/abc/.."));
    BOOST_TEST_EQ(fs::absolute(fs::path("./foo"), "//xyz/abc"), fs::path("//xyz/abc/./foo"));
    BOOST_TEST_EQ(fs::absolute(fs::path("../foo"), "//xyz/abc"), fs::path("//xyz/abc/../foo"));
    if (platform == "POSIX")
    {
        BOOST_TEST_EQ(fs::absolute(fs::path("foo"), "/abc"), fs::path("/abc/foo"));
        BOOST_TEST_EQ(fs::absolute(fs::path("foo/bar"), "/abc"), fs::path("/abc/foo/bar"));
        BOOST_TEST_EQ(fs::absolute(fs::path("."), "/abc"), fs::path("/abc/."));
        BOOST_TEST_EQ(fs::absolute(fs::path(".."), "/abc"), fs::path("/abc/.."));
        BOOST_TEST_EQ(fs::absolute(fs::path("./foo"), "/abc"), fs::path("/abc/./foo"));
        BOOST_TEST_EQ(fs::absolute(fs::path("../foo"), "/abc"), fs::path("/abc/../foo"));
    }
}

//  canonical_basic_tests  -----------------------------------------------------------//

void canonical_basic_tests()
{
    cout << "canonical_basic_tests..." << endl;

    // error handling
    error_code ec;
    ec.clear();
    fs::canonical("no-such-file", ec);
    BOOST_TEST(ec);
    ec.clear();
    fs::canonical("no-such-file", "x", ec);
    BOOST_TEST(ec);
    bool ok(false);
    try
    {
        fs::canonical("no-such-file");
    }
    catch (const fs::filesystem_error&)
    {
        ok = true;
    }
    BOOST_TEST(ok);

    // Note: Use cacnonical form of the root path in all paths to make path comparisons more stable
    const fs::path cur_path = canonicalize_root_path(fs::current_path());

    // non-symlink tests; also see canonical_symlink_tests()
    BOOST_TEST_EQ(fs::canonical(""), cur_path);
    BOOST_TEST_EQ(fs::canonical("", fs::current_path()), cur_path);
    BOOST_TEST_EQ(fs::canonical("", ""), cur_path);
    BOOST_TEST_EQ(fs::canonical(fs::current_path()), cur_path);
    BOOST_TEST_EQ(fs::canonical(fs::current_path(), ""), cur_path);
    BOOST_TEST_EQ(fs::canonical(fs::current_path(), "no-such-file"), cur_path);

    BOOST_TEST_EQ(fs::canonical("."), cur_path);
    BOOST_TEST_EQ(fs::canonical(".."), cur_path.parent_path());
    BOOST_TEST_EQ(fs::canonical("/"), cur_path.root_path());

    fs::path relative_dir(dir.filename());
    BOOST_TEST_EQ(fs::canonical(dir), dir);
    BOOST_TEST_EQ(fs::canonical(relative_dir), dir);
    BOOST_TEST_EQ(fs::canonical(dir / "f0"), dir / "f0");
    BOOST_TEST_EQ(fs::canonical(relative_dir / "f0"), dir / "f0");
    BOOST_TEST_EQ(fs::canonical(relative_dir / "./f0"), dir / "f0");
    BOOST_TEST_EQ(fs::canonical(relative_dir / "d1/../f0"), dir / "f0");

    // treat parent of root as itself on both POSIX and Windows
    fs::path init(canonicalize_root_path(fs::initial_path()));
    fs::path root(init.root_path());
    fs::path::const_iterator it(init.begin());
    fs::path first; // relative first non-root directory
#ifdef BOOST_WINDOWS_API
    if (!init.empty())
        ++it;
#endif
    if (++it != init.end())
        first = *it;
    fs::path expected(root / first);

    cout << "  init: " << init << endl;
    cout << "  root: " << root << endl;
    cout << "  first: " << first << endl;
    cout << "  expected: " << expected << endl;

    //  ticket 10187 tests
    BOOST_TEST_EQ(fs::canonical(root / "../.." / first), expected);
    BOOST_TEST_EQ(fs::canonical(fs::path("../..") / first, root), expected);
    BOOST_TEST_EQ(fs::canonical(fs::path("/../..") / first, fs::current_path().root_name()), expected);

    //  ticket 9683 test
    BOOST_TEST_EQ(fs::canonical(root / first / "../../../../.."), root);

#ifdef BOOST_WINDOWS_API
    // Test Windows long paths
    fs::path long_path = make_long_path(dir / L"f0");
    BOOST_TEST_EQ(fs::canonical(long_path), long_path);

    // Test that canonical() consistently converts drive letters to upper case.
    // https://github.com/boostorg/filesystem/issues/325
    {
        fs::path::string_type root_path = dir.root_path().native(), lc_root_path, uc_root_path;
        for (fs::path::string_type::value_type c : root_path)
        {
            fs::path::string_type::value_type lc = c, uc = c;
            if (lc >= L'A' && lc <= L'Z')
                lc += L'a' - L'A';
            if (uc >= L'a' && uc <= L'z')
                uc -= L'a' - L'A';
            lc_root_path.push_back(lc);
            uc_root_path.push_back(uc);
        }
        BOOST_TEST_EQ(fs::canonical(fs::path(lc_root_path) / dir.relative_path()), fs::canonical(fs::path(uc_root_path) / dir.relative_path()));
    }
#endif
}

//  canonical_symlink_tests  -----------------------------------------------------------//

void canonical_symlink_tests()
{
    cout << "canonical_symlink_tests..." << endl;

    fs::path relative_dir(dir.filename());
    BOOST_TEST_EQ(fs::canonical(dir / "sym-d1/f2"), d1 / "f2");
    BOOST_TEST_EQ(fs::canonical(relative_dir / "sym-d1/f2"), d1 / "f2");
}

//  copy_file_tests  ------------------------------------------------------------------//

void copy_file_tests(const fs::path& f1x, const fs::path& d1x)
{
    cout << "copy_file_tests..." << endl;

    BOOST_TEST(fs::exists(f1x));
    fs::remove(d1x / "f2"); // remove possible residue from prior testing
    BOOST_TEST(fs::exists(d1x));
    BOOST_TEST(!fs::exists(d1x / "f2"));
    cout << " copy " << f1x << " to " << d1x / "f2" << endl;
    bool file_copied = fs::copy_file(f1x, d1x / "f2");
    cout << " copy complete" << endl;
    BOOST_TEST(file_copied);
    BOOST_TEST(fs::exists(f1x));
    BOOST_TEST(fs::exists(d1x / "f2"));
    BOOST_TEST(!fs::is_directory(d1x / "f2"));
    verify_file(d1x / "f2", "file-f1");

    bool copy_ex_ok = false;
    file_copied = false;
    try
    {
        file_copied = fs::copy_file(f1x, d1x / "f2");
    }
    catch (const fs::filesystem_error&)
    {
        copy_ex_ok = true;
    }
    BOOST_TEST(copy_ex_ok);
    BOOST_TEST(!file_copied);

    file_copied = false;
    copy_ex_ok = false;
    try
    {
        file_copied = fs::copy_file(f1x, d1x / "f2", fs::copy_options::none);
    }
    catch (const fs::filesystem_error&)
    {
        copy_ex_ok = true;
    }
    BOOST_TEST(copy_ex_ok);
    BOOST_TEST(!file_copied);

    fs::remove(d1x / "f2");
    create_file(d1x / "f2", "1234567890");
    BOOST_TEST_EQ(fs::file_size(d1x / "f2"), 10U);
    file_copied = false;
    copy_ex_ok = true;
    try
    {
        file_copied = fs::copy_file(f1x, d1x / "f2", fs::copy_options::skip_existing);
    }
    catch (const fs::filesystem_error&)
    {
        copy_ex_ok = false;
    }
    BOOST_TEST(copy_ex_ok);
    BOOST_TEST(!file_copied);
    BOOST_TEST_EQ(fs::file_size(d1x / "f2"), 10U);
    verify_file(d1x / "f2", "1234567890");

    file_copied = false;
    copy_ex_ok = true;
    try
    {
        file_copied = fs::copy_file(f1x, d1x / "f2-non-existing", fs::copy_options::skip_existing);
    }
    catch (const fs::filesystem_error&)
    {
        copy_ex_ok = false;
    }
    BOOST_TEST(copy_ex_ok);
    BOOST_TEST(file_copied);
    BOOST_TEST_EQ(fs::file_size(d1x / "f2-non-existing"), 7U);
    verify_file(d1x / "f2-non-existing", "file-f1");
    fs::remove(d1x / "f2-non-existing");

    // Sleep for a while so that the last modify time is more recent for new files
#if defined(BOOST_POSIX_API)
    sleep(2);
#else
    Sleep(2000);
#endif

    create_file(d1x / "f2-more-recent", "x");
    BOOST_TEST_EQ(fs::file_size(d1x / "f2-more-recent"), 1U);

    file_copied = false;
    copy_ex_ok = true;
    try
    {
        file_copied = fs::copy_file(d1x / "f2", d1x / "f2-more-recent", fs::copy_options::update_existing);
    }
    catch (const fs::filesystem_error&)
    {
        copy_ex_ok = false;
    }
    BOOST_TEST(copy_ex_ok);
    BOOST_TEST(!file_copied);
    BOOST_TEST_EQ(fs::file_size(d1x / "f2-more-recent"), 1U);
    verify_file(d1x / "f2-more-recent", "x");

    file_copied = false;
    copy_ex_ok = true;
    try
    {
        file_copied = fs::copy_file(d1x / "f2-more-recent", d1x / "f2", fs::copy_options::update_existing);
    }
    catch (const fs::filesystem_error&)
    {
        copy_ex_ok = false;
    }
    BOOST_TEST(copy_ex_ok);
    BOOST_TEST(file_copied);
    BOOST_TEST_EQ(fs::file_size(d1x / "f2"), 1U);
    verify_file(d1x / "f2", "x");
    fs::remove(d1x / "f2-more-recent");

    fs::remove(d1x / "f2");
    create_file(d1x / "f2", "1234567890");
    BOOST_TEST_EQ(fs::file_size(d1x / "f2"), 10U);
    file_copied = false;
    copy_ex_ok = true;
    try
    {
        file_copied = fs::copy_file(f1x, d1x / "f2", fs::copy_options::overwrite_existing);
    }
    catch (const fs::filesystem_error&)
    {
        copy_ex_ok = false;
    }
    BOOST_TEST(copy_ex_ok);
    BOOST_TEST(file_copied);
    BOOST_TEST_EQ(fs::file_size(d1x / "f2"), 7U);
    verify_file(d1x / "f2", "file-f1");

    fs::remove(d1x / "f2");
    file_copied = false;
    copy_ex_ok = true;
    try
    {
        file_copied = fs::copy_file(f1x, d1x / "f2", fs::copy_options::synchronize_data);
    }
    catch (const fs::filesystem_error&)
    {
        copy_ex_ok = false;
    }
    BOOST_TEST(copy_ex_ok);
    BOOST_TEST(file_copied);
    verify_file(d1x / "f2", "file-f1");

    fs::remove(d1x / "f2");
    file_copied = false;
    copy_ex_ok = true;
    try
    {
        file_copied = fs::copy_file(f1x, d1x / "f2", fs::copy_options::synchronize);
    }
    catch (const fs::filesystem_error&)
    {
        copy_ex_ok = false;
    }
    BOOST_TEST(copy_ex_ok);
    BOOST_TEST(file_copied);
    verify_file(d1x / "f2", "file-f1");

    // Test copy_file with special files with generated content. Such files have zero size,
    // but have contents.
    if (fs::is_regular_file("/proc/self/cmdline"))
    {
        file_copied = false;
        copy_ex_ok = true;
        try
        {
            file_copied = fs::copy_file("/proc/self/cmdline", d1x / "cmdline");
        }
        catch (const fs::filesystem_error&)
        {
            copy_ex_ok = false;
        }
        BOOST_TEST(copy_ex_ok);
        BOOST_TEST(file_copied);
        BOOST_TEST_GT(fs::file_size(d1x / "cmdline"), 0u);
    }

#ifdef BOOST_WINDOWS_API
    // Test copying files with multiple NTFS streams
    fs::path multi_stream_path = d1x / "multi-stream";
    fs::path multi_stream_alt_path = d1x / "multi-stream:alt-stream";
    fs::path multi_stream_copy_path = d1x / "multi-stream-copy";
    fs::path multi_stream_alt_copy_path = d1x / "multi-stream-copy:alt-stream";
    create_file(multi_stream_path, "multi-stream:default");
    try
    {
        // Check that the filesystem supports alternate streams
        create_file(multi_stream_alt_path, "multi-stream:alternate");
        verify_file(multi_stream_alt_path, "multi-stream:alternate");

        fs::remove(multi_stream_copy_path);
        copy_ex_ok = true;
        file_copied = false;
        try
        {
            file_copied = fs::copy_file(multi_stream_path, multi_stream_copy_path);
        }
        catch (const fs::filesystem_error&)
        {
            copy_ex_ok = false;
        }
        BOOST_TEST(copy_ex_ok);
        BOOST_TEST(file_copied);
        verify_file(multi_stream_copy_path, "multi-stream:default");
        verify_file(multi_stream_alt_copy_path, "multi-stream:alternate");

        fs::remove(multi_stream_copy_path);
        copy_ex_ok = true;
        file_copied = false;
        try
        {
            file_copied = fs::copy_file(multi_stream_path, multi_stream_copy_path, fs::copy_options::synchronize_data);
        }
        catch (const fs::filesystem_error&)
        {
            copy_ex_ok = false;
        }
        BOOST_TEST(copy_ex_ok);
        BOOST_TEST(file_copied);
        verify_file(multi_stream_copy_path, "multi-stream:default");
        verify_file(multi_stream_alt_copy_path, "multi-stream:alternate");

        fs::remove(multi_stream_copy_path);
        copy_ex_ok = true;
        file_copied = false;
        try
        {
            file_copied = fs::copy_file(multi_stream_path, multi_stream_copy_path, fs::copy_options::synchronize);
        }
        catch (const fs::filesystem_error&)
        {
            copy_ex_ok = false;
        }
        BOOST_TEST(copy_ex_ok);
        BOOST_TEST(file_copied);
        verify_file(multi_stream_copy_path, "multi-stream:default");
        verify_file(multi_stream_alt_copy_path, "multi-stream:alternate");

        fs::remove(multi_stream_copy_path);
    }
    catch (const fs::filesystem_error& e)
    {
        cout << "Multiple streams per file are not supported: " << e.what() << "\nSkipping multi-stream tests..." << endl;
    }
#endif // BOOST_WINDOWS_API
}

//  symlink_status_tests  -------------------------------------------------------------//

void symlink_status_tests()
{
    cout << "symlink_status_tests..." << endl;

    boost::system::error_code ec;

    fs::path dangling_sym(dir / "dangling-sym");
    fs::path dangling_directory_sym(dir / "dangling-directory-sym");
    fs::path sym_d1(dir / "sym-d1");
    fs::path symsym_d1(dir / "symsym-d1");
    fs::path sym_f1(dir / "sym-f1");
    fs::path symsym_f1(dir / "symsym-f1");
    fs::create_symlink("does not exist", dangling_sym);
    fs::create_directory_symlink("does not exist", dangling_directory_sym);
    fs::create_directory_symlink(d1, sym_d1);
    fs::create_directory_symlink(sym_d1, symsym_d1);
    fs::create_symlink(f1, sym_f1);
    fs::create_symlink(sym_f1, symsym_f1);

    //  verify all cases detected as symlinks
    BOOST_TEST_EQ(fs::symlink_status(dangling_sym, ec).type(), fs::symlink_file);
    BOOST_TEST_EQ(fs::symlink_status(dangling_directory_sym, ec).type(), fs::symlink_file);
    BOOST_TEST_EQ(fs::symlink_status(sym_d1, ec).type(), fs::symlink_file);
    BOOST_TEST_EQ(fs::symlink_status(symsym_d1, ec).type(), fs::symlink_file);
    BOOST_TEST_EQ(fs::symlink_status(sym_f1, ec).type(), fs::symlink_file);
    BOOST_TEST_EQ(fs::symlink_status(symsym_f1, ec).type(), fs::symlink_file);

    //  verify all cases resolve to the (possibly recursive) symlink target
    BOOST_TEST_EQ(fs::status(dangling_sym, ec).type(), fs::file_not_found);
    BOOST_TEST_EQ(fs::status(dangling_directory_sym, ec).type(), fs::file_not_found);

    BOOST_TEST_EQ(fs::status(sym_d1, ec).type(), fs::directory_file);
    BOOST_TEST_EQ(fs::status(sym_d1 / "d1f1", ec).type(), fs::regular_file);
    BOOST_TEST_EQ(fs::status(symsym_d1, ec).type(), fs::directory_file);
    BOOST_TEST_EQ(fs::status(symsym_d1 / "d1f1", ec).type(), fs::regular_file);
    BOOST_TEST_EQ(fs::status(sym_f1, ec).type(), fs::regular_file);
    BOOST_TEST_EQ(fs::status(symsym_f1, ec).type(), fs::regular_file);

#ifdef BOOST_WINDOWS_API

    //  On Windows, telling if a filesystem entry is a symlink (or junction which is
    //  treated as a symlink), rather than some other kind of reparse point, requires some
    //  baroque code. See ticket #4663, filesystem objects falsely identified as symlinks.
    //  This test checks two directory entries created by Windows itself to verify
    //  is_symlink() works correctly. Try "dir /A %HOMEPATH%\.." from the command line to
    //  verify this test is valid on your version of Windows. It only works on Vista and
    //  later.

    fs::path users(getenv("HOMEDRIVE"));
    BOOST_TEST(!users.empty());
    users /= "\\Users";
    BOOST_TEST(fs::exists(users));
    BOOST_TEST(fs::exists(users / "All Users"));
    BOOST_TEST(fs::exists(users / "Default User"));
    BOOST_TEST(fs::is_symlink(users / "All Users"));    // dir /A reports <SYMLINKD>
    BOOST_TEST(fs::is_symlink(users / "Default User")); // dir /A reports <JUNCTION>

    fs::file_status stat(fs::symlink_status(L"\\System Volume Information"));
    BOOST_TEST(fs::type_present(stat));
    BOOST_TEST(fs::permissions_present(stat));
    BOOST_TEST(fs::status_known(stat));
    BOOST_TEST(fs::exists(stat));
    BOOST_TEST(fs::is_directory(stat));
    BOOST_TEST(!fs::is_regular_file(stat));
    BOOST_TEST(!fs::is_other(stat));
    BOOST_TEST(!fs::is_symlink(stat));

#endif
}

//  copy_symlink_tests  ---------------------------------------------------------------//

void copy_symlink_tests(const fs::path& f1x, const fs::path& d1x)
{
    cout << "copy_symlink_tests..." << endl;

    BOOST_TEST(fs::exists(f1x));
    BOOST_TEST(fs::exists(d1x));
    fs::path sym1(d1x / "symlink1");
    fs::remove(sym1); // remove possible residue from prior testing
    fs::create_symlink(f1x, sym1);
    BOOST_TEST(fs::exists(sym1));
    BOOST_TEST(fs::is_symlink(sym1));
    fs::path sym2(d1x / "symlink2");
    fs::copy_symlink(sym1, sym2);
    BOOST_TEST(fs::exists(sym2));
    BOOST_TEST(fs::is_symlink(sym2));
    //fs::path sym3(d1x / "symlink3");
    //fs::copy(sym1, sym3);
    //BOOST_TEST(fs::exists(sym3));
    //BOOST_TEST(fs::is_symlink(sym3));

    bool copy_ex_ok = false;
    try
    {
        fs::copy_symlink("no-such-file", "new-symlink1");
    }
    catch (const fs::filesystem_error&)
    {
        copy_ex_ok = true;
    }
    BOOST_TEST(copy_ex_ok);

    copy_ex_ok = false;
    try
    {
        fs::copy_symlink(f1x, "new-symlink2");
    } // should fail; f1x not symlink
    catch (const fs::filesystem_error&)
    {
        copy_ex_ok = true;
    }
    BOOST_TEST(copy_ex_ok);
}

//  creation_time_tests  -------------------------------------------------------------//

void creation_time_tests(const fs::path& dirx)
{
    cout << "creation_time_tests..." << endl;

    fs::path f1x = dirx / "creation_time_file";

    std::time_t start = std::time(nullptr);

    // These pauses are inserted because the test spuriously fails on Windows, presumably because of
    // different converting FILETIME to seconds in time() and Boost.Filesystem or some sort of quirk
    // in the Windows implementation of filesystem API.
#if defined(BOOST_POSIX_API)
    sleep(1);
#else
    Sleep(1000);
#endif
    create_file(f1x, "creation_time_file");
    BOOST_TEST(fs::is_regular_file(f1x));
    try
    {
        std::time_t ft = fs::creation_time(f1x);
#if defined(BOOST_POSIX_API)
        sleep(1);
#else
        Sleep(1000);
#endif
        std::time_t finish = std::time(nullptr);
        cout << "  start time: " << start << ", file creation time: " << ft << ", finish time: " << finish << endl;

        BOOST_TEST(ft >= start && ft <= finish);
    }
    catch (fs::filesystem_error& e)
    {
        if (e.code() == make_error_condition(boost::system::errc::function_not_supported))
        {
            cout << "creation_time is not supported by the current system" << endl;
        }
        else
        {
            cout << "creation_time failed: " << e.what() << endl;
            BOOST_TEST(false);
        }
    }

    fs::remove(f1x);
}

//  symlink_file_size_tests  ---------------------------------------------------------//

void symlink_file_size_tests()
{
    cout << "symlink_file_size_tests..." << endl;

    // Most of these symlinks are already created in symlink_status_tests(), which is run before this test
    fs::path dangling_sym(dir / "dangling-sym");
    fs::path dangling_directory_sym(dir / "dangling-directory-sym");
    fs::path sym_d1(dir / "sym-d1");
    fs::path sym_f1(dir / "sym-f1");
    fs::path sym_d1f1(d1 / "sym-d1f1");
    fs::create_symlink(d1f1, sym_d1f1);

    bool exception_thrown = false;
    try
    {
        fs::file_size(dangling_sym);
    }
    catch (fs::filesystem_error&)
    {
        exception_thrown = true;
    }
    BOOST_TEST(exception_thrown);

    exception_thrown = false;
    try
    {
        fs::file_size(dangling_directory_sym);
    }
    catch (fs::filesystem_error&)
    {
        exception_thrown = true;
    }
    BOOST_TEST(exception_thrown);

    exception_thrown = false;
    try
    {
        fs::file_size(sym_d1);
    }
    catch (fs::filesystem_error&)
    {
        exception_thrown = true;
    }
    BOOST_TEST(exception_thrown);

    boost::uintmax_t size = fs::file_size(sym_f1);
    BOOST_TEST_EQ(size, 7u);

    size = fs::file_size(sym_d1f1);
    BOOST_TEST_EQ(size, 0u);
}

//  symlink_is_empty_tests  ----------------------------------------------------------//

void symlink_is_empty_tests()
{
    cout << "symlink_is_empty_tests..." << endl;

    // These symlinks are already created in symlink_status_tests() and symlink_file_size_tests(), which are run before this test
    fs::path dangling_sym(dir / "dangling-sym");
    fs::path dangling_directory_sym(dir / "dangling-directory-sym");
    fs::path sym_d1(dir / "sym-d1");
    fs::path sym_f1(dir / "sym-f1");
    fs::path sym_d1f1(d1 / "sym-d1f1");

    bool exception_thrown = false;
    try
    {
        fs::is_empty(dangling_sym);
    }
    catch (fs::filesystem_error&)
    {
        exception_thrown = true;
    }
    BOOST_TEST(exception_thrown);

    exception_thrown = false;
    try
    {
        fs::is_empty(dangling_directory_sym);
    }
    catch (fs::filesystem_error&)
    {
        exception_thrown = true;
    }
    BOOST_TEST(exception_thrown);

    bool empty = fs::is_empty(sym_d1);
    BOOST_TEST_EQ(empty, false);

    empty = fs::is_empty(sym_f1);
    BOOST_TEST_EQ(empty, false);

    empty = fs::is_empty(sym_d1f1);
    BOOST_TEST_EQ(empty, true);
}

//  write_time_tests  ----------------------------------------------------------------//

void write_time_tests(const fs::path& dirx)
{
    cout << "write_time_tests..." << endl;

    fs::path f1x = dirx / "foobar2";
    create_file(f1x, "foobar2");
    BOOST_TEST(fs::exists(f1x));
    BOOST_TEST(!fs::is_directory(f1x));
    BOOST_TEST(fs::is_regular_file(f1x));
    BOOST_TEST(fs::file_size(f1x) == 7);
    verify_file(f1x, "foobar2");

    // Some file system report last write time as local (FAT), while
    // others (NTFS) report it as UTC. The C standard does not specify
    // if time_t is local or UTC.

    std::time_t ft = fs::last_write_time(f1x);
    cout << "\n  UTC last_write_time() for a file just created is "
         << std::asctime(std::gmtime(&ft)) << endl;

    std::tm* tmp = std::localtime(&ft);
    cout << "\n  Year is " << tmp->tm_year << endl;
    --tmp->tm_year;
    cout << "  Change year to " << tmp->tm_year << endl;
    fs::last_write_time(f1x, std::mktime(tmp));
    std::time_t ft2 = fs::last_write_time(f1x);
    cout << "  last_write_time() for the file is now "
         << std::asctime(std::gmtime(&ft2)) << endl;
    BOOST_TEST(ft != fs::last_write_time(f1x));

    cout << "\n  Reset to current time" << endl;
    fs::last_write_time(f1x, ft);
    double time_diff = std::difftime(ft, fs::last_write_time(f1x));
    cout
        << "  original last_write_time() - current last_write_time() is "
        << time_diff << " seconds" << endl;
    BOOST_TEST(time_diff >= -60.0 && time_diff <= 60.0);
}

//  platform_specific_tests  ---------------------------------------------------------//

void platform_specific_tests()
{
    // Windows only tests
    if (platform == "Windows")
    {
        cout << "Windows specific tests..." << endl;
        if (!skip_long_windows_tests)
        {
            cout << "  (may take several seconds)" << endl;

            BOOST_TEST(!fs::exists(fs::path("//share-not")));
            BOOST_TEST(!fs::exists(fs::path("//share-not/")));
            BOOST_TEST(!fs::exists(fs::path("//share-not/foo")));
        }
        cout << endl;

        BOOST_TEST(!fs::exists("tools/jam/src/:sys:stat.h"));          // !exists() if ERROR_INVALID_NAME
        BOOST_TEST(!fs::exists(":sys:stat.h"));                        // !exists() if ERROR_INVALID_PARAMETER
        BOOST_TEST(dir.string().size() > 1 && dir.string()[1] == ':'); // verify path includes drive

        BOOST_TEST(fs::system_complete("").empty());
        BOOST_TEST(fs::system_complete("/") == fs::initial_path().root_path());
        BOOST_TEST(fs::system_complete("foo") == fs::initial_path() / "foo");

        fs::path p1(fs::system_complete("/foo"));
        BOOST_TEST_EQ(p1.string().size(), 6U); // this failed during v3 development due to bug
        std::string s1(p1.string());
        std::string s2(fs::initial_path().root_path().string() + "foo");
        BOOST_TEST_EQ(s1, s2);

        BOOST_TEST(fs::system_complete(fs::path(fs::initial_path().root_name())) == fs::initial_path());
        BOOST_TEST(fs::system_complete(fs::path(fs::initial_path().root_name().string() + "foo")).string() == fs::initial_path() / "foo");
        BOOST_TEST_EQ(fs::system_complete(fs::path("c:/")).generic_string(), std::string("c:/"));
        BOOST_TEST_EQ(fs::system_complete(fs::path("c:/foo")).generic_string(), std::string("c:/foo"));
#if BOOST_FILESYSTEM_VERSION == 3
        BOOST_TEST_EQ(fs::system_complete(fs::path("\\\\share")).generic_string(), std::string("//share"));
#else
        BOOST_TEST_EQ(fs::system_complete(fs::path("\\\\share")).generic_string(), std::string("\\\\share"));
#endif

#if defined(BOOST_FILESYSTEM_HAS_MKLINK)
        // Issue 9016 asked that NTFS directory junctions be recognized as directories.
        // That is equivalent to recognizing them as symlinks, and then the normal symlink
        // mechanism takes care of recognizing them as directories.
        //
        // Directory junctions are very similar to symlinks, but have some performance
        // and other advantages over symlinks. They can be created from the command line
        // with "mklink /J junction-name target-path".

        {
            cout << "  directory junction tests..." << endl;
            BOOST_TEST(fs::exists(dir));
            BOOST_TEST(fs::exists(dir / "d1/d1f1"));
            fs::path junc(dir / "junc");
            if (fs::exists(junc))
                fs::remove(junc);
            fs::path new_junc(dir / "new-junc");
            if (fs::exists(new_junc))
                fs::remove(new_junc);

            //cout << "    dir is " << dir << endl;
            //cout << "    junc is " << junc << endl;
            //cout << "    new_junc is " << new_junc << endl;
            //cout << "    current_path() is " << fs::current_path() << endl;

            fs::path cur_path(fs::current_path());
            fs::current_path(dir);
            //cout << "    current_path() is " << fs::current_path() << endl;
            BOOST_TEST(std::system("mklink /J junc d1") == 0);
            //std::system("dir");
            fs::current_path(cur_path);
            //cout << "    current_path() is " << fs::current_path() << endl;

            BOOST_TEST(fs::exists(junc));
            BOOST_TEST(fs::is_symlink(junc));
            BOOST_TEST(fs::is_directory(junc));
            BOOST_TEST(!fs::is_regular_file(junc));
            BOOST_TEST(fs::exists(junc / "d1f1"));
            BOOST_TEST(fs::is_regular_file(junc / "d1f1"));

            int count = 0;
            for (fs::directory_iterator itr(junc); itr != fs::directory_iterator(); ++itr)
            {
                //cout << itr->path() << endl;
                ++count;
            }
            cout << "    iteration count is " << count << endl;
            BOOST_TEST(count > 0);

            fs::rename(junc, new_junc);
            BOOST_TEST(!fs::exists(junc));
            BOOST_TEST(fs::exists(new_junc));
            BOOST_TEST(fs::is_symlink(new_junc));
            BOOST_TEST(fs::is_directory(new_junc));
            BOOST_TEST(!fs::is_regular_file(new_junc));
            BOOST_TEST(fs::exists(new_junc / "d1f1"));
            BOOST_TEST(fs::is_regular_file(new_junc / "d1f1"));

            fs::remove(new_junc);
            BOOST_TEST(!fs::exists(new_junc / "d1f1"));
            BOOST_TEST(!fs::exists(new_junc));
            BOOST_TEST(fs::exists(dir));
            BOOST_TEST(fs::exists(dir / "d1/d1f1"));
        }
#endif // defined(BOOST_FILESYSTEM_HAS_MKLINK)
    }  // Windows

    else if (platform == "POSIX")
    {
        cout << "POSIX specific tests..." << endl;
        BOOST_TEST(fs::system_complete("").empty());
        BOOST_TEST_EQ(fs::initial_path().root_path().string(), std::string("/"));
        BOOST_TEST_EQ(fs::system_complete("/").string(), std::string("/"));
        BOOST_TEST_EQ(fs::system_complete("foo").string(), fs::initial_path().string() + "/foo");
        BOOST_TEST_EQ(fs::system_complete("/foo").string(), fs::initial_path().root_path().string() + "foo");
    } // POSIX
}

//  initial_tests  -------------------------------------------------------------------//

void initial_tests()
{
    cout << "initial_tests..." << endl;

    cout << "  current_path().string() is\n  \""
         << fs::initial_path().string()
         << "\"\n\n";
    BOOST_TEST(fs::initial_path() == fs::current_path());
    BOOST_TEST(fs::initial_path().is_absolute());
    BOOST_TEST(fs::current_path().is_absolute());
    BOOST_TEST_EQ(fs::initial_path().string(), fs::current_path().string());
}

//  space_tests  ---------------------------------------------------------------------//

void space_tests()
{
    cout << "space_tests..." << endl;

    // make some reasonable assuptions for testing purposes
    fs::space_info spi(fs::space(dir));
    BOOST_TEST(spi.capacity > 1000000);
    BOOST_TEST(spi.free > 1000);
    BOOST_TEST(spi.capacity > spi.free);
    BOOST_TEST(spi.free >= spi.available);

    // it is convenient to display space, but older VC++ versions choke
#if !defined(BOOST_MSVC) || _MSC_VER >= 1300 // 1300 == VC++ 7.0
    cout << "   capacity = " << spi.capacity << '\n';
    cout << "       free = " << spi.free << '\n';
    cout << "  available = " << spi.available << '\n';
#endif

    // Test that we can specify path to file
    fs::path file = dir / "file";
    create_file(file);

    fs::space_info spi_file(fs::space(file));
    BOOST_TEST_EQ(spi_file.capacity, spi.capacity);

    fs::remove(file);

    // Test that an error is indicated if a path to a non-existing file is passed
    BOOST_TEST(CHECK_EXCEPTION(bad_space, ENOENT));
}

//  equivalent_tests  ----------------------------------------------------------------//

void equivalent_tests(const fs::path& f1x)
{
    cout << "equivalent_tests..." << endl;

    BOOST_TEST(CHECK_EXCEPTION(bad_equivalent, ENOENT));
    BOOST_TEST(fs::equivalent(f1x, dir / "f1"));
    BOOST_TEST(fs::equivalent(dir, d1 / ".."));
    BOOST_TEST(!fs::equivalent(f1x, dir));
    BOOST_TEST(!fs::equivalent(dir, f1x));
    BOOST_TEST(!fs::equivalent(d1, d2));
#if BOOST_FILESYSTEM_VERSION == 3
    BOOST_TEST(!fs::equivalent(dir, ng));
    BOOST_TEST(!fs::equivalent(ng, dir));
    BOOST_TEST(!fs::equivalent(f1x, ng));
    BOOST_TEST(!fs::equivalent(ng, f1x));
#else
    BOOST_TEST(CHECK_EXCEPTION(([] { fs::equivalent(dir, ng); }), ENOENT));
    BOOST_TEST(CHECK_EXCEPTION(([] { fs::equivalent(ng, dir); }), ENOENT));
    BOOST_TEST(CHECK_EXCEPTION(([&f1x] { fs::equivalent(f1x, ng); }), ENOENT));
    BOOST_TEST(CHECK_EXCEPTION(([&f1x] { fs::equivalent(ng, f1x); }), ENOENT));
#endif
}

//  temp_directory_path_tests  -------------------------------------------------------//
//    contributed by Jeff Flinn

struct guarded_env_var
{
    struct previous_value
    {
        std::string m_name;
        std::string m_string;
        bool m_empty;

        previous_value(const char* name) :
            m_name(name), m_empty(true)
        {
            if (const char* value = getenv(name))
            {
                m_string.assign(value);
                m_empty = false;
            }
            else
            {
                m_empty = true;
            }
        }
        ~previous_value()
        {
            m_empty ? unsetenv_(m_name.c_str()) : setenv_(m_name.c_str(), m_string.c_str(), 1);
        }
    };

    previous_value m_previous_value;

    guarded_env_var(const char* name, const char* value) :
        m_previous_value(name)
    {
        //      std::cout << name << " old value is \"" << getenv(name) << "\"" << std::endl;
        value ? setenv_(name, value, 1) : unsetenv_(name);
        //      std::cout << name << " new value is \"" << getenv(name) << "\"" << std::endl;
    }
};

void temp_directory_path_tests()
{
    {
        cout << "temp_directory_path_tests..." << endl;
        cout << " temp_directory_path() is " << fs::temp_directory_path() << endl;

#if defined(BOOST_WINDOWS_API)

        //**************************************************************************************//
        //   Bug in GCC 4.9 getenv() when !defined(__GXX_EXPERIMENTAL_CXX0X__) makes these
        //   tests meaningless, so skip them
        //**************************************************************************************//

#if defined(__CYGWIN__) && !defined(__GXX_EXPERIMENTAL_CXX0X__) && __GNUC__ == 4
        cout << "Bug in GCC 4.9 getenv() when !defined(__GXX_EXPERIMENTAL_CXX0X__) makes these"
                "tests meaningless, so skip them"
             << endl;
        return;
#endif
        // Test ticket #5300, temp_directory_path failure on Windows with path length > 130.
        // (This test failed prior to the fix being applied.)
        {
            const wchar_t long_name[] =
                L"12345678901234567890123456789012345678901234567890"
                L"12345678901234567890123456789012345678901234567890"
                L"12345678901234567890123456789012345678901234567890#" // total 151 chars
                ;
            fs::path p(temp_dir);
            p /= long_name;
            fs::create_directory(p);

            guarded_env_var tmp_guard("TMP", p.string().c_str());
            error_code ec;
            fs::path tmp_path = fs::temp_directory_path(ec);
            BOOST_TEST(!ec);
            BOOST_TEST_EQ(p, tmp_path);
            fs::remove(p);
        }

        // Test ticket #10388, null character at end of filesystem::temp_directory_path path
        {
            guarded_env_var tmp_guard("TMP", fs::initial_path().string().c_str());

            error_code ec;
            fs::path tmp_path = fs::temp_directory_path(ec);
            BOOST_TEST_EQ(tmp_path, fs::initial_path());
        }

#endif
        BOOST_TEST(!fs::temp_directory_path().empty());
        BOOST_TEST(exists(fs::temp_directory_path()));
        fs::path ph = fs::temp_directory_path() / fs::unique_path("temp_directory_path_test_%%%%_%%%%.txt");
        {
            if (exists(ph))
                remove(ph);
            std::ofstream f(BOOST_FILESYSTEM_C_STR(ph));
            f << "passed";
        }
        BOOST_TEST(exists(ph));
        {
            std::ifstream f(BOOST_FILESYSTEM_C_STR(ph));
            std::string s;
            f >> s;
            BOOST_TEST(s == "passed");
        }
        remove(ph);
        BOOST_TEST(!exists(ph));
    }

    fs::path test_temp_dir = temp_dir;

#if defined(BOOST_POSIX_API)
    {
        struct guarded_tmp_vars
        {
            guarded_env_var m_tmpdir;
            guarded_env_var m_tmp;
            guarded_env_var m_temp;
            guarded_env_var m_tempdir;

            guarded_tmp_vars(const fs::path::value_type* tmpdir, const fs::path::value_type* tmp, const fs::path::value_type* temp, const fs::path::value_type* tempdir) :
                m_tmpdir("TMPDIR", tmpdir), m_tmp("TMP", tmp), m_temp("TEMP", temp), m_tempdir("TEMPDIR", tempdir)
            {
            }
        };

        {
            guarded_tmp_vars vars(test_temp_dir.c_str(), 0, 0, 0);
            fs::path ph = fs::temp_directory_path();
            BOOST_TEST(equivalent(test_temp_dir, ph));
        }
        {
            guarded_tmp_vars vars(0, test_temp_dir.c_str(), 0, 0);
            fs::path ph = fs::temp_directory_path();
            BOOST_TEST(equivalent(test_temp_dir, ph));
        }
        {
            guarded_tmp_vars vars(0, 0, test_temp_dir.c_str(), 0);
            fs::path ph = fs::temp_directory_path();
            BOOST_TEST(equivalent(test_temp_dir, ph));
        }
        {
            guarded_tmp_vars vars(0, 0, 0, test_temp_dir.c_str());
            fs::path ph = fs::temp_directory_path();
            BOOST_TEST(equivalent(test_temp_dir, ph));
        }
    }
#endif

#if defined(BOOST_WINDOWS_API)

    struct guarded_tmp_vars
    {
        guarded_env_var m_tmp;
        guarded_env_var m_temp;
        guarded_env_var m_localappdata;
        guarded_env_var m_userprofile;

        guarded_tmp_vars(const char* tmp, const char* temp, const char* localappdata, const char* userprofile) :
            m_tmp("TMP", tmp), m_temp("TEMP", temp), m_localappdata("LOCALAPPDATA", localappdata), m_userprofile("USERPROFILE", userprofile)
        {
        }
    };

    // test the GetWindowsDirectoryW()/Temp fallback
    {
        guarded_tmp_vars vars(0, 0, 0, 0);
        error_code ec;
        fs::path ph = fs::temp_directory_path(ec);
        BOOST_TEST(!ec);
        cout << "Fallback test, temp_directory_path() returned " << ph << endl;
    }

    {
        guarded_tmp_vars vars(test_temp_dir.string().c_str(), 0, 0, 0);
        fs::path ph = fs::temp_directory_path();
        BOOST_TEST(equivalent(test_temp_dir, ph));
    }
    {
        guarded_tmp_vars vars(0, test_temp_dir.string().c_str(), 0, 0);
        fs::path ph = fs::temp_directory_path();
        BOOST_TEST(equivalent(test_temp_dir, ph));
    }

    fs::create_directory(test_temp_dir / L"Temp");
    {
        guarded_tmp_vars vars(0, 0, test_temp_dir.string().c_str(), 0);
        fs::path ph = fs::temp_directory_path();
        BOOST_TEST(equivalent(test_temp_dir / L"Temp", ph));
        cout << "temp_directory_path() returned " << ph << endl;
    }
    {
        guarded_tmp_vars vars(0, 0, 0, test_temp_dir.string().c_str());
        fs::path ph = fs::temp_directory_path();
        BOOST_TEST(equivalent(test_temp_dir / L"Temp", ph));
        cout << "temp_directory_path() returned " << ph << endl;
    }
#endif
}

//  weakly_canonical_basic_tests  ----------------------------------------------------//

void weakly_canonical_basic_tests()
{
    cout << "weakly_canonical_basic_tests..." << endl;
    cout << "  dir is " << dir << endl;

    // Note: Use cacnonical form of the root path in all paths to make path comparisons more stable
    const fs::path cur_path = canonicalize_root_path(fs::current_path());

    BOOST_TEST_EQ(fs::weakly_canonical("no-such/foo/bar"), cur_path / fs::path("no-such/foo/bar"));
    BOOST_TEST_EQ(fs::weakly_canonical("no-such/foo/../bar"), cur_path / fs::path("no-such/bar"));
    BOOST_TEST_EQ(fs::weakly_canonical(dir), dir);
    BOOST_TEST_EQ(fs::weakly_canonical(dir / "no-such/foo/bar"), dir / "no-such/foo/bar");
    BOOST_TEST_EQ(fs::weakly_canonical(dir / "no-such/foo/../bar"), dir / "no-such/bar");
    BOOST_TEST_EQ(fs::weakly_canonical(dir / "../no-such/foo/../bar"), dir.parent_path() / "no-such/bar");
    BOOST_TEST_EQ(fs::weakly_canonical(dir / "no-such/../f0"), dir / "f0"); // dir / "f0" exists, dir / "no-such" does not
    BOOST_TEST_EQ(fs::weakly_canonical("f0", d1), d1 / "f0");
    BOOST_TEST_EQ(fs::weakly_canonical("./f0", d1), d1 / "f0");
    BOOST_TEST_EQ(fs::weakly_canonical("./foo", d1), d1 / "foo");
    BOOST_TEST_EQ(fs::weakly_canonical("../f0", d1), dir / "f0");
    BOOST_TEST_EQ(fs::weakly_canonical("../foo", d1), dir / "foo");
    BOOST_TEST_EQ(fs::weakly_canonical("..//foo", d1), dir / "foo");

#ifdef BOOST_WINDOWS_API
    BOOST_TEST_EQ(fs::weakly_canonical("c:/no-such/foo/bar"), fs::path("C:/no-such/foo/bar"));

    // Test Windows long paths
    fs::path long_path = make_long_path(dir / L"f0");
    BOOST_TEST_EQ(fs::weakly_canonical(long_path), long_path);

    long_path = make_long_path(dir / L"no-such/foo/bar");
    BOOST_TEST_EQ(fs::weakly_canonical(long_path), long_path);
#endif
}

//  weakly_canonical_symlink_tests  --------------------------------------------------//

void weakly_canonical_symlink_tests()
{
    cout << "weakly_canonical_symlink_tests..." << endl;
    cout << "  dir is " << dir << endl;

    fs::create_directory_symlink(dir / "d1", dir / "sld1");
    BOOST_TEST_EQ(fs::weakly_canonical(dir / "sld1/foo/bar"), dir / "d1/foo/bar");

    BOOST_TEST_EQ(relative(dir / "sld1/foo/bar/baz", dir / "d1/foo"), fs::path("bar/baz"));
}

//  _tests  --------------------------------------------------------------------------//

//void _tests()
//{
//  cout << "_tests..." << endl;
//}

} // unnamed namespace

//------------------------------------------------------------------------------------//
//                                                                                    //
//                                    main                                            //
//                                                                                    //
//------------------------------------------------------------------------------------//

int cpp_main(int argc, char* argv[])
{
// document state of critical macros
#ifdef BOOST_POSIX_API
    cout << "BOOST_POSIX_API is defined\n";
#endif
#ifdef BOOST_WINDOWS_API
    cout << "BOOST_WINDOWS_API is defined\n";
#endif

    for (; argc > 1; --argc, ++argv)
    {
        if (*argv[1] == '-' && *(argv[1] + 1) == 't')
            report_throws = true;
        else if (*argv[1] == '-' && *(argv[1] + 1) == 'x')
            cleanup = false;
        else if (*argv[1] == '-' && *(argv[1] + 1) == 'w')
            skip_long_windows_tests = true;
    }

    // The choice of platform to test is made at runtime rather than compile-time
    // so that compile errors for all platforms will be detected even though
    // only the current platform is runtime tested.
#if defined(BOOST_POSIX_API)
    platform = "POSIX";
#elif defined(BOOST_WINDOWS_API)
    platform = "Windows";
#else
#error neither BOOST_POSIX_API nor BOOST_WINDOWS_API is defined. See boost/system/api_config.hpp
#endif
    cout << "API is " << platform << endl;
    const fs::path ip = fs::initial_path();
    cout << "initial_path() is " << ip << endl;
    do_the_right_thing_tests(); // compile-only tests, but call anyhow to suppress warnings

    for (fs::path::const_iterator it = ip.begin(); it != ip.end(); ++it)
    {
        if (it != ip.begin())
            cout << ", ";
        cout << *it;
    }
    cout << endl;

    // Note: Use cacnonical form of the root path in all subsequent paths to make path comparisons more stable
    dir = canonicalize_root_path(ip / temp_dir);

    if (fs::exists(dir))
    {
        cout << "remove residue from prior failed tests..." << endl;
        fs::remove_all(dir);
    }
    BOOST_TEST(!fs::exists(dir));

    // several functions give unreasonable results if uintmax_t isn't 64-bits
    cout << "sizeof(boost::uintmax_t) = " << sizeof(boost::uintmax_t) << '\n';
    BOOST_TEST(sizeof(boost::uintmax_t) >= 8);

    initial_tests();
    predicate_and_status_tests();
    exception_tests();
    create_directory_tests();
    current_directory_tests();
    space_tests();

    // create a directory tree that can be used by subsequent tests
    //
    //    dir
    //      d1
    //        d1f1       // an empty file
    //      f0           // an empty file
    //      f1           // a file containing "file f1"
    //
    create_tree();

    status_of_nonexistent_tests();
    status_error_reporting_tests();
    directory_iterator_tests();
    create_directories_tests(); // must run AFTER directory_iterator_tests

    bad_create_directory_path = f1;
    BOOST_TEST(CHECK_EXCEPTION(bad_create_directory, EEXIST));
    fs::file_status stat = fs::status(f1);
    BOOST_TEST(fs::status_known(stat));
    BOOST_TEST(fs::exists(stat));
    BOOST_TEST(!fs::is_directory(stat));
    BOOST_TEST(fs::is_regular_file(stat));
    BOOST_TEST(!fs::is_other(stat));
    BOOST_TEST(!fs::is_symlink(stat));

    equivalent_tests(f1);
    create_hard_link_tests();
    create_symlink_tests();
    resize_file_tests();
    absolute_tests();
    canonical_basic_tests();
    weakly_canonical_basic_tests();
    permissions_tests();
    copy_file_tests(f1, d1);
    if (create_symlink_ok) // only if symlinks supported
    {
        symlink_status_tests();
        copy_symlink_tests(f1, d1);
        canonical_symlink_tests();
        weakly_canonical_symlink_tests();
        symlink_file_size_tests();
        symlink_is_empty_tests();
    }
    iterator_status_tests(); // lots of cases by now, so a good time to test
                             //  dump_tree(dir);
    recursive_directory_iterator_tests();
    recursive_iterator_status_tests(); // lots of cases by now, so a good time to test
    rename_tests();
    remove_tests(dir);
    remove_all_tests(dir);
    if (create_symlink_ok) // only if symlinks supported
    {
        remove_symlink_tests();
        remove_all_symlink_tests(dir);
    }
    creation_time_tests(dir);
    write_time_tests(dir);
    temp_directory_path_tests();

    platform_specific_tests(); // do these last since they take a lot of time on Windows,
                               // and that's a pain during manual testing

    cout << "testing complete" << endl;

    // post-test cleanup
    if (cleanup)
    {
        cout << "post-test removal of " << dir << endl;
        BOOST_TEST(fs::remove_all(dir) != 0);
        // above was added just to simplify testing, but it ended up detecting
        // a bug (failure to close an internal search handle).
        cout << "post-test removal complete" << endl;
        //    BOOST_TEST(!fs::exists(dir));  // nice test, but doesn't play well with TortoiseGit cache
    }

    cout << "returning from main()" << endl;
    return ::boost::report_errors();
}