WebUI: enforce parentheses around operators
[qBittorrent.git] / test / testpath.cpp
blob6449a8071437202689b8971e48c68bdb2f3c238f
1 /*
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.
29 #include <QtSystemDetection>
30 #include <QObject>
31 #include <QTest>
33 #include "base/global.h"
34 #include "base/path.h"
36 class TestPath final : public QObject
38 Q_OBJECT
39 Q_DISABLE_COPY_MOVE(TestPath)
41 public:
42 TestPath() = default;
44 private slots:
45 void testConstructors() const
47 QVERIFY(Path(u""_s) == Path(std::string("")));
48 QVERIFY(Path(u"abc"_s) == Path(std::string("abc")));
49 QVERIFY(Path(u"/abc"_s) == Path(std::string("/abc")));
50 QVERIFY(Path(uR"(\abc)"_s) == Path(std::string(R"(\abc)")));
52 #ifdef Q_OS_WIN
53 QVERIFY(Path(uR"(c:)"_s) == Path(std::string(R"(c:)")));
54 QVERIFY(Path(uR"(c:/)"_s) == Path(std::string(R"(c:/)")));
55 QVERIFY(Path(uR"(c:/)"_s) == Path(std::string(R"(c:\)")));
56 QVERIFY(Path(uR"(c:\)"_s) == Path(std::string(R"(c:/)")));
57 QVERIFY(Path(uR"(c:\)"_s) == Path(std::string(R"(c:\)")));
59 QVERIFY(Path(uR"(\\?\C:)"_s) == Path(std::string(R"(\\?\C:)")));
60 QVERIFY(Path(uR"(\\?\C:/)"_s) == Path(std::string(R"(\\?\C:/)")));
61 QVERIFY(Path(uR"(\\?\C:/)"_s) == Path(std::string(R"(\\?\C:\)")));
62 QVERIFY(Path(uR"(\\?\C:\)"_s) == Path(std::string(R"(\\?\C:/)")));
63 QVERIFY(Path(uR"(\\?\C:\)"_s) == Path(std::string(R"(\\?\C:\)")));
65 QVERIFY(Path(uR"(\\?\C:\abc)"_s) == Path(std::string(R"(\\?\C:\abc)")));
66 #endif
69 void testIsValid() const
71 QCOMPARE(Path().isValid(), false);
72 QCOMPARE(Path(u""_s).isValid(), false);
74 QCOMPARE(Path(u"/"_s).isValid(), true);
75 QCOMPARE(Path(uR"(\)"_s).isValid(), true);
77 QCOMPARE(Path(u"a"_s).isValid(), true);
78 QCOMPARE(Path(u"/a"_s).isValid(), true);
79 QCOMPARE(Path(uR"(\a)"_s).isValid(), true);
81 QCOMPARE(Path(u"a/b"_s).isValid(), true);
82 QCOMPARE(Path(uR"(a\b)"_s).isValid(), true);
83 QCOMPARE(Path(u"/a/b"_s).isValid(), true);
84 QCOMPARE(Path(uR"(/a\b)"_s).isValid(), true);
85 QCOMPARE(Path(uR"(\a/b)"_s).isValid(), true);
86 QCOMPARE(Path(uR"(\a\b)"_s).isValid(), true);
88 QCOMPARE(Path(u"//"_s).isValid(), true);
89 QCOMPARE(Path(uR"(\\)"_s).isValid(), true);
90 QCOMPARE(Path(u"//a"_s).isValid(), true);
91 QCOMPARE(Path(uR"(\\a)"_s).isValid(), true);
93 #if defined Q_OS_MACOS
94 QCOMPARE(Path(u"\0"_s).isValid(), false);
95 QCOMPARE(Path(u":"_s).isValid(), false);
96 #elif defined Q_OS_WIN
97 QCOMPARE(Path(u"c:"_s).isValid(), false);
98 QCOMPARE(Path(u"c:/"_s).isValid(), true);
99 QCOMPARE(Path(uR"(c:\)"_s).isValid(), true);
100 QCOMPARE(Path(uR"(c:\a)"_s).isValid(), true);
101 QCOMPARE(Path(uR"(c:\a\b)"_s).isValid(), true);
103 for (int i = 0; i <= 31; ++i)
104 QCOMPARE(Path(QChar(i)).isValid(), false);
105 QCOMPARE(Path(u":"_s).isValid(), false);
106 QCOMPARE(Path(u"?"_s).isValid(), false);
107 QCOMPARE(Path(u"\""_s).isValid(), false);
108 QCOMPARE(Path(u"*"_s).isValid(), false);
109 QCOMPARE(Path(u"<"_s).isValid(), false);
110 QCOMPARE(Path(u">"_s).isValid(), false);
111 QCOMPARE(Path(u"|"_s).isValid(), false);
112 #else
113 QCOMPARE(Path(u"\0"_s).isValid(), false);
114 #endif
117 void testIsEmpty() const
119 QCOMPARE(Path().isEmpty(), true);
120 QCOMPARE(Path(u""_s).isEmpty(), true);
122 QCOMPARE(Path(u"\0"_s).isEmpty(), false);
124 QCOMPARE(Path(u"/"_s).isEmpty(), false);
125 QCOMPARE(Path(uR"(\)"_s).isEmpty(), false);
127 QCOMPARE(Path(u"a"_s).isEmpty(), false);
128 QCOMPARE(Path(u"/a"_s).isEmpty(), false);
129 QCOMPARE(Path(uR"(\a)"_s).isEmpty(), false);
131 QCOMPARE(Path(uR"(c:)"_s).isEmpty(), false);
132 QCOMPARE(Path(uR"(c:/)"_s).isEmpty(), false);
133 QCOMPARE(Path(uR"(c:\)"_s).isEmpty(), false);
136 void testIsAbsolute() const
138 QCOMPARE(Path().isAbsolute(), false);
139 QCOMPARE(Path(u""_s).isAbsolute(), false);
141 #ifdef Q_OS_WIN
142 QCOMPARE(Path(u"/"_s).isAbsolute(), true);
143 QCOMPARE(Path(uR"(\)"_s).isAbsolute(), true);
145 QCOMPARE(Path(u"a"_s).isAbsolute(), false);
146 QCOMPARE(Path(u"/a"_s).isAbsolute(), true);
147 QCOMPARE(Path(uR"(\a)"_s).isAbsolute(), true);
149 QCOMPARE(Path(u"//"_s).isAbsolute(), true);
150 QCOMPARE(Path(uR"(\\)"_s).isAbsolute(), true);
151 QCOMPARE(Path(u"//a"_s).isAbsolute(), true);
152 QCOMPARE(Path(uR"(\\a)"_s).isAbsolute(), true);
154 QCOMPARE(Path(uR"(c:)"_s).isAbsolute(), true);
155 QCOMPARE(Path(uR"(c:/)"_s).isAbsolute(), true);
156 QCOMPARE(Path(uR"(c:\)"_s).isAbsolute(), true);
157 QCOMPARE(Path(uR"(c:\a)"_s).isAbsolute(), true);
158 QCOMPARE(Path(uR"(c:\a\b)"_s).isAbsolute(), true);
160 QCOMPARE(Path(uR"(\\?\C:)"_s).isAbsolute(), true);
161 QCOMPARE(Path(uR"(\\?\C:/)"_s).isAbsolute(), true);
162 QCOMPARE(Path(uR"(\\?\C:\)"_s).isAbsolute(), true);
163 QCOMPARE(Path(uR"(\\?\C:\a)"_s).isAbsolute(), true);
164 QCOMPARE(Path(uR"(\\?\C:\a\b)"_s).isAbsolute(), true);
165 #else
166 QCOMPARE(Path(u"/"_s).isAbsolute(), true);
167 QCOMPARE(Path(uR"(\)"_s).isAbsolute(), false);
169 QCOMPARE(Path(u"a"_s).isAbsolute(), false);
170 QCOMPARE(Path(u"/a"_s).isAbsolute(), true);
171 QCOMPARE(Path(uR"(\a)"_s).isAbsolute(), false);
173 QCOMPARE(Path(u"//"_s).isAbsolute(), true);
174 QCOMPARE(Path(uR"(\\)"_s).isAbsolute(), false);
175 QCOMPARE(Path(u"//a"_s).isAbsolute(), true);
176 QCOMPARE(Path(uR"(\\a)"_s).isAbsolute(), false);
177 #endif
180 void testIsRelative() const
182 QCOMPARE(Path().isRelative(), true);
183 QCOMPARE(Path(u""_s).isRelative(), true);
185 #if defined Q_OS_WIN
186 QCOMPARE(Path(u"/"_s).isRelative(), false);
187 QCOMPARE(Path(uR"(\)"_s).isRelative(), false);
189 QCOMPARE(Path(u"a"_s).isRelative(), true);
190 QCOMPARE(Path(u"/a"_s).isRelative(), false);
191 QCOMPARE(Path(uR"(\a)"_s).isRelative(), false);
193 QCOMPARE(Path(u"//"_s).isRelative(), false);
194 QCOMPARE(Path(uR"(\\)"_s).isRelative(), false);
195 QCOMPARE(Path(u"//a"_s).isRelative(), false);
196 QCOMPARE(Path(uR"(\\a)"_s).isRelative(), false);
198 QCOMPARE(Path(uR"(c:)"_s).isRelative(), false);
199 QCOMPARE(Path(uR"(c:/)"_s).isRelative(), false);
200 QCOMPARE(Path(uR"(c:\)"_s).isRelative(), false);
201 QCOMPARE(Path(uR"(c:\a)"_s).isRelative(), false);
202 QCOMPARE(Path(uR"(c:\a\b)"_s).isRelative(), false);
204 QCOMPARE(Path(uR"(\\?\C:)"_s).isRelative(), false);
205 QCOMPARE(Path(uR"(\\?\C:/)"_s).isRelative(), false);
206 QCOMPARE(Path(uR"(\\?\C:\)"_s).isRelative(), false);
207 QCOMPARE(Path(uR"(\\?\C:\a)"_s).isRelative(), false);
208 QCOMPARE(Path(uR"(\\?\C:\a\b)"_s).isRelative(), false);
209 #else
210 QCOMPARE(Path(u"/"_s).isRelative(), false);
211 QCOMPARE(Path(uR"(\)"_s).isRelative(), true);
213 QCOMPARE(Path(u"a"_s).isRelative(), true);
214 QCOMPARE(Path(u"/a"_s).isRelative(), false);
215 QCOMPARE(Path(uR"(\a)"_s).isRelative(), true);
217 QCOMPARE(Path(u"//"_s).isRelative(), false);
218 QCOMPARE(Path(uR"(\\)"_s).isRelative(), true);
219 QCOMPARE(Path(u"//a"_s).isRelative(), false);
220 QCOMPARE(Path(uR"(\\a)"_s).isRelative(), true);
221 #endif
224 void testRootItem() const
226 QCOMPARE(Path().rootItem(), Path());
227 QCOMPARE(Path(u""_s).rootItem(), Path());
229 QCOMPARE(Path(u"/"_s).rootItem(), Path(u"/"_s));
230 QCOMPARE(Path(uR"(\)"_s).rootItem(), Path(uR"(\)"_s));
232 QCOMPARE(Path(u"a"_s).rootItem(), Path(u"a"_s));
233 QCOMPARE(Path(u"/a"_s).rootItem(), Path(u"/"_s));
234 QCOMPARE(Path(u"/a/b"_s).rootItem(), Path(u"/"_s));
236 QCOMPARE(Path(u"//"_s).rootItem(), Path(u"/"_s));
237 QCOMPARE(Path(uR"(\\)"_s).rootItem(), Path(uR"(\\)"_s));
238 QCOMPARE(Path(u"//a"_s).rootItem(), Path(u"/"_s));
240 #ifdef Q_OS_WIN
241 QCOMPARE(Path(uR"(\a)"_s).rootItem(), Path(uR"(\)"_s));
242 QCOMPARE(Path(uR"(\\a)"_s).rootItem(), Path(uR"(\)"_s));
244 QCOMPARE(Path(uR"(c:)"_s).rootItem(), Path(uR"(c:)"_s));
245 QCOMPARE(Path(uR"(c:/)"_s).rootItem(), Path(uR"(c:/)"_s));
246 QCOMPARE(Path(uR"(c:\)"_s).rootItem(), Path(uR"(c:\)"_s));
247 QCOMPARE(Path(uR"(c:\)"_s).rootItem(), Path(uR"(c:/)"_s));
248 QCOMPARE(Path(uR"(c:\a)"_s).rootItem(), Path(uR"(c:\)"_s));
249 QCOMPARE(Path(uR"(c:\a\b)"_s).rootItem(), Path(uR"(c:\)"_s));
250 #else
251 QCOMPARE(Path(uR"(\a)"_s).rootItem(), Path(uR"(\a)"_s));
252 QCOMPARE(Path(uR"(\\a)"_s).rootItem(), Path(uR"(\\a)"_s));
253 #endif
256 void testParentPath() const
258 QCOMPARE(Path().parentPath(), Path());
259 QCOMPARE(Path(u""_s).parentPath(), Path());
261 QCOMPARE(Path(u"/"_s).parentPath(), Path());
262 QCOMPARE(Path(uR"(\)"_s).parentPath(), Path());
264 QCOMPARE(Path(u"a"_s).parentPath(), Path());
265 QCOMPARE(Path(u"/a"_s).parentPath(), Path(u"/"_s));
267 QCOMPARE(Path(u"//"_s).parentPath(), Path());
268 QCOMPARE(Path(uR"(\\)"_s).parentPath(), Path());
269 QCOMPARE(Path(u"//a"_s).parentPath(), Path(u"/"_s));
271 QCOMPARE(Path(u"a/b"_s).parentPath(), Path(u"a"_s));
273 #ifdef Q_OS_WIN
274 QCOMPARE(Path(uR"(\a)"_s).parentPath(), Path(uR"(\)"_s));
275 QCOMPARE(Path(uR"(\\a)"_s).parentPath(), Path(uR"(\)"_s));
276 QCOMPARE(Path(uR"(a\b)"_s).parentPath(), Path(u"a"_s));
278 QCOMPARE(Path(uR"(c:)"_s).parentPath(), Path());
279 QCOMPARE(Path(uR"(c:/)"_s).parentPath(), Path());
280 QCOMPARE(Path(uR"(c:\)"_s).parentPath(), Path());
281 QCOMPARE(Path(uR"(c:\a)"_s).parentPath(), Path(uR"(c:\)"_s));
282 QCOMPARE(Path(uR"(c:\a\b)"_s).parentPath(), Path(uR"(c:\a)"_s));
283 #else
284 QCOMPARE(Path(uR"(\a)"_s).parentPath(), Path());
285 QCOMPARE(Path(uR"(\\a)"_s).parentPath(), Path());
286 QCOMPARE(Path(uR"(a\b)"_s).parentPath(), Path());
287 #endif
290 // TODO: add tests for remaining methods
293 QTEST_APPLESS_MAIN(TestPath)
294 #include "testpath.moc"