Provide UI Theme editor
[qBittorrent.git] / test / testutilscompare.cpp
blob0e022c58c8503f4e4e8a07d72abb6bd003505b33
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 <tuple>
31 #include <QTest>
33 #include "base/global.h"
34 #include "base/utils/compare.h"
36 #ifndef QBT_USE_QCOLLATOR // only test qbt own implementation, not QCollator
37 namespace
39 enum class CompareResult
41 Equal,
42 Greater,
43 Less
46 struct TestData
48 QString lhs;
49 QString rhs;
50 CompareResult caseInsensitiveResult = CompareResult::Equal;
51 CompareResult caseSensitiveResult = CompareResult::Equal;
54 const TestData testData[] =
56 {u""_qs, u""_qs, CompareResult::Equal, CompareResult::Equal},
57 {u""_qs, u"a"_qs, CompareResult::Less, CompareResult::Less},
58 {u"a"_qs, u""_qs, CompareResult::Greater, CompareResult::Greater},
60 {u"a"_qs, u"a"_qs, CompareResult::Equal, CompareResult::Equal},
61 {u"A"_qs, u"a"_qs, CompareResult::Equal, CompareResult::Less}, // ascii code of 'A' is smaller than 'a'
62 {u"a"_qs, u"A"_qs, CompareResult::Equal, CompareResult::Greater},
64 {u"0"_qs, u"0"_qs, CompareResult::Equal, CompareResult::Equal},
65 {u"1"_qs, u"0"_qs, CompareResult::Greater, CompareResult::Greater},
66 {u"0"_qs, u"1"_qs, CompareResult::Less, CompareResult::Less},
68 {u"😀"_qs, u"😀"_qs, CompareResult::Equal, CompareResult::Equal},
69 {u"😀"_qs, u"😁"_qs, CompareResult::Less, CompareResult::Less},
70 {u"😁"_qs, u"😀"_qs, CompareResult::Greater, CompareResult::Greater},
72 {u"a1"_qs, u"a1"_qs, CompareResult::Equal, CompareResult::Equal},
73 {u"A1"_qs, u"a1"_qs, CompareResult::Equal, CompareResult::Less},
74 {u"a1"_qs, u"A1"_qs, CompareResult::Equal, CompareResult::Greater},
76 {u"a1"_qs, u"a2"_qs, CompareResult::Less, CompareResult::Less},
77 {u"A1"_qs, u"a2"_qs, CompareResult::Less, CompareResult::Less},
78 {u"a1"_qs, u"A2"_qs, CompareResult::Less, CompareResult::Greater},
79 {u"A1"_qs, u"A2"_qs, CompareResult::Less, CompareResult::Less},
81 {u"abc100"_qs, u"abc99"_qs, CompareResult::Greater, CompareResult::Greater},
82 {u"ABC100"_qs, u"abc99"_qs, CompareResult::Greater, CompareResult::Less},
83 {u"abc100"_qs, u"ABC99"_qs, CompareResult::Greater, CompareResult::Greater},
84 {u"ABC100"_qs, u"ABC99"_qs, CompareResult::Greater, CompareResult::Greater},
86 {u"100abc"_qs, u"99abc"_qs, CompareResult::Greater, CompareResult::Greater},
87 {u"100ABC"_qs, u"99abc"_qs, CompareResult::Greater, CompareResult::Greater},
88 {u"100abc"_qs, u"99ABC"_qs, CompareResult::Greater, CompareResult::Greater},
89 {u"100ABC"_qs, u"99ABC"_qs, CompareResult::Greater, CompareResult::Greater},
91 {u"😀😀😀99"_qs, u"😀😀😀100"_qs, CompareResult::Less, CompareResult::Less},
92 {u"😀😀😀100"_qs, u"😀😀😀99"_qs, CompareResult::Greater, CompareResult::Greater}
95 void testCompare(const TestData &data, const int actual, const CompareResult expected)
97 const auto errorMessage = u"Wrong result. LHS: \"%1\". RHS: \"%2\". Result: %3"_qs
98 .arg(data.lhs, data.rhs, QString::number(actual));
100 switch (expected)
102 case CompareResult::Equal:
103 QVERIFY2((actual == 0), qPrintable(errorMessage));
104 break;
105 case CompareResult::Greater:
106 QVERIFY2((actual > 0), qPrintable(errorMessage));
107 break;
108 case CompareResult::Less:
109 QVERIFY2((actual < 0), qPrintable(errorMessage));
110 break;
111 default:
112 QFAIL("Unhandled case");
113 break;
117 void testLessThan(const TestData &data, const bool actual, const CompareResult expected)
119 const auto errorMessage = u"Wrong result. LHS: \"%1\". RHS: \"%2\". Result: %3"_qs
120 .arg(data.lhs, data.rhs, QString::number(actual));
122 switch (expected)
124 case CompareResult::Equal:
125 case CompareResult::Greater:
126 QVERIFY2(!actual, qPrintable(errorMessage));
127 break;
128 case CompareResult::Less:
129 QVERIFY2(actual, qPrintable(errorMessage));
130 break;
131 default:
132 QFAIL("Unhandled case");
133 break;
137 #endif
139 class TestUtilsCompare final : public QObject
141 Q_OBJECT
142 Q_DISABLE_COPY_MOVE(TestUtilsCompare)
144 public:
145 TestUtilsCompare() = default;
147 #ifndef QBT_USE_QCOLLATOR // only test qbt own implementation, not QCollator
148 private slots:
149 void testNaturalCompareCaseInsensitive() const
151 const Utils::Compare::NaturalCompare<Qt::CaseInsensitive> cmp;
153 for (const TestData &data : testData)
154 testCompare(data, cmp(data.lhs, data.rhs), data.caseInsensitiveResult);
157 void testNaturalCompareCaseSensitive() const
159 const Utils::Compare::NaturalCompare<Qt::CaseSensitive> cmp;
161 for (const TestData &data : testData)
162 testCompare(data, cmp(data.lhs, data.rhs), data.caseSensitiveResult);
165 void testNaturalLessThanCaseInsensitive() const
167 const Utils::Compare::NaturalLessThan<Qt::CaseInsensitive> cmp {};
169 for (const TestData &data : testData)
170 testLessThan(data, cmp(data.lhs, data.rhs), data.caseInsensitiveResult);
173 void testNaturalLessThanCaseSensitive() const
175 const Utils::Compare::NaturalLessThan<Qt::CaseSensitive> cmp {};
177 for (const TestData &data : testData)
178 testLessThan(data, cmp(data.lhs, data.rhs), data.caseSensitiveResult);
180 #endif
183 QTEST_APPLESS_MAIN(TestUtilsCompare)
184 #include "testutilscompare.moc"