summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2021-11-11 14:55:13 +0100
committerEdward Welbourne <edward.welbourne@qt.io>2021-11-15 20:00:21 +0100
commitf3e26891061f26a42d009ec6395f82975019039d (patch)
tree675a1ad3e7f111ea9bfcbe4be1beb27de4611875 /tests/auto/corelib
parente6364f9c9c827573c02893adf71c17c6f1a538d6 (diff)
Revise (recently-added) yearSharingWeekDays() to take a QDate
This lets it avoid a two-digit year that would clash with month or day. That shall make fixing up system locale date formatting run cleaner. Add a test for QGregorianCalendar's two extensions. Change-Id: I77083ff9d5e4035763250904a59fcf416286545b Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests/auto/corelib')
-rw-r--r--tests/auto/corelib/time/qcalendar/CMakeLists.txt2
-rw-r--r--tests/auto/corelib/time/qcalendar/tst_qcalendar.cpp48
2 files changed, 50 insertions, 0 deletions
diff --git a/tests/auto/corelib/time/qcalendar/CMakeLists.txt b/tests/auto/corelib/time/qcalendar/CMakeLists.txt
index 4a8ab7f846..685039523d 100644
--- a/tests/auto/corelib/time/qcalendar/CMakeLists.txt
+++ b/tests/auto/corelib/time/qcalendar/CMakeLists.txt
@@ -7,4 +7,6 @@
qt_internal_add_test(tst_qcalendar
SOURCES
tst_qcalendar.cpp
+ LIBRARIES
+ Qt::CorePrivate
)
diff --git a/tests/auto/corelib/time/qcalendar/tst_qcalendar.cpp b/tests/auto/corelib/time/qcalendar/tst_qcalendar.cpp
index b0da158775..0bb41fd730 100644
--- a/tests/auto/corelib/time/qcalendar/tst_qcalendar.cpp
+++ b/tests/auto/corelib/time/qcalendar/tst_qcalendar.cpp
@@ -29,6 +29,7 @@
#include <QTest>
#include <QCalendar>
+#include <private/qgregoriancalendar_p.h>
Q_DECLARE_METATYPE(QCalendar::System)
class tst_QCalendar : public QObject
@@ -51,6 +52,8 @@ private slots:
void properties_data();
void properties();
void aliases();
+
+ void gregory();
};
// Support for basic():
@@ -380,5 +383,50 @@ void tst_QCalendar::aliases()
QCOMPARE(QCalendar(QCalendar::System::User).name(), QString());
}
+void tst_QCalendar::gregory()
+{
+ // Test QGregorianCalendar's internal-use methods.
+
+ // Julian day number 0 is in 4713; and reach past the end of four-digit years:
+ for (int year = -4720; year < 12345; ++year) {
+ // Test yearStartWeekDay() and yearSharingWeekDays() are consistent with
+ // dateToJulianDay() and weekDayOfJulian():
+ if (!year) // No year zero.
+ continue;
+ qint64 first, last;
+ QVERIFY2(QGregorianCalendar::julianFromParts(year, 1, 1, &first),
+ "Only year zero should lack a first day");
+ QCOMPARE(QGregorianCalendar::yearStartWeekDay(year),
+ QGregorianCalendar::weekDayOfJulian(first));
+ QVERIFY2(QGregorianCalendar::julianFromParts(year, 12, 31, &last),
+ "Only year zero should lack a last day");
+
+ const int lastTwo = (year + (year < 0 ? 1 : 0)) % 100 + (year < -1 ? 100 : 0);
+ const QDate probe(year, lastTwo && lastTwo <= 12 ? lastTwo : 8,
+ lastTwo <= 31 && lastTwo > 12 ? lastTwo : 17);
+ const int match = QGregorianCalendar::yearSharingWeekDays(probe);
+ // A post-epoch year, no later than 2400 (implies four-digit):
+ QVERIFY(match >= 1970);
+ QVERIFY(match <= 2400);
+ // Either that's the year we started with or:
+ if (match != year) {
+ // Its last two digits can't be mistaken for month or day:
+ QVERIFY(match % 100 != probe.month());
+ QVERIFY(match % 100 != probe.day());
+ // If that wasn't in danger of happening, with year positive, they match lastTwo:
+ if (year > 0 && lastTwo > 31)
+ QCOMPARE(match % 100, lastTwo);
+ // Its first and last days of the year match those of year:
+ qint64 day;
+ QVERIFY(QGregorianCalendar::julianFromParts(match, 1, 1, &day));
+ QCOMPARE(QGregorianCalendar::weekDayOfJulian(day),
+ QGregorianCalendar::weekDayOfJulian(first));
+ QVERIFY(QGregorianCalendar::julianFromParts(match, 12, 31, &day));
+ QCOMPARE(QGregorianCalendar::weekDayOfJulian(day),
+ QGregorianCalendar::weekDayOfJulian(last));
+ }
+ }
+}
+
QTEST_APPLESS_MAIN(tst_QCalendar)
#include "tst_qcalendar.moc"