Fix order and types of members in C++17 insert_return_type structs
[official-gcc.git] / libstdc++-v3 / testsuite / util / testsuite_fs.h
blob38ebd4fb07970b684529ad56be001ce3af31a22b
1 // -*- C++ -*-
2 // Filesystem utils for the C++ library testsuite.
3 //
4 // Copyright (C) 2014-2017 Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
22 #ifndef _TESTSUITE_FS_H
23 #define _TESTSUITE_FS_H 1
25 #include <experimental/filesystem>
26 #include <fstream>
27 #include <string>
28 #include <cstdio>
29 #include <stdlib.h>
30 #include <unistd.h>
32 namespace __gnu_test
34 #define PATH_CHK(p1, p2, fn) \
35 if ( p1.fn() != p2.fn() ) \
36 throw std::experimental::filesystem::filesystem_error( #fn, p1, p2, \
37 std::make_error_code(std::errc::invalid_argument) )
39 void
40 compare_paths(const std::experimental::filesystem::path& p1,
41 const std::experimental::filesystem::path& p2)
43 PATH_CHK( p1, p2, string );
44 PATH_CHK( p1, p2, empty );
45 PATH_CHK( p1, p2, has_root_path );
46 PATH_CHK( p1, p2, has_root_name );
47 PATH_CHK( p1, p2, has_root_directory );
48 PATH_CHK( p1, p2, has_relative_path );
49 PATH_CHK( p1, p2, has_parent_path );
50 PATH_CHK( p1, p2, has_filename );
51 PATH_CHK( p1, p2, has_stem );
52 PATH_CHK( p1, p2, has_extension );
53 PATH_CHK( p1, p2, is_absolute );
54 PATH_CHK( p1, p2, is_relative );
55 auto d1 = std::distance(p1.begin(), p1.end());
56 auto d2 = std::distance(p2.begin(), p2.end());
57 if( d1 != d2 )
58 throw std::experimental::filesystem::filesystem_error(
59 "distance(begin, end)", p1, p2,
60 std::make_error_code(std::errc::invalid_argument) );
63 const std::string test_paths[] = {
64 "", "/", "//", "/.", "/./", "/a", "/a/", "/a//", "/a/b/c/d", "/a//b",
65 "a", "a/b", "a/b/", "a/b/c", "a/b/c.d", "a/b/..", "a/b/c.", "a/b/.c"
68 // This is NOT supposed to be a secure way to get a unique name!
69 // We just need a path that doesn't exist for testing purposes.
70 std::experimental::filesystem::path
71 nonexistent_path()
73 std::experimental::filesystem::path p;
74 #if defined(_GNU_SOURCE) || _XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200112L
75 char tmp[] = "filesystem-ts-test.XXXXXX";
76 int fd = ::mkstemp(tmp);
77 if (fd == -1)
78 throw std::experimental::filesystem::filesystem_error("mkstemp failed",
79 std::error_code(errno, std::generic_category()));
80 ::unlink(tmp);
81 ::close(fd);
82 p = tmp;
83 #else
84 char buf[64];
85 static int counter;
86 #if _GLIBCXX_USE_C99_STDIO
87 std::snprintf(buf, 64,
88 #else
89 std::sprintf(buf,
90 #endif
91 "filesystem-ts-test.%d.%lu", counter++, (unsigned long) ::getpid());
92 p = buf;
93 #endif
94 return p;
97 // RAII helper to remove a file on scope exit.
98 struct scoped_file
100 using path_type = std::experimental::filesystem::path;
102 enum adopt_file_t { adopt_file };
104 explicit
105 scoped_file(const path_type& p = nonexistent_path()) : path(p)
106 { std::ofstream{p.native()}; }
108 scoped_file(path_type p, adopt_file_t) : path(p) { }
110 ~scoped_file() { if (!path.empty()) remove(path); }
112 path_type path;
115 } // namespace __gnu_test
116 #endif