summaryrefslogtreecommitdiff
path: root/tests/unit/qmap/test_livecq_parser.cpp
blob: e3fc7f23007a38389d2801eec40bba6ca444366e (plain)
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
#include <QtTest>

#include "qmap/livecq_parser.h"

class TestQMapLiveCQParser final : public QObject
{
  Q_OBJECT

private slots:
  void parsesSupportedLayouts()
  {
    QMapLiveCQ::Record record;
    QVERIFY(QMapLiveCQ::parse(
      {"123456", "-10", "0.1", "1234", "-12", "60", "CQ", "N5L", "1200.0"},
      100, record));
    QCOMPARE(record.callsign, QString {"N5L"});
    QCOMPARE(record.grid, QString {"--"});
    QCOMPARE(record.message, QString {"CQ N5L"});
    QCOMPARE(record.receiveFrequency, 100);
    QCOMPARE(record.scheduledFrequency, 1200);

    QVERIFY(QMapLiveCQ::parse(
      {"123456", "-10", "0.1", "1234", "-12", "60", "CQ", "N5L", "FN20", "1200.0"},
      100, record));
    QCOMPARE(record.grid, QString {"FN20"});
    QCOMPARE(record.message, QString {"CQ N5L FN20"});

    QVERIFY(QMapLiveCQ::parse(
      {"123456", "-10", "0.1", "1234", "-12", "60", "CQ", "DX", "N5L", "1200.0"},
      100, record));
    QCOMPARE(record.callsign, QString {"N5L"});
    QCOMPARE(record.grid, QString {"--"});
    QCOMPARE(record.message, QString {"CQ N5L"});

    QVERIFY(QMapLiveCQ::parse(
      {"123456", "-10", "0.1", "1234", "-12", "60", "CQ", "DX", "N5L", "FN20", "1200.0"},
      100, record));
    QCOMPARE(record.grid, QString {"FN20"});
    QCOMPARE(record.message, QString {"CQ N5L FN20"});
  }

  void parsesHighFrequencyFraction()
  {
    QMapLiveCQ::Record record;
    QVERIFY(QMapLiveCQ::parse(
      {"123456", "-10", "0.1", "1234", "-12", "60", "CQ", "N5L", "1200.750"},
      100, record));
    QCOMPARE(record.receiveFrequency, -150);
    QCOMPARE(record.scheduledFrequency, 1201);
  }

  void rejectsMalformedRecordsBeforeIndexing()
  {
    QMapLiveCQ::Record record;
    for (auto const& tokens : {
           QStringList {},
           QStringList {"123456", "-10", "0.1", "1234", "-12", "60", "CQ"},
           QStringList {"123456", "-10", "0.1", "1234", "-12", "60", "CQ", "N5L"},
           QStringList {"123456", "-10", "0.1", "1234", "-12", "60", "CQ", "N5L", "FN20", "1200.0", "extra", "too-much"}}) {
      QVERIFY(!QMapLiveCQ::parse(tokens, 100, record));
    }
  }

  void rejectsMalformedFrequencyAndCalls()
  {
    QMapLiveCQ::Record record;
    for (auto const& frequency : {QString {"1200"}, QString {"1200."},
                                  QString {"1200.x"}, QString {"1200.0.1"}}) {
      QVERIFY(!QMapLiveCQ::parse(
        {"123456", "-10", "0.1", "1234", "-12", "60", "CQ", "N5L", frequency},
        100, record));
    }
    QVERIFY(!QMapLiveCQ::parse(
      {"123456", "-10", "0.1", "1234", "-12", "60", "CQ", "N5", "1200.0"},
      100, record));
    QVERIFY(!QMapLiveCQ::parse(
      {"123456", "-10", "0.1", "1234", "-12", "60", "CQ", "DX", "N5", "1200.0"},
      100, record));
  }

  void spotTimeUsesTodaysUtcDate()
  {
    QDateTime const now {QDate {2026, 9, 30}, QTime {18, 7, 10}, Qt::UTC};
    QCOMPARE(QMapLiveCQ::spotTime("180630", now),
             QDateTime(QDate {2026, 9, 30}, QTime {18, 6, 30}, Qt::UTC));
    QCOMPARE(QMapLiveCQ::spotTime("1806", now),
             QDateTime(QDate {2026, 9, 30}, QTime {18, 6}, Qt::UTC));
    QCOMPARE(QMapLiveCQ::spotTime("180630", now).toString("yyyy-MM-ddTHH:mm:ss") + "Z",
             QString {"2026-09-30T18:06:30Z"});
  }

  void spotTimeIsIndependentOfLocale()
  {
    QDateTime const now {QDate {2026, 9, 30}, QTime {18, 7, 10}, Qt::UTC};
    auto const previous = QLocale {};
    for (auto const name : {"cs_CZ", "es_AR", "hu_HU", "fr_FR", "de_DE", "zh_TW", "ru_RU"}) {
      QLocale::setDefault(QLocale {name});
      QCOMPARE(QMapLiveCQ::spotTime("180630", now).toString("yyyy-MM-ddTHH:mm:ss") + "Z",
               QString {"2026-09-30T18:06:30Z"});
    }
    QLocale::setDefault(previous);
  }

  void spotTimeAcrossMidnight()
  {
    // Early decode of the last period before midnight: still the same day.
    QCOMPARE(QMapLiveCQ::spotTime("235930", QDateTime(QDate {2026, 9, 30}, QTime {23, 59, 52}, Qt::UTC)),
             QDateTime(QDate {2026, 9, 30}, QTime {23, 59, 30}, Qt::UTC));
    // Decoded just after midnight: the period began yesterday.
    QCOMPARE(QMapLiveCQ::spotTime("235900", QDateTime(QDate {2026, 10, 1}, QTime {0, 0, 5}, Qt::UTC)),
             QDateTime(QDate {2026, 9, 30}, QTime {23, 59}, Qt::UTC));
    QCOMPARE(QMapLiveCQ::spotTime("235930", QDateTime(QDate {2026, 10, 1}, QTime {0, 0, 22}, Qt::UTC)),
             QDateTime(QDate {2026, 9, 30}, QTime {23, 59, 30}, Qt::UTC));
    // First period of the new day.
    QCOMPARE(QMapLiveCQ::spotTime("000000", QDateTime(QDate {2026, 10, 1}, QTime {0, 0, 52}, Qt::UTC)),
             QDateTime(QDate {2026, 10, 1}, QTime {0, 0}, Qt::UTC));
    // Clock a few seconds behind the decoder: not pushed back a day.
    QCOMPARE(QMapLiveCQ::spotTime("120000", QDateTime(QDate {2026, 9, 30}, QTime {11, 59, 58}, Qt::UTC)),
             QDateTime(QDate {2026, 9, 30}, QTime {12, 0}, Qt::UTC));
  }

  void spotTimeRejectsMalformedTimes()
  {
    QDateTime const now {QDate {2026, 9, 30}, QTime {18, 7, 10}, Qt::UTC};
    for (auto const& decodeTime : {QString {}, QString {"123"}, QString {"1234567"},
                                   QString {"2460"}, QString {"1260"}, QString {"126099"},
                                   QString {"12x4"}, QString {"12:34"}}) {
      QVERIFY(!QMapLiveCQ::spotTime(decodeTime, now).isValid());
    }
    QVERIFY(!QMapLiveCQ::spotTime("123456", QDateTime {}).isValid());
  }
};

QTEST_APPLESS_MAIN(TestQMapLiveCQParser)

#include "test_livecq_parser.moc"