libstdc++/71037 Add base path to filesystem::canonical exceptions
[gcc.git] / libstdc++-v3 / testsuite / experimental / filesystem / operations / canonical.cc
1 // Copyright (C) 2015-2016 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // { dg-options "-std=gnu++11 -lstdc++fs" }
19 // { dg-require-filesystem-ts "" }
20
21 #include <experimental/filesystem>
22 #include <testsuite_hooks.h>
23 #include <testsuite_fs.h>
24
25 namespace fs = std::experimental::filesystem;
26
27 void
28 test01()
29 {
30 bool test __attribute__((unused)) = false;
31
32 std::error_code ec;
33 auto p = __gnu_test::nonexistent_path();
34 canonical( p, ec );
35 VERIFY( ec );
36
37 p = fs::current_path();
38 canonical( p, ec );
39 VERIFY( !ec );
40
41 p = "/";
42 p = canonical( p, ec );
43 VERIFY( p == "/" );
44 VERIFY( !ec );
45
46 p = "/.";
47 p = canonical( p, ec );
48 VERIFY( p == "/" );
49 VERIFY( !ec );
50
51 p = "/..";
52 p = canonical( p, ec );
53 VERIFY( p == "/" );
54 VERIFY( !ec );
55
56 p = "/../.././.";
57 p = canonical( p, ec );
58 VERIFY( p == "/" );
59 VERIFY( !ec );
60 }
61
62 void
63 test02()
64 {
65 #if __cpp_exceptions
66 bool test __attribute__((unused)) = false;
67
68 fs::path p = "rel", base = __gnu_test::nonexistent_path();
69 fs::path e1, e2;
70 try {
71 canonical(p, base);
72 } catch (const fs::filesystem_error& e) {
73 e1 = e.path1();
74 e2 = e.path2();
75 }
76 VERIFY( e1 == p );
77 VERIFY( e2 == base );
78 #endif
79 }
80
81 int
82 main()
83 {
84 test01();
85 test02();
86 }