OpenVPN 3 Core Library
Loading...
Searching...
No Matches
test_path.cpp
Go to the documentation of this file.
1#include "test_common.hpp"
2
3#include <iostream>
4
6
7using namespace openvpn;
8
9// Ugly hack
10static std::stringstream out;
11
12void test_basename(const std::string &path)
13{
14 const std::string res = path::basename(path);
15 out << "basename('" << path << "') = '" << res << "'" << std::endl;
16}
17
18void dirname(const std::string &path)
19{
20 const std::string res = path::dirname(path);
21 out << "dirname('" << path << "') = '" << res << "'" << std::endl;
22}
23
24void ext(const std::string &path)
25{
26 const std::string res = path::ext(path);
27 out << "ext('" << path << "') = '" << res << "'" << std::endl;
28}
29
30void is_flat(const std::string &path)
31{
32 const bool res = path::is_flat(path);
33 out << "is_flat('" << path << "') = " << res << std::endl;
34}
35
36void join(const std::string &p1, const std::string &p2)
37{
38 const std::string res = path::join(p1, p2);
39 out << "join('" << p1 << "', '" << p2 << "') = '" << res << "'" << std::endl;
40}
41
42void join3(const std::string &p1, const std::string &p2, const std::string &p3)
43{
44 const std::string res = path::join(p1, p2, p3);
45 out << "join('" << p1 << "', '" << p2 << "', '" << p3 << "') = '" << res << "'" << std::endl;
46}
47
48void join4(const std::string &p1, const std::string &p2, const std::string &p3, const std::string &p4)
49{
50 const std::string res = path::join(p1, p2, p3, p4);
51 out << "join('" << p1 << "', '" << p2 << "', '" << p3 << "', '" << p4 << "') = '" << res << "'" << std::endl;
52}
53
54void splitjoin(const std::string &p1)
55{
56 const std::string d = path::dirname(p1);
57 const std::string b = path::basename(p1);
58 const std::string p2 = path::join(d, b);
59 out << "splitjoin p1='" << p1 << "' dir='" << d << "' bn='" << b << "' p2='" << p2 << "'" << std::endl;
60}
61
62TEST(path, test1)
63{
64 out.clear();
65 out.str("");
66 // basename
67 test_basename("");
68 test_basename("/");
69 test_basename("/foo");
70 test_basename("/foo/bar");
71 test_basename("foo/bar/boo");
72 test_basename("foo/bar/");
73 test_basename("foo\\bar\\boo");
74
75 // dirname
76 dirname("");
77 dirname("/");
78 dirname("/foo");
79 dirname("/foo/bar");
80 dirname("foo/bar/boo");
81 dirname("foo/bar/");
82 dirname("foo\\bar\\boo");
83
84 // is_flat
85 is_flat("");
86 is_flat("/");
87 is_flat("foo.bar");
88 is_flat("foo/bar");
89 is_flat("c:/foo");
90 is_flat("c:foo");
91 is_flat("z:\\foo");
92 is_flat(".");
93 is_flat("..");
94 is_flat("./foo");
95
96 // join
97 join("foo", "bar");
98 join("foo", "");
99 join("", "foo/bar");
100 join("", "bar");
101 join("foo", "/bar");
102 join("/", "bar");
103
104 // join (3 or more parms)
105 join3("", "", "three");
106 join3("one", "two", "three");
107 join3("one", "/two", "three");
108 join4("one", "two", "three", "four");
109 join4("one", "two", "", "four");
110
111 // ext
112 ext("");
113 ext("foo");
114 ext("foo.bar");
115 ext("foo.bar.moo");
116 ext("foo.");
117 ext(".foo");
118
119 // splitjoin
120 splitjoin("");
121 splitjoin("/");
122 splitjoin("/foo");
123 splitjoin("/foo/");
124 splitjoin("/foo/bar");
125 splitjoin("/foo/bar/");
126
127#ifdef WIN32
128 ASSERT_EQ(getExpectedOutput("test_path_win32.txt"), out.str());
129#else
130 ASSERT_EQ(getExpectedOutput("test_path.txt"), out.str());
131#endif
132}
133
134void test_contained(const std::string &path, const bool expected)
135{
136 const bool contained = path::is_contained(path);
137 ASSERT_EQ(contained, expected);
138}
139
140TEST(path, test2)
141{
142 test_contained("", false);
143 test_contained(".", true);
144 test_contained("..", false);
145 test_contained("..x", true);
146 test_contained("x..", true);
147 test_contained("...", true);
148 test_contained("../", false);
149 test_contained("/..", false);
150 test_contained("/foo", false);
151 test_contained("foo", true);
152 test_contained("foo/bar", true);
153 test_contained("foo//bar", true);
154 test_contained("foo/bar/", true);
155 test_contained("foo/bar//", true);
156 test_contained("..foo", true);
157 test_contained(".foo", true);
158 test_contained("./foo", true);
159 test_contained("../foo", false);
160 test_contained("..//foo", false);
161 test_contained(".../foo", true);
162 test_contained("foo/..", false);
163 test_contained("foo/.", true);
164 test_contained("foo//..", false);
165 test_contained("foo/...", true);
166 test_contained("foo/./bar", true);
167 test_contained("foo/../bar", false);
168 test_contained("foo/.../bar", true);
169}
170
172{
173 size_t count = 0;
174 for (int i = 0; i < 10000000; ++i)
175 {
176 const std::string s = path::join("one", "two", "three", "four");
177 count += s.length();
178 }
179 std::cout << count << std::endl;
180}
std::string ext(const std::string &basename)
Definition path.hpp:156
bool is_contained(const std::string &path)
Definition path.hpp:106
std::string dirname(const std::string &path)
Definition path.hpp:91
std::string basename(const std::string &path)
Definition path.hpp:76
std::string join(const std::string &p1, const std::string &p2)
Definition path.hpp:180
bool is_flat(const std::string &path)
Definition path.hpp:67
const std::string expected
std::string getExpectedOutput(const std::string &filename)
void join4(const std::string &p1, const std::string &p2, const std::string &p3, const std::string &p4)
Definition test_path.cpp:48
void is_flat(const std::string &path)
Definition test_path.cpp:30
void dirname(const std::string &path)
Definition test_path.cpp:18
void test_contained(const std::string &path, const bool expected)
void ext(const std::string &path)
Definition test_path.cpp:24
void join3(const std::string &p1, const std::string &p2, const std::string &p3)
Definition test_path.cpp:42
TEST(path, test1)
Definition test_path.cpp:62
void test_join_speed()
void join(const std::string &p1, const std::string &p2)
Definition test_path.cpp:36
static std::stringstream out
Definition test_path.cpp:10
void test_basename(const std::string &path)
Definition test_path.cpp:12
void splitjoin(const std::string &p1)
Definition test_path.cpp:54