-
Notifications
You must be signed in to change notification settings - Fork 0
/
testutilscompare.cpp
185 lines (156 loc) · 6.83 KB
/
testutilscompare.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2022 Mike Tzou (Chocobo1)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#include <tuple>
#include <QObject>
#include <QTest>
#include "base/global.h"
#include "base/utils/compare.h"
#ifndef QBT_USE_QCOLLATOR // only test qbt own implementation, not QCollator
namespace
{
enum class CompareResult
{
Equal,
Greater,
Less
};
struct TestData
{
QString lhs;
QString rhs;
CompareResult caseInsensitiveResult = CompareResult::Equal;
CompareResult caseSensitiveResult = CompareResult::Equal;
};
const TestData testData[] =
{
{u""_s, u""_s, CompareResult::Equal, CompareResult::Equal},
{u""_s, u"a"_s, CompareResult::Less, CompareResult::Less},
{u"a"_s, u""_s, CompareResult::Greater, CompareResult::Greater},
{u"a"_s, u"a"_s, CompareResult::Equal, CompareResult::Equal},
{u"A"_s, u"a"_s, CompareResult::Equal, CompareResult::Less}, // ascii code of 'A' is smaller than 'a'
{u"a"_s, u"A"_s, CompareResult::Equal, CompareResult::Greater},
{u"0"_s, u"0"_s, CompareResult::Equal, CompareResult::Equal},
{u"1"_s, u"0"_s, CompareResult::Greater, CompareResult::Greater},
{u"0"_s, u"1"_s, CompareResult::Less, CompareResult::Less},
{u"😀"_s, u"😀"_s, CompareResult::Equal, CompareResult::Equal},
{u"😀"_s, u"😁"_s, CompareResult::Less, CompareResult::Less},
{u"😁"_s, u"😀"_s, CompareResult::Greater, CompareResult::Greater},
{u"a1"_s, u"a1"_s, CompareResult::Equal, CompareResult::Equal},
{u"A1"_s, u"a1"_s, CompareResult::Equal, CompareResult::Less},
{u"a1"_s, u"A1"_s, CompareResult::Equal, CompareResult::Greater},
{u"a1"_s, u"a2"_s, CompareResult::Less, CompareResult::Less},
{u"A1"_s, u"a2"_s, CompareResult::Less, CompareResult::Less},
{u"a1"_s, u"A2"_s, CompareResult::Less, CompareResult::Greater},
{u"A1"_s, u"A2"_s, CompareResult::Less, CompareResult::Less},
{u"abc100"_s, u"abc99"_s, CompareResult::Greater, CompareResult::Greater},
{u"ABC100"_s, u"abc99"_s, CompareResult::Greater, CompareResult::Less},
{u"abc100"_s, u"ABC99"_s, CompareResult::Greater, CompareResult::Greater},
{u"ABC100"_s, u"ABC99"_s, CompareResult::Greater, CompareResult::Greater},
{u"100abc"_s, u"99abc"_s, CompareResult::Greater, CompareResult::Greater},
{u"100ABC"_s, u"99abc"_s, CompareResult::Greater, CompareResult::Greater},
{u"100abc"_s, u"99ABC"_s, CompareResult::Greater, CompareResult::Greater},
{u"100ABC"_s, u"99ABC"_s, CompareResult::Greater, CompareResult::Greater},
{u"😀😀😀99"_s, u"😀😀😀100"_s, CompareResult::Less, CompareResult::Less},
{u"😀😀😀100"_s, u"😀😀😀99"_s, CompareResult::Greater, CompareResult::Greater}
};
void testCompare(const TestData &data, const int actual, const CompareResult expected)
{
const auto errorMessage = u"Wrong result. LHS: \"%1\". RHS: \"%2\". Result: %3"_s
.arg(data.lhs, data.rhs, QString::number(actual));
switch (expected)
{
case CompareResult::Equal:
QVERIFY2((actual == 0), qPrintable(errorMessage));
break;
case CompareResult::Greater:
QVERIFY2((actual > 0), qPrintable(errorMessage));
break;
case CompareResult::Less:
QVERIFY2((actual < 0), qPrintable(errorMessage));
break;
default:
QFAIL("Unhandled case");
break;
}
}
void testLessThan(const TestData &data, const bool actual, const CompareResult expected)
{
const auto errorMessage = u"Wrong result. LHS: \"%1\". RHS: \"%2\". Result: %3"_s
.arg(data.lhs, data.rhs, QString::number(actual));
switch (expected)
{
case CompareResult::Equal:
case CompareResult::Greater:
QVERIFY2(!actual, qPrintable(errorMessage));
break;
case CompareResult::Less:
QVERIFY2(actual, qPrintable(errorMessage));
break;
default:
QFAIL("Unhandled case");
break;
}
}
}
#endif
class TestUtilsCompare final : public QObject
{
Q_OBJECT
Q_DISABLE_COPY_MOVE(TestUtilsCompare)
public:
TestUtilsCompare() = default;
#ifndef QBT_USE_QCOLLATOR // only test qbt own implementation, not QCollator
private slots:
void testNaturalCompareCaseInsensitive() const
{
const Utils::Compare::NaturalCompare<Qt::CaseInsensitive> cmp;
for (const TestData &data : testData)
testCompare(data, cmp(data.lhs, data.rhs), data.caseInsensitiveResult);
}
void testNaturalCompareCaseSensitive() const
{
const Utils::Compare::NaturalCompare<Qt::CaseSensitive> cmp;
for (const TestData &data : testData)
testCompare(data, cmp(data.lhs, data.rhs), data.caseSensitiveResult);
}
void testNaturalLessThanCaseInsensitive() const
{
const Utils::Compare::NaturalLessThan<Qt::CaseInsensitive> cmp {};
for (const TestData &data : testData)
testLessThan(data, cmp(data.lhs, data.rhs), data.caseInsensitiveResult);
}
void testNaturalLessThanCaseSensitive() const
{
const Utils::Compare::NaturalLessThan<Qt::CaseSensitive> cmp {};
for (const TestData &data : testData)
testLessThan(data, cmp(data.lhs, data.rhs), data.caseSensitiveResult);
}
#endif
};
QTEST_APPLESS_MAIN(TestUtilsCompare)
#include "testutilscompare.moc"