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
|
// filesystem path_unit_test.cpp --------------------------------------------------- //
// Copyright Beman Dawes 2008, 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
// ---------------------------------------------------------------------------------- //
//
// The purpose of this test is to ensure that each function in the public
// interface can be called with arguments of the appropriate types. It does
// not attempt to verify that the full range of values for each argument
// are processed correctly.
//
// For full functionality tests, including probes with many different argument
// values, see path_test.cpp and other test programs.
//
// ---------------------------------------------------------------------------------- //
#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/path.hpp>
#include <boost/filesystem/detail/utf8_codecvt_facet.hpp> // for imbue tests
#include "test_codecvt.hpp" // for codecvt arg tests
#include <boost/detail/lightweight_test_report.hpp>
#include <boost/smart_ptr.hpp> // used constructor tests
#include <boost/functional/hash.hpp>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <cstring>
#include <cwchar>
#include <locale>
#include <list>
namespace fs = boost::filesystem;
namespace bs = boost::system;
using boost::filesystem::path;
using std::cout;
using std::endl;
using std::string;
using std::wstring;
#define CHECK(x) check(x, __FILE__, __LINE__)
#define PATH_IS(a, b) check_path(a, b, __FILE__, __LINE__)
#define NATIVE_IS(p, s, ws) check_native(p, s, ws, __FILE__, __LINE__)
#define IS(a,b) check_equal(a, b, __FILE__, __LINE__)
#if defined(_MSC_VER)
# pragma warning(push) // Save warning settings.
# pragma warning(disable : 4428) // Disable universal-character-name encountered in source warning.
#endif
namespace
{
boost::system::error_code ec;
const boost::system::error_code ok;
const boost::system::error_code ng(-1, boost::system::system_category());
std::string platform(BOOST_PLATFORM);
void check_path(const path& source,
const wstring& expected, const char* file, int line)
{
if (source == expected) return;
++::boost::detail::test_errors();
std::cout << file;
std::wcout << L'(' << line << L"): source.wstring(): \""
<< source.wstring()
<< L"\" != expected: \"" << expected
<< L"\"\n" ;
}
# ifdef BOOST_WINDOWS_API
void check_native(const path& p,
const string&, const wstring& expected, const char* file, int line)
# else
void check_native(const path& p,
const string& expected, const wstring&, const char* file, int line)
# endif
{
if (p.native() == expected) return;
++::boost::detail::test_errors();
std::cout << file << '(' << line << "): native() is not equal expected\n"
" native---: " << std::hex;
path::string_type nat(p.native());
for (path::string_type::const_iterator it = nat.begin(); it != nat.end(); ++it)
std::cout << long(*it) << ' ';
std::cout << "\n expected-: ";
for (path::string_type::const_iterator it = expected.begin(); it != expected.end(); ++it)
std::cout << long(*it) << ' ';
std::cout << std::dec << std::endl;
}
template< class T1, class T2 >
void check_equal(const T1& value,
const T2& expected, const char* file, int line)
{
if (value == expected) return;
++::boost::detail::test_errors();
std::cout << file;
std::wcout << L'(' << line << L"): value: \"" << value
<< L"\" != expected: \"" << expected
<< L"\"\n" ;
}
void check(bool ok_, const char* file, int line)
{
if (ok_) return;
++::boost::detail::test_errors();
std::cout << file << '(' << line << "): test failed\n";
}
string s("string");
wstring ws(L"wstring");
std::list<char> l; // see main() for initialization to s, t, r, i, n, g
std::list<wchar_t> wl; // see main() for initialization to w, s, t, r, i, n, g
std::vector<char> v; // see main() for initialization to f, u, z
std::vector<wchar_t> wv; // see main() for initialization to w, f, u, z
class Base {};
class Derived : public Base {};
void fun(const boost::shared_ptr< Base >&) {}
// test_constructors ---------------------------------------------------------------//
void test_constructors()
{
std::cout << "testing constructors..." << std::endl;
path x0; // default constructor
PATH_IS(x0, L"");
BOOST_TEST_EQ(x0.native().size(), 0U);
path x1(l.begin(), l.end()); // iterator range char
PATH_IS(x1, L"string");
BOOST_TEST_EQ(x1.native().size(), 6U);
path x2(x1); // copy constructor
PATH_IS(x2, L"string");
BOOST_TEST_EQ(x2.native().size(), 6U);
path x3(wl.begin(), wl.end()); // iterator range wchar_t
PATH_IS(x3, L"wstring");
BOOST_TEST_EQ(x3.native().size(), 7U);
// contiguous containers
path x4(string("std::string")); // std::string
PATH_IS(x4, L"std::string");
BOOST_TEST_EQ(x4.native().size(), 11U);
path x5(wstring(L"std::wstring")); // std::wstring
PATH_IS(x5, L"std::wstring");
BOOST_TEST_EQ(x5.native().size(), 12U);
path x4v(v); // std::vector<char>
PATH_IS(x4v, L"fuz");
BOOST_TEST_EQ(x4v.native().size(), 3U);
path x5v(wv); // std::vector<wchar_t>
PATH_IS(x5v, L"wfuz");
BOOST_TEST_EQ(x5v.native().size(), 4U);
path x6("array char"); // array char
PATH_IS(x6, L"array char");
BOOST_TEST_EQ(x6.native().size(), 10U);
path x7(L"array wchar_t"); // array wchar_t
PATH_IS(x7, L"array wchar_t");
BOOST_TEST_EQ(x7.native().size(), 13U);
char char_array[100];
std::strcpy(char_array, "big array char");
path x6o(char_array); // array char, only partially full
PATH_IS(x6o, L"big array char");
BOOST_TEST_EQ(x6o.native().size(), 14U);
wchar_t wchar_array[100];
std::wcscpy(wchar_array, L"big array wchar_t");
path x7o(wchar_array); // array char, only partially full
PATH_IS(x7o, L"big array wchar_t");
BOOST_TEST_EQ(x7o.native().size(), 17U);
path x8(s.c_str()); // const char* null terminated
PATH_IS(x8, L"string");
BOOST_TEST_EQ(x8.native().size(), 6U);
path x9(ws.c_str()); // const wchar_t* null terminated
PATH_IS(x9, L"wstring");
BOOST_TEST_EQ(x9.native().size(), 7U);
path x8nc(const_cast<char*>(s.c_str())); // char* null terminated
PATH_IS(x8nc, L"string");
BOOST_TEST_EQ(x8nc.native().size(), 6U);
path x9nc(const_cast<wchar_t*>(ws.c_str())); // wchar_t* null terminated
PATH_IS(x9nc, L"wstring");
BOOST_TEST_EQ(x9nc.native().size(), 7U);
// non-contiguous containers
path x10(l); // std::list<char>
PATH_IS(x10, L"string");
BOOST_TEST_EQ(x10.native().size(), 6U);
path xll(wl); // std::list<wchar_t>
PATH_IS(xll, L"wstring");
BOOST_TEST_EQ(xll.native().size(), 7U);
// easy-to-make coding errors
// path e1(x0, path::codecvt()); // fails to compile, and that is OK
boost::shared_ptr< Derived > pDerived( new Derived() );
fun( pDerived ); // tests constructor member template enable_if working correctly;
// will fail to compile if enable_if not taking path off the table
}
path x;
path y;
// test_assignments ----------------------------------------------------------------//
void test_assignments()
{
std::cout << "testing assignments..." << std::endl;
x = path("yet another path"); // another path
PATH_IS(x, L"yet another path");
BOOST_TEST_EQ(x.native().size(), 16U);
x = x; // self-assignment
PATH_IS(x, L"yet another path");
BOOST_TEST_EQ(x.native().size(), 16U);
x.assign(l.begin(), l.end()); // iterator range char
PATH_IS(x, L"string");
x.assign(wl.begin(), wl.end()); // iterator range wchar_t
PATH_IS(x, L"wstring");
x = string("std::string"); // container char
PATH_IS(x, L"std::string");
x = wstring(L"std::wstring"); // container wchar_t
PATH_IS(x, L"std::wstring");
x = "array char"; // array char
PATH_IS(x, L"array char");
x = L"array wchar"; // array wchar_t
PATH_IS(x, L"array wchar");
x = s.c_str(); // const char* null terminated
PATH_IS(x, L"string");
x = ws.c_str(); // const wchar_t* null terminated
PATH_IS(x, L"wstring");
}
// test_move_construction_and_assignment -------------------------------------------//
void test_move_construction_and_assignment()
{
std::cout << "testing move_construction_and_assignment..." << std::endl;
# if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
path from("long enough to avoid small object optimization");
path to(std::move(from));
BOOST_TEST(to == "long enough to avoid small object optimization");
if (!from.empty())
cout << "Note: move construction did not result in empty source path" << endl;
path from2("long enough to avoid small object optimization");
path to2;
to2 = std::move(from2);
BOOST_TEST(to2 == "long enough to avoid small object optimization");
if (!from2.empty())
cout << "Note: move assignment did not result in empty rhs path" << endl;
# else
std::cout <<
"Test skipped because compiler does not support move semantics" << std::endl;
# endif
}
// test_appends --------------------------------------------------------------------//
void test_appends()
{
std::cout << "testing appends..." << std::endl;
# ifdef BOOST_WINDOWS_API
# define BOOST_FS_FOO L"/foo\\"
# else // POSIX paths
# define BOOST_FS_FOO L"/foo/"
# endif
x = "/foo";
x /= path(""); // empty path
PATH_IS(x, L"/foo");
x = "/foo";
x /= path("/"); // slash path
PATH_IS(x, L"/foo/");
x = "/foo";
x /= path("/boo"); // slash path
PATH_IS(x, L"/foo/boo");
x = "/foo";
x /= x; // self-append
PATH_IS(x, L"/foo/foo");
x = "/foo";
x /= path("yet another path"); // another path
PATH_IS(x, BOOST_FS_FOO L"yet another path");
x = "/foo";
x.append(l.begin(), l.end()); // iterator range char
PATH_IS(x, BOOST_FS_FOO L"string");
x = "/foo";
x.append(wl.begin(), wl.end()); // iterator range wchar_t
PATH_IS(x, BOOST_FS_FOO L"wstring");
x = "/foo";
x /= string("std::string"); // container char
PATH_IS(x, BOOST_FS_FOO L"std::string");
x = "/foo";
x /= wstring(L"std::wstring"); // container wchar_t
PATH_IS(x, BOOST_FS_FOO L"std::wstring");
x = "/foo";
x /= "array char"; // array char
PATH_IS(x, BOOST_FS_FOO L"array char");
x = "/foo";
x /= L"array wchar"; // array wchar_t
PATH_IS(x, BOOST_FS_FOO L"array wchar");
x = "/foo";
x /= s.c_str(); // const char* null terminated
PATH_IS(x, BOOST_FS_FOO L"string");
x = "/foo";
x /= ws.c_str(); // const wchar_t* null terminated
PATH_IS(x, BOOST_FS_FOO L"wstring");
}
// test_concats --------------------------------------------------------------------//
void test_concats()
{
std::cout << "testing concats..." << std::endl;
x = "/foo";
x += path(""); // empty path
PATH_IS(x, L"/foo");
x = "/foo";
x += path("/"); // slash path
PATH_IS(x, L"/foo/");
x = "/foo";
x += path("boo"); // slash path
PATH_IS(x, L"/fooboo");
x = "foo";
x += x; // self-append
PATH_IS(x, L"foofoo");
x = "foo-";
x += path("yet another path"); // another path
PATH_IS(x, L"foo-yet another path");
x = "foo-";
x.concat(l.begin(), l.end()); // iterator range char
PATH_IS(x, L"foo-string");
x = "foo-";
x.concat(wl.begin(), wl.end()); // iterator range wchar_t
PATH_IS(x, L"foo-wstring");
x = "foo-";
x += string("std::string"); // container char
PATH_IS(x, L"foo-std::string");
x = "foo-";
x += wstring(L"std::wstring"); // container wchar_t
PATH_IS(x, L"foo-std::wstring");
x = "foo-";
x += "array char"; // array char
PATH_IS(x, L"foo-array char");
x = "foo-";
x += L"array wchar"; // array wchar_t
PATH_IS(x, L"foo-array wchar");
x = "foo-";
x += s.c_str(); // const char* null terminated
PATH_IS(x, L"foo-string");
x = "foo-";
x += ws.c_str(); // const wchar_t* null terminated
PATH_IS(x, L"foo-wstring");
x = "foo-";
x += 'x'; // char
PATH_IS(x, L"foo-x");
x = "foo-";
x += L'x'; // wchar
PATH_IS(x, L"foo-x");
}
// test_observers ------------------------------------------------------------------//
void test_observers()
{
std::cout << "testing observers..." << std::endl;
path p0("abc");
CHECK(p0.native().size() == 3);
CHECK(p0.size() == 3);
CHECK(p0.string() == "abc");
CHECK(p0.string().size() == 3);
CHECK(p0.wstring() == L"abc");
CHECK(p0.wstring().size() == 3);
p0 = "";
CHECK(p0.native().size() == 0);
CHECK(p0.size() == 0);
# ifdef BOOST_WINDOWS_API
path p("abc\\def/ghi");
CHECK(std::wstring(p.c_str()) == L"abc\\def/ghi");
CHECK(p.string() == "abc\\def/ghi");
CHECK(p.wstring() == L"abc\\def/ghi");
CHECK(p.generic_path().string() == "abc/def/ghi");
CHECK(p.generic_string() == "abc/def/ghi");
CHECK(p.generic_wstring() == L"abc/def/ghi");
CHECK(p.generic_string<string>() == "abc/def/ghi");
CHECK(p.generic_string<wstring>() == L"abc/def/ghi");
CHECK(p.generic_string<path::string_type>() == L"abc/def/ghi");
# else // BOOST_POSIX_API
path p("abc\\def/ghi");
CHECK(string(p.c_str()) == "abc\\def/ghi");
CHECK(p.string() == "abc\\def/ghi");
CHECK(p.wstring() == L"abc\\def/ghi");
CHECK(p.generic_path().string() == "abc\\def/ghi");
CHECK(p.generic_string() == "abc\\def/ghi");
CHECK(p.generic_wstring() == L"abc\\def/ghi");
CHECK(p.generic_string<string>() == "abc\\def/ghi");
CHECK(p.generic_string<wstring>() == L"abc\\def/ghi");
CHECK(p.generic_string<path::string_type>() == "abc\\def/ghi");
# endif
}
// test_relationals ----------------------------------------------------------------//
void test_relationals()
{
std::cout << "testing relationals..." << std::endl;
boost::hash<path> hash;
# ifdef BOOST_WINDOWS_API
// this is a critical use case to meet user expectations
CHECK(path("c:\\abc") == path("c:/abc"));
CHECK(hash(path("c:\\abc")) == hash(path("c:/abc")));
# endif
const path p("bar");
const path p2("baz");
CHECK(!(p < p));
CHECK(p < p2);
CHECK(!(p2 < p));
CHECK(p < "baz");
CHECK(p < string("baz"));
CHECK(p < L"baz");
CHECK(p < wstring(L"baz"));
CHECK(!("baz" < p));
CHECK(!(string("baz") < p));
CHECK(!(L"baz" < p));
CHECK(!(wstring(L"baz") < p));
CHECK(p == p);
CHECK(!(p == p2));
CHECK(!(p2 == p));
CHECK(p2 == "baz");
CHECK(p2 == string("baz"));
CHECK(p2 == L"baz");
CHECK(p2 == wstring(L"baz"));
CHECK("baz" == p2);
CHECK(string("baz") == p2);
CHECK(L"baz" == p2);
CHECK(wstring(L"baz") == p2);
CHECK(hash(p) == hash(p));
CHECK(hash(p) != hash(p2)); // Not strictly required, but desirable
CHECK(!(p != p));
CHECK(p != p2);
CHECK(p2 != p);
CHECK(p <= p);
CHECK(p <= p2);
CHECK(!(p2 <= p));
CHECK(!(p > p));
CHECK(!(p > p2));
CHECK(p2 > p);
CHECK(p >= p);
CHECK(!(p >= p2));
CHECK(p2 >= p);
}
// test_inserter_and_extractor -----------------------------------------------------//
void test_inserter_and_extractor()
{
std::cout << "testing inserter and extractor..." << std::endl;
path p1("foo bar"); // verify space in path roundtrips per ticket #3863
path p2;
std::stringstream ss;
CHECK(p1 != p2);
ss << p1;
ss >> p2;
CHECK(p1 == p2);
path wp1(L"foo bar");
path wp2;
std::wstringstream wss;
CHECK(wp1 != wp2);
wss << wp1;
wss >> wp2;
CHECK(wp1 == wp2);
}
// test_other_non_members ----------------------------------------------------------//
void test_other_non_members()
{
std::cout << "testing other_non_members..." << std::endl;
path p1("foo");
path p2("bar");
// operator /
CHECK(p1 / p2 == path("foo/bar").make_preferred());
CHECK("foo" / p2 == path("foo/bar").make_preferred());
CHECK(L"foo" / p2 == path("foo/bar").make_preferred());
CHECK(string("foo") / p2 == path("foo/bar").make_preferred());
CHECK(wstring(L"foo") / p2 == path("foo/bar").make_preferred());
CHECK(p1 / "bar" == path("foo/bar").make_preferred());
CHECK(p1 / L"bar" == path("foo/bar").make_preferred());
CHECK(p1 / string("bar") == path("foo/bar").make_preferred());
CHECK(p1 / wstring(L"bar") == path("foo/bar").make_preferred());
swap(p1, p2);
CHECK(p1 == "bar");
CHECK(p2 == "foo");
CHECK(!path("").filename_is_dot());
CHECK(!path("").filename_is_dot_dot());
CHECK(!path("..").filename_is_dot());
CHECK(!path(".").filename_is_dot_dot());
CHECK(!path("...").filename_is_dot_dot());
CHECK(path(".").filename_is_dot());
CHECK(path("..").filename_is_dot_dot());
CHECK(path("/.").filename_is_dot());
CHECK(path("/..").filename_is_dot_dot());
CHECK(!path("a.").filename_is_dot());
CHECK(!path("a..").filename_is_dot_dot());
// edge cases
CHECK(path("foo/").filename() == path("."));
CHECK(path("foo/").filename_is_dot());
CHECK(path("/").filename() == path("/"));
CHECK(!path("/").filename_is_dot());
# ifdef BOOST_WINDOWS_API
CHECK(path("c:.").filename() == path("."));
CHECK(path("c:.").filename_is_dot());
CHECK(path("c:/").filename() == path("/"));
CHECK(!path("c:\\").filename_is_dot());
# else
CHECK(path("c:.").filename() == path("c:."));
CHECK(!path("c:.").filename_is_dot());
CHECK(path("c:/").filename() == path("."));
CHECK(path("c:/").filename_is_dot());
# endif
// check that the implementation code to make the edge cases above work right
// doesn't cause some non-edge cases to fail
CHECK(path("c:").filename() != path("."));
CHECK(!path("c:").filename_is_dot());
// examples from reference.html
std::cout << path(".").filename_is_dot(); // outputs 1
std::cout << path("/.").filename_is_dot(); // outputs 1
std::cout << path("foo/.").filename_is_dot(); // outputs 1
std::cout << path("foo/").filename_is_dot(); // outputs 1
std::cout << path("/").filename_is_dot(); // outputs 0
std::cout << path("/foo").filename_is_dot(); // outputs 0
std::cout << path("/foo.").filename_is_dot(); // outputs 0
std::cout << path("..").filename_is_dot(); // outputs 0
cout << std::endl;
}
// // test_modifiers ------------------------------------------------------------------//
//
// void test_modifiers()
// {
// std::cout << "testing modifiers..." << std::endl;
//
// }
// test_iterators ------------------------------------------------------------------//
void test_iterators()
{
std::cout << "testing iterators..." << std::endl;
path p1;
CHECK(p1.begin() == p1.end());
path p2("/");
CHECK(p2.begin() != p2.end());
CHECK(*p2.begin() == "/");
CHECK(++p2.begin() == p2.end());
path p3("foo/bar/baz");
path::iterator it(p3.begin());
CHECK(p3.begin() != p3.end());
CHECK(*it == "foo");
CHECK(*++it == "bar");
CHECK(*++it == "baz");
CHECK(*--it == "bar");
CHECK(*--it == "foo");
CHECK(*++it == "bar");
CHECK(*++it == "baz");
CHECK(++it == p3.end());
}
// test_reverse_iterators ----------------------------------------------------------//
void test_reverse_iterators()
{
std::cout << "testing reverse_iterators..." << std::endl;
path p1;
CHECK(p1.rbegin() == p1.rend());
path p2("/");
CHECK(p2.rbegin() != p2.rend());
CHECK(*p2.rbegin() == "/");
CHECK(++p2.rbegin() == p2.rend());
path p3("foo/bar/baz");
path::reverse_iterator it(p3.rbegin());
CHECK(p3.rbegin() != p3.rend());
CHECK(*it == "baz");
CHECK(*++it == "bar");
CHECK(*++it == "foo");
CHECK(*--it == "bar");
CHECK(*--it == "baz");
CHECK(*++it == "bar");
CHECK(*++it == "foo");
CHECK(++it == p3.rend());
}
// test_modifiers ------------------------------------------------------------------//
void test_modifiers()
{
std::cout << "testing modifiers..." << std::endl;
CHECK(path("").remove_filename() == "");
CHECK(path("foo").remove_filename() == "");
CHECK(path("/foo").remove_filename() == "/");
CHECK(path("foo/bar").remove_filename() == "foo");
BOOST_TEST_EQ(path("foo/bar/").remove_filename(), path("foo/bar"));
BOOST_TEST_EQ(path(".").remove_filename(), path(""));
BOOST_TEST_EQ(path("./.").remove_filename(), path("."));
BOOST_TEST_EQ(path("/.").remove_filename(), path("/"));
BOOST_TEST_EQ(path("..").remove_filename(), path(""));
BOOST_TEST_EQ(path("../..").remove_filename(), path(".."));
BOOST_TEST_EQ(path("/..").remove_filename(), path("/"));
}
// test_decompositions -------------------------------------------------------------//
void test_decompositions()
{
std::cout << "testing decompositions..." << std::endl;
CHECK(path("").root_name().string() == "");
CHECK(path("foo").root_name().string() == "");
CHECK(path("/").root_name().string() == "");
CHECK(path("/foo").root_name().string() == "");
CHECK(path("//netname").root_name().string() == "//netname");
CHECK(path("//netname/foo").root_name().string() == "//netname");
CHECK(path("").root_directory().string() == "");
CHECK(path("foo").root_directory().string() == "");
CHECK(path("/").root_directory().string() == "/");
CHECK(path("/foo").root_directory().string() == "/");
CHECK(path("//netname").root_directory().string() == "");
CHECK(path("//netname/foo").root_directory().string() == "/");
CHECK(path("").root_path().string() == "");
CHECK(path("/").root_path().string() == "/");
CHECK(path("/foo").root_path().string() == "/");
CHECK(path("//netname").root_path().string() == "//netname");
CHECK(path("//netname/foo").root_path().string() == "//netname/");
# ifdef BOOST_WINDOWS_API
CHECK(path("c:/foo").root_path().string() == "c:/");
# endif
CHECK(path("").relative_path().string() == "");
CHECK(path("/").relative_path().string() == "");
CHECK(path("/foo").relative_path().string() == "foo");
CHECK(path("").parent_path().string() == "");
CHECK(path("/").parent_path().string() == "");
CHECK(path("/foo").parent_path().string() == "/");
CHECK(path("/foo/bar").parent_path().string() == "/foo");
CHECK(path("/foo/bar/baz.zoo").filename().string() == "baz.zoo");
CHECK(path("/foo/bar/baz.zoo").stem().string() == "baz");
CHECK(path("/foo/bar.woo/baz").stem().string() == "baz");
CHECK(path("foo.bar.baz.tar.bz2").extension().string() == ".bz2");
CHECK(path("/foo/bar/baz.zoo").extension().string() == ".zoo");
CHECK(path("/foo/bar.woo/baz").extension().string() == "");
}
// test_queries --------------------------------------------------------------------//
void test_queries()
{
std::cout << "testing queries..." << std::endl;
path p1("");
path p2("//netname/foo.doo");
CHECK(p1.empty());
CHECK(!p1.has_root_path());
CHECK(!p1.has_root_name());
CHECK(!p1.has_root_directory());
CHECK(!p1.has_relative_path());
CHECK(!p1.has_parent_path());
CHECK(!p1.has_filename());
CHECK(!p1.has_stem());
CHECK(!p1.has_extension());
CHECK(!p1.is_absolute());
CHECK(p1.is_relative());
CHECK(!p2.empty());
CHECK(p2.has_root_path());
CHECK(p2.has_root_name());
CHECK(p2.has_root_directory());
CHECK(p2.has_relative_path());
CHECK(p2.has_parent_path());
CHECK(p2.has_filename());
CHECK(p2.has_stem());
CHECK(p2.has_extension());
CHECK(p2.is_absolute());
CHECK(!p2.is_relative());
}
// test_imbue_locale ---------------------------------------------------------------//
void test_imbue_locale()
{
std::cout << "testing imbue locale..." << std::endl;
// weak test case for before/after states since we don't know what characters the
// default locale accepts.
path before("abc");
// So that tests are run with known encoding, use Boost UTF-8 codecvt
// \u2722 and \xE2\x9C\xA2 are UTF-16 and UTF-8 FOUR TEARDROP-SPOKED ASTERISK
std::locale global_loc = std::locale();
std::locale loc(global_loc, new fs::detail::utf8_codecvt_facet);
std::cout << " imbuing locale ..." << std::endl;
std::locale old_loc = path::imbue(loc);
std::cout << " testing with the imbued locale ..." << std::endl;
path p2("\xE2\x9C\xA2");
CHECK(p2.wstring().size() == 1);
CHECK(p2.wstring()[0] == 0x2722);
std::cout << " imbuing the original locale ..." << std::endl;
path::imbue(old_loc);
std::cout << " testing with the original locale ..." << std::endl;
path after("abc");
CHECK(before == after);
std::cout << " locale testing complete" << std::endl;
}
// test_codecvt_argument -----------------------------------------------------------//
void test_codecvt_argument()
{
std::cout << "testing codecvt arguments..." << std::endl;
const char * c1 = "a1";
const std::string s1(c1);
const std::wstring ws1(L"b2"); // off-by-one mimics test_codecvt
const std::string s2("y8");
const std::wstring ws2(L"z9");
test_codecvt cvt; // produces off-by-one values that will always differ from
// the system's default locale codecvt facet
int t = 0;
// constructors
std::cout << " constructors test " << ++t << std::endl;
path p(c1, cvt);
NATIVE_IS(p, s1, ws1);
std::cout << " test " << ++t << std::endl;
path p1(s1.begin(), s1.end(), cvt);
NATIVE_IS(p1, s1, ws1);
std::cout << " test " << ++t << std::endl;
path p2(ws2, cvt);
NATIVE_IS(p2, s2, ws2);
std::cout << " test " << ++t << std::endl;
path p3(ws2.begin(), ws2.end(), cvt);
NATIVE_IS(p3, s2, ws2);
// path p2(p1, cvt); // fails to compile, and that is OK
// assigns
p1.clear();
std::cout << " assigns test " << ++t << std::endl;
p1.assign(s1,cvt);
NATIVE_IS(p1, s1, ws1);
p1.clear();
std::cout << " test " << ++t << std::endl;
p1.assign(s1.begin(), s1.end(), cvt);
NATIVE_IS(p1, s1, ws1);
// p1.assign(p, cvt); // fails to compile, and that is OK
// appends
p1.clear();
std::cout << " appends test " << ++t << std::endl;
p1.append(s1,cvt);
NATIVE_IS(p1, s1, ws1);
p1.clear();
std::cout << " test " << ++t << std::endl;
p1.append(s1.begin(), s1.end(), cvt);
NATIVE_IS(p1, s1, ws1);
// p1.append(p, cvt); // fails to compile, and that is OK
// native observers
std::cout << " native observers test " << ++t << std::endl;
CHECK(p.string<std::string>(cvt) == s1);
std::cout << " test " << ++t << std::endl;
CHECK(p.string(cvt) == s1);
std::cout << " test " << ++t << std::endl;
CHECK(p.string<std::wstring>(cvt) == ws1);
std::cout << " test " << ++t << std::endl;
CHECK(p.wstring(cvt) == ws1);
// generic observers
std::cout << " generic observers test " << ++t << std::endl;
CHECK(p.generic_string<std::string>(cvt) == s1);
std::cout << " test " << ++t << std::endl;
CHECK(p.generic_string(cvt) == s1);
std::cout << " test " << ++t << std::endl;
CHECK(p.generic_string<std::wstring>(cvt) == ws1);
std::cout << " test " << ++t << std::endl;
CHECK(p.generic_wstring(cvt) == ws1);
std::cout << " codecvt arguments testing complete" << std::endl;
}
// test_overloads ------------------------------------------------------------------//
void test_overloads()
{
std::cout << "testing overloads..." << std::endl;
std::string sto("hello");
const char a[] = "goodbye";
path p1(sto);
path p2(sto.c_str());
path p3(a);
path p4("foo");
std::wstring wsto(L"hello");
const wchar_t wa[] = L"goodbye";
path wp1(wsto);
path wp2(wsto.c_str());
path wp3(wa);
path wp4(L"foo");
}
// test_error_handling -------------------------------------------------------------//
class error_codecvt
: public std::codecvt< wchar_t, char, std::mbstate_t >
{
public:
explicit error_codecvt()
: std::codecvt<wchar_t, char, std::mbstate_t>() {}
protected:
virtual bool do_always_noconv() const throw() { return false; }
virtual int do_encoding() const throw() { return 0; }
virtual std::codecvt_base::result do_in(std::mbstate_t&,
const char*, const char*, const char*&,
wchar_t*, wchar_t*, wchar_t*&) const
{
static std::codecvt_base::result r = std::codecvt_base::noconv;
if (r == std::codecvt_base::partial) r = std::codecvt_base::error;
else if (r == std::codecvt_base::error) r = std::codecvt_base::noconv;
else r = std::codecvt_base::partial;
return r;
}
virtual std::codecvt_base::result do_out(std::mbstate_t &,
const wchar_t*, const wchar_t*, const wchar_t*&,
char*, char*, char*&) const
{
static std::codecvt_base::result r = std::codecvt_base::noconv;
if (r == std::codecvt_base::partial) r = std::codecvt_base::error;
else if (r == std::codecvt_base::error) r = std::codecvt_base::noconv;
else r = std::codecvt_base::partial;
return r;
}
virtual std::codecvt_base::result do_unshift(std::mbstate_t&,
char*, char*, char* &) const { return ok; }
virtual int do_length(std::mbstate_t &,
const char*, const char*, std::size_t) const { return 0; }
virtual int do_max_length() const throw () { return 0; }
};
void test_error_handling()
{
std::cout << "testing error handling..." << std::endl;
std::locale global_loc = std::locale();
std::locale loc(global_loc, new error_codecvt);
std::cout << " imbuing error locale ..." << std::endl;
std::locale old_loc = path::imbue(loc);
// These tests rely on a path constructor that fails in the locale conversion.
// Thus construction has to call codecvt. Force that by using a narrow string
// for Windows, and a wide string for POSIX.
# ifdef BOOST_WINDOWS_API
# define STRING_FOO_ "foo"
# else
# define STRING_FOO_ L"foo"
# endif
{
std::cout << " testing std::codecvt_base::partial error..." << std::endl;
bool exception_thrown (false);
try { path(STRING_FOO_); }
catch (const bs::system_error & ex)
{
exception_thrown = true;
BOOST_TEST_EQ(ex.code(), bs::error_code(std::codecvt_base::partial,
fs::codecvt_error_category()));
}
catch (...) { std::cout << "***** unexpected exception type *****" << std::endl; }
BOOST_TEST(exception_thrown);
}
{
std::cout << " testing std::codecvt_base::error error..." << std::endl;
bool exception_thrown (false);
try { path(STRING_FOO_); }
catch (const bs::system_error & ex)
{
exception_thrown = true;
BOOST_TEST_EQ(ex.code(), bs::error_code(std::codecvt_base::error,
fs::codecvt_error_category()));
}
catch (...) { std::cout << "***** unexpected exception type *****" << std::endl; }
BOOST_TEST(exception_thrown);
}
{
std::cout << " testing std::codecvt_base::noconv error..." << std::endl;
bool exception_thrown (false);
try { path(STRING_FOO_); }
catch (const bs::system_error & ex)
{
exception_thrown = true;
BOOST_TEST_EQ(ex.code(), bs::error_code(std::codecvt_base::noconv,
fs::codecvt_error_category()));
}
catch (...) { std::cout << "***** unexpected exception type *****" << std::endl; }
BOOST_TEST(exception_thrown);
}
std::cout << " restoring original locale ..." << std::endl;
path::imbue(old_loc);
std::cout << " testing error handling complete" << std::endl;
}
# if 0
// // test_locales --------------------------------------------------------------------//
//
// void test_locales()
// {
// std::cout << "testing locales..." << std::endl;
//
// }
// test_user_supplied_type ---------------------------------------------------------//
typedef std::basic_string<int> user_string;
} // unnamed namespace
namespace boost
{
namespace filesystem
{
namespace path_traits
{
template<> struct is_iterator<const user_string::value_type *> { static const bool value = true; };
template<> struct is_iterator<user_string::value_type *> { static const bool value = true; };
template<> struct is_iterator<user_string::iterator> { static const bool value = true; };
template<> struct is_iterator<user_string::const_iterator> { static const bool value = true; };
template<> struct is_container<user_string> { static const bool value = true; };
template<>
void append<user_string::value_type>(const user_string::value_type * begin,
const user_string::value_type * end, string_type & target, system::error_code & ec)
{
for (; begin != end && *begin; ++begin)
target += *begin + 1; // change so that results distinguishable from char cvts
}
# ifdef __GNUC__
// This specialization shouldn't be needed, and VC++, Intel, and others work
// fine without it. But gcc 4.3.2, and presumably other versions, need it.
template<>
void append<user_string::value_type>(const user_string::value_type * begin,
string_type & target, system::error_code & ec)
{
path_traits::append<user_string::value_type>(begin,
static_cast<const user_string::value_type *>(0), target, ec);
}
# endif
template<>
user_string convert<user_string>(const string_type & source,
system::error_code & ec)
{
user_string temp;
for (string_type::const_iterator it = source.begin();
it != source.end(); ++it)
temp += *it - 1;
return temp;
}
} // namespace path_traits
} // namespace filesystem
} // namespace boost
namespace
{
void test_user_supplied_type()
{
std::cout << "testing user supplied type..." << std::endl;
user_string::value_type usr_c_str[] = { 'a', 'b', 'c', 0 };
user_string usr(usr_c_str);
path p1(usr.c_str());
CHECK(p1 == path("bcd"));
CHECK(p1 == "bcd");
user_string s1(p1.string<user_string>());
CHECK(s1 == usr);
}
# endif
inline const char* macro_value(const char* name, const char* value)
{
static const char* no_value = "[no value]";
static const char* not_defined = "[not defined]";
//if (0 != strcmp(name, value + 1)) // macro is defined
//{
// if (value[1])
// return value;
// else
// return no_value;
//}
//return not_defined;
return 0 == strcmp(name, value + 1)
? not_defined
: (value[1] ? value : no_value);
}
#define BOOST_MACRO_VALUE(X) macro_value(#X, BOOST_STRINGIZE(=X))
} // unnamed namespace
//--------------------------------------------------------------------------------------//
// //
// main //
// //
//--------------------------------------------------------------------------------------//
int test_main(int, char*[])
{
// document state of critical macros
#ifdef BOOST_POSIX_API
cout << "BOOST_POSIX_API" << endl;
BOOST_TEST(path::preferred_separator == '/');
#endif
#ifdef BOOST_WINDOWS_API
cout << "BOOST_WINDOWS_API" << endl;
BOOST_TEST(path::preferred_separator == '\\');
#endif
cout << "BOOST_FILESYSTEM_DECL "
<< BOOST_MACRO_VALUE(BOOST_FILESYSTEM_DECL) << endl;
//#ifdef BOOST_FILESYSTEM_DECL
// cout << "BOOST_FILESYSTEM_DECL is defined as "
// << BOOST_STRINGIZE(BOOST_FILESYSTEM_DECL) << endl;
//#else
// cout << "BOOST_FILESYSTEM_DECL is not defined" << endl;
//#endif
l.push_back('s');
l.push_back('t');
l.push_back('r');
l.push_back('i');
l.push_back('n');
l.push_back('g');
wl.push_back(L'w');
wl.push_back(L's');
wl.push_back(L't');
wl.push_back(L'r');
wl.push_back(L'i');
wl.push_back(L'n');
wl.push_back(L'g');
v.push_back('f');
v.push_back('u');
v.push_back('z');
wv.push_back(L'w');
wv.push_back(L'f');
wv.push_back(L'u');
wv.push_back(L'z');
test_overloads();
test_constructors();
test_assignments();
test_move_construction_and_assignment();
test_appends();
test_concats();
test_modifiers();
test_observers();
test_relationals();
test_inserter_and_extractor();
test_other_non_members();
test_iterators();
test_reverse_iterators();
test_decompositions();
test_queries();
test_imbue_locale();
test_codecvt_argument();
test_error_handling();
#if 0
test_user_supplied_type();
#endif
std::string foo("\\abc");
const char* bar = "/abc";
if (foo == bar)
cout << "unintended consequence\n";
return ::boost::report_errors();
}
|