From 92b112475247d42b221b03953b11b060fdf35538 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C4=99drzej=20Nowacki?= Date: Tue, 31 Jan 2012 14:16:52 +0100 Subject: [PATCH] Add movability tests for QByteArray. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit QByteArray is declared as movable but it was not tested before. Change-Id: I4fb636f8705c3fd792a768872206203ee5fd4ddb Reviewed-by: João Abecasis --- .../corelib/tools/qbytearray/tst_qbytearray.cpp | 82 +++++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) diff --git a/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp b/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp index c50deb6..c1598c4 100644 --- a/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp +++ b/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp @@ -130,7 +130,8 @@ private slots: void byteRefDetaching() const; void reserve(); - + void movablity_data(); + void movablity(); void literals(); }; @@ -1497,6 +1498,85 @@ void tst_QByteArray::reserve() nil2.reserve(0); } +void tst_QByteArray::movablity_data() +{ + QTest::addColumn("array"); + + QTest::newRow("0x00000000") << QByteArray("\x00\x00\x00\x00", 4); + QTest::newRow("0x000000ff") << QByteArray("\x00\x00\x00\xff", 4); + QTest::newRow("0xffffffff") << QByteArray("\xff\xff\xff\xff", 4); + QTest::newRow("empty") << QByteArray(""); + QTest::newRow("null") << QByteArray(); + QTest::newRow("sss") << QByteArray(3, 's'); +} + +void tst_QByteArray::movablity() +{ + QFETCH(QByteArray, array); + + QVERIFY(!QTypeInfo::isStatic); + + const int size = array.size(); + const bool isEmpty = array.isEmpty(); + const bool isNull = array.isNull(); + const int capacity = array.capacity(); + + QByteArray memSpace; + + // we need only memory space not the instance + memSpace.~QByteArray(); + // move array -> memSpace + memcpy(&memSpace, &array, sizeof(QByteArray)); + // reconstruct empty QByteArray + new (&array) QByteArray; + + QCOMPARE(memSpace.size(), size); + QCOMPARE(memSpace.isEmpty(), isEmpty); + QCOMPARE(memSpace.isNull(), isNull); + QCOMPARE(memSpace.capacity(), capacity); + + // try to not crash + memSpace.toLower(); + memSpace.toUpper(); + memSpace.prepend('a'); + memSpace.append("b", 1); + memSpace.squeeze(); + memSpace.reserve(array.size() + 16); + + QByteArray copy(memSpace); + + // reinitialize base values + const int newSize = size + 2; + const bool newIsEmpty = false; + const bool newIsNull = false; + const int newCapacity = 16; + + // move back memSpace -> array + array.~QByteArray(); + memcpy(&array, &memSpace, sizeof(QByteArray)); + // reconstruct empty QByteArray + new (&memSpace) QByteArray; + + QCOMPARE(array.size(), newSize); + QCOMPARE(array.isEmpty(), newIsEmpty); + QCOMPARE(array.isNull(), newIsNull); + QCOMPARE(array.capacity(), newCapacity); + QVERIFY(array.startsWith("a")); + QVERIFY(array.endsWith("b")); + + QCOMPARE(copy.size(), newSize); + QCOMPARE(copy.isEmpty(), newIsEmpty); + QCOMPARE(copy.isNull(), newIsNull); + QCOMPARE(copy.capacity(), newCapacity); + QVERIFY(copy.startsWith("a")); + QVERIFY(copy.endsWith("b")); + + // try to not crash + array.squeeze(); + array.reserve(array.size() + 3); + QVERIFY(true); +} + void tst_QByteArray::literals() { #if defined(Q_COMPILER_LAMBDA) || defined(Q_CC_GNU) -- 2.7.4