diff options
author | João Abecasis <joao.abecasis@nokia.com> | 2012-02-05 21:25:31 +0100 |
---|---|---|
committer | João Abecasis <joao.abecasis@nokia.com> | 2012-02-05 21:26:33 +0100 |
commit | d065dfd454890c332482a6109ed34a989e50809b (patch) | |
tree | 0d3e9c5367c1fad7d089d368182374e8d1d46ca1 /tests/auto/corelib/tools/qbytearray | |
parent | 632840cb0f5ad355d87fc040b015d04af86371ec (diff) | |
parent | 9f54846d951838361f4188b423e7aa7c7b9a9540 (diff) |
Merge remote-tracking branch 'gerrit/master' into containers
Conflicts:
src/corelib/tools/qstring.cpp
Change-Id: I23d214bf33c2badfae1876da3cc7d6d8f6e635fb
Diffstat (limited to 'tests/auto/corelib/tools/qbytearray')
-rw-r--r-- | tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp | 86 |
1 files changed, 83 insertions, 3 deletions
diff --git a/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp b/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp index 5b660ecd3f..585d6afa44 100644 --- a/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp +++ b/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp @@ -1,8 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. -** Contact: Nokia Corporation (qt-info@nokia.com) +** Contact: http://www.qt-project.org/ ** ** This file is part of the test suite of the Qt Toolkit. ** @@ -35,6 +34,7 @@ ** ** ** +** ** $QT_END_LICENSE$ ** ****************************************************************************/ @@ -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) |