File indexing completed on 2024-04-14 04:35:56

0001 /*
0002     SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <osm/datatypes.h>
0008 #include <osm/o5mparser.h>
0009 
0010 #include <QTest>
0011 
0012 // see https://wiki.openstreetmap.org/wiki/O5m for the examples used below
0013 class O5mParserTest : public QObject
0014 {
0015     Q_OBJECT
0016 private Q_SLOTS:
0017     void testParseUnsignedInt_data()
0018     {
0019         QTest::addColumn<QByteArray>("data");
0020         QTest::addColumn<uint32_t>("num");
0021 
0022         QTest::newRow("0") << QByteArray::fromHex("00") << 0u;
0023         QTest::newRow("1") << QByteArray::fromHex("01") << 1u;
0024         QTest::newRow("127") << QByteArray::fromHex("7f") << 127u;
0025         QTest::newRow("128") << QByteArray::fromHex("8001") << 128u;
0026         QTest::newRow("255") << QByteArray::fromHex("FF01") << 255u;
0027         QTest::newRow("256") << QByteArray::fromHex("8002") << 256u;
0028         QTest::newRow("323") << QByteArray::fromHex("c302") << 323u;
0029         QTest::newRow("16384") << QByteArray::fromHex("808001") << 16384u;
0030     }
0031 
0032     void testParseUnsignedInt()
0033     {
0034         QFETCH(QByteArray, data);
0035         QFETCH(uint32_t, num);
0036 
0037         OSM::DataSet dataSet;
0038         OSM::O5mParser p(&dataSet);
0039         const auto beginIt = reinterpret_cast<const uint8_t*>(data.constBegin());
0040         auto it = beginIt;
0041         const auto endIt = reinterpret_cast<const uint8_t*>(data.constEnd());
0042         QCOMPARE(p.readUnsigned(it, endIt), num);
0043         QVERIFY(it > beginIt);
0044         QVERIFY(it <= endIt);
0045     }
0046 
0047     void testParseSignedInt_data()
0048     {
0049         QTest::addColumn<QByteArray>("data");
0050         QTest::addColumn<int32_t>("num");
0051 
0052         QTest::newRow("0") << QByteArray::fromHex("00") << 0;
0053         QTest::newRow("64") << QByteArray::fromHex("8001") << 64;
0054         QTest::newRow("-2") << QByteArray::fromHex("03") << -2;
0055         QTest::newRow("-65") << QByteArray::fromHex("8101") << -65;
0056     }
0057 
0058     void testParseSignedInt()
0059     {
0060         QFETCH(QByteArray, data);
0061         QFETCH(int32_t, num);
0062 
0063         OSM::DataSet dataSet;
0064         OSM::O5mParser p(&dataSet);
0065         const auto beginIt = reinterpret_cast<const uint8_t*>(data.constBegin());
0066         auto it = beginIt;
0067         const auto endIt = reinterpret_cast<const uint8_t*>(data.constEnd());
0068         QCOMPARE(p.readSigned(it, endIt), num);
0069         QVERIFY(it > beginIt);
0070         QVERIFY(it <= endIt);
0071     }
0072 
0073     void testParseString()
0074     {
0075         const auto data = QByteArray::fromHex("0031696e6e65720001");
0076         const auto beginIt = reinterpret_cast<const uint8_t*>(data.constBegin());
0077         auto it = beginIt;
0078         const auto endIt = reinterpret_cast<const uint8_t*>(data.constEnd());
0079 
0080         OSM::DataSet dataSet;
0081         OSM::O5mParser p(&dataSet);
0082         auto s = p.readString(it, endIt);
0083         QCOMPARE(s, "1inner");
0084         QCOMPARE(it, beginIt + 8);
0085 
0086         s = p.readString(it, endIt);
0087         QCOMPARE(s, "1inner");
0088         QCOMPARE(it, beginIt + 9);
0089     }
0090 
0091     void testParseStringPair()
0092     {
0093         const auto data = QByteArray::fromHex("006f6e6577617900796573000061746d006e6f000200fc07004a6f686e00020301");
0094         const auto beginIt = reinterpret_cast<const uint8_t*>(data.constBegin());
0095         auto it = beginIt;
0096         const auto endIt = reinterpret_cast<const uint8_t*>(data.constEnd());
0097 
0098         OSM::DataSet dataSet;
0099         OSM::O5mParser p(&dataSet);
0100         auto s = p.readStringPair(it, endIt);
0101         QCOMPARE(s.first, "oneway");
0102         QCOMPARE(s.second, "yes");
0103         QCOMPARE(it, beginIt + 12);
0104 
0105         s = p.readStringPair(it, endIt);
0106         QCOMPARE(s.first, "atm");
0107         QCOMPARE(s.second, "no");
0108         QCOMPARE(it, beginIt + 20);
0109 
0110         s = p.readStringPair(it, endIt);
0111         QCOMPARE(s.first, "oneway");
0112         QCOMPARE(s.second, "yes");
0113         QCOMPARE(it, beginIt + 21);
0114 
0115         s = p.readStringPair(it, endIt);
0116         QCOMPARE(s.first, "\xfc\x07");
0117         QCOMPARE(s.second, "John");
0118         QCOMPARE(it, beginIt + 30);
0119 
0120         s = p.readStringPair(it, endIt);
0121         QCOMPARE(s.first, "atm");
0122         QCOMPARE(s.second, "no");
0123         QCOMPARE(it, beginIt + 31);
0124 
0125         s = p.readStringPair(it, endIt);
0126         QCOMPARE(s.first, "oneway");
0127         QCOMPARE(s.second, "yes");
0128         QCOMPARE(it, beginIt + 32);
0129 
0130         s = p.readStringPair(it, endIt);
0131         QCOMPARE(s.first, "\xfc\x07");
0132         QCOMPARE(s.second, "John");
0133         QCOMPARE(it, beginIt + 33);
0134     }
0135 
0136     void testParseWay()
0137     {
0138         const auto data = QByteArray::fromHex("CCE48E04002DCAAFA01A02BCA0AFF6018FFAD5F70180DFBB9E0FA5E5E5A60DE4E5E5A60DE385959D0F9E86959D0FF7E6E5A60D0062426F780031332E333634313031392C35322E353233323734312C31332E333635373039392C35322E353234323033310000726566003630323400");
0139         const auto beginIt = reinterpret_cast<const uint8_t*>(data.constBegin());
0140         auto it = beginIt;
0141         const auto endIt = reinterpret_cast<const uint8_t*>(data.constEnd());
0142 
0143         OSM::DataSet dataSet;
0144         OSM::O5mParser p(&dataSet);
0145         p.readWay(it, endIt);
0146 
0147         QCOMPARE(dataSet.ways.size(), 1);
0148         const auto &way = dataSet.ways[0];
0149         QCOMPARE(way.id, 4315430ll);
0150         QCOMPARE(way.nodes.size(), 10);
0151         QCOMPARE(way.tags.size(), 1);
0152         QCOMPARE(way.bbox.isValid(), true);
0153     }
0154 
0155     void testParseRelation()
0156     {
0157         const auto data = QByteArray::fromHex("902e0011f498830b0031696e6e657200ca93d30d010074797065006d756c7469706f6c79676f6e00");
0158         const auto beginIt = reinterpret_cast<const uint8_t*>(data.constBegin());
0159         auto it = beginIt;
0160         const auto endIt = reinterpret_cast<const uint8_t*>(data.constEnd());
0161 
0162         OSM::DataSet dataSet;
0163         OSM::O5mParser p(&dataSet);
0164         p.readRelation(it, endIt);
0165 
0166         QCOMPARE(dataSet.relations.size(), 1);
0167         const auto &rel = dataSet.relations[0];
0168         QCOMPARE(rel.id, 2952ll);
0169         QCOMPARE(rel.members.size(), 2);
0170         QCOMPARE(rel.members[0].id, 11560506ll);
0171         QCOMPARE(rel.members[0].role().name(), "inner");
0172         QCOMPARE(rel.members[0].type(), OSM::Type::Way);
0173         QCOMPARE(rel.members[1].id, 25873183ll);
0174         QCOMPARE(rel.members[1].role().name(), "inner");
0175         QCOMPARE(rel.members[1].type(), OSM::Type::Way);
0176         QCOMPARE(rel.tags.size(), 1);
0177         QCOMPARE(rel.tags[0].key.name(), "type");
0178         QCOMPARE(rel.tags[0].value, "multipolygon");
0179     }
0180 };
0181 
0182 QTEST_GUILESS_MAIN(O5mParserTest)
0183 
0184 #include "o5mparsertest.moc"