tests/auto/qstring/tst_qstring.cpp
changeset 30 5dc02b23752f
parent 19 fcece45ef507
child 33 3e2da88830cd
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
   118     void operator_pluseq_bytearray_data();
   118     void operator_pluseq_bytearray_data();
   119     void operator_pluseq_bytearray();
   119     void operator_pluseq_bytearray();
   120     void operator_eqeq_nullstring();
   120     void operator_eqeq_nullstring();
   121     void operator_smaller();
   121     void operator_smaller();
   122     void insert();
   122     void insert();
       
   123     void simplified_data();
   123     void simplified();
   124     void simplified();
   124     void trimmed();
   125     void trimmed();
   125     void toLower();
   126     void toLower();
   126     void toUpper();
   127     void toUpper();
   127     void rightJustified();
   128     void rightJustified();
   162     void nullFromUtf8();
   163     void nullFromUtf8();
   163     void fromLocal8Bit_data();
   164     void fromLocal8Bit_data();
   164     void fromLocal8Bit();
   165     void fromLocal8Bit();
   165     void local8Bit_data();
   166     void local8Bit_data();
   166     void local8Bit();
   167     void local8Bit();
       
   168     void fromLatin1Roundtrip_data();
       
   169     void fromLatin1Roundtrip();
       
   170     void toLatin1Roundtrip_data();
       
   171     void toLatin1Roundtrip();
   167     void fromLatin1();
   172     void fromLatin1();
   168     void fromAscii();
   173     void fromAscii();
   169     void arg();
   174     void arg();
   170     void number();
   175     void number();
   171     void arg_fillChar_data();
   176     void arg_fillChar_data();
   931     QCOMPARE(a.sprintf("%-5.5s", "Hello" ),(QString)"Hello");
   936     QCOMPARE(a.sprintf("%-5.5s", "Hello" ),(QString)"Hello");
   932 
   937 
   933     // Check utf8 conversion for %s
   938     // Check utf8 conversion for %s
   934     QCOMPARE(a.sprintf("%s", "\303\266\303\244\303\274\303\226\303\204\303\234\303\270\303\246\303\245\303\230\303\206\303\205"), QString("\366\344\374\326\304\334\370\346\345\330\306\305"));
   939     QCOMPARE(a.sprintf("%s", "\303\266\303\244\303\274\303\226\303\204\303\234\303\270\303\246\303\245\303\230\303\206\303\205"), QString("\366\344\374\326\304\334\370\346\345\330\306\305"));
   935 
   940 
       
   941     // Check codecForCStrings is used to read non-modifier sequences in the format string
       
   942     QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
       
   943     QCOMPARE(a.sprintf("\303\251\303\250\303\240 %s", "\303\251\303\250\303\240"), QString("\303\251\303\250\303\240 \303\251\303\250\303\240"));
       
   944     QTextCodec::setCodecForCStrings(0);
       
   945 
   936     int n1;
   946     int n1;
   937     a.sprintf("%s%n%s", "hello", &n1, "goodbye");
   947     a.sprintf("%s%n%s", "hello", &n1, "goodbye");
   938     QCOMPARE(n1, 5);
   948     QCOMPARE(n1, 5);
   939     QCOMPARE(a, QString("hellogoodbye"));
   949     QCOMPARE(a, QString("hellogoodbye"));
   940     qlonglong n2;
   950     qlonglong n2;
  1590     QCOMPARE(a,(QString)" ");
  1600     QCOMPARE(a,(QString)" ");
  1591     a=" a   ";
  1601     a=" a   ";
  1592     QCOMPARE(a.trimmed(),(QString)"a");
  1602     QCOMPARE(a.trimmed(),(QString)"a");
  1593 }
  1603 }
  1594 
  1604 
       
  1605 void tst_QString::simplified_data()
       
  1606 {
       
  1607     QTest::addColumn<QString>("full" );
       
  1608     QTest::addColumn<QString>("simple" );
       
  1609 
       
  1610     QTest::newRow("null") << QString() << QString();
       
  1611     QTest::newRow("empty") << "" << "";
       
  1612     QTest::newRow("one char") << "a" << "a";
       
  1613     QTest::newRow("one word") << "foo" << "foo";
       
  1614     QTest::newRow("chars trivial") << "a b" << "a b";
       
  1615     QTest::newRow("words trivial") << "foo bar" << "foo bar";
       
  1616     QTest::newRow("allspace") << "  \t\v " << "";
       
  1617     QTest::newRow("char trailing") << "a " << "a";
       
  1618     QTest::newRow("char trailing tab") << "a\t" << "a";
       
  1619     QTest::newRow("char multitrailing") << "a   " << "a";
       
  1620     QTest::newRow("char multitrailing tab") << "a   \t" << "a";
       
  1621     QTest::newRow("char leading") << " a" << "a";
       
  1622     QTest::newRow("char leading tab") << "\ta" << "a";
       
  1623     QTest::newRow("char multileading") << "   a" << "a";
       
  1624     QTest::newRow("char multileading tab") << "\t   a" << "a";
       
  1625     QTest::newRow("chars apart") << "a  b" << "a b";
       
  1626     QTest::newRow("words apart") << "foo  bar" << "foo bar";
       
  1627     QTest::newRow("enclosed word") << "   foo \t " << "foo";
       
  1628     QTest::newRow("enclosed chars apart") << " a   b " << "a b";
       
  1629     QTest::newRow("enclosed words apart") << " foo   bar " << "foo bar";
       
  1630     QTest::newRow("chars apart posttab") << "a \tb" << "a b";
       
  1631     QTest::newRow("chars apart pretab") << "a\t b" << "a b";
       
  1632     QTest::newRow("many words") << "  just some    random\ttext here" << "just some random text here";
       
  1633 }
       
  1634 
  1595 void tst_QString::simplified()
  1635 void tst_QString::simplified()
  1596 {
  1636 {
  1597     QString j;
  1637     QFETCH(QString, full);
  1598     j.simplified();
  1638     QFETCH(QString, simple);
  1599 
  1639 
  1600     QString a;
  1640     QString result = full.simplified();
  1601     a = "a ";
  1641     if (simple.isNull()) {
  1602     QCOMPARE(a.simplified(),(QString)"a");
  1642         QVERIFY2(result.isNull(), qPrintable("'" + full + "' did not yield null: " + result));
  1603     a=" a   b ";
  1643     } else if (simple.isEmpty()) {
  1604     QCOMPARE(a.simplified(),(QString)"a b");
  1644         QVERIFY2(result.isEmpty() && !result.isNull(), qPrintable("'" + full + "' did not yield empty: " + result));
       
  1645     } else {
       
  1646         QCOMPARE(result, simple);
       
  1647         if (full == simple)
       
  1648             QVERIFY(result.isSharedWith(full));
       
  1649     }
  1605 }
  1650 }
  1606 
  1651 
  1607 void tst_QString::insert()
  1652 void tst_QString::insert()
  1608 {
  1653 {
  1609     QString a;
  1654     QString a;
  3184 {
  3229 {
  3185     QFETCH(QString, local8Bit);
  3230     QFETCH(QString, local8Bit);
  3186     QFETCH(QByteArray, result);
  3231     QFETCH(QByteArray, result);
  3187 
  3232 
  3188     QCOMPARE(local8Bit.toLocal8Bit(), QByteArray(result));
  3233     QCOMPARE(local8Bit.toLocal8Bit(), QByteArray(result));
       
  3234 }
       
  3235 
       
  3236 void tst_QString::fromLatin1Roundtrip_data()
       
  3237 {
       
  3238     QTest::addColumn<QByteArray>("latin1");
       
  3239     QTest::addColumn<QString>("unicode");
       
  3240 
       
  3241     QTest::newRow("null") << QByteArray() << QString();
       
  3242     QTest::newRow("empty") << QByteArray("") << "";
       
  3243 
       
  3244     static const ushort unicode1[] = { 'H', 'e', 'l', 'l', 'o', 1, '\r', '\n', 0x7f };
       
  3245     QTest::newRow("ascii-only") << QByteArray("Hello") << QString::fromUtf16(unicode1, 5);
       
  3246     QTest::newRow("ascii+control") << QByteArray("Hello\1\r\n\x7f") << QString::fromUtf16(unicode1, 9);
       
  3247 
       
  3248     static const ushort unicode3[] = { 'a', 0, 'z' };
       
  3249     QTest::newRow("ascii+nul") << QByteArray("a\0z", 3) << QString::fromUtf16(unicode3, 3);
       
  3250 
       
  3251     static const ushort unicode4[] = { 0x80, 0xc0, 0xff };
       
  3252     QTest::newRow("non-ascii") << QByteArray("\x80\xc0\xff") << QString::fromUtf16(unicode4, 3);
       
  3253 }
       
  3254 
       
  3255 void tst_QString::fromLatin1Roundtrip()
       
  3256 {
       
  3257     QFETCH(QByteArray, latin1);
       
  3258     QFETCH(QString, unicode);
       
  3259 
       
  3260     // QtTest safety check:
       
  3261     Q_ASSERT(latin1.isNull() == unicode.isNull());
       
  3262     Q_ASSERT(latin1.isEmpty() == unicode.isEmpty());
       
  3263     Q_ASSERT(latin1.length() == unicode.length());
       
  3264 
       
  3265     if (!latin1.isEmpty())
       
  3266         while (latin1.length() < 128) {
       
  3267             latin1 += latin1;
       
  3268             unicode += unicode;
       
  3269         }
       
  3270 
       
  3271     // fromLatin1
       
  3272     QCOMPARE(QString::fromLatin1(latin1, latin1.length()).length(), unicode.length());
       
  3273     QCOMPARE(QString::fromLatin1(latin1, latin1.length()), unicode);
       
  3274 
       
  3275     // and back:
       
  3276     QCOMPARE(unicode.toLatin1().length(), latin1.length());
       
  3277     QCOMPARE(unicode.toLatin1(), latin1);
       
  3278 }
       
  3279 
       
  3280 void tst_QString::toLatin1Roundtrip_data()
       
  3281 {
       
  3282     QTest::addColumn<QByteArray>("latin1");
       
  3283     QTest::addColumn<QString>("unicodesrc");
       
  3284     QTest::addColumn<QString>("unicodedst");
       
  3285 
       
  3286     QTest::newRow("null") << QByteArray() << QString() << QString();
       
  3287     QTest::newRow("empty") << QByteArray("") << "" << "";
       
  3288 
       
  3289     static const ushort unicode1[] = { 'H', 'e', 'l', 'l', 'o', 1, '\r', '\n', 0x7f };
       
  3290     QTest::newRow("ascii-only") << QByteArray("Hello") << QString::fromUtf16(unicode1, 5) << QString::fromUtf16(unicode1, 5);
       
  3291     QTest::newRow("ascii+control") << QByteArray("Hello\1\r\n\x7f") << QString::fromUtf16(unicode1, 9)  << QString::fromUtf16(unicode1, 9);
       
  3292 
       
  3293     static const ushort unicode3[] = { 'a', 0, 'z' };
       
  3294     QTest::newRow("ascii+nul") << QByteArray("a\0z", 3) << QString::fromUtf16(unicode3, 3) << QString::fromUtf16(unicode3, 3);
       
  3295 
       
  3296     static const ushort unicode4[] = { 0x80, 0xc0, 0xff };
       
  3297     QTest::newRow("non-ascii") << QByteArray("\x80\xc0\xff") << QString::fromUtf16(unicode4, 3) << QString::fromUtf16(unicode4, 3);
       
  3298 
       
  3299     static const ushort unicodeq[] = { '?', '?', '?', '?', '?' };
       
  3300     const QString questionmarks = QString::fromUtf16(unicodeq, 5);
       
  3301 
       
  3302     static const ushort unicode5[] = { 0x100, 0x101, 0x17f, 0x7f00, 0x7f7f };
       
  3303     QTest::newRow("non-latin1a") << QByteArray("?????") << QString::fromUtf16(unicode5, 5) << questionmarks;
       
  3304 
       
  3305     static const ushort unicode6[] = { 0x180, 0x1ff, 0x8001, 0x8080, 0xfffc };
       
  3306     QTest::newRow("non-latin1b") << QByteArray("?????") << QString::fromUtf16(unicode6, 5) << questionmarks;
       
  3307 }
       
  3308 
       
  3309 void tst_QString::toLatin1Roundtrip()
       
  3310 {
       
  3311     QFETCH(QByteArray, latin1);
       
  3312     QFETCH(QString, unicodesrc);
       
  3313     QFETCH(QString, unicodedst);
       
  3314 
       
  3315     // QtTest safety check:
       
  3316     Q_ASSERT(latin1.isNull() == unicodesrc.isNull());
       
  3317     Q_ASSERT(latin1.isEmpty() == unicodesrc.isEmpty());
       
  3318     Q_ASSERT(latin1.length() == unicodesrc.length());
       
  3319     Q_ASSERT(latin1.isNull() == unicodedst.isNull());
       
  3320     Q_ASSERT(latin1.isEmpty() == unicodedst.isEmpty());
       
  3321     Q_ASSERT(latin1.length() == unicodedst.length());
       
  3322 
       
  3323     if (!latin1.isEmpty())
       
  3324         while (latin1.length() < 128) {
       
  3325             latin1 += latin1;
       
  3326             unicodesrc += unicodesrc;
       
  3327             unicodedst += unicodedst;
       
  3328         }
       
  3329 
       
  3330     // toLatin1
       
  3331     QCOMPARE(unicodesrc.toLatin1().length(), latin1.length());
       
  3332     QCOMPARE(unicodesrc.toLatin1(), latin1);
       
  3333 
       
  3334     // and back:
       
  3335     QCOMPARE(QString::fromLatin1(latin1, latin1.length()).length(), unicodedst.length());
       
  3336     QCOMPARE(QString::fromLatin1(latin1, latin1.length()), unicodedst);
  3189 }
  3337 }
  3190 
  3338 
  3191 void tst_QString::fromLatin1()
  3339 void tst_QString::fromLatin1()
  3192 {
  3340 {
  3193     QString a;
  3341     QString a;