Add name to threads
[qBittorrent.git] / test / testutilscompare.cpp
blob65edde99226086f8ffa1313a3f499921689c1b6a
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 <QLocale>
30 #include <QObject>
31 #include <QTest>
33 #include "base/global.h"
35 // only test qbt own implementation, not QCollator
36 #define QBT_USE_QCOLLATOR 0
37 #include "base/utils/compare.h"
39 namespace
41 enum class CompareResult
43 Equal,
44 Greater,
45 Less
48 struct TestData
50 QString lhs;
51 QString rhs;
52 CompareResult caseInsensitiveResult = CompareResult::Equal;
53 CompareResult caseSensitiveResult = CompareResult::Equal;
56 const TestData testData[] =
58 {u""_s, u""_s, CompareResult::Equal, CompareResult::Equal},
59 {u""_s, u"a"_s, CompareResult::Less, CompareResult::Less},
60 {u"a"_s, u""_s, CompareResult::Greater, CompareResult::Greater},
62 {u"a"_s, u"a"_s, CompareResult::Equal, CompareResult::Equal},
63 {u"A"_s, u"a"_s, CompareResult::Equal, CompareResult::Greater},
64 {u"a"_s, u"A"_s, CompareResult::Equal, CompareResult::Less},
66 {u"0"_s, u"0"_s, CompareResult::Equal, CompareResult::Equal},
67 {u"1"_s, u"0"_s, CompareResult::Greater, CompareResult::Greater},
68 {u"0"_s, u"1"_s, CompareResult::Less, CompareResult::Less},
70 {u"😀"_s, u"😀"_s, CompareResult::Equal, CompareResult::Equal},
71 {u"😀"_s, u"😁"_s, CompareResult::Less, CompareResult::Less},
72 {u"😁"_s, u"😀"_s, CompareResult::Greater, CompareResult::Greater},
74 {u"a1"_s, u"a1"_s, CompareResult::Equal, CompareResult::Equal},
75 {u"A1"_s, u"a1"_s, CompareResult::Equal, CompareResult::Greater},
76 {u"a1"_s, u"A1"_s, CompareResult::Equal, CompareResult::Less},
78 {u"a1"_s, u"a2"_s, CompareResult::Less, CompareResult::Less},
79 {u"A1"_s, u"a2"_s, CompareResult::Less, CompareResult::Greater},
80 {u"a1"_s, u"A2"_s, CompareResult::Less, CompareResult::Less},
81 {u"A1"_s, u"A2"_s, CompareResult::Less, CompareResult::Less},
83 {u"abc100"_s, u"abc99"_s, CompareResult::Greater, CompareResult::Greater},
84 {u"ABC100"_s, u"abc99"_s, CompareResult::Greater, CompareResult::Greater},
85 {u"abc100"_s, u"ABC99"_s, CompareResult::Greater, CompareResult::Less},
86 {u"ABC100"_s, u"ABC99"_s, CompareResult::Greater, CompareResult::Greater},
88 {u"100abc"_s, u"99abc"_s, CompareResult::Greater, CompareResult::Greater},
89 {u"100ABC"_s, u"99abc"_s, CompareResult::Greater, CompareResult::Greater},
90 {u"100abc"_s, u"99ABC"_s, CompareResult::Greater, CompareResult::Greater},
91 {u"100ABC"_s, u"99ABC"_s, CompareResult::Greater, CompareResult::Greater},
93 {u"😀😀😀99"_s, u"😀😀😀100"_s, CompareResult::Less, CompareResult::Less},
94 {u"😀😀😀100"_s, u"😀😀😀99"_s, CompareResult::Greater, CompareResult::Greater}
97 void testCompare(const TestData &data, const int actual, const CompareResult expected)
99 const auto errorMessage = u"Wrong result. LHS: \"%1\". RHS: \"%2\". Result: %3"_s
100 .arg(data.lhs, data.rhs, QString::number(actual));
102 switch (expected)
104 case CompareResult::Equal:
105 QVERIFY2((actual == 0), qPrintable(errorMessage));
106 break;
107 case CompareResult::Greater:
108 QVERIFY2((actual > 0), qPrintable(errorMessage));
109 break;
110 case CompareResult::Less:
111 QVERIFY2((actual < 0), qPrintable(errorMessage));
112 break;
113 default:
114 QFAIL("Unhandled case");
115 break;
119 void testLessThan(const TestData &data, const bool actual, const CompareResult expected)
121 const auto errorMessage = u"Wrong result. LHS: \"%1\". RHS: \"%2\". Result: %3"_s
122 .arg(data.lhs, data.rhs, QString::number(actual));
124 switch (expected)
126 case CompareResult::Equal:
127 case CompareResult::Greater:
128 QVERIFY2(!actual, qPrintable(errorMessage));
129 break;
130 case CompareResult::Less:
131 QVERIFY2(actual, qPrintable(errorMessage));
132 break;
133 default:
134 QFAIL("Unhandled case");
135 break;
140 class TestUtilsCompare final : public QObject
142 Q_OBJECT
143 Q_DISABLE_COPY_MOVE(TestUtilsCompare)
145 public:
146 TestUtilsCompare() = default;
148 private slots:
149 void initTestCase() const
151 // Test will fail if ran with `C` locale. This is because `C` locale compare chars by code points
152 // and doesn't take account of human expectations
153 QLocale::setDefault(QLocale::English);
156 void cleanupTestCase() const
158 // restore global state
159 QLocale::setDefault(QLocale::system());
162 void testNaturalCompareCaseInsensitive() const
164 const Utils::Compare::NaturalCompare<Qt::CaseInsensitive> cmp;
166 for (const TestData &data : testData)
167 testCompare(data, cmp(data.lhs, data.rhs), data.caseInsensitiveResult);
170 void testNaturalCompareCaseSensitive() const
172 const Utils::Compare::NaturalCompare<Qt::CaseSensitive> cmp;
174 for (const TestData &data : testData)
175 testCompare(data, cmp(data.lhs, data.rhs), data.caseSensitiveResult);
178 void testNaturalLessThanCaseInsensitive() const
180 const Utils::Compare::NaturalLessThan<Qt::CaseInsensitive> cmp {};
182 for (const TestData &data : testData)
183 testLessThan(data, cmp(data.lhs, data.rhs), data.caseInsensitiveResult);
186 void testNaturalLessThanCaseSensitive() const
188 const Utils::Compare::NaturalLessThan<Qt::CaseSensitive> cmp {};
190 for (const TestData &data : testData)
191 testLessThan(data, cmp(data.lhs, data.rhs), data.caseSensitiveResult);
195 QTEST_APPLESS_MAIN(TestUtilsCompare)
196 #include "testutilscompare.moc"