tests/auto/qstringbuilder1/stringbuilder.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
    37 **
    37 **
    38 ** $QT_END_LICENSE$
    38 ** $QT_END_LICENSE$
    39 **
    39 **
    40 ****************************************************************************/
    40 ****************************************************************************/
    41 
    41 
    42 // This is included in various .cpp files as a compile test for various scenarios
       
    43 // depending on NO_CAST_*  and  QT_USE_FAST_OPERATOR_PLUS and QT_USE_FAST_CONCATENATION
       
    44 
       
    45 #if SCENARIO == 1
       
    46 // this is the "no harm done" version. Only operator% is active,
       
    47 // with NO_CAST * defined
       
    48 #define P %
       
    49 #undef QT_USE_FAST_OPERATOR_PLUS
       
    50 #undef QT_USE_FAST_CONCATENATION
       
    51 #define QT_NO_CAST_FROM_ASCII
       
    52 #define QT_NO_CAST_TO_ASCII
       
    53 #endif
       
    54 
       
    55 
       
    56 #if SCENARIO == 2
       
    57 // this is the "full" version. Operator+ is replaced by a QStringBuilder
       
    58 // based version
       
    59 // with NO_CAST * defined
       
    60 #define P +
       
    61 #define QT_USE_FAST_OPERATOR_PLUS
       
    62 #define QT_USE_FAST_CONCATENATION
       
    63 #define QT_NO_CAST_FROM_ASCII
       
    64 #define QT_NO_CAST_TO_ASCII
       
    65 #endif
       
    66 
       
    67 #if SCENARIO == 3
       
    68 // this is the "no harm done" version. Only operator% is active,
       
    69 // with NO_CAST * _not_ defined
       
    70 #define P %
       
    71 #undef QT_USE_FAST_OPERATOR_PLUS
       
    72 #undef QT_USE_FAST_CONCATENATION
       
    73 #undef QT_NO_CAST_FROM_ASCII
       
    74 #undef QT_NO_CAST_TO_ASCII
       
    75 #endif
       
    76 
       
    77 #if SCENARIO == 4
       
    78 // this is the "full" version. Operator+ is replaced by a QStringBuilder
       
    79 // based version
       
    80 // with NO_CAST * _not_ defined
       
    81 #define P +
       
    82 #define QT_USE_FAST_OPERATOR_PLUS
       
    83 #define QT_USE_FAST_CONCATENATION
       
    84 #undef QT_NO_CAST_FROM_ASCII
       
    85 #undef QT_NO_CAST_TO_ASCII
       
    86 #endif
       
    87 
       
    88 #include <QtTest/QtTest>
       
    89 #include "stringbuilder.h"
       
    90 
       
    91 //TESTED_CLASS=QStringBuilder
       
    92 //TESTED_FILES=qstringbuilder.cpp
       
    93 
       
    94 #define LITERAL "some literal"
    42 #define LITERAL "some literal"
    95 
    43 
    96 void tst_QStringBuilder::scenario()
    44 // "some literal", but replacing all vocals by their umlauted UTF-8 string :)
       
    45 #define UTF8_LITERAL "s\xc3\xb6m\xc3\xab l\xc3\xaft\xc3\xabr\xc3\xa4l"
       
    46 
       
    47 void runScenario()
    97 {
    48 {
       
    49     // set codec for C strings to 0, enforcing Latin1
       
    50     QTextCodec::setCodecForCStrings(0);
       
    51     QVERIFY(!QTextCodec::codecForCStrings());
       
    52 
    98     QLatin1Literal l1literal(LITERAL);
    53     QLatin1Literal l1literal(LITERAL);
    99     QLatin1String l1string(LITERAL);
    54     QLatin1String l1string(LITERAL);
   100     QString string(l1string);
    55     QString string(l1string);
   101     QStringRef stringref(&string, 2, 10);
    56     QStringRef stringref(&string, 2, 10);
   102     QLatin1Char achar('c');
    57     QLatin1Char achar('c');
   125     QCOMPARE(r, r2);
    80     QCOMPARE(r, r2);
   126     r = ba P string;
    81     r = ba P string;
   127     QCOMPARE(r, r2);
    82     QCOMPARE(r, r2);
   128     r = string P ba;
    83     r = string P ba;
   129     QCOMPARE(r, r2);
    84     QCOMPARE(r, r2);
       
    85 
       
    86     // now test with codec for C strings set
       
    87     QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
       
    88     QVERIFY(QTextCodec::codecForCStrings());
       
    89     QCOMPARE(QTextCodec::codecForCStrings()->name(), QByteArray("UTF-8"));
       
    90 
       
    91     string = QString::fromUtf8(UTF8_LITERAL);
       
    92     r2 = QString::fromUtf8(UTF8_LITERAL UTF8_LITERAL);
       
    93     ba = UTF8_LITERAL;
       
    94 
       
    95     r = string P UTF8_LITERAL;
       
    96     QCOMPARE(r.size(), r2.size());
       
    97     QCOMPARE(r, r2);
       
    98     r = UTF8_LITERAL P string;
       
    99     QCOMPARE(r, r2);
       
   100     r = ba P string;
       
   101     QCOMPARE(r, r2);
       
   102     r = string P ba;
       
   103     QCOMPARE(r, r2);
   130 #endif
   104 #endif
   131 }
   105 }
   132 
       
   133 QTEST_APPLESS_MAIN(tst_QStringBuilder)