From 6ce9404a6e7ad6ba3ff37f6890fe400c643c3d52 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 1 Aug 2019 22:56:30 -0700 Subject: QBitArray: fix fromBits() and actually test it When I initially added it, it was ony for QCborValue, but I never added the tests. Turns out there were two bugs: [ChangeLog][QtCore][QBitArray] Fixed two bugs that caused QBitArrays created using fromBits() not to compare equal to the equivalent QBitArray created using other methods if the size was zero or not a multiple of 4. If the size modulus 8 was 5, 6, or 7, the data was actually incorrect. Fixes: QTBUG-77285 Change-Id: Ife213d861bb14c1787e1fffd15b70573d162042c Reviewed-by: Lars Knoll --- .../auto/corelib/tools/qbitarray/tst_qbitarray.cpp | 57 ++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'tests/auto/corelib/tools') diff --git a/tests/auto/corelib/tools/qbitarray/tst_qbitarray.cpp b/tests/auto/corelib/tools/qbitarray/tst_qbitarray.cpp index d19eac7530..9a7c099228 100644 --- a/tests/auto/corelib/tools/qbitarray/tst_qbitarray.cpp +++ b/tests/auto/corelib/tools/qbitarray/tst_qbitarray.cpp @@ -84,6 +84,8 @@ private slots: void operator_noteq(); void resize(); + void fromBits_data(); + void fromBits(); }; void tst_QBitArray::size_data() @@ -610,5 +612,60 @@ void tst_QBitArray::resize() } +void tst_QBitArray::fromBits_data() +{ + QTest::addColumn("data"); + QTest::addColumn("size"); + QTest::addColumn("expected"); + + QTest::newRow("empty") << QByteArray() << 0 << QBitArray(); + + auto add = [](const QByteArray &tag, const char *data) { + QTest::newRow(tag) << QByteArray(data, (tag.size() + 7) / 8) << tag.size() + << QStringToQBitArray(tag); + }; + + // "0" to "0000000000000000" + for (int i = 1; i < 16; ++i) { + char zero[2] = { 0, 0 }; + QByteArray pattern(i, '0'); + add(pattern, zero); + } + + // "1" to "1111111111111111" + for (int i = 1; i < 16; ++i) { + char one[2] = { '\xff', '\xff' }; + QByteArray pattern(i, '1'); + add(pattern, one); + } + + // trailing 0 and 1 + char zero = 1; + char one = 0; + QByteArray pzero = "1"; + QByteArray pone = "0"; + for (int i = 2; i < 8; ++i) { + zero <<= 1; + pzero.prepend('0'); + add(pzero, &zero); + + one = (one << 1) | 1; + pone.prepend('1'); + add(pone, &one); + } +} + +void tst_QBitArray::fromBits() +{ + QFETCH(QByteArray, data); + QFETCH(int, size); + QFETCH(QBitArray, expected); + + QBitArray fromBits = QBitArray::fromBits(data, size); + QCOMPARE(fromBits, expected); + + QCOMPARE(QBitArray::fromBits(fromBits.bits(), fromBits.size()), expected); +} + QTEST_APPLESS_MAIN(tst_QBitArray) #include "tst_qbitarray.moc" -- cgit v1.2.3