0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the test suite of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
|
|
43 |
#include <QtTest/QtTest>
|
|
44 |
#include <QtCore/QBuffer>
|
|
45 |
#include <QtCore/QDataStream>
|
|
46 |
|
|
47 |
#include "qbitarray.h"
|
|
48 |
|
|
49 |
/**
|
|
50 |
* Helper function to initialize a bitarray from a string
|
|
51 |
*/
|
|
52 |
static QBitArray QStringToQBitArray(const QString &str)
|
|
53 |
{
|
|
54 |
QBitArray ba;
|
|
55 |
ba.resize(str.length());
|
|
56 |
int i;
|
|
57 |
QChar tru('1');
|
|
58 |
for (i = 0; i < str.length(); i++)
|
|
59 |
{
|
|
60 |
if (str.at(i) == tru)
|
|
61 |
{
|
|
62 |
ba.setBit(i, true);
|
|
63 |
}
|
|
64 |
}
|
|
65 |
return ba;
|
|
66 |
}
|
|
67 |
|
|
68 |
|
|
69 |
//TESTED_CLASS=
|
|
70 |
//TESTED_FILES=
|
|
71 |
|
|
72 |
class tst_QBitArray : public QObject
|
|
73 |
{
|
|
74 |
Q_OBJECT
|
|
75 |
|
|
76 |
public:
|
|
77 |
tst_QBitArray();
|
|
78 |
virtual ~tst_QBitArray();
|
|
79 |
|
|
80 |
|
|
81 |
public slots:
|
|
82 |
void init();
|
|
83 |
void cleanup();
|
|
84 |
private slots:
|
|
85 |
void size_data();
|
|
86 |
void size();
|
|
87 |
void countBits_data();
|
|
88 |
void countBits();
|
|
89 |
void countBits2();
|
|
90 |
void isEmpty();
|
|
91 |
void fill();
|
|
92 |
void toggleBit_data();
|
|
93 |
void toggleBit();
|
|
94 |
// operator &=
|
|
95 |
void operator_andeq_data();
|
|
96 |
void operator_andeq();
|
|
97 |
// operator |=
|
|
98 |
void operator_oreq_data();
|
|
99 |
void operator_oreq();
|
|
100 |
// operator ^=
|
|
101 |
void operator_xoreq_data();
|
|
102 |
void operator_xoreq();
|
|
103 |
// operator ~
|
|
104 |
void operator_neg_data();
|
|
105 |
void operator_neg();
|
|
106 |
void datastream_data();
|
|
107 |
void datastream();
|
|
108 |
void invertOnNull() const;
|
|
109 |
void operator_noteq_data();
|
|
110 |
void operator_noteq();
|
|
111 |
|
|
112 |
void resize();
|
|
113 |
};
|
|
114 |
|
|
115 |
Q_DECLARE_METATYPE(QBitArray)
|
|
116 |
|
|
117 |
tst_QBitArray::tst_QBitArray()
|
|
118 |
{
|
|
119 |
}
|
|
120 |
|
|
121 |
tst_QBitArray::~tst_QBitArray()
|
|
122 |
{
|
|
123 |
|
|
124 |
}
|
|
125 |
|
|
126 |
void tst_QBitArray::init()
|
|
127 |
{
|
|
128 |
// TODO: Add initialization code here.
|
|
129 |
// This will be executed immediately before each test is run.
|
|
130 |
}
|
|
131 |
|
|
132 |
void tst_QBitArray::cleanup()
|
|
133 |
{
|
|
134 |
// TODO: Add cleanup code here.
|
|
135 |
// This will be executed immediately after each test is run.
|
|
136 |
}
|
|
137 |
|
|
138 |
void tst_QBitArray::size_data()
|
|
139 |
{
|
|
140 |
//create the testtable instance and define the elements
|
|
141 |
QTest::addColumn<int>("count");
|
|
142 |
QTest::addColumn<QString>("res");
|
|
143 |
|
|
144 |
//next we fill it with data
|
|
145 |
QTest::newRow( "data0" ) << 1 << QString("1");
|
|
146 |
QTest::newRow( "data1" ) << 2 << QString("11");
|
|
147 |
QTest::newRow( "data2" ) << 3 << QString("111");
|
|
148 |
QTest::newRow( "data3" ) << 9 << QString("111111111");
|
|
149 |
QTest::newRow( "data4" ) << 10 << QString("1111111111");
|
|
150 |
QTest::newRow( "data5" ) << 17 << QString("11111111111111111");
|
|
151 |
QTest::newRow( "data6" ) << 18 << QString("111111111111111111");
|
|
152 |
QTest::newRow( "data7" ) << 19 << QString("1111111111111111111");
|
|
153 |
QTest::newRow( "data8" ) << 20 << QString("11111111111111111111");
|
|
154 |
QTest::newRow( "data9" ) << 21 << QString("111111111111111111111");
|
|
155 |
QTest::newRow( "data10" ) << 22 << QString("1111111111111111111111");
|
|
156 |
QTest::newRow( "data11" ) << 23 << QString("11111111111111111111111");
|
|
157 |
QTest::newRow( "data12" ) << 24 << QString("111111111111111111111111");
|
|
158 |
QTest::newRow( "data13" ) << 25 << QString("1111111111111111111111111");
|
|
159 |
QTest::newRow( "data14" ) << 32 << QString("11111111111111111111111111111111");
|
|
160 |
}
|
|
161 |
|
|
162 |
void tst_QBitArray::size()
|
|
163 |
{
|
|
164 |
QFETCH(int,count);
|
|
165 |
|
|
166 |
QString S;
|
|
167 |
QBitArray a(count);
|
|
168 |
a.fill(1);
|
|
169 |
int len = a.size();
|
|
170 |
for (int j=0; j<len; j++) {
|
|
171 |
bool b = a[j];
|
|
172 |
if (b)
|
|
173 |
S+= "1";
|
|
174 |
else
|
|
175 |
S+= "0";
|
|
176 |
}
|
|
177 |
QTEST(S,"res");
|
|
178 |
}
|
|
179 |
|
|
180 |
void tst_QBitArray::countBits_data()
|
|
181 |
{
|
|
182 |
QTest::addColumn<QString>("bitField");
|
|
183 |
QTest::addColumn<int>("numBits");
|
|
184 |
QTest::addColumn<int>("onBits");
|
|
185 |
|
|
186 |
QTest::newRow("empty") << QString() << 0 << 0;
|
|
187 |
QTest::newRow("1") << QString("1") << 1 << 1;
|
|
188 |
QTest::newRow("101") << QString("101") << 3 << 2;
|
|
189 |
QTest::newRow("101100001") << QString("101100001") << 9 << 4;
|
|
190 |
QTest::newRow("101100001101100001") << QString("101100001101100001") << 18 << 8;
|
|
191 |
QTest::newRow("101100001101100001101100001101100001") << QString("101100001101100001101100001101100001") << 36 << 16;
|
|
192 |
QTest::newRow("00000000000000000000000000000000000") << QString("00000000000000000000000000000000000") << 35 << 0;
|
|
193 |
QTest::newRow("11111111111111111111111111111111111") << QString("11111111111111111111111111111111111") << 35 << 35;
|
|
194 |
QTest::newRow("11111111111111111111111111111111") << QString("11111111111111111111111111111111") << 32 << 32;
|
|
195 |
QTest::newRow("11111111111111111111111111111111111111111111111111111111")
|
|
196 |
<< QString("11111111111111111111111111111111111111111111111111111111") << 56 << 56;
|
|
197 |
QTest::newRow("00000000000000000000000000000000000") << QString("00000000000000000000000000000000000") << 35 << 0;
|
|
198 |
QTest::newRow("00000000000000000000000000000000") << QString("00000000000000000000000000000000") << 32 << 0;
|
|
199 |
QTest::newRow("00000000000000000000000000000000000000000000000000000000")
|
|
200 |
<< QString("00000000000000000000000000000000000000000000000000000000") << 56 << 0;
|
|
201 |
}
|
|
202 |
|
|
203 |
void tst_QBitArray::countBits()
|
|
204 |
{
|
|
205 |
QFETCH(QString, bitField);
|
|
206 |
QFETCH(int, numBits);
|
|
207 |
QFETCH(int, onBits);
|
|
208 |
|
|
209 |
QBitArray bits(bitField.size());
|
|
210 |
for (int i = 0; i < bitField.size(); ++i) {
|
|
211 |
if (bitField.at(i) == QLatin1Char('1'))
|
|
212 |
bits.setBit(i);
|
|
213 |
}
|
|
214 |
|
|
215 |
QCOMPARE(bits.count(), numBits);
|
|
216 |
QCOMPARE(bits.count(true), onBits);
|
|
217 |
QCOMPARE(bits.count(false), numBits - onBits);
|
|
218 |
}
|
|
219 |
|
|
220 |
void tst_QBitArray::countBits2()
|
|
221 |
{
|
|
222 |
QBitArray bitArray;
|
|
223 |
for (int i = 0; i < 4017; ++i) {
|
|
224 |
bitArray.resize(i);
|
|
225 |
bitArray.fill(true);
|
|
226 |
QCOMPARE(bitArray.count(true), i);
|
|
227 |
QCOMPARE(bitArray.count(false), 0);
|
|
228 |
bitArray.fill(false);
|
|
229 |
QCOMPARE(bitArray.count(true), 0);
|
|
230 |
QCOMPARE(bitArray.count(false), i);
|
|
231 |
}
|
|
232 |
}
|
|
233 |
|
|
234 |
void tst_QBitArray::isEmpty()
|
|
235 |
{
|
|
236 |
QBitArray a1;
|
|
237 |
QVERIFY(a1.isEmpty());
|
|
238 |
QVERIFY(a1.isNull());
|
|
239 |
QVERIFY(a1.size() == 0);
|
|
240 |
|
|
241 |
QBitArray a2(0, true);
|
|
242 |
QVERIFY(a2.isEmpty());
|
|
243 |
QVERIFY(!a2.isNull());
|
|
244 |
QVERIFY(a2.size() == 0);
|
|
245 |
|
|
246 |
QBitArray a3(1, true);
|
|
247 |
QVERIFY(!a3.isEmpty());
|
|
248 |
QVERIFY(!a3.isNull());
|
|
249 |
QVERIFY(a3.size() == 1);
|
|
250 |
|
|
251 |
a1.resize(0);
|
|
252 |
QVERIFY(a1.isEmpty());
|
|
253 |
QVERIFY(!a1.isNull());
|
|
254 |
QVERIFY(a1.size() == 0);
|
|
255 |
|
|
256 |
a2.resize(0);
|
|
257 |
QVERIFY(a2.isEmpty());
|
|
258 |
QVERIFY(!a2.isNull());
|
|
259 |
QVERIFY(a2.size() == 0);
|
|
260 |
|
|
261 |
a1.resize(1);
|
|
262 |
QVERIFY(!a1.isEmpty());
|
|
263 |
QVERIFY(!a1.isNull());
|
|
264 |
QVERIFY(a1.size() == 1);
|
|
265 |
|
|
266 |
a1.resize(2);
|
|
267 |
QVERIFY(!a1.isEmpty());
|
|
268 |
QVERIFY(!a1.isNull());
|
|
269 |
QVERIFY(a1.size() == 2);
|
|
270 |
}
|
|
271 |
|
|
272 |
void tst_QBitArray::fill()
|
|
273 |
{
|
|
274 |
int N = 64;
|
|
275 |
int M = 17;
|
|
276 |
QBitArray a(N, false);
|
|
277 |
int i, j;
|
|
278 |
|
|
279 |
for (i = 0; i < N-M; ++i) {
|
|
280 |
a.fill(true, i, i + M);
|
|
281 |
/*
|
|
282 |
for (int k = 0; k < N; ++k)
|
|
283 |
qDebug("%*s%d", k, "", a.at(k));
|
|
284 |
*/
|
|
285 |
for (j = 0; j < N; ++j) {
|
|
286 |
if (j >= i && j < i + M) {
|
|
287 |
QVERIFY(a.at(j));
|
|
288 |
} else {
|
|
289 |
QVERIFY(!a.at(j));
|
|
290 |
}
|
|
291 |
}
|
|
292 |
a.fill(false, i, i + M);
|
|
293 |
}
|
|
294 |
for (i = 0; i < N; ++i)
|
|
295 |
a.fill(i % 2 == 0, i, i + 1);
|
|
296 |
for (i = 0; i < N; ++i) {
|
|
297 |
QVERIFY(a.at(i) == (i % 2 == 0));
|
|
298 |
}
|
|
299 |
}
|
|
300 |
|
|
301 |
void tst_QBitArray::toggleBit_data()
|
|
302 |
{
|
|
303 |
QTest::addColumn<int>("index");
|
|
304 |
QTest::addColumn<QBitArray>("input");
|
|
305 |
QTest::addColumn<QBitArray>("res");
|
|
306 |
// 8 bits, toggle first bit
|
|
307 |
QTest::newRow( "data0" ) << 0 << QStringToQBitArray(QString("11111111")) << QStringToQBitArray(QString("01111111"));
|
|
308 |
// 8 bits
|
|
309 |
QTest::newRow( "data1" ) << 1 << QStringToQBitArray(QString("11111111")) << QStringToQBitArray(QString("10111111"));
|
|
310 |
// 11 bits, toggle last bit
|
|
311 |
QTest::newRow( "data2" ) << 10 << QStringToQBitArray(QString("11111111111")) << QStringToQBitArray(QString("11111111110"));
|
|
312 |
|
|
313 |
}
|
|
314 |
|
|
315 |
void tst_QBitArray::toggleBit()
|
|
316 |
{
|
|
317 |
QFETCH(int,index);
|
|
318 |
QFETCH(QBitArray, input);
|
|
319 |
QFETCH(QBitArray, res);
|
|
320 |
|
|
321 |
input.toggleBit(index);
|
|
322 |
|
|
323 |
QCOMPARE(input, res);
|
|
324 |
}
|
|
325 |
|
|
326 |
void tst_QBitArray::operator_andeq_data()
|
|
327 |
{
|
|
328 |
QTest::addColumn<QBitArray>("input1");
|
|
329 |
QTest::addColumn<QBitArray>("input2");
|
|
330 |
QTest::addColumn<QBitArray>("res");
|
|
331 |
|
|
332 |
QTest::newRow( "data0" ) << QStringToQBitArray(QString("11111111"))
|
|
333 |
<< QStringToQBitArray(QString("00101100"))
|
|
334 |
<< QStringToQBitArray(QString("00101100"));
|
|
335 |
|
|
336 |
|
|
337 |
QTest::newRow( "data1" ) << QStringToQBitArray(QString("11011011"))
|
|
338 |
<< QStringToQBitArray(QString("00101100"))
|
|
339 |
<< QStringToQBitArray(QString("00001000"));
|
|
340 |
|
|
341 |
QTest::newRow( "data2" ) << QStringToQBitArray(QString("11011011111"))
|
|
342 |
<< QStringToQBitArray(QString("00101100"))
|
|
343 |
<< QStringToQBitArray(QString("00001000000"));
|
|
344 |
|
|
345 |
QTest::newRow( "data3" ) << QStringToQBitArray(QString("11011011"))
|
|
346 |
<< QStringToQBitArray(QString("00101100111"))
|
|
347 |
<< QStringToQBitArray(QString("00001000000"));
|
|
348 |
|
|
349 |
QTest::newRow( "data4" ) << QStringToQBitArray(QString())
|
|
350 |
<< QStringToQBitArray(QString("00101100111"))
|
|
351 |
<< QStringToQBitArray(QString("00000000000"));
|
|
352 |
|
|
353 |
QTest::newRow( "data5" ) << QStringToQBitArray(QString("00101100111"))
|
|
354 |
<< QStringToQBitArray(QString())
|
|
355 |
<< QStringToQBitArray(QString("00000000000"));
|
|
356 |
|
|
357 |
QTest::newRow( "data6" ) << QStringToQBitArray(QString())
|
|
358 |
<< QStringToQBitArray(QString())
|
|
359 |
<< QStringToQBitArray(QString());
|
|
360 |
}
|
|
361 |
|
|
362 |
void tst_QBitArray::operator_andeq()
|
|
363 |
{
|
|
364 |
QFETCH(QBitArray, input1);
|
|
365 |
QFETCH(QBitArray, input2);
|
|
366 |
QFETCH(QBitArray, res);
|
|
367 |
|
|
368 |
input1&=input2;
|
|
369 |
|
|
370 |
QCOMPARE(input1, res);
|
|
371 |
}
|
|
372 |
|
|
373 |
void tst_QBitArray::operator_oreq_data()
|
|
374 |
{
|
|
375 |
QTest::addColumn<QBitArray>("input1");
|
|
376 |
QTest::addColumn<QBitArray>("input2");
|
|
377 |
QTest::addColumn<QBitArray>("res");
|
|
378 |
|
|
379 |
QTest::newRow( "data0" ) << QStringToQBitArray(QString("11111111"))
|
|
380 |
<< QStringToQBitArray(QString("00101100"))
|
|
381 |
<< QStringToQBitArray(QString("11111111"));
|
|
382 |
|
|
383 |
|
|
384 |
QTest::newRow( "data1" ) << QStringToQBitArray(QString("11011011"))
|
|
385 |
<< QStringToQBitArray(QString("00101100"))
|
|
386 |
<< QStringToQBitArray(QString("11111111"));
|
|
387 |
|
|
388 |
QTest::newRow( "data2" ) << QStringToQBitArray(QString("01000010"))
|
|
389 |
<< QStringToQBitArray(QString("10100001"))
|
|
390 |
<< QStringToQBitArray(QString("11100011"));
|
|
391 |
|
|
392 |
QTest::newRow( "data3" ) << QStringToQBitArray(QString("11011011"))
|
|
393 |
<< QStringToQBitArray(QString("00101100000"))
|
|
394 |
<< QStringToQBitArray(QString("11111111000"));
|
|
395 |
|
|
396 |
QTest::newRow( "data4" ) << QStringToQBitArray(QString("11011011111"))
|
|
397 |
<< QStringToQBitArray(QString("00101100"))
|
|
398 |
<< QStringToQBitArray(QString("11111111111"));
|
|
399 |
|
|
400 |
QTest::newRow( "data5" ) << QStringToQBitArray(QString())
|
|
401 |
<< QStringToQBitArray(QString("00101100111"))
|
|
402 |
<< QStringToQBitArray(QString("00101100111"));
|
|
403 |
|
|
404 |
QTest::newRow( "data6" ) << QStringToQBitArray(QString("00101100111"))
|
|
405 |
<< QStringToQBitArray(QString())
|
|
406 |
<< QStringToQBitArray(QString("00101100111"));
|
|
407 |
|
|
408 |
QTest::newRow( "data7" ) << QStringToQBitArray(QString())
|
|
409 |
<< QStringToQBitArray(QString())
|
|
410 |
<< QStringToQBitArray(QString());
|
|
411 |
}
|
|
412 |
|
|
413 |
void tst_QBitArray::operator_oreq()
|
|
414 |
{
|
|
415 |
QFETCH(QBitArray, input1);
|
|
416 |
QFETCH(QBitArray, input2);
|
|
417 |
QFETCH(QBitArray, res);
|
|
418 |
|
|
419 |
input1|=input2;
|
|
420 |
|
|
421 |
QCOMPARE(input1, res);
|
|
422 |
}
|
|
423 |
|
|
424 |
void tst_QBitArray::operator_xoreq_data()
|
|
425 |
{
|
|
426 |
QTest::addColumn<QBitArray>("input1");
|
|
427 |
QTest::addColumn<QBitArray>("input2");
|
|
428 |
QTest::addColumn<QBitArray>("res");
|
|
429 |
QTest::newRow( "data0" ) << QStringToQBitArray(QString("11111111"))
|
|
430 |
<< QStringToQBitArray(QString("00101100"))
|
|
431 |
<< QStringToQBitArray(QString("11010011"));
|
|
432 |
|
|
433 |
QTest::newRow( "data1" ) << QStringToQBitArray(QString("11011011"))
|
|
434 |
<< QStringToQBitArray(QString("00101100"))
|
|
435 |
<< QStringToQBitArray(QString("11110111"));
|
|
436 |
|
|
437 |
QTest::newRow( "data2" ) << QStringToQBitArray(QString("01000010"))
|
|
438 |
<< QStringToQBitArray(QString("10100001"))
|
|
439 |
<< QStringToQBitArray(QString("11100011"));
|
|
440 |
|
|
441 |
QTest::newRow( "data3" ) << QStringToQBitArray(QString("01000010"))
|
|
442 |
<< QStringToQBitArray(QString("10100001101"))
|
|
443 |
<< QStringToQBitArray(QString("11100011101"));
|
|
444 |
|
|
445 |
QTest::newRow( "data4" ) << QStringToQBitArray(QString("01000010111"))
|
|
446 |
<< QStringToQBitArray(QString("101000011"))
|
|
447 |
<< QStringToQBitArray(QString("11100011011"));
|
|
448 |
|
|
449 |
QTest::newRow( "data5" ) << QStringToQBitArray(QString())
|
|
450 |
<< QStringToQBitArray(QString("00101100111"))
|
|
451 |
<< QStringToQBitArray(QString("00101100111"));
|
|
452 |
|
|
453 |
QTest::newRow( "data6" ) << QStringToQBitArray(QString("00101100111"))
|
|
454 |
<< QStringToQBitArray(QString())
|
|
455 |
<< QStringToQBitArray(QString("00101100111"));
|
|
456 |
|
|
457 |
QTest::newRow( "data7" ) << QStringToQBitArray(QString())
|
|
458 |
<< QStringToQBitArray(QString())
|
|
459 |
<< QStringToQBitArray(QString());
|
|
460 |
}
|
|
461 |
|
|
462 |
void tst_QBitArray::operator_xoreq()
|
|
463 |
{
|
|
464 |
QFETCH(QBitArray, input1);
|
|
465 |
QFETCH(QBitArray, input2);
|
|
466 |
QFETCH(QBitArray, res);
|
|
467 |
|
|
468 |
input1^=input2;
|
|
469 |
|
|
470 |
QCOMPARE(input1, res);
|
|
471 |
}
|
|
472 |
|
|
473 |
|
|
474 |
void tst_QBitArray::operator_neg_data()
|
|
475 |
{
|
|
476 |
QTest::addColumn<QBitArray>("input");
|
|
477 |
QTest::addColumn<QBitArray>("res");
|
|
478 |
|
|
479 |
QTest::newRow( "data0" ) << QStringToQBitArray(QString("11111111"))
|
|
480 |
<< QStringToQBitArray(QString("00000000"));
|
|
481 |
|
|
482 |
QTest::newRow( "data1" ) << QStringToQBitArray(QString("11011011"))
|
|
483 |
<< QStringToQBitArray(QString("00100100"));
|
|
484 |
|
|
485 |
QTest::newRow( "data2" ) << QStringToQBitArray(QString("00000000"))
|
|
486 |
<< QStringToQBitArray(QString("11111111"));
|
|
487 |
|
|
488 |
QTest::newRow( "data3" ) << QStringToQBitArray(QString())
|
|
489 |
<< QStringToQBitArray(QString());
|
|
490 |
|
|
491 |
QTest::newRow( "data4" ) << QStringToQBitArray("1")
|
|
492 |
<< QStringToQBitArray("0");
|
|
493 |
|
|
494 |
QTest::newRow( "data5" ) << QStringToQBitArray("0")
|
|
495 |
<< QStringToQBitArray("1");
|
|
496 |
|
|
497 |
QTest::newRow( "data6" ) << QStringToQBitArray("01")
|
|
498 |
<< QStringToQBitArray("10");
|
|
499 |
|
|
500 |
QTest::newRow( "data7" ) << QStringToQBitArray("1110101")
|
|
501 |
<< QStringToQBitArray("0001010");
|
|
502 |
|
|
503 |
QTest::newRow( "data8" ) << QStringToQBitArray("01110101")
|
|
504 |
<< QStringToQBitArray("10001010");
|
|
505 |
|
|
506 |
QTest::newRow( "data9" ) << QStringToQBitArray("011101010")
|
|
507 |
<< QStringToQBitArray("100010101");
|
|
508 |
|
|
509 |
QTest::newRow( "data10" ) << QStringToQBitArray("0111010101111010")
|
|
510 |
<< QStringToQBitArray("1000101010000101");
|
|
511 |
}
|
|
512 |
|
|
513 |
void tst_QBitArray::operator_neg()
|
|
514 |
{
|
|
515 |
QFETCH(QBitArray, input);
|
|
516 |
QFETCH(QBitArray, res);
|
|
517 |
|
|
518 |
input = ~input;
|
|
519 |
|
|
520 |
QCOMPARE(input, res);
|
|
521 |
}
|
|
522 |
|
|
523 |
void tst_QBitArray::datastream_data()
|
|
524 |
{
|
|
525 |
QTest::addColumn<QString>("bitField");
|
|
526 |
QTest::addColumn<int>("numBits");
|
|
527 |
QTest::addColumn<int>("onBits");
|
|
528 |
|
|
529 |
QTest::newRow("empty") << QString() << 0 << 0;
|
|
530 |
QTest::newRow("1") << QString("1") << 1 << 1;
|
|
531 |
QTest::newRow("101") << QString("101") << 3 << 2;
|
|
532 |
QTest::newRow("101100001") << QString("101100001") << 9 << 4;
|
|
533 |
QTest::newRow("101100001101100001") << QString("101100001101100001") << 18 << 8;
|
|
534 |
QTest::newRow("101100001101100001101100001101100001") << QString("101100001101100001101100001101100001") << 36 << 16;
|
|
535 |
QTest::newRow("00000000000000000000000000000000000") << QString("00000000000000000000000000000000000") << 35 << 0;
|
|
536 |
QTest::newRow("11111111111111111111111111111111111") << QString("11111111111111111111111111111111111") << 35 << 35;
|
|
537 |
QTest::newRow("11111111111111111111111111111111") << QString("11111111111111111111111111111111") << 32 << 32;
|
|
538 |
QTest::newRow("11111111111111111111111111111111111111111111111111111111")
|
|
539 |
<< QString("11111111111111111111111111111111111111111111111111111111") << 56 << 56;
|
|
540 |
QTest::newRow("00000000000000000000000000000000000") << QString("00000000000000000000000000000000000") << 35 << 0;
|
|
541 |
QTest::newRow("00000000000000000000000000000000") << QString("00000000000000000000000000000000") << 32 << 0;
|
|
542 |
QTest::newRow("00000000000000000000000000000000000000000000000000000000")
|
|
543 |
<< QString("00000000000000000000000000000000000000000000000000000000") << 56 << 0;
|
|
544 |
}
|
|
545 |
|
|
546 |
void tst_QBitArray::datastream()
|
|
547 |
{
|
|
548 |
QFETCH(QString, bitField);
|
|
549 |
QFETCH(int, numBits);
|
|
550 |
QFETCH(int, onBits);
|
|
551 |
|
|
552 |
QBuffer buffer;
|
|
553 |
QVERIFY(buffer.open(QBuffer::ReadWrite));
|
|
554 |
QDataStream stream(&buffer);
|
|
555 |
|
|
556 |
QBitArray bits(bitField.size());
|
|
557 |
for (int i = 0; i < bitField.size(); ++i) {
|
|
558 |
if (bitField.at(i) == QLatin1Char('1'))
|
|
559 |
bits.setBit(i);
|
|
560 |
}
|
|
561 |
|
|
562 |
QCOMPARE(bits.count(), numBits);
|
|
563 |
QCOMPARE(bits.count(true), onBits);
|
|
564 |
QCOMPARE(bits.count(false), numBits - onBits);
|
|
565 |
|
|
566 |
stream << bits << bits << bits;
|
|
567 |
buffer.close();
|
|
568 |
|
|
569 |
QCOMPARE(stream.status(), QDataStream::Ok);
|
|
570 |
|
|
571 |
QVERIFY(buffer.open(QBuffer::ReadWrite));
|
|
572 |
QDataStream stream2(&buffer);
|
|
573 |
|
|
574 |
QBitArray array1, array2, array3;
|
|
575 |
stream2 >> array1 >> array2 >> array3;
|
|
576 |
|
|
577 |
QCOMPARE(array1.count(), numBits);
|
|
578 |
QCOMPARE(array1.count(true), onBits);
|
|
579 |
QCOMPARE(array1.count(false), numBits - onBits);
|
|
580 |
|
|
581 |
QCOMPARE(array1, bits);
|
|
582 |
QCOMPARE(array2, bits);
|
|
583 |
QCOMPARE(array3, bits);
|
|
584 |
}
|
|
585 |
|
|
586 |
void tst_QBitArray::invertOnNull() const
|
|
587 |
{
|
|
588 |
QBitArray a;
|
|
589 |
QCOMPARE(a = ~a, QBitArray());
|
|
590 |
}
|
|
591 |
|
|
592 |
void tst_QBitArray::operator_noteq_data()
|
|
593 |
{
|
|
594 |
QTest::addColumn<QBitArray>("input1");
|
|
595 |
QTest::addColumn<QBitArray>("input2");
|
|
596 |
QTest::addColumn<bool>("res");
|
|
597 |
|
|
598 |
QTest::newRow("data0") << QStringToQBitArray(QString("11111111"))
|
|
599 |
<< QStringToQBitArray(QString("00101100"))
|
|
600 |
<< true;
|
|
601 |
|
|
602 |
QTest::newRow("data1") << QStringToQBitArray(QString("11011011"))
|
|
603 |
<< QStringToQBitArray(QString("11011011"))
|
|
604 |
<< false;
|
|
605 |
|
|
606 |
QTest::newRow("data2") << QStringToQBitArray(QString())
|
|
607 |
<< QStringToQBitArray(QString("00101100111"))
|
|
608 |
<< true;
|
|
609 |
|
|
610 |
QTest::newRow("data3") << QStringToQBitArray(QString())
|
|
611 |
<< QStringToQBitArray(QString())
|
|
612 |
<< false;
|
|
613 |
|
|
614 |
QTest::newRow("data4") << QStringToQBitArray(QString("00101100"))
|
|
615 |
<< QStringToQBitArray(QString("11111111"))
|
|
616 |
<< true;
|
|
617 |
|
|
618 |
QTest::newRow("data5") << QStringToQBitArray(QString("00101100111"))
|
|
619 |
<< QStringToQBitArray(QString())
|
|
620 |
<< true;
|
|
621 |
}
|
|
622 |
|
|
623 |
void tst_QBitArray::operator_noteq()
|
|
624 |
{
|
|
625 |
QFETCH(QBitArray, input1);
|
|
626 |
QFETCH(QBitArray, input2);
|
|
627 |
QFETCH(bool, res);
|
|
628 |
|
|
629 |
bool b = input1 != input2;
|
|
630 |
QCOMPARE(b, res);
|
|
631 |
}
|
|
632 |
|
|
633 |
void tst_QBitArray::resize()
|
|
634 |
{
|
|
635 |
// -- check that a resize handles the bits correctly
|
|
636 |
QBitArray a = QStringToQBitArray(QString("11"));
|
|
637 |
a.resize(10);
|
|
638 |
QVERIFY(a.size() == 10);
|
|
639 |
QCOMPARE( a, QStringToQBitArray(QString("1100000000")) );
|
|
640 |
|
|
641 |
a.setBit(9);
|
|
642 |
a.resize(9);
|
|
643 |
// now the bit in a should have been gone:
|
|
644 |
QCOMPARE( a, QStringToQBitArray(QString("110000000")) );
|
|
645 |
|
|
646 |
// grow the array back and check the new bit
|
|
647 |
a.resize(10);
|
|
648 |
QCOMPARE( a, QStringToQBitArray(QString("1100000000")) );
|
|
649 |
|
|
650 |
// other test with and
|
|
651 |
a.resize(9);
|
|
652 |
QBitArray b = QStringToQBitArray(QString("1111111111"));
|
|
653 |
b &= a;
|
|
654 |
QCOMPARE( b, QStringToQBitArray(QString("1100000000")) );
|
|
655 |
|
|
656 |
}
|
|
657 |
|
|
658 |
QTEST_APPLESS_MAIN(tst_QBitArray)
|
|
659 |
#include "tst_qbitarray.moc"
|