From 1911a4d40ab63d5e314d13d083d505cd3c64f683 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Nordheim?= Date: Fri, 13 Nov 2020 11:13:26 +0100 Subject: QDuplicateTracker: add tests It didn't initially have tests. To avoid relying on realizing breakage implicitly through other classes we'll just add tests instead. Task-number: QTBUG-88183 Pick-to: 6.0 Change-Id: I7449dc1f9a118d4b7a8158a2c34563dbd9c43c66 Reviewed-by: Giuseppe D'Angelo --- tests/auto/corelib/tools/.prev_CMakeLists.txt | 1 + tests/auto/corelib/tools/CMakeLists.txt | 1 + .../corelib/tools/qduplicatetracker/CMakeLists.txt | 12 ++ .../tools/qduplicatetracker/qduplicatetracker.pro | 4 + .../qduplicatetracker/tst_qduplicatetracker.cpp | 192 +++++++++++++++++++++ tests/auto/corelib/tools/tools.pro | 1 + 6 files changed, 211 insertions(+) create mode 100644 tests/auto/corelib/tools/qduplicatetracker/CMakeLists.txt create mode 100644 tests/auto/corelib/tools/qduplicatetracker/qduplicatetracker.pro create mode 100644 tests/auto/corelib/tools/qduplicatetracker/tst_qduplicatetracker.cpp (limited to 'tests/auto/corelib/tools') diff --git a/tests/auto/corelib/tools/.prev_CMakeLists.txt b/tests/auto/corelib/tools/.prev_CMakeLists.txt index 6bd31465bd..f9d158bbe8 100644 --- a/tests/auto/corelib/tools/.prev_CMakeLists.txt +++ b/tests/auto/corelib/tools/.prev_CMakeLists.txt @@ -9,6 +9,7 @@ add_subdirectory(qcache) add_subdirectory(qcommandlineparser) add_subdirectory(qcontiguouscache) add_subdirectory(qcryptographichash) +add_subdirectory(qduplicatetracker) add_subdirectory(qeasingcurve) add_subdirectory(qexplicitlyshareddatapointer) add_subdirectory(qflatmap) diff --git a/tests/auto/corelib/tools/CMakeLists.txt b/tests/auto/corelib/tools/CMakeLists.txt index bf6a97bc07..8fb36a956c 100644 --- a/tests/auto/corelib/tools/CMakeLists.txt +++ b/tests/auto/corelib/tools/CMakeLists.txt @@ -9,6 +9,7 @@ add_subdirectory(qcache) add_subdirectory(qcommandlineparser) add_subdirectory(qcontiguouscache) add_subdirectory(qcryptographichash) +add_subdirectory(qduplicatetracker) add_subdirectory(qeasingcurve) add_subdirectory(qexplicitlyshareddatapointer) add_subdirectory(qflatmap) diff --git a/tests/auto/corelib/tools/qduplicatetracker/CMakeLists.txt b/tests/auto/corelib/tools/qduplicatetracker/CMakeLists.txt new file mode 100644 index 0000000000..a38255f3e9 --- /dev/null +++ b/tests/auto/corelib/tools/qduplicatetracker/CMakeLists.txt @@ -0,0 +1,12 @@ +# Generated from qduplicatetracker.pro. + +##################################################################### +## tst_qduplicatetracker Test: +##################################################################### + +qt_internal_add_test(tst_qduplicatetracker + SOURCES + tst_qduplicatetracker.cpp + PUBLIC_LIBRARIES + Qt::CorePrivate +) diff --git a/tests/auto/corelib/tools/qduplicatetracker/qduplicatetracker.pro b/tests/auto/corelib/tools/qduplicatetracker/qduplicatetracker.pro new file mode 100644 index 0000000000..c3a70a07a0 --- /dev/null +++ b/tests/auto/corelib/tools/qduplicatetracker/qduplicatetracker.pro @@ -0,0 +1,4 @@ +CONFIG += testcase +TARGET = tst_qduplicatetracker +SOURCES = tst_qduplicatetracker.cpp +QT = core-private testlib diff --git a/tests/auto/corelib/tools/qduplicatetracker/tst_qduplicatetracker.cpp b/tests/auto/corelib/tools/qduplicatetracker/tst_qduplicatetracker.cpp new file mode 100644 index 0000000000..056934ae70 --- /dev/null +++ b/tests/auto/corelib/tools/qduplicatetracker/tst_qduplicatetracker.cpp @@ -0,0 +1,192 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include + +#include +#include + +class tst_QDuplicateTracker : public QObject +{ + Q_OBJECT +private slots: + void hasSeen(); + void appendTo(); + void appendTo_special(); +}; + +void tst_QDuplicateTracker::hasSeen() +{ + { + QDuplicateTracker tracker; + QVERIFY(!tracker.hasSeen(0)); + QVERIFY(tracker.hasSeen(0)); + QVERIFY(!tracker.hasSeen(1)); + QVERIFY(tracker.hasSeen(1)); + // past the prealloc amount + QVERIFY(!tracker.hasSeen(2)); + QVERIFY(tracker.hasSeen(2)); + } + + { + QDuplicateTracker tracker; + QString string1("string1"); + QString string2("string2"); + QString string2_2("string2"); + QString string3("string3"); + + // Move when seen + QVERIFY(!tracker.hasSeen(string1)); + QVERIFY(tracker.hasSeen(std::move(string1))); + + // Move when unseen + QVERIFY(!tracker.hasSeen(std::move(string2))); + QVERIFY(tracker.hasSeen(string2_2)); + + // Past the prealloc amount + QVERIFY(!tracker.hasSeen(string3)); + QVERIFY(tracker.hasSeen(string3)); + } +} + +void tst_QDuplicateTracker::appendTo() +{ + QDuplicateTracker tracker; + QVERIFY(!tracker.hasSeen(0)); + QVERIFY(!tracker.hasSeen(1)); + QList a; + a.append(-1); + tracker.appendTo(a); + std::sort(a.begin(), a.end()); + QCOMPARE(a, QList({ -1, 0, 1 })); + + QList b; + tracker.appendTo(b); + std::sort(b.begin(), b.end()); + QCOMPARE(b, QList({ 0, 1 })); + + QVERIFY(!tracker.hasSeen(2)); + QList c; + tracker.appendTo(c); + std::sort(c.begin(), c.end()); + QCOMPARE(c, QList({ 0, 1, 2 })); +} + +struct ConstructionCounted +{ + ConstructionCounted(int i) : i(i) { } + ConstructionCounted(ConstructionCounted &&other) noexcept + : i(other.i), copies(other.copies), moves(other.moves + 1) + { + // set to some easily noticeable values + other.i = -64; + other.copies = -64; + other.moves = -64; + } + ConstructionCounted &operator=(ConstructionCounted &&other) noexcept + { + ConstructionCounted moved = std::move(other); + swap(moved); + // set to some easily noticeable values + other.i = -64; + other.copies = -64; + other.moves = -64; + return *this; + } + ConstructionCounted(const ConstructionCounted &other) noexcept + : i(other.i), copies(other.copies + 1), moves(other.moves) + { + } + ConstructionCounted &operator=(const ConstructionCounted &other) noexcept + { + ConstructionCounted copy = other; + swap(copy); + return *this; + } + ~ConstructionCounted() = default; + + friend bool operator==(const ConstructionCounted &lhs, const ConstructionCounted &rhs) + { + return lhs.i == rhs.i; + } + + QString toString() { return QString::number(i); } + + void swap(ConstructionCounted &other) + { + std::swap(copies, other.copies); + std::swap(i, other.i); + std::swap(moves, other.moves); + } + + int i; + int copies = 0; + int moves = 0; +}; + +// for std::unordered_set +namespace std { +template<> +struct hash +{ + std::size_t operator()(const ConstructionCounted &c) const noexcept { return c.i; } +}; +} + +// for QSet +size_t qHash(const ConstructionCounted &c, std::size_t seed = 0) +{ + return qHash(c.i, seed); +} + +void tst_QDuplicateTracker::appendTo_special() +{ + QDuplicateTracker tracker; + tracker.reserve(3); + QVERIFY(!tracker.hasSeen(1)); + QVERIFY(!tracker.hasSeen(2)); + QVERIFY(!tracker.hasSeen(3)); + QList a; + a.reserve(3); + tracker.appendTo(a); + for (const auto &counter : a) { +#if QT_HAS_INCLUDE() && __cplusplus > 201402L // uses pmr::unordered_set + QCOMPARE(counter.moves, 1); + QCOMPARE(counter.copies, 1); +#else // Uses QSet + QCOMPARE(counter.moves, 1); + QCOMPARE(counter.copies, 2); +#endif + } +} + +QTEST_MAIN(tst_QDuplicateTracker) + +#include "tst_qduplicatetracker.moc" diff --git a/tests/auto/corelib/tools/tools.pro b/tests/auto/corelib/tools/tools.pro index c0519a5c5e..5718db760b 100644 --- a/tests/auto/corelib/tools/tools.pro +++ b/tests/auto/corelib/tools/tools.pro @@ -9,6 +9,7 @@ SUBDIRS=\ qcommandlineparser \ qcontiguouscache \ qcryptographichash \ + qduplicatetracker \ qeasingcurve \ qexplicitlyshareddatapointer \ qflatmap \ -- cgit v1.2.3