From ad35a41739c8e1fb6db62ed37b764448b2e59ece Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Mon, 11 Jul 2011 23:16:58 +0200 Subject: Refactor QByteArray to allow for const data Similar refactoring as done for QString. Make shared_null read-only, and add support for compile time generated QByteArrayData. Add support for properly reserving capacity. Change-Id: Ie4c41d4caac7b3b4bb1aef40c1c860a30b82edb8 Reviewed-on: http://codereview.qt.nokia.com/1484 Reviewed-by: Qt Sanity Bot Reviewed-by: Olivier Goffart --- tests/auto/qbytearray/tst_qbytearray.cpp | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) (limited to 'tests/auto') diff --git a/tests/auto/qbytearray/tst_qbytearray.cpp b/tests/auto/qbytearray/tst_qbytearray.cpp index e2f64b84f8..78b655419e 100644 --- a/tests/auto/qbytearray/tst_qbytearray.cpp +++ b/tests/auto/qbytearray/tst_qbytearray.cpp @@ -145,6 +145,8 @@ private slots: void byteRefDetaching() const; void reserve(); + + void literals(); }; tst_QByteArray::tst_QByteArray() @@ -1527,14 +1529,38 @@ void tst_QByteArray::reserve() QVERIFY(qba.capacity() == capacity); char *data = qba.data(); - // FIXME count from 0 to make it fail - for (int i = 1; i < capacity; i++) { + for (int i = 0; i < capacity; i++) { qba.resize(i); QVERIFY(capacity == qba.capacity()); QVERIFY(data == qba.data()); } } +void tst_QByteArray::literals() +{ +#if defined(Q_COMPILER_LAMBDA) || defined(Q_CC_GNU) + QByteArray str(QByteArrayLiteral("abcd")); + + QVERIFY(str.length() == 4); + QVERIFY(str == "abcd"); + QVERIFY(str.data_ptr()->ref == -1); + QVERIFY(str.data_ptr()->offset == 0); + + const char *s = str.constData(); + QByteArray str2 = str; + QVERIFY(str2.constData() == s); + + // detach on non const access + QVERIFY(str.data() != s); + + QVERIFY(str2.constData() == s); + QVERIFY(str2.data() != s); + +#else + QSKIP("Only tested on c++0x compliant compiler or gcc", SkipAll); +#endif +} + const char globalChar = '1'; QTEST_APPLESS_MAIN(tst_QByteArray) -- cgit v1.2.3