Skip to content

Commit

Permalink
Add some extra tests to tst_QString to ensure the encoding is correct
Browse files Browse the repository at this point in the history
This also tests by consequence that the behaviour of QByteArrays
containing NULs is consistent. Right now, that means the QByteArray
processing stops at the NUL, which is the same behaviour as if a
pointer to the byte array's data were used. (it's what happens if
there's no QByteArray overload and the const char* one is called)

Change-Id: If56a822f95866e8cb5b153d07b48198bb83fb386
Reviewed-by: Lars Knoll <[email protected]>
  • Loading branch information
thiagomacieira authored and Qt by Nokia committed May 7, 2012
1 parent 8ddd8c8 commit c48876a
Showing 1 changed file with 193 additions and 3 deletions.
196 changes: 193 additions & 3 deletions tests/auto/corelib/tools/qstring/tst_qstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ private slots:
void append_bytearray();
void operator_pluseq_bytearray_data();
void operator_pluseq_bytearray();
void operator_eqeq_bytearray_data();
void operator_eqeq_bytearray();
void operator_eqeq_nullstring();
void operator_smaller();
void insert();
Expand Down Expand Up @@ -226,14 +228,13 @@ private slots:
void repeated_data() const;
void compareRef();
void arg_locale();

void toUpperLower_icu();
void literals();

void eightBitLiterals_data();
void eightBitLiterals();
void reserve();
void toHtmlEscaped_data();
void toHtmlEscaped();

void operatorGreaterWithQLatin1String();
void compareQLatin1Strings();
void fromQLatin1StringWithLength();
Expand Down Expand Up @@ -878,6 +879,9 @@ void tst_QString::constructorQByteArray_data()
QTest::newRow( "2" ) << ba1 << QString("abc");

QTest::newRow( "3" ) << QByteArray::fromRawData("abcd", 3) << QString("abc");
QTest::newRow( "4" ) << QByteArray("\xc3\xa9") << QString("\xc3\xa9");
QTest::newRow( "4-bis" ) << QByteArray("\xc3\xa9") << QString::fromUtf8("\xc3\xa9");
QTest::newRow( "4-tre" ) << QByteArray("\xc3\xa9") << QString::fromLatin1("\xe9");
}

void tst_QString::constructorQByteArray()
Expand All @@ -891,6 +895,17 @@ void tst_QString::constructorQByteArray()

QString strBA(src);
QCOMPARE( strBA, expected );

// test operator= too
if (src.constData()[src.length()] == '\0') {
str1.clear();
str1 = src.constData();
QCOMPARE( str1, expected );
}

strBA.clear();
strBA = src;
QCOMPARE( strBA, expected );
}

void tst_QString::STL()
Expand Down Expand Up @@ -2025,6 +2040,10 @@ void tst_QString::append_bytearray_data()
// empty byte array
ba.resize( 0 );
QTest::newRow( "emptyByteArray" ) << QString("foobar ") << ba << QString("foobar ");

// non-ascii byte array
QTest::newRow( "nonAsciiByteArray") << QString() << QByteArray("\xc3\xa9") << QString("\xc3\xa9");
QTest::newRow( "nonAsciiByteArray2") << QString() << QByteArray("\xc3\xa9") << QString::fromUtf8("\xc3\xa9");
}

void tst_QString::append_bytearray()
Expand All @@ -2045,6 +2064,14 @@ void tst_QString::append_bytearray()

QTEST( str, "res" );
}

QFETCH( QByteArray, ba );
if (ba.constData()[ba.length()] == '\0') {
QFETCH( QString, str );

str.append(ba.constData());
QTEST( str, "res" );
}
}

void tst_QString::operator_pluseq_bytearray_data()
Expand All @@ -2070,6 +2097,33 @@ void tst_QString::operator_pluseq_bytearray()

QTEST( str, "res" );
}

QFETCH( QByteArray, ba );
if (ba.constData()[ba.length()] == '\0') {
QFETCH( QString, str );

str += ba.constData();
QTEST( str, "res" );
}
}

void tst_QString::operator_eqeq_bytearray_data()
{
constructorQByteArray_data();
}

void tst_QString::operator_eqeq_bytearray()
{
QFETCH(QByteArray, src);
QFETCH(QString, expected);

QVERIFY(expected == src);
QVERIFY(!(expected != src));

if (src.constData()[src.length()] == '\0') {
QVERIFY(expected == src.constData());
QVERIFY(!(expected != src.constData()));
}
}

void tst_QString::swap()
Expand Down Expand Up @@ -2109,6 +2163,10 @@ void tst_QString::prepend_bytearray_data()
// empty byte array
ba.resize( 0 );
QTest::newRow( "emptyByteArray" ) << QString(" foobar") << ba << QString(" foobar");

