mirror of
https://github.com/qbittorrent/qBittorrent.git
synced 2024-11-26 19:26:59 +03:00
Merge pull request #17359 from Chocobo1/testnaturalcmp
Add testing for various classes
This commit is contained in:
commit
864d806ee8
4 changed files with 447 additions and 1 deletions
|
@ -9,7 +9,12 @@ add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure)
|
|||
|
||||
include_directories("../src")
|
||||
|
||||
file(GLOB testFiles "${CMAKE_CURRENT_SOURCE_DIR}/test*.cpp")
|
||||
set(testFiles
|
||||
testalgorithm.cpp
|
||||
testorderedset.cpp
|
||||
testutilscompare.cpp
|
||||
testutilsgzip.cpp
|
||||
)
|
||||
foreach(testFile ${testFiles})
|
||||
get_filename_component(testFilename "${testFile}" NAME_WLE)
|
||||
|
||||
|
|
126
test/testalgorithm.cpp
Normal file
126
test/testalgorithm.cpp
Normal file
|
@ -0,0 +1,126 @@
|
|||
/*
|
||||
* 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 <map>
|
||||
#include <set>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
|
||||
#include <QTest>
|
||||
|
||||
#include "base/algorithm.h"
|
||||
#include "base/global.h"
|
||||
|
||||
class TestAlgorithm final : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_DISABLE_COPY_MOVE(TestAlgorithm)
|
||||
|
||||
public:
|
||||
TestAlgorithm() = default;
|
||||
|
||||
private slots:
|
||||
void testHasMappedType() const
|
||||
{
|
||||
QVERIFY(static_cast<bool>(Algorithm::HasMappedType<std::map<bool, bool>>::value));
|
||||
QVERIFY(static_cast<bool>(Algorithm::HasMappedType<std::unordered_map<bool, bool>>::value));
|
||||
QVERIFY(static_cast<bool>(Algorithm::HasMappedType<QHash<bool, bool>>::value));
|
||||
QVERIFY(static_cast<bool>(Algorithm::HasMappedType<QMap<bool, bool>>::value));
|
||||
|
||||
QVERIFY(!static_cast<bool>(Algorithm::HasMappedType<std::set<bool>>::value));
|
||||
QVERIFY(!static_cast<bool>(Algorithm::HasMappedType<std::unordered_set<bool>>::value));
|
||||
QVERIFY(!static_cast<bool>(Algorithm::HasMappedType<QSet<bool>>::value));
|
||||
}
|
||||
|
||||
void testMappedTypeRemoveIf() const
|
||||
{
|
||||
{
|
||||
QMap<int, char> data =
|
||||
{
|
||||
{0, 'a'},
|
||||
{1, 'b'},
|
||||
{2, 'c'},
|
||||
{3, 'b'},
|
||||
{4, 'd'}
|
||||
};
|
||||
Algorithm::removeIf(data, [](const int key, const char value)
|
||||
{
|
||||
Q_UNUSED(key);
|
||||
return (value == 'b');
|
||||
});
|
||||
QCOMPARE(data.size(), 3);
|
||||
QCOMPARE(data.value(0), 'a');
|
||||
QVERIFY(!data.contains(1));
|
||||
QCOMPARE(data.value(2), 'c');
|
||||
QVERIFY(!data.contains(3));
|
||||
QCOMPARE(data.value(4), 'd');
|
||||
}
|
||||
{
|
||||
QHash<int, char> data;
|
||||
Algorithm::removeIf(data, [](const int key, const char value)
|
||||
{
|
||||
Q_UNUSED(key);
|
||||
return (value == 'b');
|
||||
});
|
||||
QVERIFY(data.empty());
|
||||
}
|
||||
}
|
||||
|
||||
void testNonMappedTypeRemoveIf() const
|
||||
{
|
||||
{
|
||||
QSet<char> data =
|
||||
{
|
||||
'a',
|
||||
'b',
|
||||
'c',
|
||||
'b',
|
||||
'd'
|
||||
};
|
||||
Algorithm::removeIf(data, [](const char value)
|
||||
{
|
||||
return (value == 'b');
|
||||
});
|
||||
QCOMPARE(data.size(), 3);
|
||||
QVERIFY(data.contains('a'));
|
||||
QVERIFY(data.contains('c'));
|
||||
QVERIFY(data.contains('d'));
|
||||
}
|
||||
{
|
||||
std::set<char> data;
|
||||
Algorithm::removeIf(data, [](const char value)
|
||||
{
|
||||
return (value == 'b');
|
||||
});
|
||||
QVERIFY(data.empty());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
QTEST_APPLESS_MAIN(TestAlgorithm)
|
||||
#include "testalgorithm.moc"
|
131
test/testorderedset.cpp
Normal file
131
test/testorderedset.cpp
Normal file
|
@ -0,0 +1,131 @@
|
|||
/*
|
||||
* 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 <QTest>
|
||||
|
||||
#include "base/global.h"
|
||||
#include "base/orderedset.h"
|
||||
|
||||
class TestOrderedSet final : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_DISABLE_COPY_MOVE(TestOrderedSet)
|
||||
|
||||
public:
|
||||
TestOrderedSet() = default;
|
||||
|
||||
private slots:
|
||||
#if __cplusplus < 202002L
|
||||
void testContains() const
|
||||
{
|
||||
const OrderedSet<QString> set {u"a"_qs, u"b"_qs, u"c"_qs};
|
||||
QVERIFY(set.contains(u"a"_qs));
|
||||
QVERIFY(set.contains(u"b"_qs));
|
||||
QVERIFY(set.contains(u"c"_qs));
|
||||
QVERIFY(!set.contains(u"z"_qs));
|
||||
|
||||
const OrderedSet<QString> emptySet;
|
||||
QVERIFY(!emptySet.contains(u"a"_qs));
|
||||
}
|
||||
#endif
|
||||
|
||||
void testCount() const
|
||||
{
|
||||
const OrderedSet<QString> set {u"a"_qs, u"b"_qs, u"c"_qs, u"c"_qs};
|
||||
QCOMPARE(set.count(), 3);
|
||||
|
||||
const OrderedSet<QString> emptySet;
|
||||
QCOMPARE(emptySet.count(), 0);
|
||||
}
|
||||
|
||||
void testIntersect() const
|
||||
{
|
||||
OrderedSet<QString> set {u"a"_qs, u"b"_qs, u"c"_qs};
|
||||
set.intersect({u"c"_qs, u"a"_qs});
|
||||
QCOMPARE(set.size(), 2);
|
||||
QCOMPARE(set.join(u","_qs), u"a,c"_qs);
|
||||
|
||||
OrderedSet<QString> emptySet;
|
||||
emptySet.intersect({u"a"_qs}).intersect({u"c"_qs});;
|
||||
QVERIFY(emptySet.isEmpty());
|
||||
}
|
||||
|
||||
void testIsEmpty() const
|
||||
{
|
||||
const OrderedSet<QString> set {u"a"_qs, u"b"_qs, u"c"_qs};
|
||||
QVERIFY(!set.isEmpty());
|
||||
|
||||
const OrderedSet<QString> emptySet;
|
||||
QVERIFY(emptySet.isEmpty());
|
||||
}
|
||||
|
||||
void testJoin() const
|
||||
{
|
||||
const OrderedSet<QString> set {u"a"_qs, u"b"_qs, u"c"_qs};
|
||||
QCOMPARE(set.join(u","_qs), u"a,b,c"_qs);
|
||||
|
||||
const OrderedSet<QString> emptySet;
|
||||
QCOMPARE(emptySet.join(u","_qs), u""_qs);
|
||||
}
|
||||
|
||||
void testRemove() const
|
||||
{
|
||||
OrderedSet<QString> set {u"a"_qs, u"b"_qs, u"c"_qs};
|
||||
QVERIFY(!set.remove(u"z"_qs));
|
||||
QCOMPARE(set.join(u","_qs), u"a,b,c"_qs);
|
||||
QVERIFY(set.remove(u"b"_qs));
|
||||
QCOMPARE(set.join(u","_qs), u"a,c"_qs);
|
||||
QVERIFY(set.remove(u"a"_qs));
|
||||
QCOMPARE(set.join(u","_qs), u"c"_qs);
|
||||
QVERIFY(set.remove(u"c"_qs));
|
||||
QVERIFY(set.isEmpty());
|
||||
|
||||
OrderedSet<QString> emptySet;
|
||||
QVERIFY(!emptySet.remove(u"a"_qs));
|
||||
QVERIFY(emptySet.isEmpty());
|
||||
}
|
||||
|
||||
void testUnite() const
|
||||
{
|
||||
const OrderedSet<QString> newData1 {u"z"_qs};
|
||||
const OrderedSet<QString> newData2 {u"y"_qs};
|
||||
|
||||
OrderedSet<QString> set {u"a"_qs, u"b"_qs, u"c"_qs};
|
||||
set.unite(newData1);
|
||||
QCOMPARE(set.join(u","_qs), u"a,b,c,z"_qs);
|
||||
set.unite(newData2);
|
||||
QCOMPARE(set.join(u","_qs), u"a,b,c,y,z"_qs);
|
||||
|
||||
OrderedSet<QString> emptySet;
|
||||
emptySet.unite(newData1).unite(newData2);
|
||||
QCOMPARE(emptySet.join(u","_qs), u"y,z"_qs);
|
||||
}
|
||||
};
|
||||
|
||||
QTEST_APPLESS_MAIN(TestOrderedSet)
|
||||
#include "testorderedset.moc"
|
184
test/testutilscompare.cpp
Normal file
184
test/testutilscompare.cpp
Normal file
|
@ -0,0 +1,184 @@
|
|||
/*
|
||||
* 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 <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""_qs, u""_qs, CompareResult::Equal, CompareResult::Equal},
|
||||
{u""_qs, u"a"_qs, CompareResult::Less, CompareResult::Less},
|
||||
{u"a"_qs, u""_qs, CompareResult::Greater, CompareResult::Greater},
|
||||
|
||||
{u"a"_qs, u"a"_qs, CompareResult::Equal, CompareResult::Equal},
|
||||
{u"A"_qs, u"a"_qs, CompareResult::Equal, CompareResult::Less}, // ascii code of 'A' is smaller than 'a'
|
||||
{u"a"_qs, u"A"_qs, CompareResult::Equal, CompareResult::Greater},
|
||||
|
||||
{u"0"_qs, u"0"_qs, CompareResult::Equal, CompareResult::Equal},
|
||||
{u"1"_qs, u"0"_qs, CompareResult::Greater, CompareResult::Greater},
|
||||
{u"0"_qs, u"1"_qs, CompareResult::Less, CompareResult::Less},
|
||||
|
||||
{u"😀"_qs, u"😀"_qs, CompareResult::Equal, CompareResult::Equal},
|
||||
{u"😀"_qs, u"😁"_qs, CompareResult::Less, CompareResult::Less},
|
||||
{u"😁"_qs, u"😀"_qs, CompareResult::Greater, CompareResult::Greater},
|
||||
|
||||
{u"a1"_qs, u"a1"_qs, CompareResult::Equal, CompareResult::Equal},
|
||||
{u"A1"_qs, u"a1"_qs, CompareResult::Equal, CompareResult::Less},
|
||||
{u"a1"_qs, u"A1"_qs, CompareResult::Equal, CompareResult::Greater},
|
||||
|
||||
{u"a1"_qs, u"a2"_qs, CompareResult::Less, CompareResult::Less},
|
||||
{u"A1"_qs, u"a2"_qs, CompareResult::Less, CompareResult::Less},
|
||||
{u"a1"_qs, u"A2"_qs, CompareResult::Less, CompareResult::Greater},
|
||||
{u"A1"_qs, u"A2"_qs, CompareResult::Less, CompareResult::Less},
|
||||
|
||||
{u"abc100"_qs, u"abc99"_qs, CompareResult::Greater, CompareResult::Greater},
|
||||
{u"ABC100"_qs, u"abc99"_qs, CompareResult::Greater, CompareResult::Less},
|
||||
{u"abc100"_qs, u"ABC99"_qs, CompareResult::Greater, CompareResult::Greater},
|
||||
{u"ABC100"_qs, u"ABC99"_qs, CompareResult::Greater, CompareResult::Greater},
|
||||
|
||||
{u"100abc"_qs, u"99abc"_qs, CompareResult::Greater, CompareResult::Greater},
|
||||
{u"100ABC"_qs, u"99abc"_qs, CompareResult::Greater, CompareResult::Greater},
|
||||
{u"100abc"_qs, u"99ABC"_qs, CompareResult::Greater, CompareResult::Greater},
|
||||
{u"100ABC"_qs, u"99ABC"_qs, CompareResult::Greater, CompareResult::Greater},
|
||||
|
||||
{u"😀😀😀99"_qs, u"😀😀😀100"_qs, CompareResult::Less, CompareResult::Less},
|
||||
{u"😀😀😀100"_qs, u"😀😀😀99"_qs, 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"_qs
|
||||
.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"_qs
|
||||
.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"
|
Loading…
Reference in a new issue