QtBase  v6.3.1
tst_qduplicatetracker.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2020 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 
29 #include <QtTest/QtTest>
30 
31 #include <QtCore/private/qduplicatetracker_p.h>
32 
33 #include <QObject>
34 #include <utility>
35 
37 {
38  Q_OBJECT
39 private slots:
40  void hasSeen();
41  void clear();
42  void appendTo();
43  void appendTo_special();
44 };
45 
46 void tst_QDuplicateTracker::hasSeen()
47 {
48  {
50  QVERIFY(!tracker.hasSeen(0));
51  QVERIFY(tracker.hasSeen(0));
52  QVERIFY(!tracker.hasSeen(1));
53  QVERIFY(tracker.hasSeen(1));
54  // past the prealloc amount
55  QVERIFY(!tracker.hasSeen(2));
56  QVERIFY(tracker.hasSeen(2));
57  }
58 
59  {
61  QString string1("string1");
62  QString string2("string2");
63  QString string2_2("string2");
64  QString string3("string3");
65 
66  // Move when seen
67  QVERIFY(!tracker.hasSeen(string1));
68  QVERIFY(tracker.hasSeen(std::move(string1)));
69 
70  // Move when unseen
71  QVERIFY(!tracker.hasSeen(std::move(string2)));
72  QVERIFY(tracker.hasSeen(string2_2));
73 
74  // Past the prealloc amount
75  QVERIFY(!tracker.hasSeen(string3));
76  QVERIFY(tracker.hasSeen(string3));
77  }
78 }
79 
80 void tst_QDuplicateTracker::clear()
81 {
83  QVERIFY(!tracker.hasSeen(0));
84  QVERIFY(tracker.hasSeen(0));
85  QVERIFY(!tracker.hasSeen(1));
86  QVERIFY(tracker.hasSeen(1));
87 
88  tracker.clear();
89  QVERIFY(!tracker.hasSeen(0));
90  QVERIFY(tracker.hasSeen(0));
91  QVERIFY(!tracker.hasSeen(1));
92  QVERIFY(tracker.hasSeen(1));
93 }
94 
95 void tst_QDuplicateTracker::appendTo()
96 {
98  QVERIFY(!tracker.hasSeen(0));
99  QVERIFY(!tracker.hasSeen(1));
100  QList<int> a;
101  a.append(-1);
102  tracker.appendTo(a);
103  std::sort(a.begin(), a.end());
104  QCOMPARE(a, QList<int>({ -1, 0, 1 }));
105 
106  QList<int> b;
107  tracker.appendTo(b);
108  std::sort(b.begin(), b.end());
109  QCOMPARE(b, QList<int>({ 0, 1 }));
110 
111  QVERIFY(!tracker.hasSeen(2));
112  QList<int> c;
113  std::move(tracker).appendTo(c);
114  std::sort(c.begin(), c.end());
115  QCOMPARE(c, QList<int>({ 0, 1, 2 }));
117  // the following is only true if we use the std container
118  QVERIFY(!tracker.hasSeen(0));
119  QVERIFY(!tracker.hasSeen(1));
120  QVERIFY(!tracker.hasSeen(2));
121  }
122 }
123 
125 {
126  ConstructionCounted(int i) : i(i) { }
128  : i(other.i), copies(other.copies), moves(other.moves + 1)
129  {
130  // set to some easily noticeable values
131  other.i = -64;
132  other.copies = -64;
133  other.moves = -64;
134  }
136  {
137  ConstructionCounted moved = std::move(other);
138  swap(moved);
139  // set to some easily noticeable values
140  other.i = -64;
141  other.copies = -64;
142  other.moves = -64;
143  return *this;
144  }
146  : i(other.i), copies(other.copies + 1), moves(other.moves)
147  {
148  }
150  {
151  ConstructionCounted copy = other;
152  swap(copy);
153  return *this;
154  }
155  ~ConstructionCounted() = default;
156 
157  friend bool operator==(const ConstructionCounted &lhs, const ConstructionCounted &rhs)
158  {
159  return lhs.i == rhs.i;
160  }
161 
163 
165  {
166  std::swap(copies, other.copies);
167  std::swap(i, other.i);
168  std::swap(moves, other.moves);
169  }
170 
171  int i;
172  int copies = 0;
173  int moves = 0;
174 };
175 
176 // for std::unordered_set
177 namespace std {
178 template<>
180 {
181  std::size_t operator()(const ConstructionCounted &c) const noexcept { return c.i; }
182 };
183 }
184 
185 // for QSet
186 size_t qHash(const ConstructionCounted &c, std::size_t seed = 0)
187 {
188  return qHash(c.i, seed);
189 }
190 
191 void tst_QDuplicateTracker::appendTo_special()
192 {
194  QVERIFY(!tracker.hasSeen(1));
195  QVERIFY(!tracker.hasSeen(2));
196  QVERIFY(!tracker.hasSeen(3));
197 
198  QVERIFY(tracker.hasSeen(1));
199  QVERIFY(tracker.hasSeen(2));
200  QVERIFY(tracker.hasSeen(3));
201  {
203  a.reserve(3);
204  tracker.appendTo(a);
205  for (const auto &counter : a) {
206  QCOMPARE(counter.moves, 1);
207  QCOMPARE(counter.copies, 1);
208  }
209  }
210  QVERIFY(tracker.hasSeen(1));
211  QVERIFY(tracker.hasSeen(2));
212  QVERIFY(tracker.hasSeen(3));
213  {
215  a.reserve(3);
216  std::move(tracker).appendTo(a);
218  // the following is only true if we use the std container
219  for (const auto &counter : a) {
220  QCOMPARE(counter.moves, 2);
221  QCOMPARE(counter.copies, 0);
222  }
223  QVERIFY(!tracker.hasSeen(1));
224  QVERIFY(!tracker.hasSeen(2));
225  QVERIFY(!tracker.hasSeen(3));
226  }
227  }
228 }
229 
231 
232 #include "tst_qduplicatetracker.moc"
void appendTo(C &c) const &
bool hasSeen(const T &s)
template< typename Enum > size_t qHash(QFlags< Enum > flags, size_t seed=0) noexcept
The QObject class is the base class of all Qt objects.
Definition: qobject.h:125
The QString class provides a Unicode character string.
Definition: qstring.h:388
static QString number(int, int base=10)
Definition: qstring.cpp:7538
QHash< int, QWidget * > hash
[35multi]
QCOMPARE(spy.count(), 1)
Definition: qfloat16.h:381
void swap(SimpleVector< T > &v1, SimpleVector< T > &v2)
Definition: simplevector.h:331
GLboolean GLboolean GLboolean b
GLboolean GLboolean GLboolean GLboolean a
[7]
GLuint counter
const GLubyte * c
Definition: qopenglext.h:12701
#define QTEST_MAIN(TestObject)
Definition: qtest.h:664
#define QVERIFY(statement)
Definition: qtestcase.h:64
#define Q_OBJECT
Definition: qtmetamacros.h:158
#define slots
Definition: qtmetamacros.h:76
QSharedPointer< T > other(t)
[5]
~ConstructionCounted()=default
ConstructionCounted & operator=(ConstructionCounted &&other) noexcept
ConstructionCounted & operator=(const ConstructionCounted &other) noexcept
ConstructionCounted(ConstructionCounted &&other) noexcept
void swap(ConstructionCounted &other)
friend bool operator==(const ConstructionCounted &lhs, const ConstructionCounted &rhs)
ConstructionCounted(const ConstructionCounted &other) noexcept
std::size_t operator()(const ConstructionCounted &c) const noexcept
#define rhs