Search code examples
qtunicodeqstringqtestlib

QTestLib results different from program output


I have some simple code that reverses a QString.

const QString reverse_qstring(const QString& str_in)
{
    QString out;
    Q_FOREACH(const QChar c, str_in) {
        out.push_front(c);
    }
    return out;
}

When I input text from the command line with non-ASCII characters, things go as expected:

"¿como estás?" :  "?sátse omoc¿"

However, when I make the following unit test (Using QTestLib):

QCOMPARE(reverse_qstring(QString("¿como estás?")), QString("?sátse omoc¿"));

I get:

FAIL!  : program::test_qstring() Compared values are not the same
   Actual (reverse_qstring(QString("??como est??s?"))): ?s??tse omoc??
   Expected (QString("?s??tse omoc??")): ?s??tse omoc??

Any ideas?


Solution

  • I think you can set the codec for Utf8: http://doc.qt.io/qt-4.8/qtextcodec.html#setCodecForCStrings

    Or you can use this instead: QString::fromUtf8("?sátse omoc¿")