knowledgedbtest.cpp 12.4 KB
Newer Older
Volker Krause's avatar
Volker Krause committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
  Copyright (c) 2018 Volker Krause <vkrause@kde.org>

   This library is free software; you can redistribute it and/or modify it
   under the terms of the GNU Library General Public License as published by
   the Free Software Foundation; either version 2 of the License, or (at your
   option) any later version.

   This library is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
   License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to the
   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301, USA.
*/

20
#include <KItinerary/CountryDb>
21

22
#include <knowledgedb/alphaid.h>
23
#include <knowledgedb/trainstationdb.h>
Volker Krause's avatar
Volker Krause committed
24
25
26
27
28
29
30
31
32
33
34
35
36

#include <QDebug>
#include <QObject>
#include <QTest>
#include <QTimeZone>

using namespace KItinerary;
using namespace KItinerary::KnowledgeDb;

class KnowledgeDbTest : public QObject
{
    Q_OBJECT
private Q_SLOTS:
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
    void testUnalignedNumber()
    {
        constexpr UnalignedNumber<3> uic1(8001337);
        constexpr UnalignedNumber<3> uic2(8001330);
        static_assert(sizeof(uic1) == 3, "");
        static_assert(alignof(UnalignedNumber<3>) == 1, "");
        QVERIFY(!(uic1 < uic2));
        QVERIFY(uic2 < uic1);
        QVERIFY(uic1 != uic2);
        QVERIFY(!(uic1 == uic2));

        constexpr UnalignedNumber<3> uic3(9899776);
        constexpr UnalignedNumber<3> uic4(1000191);
        QVERIFY(!(uic3 < uic4));
        QVERIFY(uic4 < uic3);
        QVERIFY(uic3 != uic4);
        QVERIFY(!(uic3 == uic4));
        QCOMPARE(uic3.value(), 9899776);
        QCOMPARE(uic4.value(), 1000191);

        constexpr UnalignedNumber<3> uic[2] = { UnalignedNumber<3>(8301700), UnalignedNumber<3>(8301701) };
        static_assert(sizeof(uic) == 6, "");
        QVERIFY(uic[0] < uic[1]);
        QVERIFY(uic[0] == uic[0]);
        QVERIFY(uic[0] != uic[1]);
    }

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
   void testAlphaId()
   {
       using ID3 = AlphaId<uint16_t, 3>;
       constexpr ID3 id1{"ABC"};
       const ID3 id2(QStringLiteral("CBA"));
       static_assert(sizeof(id1) == 2, "");
       QVERIFY(id1.isValid());
       QVERIFY(id2.isValid());
       QVERIFY(id1 < id2);
       QVERIFY(!(id2 < id1));
       QVERIFY(id1 == id1);
       QVERIFY(id1 != id2);
       QVERIFY(!(id1 == id2));
       QVERIFY(!(id1 != id1));

       QCOMPARE(id1.toString(), QLatin1String("ABC"));
       QCOMPARE(id2.toString(), QLatin1String("CBA"));

       constexpr ID3 id3;
       QVERIFY(!id3.isValid());
       QVERIFY(id3.toString().isEmpty());

       qDebug() << id1;
    }

Volker Krause's avatar
Volker Krause committed
89
90
    void testIBNRLookup()
    {
91
        auto station = KnowledgeDb::stationForIbnr(IBNR{1234567});
Volker Krause's avatar
Volker Krause committed
92
        QVERIFY(!station.coordinate.isValid());
93
        QCOMPARE(toQTimeZone(station.timezone), QTimeZone());
Volker Krause's avatar
Volker Krause committed
94

95
        station = KnowledgeDb::stationForIbnr({});
Volker Krause's avatar
Volker Krause committed
96
        QVERIFY(!station.coordinate.isValid());
97
        QCOMPARE(toQTimeZone(station.timezone), QTimeZone());
Volker Krause's avatar
Volker Krause committed
98

99
        station = KnowledgeDb::stationForIbnr(IBNR{8011160});
Volker Krause's avatar
Volker Krause committed
100
        QVERIFY(station.coordinate.isValid());
101
        QCOMPARE(toQTimeZone(station.timezone), QTimeZone("Europe/Berlin"));
102
        QCOMPARE(station.country, CountryId{"DE"});
Volker Krause's avatar
Volker Krause committed
103

104
        station = KnowledgeDb::stationForIbnr(IBNR{8501687});
Volker Krause's avatar
Volker Krause committed
105
        QVERIFY(station.coordinate.isValid());
106
        QCOMPARE(toQTimeZone(station.timezone), QTimeZone("Europe/Zurich"));
107
        QCOMPARE(station.country, CountryId{"CH"});
108
109
110
111

        // Aachen West, very close to the NL border, should be in DE timezone
        station = KnowledgeDb::stationForIbnr(IBNR{8000404});
        QVERIFY(station.coordinate.isValid());
112
        QCOMPARE(toQTimeZone(station.timezone), QTimeZone("Europe/Berlin"));
113
        QCOMPARE(station.country, CountryId{"DE"});
Volker Krause's avatar
Volker Krause committed
114
115
    }

116
117
118
119
    void testUICLookup()
    {
        auto station = KnowledgeDb::stationForUic(UICStation{1234567});
        QVERIFY(!station.coordinate.isValid());
120
        QCOMPARE(toQTimeZone(station.timezone), QTimeZone());
121
122
123

        station = KnowledgeDb::stationForUic({});
        QVERIFY(!station.coordinate.isValid());
124
        QCOMPARE(toQTimeZone(station.timezone), QTimeZone());
125
126
127

        station = KnowledgeDb::stationForUic(UICStation{1001332});
        QVERIFY(station.coordinate.isValid());
128
        QCOMPARE(toQTimeZone(station.timezone), QTimeZone("Europe/Helsinki"));
129
130
131
        QCOMPARE(station.country, CountryId{"FI"});
    }

Volker Krause's avatar
Volker Krause committed
132
133
    void testGaresConnexionsIdLookup()
    {
134
        auto station = KnowledgeDb::stationForGaresConnexionsId({});
Volker Krause's avatar
Volker Krause committed
135
        QVERIFY(!station.coordinate.isValid());
136
        QCOMPARE(toQTimeZone(station.timezone), QTimeZone());
Volker Krause's avatar
Volker Krause committed
137

138
        station = KnowledgeDb::stationForGaresConnexionsId(GaresConnexionsId{"XXXXX"});
Volker Krause's avatar
Volker Krause committed
139
        QVERIFY(!station.coordinate.isValid());
140
        QCOMPARE(toQTimeZone(station.timezone), QTimeZone());
Volker Krause's avatar
Volker Krause committed
141

142
        station = KnowledgeDb::stationForGaresConnexionsId(GaresConnexionsId{"FRAES"});
Volker Krause's avatar
Volker Krause committed
143
        QVERIFY(station.coordinate.isValid());
144
        QCOMPARE(toQTimeZone(station.timezone), QTimeZone("Europe/Paris"));
145
        QCOMPARE(station.country, CountryId{"FR"});
Volker Krause's avatar
Volker Krause committed
146

147
        station = KnowledgeDb::stationForGaresConnexionsId(GaresConnexionsId{QStringLiteral("FRXYT")});
Volker Krause's avatar
Volker Krause committed
148
        QVERIFY(station.coordinate.isValid());
149
        QCOMPARE(toQTimeZone(station.timezone), QTimeZone("Europe/Paris"));
150
        QCOMPARE(station.country, CountryId{"FR"});
Volker Krause's avatar
Volker Krause committed
151

152
        station = KnowledgeDb::stationForGaresConnexionsId(GaresConnexionsId{"CHGVA"});
Volker Krause's avatar
Volker Krause committed
153
154
155
        QEXPECT_FAIL("", "Wikidata does not supply ids for non-French stations yet", Continue);
        QVERIFY(station.coordinate.isValid());
        QEXPECT_FAIL("", "Wikidata does not supply ids for non-French stations yet", Continue);
156
        QCOMPARE(toQTimeZone(station.timezone), QTimeZone("Europe/Zurich"));
157
158
        QEXPECT_FAIL("", "Wikidata does not supply ids for non-French stations yet", Continue);
        QCOMPARE(station.country, CountryId{"CH"});
Volker Krause's avatar
Volker Krause committed
159
    }
160
161
162
163
164

