From: Konstantin Ritt Date: Thu, 31 May 2012 09:46:18 +0000 (+0300) Subject: Improve the QTextBoundaryFinder autotests a lot X-Git-Tag: 071012110112~605 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=93ead3593997f9b67868170aa0f876eec30d3130;p=profile%2Fivi%2Fqtbase.git Improve the QTextBoundaryFinder autotests a lot The existing tests has been retained, the new ones has been added. Change-Id: I12ae1b4e63dde46f3b14a7c1423c13d5881d4507 Reviewed-by: Lars Knoll --- diff --git a/tests/auto/corelib/tools/qtextboundaryfinder/tst_qtextboundaryfinder.cpp b/tests/auto/corelib/tools/qtextboundaryfinder/tst_qtextboundaryfinder.cpp index e6f33eb..d8dd0f6 100644 --- a/tests/auto/corelib/tools/qtextboundaryfinder/tst_qtextboundaryfinder.cpp +++ b/tests/auto/corelib/tools/qtextboundaryfinder/tst_qtextboundaryfinder.cpp @@ -45,6 +45,7 @@ #include #include #include +#include class tst_QTextBoundaryFinder : public QObject { @@ -52,16 +53,23 @@ class tst_QTextBoundaryFinder : public QObject public slots: void init(); private slots: - void graphemeBoundaries(); - void wordBoundaries(); - void sentenceBoundaries(); - void isAtWordStart(); +#ifdef QT_BUILD_INTERNAL + void graphemeBoundariesDefault_data(); + void graphemeBoundariesDefault(); + void wordBoundariesDefault_data(); + void wordBoundariesDefault(); + void sentenceBoundariesDefault_data(); + void sentenceBoundariesDefault(); +#endif + + void wordBoundaries_manual_data(); + void wordBoundaries_manual(); + void sentenceBoundaries_manual_data(); + void sentenceBoundaries_manual(); + void lineBoundaries_manual_data(); + void lineBoundaries_manual(); + void fastConstructor(); - void isAtBoundaryLine(); - void toNextBoundary_data(); - void toNextBoundary(); - void toPreviousBoundary_data(); - void toPreviousBoundary(); void thaiLineBreak(); }; @@ -74,314 +82,444 @@ void tst_QTextBoundaryFinder::init() #endif } -void tst_QTextBoundaryFinder::graphemeBoundaries() -{ - QFile file("data/GraphemeBreakTest.txt"); - file.open(QFile::ReadOnly); - - int lines = 0; - while (!file.atEnd()) { - QByteArray line = file.readLine(); - if (line.startsWith('#')) - continue; - - lines++; - QString test = QString::fromUtf8(line); - int hash = test.indexOf('#'); - if (hash > 0) - test = test.left(hash); - test = test.simplified(); - test = test.replace(QLatin1String(" "), QString()); +Q_DECLARE_METATYPE(QList) - QList breakPositions; - QString testString; - int pos = 0; - int strPos = 0; - while (pos < test.length()) { - if (test.at(pos).unicode() == 0xf7) - breakPositions.append(strPos); - else - QVERIFY(test.at(pos).unicode() == 0xd7); - ++pos; - if (pos < test.length()) { - QVERIFY(pos < test.length() - 4); - QString hex = test.mid(pos, 4); - bool ok = true; - testString.append(QChar(hex.toInt(&ok, 16))); - QVERIFY(ok); - pos += 4; - } - ++strPos; - } +QT_BEGIN_NAMESPACE +namespace QTest { - QTextBoundaryFinder finder(QTextBoundaryFinder::Grapheme, testString); - for (int i = 0; i < breakPositions.size(); ++i) { - QCOMPARE(finder.position(), breakPositions.at(i)); - finder.toNextBoundary(); - } - QCOMPARE(finder.toNextBoundary(), -1); +template<> +inline char *toString(const QTextBoundaryFinder::BoundaryReasons &flags) +{ + return qstrdup(QByteArray::number(int(flags)).constData()); +} - for (int i = 0; i < testString.length(); ++i) { - finder.setPosition(i); - QCOMPARE(finder.isAtBoundary(), breakPositions.contains(i) == true); - } +template<> +inline char *toString(const QList &list) +{ + QByteArray s; + for (QList::const_iterator it = list.constBegin(); it != list.constEnd(); ++it) { + if (!s.isEmpty()) + s += ", "; + s += QByteArray::number(*it); } - QCOMPARE(lines, 100); // to see it actually found the file and all. + s = "{ " + s + " }"; + return qstrdup(s.constData()); } -void tst_QTextBoundaryFinder::wordBoundaries() +} // namespace QTest +QT_END_NAMESPACE + +static void generateDataFromFile(const QString &fname) { - QFile file("data/WordBreakTest.txt"); - file.open(QFile::ReadOnly); + QTest::addColumn("testString"); + QTest::addColumn >("expectedBreakPositions"); + + QString testFile = QFINDTESTDATA(fname); + QVERIFY2(!testFile.isEmpty(), (fname.toLatin1() + QByteArray(" not found!"))); + QFile f(testFile); + QVERIFY(f.exists()); - while (!file.atEnd()) { - QByteArray line = file.readLine(); + f.open(QIODevice::ReadOnly); + + int linenum = 0; + while (!f.atEnd()) { + linenum++; + + QByteArray line = f.readLine(); if (line.startsWith('#')) continue; QString test = QString::fromUtf8(line); + QString comments; int hash = test.indexOf('#'); - if (hash > 0) + if (hash > 0) { + comments = test.mid(hash + 1).simplified(); test = test.left(hash); - test = test.simplified(); - test = test.replace(QLatin1String(" "), QString()); + } - QList breakPositions; QString testString; - int pos = 0; - int strPos = 0; - while (pos < test.length()) { - if (test.at(pos).unicode() == 0xf7) - breakPositions.append(strPos); - else - QVERIFY(test.at(pos).unicode() == 0xd7); - ++pos; - if (pos < test.length()) { - QVERIFY(pos < test.length() - 4); - QString hex = test.mid(pos, 4); - bool ok = true; - testString.append(QChar(hex.toInt(&ok, 16))); - QVERIFY(ok); - pos += 4; + QList expectedBreakPositions; + foreach (const QString &part, test.simplified().split(QLatin1Char(' '), QString::SkipEmptyParts)) { + if (part.size() == 1) { + if (part.at(0).unicode() == 0xf7) + expectedBreakPositions.append(testString.size()); + else + QVERIFY(part.at(0).unicode() == 0xd7); + continue; } - ++strPos; + bool ok = true; + uint ucs4 = part.toInt(&ok, 16); + QVERIFY(ok && ucs4 > 0); + QVERIFY(!QChar::requiresSurrogates(ucs4)); + testString.append(QChar(ucs4)); } - - QTextBoundaryFinder finder(QTextBoundaryFinder::Word, testString); - for (int i = 0; i < breakPositions.size(); ++i) { - QCOMPARE(finder.position(), breakPositions.at(i)); - finder.toNextBoundary(); + QVERIFY(!testString.isEmpty()); + QVERIFY(!expectedBreakPositions.isEmpty()); + + if (!comments.isEmpty()) { + const QStringList lst = comments.simplified().split(QLatin1Char(' '), QString::SkipEmptyParts); + comments.clear(); + foreach (const QString &part, lst) { + if (part.size() == 1) { + if (part.at(0).unicode() == 0xf7) + comments += QLatin1Char('+'); + else if (part.at(0).unicode() == 0xd7) + comments += QLatin1Char('x'); + continue; + } + if (part.startsWith(QLatin1Char('(')) && part.endsWith(QLatin1Char(')'))) + comments += part; + } } - QCOMPARE(finder.toNextBoundary(), -1); - for (int i = 0; i < testString.length(); ++i) { - finder.setPosition(i); - QCOMPARE(finder.isAtBoundary(), breakPositions.contains(i) == true); - } + QString nm = QString("line #%1: %2").arg(linenum).arg(comments); + QTest::newRow(nm.toLatin1()) << testString << expectedBreakPositions; } } -void tst_QTextBoundaryFinder::sentenceBoundaries() -{ - QFile file("data/SentenceBreakTest.txt"); - file.open(QFile::ReadOnly); +#ifdef QT_BUILD_INTERNAL +QT_BEGIN_NAMESPACE +extern Q_AUTOTEST_EXPORT int qt_initcharattributes_default_algorithm_only; +QT_END_NAMESPACE +#endif - while (!file.atEnd()) { - QByteArray line = file.readLine(); - if (line.startsWith('#')) - continue; +static void doTestData(const QString &testString, const QList &expectedBreakPositions, + QTextBoundaryFinder::BoundaryType type, bool default_algorithm_only = false) +{ +#ifdef QT_BUILD_INTERNAL + QScopedValueRollback default_algorithm(qt_initcharattributes_default_algorithm_only); + if (default_algorithm_only) + qt_initcharattributes_default_algorithm_only++; +#else + Q_UNUSED(default_algorithm_only) +#endif - QString test = QString::fromUtf8(line); - int hash = test.indexOf('#'); - if (hash > 0) - test = test.left(hash); - test = test.simplified(); - test = test.replace(QLatin1String(" "), QString()); + QTextBoundaryFinder boundaryFinder(type, testString); - QList breakPositions; - QString testString; - int pos = 0; - int strPos = 0; - while (pos < test.length()) { - if (test.at(pos).unicode() == 0xf7) - breakPositions.append(strPos); - else - QVERIFY(test.at(pos).unicode() == 0xd7); - ++pos; - if (pos < test.length()) { - QVERIFY(pos < test.length() - 4); - QString hex = test.mid(pos, 4); - bool ok = true; - testString.append(QChar(hex.toInt(&ok, 16))); - QVERIFY(ok); - pos += 4; - } - ++strPos; + // test toNextBoundary() + { + QList actualBreakPositions; + if (boundaryFinder.isAtBoundary()) + actualBreakPositions.append(boundaryFinder.position()); + while (boundaryFinder.toNextBoundary() != -1) { + QVERIFY(boundaryFinder.isAtBoundary()); + actualBreakPositions.append(boundaryFinder.position()); } + QCOMPARE(actualBreakPositions, expectedBreakPositions); + } - QTextBoundaryFinder finder(QTextBoundaryFinder::Sentence, testString); - for (int i = 0; i < breakPositions.size(); ++i) { - QCOMPARE(finder.position(), breakPositions.at(i)); - finder.toNextBoundary(); + // test toPreviousBoundary() + { + QList expectedBreakPositionsRev = expectedBreakPositions; + qSort(expectedBreakPositionsRev.begin(), expectedBreakPositionsRev.end(), qGreater()); + + QList actualBreakPositions; + boundaryFinder.toEnd(); + if (boundaryFinder.isAtBoundary()) + actualBreakPositions.append(boundaryFinder.position()); + while (boundaryFinder.toPreviousBoundary() != -1) { + QVERIFY(boundaryFinder.isAtBoundary()); + actualBreakPositions.append(boundaryFinder.position()); } - QCOMPARE(finder.toNextBoundary(), -1); + QCOMPARE(actualBreakPositions, expectedBreakPositionsRev); + } - for (int i = 0; i < testString.length(); ++i) { - finder.setPosition(i); - QCOMPARE(finder.isAtBoundary(), breakPositions.contains(i) == true); - } + // test isAtBoundary() + for (int i = 0; i < testString.length(); ++i) { + boundaryFinder.setPosition(i); + QCOMPARE(boundaryFinder.isAtBoundary(), expectedBreakPositions.contains(i)); } } -void tst_QTextBoundaryFinder::isAtWordStart() +#ifdef QT_BUILD_INTERNAL +void tst_QTextBoundaryFinder::graphemeBoundariesDefault_data() { - QString txt("The quick brown fox jumped over $the lazy. dog I win!"); - QList start, end; - start << 0 << 4 << 10 << 16 << 20 << 27 << 32 << 33 << 37 << 41 << 43 << 48 << 50 << 53; - end << 3 << 9 << 15 << 19 << 26 << 31 << 33 << 36 << 41 << 42 << 46 << 49 << 53 << 54; - QTextBoundaryFinder finder(QTextBoundaryFinder::Word, txt); - for(int i=0; i < txt.length(); ++i) { - finder.setPosition(i); - QTextBoundaryFinder::BoundaryReasons r = finder.boundaryReasons(); - QCOMPARE((r & QTextBoundaryFinder::StartWord) != 0, start.contains(i) == true); - QCOMPARE((r & QTextBoundaryFinder::EndWord) != 0, end.contains(i) == true); - } + generateDataFromFile("data/GraphemeBreakTest.txt"); } -void tst_QTextBoundaryFinder::fastConstructor() +void tst_QTextBoundaryFinder::graphemeBoundariesDefault() { - QString text("Hello World"); - QTextBoundaryFinder finder(QTextBoundaryFinder::Word, text.constData(), text.length(), /*buffer*/0, /*buffer size*/0); - QVERIFY(finder.boundaryReasons() == QTextBoundaryFinder::StartWord); + QFETCH(QString, testString); + QFETCH(QList, expectedBreakPositions); - finder.toNextBoundary(); - QCOMPARE(finder.position(), 5); - QVERIFY(finder.boundaryReasons() == QTextBoundaryFinder::EndWord); + doTestData(testString, expectedBreakPositions, QTextBoundaryFinder::Grapheme, true); +} - finder.toNextBoundary(); - QCOMPARE(finder.position(), 6); - QVERIFY(finder.boundaryReasons() == QTextBoundaryFinder::StartWord); +void tst_QTextBoundaryFinder::wordBoundariesDefault_data() +{ + generateDataFromFile("data/WordBreakTest.txt"); +} - finder.toNextBoundary(); - QCOMPARE(finder.position(), text.length()); - QVERIFY(finder.boundaryReasons() == QTextBoundaryFinder::EndWord); +void tst_QTextBoundaryFinder::wordBoundariesDefault() +{ + QFETCH(QString, testString); + QFETCH(QList, expectedBreakPositions); - finder.toNextBoundary(); - QVERIFY(finder.boundaryReasons() == QTextBoundaryFinder::NotAtBoundary); - QCOMPARE(finder.position(), -1); + doTestData(testString, expectedBreakPositions, QTextBoundaryFinder::Word, true); } -void tst_QTextBoundaryFinder::isAtBoundaryLine() +void tst_QTextBoundaryFinder::sentenceBoundariesDefault_data() { - // idx 0 1 2 3 4 5 6 - // break? - - - - + - + - QChar s[] = { 0x0061, 0x00AD, 0x0062, 0x0009, 0x0063, 0x0064 }; - QString text(s, sizeof(s)/sizeof(s[0])); - QTextBoundaryFinder finder(QTextBoundaryFinder::Line, text.constData(), text.length(), /*buffer*/0, /*buffer size*/0); - finder.setPosition(0); - QVERIFY(finder.isAtBoundary()); - finder.setPosition(1); - QVERIFY(!finder.isAtBoundary()); - finder.setPosition(2); - QVERIFY(!finder.isAtBoundary()); - finder.setPosition(3); - QVERIFY(!finder.isAtBoundary()); - finder.setPosition(4); - QVERIFY(finder.isAtBoundary()); - finder.setPosition(5); - QVERIFY(!finder.isAtBoundary()); - finder.setPosition(6); - QVERIFY(finder.isAtBoundary()); + generateDataFromFile("data/SentenceBreakTest.txt"); } -Q_DECLARE_METATYPE(QList) +void tst_QTextBoundaryFinder::sentenceBoundariesDefault() +{ + QFETCH(QString, testString); + QFETCH(QList, expectedBreakPositions); -void tst_QTextBoundaryFinder::toNextBoundary_data() + doTestData(testString, expectedBreakPositions, QTextBoundaryFinder::Sentence, true); +} +#endif // QT_BUILD_INTERNAL + +void tst_QTextBoundaryFinder::wordBoundaries_manual_data() { - QTest::addColumn("text"); - QTest::addColumn("type"); - QTest::addColumn< QList >("boundaries"); - - QList boundaries; - boundaries << 0 << 3 << 4 << 7 << 8 << 11 << 12 << 13 << 16 << 17 << 20 << 21 << 24 << 25; - QTest::newRow("data1") << QString("Aaa bbb ccc. Ddd eee fff.") << int(QTextBoundaryFinder::Word) \ - << boundaries; - - boundaries.clear(); - boundaries << 0 << 13 << 25; - QTest::newRow("data2") << QString("Aaa bbb ccc. Ddd eee fff.") << int(QTextBoundaryFinder::Sentence) \ - << boundaries; - - boundaries.clear(); - boundaries << 0 << 4 << 8 << 13 << 17 << 21 << 25; - QTest::newRow("data3") << QString("Aaa bbb ccc. Ddd eee fff.") << int(QTextBoundaryFinder::Line) \ - << boundaries; - - boundaries.clear(); - boundaries << 0 << 5 << 9 << 15 << 17 << 21 << 28; - QTest::newRow("data4") << QString::fromUtf8("Diga-nos qualé a sua opinião") << int(QTextBoundaryFinder::Line) - << boundaries; + QTest::addColumn("testString"); + QTest::addColumn >("expectedBreakPositions"); + + { + QChar s[] = { 0x000D, 0x000A, 0x000A }; + QString testString(s, sizeof(s)/sizeof(s[0])); + QList expectedBreakPositions; + expectedBreakPositions << 0 << 2 << 3; + + QTest::newRow("+CRxLF+LF+") << testString << expectedBreakPositions; + } + { + QChar s[] = { 0x000D, 0x0308, 0x000A, 0x000A }; + QString testString(s, sizeof(s)/sizeof(s[0])); + QList expectedBreakPositions; + expectedBreakPositions << 0 << 1 << 2 << 3 << 4; + + QTest::newRow("+CR+FE+LF+LF+") << testString << expectedBreakPositions; + } + { + QString testString(QString::fromUtf8("Aaa bbb ccc.\r\nDdd eee fff.")); + QList expectedBreakPositions; + expectedBreakPositions << 0 << 3 << 4 << 7 << 8 << 11 << 12 << 14 << 17 << 18 << 21 << 22 << 25 << 26; + QTest::newRow("data1") << testString << expectedBreakPositions; + } + + // Sample Strings from WordBreakTest.html + { + QChar s[] = { 0x0063, 0x0061, 0x006E, 0x0027, 0x0074 }; + QString testString(s, sizeof(s)/sizeof(s[0])); + QList expectedBreakPositions; + expectedBreakPositions << 0 << 5; + + QTest::newRow("ts 1") << testString << expectedBreakPositions; + } + { + QChar s[] = { 0x0063, 0x0061, 0x006E, 0x2019, 0x0074 }; + QString testString(s, sizeof(s)/sizeof(s[0])); + QList expectedBreakPositions; + expectedBreakPositions << 0 << 5; + + QTest::newRow("ts 2") << testString << expectedBreakPositions; + } + { + QChar s[] = { 0x0061, 0x0062, 0x00AD, 0x0062, 0x0061 }; + QString testString(s, sizeof(s)/sizeof(s[0])); + QList expectedBreakPositions; + expectedBreakPositions << 0 << 5; + + QTest::newRow("ts 3") << testString << expectedBreakPositions; + } + { + QChar s[] = { 0x0061, 0x0024, 0x002D, 0x0033, 0x0034, 0x002C, 0x0035, 0x0036, + 0x0037, 0x002E, 0x0031, 0x0034, 0x0025, 0x0062 }; + QString testString(s, sizeof(s)/sizeof(s[0])); + QList expectedBreakPositions; + expectedBreakPositions << 0 << 1 << 2 << 3 << 12 << 13 << 14; + + QTest::newRow("ts 4") << testString << expectedBreakPositions; + } + { + QChar s[] = { 0x0033, 0x0061 }; + QString testString(s, sizeof(s)/sizeof(s[0])); + QList expectedBreakPositions; + expectedBreakPositions << 0 << 2; + + QTest::newRow("ts 5") << testString << expectedBreakPositions; + } + { + QChar s[] = { 0x2060, 0x0063, 0x2060, 0x0061, 0x2060, 0x006E, 0x2060, 0x0027, + 0x2060, 0x0074, 0x2060, 0x2060 }; + QString testString(s, sizeof(s)/sizeof(s[0])); + QList expectedBreakPositions; + expectedBreakPositions << 0 << 1 << 12; + + QTest::newRow("ts 1e") << testString << expectedBreakPositions; + } + { + QChar s[] = { 0x2060, 0x0063, 0x2060, 0x0061, 0x2060, 0x006E, 0x2060, 0x2019, + 0x2060, 0x0074, 0x2060, 0x2060 }; + QString testString(s, sizeof(s)/sizeof(s[0])); + QList expectedBreakPositions; + expectedBreakPositions << 0 << 1 << 12; + + QTest::newRow("ts 2e") << testString << expectedBreakPositions; + } + { + QChar s[] = { 0x2060, 0x0061, 0x2060, 0x0062, 0x2060, 0x00AD, 0x2060, 0x0062, + 0x2060, 0x0061, 0x2060, 0x2060 }; + QString testString(s, sizeof(s)/sizeof(s[0])); + QList expectedBreakPositions; + expectedBreakPositions << 0 << 1 << 12; + + QTest::newRow("ts 3e") << testString << expectedBreakPositions; + } + { + QChar s[] = { 0x2060, 0x0061, 0x2060, 0x0024, 0x2060, 0x002D, 0x2060, 0x0033, + 0x2060, 0x0034, 0x2060, 0x002C, 0x2060, 0x0035, 0x2060, 0x0036, + 0x2060, 0x0037, 0x2060, 0x002E, 0x2060, 0x0031, 0x2060, 0x0034, + 0x2060, 0x0025, 0x2060, 0x0062, 0x2060, 0x2060 }; + QString testString(s, sizeof(s)/sizeof(s[0])); + QList expectedBreakPositions; + expectedBreakPositions << 0 << 1 << 3 << 5 << 7 << 25 << 27 << 30; + + QTest::newRow("ts 4e") << testString << expectedBreakPositions; + } + { + QChar s[] = { 0x2060, 0x0033, 0x2060, 0x0061, 0x2060, 0x2060 }; + QString testString(s, sizeof(s)/sizeof(s[0])); + QList expectedBreakPositions; + expectedBreakPositions << 0 << 1 << 6; + + QTest::newRow("ts 5e") << testString << expectedBreakPositions; + } } -void tst_QTextBoundaryFinder::toNextBoundary() +void tst_QTextBoundaryFinder::wordBoundaries_manual() { - QFETCH(QString, text); - QFETCH(int, type); - QFETCH(QList, boundaries); - - QList foundBoundaries; - QTextBoundaryFinder boundaryFinder(QTextBoundaryFinder::BoundaryType(type), text); - boundaryFinder.toStart(); - for(int next = 0; next != -1; next = boundaryFinder.toNextBoundary()) - foundBoundaries << next; - QCOMPARE(boundaries, foundBoundaries); + QFETCH(QString, testString); + QFETCH(QList, expectedBreakPositions); + + doTestData(testString, expectedBreakPositions, QTextBoundaryFinder::Word); +} + +void tst_QTextBoundaryFinder::sentenceBoundaries_manual_data() +{ + QTest::addColumn("testString"); + QTest::addColumn >("expectedBreakPositions"); + + { + QChar s[] = { 0x000D, 0x000A, 0x000A }; + QString testString(s, sizeof(s)/sizeof(s[0])); + QList expectedBreakPositions; + expectedBreakPositions << 0 << 2 << 3; + + QTest::newRow("+CRxLF+LF+") << testString << expectedBreakPositions; + } + { + QChar s[] = { 0x000D, 0x0308, 0x000A, 0x000A }; + QString testString(s, sizeof(s)/sizeof(s[0])); + QList expectedBreakPositions; + expectedBreakPositions << 0 << 1 << 3 << 4; + + QTest::newRow("+CR+FExLF+LF+") << testString << expectedBreakPositions; + } + { + QString testString(QString::fromUtf8("Aaa bbb ccc.\r\nDdd eee fff.")); + QList expectedBreakPositions; + expectedBreakPositions << 0 << 14 << 26; + + QTest::newRow("data1") << testString << expectedBreakPositions; + } + { + QString testString(QString::fromUtf8("Diga-nos qualé a sua opinião")); + QList expectedBreakPositions; + expectedBreakPositions << 0 << 28; + + QTest::newRow("data2") << testString << expectedBreakPositions; + } } -void tst_QTextBoundaryFinder::toPreviousBoundary_data() +void tst_QTextBoundaryFinder::sentenceBoundaries_manual() { - QTest::addColumn("text"); - QTest::addColumn("type"); - QTest::addColumn< QList >("boundaries"); - - QList boundaries; - boundaries << 25 << 24 << 21 << 20 << 17 << 16 << 13 << 12 << 11 << 8 << 7 << 4 << 3 << 0; - QTest::newRow("data1") << QString("Aaa bbb ccc. Ddd eee fff.") << int(QTextBoundaryFinder::Word) - << boundaries; - - boundaries.clear(); - boundaries << 25 << 13 << 0; - QTest::newRow("data2") << QString("Aaa bbb ccc. Ddd eee fff.") << int(QTextBoundaryFinder::Sentence) - << boundaries; - - boundaries.clear(); - boundaries << 25 << 21 << 17 << 13 << 8 << 4 << 0; - QTest::newRow("data3") << QString("Aaa bbb ccc. Ddd eee fff.") << int(QTextBoundaryFinder::Line) - << boundaries; - - boundaries.clear(); - boundaries << 28 << 21 << 17 << 15 << 9 << 5 << 0; - QTest::newRow("data4") << QString::fromUtf8("Diga-nos qualé a sua opinião") << int(QTextBoundaryFinder::Line) - << boundaries; + QFETCH(QString, testString); + QFETCH(QList, expectedBreakPositions); + doTestData(testString, expectedBreakPositions, QTextBoundaryFinder::Sentence); } -void tst_QTextBoundaryFinder::toPreviousBoundary() +void tst_QTextBoundaryFinder::lineBoundaries_manual_data() { - QFETCH(QString, text); - QFETCH(int, type); - QFETCH(QList, boundaries); - - QList foundBoundaries; - QTextBoundaryFinder boundaryFinder(QTextBoundaryFinder::BoundaryType(type), text); - boundaryFinder.toEnd(); - for (int previous = boundaryFinder.position(); - previous != -1; - previous = boundaryFinder.toPreviousBoundary()) + QTest::addColumn("testString"); + QTest::addColumn >("expectedBreakPositions"); + + { + QString testString(QString::fromUtf8("Aaa bbb ccc.\r\nDdd eee fff.")); + QList expectedBreakPositions; + expectedBreakPositions << 0 << 4 << 8 << 14 << 18 << 22 << 26; + + QTest::newRow("data1") << testString << expectedBreakPositions; + } + { + QString testString(QString::fromUtf8("Diga-nos qualé a sua opinião")); + QList expectedBreakPositions; + expectedBreakPositions << 0 << 5 << 9 << 15 << 17 << 21 << 28; + + QTest::newRow("data2") << testString << expectedBreakPositions; + } + + { + QChar s[] = { 0x000A, 0x2E80, 0x0308, 0x0023, 0x0023 }; + QString testString(s, sizeof(s)/sizeof(QChar)); + QList expectedBreakPositions; + expectedBreakPositions << 0 << 1 << 3 << 5; + + QTest::newRow("x(LF)+(ID)x(CM)+(AL)x(AL)+") << testString << expectedBreakPositions; + } { - foundBoundaries << previous; + QChar s[] = { 0x000A, 0x0308, 0x0023, 0x0023 }; + QString testString(s, sizeof(s)/sizeof(QChar)); + QList expectedBreakPositions; + expectedBreakPositions << 0 << 1 << 4; + + QTest::newRow("x(LF)+(CM)x(AL)x(AL)+") << testString << expectedBreakPositions; } - QCOMPARE(boundaries, foundBoundaries); + + { + QChar s[] = { 0x0061, 0x00AD, 0x0062, 0x0009, 0x0063, 0x0064 }; + QString testString(s, sizeof(s)/sizeof(s[0])); + QList expectedBreakPositions; + expectedBreakPositions << 0 << 4 << 6; + + QTest::newRow("x(AL)x(BA)+(AL)x(BA)+(AL)x(AL)+") << testString << expectedBreakPositions; + } +} + +void tst_QTextBoundaryFinder::lineBoundaries_manual() +{ + QFETCH(QString, testString); + QFETCH(QList, expectedBreakPositions); + + doTestData(testString, expectedBreakPositions, QTextBoundaryFinder::Line); +} + +void tst_QTextBoundaryFinder::fastConstructor() +{ + QString text("Hello World"); + QTextBoundaryFinder finder(QTextBoundaryFinder::Word, text.constData(), text.length(), /*buffer*/0, /*buffer size*/0); + QCOMPARE(finder.boundaryReasons(), QTextBoundaryFinder::StartWord); + + finder.toNextBoundary(); + QCOMPARE(finder.position(), 5); + QCOMPARE(finder.boundaryReasons(), QTextBoundaryFinder::EndWord); + + finder.toNextBoundary(); + QCOMPARE(finder.position(), 6); + QCOMPARE(finder.boundaryReasons(), QTextBoundaryFinder::StartWord); + + finder.toNextBoundary(); + QCOMPARE(finder.position(), text.length()); + QCOMPARE(finder.boundaryReasons(), QTextBoundaryFinder::EndWord); + + finder.toNextBoundary(); + QCOMPARE(finder.position(), -1); + QCOMPARE(finder.boundaryReasons(), QTextBoundaryFinder::NotAtBoundary); } #include