// non-ascii byte array
QTest::newRow( "nonAsciiByteArray") << QString() << QByteArray("\xc3\xa9") << QString("\xc3\xa9");
QTest::newRow( "nonAsciiByteArray2") << QString() << QByteArray("\xc3\xa9") << QString::fromUtf8("\xc3\xa9");
}

void tst_QString::prepend_bytearray()
Expand All @@ -2130,6 +2188,14 @@ void tst_QString::prepend_bytearray()

QTEST( str, "res" );
}

QFETCH( QByteArray, ba );
if (ba.constData()[ba.length()] == '\0') {
QFETCH( QString, str );

str.prepend(ba.constData());
QTEST( str, "res" );
}
}

void tst_QString::replace_uint_uint()
Expand Down Expand Up @@ -3136,6 +3202,13 @@ void tst_QString::startsWith()
QVERIFY( !a.startsWith(QLatin1Char('x')) );
QVERIFY( !a.startsWith(QChar()) );

// this test is independent of encoding
a = "\xc3\xa9";
QVERIFY( a.startsWith("\xc3\xa9") );
QVERIFY( !a.startsWith("\xc3\xa1") );

// this one is dependent of encoding
QVERIFY( a.startsWith("\xc3\x89", Qt::CaseInsensitive) );
}

void tst_QString::endsWith()
Expand Down Expand Up @@ -3238,6 +3311,14 @@ void tst_QString::endsWith()
QVERIFY( !a.endsWith(QLatin1Char(0)) );
QVERIFY( !a.endsWith(QLatin1Char('x')) );
QVERIFY( !a.endsWith(QChar()) );

// this test is independent of encoding
a = "\xc3\xa9";
QVERIFY( a.endsWith("\xc3\xa9") );
QVERIFY( !a.endsWith("\xc3\xa1") );

// this one is dependent of encoding
QVERIFY( a.endsWith("\xc3\x89", Qt::CaseInsensitive) );
}

void tst_QString::check_QDataStream()
Expand Down Expand Up @@ -4159,6 +4240,10 @@ void tst_QString::operator_smaller()
QVERIFY( !(foo > QLatin1String("z")));
QVERIFY( !(QLatin1String("z") < foo));
QVERIFY( (QLatin1String("z") > foo));

// operator< is not locale-aware (or shouldn't be)
QVERIFY( foo < QString("\xc3\xa9") );
QVERIFY( foo < "\xc3\xa9" );
}

void tst_QString::integer_conversion_data()
Expand Down Expand Up @@ -5286,6 +5371,111 @@ void tst_QString::literals()
#endif
}

void tst_QString::eightBitLiterals_data()
{
QTest::addColumn<QByteArray>("data");
QTest::addColumn<QString>("stringData");

QTest::newRow("null") << QByteArray() << QString();
QTest::newRow("empty") << QByteArray("") << QString("");
QTest::newRow("regular") << QByteArray("foo") << "foo";
QTest::newRow("non-ascii") << QByteArray("\xc3\xa9") << QString::fromLatin1("\xe9");
}

void tst_QString::eightBitLiterals()
{
QFETCH(QByteArray, data);
QFETCH(QString, stringData);

{
QString s(data);
QCOMPARE(s, stringData);
}
{
QString s(data.constData());
QCOMPARE(s, stringData);
}
{
QString s;
s = data;
QCOMPARE(s, stringData);
}
{
QString s;
s = data.constData();
QCOMPARE(s, stringData);
}
{
QString s;
s.append(data);
QCOMPARE(s, stringData);
}
{
QString s;
s.append(data.constData());
QCOMPARE(s, stringData);
}
{
QString s;
s += data;
QCOMPARE(s, stringData);
}
{
QString s;
s += data.constData();
QCOMPARE(s, stringData);
}
{
QString s;
s.prepend(data);
QCOMPARE(s, stringData);
}
{
QString s;
s.prepend(data.constData());
QCOMPARE(s, stringData);
}
{
QString s = QString() + data;
QCOMPARE(s, stringData);
}
{
QString s = QString() + data.constData();
QCOMPARE(s, stringData);
}
{
QString s = data + QString();
QCOMPARE(s, stringData);
}
{
QString s = QString() % data;
QCOMPARE(s, stringData);
}
{
QString s = QString() % data.constData();
QCOMPARE(s, stringData);
}
{
QString s = data % QString();
QCOMPARE(s, stringData);
}

{
QVERIFY(stringData == data);
QVERIFY(stringData == data.constData());
QVERIFY(!(stringData != data));
QVERIFY(!(stringData != data.constData()));
QVERIFY(!(stringData < data));
QVERIFY(!(stringData < data.constData()));
QVERIFY(!(stringData > data));
QVERIFY(!(stringData > data.constData()));
QVERIFY(stringData <= data);
QVERIFY(stringData <= data.constData());
QVERIFY(stringData >= data);
QVERIFY(stringData >= data.constData());
}
}

void tst_QString::reserve()
{
QString nil1, nil2;
Expand Down

0 comments on commit c48876a

Please sign in to comment.