1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
/****************************************************************************
**
** 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$
**
****************************************************************************/
#include <QtTest/QtTest>
#include <QCalendar>
Q_DECLARE_METATYPE(QCalendar::System)
class tst_QCalendar : public QObject
{
Q_OBJECT
private:
void checkYear(const QCalendar &cal, int year, bool normal=false);
private slots:
void basic_data();
void basic();
void unspecified_data() { basic_data(); }
void unspecified();
void nameCase();
void specific_data();
void specific();
void daily_data() { basic_data(); }
void daily();
void properties_data();
void properties();
void aliases();
};
// Support for basic():
void tst_QCalendar::checkYear(const QCalendar &cal, int year, bool normal)
{
const int moons = cal.monthsInYear(year);
// Months are numbered from 1 to moons:
QVERIFY(moons > 0);
QVERIFY(!cal.isDateValid(year, moons + 1, 1));
QVERIFY(!cal.isDateValid(year, 0, 1));
QVERIFY(!QDate(year, 0, 1, cal).isValid());
QVERIFY(moons <= cal.maximumMonthsInYear());
QCOMPARE(cal.standaloneMonthName(QLocale::c(), moons + 1, year), QString());
QCOMPARE(cal.monthName(QLocale::c(), 0, year), QString());
const int days = cal.daysInYear(year);
QVERIFY(days > 0);
int sum = 0;
const int longest = cal.maximumDaysInMonth();
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:
QVERIFY(last > 0);
QVERIFY(last <= longest);
// Days are numbered from 1 to last:
QVERIFY(cal.isDateValid(year, i, 1));
QVERIFY(cal.isDateValid(year, i, last));
QVERIFY(!cal.isDateValid(year, i, 0));
QVERIFY(!cal.isDateValid(year, i, last + 1));
if (normal) // Unspecified year gets same daysInMonth():
QCOMPARE(cal.daysInMonth(i), last);
}
// Months add up to the whole year:
QCOMPARE(sum, days);
}
#define CHECKYEAR(cal, year) checkYear(cal, year); \
if (QTest::currentTestFailed()) \
return
#define NORMALYEAR(cal, year) checkYear(cal, year, true); \
if (QTest::currentTestFailed()) \
return
void tst_QCalendar::basic_data()
{
QTest::addColumn<QCalendar::System>("system");
QMetaEnum e = QCalendar::staticMetaObject.enumerator(0);
Q_ASSERT(qstrcmp(e.name(), "System") == 0);
for (int i = 0; i <= int(QCalendar::System::Last); ++i) {
// There may be gaps in the enum's numbering; and Last is a duplicate:
if (e.value(i) != -1 && qstrcmp(e.key(i), "Last"))
QTest::newRow(e.key(i)) << QCalendar::System(e.value(i));
}
}
void tst_QCalendar::basic()
{
QFETCH(QCalendar::System, system);
QCalendar cal(system);
QVERIFY(cal.isValid());
QCOMPARE(QCalendar(cal.name()).isGregorian(), cal.isGregorian());
QCOMPARE(QCalendar(cal.name()).name(), cal.name());
if (cal.hasYearZero()) {
CHECKYEAR(cal, 0);
} else {
QCOMPARE(cal.monthsInYear(0), 0);
QCOMPARE(cal.daysInYear(0), 0);
QVERIFY(!cal.isDateValid(0, 1, 1));
QVERIFY(!QDate(0, 1, 1, cal).isValid());
}
if (cal.isProleptic()) {
CHECKYEAR(cal, -1);
} else {
QCOMPARE(cal.monthsInYear(-1), 0);
QCOMPARE(cal.daysInYear(-1), 0);
QVERIFY(!cal.isDateValid(-1, 1, 1));
}
// Look for a leap year in the last decade.
int year = QDate::currentDate().year(cal);
for (int i = 10; i > 0 && !cal.isLeapYear(year); --i)
--year;
if (cal.isLeapYear(year)) {
// ... and a non-leap year within a decade before it.
int leap = year--;
for (int i = 10; i > 0 && cal.isLeapYear(year); --i)
year--;
if (!cal.isLeapYear(year))
QVERIFY(cal.daysInYear(year) < cal.daysInYear(leap));
CHECKYEAR(cal, leap);
}
// Either year is non-leap or we have a decade of leap years together;
// expect daysInMonth() to treat year the same as unspecified.
NORMALYEAR(cal, year);
}
void tst_QCalendar::unspecified()
{
QFETCH(QCalendar::System, system);
QCalendar cal(system);
const QDate today = QDate::currentDate();
const int thisYear = today.year();
QCOMPARE(cal.monthsInYear(QCalendar::Unspecified), cal.maximumMonthsInYear());
for (int month = cal.maximumMonthsInYear(); month > 0; month--) {
const int days = cal.daysInMonth(month);
int count = 0;
// 19 years = one Metonic cycle (used by some lunar calendars)
for (int i = 19; i > 0; --i) {
if (cal.daysInMonth(month, thisYear - i) == days)
count++;
}
// Require a majority of the years tested:
QVERIFY2(count > 9, "Default daysInMonth() should be for a normal year");
}
}
void tst_QCalendar::nameCase()
{
QVERIFY(QCalendar::availableCalendars().contains(QStringLiteral("Gregorian")));
}
void tst_QCalendar::specific_data()
{
QTest::addColumn<QCalendar::System>("system");
// Date in that system:
QTest::addColumn<QString>("monthName");
QTest::addColumn<int>("sysyear");
QTest::addColumn<int>("sysmonth");
QTest::addColumn<int>("sysday");
// Gregorian equivalent:
QTest::addColumn<int>("gregyear");
QTest::addColumn<int>("gregmonth");
QTest::addColumn<int>("gregday");
#define ADDROW(cal, monthName, year, month, day, gy, gm, gd) \
QTest::newRow(#cal) << QCalendar::System::cal << QStringLiteral(monthName) \
<< year << month << day << gy << gm << gd
ADDROW(Gregorian, "January", 1970, 1, 1, 1970, 1, 1);
// One known specific date, for each calendar
#ifndef QT_BOOTSTRAPPED
// Julian 1582-10-4 was followed by Gregorian 1582-10-15
ADDROW(Julian, "October", 1582, 10, 4, 1582, 10, 14);
// Milankovic matches Gregorian for a few centuries
ADDROW(Milankovic, "March", 1923, 3, 20, 1923, 3, 20);
#endif
#if QT_CONFIG(jalalicalendar)
// Jalali year 1355 started on Gregorian 1976-3-21:
ADDROW(Jalali, "Farvardin", 1355, 1, 1, 1976, 3, 21);
#endif // jalali
#if QT_CONFIG(islamiccivilcalendar)
// TODO: confirm this is correct
ADDROW(IslamicCivil, "Muharram", 1, 1, 1, 622, 7, 19);
#endif
#undef ADDROW
}
void tst_QCalendar::specific()
{
QFETCH(QCalendar::System, system);
QFETCH(const QString, monthName);
QFETCH(int, sysyear);
QFETCH(int, sysmonth);
QFETCH(int, sysday);
QFETCH(int, gregyear);
QFETCH(int, gregmonth);
QFETCH(int, gregday);
const QCalendar cal(system);
QCOMPARE(cal.monthName(QLocale::c(), sysmonth), monthName);
const QDate date(sysyear, sysmonth, sysday, cal), gregory(gregyear, gregmonth, gregday);
QCOMPARE(date, gregory);
QCOMPARE(gregory.year(cal), sysyear);
QCOMPARE(gregory.month(cal), sysmonth);
QCOMPARE(gregory.day(cal), sysday);
QCOMPARE(date.year(), gregyear);
QCOMPARE(date.month(), gregmonth);
QCOMPARE(date.day(), gregday);
}
void tst_QCalendar::daily()
{
QFETCH(QCalendar::System, system);
QCalendar calendar(system);
const quint64 startJDN = 0, endJDN = 2488070;
// Iterate from -4713-01-01 (Julian calendar) to 2100-01-01
for (quint64 expect = startJDN; expect <= endJDN; ++expect)
{
QDate date = QDate::fromJulianDay(expect);
auto parts = calendar.partsFromDate(date);
if (!parts.isValid())
continue;
const int year = date.year(calendar);
QCOMPARE(year, parts.year);
const int month = date.month(calendar);
QCOMPARE(month, parts.month);
const int day = date.day(calendar);
QCOMPARE(day, parts.day);
const quint64 actual = QDate(year, month, day, calendar).toJulianDay();
QCOMPARE(actual, expect);
}
}
void tst_QCalendar::properties_data()
{
QTest::addColumn<QCalendar::System>("system");
QTest::addColumn<bool>("gregory");
QTest::addColumn<bool>("lunar");
QTest::addColumn<bool>("luniSolar");
QTest::addColumn<bool>("solar");
QTest::addColumn<bool>("proleptic");
QTest::addColumn<bool>("yearZero");
QTest::addColumn<int>("monthMax");
QTest::addColumn<int>("monthMin");
QTest::addColumn<int>("yearMax");
QTest::addColumn<QString>("name");
QTest::addRow("Gregorian")
<< QCalendar::System::Gregorian << true << false << false << true << true << false
<< 31 << 28 << 12 << QStringLiteral("Gregorian");
#ifndef QT_BOOTSTRAPPED
QTest::addRow("Julian")
<< QCalendar::System::Julian << false << false << false << true << true << false
<< 31 << 28 << 12 << QStringLiteral("Julian");
QTest::addRow("Milankovic")
<< QCalendar::System::Milankovic << false << false << false << true << true << false
<< 31 << 28 << 12 << QStringLiteral("Milankovic");
#endif
#if QT_CONFIG(jalalicalendar)
QTest::addRow("Jalali")
<< QCalendar::System::Jalali << false << false << false << true << true << false
<< 31 << 29 << 12 << QStringLiteral("Jalali");
#endif
#if QT_CONFIG(islamiccivilcalendar)
QTest::addRow("IslamicCivil")
<< QCalendar::System::IslamicCivil << false << true << false << false << true << false
<< 30 << 29 << 12 << QStringLiteral("Islamic Civil");
#endif
}
void tst_QCalendar::properties()
{
QFETCH(const QCalendar::System, system);
QFETCH(const bool, gregory);
QFETCH(const bool, lunar);
QFETCH(const bool, luniSolar);
QFETCH(const bool, solar);
QFETCH(const bool, proleptic);
QFETCH(const bool, yearZero);
QFETCH(const int, monthMax);
QFETCH(const int, monthMin);
QFETCH(const int, yearMax);
QFETCH(const QString, name);
const QCalendar cal(system);
QCOMPARE(cal.isGregorian(), gregory);
QCOMPARE(cal.isLunar(), lunar);
QCOMPARE(cal.isLuniSolar(), luniSolar);
QCOMPARE(cal.isSolar(), solar);
QCOMPARE(cal.isProleptic(), proleptic);
QCOMPARE(cal.hasYearZero(), yearZero);
QCOMPARE(cal.maximumDaysInMonth(), monthMax);
QCOMPARE(cal.minimumDaysInMonth(), monthMin);
QCOMPARE(cal.maximumMonthsInYear(), yearMax);
QCOMPARE(cal.name(), name);
}
void tst_QCalendar::aliases()
{
QCOMPARE(QCalendar(u"gregory").name(), u"Gregorian");
#if QT_CONFIG(jalalicalendar)
QCOMPARE(QCalendar(u"Persian").name(), u"Jalali");
#endif
#if QT_CONFIG(islamiccivilcalendar)
// Exercise all constructors from name, while we're at it:
QCOMPARE(QCalendar(u"islamic-civil").name(), u"Islamic Civil");
QCOMPARE(QCalendar(QLatin1String("islamic")).name(), u"Islamic Civil");
QCOMPARE(QCalendar(QStringLiteral("Islamic")).name(), u"Islamic Civil");
#endif
// Invalid is handled gracefully:
QCOMPARE(QCalendar(u"").name(), QString());
QCOMPARE(QCalendar(QCalendar::System::User).name(), QString());
}
QTEST_APPLESS_MAIN(tst_QCalendar)
#include "tst_qcalendar.moc"
|