diff options
author | Jędrzej Nowacki <jedrzej.nowacki@nokia.com> | 2012-01-31 14:16:52 +0100 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-02-05 13:07:56 +0100 |
commit | 92b112475247d42b221b03953b11b060fdf35538 (patch) | |
tree | 627f8f3d2ebcb314d0e069d1e6deda1eb987398b /tests | |
parent | c9319f17f4176ea895979dd11e5e1304f14660fc (diff) |
Add movability tests for QByteArray.
QByteArray is declared as movable but it was not tested before.
Change-Id: I4fb636f8705c3fd792a768872206203ee5fd4ddb
Reviewed-by: João Abecasis <joao.abecasis@nokia.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp | 82 |
1 files changed, 81 insertions, 1 deletions
diff --git a/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp b/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp index c50deb63c2..c1598c452d 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<QByteArray>("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<QByteArray>::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) |