]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/filesystem/test/path_unit_test.cpp
1 // filesystem path_unit_test.cpp --------------------------------------------------- //
3 // Copyright Beman Dawes 2008, 2009
5 // Distributed under the Boost Software License, Version 1.0.
6 // See http://www.boost.org/LICENSE_1_0.txt
8 // Library home page: http://www.boost.org/libs/filesystem
10 // ---------------------------------------------------------------------------------- //
12 // The purpose of this test is to ensure that each function in the public
13 // interface can be called with arguments of the appropriate types. It does
14 // not attempt to verify that the full range of values for each argument
15 // are processed correctly.
17 // For full functionality tests, including probes with many different argument
18 // values, see path_test.cpp and other test programs.
20 // ---------------------------------------------------------------------------------- //
22 #include <boost/config/warning_disable.hpp>
24 // See deprecated_test for tests of deprecated features
25 #ifndef BOOST_FILESYSTEM_NO_DEPRECATED
26 # define BOOST_FILESYSTEM_NO_DEPRECATED
28 #ifndef BOOST_SYSTEM_NO_DEPRECATED
29 # define BOOST_SYSTEM_NO_DEPRECATED
32 #include <boost/filesystem/path.hpp>
34 #include <boost/filesystem/detail/utf8_codecvt_facet.hpp> // for imbue tests
35 #include "test_codecvt.hpp" // for codecvt arg tests
36 #include <boost/detail/lightweight_test_report.hpp>
37 #include <boost/smart_ptr.hpp> // used constructor tests
38 #include <boost/functional/hash.hpp>
49 namespace fs
= boost::filesystem
;
50 namespace bs
= boost::system
;
51 using boost::filesystem::path
;
57 #define CHECK(x) check(x, __FILE__, __LINE__)
58 #define PATH_IS(a, b) check_path(a, b, __FILE__, __LINE__)
59 #define NATIVE_IS(p, s, ws) check_native(p, s, ws, __FILE__, __LINE__)
60 #define IS(a,b) check_equal(a, b, __FILE__, __LINE__)
63 # pragma warning(push) // Save warning settings.
64 # pragma warning(disable : 4428) // Disable universal-character-name encountered in source warning.
70 boost::system::error_code ec
;
71 const boost::system::error_code ok
;
72 const boost::system::error_code
ng(-1, boost::system::system_category());
74 std::string
platform(BOOST_PLATFORM
);
76 void check_path(const path
& source
,
77 const wstring
& expected
, const char* file
, int line
)
79 if (source
== expected
) return;
81 ++::boost::detail::test_errors();
84 std::wcout
<< L
'(' << line
<< L
"): source.wstring(): \""
86 << L
"\" != expected: \"" << expected
90 # ifdef BOOST_WINDOWS_API
91 void check_native(const path
& p
,
92 const string
&, const wstring
& expected
, const char* file
, int line
)
94 void check_native(const path
& p
,
95 const string
& expected
, const wstring
&, const char* file
, int line
)
98 if (p
.native() == expected
) return;
100 ++::boost::detail::test_errors();
102 std::cout
<< file
<< '(' << line
<< "): native() is not equal expected\n"
103 " native---: " << std::hex
;
104 path::string_type
nat(p
.native());
105 for (path::string_type::const_iterator it
= nat
.begin(); it
!= nat
.end(); ++it
)
106 std::cout
<< long(*it
) << ' ';
107 std::cout
<< "\n expected-: ";
108 for (path::string_type::const_iterator it
= expected
.begin(); it
!= expected
.end(); ++it
)
109 std::cout
<< long(*it
) << ' ';
110 std::cout
<< std::dec
<< std::endl
;
113 template< class T1
, class T2
>
114 void check_equal(const T1
& value
,
115 const T2
& expected
, const char* file
, int line
)
117 if (value
== expected
) return;
119 ++::boost::detail::test_errors();
123 std::wcout
<< L
'(' << line
<< L
"): value: \"" << value
124 << L
"\" != expected: \"" << expected
128 void check(bool ok_
, const char* file
, int line
)
132 ++::boost::detail::test_errors();
134 std::cout
<< file
<< '(' << line
<< "): test failed\n";
138 wstring
ws(L
"wstring");
139 std::list
<char> l
; // see main() for initialization to s, t, r, i, n, g
140 std::list
<wchar_t> wl
; // see main() for initialization to w, s, t, r, i, n, g
141 std::vector
<char> v
; // see main() for initialization to f, u, z
142 std::vector
<wchar_t> wv
; // see main() for initialization to w, f, u, z
145 class Derived
: public Base
{};
146 void fun(const boost::shared_ptr
< Base
>&) {}
148 // test_constructors ---------------------------------------------------------------//
150 void test_constructors()
152 std::cout
<< "testing constructors..." << std::endl
;
154 path x0
; // default constructor
156 BOOST_TEST_EQ(x0
.native().size(), 0U);
158 path
x1(l
.begin(), l
.end()); // iterator range char
159 PATH_IS(x1
, L
"string");
160 BOOST_TEST_EQ(x1
.native().size(), 6U);
162 path
x2(x1
); // copy constructor
163 PATH_IS(x2
, L
"string");
164 BOOST_TEST_EQ(x2
.native().size(), 6U);
166 path
x3(wl
.begin(), wl
.end()); // iterator range wchar_t
167 PATH_IS(x3
, L
"wstring");
168 BOOST_TEST_EQ(x3
.native().size(), 7U);
170 // contiguous containers
171 path
x4(string("std::string")); // std::string
172 PATH_IS(x4
, L
"std::string");
173 BOOST_TEST_EQ(x4
.native().size(), 11U);
175 path
x5(wstring(L
"std::wstring")); // std::wstring
176 PATH_IS(x5
, L
"std::wstring");
177 BOOST_TEST_EQ(x5
.native().size(), 12U);
179 path
x4v(v
); // std::vector<char>
180 PATH_IS(x4v
, L
"fuz");
181 BOOST_TEST_EQ(x4v
.native().size(), 3U);
183 path
x5v(wv
); // std::vector<wchar_t>
184 PATH_IS(x5v
, L
"wfuz");
185 BOOST_TEST_EQ(x5v
.native().size(), 4U);
187 path
x6("array char"); // array char
188 PATH_IS(x6
, L
"array char");
189 BOOST_TEST_EQ(x6
.native().size(), 10U);
191 path
x7(L
"array wchar_t"); // array wchar_t
192 PATH_IS(x7
, L
"array wchar_t");
193 BOOST_TEST_EQ(x7
.native().size(), 13U);
195 char char_array
[100];
196 std::strcpy(char_array
, "big array char");
197 path
x6o(char_array
); // array char, only partially full
198 PATH_IS(x6o
, L
"big array char");
199 BOOST_TEST_EQ(x6o
.native().size(), 14U);
201 wchar_t wchar_array
[100];
202 std::wcscpy(wchar_array
, L
"big array wchar_t");
203 path
x7o(wchar_array
); // array char, only partially full
204 PATH_IS(x7o
, L
"big array wchar_t");
205 BOOST_TEST_EQ(x7o
.native().size(), 17U);
207 path
x8(s
.c_str()); // const char* null terminated
208 PATH_IS(x8
, L
"string");
209 BOOST_TEST_EQ(x8
.native().size(), 6U);
211 path
x9(ws
.c_str()); // const wchar_t* null terminated
212 PATH_IS(x9
, L
"wstring");
213 BOOST_TEST_EQ(x9
.native().size(), 7U);
215 path
x8nc(const_cast<char*>(s
.c_str())); // char* null terminated
216 PATH_IS(x8nc
, L
"string");
217 BOOST_TEST_EQ(x8nc
.native().size(), 6U);
219 path
x9nc(const_cast<wchar_t*>(ws
.c_str())); // wchar_t* null terminated
220 PATH_IS(x9nc
, L
"wstring");
221 BOOST_TEST_EQ(x9nc
.native().size(), 7U);
223 // non-contiguous containers
224 path
x10(l
); // std::list<char>
225 PATH_IS(x10
, L
"string");
226 BOOST_TEST_EQ(x10
.native().size(), 6U);
228 path
xll(wl
); // std::list<wchar_t>
229 PATH_IS(xll
, L
"wstring");
230 BOOST_TEST_EQ(xll
.native().size(), 7U);
232 // easy-to-make coding errors
233 // path e1(x0, path::codecvt()); // fails to compile, and that is OK
235 boost::shared_ptr
< Derived
> pDerived( new Derived() );
236 fun( pDerived
); // tests constructor member template enable_if working correctly;
237 // will fail to compile if enable_if not taking path off the table
243 // test_assignments ----------------------------------------------------------------//
245 void test_assignments()
247 std::cout
<< "testing assignments..." << std::endl
;
249 x
= path("yet another path"); // another path
250 PATH_IS(x
, L
"yet another path");
251 BOOST_TEST_EQ(x
.native().size(), 16U);
253 x
= x
; // self-assignment
254 PATH_IS(x
, L
"yet another path");
255 BOOST_TEST_EQ(x
.native().size(), 16U);
257 x
.assign(l
.begin(), l
.end()); // iterator range char
258 PATH_IS(x
, L
"string");
260 x
.assign(wl
.begin(), wl
.end()); // iterator range wchar_t
261 PATH_IS(x
, L
"wstring");
263 x
= string("std::string"); // container char
264 PATH_IS(x
, L
"std::string");
266 x
= wstring(L
"std::wstring"); // container wchar_t
267 PATH_IS(x
, L
"std::wstring");
269 x
= "array char"; // array char
270 PATH_IS(x
, L
"array char");
272 x
= L
"array wchar"; // array wchar_t
273 PATH_IS(x
, L
"array wchar");
275 x
= s
.c_str(); // const char* null terminated
276 PATH_IS(x
, L
"string");
278 x
= ws
.c_str(); // const wchar_t* null terminated
279 PATH_IS(x
, L
"wstring");
282 // test_move_construction_and_assignment -------------------------------------------//
284 void test_move_construction_and_assignment()
286 std::cout
<< "testing move_construction_and_assignment..." << std::endl
;
288 # if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
289 path
from("long enough to avoid small object optimization");
290 path
to(std::move(from
));
291 BOOST_TEST(to
== "long enough to avoid small object optimization");
293 cout
<< "Note: move construction did not result in empty source path" << endl
;
295 path
from2("long enough to avoid small object optimization");
297 to2
= std::move(from2
);
298 BOOST_TEST(to2
== "long enough to avoid small object optimization");
300 cout
<< "Note: move assignment did not result in empty rhs path" << endl
;
303 "Test skipped because compiler does not support move semantics" << std::endl
;
308 // test_appends --------------------------------------------------------------------//
312 std::cout
<< "testing appends..." << std::endl
;
314 # ifdef BOOST_WINDOWS_API
315 # define BOOST_FS_FOO L"/foo\\"
316 # else // POSIX paths
317 # define BOOST_FS_FOO L"/foo/"
321 x
/= path(""); // empty path
325 x
/= path("/"); // slash path
326 PATH_IS(x
, L
"/foo/");
329 x
/= path("/boo"); // slash path
330 PATH_IS(x
, L
"/foo/boo");
333 x
/= x
; // self-append
334 PATH_IS(x
, L
"/foo/foo");
337 x
/= path("yet another path"); // another path
338 PATH_IS(x
, BOOST_FS_FOO L
"yet another path");
341 x
.append(l
.begin(), l
.end()); // iterator range char
342 PATH_IS(x
, BOOST_FS_FOO L
"string");
345 x
.append(wl
.begin(), wl
.end()); // iterator range wchar_t
346 PATH_IS(x
, BOOST_FS_FOO L
"wstring");
349 x
/= string("std::string"); // container char
350 PATH_IS(x
, BOOST_FS_FOO L
"std::string");
353 x
/= wstring(L
"std::wstring"); // container wchar_t
354 PATH_IS(x
, BOOST_FS_FOO L
"std::wstring");
357 x
/= "array char"; // array char
358 PATH_IS(x
, BOOST_FS_FOO L
"array char");
361 x
/= L
"array wchar"; // array wchar_t
362 PATH_IS(x
, BOOST_FS_FOO L
"array wchar");
365 x
/= s
.c_str(); // const char* null terminated
366 PATH_IS(x
, BOOST_FS_FOO L
"string");
369 x
/= ws
.c_str(); // const wchar_t* null terminated
370 PATH_IS(x
, BOOST_FS_FOO L
"wstring");
373 // test_concats --------------------------------------------------------------------//
377 std::cout
<< "testing concats..." << std::endl
;
380 x
+= path(""); // empty path
384 x
+= path("/"); // slash path
385 PATH_IS(x
, L
"/foo/");
388 x
+= path("boo"); // slash path
389 PATH_IS(x
, L
"/fooboo");
392 x
+= x
; // self-append
393 PATH_IS(x
, L
"foofoo");
396 x
+= path("yet another path"); // another path
397 PATH_IS(x
, L
"foo-yet another path");
400 x
.concat(l
.begin(), l
.end()); // iterator range char
401 PATH_IS(x
, L
"foo-string");
404 x
.concat(wl
.begin(), wl
.end()); // iterator range wchar_t
405 PATH_IS(x
, L
"foo-wstring");
408 x
+= string("std::string"); // container char
409 PATH_IS(x
, L
"foo-std::string");
412 x
+= wstring(L
"std::wstring"); // container wchar_t
413 PATH_IS(x
, L
"foo-std::wstring");
416 x
+= "array char"; // array char
417 PATH_IS(x
, L
"foo-array char");
420 x
+= L
"array wchar"; // array wchar_t
421 PATH_IS(x
, L
"foo-array wchar");
424 x
+= s
.c_str(); // const char* null terminated
425 PATH_IS(x
, L
"foo-string");
428 x
+= ws
.c_str(); // const wchar_t* null terminated
429 PATH_IS(x
, L
"foo-wstring");
433 PATH_IS(x
, L
"foo-x");
437 PATH_IS(x
, L
"foo-x");
440 // test_observers ------------------------------------------------------------------//
442 void test_observers()
444 std::cout
<< "testing observers..." << std::endl
;
448 CHECK(p0
.native().size() == 3);
449 CHECK(p0
.size() == 3);
450 CHECK(p0
.string() == "abc");
451 CHECK(p0
.string().size() == 3);
452 CHECK(p0
.wstring() == L
"abc");
453 CHECK(p0
.wstring().size() == 3);
456 CHECK(p0
.native().size() == 0);
457 CHECK(p0
.size() == 0);
459 # ifdef BOOST_WINDOWS_API
461 path
p("abc\\def/ghi");
463 CHECK(std::wstring(p
.c_str()) == L
"abc\\def/ghi");
465 CHECK(p
.string() == "abc\\def/ghi");
466 CHECK(p
.wstring() == L
"abc\\def/ghi");
468 CHECK(p
.generic_path().string() == "abc/def/ghi");
469 CHECK(p
.generic_string() == "abc/def/ghi");
470 CHECK(p
.generic_wstring() == L
"abc/def/ghi");
472 CHECK(p
.generic_string
<string
>() == "abc/def/ghi");
473 CHECK(p
.generic_string
<wstring
>() == L
"abc/def/ghi");
474 CHECK(p
.generic_string
<path::string_type
>() == L
"abc/def/ghi");
476 # else // BOOST_POSIX_API
478 path
p("abc\\def/ghi");
480 CHECK(string(p
.c_str()) == "abc\\def/ghi");
482 CHECK(p
.string() == "abc\\def/ghi");
483 CHECK(p
.wstring() == L
"abc\\def/ghi");
485 CHECK(p
.generic_path().string() == "abc\\def/ghi");
486 CHECK(p
.generic_string() == "abc\\def/ghi");
487 CHECK(p
.generic_wstring() == L
"abc\\def/ghi");
489 CHECK(p
.generic_string
<string
>() == "abc\\def/ghi");
490 CHECK(p
.generic_string
<wstring
>() == L
"abc\\def/ghi");
491 CHECK(p
.generic_string
<path::string_type
>() == "abc\\def/ghi");
496 // test_relationals ----------------------------------------------------------------//
498 void test_relationals()
500 std::cout
<< "testing relationals..." << std::endl
;
502 boost::hash
<path
> hash
;
504 # ifdef BOOST_WINDOWS_API
505 // this is a critical use case to meet user expectations
506 CHECK(path("c:\\abc") == path("c:/abc"));
507 CHECK(hash(path("c:\\abc")) == hash(path("c:/abc")));
511 const path
p2("baz");
517 CHECK(p
< string("baz"));
519 CHECK(p
< wstring(L
"baz"));
521 CHECK(!(string("baz") < p
));
522 CHECK(!(L
"baz" < p
));
523 CHECK(!(wstring(L
"baz") < p
));
529 CHECK(p2
== string("baz"));
531 CHECK(p2
== wstring(L
"baz"));
533 CHECK(string("baz") == p2
);
535 CHECK(wstring(L
"baz") == p2
);
537 CHECK(hash(p
) == hash(p
));
538 CHECK(hash(p
) != hash(p2
)); // Not strictly required, but desirable
557 // test_inserter_and_extractor -----------------------------------------------------//
559 void test_inserter_and_extractor()
561 std::cout
<< "testing inserter and extractor..." << std::endl
;
563 path
p1("foo bar"); // verify space in path roundtrips per ticket #3863
566 std::stringstream ss
;
573 path
wp1(L
"foo bar");
576 std::wstringstream wss
;
584 // test_other_non_members ----------------------------------------------------------//
586 void test_other_non_members()
588 std::cout
<< "testing other_non_members..." << std::endl
;
595 CHECK(p1
/ p2
== path("foo/bar").make_preferred());
596 CHECK("foo" / p2
== path("foo/bar").make_preferred());
597 CHECK(L
"foo" / p2
== path("foo/bar").make_preferred());
598 CHECK(string("foo") / p2
== path("foo/bar").make_preferred());
599 CHECK(wstring(L
"foo") / p2
== path("foo/bar").make_preferred());
600 CHECK(p1
/ "bar" == path("foo/bar").make_preferred());
601 CHECK(p1
/ L
"bar" == path("foo/bar").make_preferred());
602 CHECK(p1
/ string("bar") == path("foo/bar").make_preferred());
603 CHECK(p1
/ wstring(L
"bar") == path("foo/bar").make_preferred());
610 CHECK(!path("").filename_is_dot());
611 CHECK(!path("").filename_is_dot_dot());
612 CHECK(!path("..").filename_is_dot());
613 CHECK(!path(".").filename_is_dot_dot());
614 CHECK(!path("...").filename_is_dot_dot());
615 CHECK(path(".").filename_is_dot());
616 CHECK(path("..").filename_is_dot_dot());
617 CHECK(path("/.").filename_is_dot());
618 CHECK(path("/..").filename_is_dot_dot());
619 CHECK(!path("a.").filename_is_dot());
620 CHECK(!path("a..").filename_is_dot_dot());
623 CHECK(path("foo/").filename() == path("."));
624 CHECK(path("foo/").filename_is_dot());
625 CHECK(path("/").filename() == path("/"));
626 CHECK(!path("/").filename_is_dot());
627 # ifdef BOOST_WINDOWS_API
628 CHECK(path("c:.").filename() == path("."));
629 CHECK(path("c:.").filename_is_dot());
630 CHECK(path("c:/").filename() == path("/"));
631 CHECK(!path("c:\\").filename_is_dot());
633 CHECK(path("c:.").filename() == path("c:."));
634 CHECK(!path("c:.").filename_is_dot());
635 CHECK(path("c:/").filename() == path("."));
636 CHECK(path("c:/").filename_is_dot());
639 // check that the implementation code to make the edge cases above work right
640 // doesn't cause some non-edge cases to fail
641 CHECK(path("c:").filename() != path("."));
642 CHECK(!path("c:").filename_is_dot());
644 // examples from reference.html
645 std::cout
<< path(".").filename_is_dot(); // outputs 1
646 std::cout
<< path("/.").filename_is_dot(); // outputs 1
647 std::cout
<< path("foo/.").filename_is_dot(); // outputs 1
648 std::cout
<< path("foo/").filename_is_dot(); // outputs 1
649 std::cout
<< path("/").filename_is_dot(); // outputs 0
650 std::cout
<< path("/foo").filename_is_dot(); // outputs 0
651 std::cout
<< path("/foo.").filename_is_dot(); // outputs 0
652 std::cout
<< path("..").filename_is_dot(); // outputs 0
656 // // test_modifiers ------------------------------------------------------------------//
658 // void test_modifiers()
660 // std::cout << "testing modifiers..." << std::endl;
664 // test_iterators ------------------------------------------------------------------//
666 void test_iterators()
668 std::cout
<< "testing iterators..." << std::endl
;
671 CHECK(p1
.begin() == p1
.end());
674 CHECK(p2
.begin() != p2
.end());
675 CHECK(*p2
.begin() == "/");
676 CHECK(++p2
.begin() == p2
.end());
678 path
p3("foo/bar/baz");
680 path::iterator
it(p3
.begin());
681 CHECK(p3
.begin() != p3
.end());
683 CHECK(*++it
== "bar");
684 CHECK(*++it
== "baz");
685 CHECK(*--it
== "bar");
686 CHECK(*--it
== "foo");
687 CHECK(*++it
== "bar");
688 CHECK(*++it
== "baz");
689 CHECK(++it
== p3
.end());
692 // test_reverse_iterators ----------------------------------------------------------//
694 void test_reverse_iterators()
696 std::cout
<< "testing reverse_iterators..." << std::endl
;
699 CHECK(p1
.rbegin() == p1
.rend());
702 CHECK(p2
.rbegin() != p2
.rend());
703 CHECK(*p2
.rbegin() == "/");
704 CHECK(++p2
.rbegin() == p2
.rend());
706 path
p3("foo/bar/baz");
708 path::reverse_iterator
it(p3
.rbegin());
709 CHECK(p3
.rbegin() != p3
.rend());
711 CHECK(*++it
== "bar");
712 CHECK(*++it
== "foo");
713 CHECK(*--it
== "bar");
714 CHECK(*--it
== "baz");
715 CHECK(*++it
== "bar");
716 CHECK(*++it
== "foo");
717 CHECK(++it
== p3
.rend());
720 // test_modifiers ------------------------------------------------------------------//
722 void test_modifiers()
724 std::cout
<< "testing modifiers..." << std::endl
;
726 CHECK(path("").remove_filename() == "");
727 CHECK(path("foo").remove_filename() == "");
728 CHECK(path("/foo").remove_filename() == "/");
729 CHECK(path("foo/bar").remove_filename() == "foo");
730 BOOST_TEST_EQ(path("foo/bar/").remove_filename(), path("foo/bar"));
731 BOOST_TEST_EQ(path(".").remove_filename(), path(""));
732 BOOST_TEST_EQ(path("./.").remove_filename(), path("."));
733 BOOST_TEST_EQ(path("/.").remove_filename(), path("/"));
734 BOOST_TEST_EQ(path("..").remove_filename(), path(""));
735 BOOST_TEST_EQ(path("../..").remove_filename(), path(".."));
736 BOOST_TEST_EQ(path("/..").remove_filename(), path("/"));
740 // test_decompositions -------------------------------------------------------------//
742 void test_decompositions()
744 std::cout
<< "testing decompositions..." << std::endl
;
746 CHECK(path("").root_name().string() == "");
747 CHECK(path("foo").root_name().string() == "");
748 CHECK(path("/").root_name().string() == "");
749 CHECK(path("/foo").root_name().string() == "");
750 CHECK(path("//netname").root_name().string() == "//netname");
751 CHECK(path("//netname/foo").root_name().string() == "//netname");
753 CHECK(path("").root_directory().string() == "");
754 CHECK(path("foo").root_directory().string() == "");
755 CHECK(path("/").root_directory().string() == "/");
756 CHECK(path("/foo").root_directory().string() == "/");
757 CHECK(path("//netname").root_directory().string() == "");
758 CHECK(path("//netname/foo").root_directory().string() == "/");
760 CHECK(path("").root_path().string() == "");
761 CHECK(path("/").root_path().string() == "/");
762 CHECK(path("/foo").root_path().string() == "/");
763 CHECK(path("//netname").root_path().string() == "//netname");
764 CHECK(path("//netname/foo").root_path().string() == "//netname/");
766 # ifdef BOOST_WINDOWS_API
767 CHECK(path("c:/foo").root_path().string() == "c:/");
770 CHECK(path("").relative_path().string() == "");
771 CHECK(path("/").relative_path().string() == "");
772 CHECK(path("/foo").relative_path().string() == "foo");
774 CHECK(path("").parent_path().string() == "");
775 CHECK(path("/").parent_path().string() == "");
776 CHECK(path("/foo").parent_path().string() == "/");
777 CHECK(path("/foo/bar").parent_path().string() == "/foo");
779 CHECK(path("/foo/bar/baz.zoo").filename().string() == "baz.zoo");
781 CHECK(path("/foo/bar/baz.zoo").stem().string() == "baz");
782 CHECK(path("/foo/bar.woo/baz").stem().string() == "baz");
784 CHECK(path("foo.bar.baz.tar.bz2").extension().string() == ".bz2");
785 CHECK(path("/foo/bar/baz.zoo").extension().string() == ".zoo");
786 CHECK(path("/foo/bar.woo/baz").extension().string() == "");
789 // test_queries --------------------------------------------------------------------//
793 std::cout
<< "testing queries..." << std::endl
;
796 path
p2("//netname/foo.doo");
799 CHECK(!p1
.has_root_path());
800 CHECK(!p1
.has_root_name());
801 CHECK(!p1
.has_root_directory());
802 CHECK(!p1
.has_relative_path());
803 CHECK(!p1
.has_parent_path());
804 CHECK(!p1
.has_filename());
805 CHECK(!p1
.has_stem());
806 CHECK(!p1
.has_extension());
807 CHECK(!p1
.is_absolute());
808 CHECK(p1
.is_relative());
811 CHECK(p2
.has_root_path());
812 CHECK(p2
.has_root_name());
813 CHECK(p2
.has_root_directory());
814 CHECK(p2
.has_relative_path());
815 CHECK(p2
.has_parent_path());
816 CHECK(p2
.has_filename());
817 CHECK(p2
.has_stem());
818 CHECK(p2
.has_extension());
819 CHECK(p2
.is_absolute());
820 CHECK(!p2
.is_relative());
824 // test_imbue_locale ---------------------------------------------------------------//
826 void test_imbue_locale()
828 std::cout
<< "testing imbue locale..." << std::endl
;
830 // weak test case for before/after states since we don't know what characters the
831 // default locale accepts.
834 // So that tests are run with known encoding, use Boost UTF-8 codecvt
835 // \u2722 and \xE2\x9C\xA2 are UTF-16 and UTF-8 FOUR TEARDROP-SPOKED ASTERISK
837 std::locale global_loc
= std::locale();
838 std::locale
loc(global_loc
, new fs::detail::utf8_codecvt_facet
);
839 std::cout
<< " imbuing locale ..." << std::endl
;
840 std::locale old_loc
= path::imbue(loc
);
842 std::cout
<< " testing with the imbued locale ..." << std::endl
;
843 path
p2("\xE2\x9C\xA2");
844 CHECK(p2
.wstring().size() == 1);
845 CHECK(p2
.wstring()[0] == 0x2722);
847 std::cout
<< " imbuing the original locale ..." << std::endl
;
848 path::imbue(old_loc
);
850 std::cout
<< " testing with the original locale ..." << std::endl
;
852 CHECK(before
== after
);
854 std::cout
<< " locale testing complete" << std::endl
;
857 // test_codecvt_argument -----------------------------------------------------------//
859 void test_codecvt_argument()
861 std::cout
<< "testing codecvt arguments..." << std::endl
;
863 const char * c1
= "a1";
864 const std::string
s1(c1
);
865 const std::wstring
ws1(L
"b2"); // off-by-one mimics test_codecvt
866 const std::string
s2("y8");
867 const std::wstring
ws2(L
"z9");
869 test_codecvt cvt
; // produces off-by-one values that will always differ from
870 // the system's default locale codecvt facet
875 std::cout
<< " constructors test " << ++t
<< std::endl
;
877 NATIVE_IS(p
, s1
, ws1
);
879 std::cout
<< " test " << ++t
<< std::endl
;
880 path
p1(s1
.begin(), s1
.end(), cvt
);
881 NATIVE_IS(p1
, s1
, ws1
);
883 std::cout
<< " test " << ++t
<< std::endl
;
885 NATIVE_IS(p2
, s2
, ws2
);
887 std::cout
<< " test " << ++t
<< std::endl
;
888 path
p3(ws2
.begin(), ws2
.end(), cvt
);
889 NATIVE_IS(p3
, s2
, ws2
);
891 // path p2(p1, cvt); // fails to compile, and that is OK
895 std::cout
<< " assigns test " << ++t
<< std::endl
;
897 NATIVE_IS(p1
, s1
, ws1
);
899 std::cout
<< " test " << ++t
<< std::endl
;
900 p1
.assign(s1
.begin(), s1
.end(), cvt
);
901 NATIVE_IS(p1
, s1
, ws1
);
902 // p1.assign(p, cvt); // fails to compile, and that is OK
906 std::cout
<< " appends test " << ++t
<< std::endl
;
908 NATIVE_IS(p1
, s1
, ws1
);
910 std::cout
<< " test " << ++t
<< std::endl
;
911 p1
.append(s1
.begin(), s1
.end(), cvt
);
912 NATIVE_IS(p1
, s1
, ws1
);
913 // p1.append(p, cvt); // fails to compile, and that is OK
916 std::cout
<< " native observers test " << ++t
<< std::endl
;
917 CHECK(p
.string
<std::string
>(cvt
) == s1
);
918 std::cout
<< " test " << ++t
<< std::endl
;
919 CHECK(p
.string(cvt
) == s1
);
920 std::cout
<< " test " << ++t
<< std::endl
;
921 CHECK(p
.string
<std::wstring
>(cvt
) == ws1
);
922 std::cout
<< " test " << ++t
<< std::endl
;
923 CHECK(p
.wstring(cvt
) == ws1
);
926 std::cout
<< " generic observers test " << ++t
<< std::endl
;
927 CHECK(p
.generic_string
<std::string
>(cvt
) == s1
);
928 std::cout
<< " test " << ++t
<< std::endl
;
929 CHECK(p
.generic_string(cvt
) == s1
);
930 std::cout
<< " test " << ++t
<< std::endl
;
931 CHECK(p
.generic_string
<std::wstring
>(cvt
) == ws1
);
932 std::cout
<< " test " << ++t
<< std::endl
;
933 CHECK(p
.generic_wstring(cvt
) == ws1
);
935 std::cout
<< " codecvt arguments testing complete" << std::endl
;
938 // test_overloads ------------------------------------------------------------------//
940 void test_overloads()
942 std::cout
<< "testing overloads..." << std::endl
;
943 std::string
sto("hello");
944 const char a
[] = "goodbye";
946 path
p2(sto
.c_str());
950 std::wstring
wsto(L
"hello");
951 const wchar_t wa
[] = L
"goodbye";
953 path
wp2(wsto
.c_str());
958 // test_error_handling -------------------------------------------------------------//
961 : public std::codecvt
< wchar_t, char, std::mbstate_t >
964 explicit error_codecvt()
965 : std::codecvt
<wchar_t, char, std::mbstate_t>() {}
968 virtual bool do_always_noconv() const throw() { return false; }
969 virtual int do_encoding() const throw() { return 0; }
971 virtual std::codecvt_base::result
do_in(std::mbstate_t&,
972 const char*, const char*, const char*&,
973 wchar_t*, wchar_t*, wchar_t*&) const
975 static std::codecvt_base::result r
= std::codecvt_base::noconv
;
976 if (r
== std::codecvt_base::partial
) r
= std::codecvt_base::error
;
977 else if (r
== std::codecvt_base::error
) r
= std::codecvt_base::noconv
;
978 else r
= std::codecvt_base::partial
;
982 virtual std::codecvt_base::result
do_out(std::mbstate_t &,
983 const wchar_t*, const wchar_t*, const wchar_t*&,
984 char*, char*, char*&) const
986 static std::codecvt_base::result r
= std::codecvt_base::noconv
;
987 if (r
== std::codecvt_base::partial
) r
= std::codecvt_base::error
;
988 else if (r
== std::codecvt_base::error
) r
= std::codecvt_base::noconv
;
989 else r
= std::codecvt_base::partial
;
993 virtual std::codecvt_base::result
do_unshift(std::mbstate_t&,
994 char*, char*, char* &) const { return ok
; }
995 virtual int do_length(std::mbstate_t &,
996 const char*, const char*, std::size_t) const { return 0; }
997 virtual int do_max_length() const throw () { return 0; }
1000 void test_error_handling()
1002 std::cout
<< "testing error handling..." << std::endl
;
1004 std::locale global_loc
= std::locale();
1005 std::locale
loc(global_loc
, new error_codecvt
);
1006 std::cout
<< " imbuing error locale ..." << std::endl
;
1007 std::locale old_loc
= path::imbue(loc
);
1009 // These tests rely on a path constructor that fails in the locale conversion.
1010 // Thus construction has to call codecvt. Force that by using a narrow string
1011 // for Windows, and a wide string for POSIX.
1012 # ifdef BOOST_WINDOWS_API
1013 # define STRING_FOO_ "foo"
1015 # define STRING_FOO_ L"foo"
1019 std::cout
<< " testing std::codecvt_base::partial error..." << std::endl
;
1020 bool exception_thrown (false);
1021 try { path(STRING_FOO_
); }
1022 catch (const bs::system_error
& ex
)
1024 exception_thrown
= true;
1025 BOOST_TEST_EQ(ex
.code(), bs::error_code(std::codecvt_base::partial
,
1026 fs::codecvt_error_category()));
1028 catch (...) { std::cout
<< "***** unexpected exception type *****" << std::endl
; }
1029 BOOST_TEST(exception_thrown
);
1033 std::cout
<< " testing std::codecvt_base::error error..." << std::endl
;
1034 bool exception_thrown (false);
1035 try { path(STRING_FOO_
); }
1036 catch (const bs::system_error
& ex
)
1038 exception_thrown
= true;
1039 BOOST_TEST_EQ(ex
.code(), bs::error_code(std::codecvt_base::error
,
1040 fs::codecvt_error_category()));
1042 catch (...) { std::cout
<< "***** unexpected exception type *****" << std::endl
; }
1043 BOOST_TEST(exception_thrown
);
1047 std::cout
<< " testing std::codecvt_base::noconv error..." << std::endl
;
1048 bool exception_thrown (false);
1049 try { path(STRING_FOO_
); }
1050 catch (const bs::system_error
& ex
)
1052 exception_thrown
= true;
1053 BOOST_TEST_EQ(ex
.code(), bs::error_code(std::codecvt_base::noconv
,
1054 fs::codecvt_error_category()));
1056 catch (...) { std::cout
<< "***** unexpected exception type *****" << std::endl
; }
1057 BOOST_TEST(exception_thrown
);
1060 std::cout
<< " restoring original locale ..." << std::endl
;
1061 path::imbue(old_loc
);
1062 std::cout
<< " testing error handling complete" << std::endl
;
1067 // // test_locales --------------------------------------------------------------------//
1069 // void test_locales()
1071 // std::cout << "testing locales..." << std::endl;
1075 // test_user_supplied_type ---------------------------------------------------------//
1077 typedef std::basic_string
<int> user_string
;
1079 } // unnamed namespace
1083 namespace filesystem
1085 namespace path_traits
1087 template<> struct is_iterator
<const user_string::value_type
*> { static const bool value
= true; };
1088 template<> struct is_iterator
<user_string::value_type
*> { static const bool value
= true; };
1089 template<> struct is_iterator
<user_string::iterator
> { static const bool value
= true; };
1090 template<> struct is_iterator
<user_string::const_iterator
> { static const bool value
= true; };
1091 template<> struct is_container
<user_string
> { static const bool value
= true; };
1094 void append
<user_string::value_type
>(const user_string::value_type
* begin
,
1095 const user_string::value_type
* end
, string_type
& target
, system::error_code
& ec
)
1097 for (; begin
!= end
&& *begin
; ++begin
)
1098 target
+= *begin
+ 1; // change so that results distinguishable from char cvts
1102 // This specialization shouldn't be needed, and VC++, Intel, and others work
1103 // fine without it. But gcc 4.3.2, and presumably other versions, need it.
1105 void append
<user_string::value_type
>(const user_string::value_type
* begin
,
1106 string_type
& target
, system::error_code
& ec
)
1108 path_traits::append
<user_string::value_type
>(begin
,
1109 static_cast<const user_string::value_type
*>(0), target
, ec
);
1114 user_string convert
<user_string
>(const string_type
& source
,
1115 system::error_code
& ec
)
1118 for (string_type::const_iterator it
= source
.begin();
1119 it
!= source
.end(); ++it
)
1123 } // namespace path_traits
1124 } // namespace filesystem
1125 } // namespace boost
1130 void test_user_supplied_type()
1132 std::cout
<< "testing user supplied type..." << std::endl
;
1134 user_string::value_type usr_c_str
[] = { 'a', 'b', 'c', 0 };
1135 user_string
usr(usr_c_str
);
1137 path
p1(usr
.c_str());
1138 CHECK(p1
== path("bcd"));
1140 user_string
s1(p1
.string
<user_string
>());
1146 inline const char* macro_value(const char* name
, const char* value
)
1148 static const char* no_value
= "[no value]";
1149 static const char* not_defined
= "[not defined]";
1151 //if (0 != strcmp(name, value + 1)) // macro is defined
1158 //return not_defined;
1160 return 0 == strcmp(name
, value
+ 1)
1162 : (value
[1] ? value
: no_value
);
1165 #define BOOST_MACRO_VALUE(X) macro_value(#X, BOOST_STRINGIZE(=X))
1167 } // unnamed namespace
1169 //--------------------------------------------------------------------------------------//
1173 //--------------------------------------------------------------------------------------//
1175 int test_main(int, char*[])
1177 // document state of critical macros
1178 #ifdef BOOST_POSIX_API
1179 cout
<< "BOOST_POSIX_API" << endl
;
1180 BOOST_TEST(path::preferred_separator
== '/');
1182 #ifdef BOOST_WINDOWS_API
1183 cout
<< "BOOST_WINDOWS_API" << endl
;
1184 BOOST_TEST(path::preferred_separator
== '\\');
1187 cout
<< "BOOST_FILESYSTEM_DECL "
1188 << BOOST_MACRO_VALUE(BOOST_FILESYSTEM_DECL
) << endl
;
1190 //#ifdef BOOST_FILESYSTEM_DECL
1191 // cout << "BOOST_FILESYSTEM_DECL is defined as "
1192 // << BOOST_STRINGIZE(BOOST_FILESYSTEM_DECL) << endl;
1194 // cout << "BOOST_FILESYSTEM_DECL is not defined" << endl;
1222 test_constructors();
1224 test_move_construction_and_assignment();
1230 test_inserter_and_extractor();
1231 test_other_non_members();
1233 test_reverse_iterators();
1234 test_decompositions();
1236 test_imbue_locale();
1237 test_codecvt_argument();
1238 test_error_handling();
1242 test_user_supplied_type();
1246 std::string
foo("\\abc");
1247 const char* bar
= "/abc";
1250 cout
<< "unintended consequence\n";
1252 return ::boost::report_errors();