QCollator: extend tests

- Add tests for QCollatorSortKey
- Add test for QCollator copy assignment and copy construct
Currently QCollatorSortKey tests are working properly only
with QT_CONFIG(icu)

Task-number: QTBUG-88546
Change-Id: Ic35dfd33038cc736245904b78fe4383a5a11b580
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Ivan Solovev 2020-11-20 11:36:29 +01:00
parent 0440614af0
commit 3cacf1d1bd

View File

@ -39,6 +39,7 @@ class tst_QCollator : public QObject
Q_OBJECT
private Q_SLOTS:
void basics();
void moveSemantics();
void compare_data();
@ -58,6 +59,37 @@ static bool dpointer_is_null(QCollator &c)
return true;
}
void tst_QCollator::basics()
{
const QLocale de_AT(QLocale::German, QLocale::Austria);
QCollator c1(de_AT);
QCOMPARE(c1.locale(), de_AT);
QCollator c2(c1);
QCOMPARE(c2.locale(), de_AT);
QCollator c3;
// Test copy assignment
c3 = c2;
QCOMPARE(c3.locale(), de_AT);
// posix implementation supports only C and default locale,
// so update it for Android build
#if defined(Q_OS_ANDROID) && !defined(Q_OS_ANDROID_EMBEDDED)
c3.setLocale(QLocale());
#endif
QCollatorSortKey key1 = c3.sortKey("test");
QCollatorSortKey key2(key1);
QCOMPARE(key1.compare(key2), 0);
QCollatorSortKey key3 = c3.sortKey("abc");
// Test copy assignment
key3 = key2;
QCOMPARE(key1.compare(key3), 0);
}
void tst_QCollator::moveSemantics()
{
const QLocale de_AT(QLocale::German, QLocale::Austria);
@ -75,6 +107,21 @@ void tst_QCollator::moveSemantics()
c1 = std::move(c2);
QCOMPARE(c1.locale(), de_AT);
QVERIFY(dpointer_is_null(c2));
// test QCollatorSortKey move assignment
// posix implementation supports only C and default locale,
// so update it for Android build
#if defined(Q_OS_ANDROID) && !defined(Q_OS_ANDROID_EMBEDDED)
c1.setLocale(QLocale());
#endif
QCollatorSortKey key1 = c1.sortKey("1");
QCollatorSortKey key2 = c1.sortKey("2");
QVERIFY(key1.compare(key2) < 0);
QCollatorSortKey key3 = c1.sortKey("a");
// test move assignment
key3 = std::move(key2);
QVERIFY(key1.compare(key3) < 0);
}
@ -107,6 +154,12 @@ void tst_QCollator::compare_data()
QTest::newRow("english8") << QString("en_US") << QString("test.19") << QString("test,19") << 1 << 1 << true << true << 0;
QTest::newRow("en-empty-word") << QString("en_US") << QString() << QString("non-empty") << -1 << -1 << false << true << -1;
QTest::newRow("en-empty-number") << QString("en_US") << QString() << QString("42") << -1 << -1 << true << true << -1;
QTest::newRow("en-word-empty") << QString("en_US") << QString("non-empty") << QString() << 1
<< 1 << false << true << 1;
QTest::newRow("en-number-empty")
<< QString("en_US") << QString("42") << QString() << 1 << 1 << true << true << 1;
QTest::newRow("en-empty-empty")
<< QString("en_US") << QString() << QString() << 0 << 0 << false << true << 0;
/*
In Swedish, a with ring above (E5) comes before a with
@ -123,7 +176,12 @@ void tst_QCollator::compare_data()
QTest::newRow("swedish8") << QString("sv_SE") << QString("test.19") << QString("test,19") << 1 << 1 << true << true << 0;
QTest::newRow("sv-empty-word") << QString("sv_SE") << QString() << QString("mett") << -1 << -1 << false << true << -1;
QTest::newRow("sv-empty-number") << QString("sv_SE") << QString() << QString("42") << -1 << -1 << true << true << -1;
QTest::newRow("sv-word-empty")
<< QString("sv_SE") << QString("mett") << QString() << 1 << 1 << false << true << 1;
QTest::newRow("sv-number-empty")
<< QString("sv_SE") << QString("42") << QString() << 1 << 1 << true << true << 1;
QTest::newRow("sv-empty-empty")
<< QString("sv_SE") << QString() << QString() << 0 << 0 << false << true << 0;
/*
In Norwegian, ae (E6) comes before o with stroke (D8), which
@ -139,6 +197,12 @@ void tst_QCollator::compare_data()
QTest::newRow("norwegian8") << QString("no_NO") << QString("test.19") << QString("test,19") << 1 << 1 << true << true << 0;
QTest::newRow("nb-empty-word") << QString("nb_NO") << QString() << QString("mett") << -1 << -1 << false << true << -1;
QTest::newRow("nb-empty-number") << QString("nb_NO") << QString() << QString("42") << -1 << -1 << true << true << -1;
QTest::newRow("nb-word-empty")
<< QString("nb_NO") << QString("mett") << QString() << 1 << 1 << false << true << 1;
QTest::newRow("nb-number-empty")
<< QString("nb_NO") << QString("42") << QString() << 1 << 1 << true << true << 1;
QTest::newRow("nb-empty-empty")
<< QString("nb_NO") << QString() << QString() << 0 << 0 << false << true << 0;
/*
In German, z comes *after* a with diaresis (E4),
@ -159,6 +223,12 @@ void tst_QCollator::compare_data()
QTest::newRow("german13") << QString("de_DE") << QString("test.19") << QString("test,19") << 1 << 1 << true << true << 0;
QTest::newRow("de-empty-word") << QString("de_DE") << QString() << QString("satt") << -1 << -1 << false << true << -1;
QTest::newRow("de-empty-number") << QString("de_DE") << QString() << QString("42") << -1 << -1 << true << true << -1;
QTest::newRow("de-word-empty")
<< QString("de_DE") << QString("satt") << QString() << 1 << 1 << false << true << 1;
QTest::newRow("de-number-empty")
<< QString("de_DE") << QString("42") << QString() << 1 << 1 << true << true << 1;
QTest::newRow("de-empty-empty")
<< QString("de_DE") << QString() << QString() << 0 << 0 << false << true << 0;
/*
French sorting of e and e with acute accent
@ -173,6 +243,12 @@ void tst_QCollator::compare_data()
QTest::newRow("french8") << QString("fr_FR") << QString("test.19") << QString("test,19") << 1 << 1 << true << true << 0;
QTest::newRow("fr-empty-word") << QString("fr_FR") << QString() << QString("plein") << -1 << -1 << false << true << -1;
QTest::newRow("fr-empty-number") << QString("fr_FR") << QString() << QString("42") << -1 << -1 << true << true << -1;
QTest::newRow("fr-word-empty")
<< QString("fr_FR") << QString("plein") << QString() << 1 << 1 << false << true << 1;
QTest::newRow("fr-number-empty")
<< QString("fr_FR") << QString("42") << QString() << 1 << 1 << true << true << 1;
QTest::newRow("fr-empty-empty")
<< QString("fr_FR") << QString() << QString() << 0 << 0 << false << true << 0;
// C locale: case sensitive [A-Z] < [a-z] but case insensitive [Aa] < [Bb] <...< [Zz]
const QString C = QStringLiteral("C");
@ -180,6 +256,12 @@ void tst_QCollator::compare_data()
QTest::newRow("C:AZa:aAZ") << C << QStringLiteral("AZa") << QStringLiteral("aAZ") << -1 << 1 << false << false << 1;
QTest::newRow("C-empty-word") << QString(C) << QString() << QString("non-empty") << -1 << -1 << false << true << -1;
QTest::newRow("C-empty-number") << QString(C) << QString() << QString("42") << -1 << -1 << true << true << -1;
QTest::newRow("C-word-empty") << QString(C) << QString("non-empty") << QString() << 1 << 1
<< false << true << 1;
QTest::newRow("C-number-empty")
<< QString(C) << QString("42") << QString() << 1 << 1 << true << true << 1;
QTest::newRow("C-empty-empty")
<< QString(C) << QString() << QString() << 0 << 0 << false << true << 0;
}
void tst_QCollator::compare()
@ -190,10 +272,8 @@ void tst_QCollator::compare()
QFETCH(int, result);
QFETCH(int, caseInsensitiveResult);
QFETCH(bool, numericMode);
#if !QT_CONFIG(icu)
QFETCH(bool, ignorePunctuation);
QFETCH(int, punctuationResult);
#endif
QCollator collator((QLocale(locale)));
// Need to canonicalize sign to -1, 0 or 1, as .compare() can produce any -ve for <, any +ve for >.
@ -209,12 +289,45 @@ void tst_QCollator::compare()
if (numericMode)
collator.setNumericMode(true);
int keyCompareResult = result;
int keyCompareCaseInsensitiveResult = caseInsensitiveResult;
int keyComparePunctuationResultResult = punctuationResult;
// trying to deal with special behavior of different OS-dependent collators
if (collator.locale() == QLocale("C")) {
#if !QT_CONFIG(icu) && defined(Q_OS_MACOS)
// for MACOS C-locale is not supported, always providing empty string for sortKey()
keyCompareResult = 0;
keyCompareCaseInsensitiveResult = 0;
keyComparePunctuationResultResult = 0;
#else
// for other platforms C-locale strings are not modified by sortKey() anyhow
keyCompareCaseInsensitiveResult = keyCompareResult;
keyComparePunctuationResultResult = keyCompareResult;
#endif
}
// NOTE: currently QCollatorSortKey::compare is not working
// properly without icu: see QTBUG-88704 for details
QCOMPARE(asSign(collator.compare(s1, s2)), result);
#if QT_CONFIG(icu)
auto key1 = collator.sortKey(s1);
auto key2 = collator.sortKey(s2);
QCOMPARE(asSign(key1.compare(key2)), keyCompareResult);
#endif
collator.setCaseSensitivity(Qt::CaseInsensitive);
QCOMPARE(asSign(collator.compare(s1, s2)), caseInsensitiveResult);
#if !QT_CONFIG(icu)
#if QT_CONFIG(icu)
key1 = collator.sortKey(s1);
key2 = collator.sortKey(s2);
QCOMPARE(asSign(key1.compare(key2)), keyCompareCaseInsensitiveResult);
#endif
collator.setIgnorePunctuation(ignorePunctuation);
QCOMPARE(asSign(collator.compare(s1, s2)), punctuationResult);
#if QT_CONFIG(icu)
key1 = collator.sortKey(s1);
key2 = collator.sortKey(s2);
QCOMPARE(asSign(key1.compare(key2)), keyComparePunctuationResultResult);
#endif
}
@ -239,7 +352,6 @@ void tst_QCollator::state()
QCOMPARE(c.numericMode(), true);
QCOMPARE(c.ignorePunctuation(), true);
QCOMPARE(c.locale(), QLocale(QLocale::NorwegianBokmal));
}
QTEST_APPLESS_MAIN(tst_QCollator)