diff options
Diffstat (limited to 'tests/auto/corelib/time/qcalendar')
-rw-r--r-- | tests/auto/corelib/time/qcalendar/CMakeLists.txt | 9 | ||||
-rw-r--r-- | tests/auto/corelib/time/qcalendar/tst_qcalendar.cpp | 104 |
2 files changed, 71 insertions, 42 deletions
diff --git a/tests/auto/corelib/time/qcalendar/CMakeLists.txt b/tests/auto/corelib/time/qcalendar/CMakeLists.txt index 685039523d..5b1b2dfc2a 100644 --- a/tests/auto/corelib/time/qcalendar/CMakeLists.txt +++ b/tests/auto/corelib/time/qcalendar/CMakeLists.txt @@ -1,9 +1,16 @@ -# Generated from qcalendar.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause ##################################################################### ## tst_qcalendar Test: ##################################################################### +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qcalendar LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + qt_internal_add_test(tst_qcalendar SOURCES tst_qcalendar.cpp diff --git a/tests/auto/corelib/time/qcalendar/tst_qcalendar.cpp b/tests/auto/corelib/time/qcalendar/tst_qcalendar.cpp index 0bb41fd730..61999202d2 100644 --- a/tests/auto/corelib/time/qcalendar/tst_qcalendar.cpp +++ b/tests/auto/corelib/time/qcalendar/tst_qcalendar.cpp @@ -1,30 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2020 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2020 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QTest> @@ -56,6 +31,49 @@ private slots: void gregory(); }; +static void checkCenturyResolution(const QCalendar &cal, const QCalendar::YearMonthDay &base) +{ + quint8 weekDayMask = 0; + for (int offset = -7; offset < 8; ++offset) { + const auto probe = QDate(base.year, base.month, base.day, cal).addYears(100 * offset, cal); + const int dow = cal.dayOfWeek(probe); + if (probe.isValid() && dow > 0 && dow < 8) + weekDayMask |= 1 << quint8(dow - 1); + } + for (int j = 1; j < 8; ++j) { + const bool seen = weekDayMask & (1 << quint8(j - 1)); + const QDate check = cal.matchCenturyToWeekday(base, j); + if (check.isValid()) { + const auto parts = cal.partsFromDate(check); + const int dow = cal.dayOfWeek(check); + QCOMPARE(dow, j); + QCOMPARE(parts.day, base.day); + QCOMPARE(parts.month, base.month); + int gap = parts.year - base.year; + if (!cal.hasYearZero() && (parts.year > 0) != (base.year > 0)) + gap += parts.year > 0 ? -1 : +1; + auto report = qScopeGuard([parts, base]() { + qDebug("Wrongly matched year: %d replaced %d", parts.year, base.year); + }); + QCOMPARE(gap % 100, 0); + // We searched 7 centuries each side of base. + if (seen) { + QCOMPARE_LT(gap / 100, 8); + QCOMPARE_GT(gap / 100, -8); + } else { + QVERIFY(gap / 100 >= 8 || gap / 100 <= -8); + } + report.dismiss(); + } else { + auto report = qScopeGuard([j, base]() { + qDebug("Missed dow[%d] for %d/%d/%d", j, base.year, base.month, base.day); + }); + QVERIFY(!seen); + report.dismiss(); + } + } +} + // Support for basic(): void tst_QCalendar::checkYear(const QCalendar &cal, int year, bool normal) { @@ -74,7 +92,7 @@ void tst_QCalendar::checkYear(const QCalendar &cal, int year, bool normal) int sum = 0; const int longest = cal.maximumDaysInMonth(); - for (int i = moons; i > 0; i--) { + for (int i = moons; i > 0; --i) { const int last = cal.daysInMonth(i, year); sum += last; // Valid month has some days and no more than max: @@ -87,6 +105,10 @@ void tst_QCalendar::checkYear(const QCalendar &cal, int year, bool normal) QVERIFY(!cal.isDateValid(year, i, last + 1)); if (normal) // Unspecified year gets same daysInMonth(): QCOMPARE(cal.daysInMonth(i), last); + + checkCenturyResolution(cal, {year, i, (last + 1) / 2}); + if (QTest::currentTestFailed()) + return; } // Months add up to the whole year: QCOMPARE(sum, days); @@ -393,13 +415,12 @@ void tst_QCalendar::gregory() // 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"); + const auto first = QGregorianCalendar::julianFromParts(year, 1, 1); + QVERIFY2(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"); + QGregorianCalendar::weekDayOfJulian(*first)); + const auto last = QGregorianCalendar::julianFromParts(year, 12, 31); + QVERIFY2(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, @@ -417,13 +438,14 @@ void tst_QCalendar::gregory() 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)); + auto day = QGregorianCalendar::julianFromParts(match, 1, 1); + QVERIFY(day); + QCOMPARE(QGregorianCalendar::weekDayOfJulian(*day), + QGregorianCalendar::weekDayOfJulian(*first)); + day = QGregorianCalendar::julianFromParts(match, 12, 31); + QVERIFY(day); + QCOMPARE(QGregorianCalendar::weekDayOfJulian(*day), + QGregorianCalendar::weekDayOfJulian(*last)); } } } |