QMCPACK
test_hdf_path.cpp
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2016 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by: Steven Hahn, hahnse@ornl.gov, Oak Ridge National Laboratory
8 //
9 // File created by: Steven Hahn, hahnse@ornl.gov, Oak Ridge National Laboratory
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 #include "catch.hpp"
13 #include "hdf/hdf_path.h"
14 
15 using namespace qmcplusplus;
16 
17 TEST_CASE("hdf_path_constructor", "[hdf]")
18 {
19  auto gold_string("a/b/c");
20  const hdf_path gold_path(gold_string);
21  REQUIRE(gold_path.string() == gold_string);
22 
23  hdf_path path_sv(std::string_view{gold_string});
24  REQUIRE(path_sv.string() == gold_string);
25  REQUIRE(path_sv == gold_path);
26 
27  hdf_path path_s(std::string{gold_string});
28  REQUIRE(path_s.string() == gold_string);
29  REQUIRE(path_s == gold_path);
30 
31  hdf_path path_path(gold_path);
32  REQUIRE(path_path.string() == gold_string);
33  REQUIRE(path_path == gold_path);
34 }
35 
36 TEST_CASE("hdf_path_append", "[hdf]")
37 {
38  hdf_path path;
39  REQUIRE(path.string().empty());
40  REQUIRE(path == hdf_path());
41 
42  path.append("a");
43  REQUIRE(path.string() == "a");
44  REQUIRE(path == hdf_path("a"));
45 
46  path /= "b";
47  REQUIRE(path.string() == "a/b");
48  REQUIRE(path == hdf_path("a/b"));
49 
50  hdf_path result = path / std::string_view("c");
51  REQUIRE(result.string() == "a/b/c");
52  REQUIRE(result == hdf_path("a/b/c"));
53 
54  result = path / std::string("c");
55  REQUIRE(result.string() == "a/b/c");
56  REQUIRE(result == hdf_path("a/b/c"));
57 
58  result = path / "c/";
59  REQUIRE(result.string() == "a/b/c/");
60  REQUIRE(result == hdf_path("a/b/c/"));
61 
62  result = result / "d";
63  REQUIRE(result.string() == "a/b/c/d");
64  REQUIRE(result == hdf_path("a/b/c/d"));
65 
66  result = path / "/c";
67  REQUIRE(result.string() == "/c");
68  REQUIRE(result == hdf_path("/c"));
69 
70  result /= hdf_path{"d"};
71  REQUIRE(result.string() == "/c/d");
72  REQUIRE(result == hdf_path("/c/d"));
73 }
74 
75 TEST_CASE("hdf_path_concat", "[hdf]")
76 {
77  hdf_path path;
78  REQUIRE(path.string().empty());
79  REQUIRE(path == hdf_path());
80 
81  path.concat("a");
82  REQUIRE(path.string() == "a");
83  REQUIRE(path == hdf_path("a"));
84 
85  path += "b";
86  REQUIRE(path.string() == "ab");
87  REQUIRE(path == hdf_path("ab"));
88 
89  path += hdf_path{"c"};
90  REQUIRE(path.string() == "abc");
91  REQUIRE(path == hdf_path("abc"));
92 }
93 
94 TEST_CASE("hdf_path_remove_path", "[hdf]")
95 {
96  hdf_path path("/a/b/c/d");
97  REQUIRE(path == hdf_path("/a/b/c/d"));
98 
99  path.remove_subgroup();
100  REQUIRE(path == hdf_path("/a/b/c"));
101 
102  path.remove_subgroup();
103  REQUIRE(path == hdf_path("/a/b"));
104 
105  path.remove_subgroup();
106  REQUIRE(path == hdf_path("/a"));
107 
108  path.remove_subgroup();
109  REQUIRE(path == hdf_path("/"));
110 
111  path.remove_subgroup();
112  REQUIRE(path == hdf_path("/"));
113 
114  hdf_path relative_path("a/b/c/d");
115  REQUIRE(relative_path == hdf_path("a/b/c/d"));
116 
117  relative_path.remove_subgroup();
118  REQUIRE(relative_path == hdf_path("a/b/c"));
119 
120  relative_path.remove_subgroup();
121  REQUIRE(relative_path == hdf_path("a/b"));
122 
123  relative_path.remove_subgroup();
124  REQUIRE(relative_path == hdf_path("a"));
125 
126  relative_path.remove_subgroup();
127  REQUIRE(relative_path == hdf_path(""));
128 
129  relative_path.remove_subgroup();
130  REQUIRE(relative_path == hdf_path(""));
131 }
132 
133 TEST_CASE("hdf_path_replace_path", "[hdf]")
134 {
135  hdf_path path("/a/b");
136  REQUIRE(path == hdf_path("/a/b"));
137 
138  path.replace_subgroup("c");
139  REQUIRE(path == hdf_path("/a/c"));
140 
141  path.replace_subgroup(std::string{"de"});
142  REQUIRE(path == hdf_path("/a/de"));
143 
144  path.replace_subgroup(std::string_view{"fs"});
145  REQUIRE(path == hdf_path("/a/fs"));
146 }
147 
148 TEST_CASE("hdf_path_has_root_path", "[hdf]")
149 {
150  hdf_path root_path("/a/b");
151  REQUIRE(root_path.has_root_directory());
152 
153  hdf_path relative_path("a/b");
154  REQUIRE(!relative_path.has_root_directory());
155 }
const std::string & string() const
Return std::string for use with HDF5.
Definition: hdf_path.h:67
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
TEST_CASE("complex_helper", "[type_traits]")
hdf_path & replace_subgroup(std::string_view p)
Replace the last subgroup in path.
Definition: hdf_path.cpp:67
REQUIRE(std::filesystem::exists(filename))
hdf_path & concat(std::string_view p)
Concatenates two paths without introducing a directory separator.
Definition: hdf_path.cpp:31
hdf_path & append(std::string_view p)
Appends elements to the path with a directory separator.
Definition: hdf_path.cpp:20
hdf_path & remove_subgroup()
Remove last subgroup in path.
Definition: hdf_path.cpp:47
bool has_root_directory() const
Does the path absolute (true) or relative to the current group in hdf_archive (false)?
Definition: hdf_path.cpp:73