Improve GUI behavior when adding multiple torrents
[qBittorrent.git] / test / testutilscompare.cpp
blob980f1895cdab22785f253763984706f80abe7e06
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 <QObject>
32 #include <QTest>
34 #include "base/global.h"
35 #include "base/utils/compare.h"
37 #ifndef QBT_USE_QCOLLATOR // only test qbt own implementation, not QCollator
38 namespace
40 enum class CompareResult
42 Equal,
43 Greater,
44 Less
47 struct TestData
49 QString lhs;
50 QString rhs;
51 CompareResult caseInsensitiveResult = CompareResult::Equal;
52 CompareResult caseSensitiveResult = CompareResult::Equal;
55 const TestData testData[] =
57 {u""_s, u""_s, CompareResult::Equal, CompareResult::Equal},
58 {u""_s, u"a"_s, CompareResult::Less, CompareResult::Less},
59 {u"a"_s, u""_s, CompareResult::Greater, CompareResult::Greater},
61 {u"a"_s, u"a"_s, CompareResult::Equal, CompareResult::Equal},
62 {u"A"_s, u"a"_s, CompareResult::Equal, CompareResult::Less}, // ascii code of 'A' is smaller than 'a'
63 {u"a"_s, u"A"_s, CompareResult::Equal, CompareResult::Greater},
65 {u"0"_s, u"0"_s, CompareResult::Equal, CompareResult::Equal},
66 {u"1"_s, u"0"_s, CompareResult::Greater, CompareResult::Greater},
67 {u"0"_s, u"1"_s, CompareResult::Less, CompareResult::Less},
69 {u"😀"_s, u"😀"_s, CompareResult::Equal, CompareResult::Equal},
70 {u"😀"_s, u"😁"_s, CompareResult::Less, CompareResult::Less},
71 {u"😁"_s, u"😀"_s, CompareResult::Greater, CompareResult::Greater},
73 {u"a1"_s, u"a1"_s, CompareResult::Equal, CompareResult::Equal},
74 {u"A1"_s, u"a1"_s, CompareResult::Equal, CompareResult::Less},
75 {u"a1"_s, u"A1"_s, CompareResult::Equal, CompareResult::Greater},
77 {u"a1"_s, u"a2"_s, CompareResult::Less, 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},
82 {u"abc100"_s, u"abc99"_s, CompareResult::Greater, CompareResult::Greater},
83 {u"ABC100"_s, u"abc99"_s, CompareResult::Greater, CompareResult::Less},
84 {u"abc100"_s, u"ABC99"_s, CompareResult::Greater, CompareResult::Greater},
85 {u"ABC100"_s, u"ABC99"_s, CompareResult::Greater, CompareResult::Greater},
87 {u"100abc"_s, u"99abc"_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},
92 {u"😀😀😀99"_s, u"😀😀😀100"_s, CompareResult::Less, CompareResult::Less},
93 {u"😀😀😀100"_s, u"😀😀😀99"_s, CompareResult::Greater, CompareResult::Greater}
96 void testCompare(const TestData &data, const int actual, const CompareResult expected)
98 const auto errorMessage = u"Wrong result. LHS: \"%1\". RHS: \"%2\". Result: %3"_s
99 .arg(data.lhs, data.rhs, QString::number(actual));
101 switch (expected)
103 case CompareResult::Equal:
104 QVERIFY2((actual == 0), qPrintable(errorMessage));
105 break;
106 case CompareResult::Greater:
107 QVERIFY2((actual > 0), qPrintable(errorMessage));
108 break;
109 case CompareResult::Less:
110 QVERIFY2((actual < 0), qPrintable(errorMessage));
111 break;
112 default:
113 QFAIL("Unhandled case");
114 break;
118 void testLessThan(const TestData &data, const bool actual, const CompareResult expected)
120 const auto errorMessage = u"Wrong result. LHS: \"%1\". RHS: \"%2\". Result: %3"_s
121 .arg(data.lhs, data.rhs, QString::number(actual));
123 switch (expected)
125 case CompareResult::Equal:
126 case CompareResult::Greater:
127 QVERIFY2(!actual, qPrintable(errorMessage));
128 break;
129 case CompareResult::Less:
130 QVERIFY2(actual, qPrintable(errorMessage));
131 break;
132 default:
133 QFAIL("Unhandled case");
134 break;
138 #endif
140 class TestUtilsCompare final : public QObject
142 Q_OBJECT
143 Q_DISABLE_COPY_MOVE(TestUtilsCompare)
145 public:
146 TestUtilsCompare() = default;
148 #ifndef QBT_USE_QCOLLATOR // only test qbt own implementation, not QCollator
149 private slots:
150 void testNaturalCompareCaseInsensitive() const
152 const Utils::Compare::NaturalCompare<Qt::CaseInsensitive> cmp;
154 for (const TestData &data : testData)
155 testCompare(data, cmp(data.lhs, data.rhs), data.caseInsensitiveResult);
158 void testNaturalCompareCaseSensitive() const
160 const Utils::Compare::NaturalCompare<Qt::CaseSensitive> cmp;
162 for (const TestData &data : testData)
163 testCompare(data, cmp(data.lhs, data.rhs), data.caseSensitiveResult);
166 void testNaturalLessThanCaseInsensitive() const
168 const Utils::Compare::NaturalLessThan<Qt::CaseInsensitive> cmp {};
170 for (const TestData &data : testData)
171 testLessThan(data, cmp(data.lhs, data.rhs), data.caseInsensitiveResult);
174 void testNaturalLessThanCaseSensitive() const
176 const Utils::Compare::NaturalLessThan<Qt::CaseSensitive> cmp {};
178 for (const TestData &data : testData)
179 testLessThan(data, cmp(data.lhs, data.rhs), data.caseSensitiveResult);
181 #endif
184 QTEST_APPLESS_MAIN(TestUtilsCompare)
185 #include "testutilscompare.moc"