File indexing completed on 2024-05-19 11:37:30

0001 /*
0002     This file is part of the kholidays library.
0003 
0004     SPDX-FileCopyrightText: 2004, 2007, 2009 Allen Winter <winter@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "testlunar.h"
0010 
0011 #include <QDebug>
0012 #include <QTest>
0013 
0014 QTEST_MAIN(LunarTest)
0015 
0016 #include "kholidays/lunarphase.h"
0017 using namespace KHolidays;
0018 
0019 void LunarTest::test2005()
0020 {
0021     QList<QDate> fQ2005;
0022     QList<QDate> fM2005;
0023     QList<QDate> lQ2005;
0024     QList<QDate> nM2005;
0025     // 2005 first quarter moons
0026     fQ2005.append(QDate(2005, 1, 17));
0027     fQ2005.append(QDate(2005, 2, 16));
0028     fQ2005.append(QDate(2005, 3, 17));
0029     fQ2005.append(QDate(2005, 4, 16));
0030     fQ2005.append(QDate(2005, 5, 16));
0031     fQ2005.append(QDate(2005, 6, 15));
0032     fQ2005.append(QDate(2005, 7, 14));
0033     fQ2005.append(QDate(2005, 8, 13));
0034     fQ2005.append(QDate(2005, 9, 11));
0035     fQ2005.append(QDate(2005, 10, 10));
0036     fQ2005.append(QDate(2005, 11, 9));
0037     fQ2005.append(QDate(2005, 12, 8));
0038 
0039     // 2005 full moons
0040     fM2005.append(QDate(2005, 1, 25));
0041     fM2005.append(QDate(2005, 2, 24));
0042     fM2005.append(QDate(2005, 3, 25));
0043     fM2005.append(QDate(2005, 4, 24));
0044     fM2005.append(QDate(2005, 5, 23));
0045     fM2005.append(QDate(2005, 6, 22));
0046     fM2005.append(QDate(2005, 7, 21));
0047     fM2005.append(QDate(2005, 8, 19));
0048     fM2005.append(QDate(2005, 9, 18));
0049     fM2005.append(QDate(2005, 10, 17));
0050     fM2005.append(QDate(2005, 11, 16));
0051     fM2005.append(QDate(2005, 12, 15));
0052 
0053     // 2005 last quarter moons
0054     lQ2005.append(QDate(2005, 1, 3));
0055     lQ2005.append(QDate(2005, 2, 2));
0056     lQ2005.append(QDate(2005, 3, 3));
0057     lQ2005.append(QDate(2005, 4, 2));
0058     lQ2005.append(QDate(2005, 5, 1));
0059     lQ2005.append(QDate(2005, 5, 30));
0060     lQ2005.append(QDate(2005, 6, 28));
0061     lQ2005.append(QDate(2005, 7, 28));
0062     lQ2005.append(QDate(2005, 8, 26));
0063     lQ2005.append(QDate(2005, 9, 25));
0064     lQ2005.append(QDate(2005, 10, 25));
0065     lQ2005.append(QDate(2005, 11, 23));
0066     lQ2005.append(QDate(2005, 12, 23));
0067 
0068     // 2005 new moons
0069     nM2005.append(QDate(2005, 1, 10));
0070     nM2005.append(QDate(2005, 2, 8));
0071     nM2005.append(QDate(2005, 3, 10));
0072     nM2005.append(QDate(2005, 4, 8));
0073     nM2005.append(QDate(2005, 5, 8));
0074     nM2005.append(QDate(2005, 6, 6));
0075     nM2005.append(QDate(2005, 7, 6));
0076     nM2005.append(QDate(2005, 8, 5));
0077     nM2005.append(QDate(2005, 9, 3));
0078     nM2005.append(QDate(2005, 10, 3));
0079     nM2005.append(QDate(2005, 11, 2));
0080     nM2005.append(QDate(2005, 12, 1));
0081     nM2005.append(QDate(2005, 12, 31));
0082 
0083     LunarPhase lp;
0084     QDate qd(2004, 12, 31);
0085     QList<QDate> firstQuarter;
0086     QList<QDate> fullMoon;
0087     QList<QDate> lastQuarter;
0088     QList<QDate> newMoon;
0089     for (int i = 1; i <= 365; ++i) {
0090         qd = qd.addDays(1);
0091         LunarPhase::Phase phase = lp.phaseAtDate(qd);
0092         if (phase == LunarPhase::FirstQuarter) {
0093             firstQuarter.append(qd);
0094             qDebug() << "first quarter" << qd;
0095         } else if (phase == LunarPhase::FullMoon) {
0096             fullMoon.append(qd);
0097             qDebug() << "full moon" << qd;
0098         } else if (phase == LunarPhase::LastQuarter) {
0099             lastQuarter.append(qd);
0100             qDebug() << "last quarter" << qd;
0101         } else if (phase == LunarPhase::NewMoon) {
0102             newMoon.append(qd);
0103             qDebug() << "new moon" << qd;
0104         }
0105     }
0106     QVERIFY(fQ2005 == firstQuarter);
0107     QVERIFY(fM2005 == fullMoon);
0108     QVERIFY(lQ2005 == lastQuarter);
0109     QVERIFY(nM2005 == newMoon);
0110 }
0111 
0112 void LunarTest::test2007()
0113 {
0114     QList<QDate> fQ2007;
0115     QList<QDate> fM2007;
0116     QList<QDate> lQ2007;
0117     QList<QDate> nM2007;
0118     // 2007 first quarter moons
0119     fQ2007.append(QDate(2007, 1, 25));
0120     fQ2007.append(QDate(2007, 2, 24));
0121     fQ2007.append(QDate(2007, 3, 25));
0122     fQ2007.append(QDate(2007, 4, 24));
0123     fQ2007.append(QDate(2007, 5, 23));
0124     fQ2007.append(QDate(2007, 6, 22));
0125     fQ2007.append(QDate(2007, 7, 22));
0126     fQ2007.append(QDate(2007, 8, 20));
0127     fQ2007.append(QDate(2007, 9, 19));
0128     fQ2007.append(QDate(2007, 10, 19));
0129     fQ2007.append(QDate(2007, 11, 17));
0130     fQ2007.append(QDate(2007, 12, 17));
0131 
0132     // 2007 full moons
0133     fM2007.append(QDate(2007, 1, 3));
0134     fM2007.append(QDate(2007, 2, 2));
0135     fM2007.append(QDate(2007, 3, 3));
0136     fM2007.append(QDate(2007, 4, 2));
0137     fM2007.append(QDate(2007, 5, 2));
0138     fM2007.append(QDate(2007, 6, 1));
0139     fM2007.append(QDate(2007, 6, 30));
0140     fM2007.append(QDate(2007, 7, 30));
0141     fM2007.append(QDate(2007, 8, 28));
0142     fM2007.append(QDate(2007, 9, 26));
0143     fM2007.append(QDate(2007, 10, 26));
0144     fM2007.append(QDate(2007, 11, 24));
0145     fM2007.append(QDate(2007, 12, 24));
0146 
0147     // 2007 last quarter moons
0148     lQ2007.append(QDate(2007, 1, 11));
0149     lQ2007.append(QDate(2007, 2, 10));
0150     lQ2007.append(QDate(2007, 3, 12));
0151     lQ2007.append(QDate(2007, 4, 10));
0152     lQ2007.append(QDate(2007, 5, 10));
0153     lQ2007.append(QDate(2007, 6, 8));
0154     lQ2007.append(QDate(2007, 7, 7));
0155     lQ2007.append(QDate(2007, 8, 5));
0156     lQ2007.append(QDate(2007, 9, 4));
0157     lQ2007.append(QDate(2007, 10, 3));
0158     lQ2007.append(QDate(2007, 11, 1));
0159     lQ2007.append(QDate(2007, 12, 1));
0160     lQ2007.append(QDate(2007, 12, 31));
0161 
0162     // 2007 new moons
0163     nM2007.append(QDate(2007, 1, 19));
0164     nM2007.append(QDate(2007, 2, 17));
0165     nM2007.append(QDate(2007, 3, 19));
0166     nM2007.append(QDate(2007, 4, 17));
0167     nM2007.append(QDate(2007, 5, 16));
0168     nM2007.append(QDate(2007, 6, 15));
0169     nM2007.append(QDate(2007, 7, 14));
0170     nM2007.append(QDate(2007, 8, 12));
0171     nM2007.append(QDate(2007, 9, 11));
0172     nM2007.append(QDate(2007, 10, 11));
0173     nM2007.append(QDate(2007, 11, 9));
0174     nM2007.append(QDate(2007, 12, 9));
0175 
0176     LunarPhase lp;
0177     QDate qd(2006, 12, 31);
0178     QList<QDate> firstQuarter;
0179     QList<QDate> fullMoon;
0180     QList<QDate> lastQuarter;
0181     QList<QDate> newMoon;
0182     for (int i = 1; i <= 365; ++i) {
0183         qd = qd.addDays(1);
0184         LunarPhase::Phase phase = lp.phaseAtDate(qd);
0185         if (phase == LunarPhase::FirstQuarter) {
0186             firstQuarter.append(qd);
0187             qDebug() << "first quarter" << qd;
0188         } else if (phase == LunarPhase::FullMoon) {
0189             fullMoon.append(qd);
0190             qDebug() << "full moon" << qd;
0191         } else if (phase == LunarPhase::LastQuarter) {
0192             lastQuarter.append(qd);
0193             qDebug() << "last quarter" << qd;
0194         } else if (phase == LunarPhase::NewMoon) {
0195             newMoon.append(qd);
0196             qDebug() << "new moon" << qd;
0197         }
0198     }
0199     QVERIFY(fQ2007 == firstQuarter);
0200     QVERIFY(fM2007 == fullMoon);
0201     QVERIFY(lQ2007 == lastQuarter);
0202     QVERIFY(nM2007 == newMoon);
0203 }
0204 
0205 void LunarTest::testIntermediatePhases()
0206 {
0207     struct {
0208         QDate date;
0209         LunarPhase::Phase phase;
0210     } const phase_changes[] = {
0211         {{2022, 1, 2}, LunarPhase::NewMoon},
0212         {{2022, 1, 3}, LunarPhase::WaxingCrescent},
0213         {{2022, 1, 9}, LunarPhase::FirstQuarter},
0214         {{2022, 1, 10}, LunarPhase::WaxingGibbous},
0215         {{2022, 1, 17}, LunarPhase::FullMoon},
0216         {{2022, 1, 18}, LunarPhase::WaningGibbous},
0217         {{2022, 1, 25}, LunarPhase::LastQuarter},
0218         {{2022, 1, 26}, LunarPhase::WaningCrescent},
0219         {{2022, 2, 1}, LunarPhase::NewMoon},
0220         {{2022, 2, 2}, LunarPhase::WaxingCrescent},
0221         {{2022, 2, 8}, LunarPhase::FirstQuarter},
0222         {{2022, 2, 9}, LunarPhase::WaxingGibbous},
0223         {{2022, 2, 16}, LunarPhase::FullMoon},
0224         {{2022, 2, 17}, LunarPhase::WaningGibbous},
0225         {{2022, 2, 23}, LunarPhase::LastQuarter},
0226         {{2022, 2, 24}, LunarPhase::WaningCrescent},
0227         {{2022, 3, 2}, LunarPhase::NewMoon},
0228     };
0229 
0230     QDate dt(2022, 1, 1);
0231     LunarPhase::Phase phase = LunarPhase::WaningCrescent;
0232     for (const auto &phaseChange : phase_changes) {
0233         for (auto i = phaseChange.date.daysTo(dt); i > 0; --i) {
0234             QCOMPARE(LunarPhase::phaseAtDate(phaseChange.date.addDays(i)), phase);
0235         }
0236         dt = phaseChange.date;
0237         phase = phaseChange.phase;
0238         QCOMPARE(LunarPhase::phaseAtDate(dt), phase);
0239         QVERIFY(!LunarPhase::phaseNameAtDate(dt).isEmpty());
0240     }
0241 }
0242 
0243 #include "moc_testlunar.cpp"