summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/tools/qduplicatetracker/tst_qduplicatetracker.cpp
blob: ad0b6abbc701519e704ad55c504768f35ed3116f (plain)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <QtTest/QtTest>

#include <QtCore/private/qduplicatetracker_p.h>

#include <QObject>

#include <string>
#include <utility>

class tst_QDuplicateTracker : public QObject
{
    Q_OBJECT
private slots:
    void hasSeen();
    void clear();
    void appendTo();
    void appendTo_special();
};

void tst_QDuplicateTracker::hasSeen()
{
    {
        QDuplicateTracker<int, 2> 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<QString, 2> 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));
    }

    {
        QDuplicateTracker<std::string, 2> tracker;
        std::string string1("string1");
        std::string string2("string2");
        std::string string2_2("string2");
        std::string 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::clear()
{
    QDuplicateTracker<int, 2> tracker;
    QVERIFY(!tracker.hasSeen(0));
    QVERIFY(tracker.hasSeen(0));
    QVERIFY(!tracker.hasSeen(1));
    QVERIFY(tracker.hasSeen(1));

    tracker.clear();
    QVERIFY(!tracker.hasSeen(0));
    QVERIFY(tracker.hasSeen(0));
    QVERIFY(!tracker.hasSeen(1));
    QVERIFY(tracker.hasSeen(1));
}

void tst_QDuplicateTracker::appendTo()
{
    QDuplicateTracker<int, 2> tracker;
    QVERIFY(!tracker.hasSeen(0));
    QVERIFY(!tracker.hasSeen(1));
    QList<int> a;
    a.append(-1);
    tracker.appendTo(a);
    std::sort(a.begin(), a.end());
    QCOMPARE(a, QList<int>({ -1, 0, 1 }));

    QList<int> b;
    tracker.appendTo(b);
    std::sort(b.begin(), b.end());
    QCOMPARE(b, QList<int>({ 0, 1 }));

    QVERIFY(!tracker.hasSeen(2));
    QList<int> c;
    std::move(tracker).appendTo(c);
    std::sort(c.begin(), c.end());
    QCOMPARE(c, QList<int>({ 0, 1, 2 }));
    if (QDuplicateTracker<int, 2>::uses_pmr) {
        // the following is only true if we use the std container
        QVERIFY(!tracker.hasSeen(0));
        QVERIFY(!tracker.hasSeen(1));
        QVERIFY(!tracker.hasSeen(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<ConstructionCounted>
{
    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<ConstructionCounted> tracker(3);
    QVERIFY(!tracker.hasSeen(1));
    QVERIFY(!tracker.hasSeen(2));
    QVERIFY(!tracker.hasSeen(3));

    QVERIFY(tracker.hasSeen(1));
    QVERIFY(tracker.hasSeen(2));
    QVERIFY(tracker.hasSeen(3));
    {
        QList<ConstructionCounted> a;
        a.reserve(3);
        tracker.appendTo(a);
        for (const auto &counter : a) {
            QCOMPARE(counter.moves, 1);
            QCOMPARE(counter.copies, 1);
        }
    }
    QVERIFY(tracker.hasSeen(1));
    QVERIFY(tracker.hasSeen(2));
    QVERIFY(tracker.hasSeen(3));
    {
        QList<ConstructionCounted> a;
        a.reserve(3);
        std::move(tracker).appendTo(a);
        if (QDuplicateTracker<ConstructionCounted>::uses_pmr) {
            // the following is only true if we use the std container
            for (const auto &counter : a) {
                QCOMPARE(counter.moves, 2);
                QCOMPARE(counter.copies, 0);
            }
            QVERIFY(!tracker.hasSeen(1));
            QVERIFY(!tracker.hasSeen(2));
            QVERIFY(!tracker.hasSeen(3));
        }
    }
}

QTEST_MAIN(tst_QDuplicateTracker)

#include "tst_qduplicatetracker.moc"