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