    void testCountryDb()
    {
        auto country = KnowledgeDb::countryForId(CountryId{});
        QCOMPARE(country.drivingSide, KnowledgeDb::DrivingSide::Unknown);
165
        QCOMPARE(country.powerPlugTypes, {Unknown});
166
167
168

        country = KnowledgeDb::countryForId(CountryId{"DE"});
        QCOMPARE(country.drivingSide, KnowledgeDb::DrivingSide::Right);
169
        QCOMPARE(country.powerPlugTypes, {TypeC|TypeF});
170
171
        country = KnowledgeDb::countryForId(CountryId{"GB"});
        QCOMPARE(country.drivingSide, KnowledgeDb::DrivingSide::Left);
172
        QCOMPARE(country.powerPlugTypes, {TypeG});
173
        country = KnowledgeDb::countryForId(CountryId{"GL"});
174
        QCOMPARE(country.drivingSide, KnowledgeDb::DrivingSide::Right);
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

    void testPowerPlugCompat_data()
    {
        using namespace KnowledgeDb;

        QTest::addColumn<PowerPlugTypes>("plugs");
        QTest::addColumn<PowerPlugTypes>("sockets");
        QTest::addColumn<PowerPlugTypes>("failPlugs");
        QTest::addColumn<PowerPlugTypes>("failSockets");

        QTest::newRow("empty") << PowerPlugTypes{} << PowerPlugTypes{} << PowerPlugTypes{} << PowerPlugTypes{};
        QTest::newRow("DE-DE") << PowerPlugTypes{TypeC|TypeF} << PowerPlugTypes{TypeC|TypeF} << PowerPlugTypes{} << PowerPlugTypes{};
        QTest::newRow("DE-CH") << PowerPlugTypes{TypeC|TypeF} << PowerPlugTypes{TypeC|TypeJ} << PowerPlugTypes{TypeF} << PowerPlugTypes{TypeJ};
        QTest::newRow("CH-DE") << PowerPlugTypes{TypeC|TypeJ} << PowerPlugTypes{TypeC|TypeF} << PowerPlugTypes{TypeJ} << PowerPlugTypes{TypeF};
        QTest::newRow("DE-FR") << PowerPlugTypes{TypeC|TypeF} << PowerPlugTypes{TypeC|TypeE} << PowerPlugTypes{} << PowerPlugTypes{};
        QTest::newRow("DE-GB") << PowerPlugTypes{TypeC|TypeF} << PowerPlugTypes{TypeG} << PowerPlugTypes{TypeC|TypeF} << PowerPlugTypes{TypeG};
        QTest::newRow("DE-IT") << PowerPlugTypes{TypeC|TypeF} << PowerPlugTypes{TypeC|TypeF|TypeL} << PowerPlugTypes{} << PowerPlugTypes{TypeL};
        QTest::newRow("IT-DE") << PowerPlugTypes{TypeC|TypeF|TypeL} << PowerPlugTypes{TypeC|TypeF} << PowerPlugTypes{TypeL} << PowerPlugTypes{};
        QTest::newRow("DE-IL") << PowerPlugTypes{TypeC|TypeF} << PowerPlugTypes{TypeC|TypeH|TypeM} << PowerPlugTypes{TypeF} << PowerPlugTypes{TypeH|TypeM};
        QTest::newRow("DE-AO") << PowerPlugTypes{TypeC|TypeF} << PowerPlugTypes{TypeC} << PowerPlugTypes{TypeF} << PowerPlugTypes{};
        QTest::newRow("AO-DE") << PowerPlugTypes{TypeC} << PowerPlugTypes{TypeC|TypeF} << PowerPlugTypes{} << PowerPlugTypes{};
        QTest::newRow("DE-DK") << PowerPlugTypes{TypeC|TypeF} << PowerPlugTypes{TypeC|TypeE|TypeF|TypeK} << PowerPlugTypes{} << PowerPlugTypes{};
        QTest::newRow("DK-DE") << PowerPlugTypes{TypeC|TypeF|TypeE|TypeK} << PowerPlugTypes{TypeC|TypeF} << PowerPlugTypes{TypeK} << PowerPlugTypes{};
        QTest::newRow("DE-ZA") << PowerPlugTypes{TypeC|TypeF} << PowerPlugTypes{TypeC|TypeD|TypeM|TypeN} << PowerPlugTypes{TypeF} << PowerPlugTypes{TypeD|TypeM|TypeN};
        QTest::newRow("ZA-CH") << PowerPlugTypes{TypeC|TypeD|TypeM|TypeN} << PowerPlugTypes{TypeC|TypeJ} << PowerPlugTypes{TypeD|TypeM|TypeN} << PowerPlugTypes{TypeJ};
        QTest::newRow("ZA-DE") << PowerPlugTypes{TypeC|TypeD|TypeM|TypeN} << PowerPlugTypes{TypeC|TypeF} << PowerPlugTypes{TypeD|TypeM|TypeN} << PowerPlugTypes{TypeF};
        QTest::newRow("ZA-IT") << PowerPlugTypes{TypeC|TypeD|TypeM|TypeN} << PowerPlugTypes{TypeC|TypeF|TypeL} << PowerPlugTypes{TypeD|TypeM|TypeN} << PowerPlugTypes{TypeF|TypeL};
    }

    void testPowerPlugCompat()
    {
        using namespace KnowledgeDb;

        QFETCH(PowerPlugTypes, plugs);
        QFETCH(PowerPlugTypes, sockets);
        QFETCH(PowerPlugTypes, failPlugs);
        QFETCH(PowerPlugTypes, failSockets);

        QCOMPARE(KnowledgeDb::incompatiblePowerPlugs(plugs, sockets), failPlugs);
        QCOMPARE(KnowledgeDb::incompatiblePowerSockets(plugs, sockets), failSockets);
    }
217
218
219
220
221

    void testTimezoneForCountry()
    {
        using namespace KnowledgeDb;

222
223
224
        QCOMPARE(toQTimeZone(timezoneForCountry(CountryId{"DE"})), QTimeZone("Europe/Berlin"));
        QCOMPARE(toQTimeZone(timezoneForCountry(CountryId{"FR"})), QTimeZone("Europe/Paris"));
        QCOMPARE(toQTimeZone(timezoneForCountry(CountryId{"BR"})), QTimeZone());
225
    }
226

227
228
229
230
231
232
233
    void testCountryForTimezone()
    {
        using namespace KnowledgeDb;

        QCOMPARE(countryForTimezone(Tz::Europe_Busingen), CountryId{"DE"});
        QCOMPARE(countryForTimezone(Tz::America_Los_Angeles), CountryId{"US"});
        QCOMPARE(countryForTimezone(Tz::Asia_Kuching), CountryId{"MY"});
234
235
        QCOMPARE(countryForTimezone(Tz::Asia_Bangkok), CountryId{});
        QCOMPARE(countryForTimezone(Tz::Asia_Ho_Chi_Minh), CountryId{"VN"});
236
237
    }

238
239
240
241
242
243
244
    void testUICCountryCodeLookup()
    {
        using namespace KnowledgeDb;

        QCOMPARE(KnowledgeDb::countryIdForUicCode(80), CountryId{"DE"});
        QCOMPARE(KnowledgeDb::countryIdForUicCode(0), CountryId{});
    }
245
246
247
248
249
250
251
252

    void testIso3Lookup()
    {
        using namespace KnowledgeDb;

        QCOMPARE(KnowledgeDb::countryIdFromIso3166_1alpha3(CountryId3{"ITA"}), CountryId{"IT"});
        QCOMPARE(KnowledgeDb::countryIdFromIso3166_1alpha3(CountryId3{"FOO"}), CountryId{});
    }
253
254
255
256
257

    void testIndianRailwaysStationCodeLookup()
    {
        auto station = KnowledgeDb::stationForIndianRailwaysStationCode(QString());
        QVERIFY(!station.coordinate.isValid());
258
        QCOMPARE(toQTimeZone(station.timezone), QTimeZone());
259
260
261

        station = KnowledgeDb::stationForIndianRailwaysStationCode(QStringLiteral("NDLS"));
        QVERIFY(station.coordinate.isValid());
262
        QCOMPARE(toQTimeZone(station.timezone), QTimeZone("Asia/Kolkata"));
263
264
265
266
        QCOMPARE(station.country, CountryId{"IN"});

        station = KnowledgeDb::stationForIndianRailwaysStationCode(QStringLiteral("ndls"));
        QVERIFY(!station.coordinate.isValid());
267
        QCOMPARE(toQTimeZone(station.timezone), QTimeZone());
268
    }
269
270
271
272
273

    void testFinishStationCodeLookup()
    {
        auto station = KnowledgeDb::stationForVRStationCode(VRStationCode(QStringLiteral("HKI")));
        QVERIFY(station.coordinate.isValid());
274
        QCOMPARE(toQTimeZone(station.timezone), QTimeZone("Europe/Helsinki"));
275
276
277

        station = KnowledgeDb::stationForVRStationCode(VRStationCode(QStringLiteral("BLÄ")));
        QVERIFY(!station.coordinate.isValid());
278
        QCOMPARE(toQTimeZone(station.timezone), QTimeZone());
279
    }
Volker Krause's avatar
Volker Krause committed
280
281
282
283
284
};

QTEST_APPLESS_MAIN(KnowledgeDbTest)

#include "knowledgedbtest.moc"