2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2022 Mike Tzou (Chocobo1)
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
32 #include "base/global.h"
33 #include "base/path.h"
35 class TestPath final
: public QObject
38 Q_DISABLE_COPY_MOVE(TestPath
)
44 void testConstructors() const
46 QVERIFY(Path(u
""_qs
) == Path(std::string("")));
47 QVERIFY(Path(u
"abc"_qs
) == Path(std::string("abc")));
48 QVERIFY(Path(u
"/abc"_qs
) == Path(std::string("/abc")));
49 QVERIFY(Path(uR
"(\abc)"_qs
) == Path(std::string(R
"(\abc)")));
52 QVERIFY(Path(uR
"(c:)"_qs
) == Path(std::string(R
"(c:)")));
53 QVERIFY(Path(uR
"(c:/)"_qs
) == Path(std::string(R
"(c:/)")));
54 QVERIFY(Path(uR
"(c:/)"_qs
) == Path(std::string(R
"(c:\)")));
55 QVERIFY(Path(uR
"(c:\)"_qs
) == Path(std::string(R
"(c:/)")));
56 QVERIFY(Path(uR
"(c:\)"_qs
) == Path(std::string(R
"(c:\)")));
58 QVERIFY(Path(uR
"(\\?\C:)"_qs
) == Path(std::string(R
"(\\?\C:)")));
59 QVERIFY(Path(uR
"(\\?\C:/)"_qs
) == Path(std::string(R
"(\\?\C:/)")));
60 QVERIFY(Path(uR
"(\\?\C:/)"_qs
) == Path(std::string(R
"(\\?\C:\)")));
61 QVERIFY(Path(uR
"(\\?\C:\)"_qs
) == Path(std::string(R
"(\\?\C:/)")));
62 QVERIFY(Path(uR
"(\\?\C:\)"_qs
) == Path(std::string(R
"(\\?\C:\)")));
64 QVERIFY(Path(uR
"(\\?\C:\abc)"_qs
) == Path(std::string(R
"(\\?\C:\abc)")));
68 void testIsValid() const
70 QCOMPARE(Path().isValid(), false);
71 QCOMPARE(Path(u
""_qs
).isValid(), false);
73 QCOMPARE(Path(u
"/"_qs
).isValid(), true);
74 QCOMPARE(Path(uR
"(\)"_qs
).isValid(), true);
76 QCOMPARE(Path(u
"a"_qs
).isValid(), true);
77 QCOMPARE(Path(u
"/a"_qs
).isValid(), true);
78 QCOMPARE(Path(uR
"(\a)"_qs
).isValid(), true);
80 QCOMPARE(Path(u
"a/b"_qs
).isValid(), true);
81 QCOMPARE(Path(uR
"(a\b)"_qs
).isValid(), true);
82 QCOMPARE(Path(u
"/a/b"_qs
).isValid(), true);
83 QCOMPARE(Path(uR
"(/a\b)"_qs
).isValid(), true);
84 QCOMPARE(Path(uR
"(\a/b)"_qs
).isValid(), true);
85 QCOMPARE(Path(uR
"(\a\b)"_qs
).isValid(), true);
87 QCOMPARE(Path(u
"//"_qs
).isValid(), true);
88 QCOMPARE(Path(uR
"(\\)"_qs
).isValid(), true);
89 QCOMPARE(Path(u
"//a"_qs
).isValid(), true);
90 QCOMPARE(Path(uR
"(\\a)"_qs
).isValid(), true);
92 #if defined Q_OS_MACOS
93 QCOMPARE(Path(u
"\0"_qs
).isValid(), false);
94 QCOMPARE(Path(u
":"_qs
).isValid(), false);
95 #elif defined Q_OS_WIN
96 QCOMPARE(Path(u
"c:"_qs
).isValid(), false);
97 QCOMPARE(Path(u
"c:/"_qs
).isValid(), true);
98 QCOMPARE(Path(uR
"(c:\)"_qs
).isValid(), true);
99 QCOMPARE(Path(uR
"(c:\a)"_qs
).isValid(), true);
100 QCOMPARE(Path(uR
"(c:\a\b)"_qs
).isValid(), true);
102 for (int i
= 0; i
<= 31; ++i
)
103 QCOMPARE(Path(QChar(i
)).isValid(), false);
104 QCOMPARE(Path(u
":"_qs
).isValid(), false);
105 QCOMPARE(Path(u
"?"_qs
).isValid(), false);
106 QCOMPARE(Path(u
"\""_qs
).isValid(), false);
107 QCOMPARE(Path(u
"*"_qs
).isValid(), false);
108 QCOMPARE(Path(u
"<"_qs
).isValid(), false);
109 QCOMPARE(Path(u
">"_qs
).isValid(), false);
110 QCOMPARE(Path(u
"|"_qs
).isValid(), false);
112 QCOMPARE(Path(u
"\0"_qs
).isValid(), false);
116 void testIsEmpty() const
118 QCOMPARE(Path().isEmpty(), true);
119 QCOMPARE(Path(u
""_qs
).isEmpty(), true);
121 QCOMPARE(Path(u
"\0"_qs
).isEmpty(), false);
123 QCOMPARE(Path(u
"/"_qs
).isEmpty(), false);
124 QCOMPARE(Path(uR
"(\)"_qs
).isEmpty(), false);
126 QCOMPARE(Path(u
"a"_qs
).isEmpty(), false);
127 QCOMPARE(Path(u
"/a"_qs
).isEmpty(), false);
128 QCOMPARE(Path(uR
"(\a)"_qs
).isEmpty(), false);
130 QCOMPARE(Path(uR
"(c:)"_qs
).isEmpty(), false);
131 QCOMPARE(Path(uR
"(c:/)"_qs
).isEmpty(), false);
132 QCOMPARE(Path(uR
"(c:\)"_qs
).isEmpty(), false);
135 void testIsAbsolute() const
137 QCOMPARE(Path().isAbsolute(), false);
138 QCOMPARE(Path(u
""_qs
).isAbsolute(), false);
141 QCOMPARE(Path(u
"/"_qs
).isAbsolute(), true);
142 QCOMPARE(Path(uR
"(\)"_qs
).isAbsolute(), true);
144 QCOMPARE(Path(u
"a"_qs
).isAbsolute(), false);
145 QCOMPARE(Path(u
"/a"_qs
).isAbsolute(), true);
146 QCOMPARE(Path(uR
"(\a)"_qs
).isAbsolute(), true);
148 QCOMPARE(Path(u
"//"_qs
).isAbsolute(), true);
149 QCOMPARE(Path(uR
"(\\)"_qs
).isAbsolute(), true);
150 QCOMPARE(Path(u
"//a"_qs
).isAbsolute(), true);
151 QCOMPARE(Path(uR
"(\\a)"_qs
).isAbsolute(), true);
153 QCOMPARE(Path(uR
"(c:)"_qs
).isAbsolute(), true);
154 QCOMPARE(Path(uR
"(c:/)"_qs
).isAbsolute(), true);
155 QCOMPARE(Path(uR
"(c:\)"_qs
).isAbsolute(), true);
156 QCOMPARE(Path(uR
"(c:\a)"_qs
).isAbsolute(), true);
157 QCOMPARE(Path(uR
"(c:\a\b)"_qs
).isAbsolute(), true);
159 QCOMPARE(Path(uR
"(\\?\C:)"_qs
).isAbsolute(), true);
160 QCOMPARE(Path(uR
"(\\?\C:/)"_qs
).isAbsolute(), true);
161 QCOMPARE(Path(uR
"(\\?\C:\)"_qs
).isAbsolute(), true);
162 QCOMPARE(Path(uR
"(\\?\C:\a)"_qs
).isAbsolute(), true);
163 QCOMPARE(Path(uR
"(\\?\C:\a\b)"_qs
).isAbsolute(), true);
165 QCOMPARE(Path(u
"/"_qs
).isAbsolute(), true);
166 QCOMPARE(Path(uR
"(\)"_qs
).isAbsolute(), false);
168 QCOMPARE(Path(u
"a"_qs
).isAbsolute(), false);
169 QCOMPARE(Path(u
"/a"_qs
).isAbsolute(), true);
170 QCOMPARE(Path(uR
"(\a)"_qs
).isAbsolute(), false);
172 QCOMPARE(Path(u
"//"_qs
).isAbsolute(), true);
173 QCOMPARE(Path(uR
"(\\)"_qs
).isAbsolute(), false);
174 QCOMPARE(Path(u
"//a"_qs
).isAbsolute(), true);
175 QCOMPARE(Path(uR
"(\\a)"_qs
).isAbsolute(), false);
179 void testIsRelative() const
181 QCOMPARE(Path().isRelative(), true);
182 QCOMPARE(Path(u
""_qs
).isRelative(), true);
185 QCOMPARE(Path(u
"/"_qs
).isRelative(), false);
186 QCOMPARE(Path(uR
"(\)"_qs
).isRelative(), false);
188 QCOMPARE(Path(u
"a"_qs
).isRelative(), true);
189 QCOMPARE(Path(u
"/a"_qs
).isRelative(), false);
190 QCOMPARE(Path(uR
"(\a)"_qs
).isRelative(), false);
192 QCOMPARE(Path(u
"//"_qs
).isRelative(), false);
193 QCOMPARE(Path(uR
"(\\)"_qs
).isRelative(), false);
194 QCOMPARE(Path(u
"//a"_qs
).isRelative(), false);
195 QCOMPARE(Path(uR
"(\\a)"_qs
).isRelative(), false);
197 QCOMPARE(Path(uR
"(c:)"_qs
).isRelative(), false);
198 QCOMPARE(Path(uR
"(c:/)"_qs
).isRelative(), false);
199 QCOMPARE(Path(uR
"(c:\)"_qs
).isRelative(), false);
200 QCOMPARE(Path(uR
"(c:\a)"_qs
).isRelative(), false);
201 QCOMPARE(Path(uR
"(c:\a\b)"_qs
).isRelative(), false);
203 QCOMPARE(Path(uR
"(\\?\C:)"_qs
).isRelative(), false);
204 QCOMPARE(Path(uR
"(\\?\C:/)"_qs
).isRelative(), false);
205 QCOMPARE(Path(uR
"(\\?\C:\)"_qs
).isRelative(), false);
206 QCOMPARE(Path(uR
"(\\?\C:\a)"_qs
).isRelative(), false);
207 QCOMPARE(Path(uR
"(\\?\C:\a\b)"_qs
).isRelative(), false);
209 QCOMPARE(Path(u
"/"_qs
).isRelative(), false);
210 QCOMPARE(Path(uR
"(\)"_qs
).isRelative(), true);
212 QCOMPARE(Path(u
"a"_qs
).isRelative(), true);
213 QCOMPARE(Path(u
"/a"_qs
).isRelative(), false);
214 QCOMPARE(Path(uR
"(\a)"_qs
).isRelative(), true);
216 QCOMPARE(Path(u
"//"_qs
).isRelative(), false);
217 QCOMPARE(Path(uR
"(\\)"_qs
).isRelative(), true);
218 QCOMPARE(Path(u
"//a"_qs
).isRelative(), false);
219 QCOMPARE(Path(uR
"(\\a)"_qs
).isRelative(), true);
223 void testRootItem() const
225 QCOMPARE(Path().rootItem(), Path());
226 QCOMPARE(Path(u
""_qs
).rootItem(), Path());
228 QCOMPARE(Path(u
"/"_qs
).rootItem(), Path(u
"/"_qs
));
229 QCOMPARE(Path(uR
"(\)"_qs
).rootItem(), Path(uR
"(\)"_qs
));
231 QCOMPARE(Path(u
"a"_qs
).rootItem(), Path(u
"a"_qs
));
232 QCOMPARE(Path(u
"/a"_qs
).rootItem(), Path(u
"/"_qs
));
233 QCOMPARE(Path(u
"/a/b"_qs
).rootItem(), Path(u
"/"_qs
));
235 QCOMPARE(Path(u
"//"_qs
).rootItem(), Path(u
"/"_qs
));
236 QCOMPARE(Path(uR
"(\\)"_qs
).rootItem(), Path(uR
"(\\)"_qs
));
237 QCOMPARE(Path(u
"//a"_qs
).rootItem(), Path(u
"/"_qs
));
240 QCOMPARE(Path(uR
"(\a)"_qs
).rootItem(), Path(uR
"(\)"_qs
));
241 QCOMPARE(Path(uR
"(\\a)"_qs
).rootItem(), Path(uR
"(\)"_qs
));
243 QCOMPARE(Path(uR
"(c:)"_qs
).rootItem(), Path(uR
"(c:)"_qs
));
244 QCOMPARE(Path(uR
"(c:/)"_qs
).rootItem(), Path(uR
"(c:/)"_qs
));
245 QCOMPARE(Path(uR
"(c:\)"_qs
).rootItem(), Path(uR
"(c:\)"_qs
));
246 QCOMPARE(Path(uR
"(c:\)"_qs
).rootItem(), Path(uR
"(c:/)"_qs
));
247 QCOMPARE(Path(uR
"(c:\a)"_qs
).rootItem(), Path(uR
"(c:\)"_qs
));
248 QCOMPARE(Path(uR
"(c:\a\b)"_qs
).rootItem(), Path(uR
"(c:\)"_qs
));
250 QCOMPARE(Path(uR
"(\a)"_qs
).rootItem(), Path(uR
"(\a)"_qs
));
251 QCOMPARE(Path(uR
"(\\a)"_qs
).rootItem(), Path(uR
"(\\a)"_qs
));
255 void testParentPath() const
257 QCOMPARE(Path().parentPath(), Path());
258 QCOMPARE(Path(u
""_qs
).parentPath(), Path());
260 QCOMPARE(Path(u
"/"_qs
).parentPath(), Path());
261 QCOMPARE(Path(uR
"(\)"_qs
).parentPath(), Path());
263 QCOMPARE(Path(u
"a"_qs
).parentPath(), Path());
264 QCOMPARE(Path(u
"/a"_qs
).parentPath(), Path(u
"/"_qs
));
266 QCOMPARE(Path(u
"//"_qs
).parentPath(), Path());
267 QCOMPARE(Path(uR
"(\\)"_qs
).parentPath(), Path());
268 QCOMPARE(Path(u
"//a"_qs
).parentPath(), Path(u
"/"_qs
));
270 QCOMPARE(Path(u
"a/b"_qs
).parentPath(), Path(u
"a"_qs
));
273 QCOMPARE(Path(uR
"(\a)"_qs
).parentPath(), Path(uR
"(\)"_qs
));
274 QCOMPARE(Path(uR
"(\\a)"_qs
).parentPath(), Path(uR
"(\)"_qs
));
275 QCOMPARE(Path(uR
"(a\b)"_qs
).parentPath(), Path(u
"a"_qs
));
277 QCOMPARE(Path(uR
"(c:)"_qs
).parentPath(), Path());
278 QCOMPARE(Path(uR
"(c:/)"_qs
).parentPath(), Path());
279 QCOMPARE(Path(uR
"(c:\)"_qs
).parentPath(), Path());
280 QCOMPARE(Path(uR
"(c:\a)"_qs
).parentPath(), Path(uR
"(c:\)"_qs
));
281 QCOMPARE(Path(uR
"(c:\a\b)"_qs
).parentPath(), Path(uR
"(c:\a)"_qs
));
283 QCOMPARE(Path(uR
"(\a)"_qs
).parentPath(), Path());
284 QCOMPARE(Path(uR
"(\\a)"_qs
).parentPath(), Path());
285 QCOMPARE(Path(uR
"(a\b)"_qs
).parentPath(), Path());
289 // TODO: add tests for remaining methods
292 QTEST_APPLESS_MAIN(TestPath
)
293 #include "testpath.moc"