swift
Loading...
Searching...
No Matches
testvariantandmap.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
10
11#include <QDBusArgument>
12#include <QDateTime>
13#include <QObject>
14#include <QTest>
15
16#include "test.h"
17
24#include "misc/network/user.h"
25#include "misc/pq/frequency.h"
26#include "misc/pq/length.h"
28#include "misc/pq/units.h"
31#include "misc/variant.h"
32#include "misc/variantlist.h"
33
34using namespace swift::misc;
35using namespace swift::misc::aviation;
36using namespace swift::misc::physical_quantities;
37using namespace swift::misc::geo;
38using namespace swift::misc::network;
39
40namespace MiscTest
41{
44 {
46
47 private slots:
49 void initTestCase();
50
52 void variant();
53
55 void variantList();
56
58 void matches();
59
61 void valueMap();
62 };
63
65 class CTestMatcher : public swift::misc::mixin::MetaType<CTestMatcher>
66 {
67 public:
68 bool matches(const swift::misc::CVariant &) const { return true; }
69
70 // methods needed for metatype registration
71 QString toQString(bool) const { return {}; } // cppcheck-suppress functionStatic
72 friend QDBusArgument &operator<<(QDBusArgument &arg, const CTestMatcher &) { return arg; }
73 friend const QDBusArgument &operator>>(const QDBusArgument &arg, CTestMatcher &) { return arg; }
74 friend QDataStream &operator<<(QDataStream &ds, const CTestMatcher &) { return ds; }
75 friend QDataStream &operator>>(QDataStream &ds, CTestMatcher &) { return ds; }
76 };
77} // namespace MiscTest
78
79Q_DECLARE_METATYPE(MiscTest::CTestMatcher)
80
81namespace MiscTest
82{
83 void CTestVariantAndMap::initTestCase() { swift::misc::registerMetadata(); }
84
85 void CTestVariantAndMap::variant()
86 {
87 // ATC station
88 CCoordinateGeodetic geoPos =
89 CCoordinateGeodetic::fromWgs84("48° 21′ 13″ N", "11° 47′ 09″ E", { 1487, CLengthUnit::ft() });
90 CAtcStation station1(CCallsign("eddm_twr"), CUser("123456", "Joe Doe"),
91 CFrequency(118.7, CFrequencyUnit::MHz()), geoPos, CLength(50, CLengthUnit::km()), false);
92 CAtcStation station2(station1);
93 CAtcStation station3(CCallsign("eddm_app"), CUser("654321", "Jen Doe"),
94 CFrequency(120.7, CFrequencyUnit::MHz()), geoPos, CLength(100, CLengthUnit::km()), false);
95
96 // compare
97 CLength l1(0, nullptr);
98 CLength l2(0, nullptr);
99 QVERIFY2(l1 == l2, "Null lengths should be equal");
100
101 CLength l3(0, CLengthUnit::m());
102 CLength l4(-1, CLengthUnit::m());
103 QVERIFY2(l1 != l3, "Null length and non-null length should not be equal");
104 QVERIFY2(l1 != l4, "Null length and non-null length should not be equal");
105 QVERIFY2((l1 < l4) != (l1 > l4), "Null length and non-null length should be comparable");
106 QVERIFY2(compare(l1, l4) != 0, "Null length and non-null length should be sortable");
107
108 CVariant station1qv = CVariant::fromValue(station1);
109 QVERIFY2(station1 == station1, "Station should be equal");
110
111 QVERIFY(station1.getController() == station2.getController());
112 QVERIFY(station1.getRelativeDistance() == station2.getRelativeDistance());
113
114 QVERIFY2(station1 == station2, "Station should be equal");
115 QVERIFY2(station1 != station3, "Station should not be equal");
116 QVERIFY2(station1qv == CVariant::fromValue(station1), "Station should be equal (CVariant)");
117 QVERIFY2(CVariant::fromValue(station1) == station1qv, "Station should be equal (CVariant)");
118 QVERIFY2(CVariant::fromValue(station2) == station1qv, "Station should be equal (CVariant)");
119 QVERIFY2(CVariant::fromValue(station3) != station1qv, "Station should be equal (CVariant)");
120
121 QVERIFY2(compare(station1, station1) == 0, "Station should be equal");
122 QVERIFY2(compare(station1, station2) == 0, "Station should be equal");
123 QVERIFY2(compare(station1, station3) != 0, "Station should not be equal");
124 }
125
126 void CTestVariantAndMap::variantList()
127 {
128 const CSequence<int> ints { 1, 2, 3 };
129 CVariant listVar = CVariant::fromValue(ints);
130 QVERIFY2(listVar.canConvert<CVariantList>(), "Variant containing list can convert to CVariantList");
131 QVERIFY2(listVar.convert(qMetaTypeId<CVariantList>()), "Variant containing list can convert to CVariantList");
132 const auto variantInts = listVar.value<CVariantList>();
133 QVERIFY2(ints.size() == variantInts.size(), "Variant list has same size as original list");
134 QVERIFY2(ints[0] == variantInts[0].value<int>(), "Variant list has same element");
135 QVERIFY2(listVar.canConvert<CSequence<int>>(), "Variant containing can convert back");
136 QVERIFY2(ints == listVar.value<CSequence<int>>(), "Variant list converted back compares equal");
137
138 const CAirlineIcaoCodeList list { CAirlineIcaoCode("BAW"), CAirlineIcaoCode("DLH"), CAirlineIcaoCode("AAL") };
139 listVar = CVariant::fromValue(list);
140 QVERIFY2(listVar.canConvert<CVariantList>(), "Variant containing list can convert to CVariantList");
141 QVERIFY2(listVar.convert(qMetaTypeId<CVariantList>()), "Variant containing list can convert to CVariantList");
142 auto variantList = listVar.value<CVariantList>();
143 QVERIFY2(list.size() == variantList.size(), "Variant list has same size as original list");
144 QVERIFY2(list[0] == variantList[0].value<CAirlineIcaoCode>(), "Variant list has same element");
145 QVERIFY2(listVar.canConvert<CAirlineIcaoCodeList>(), "Variant containing can convert back");
146 QVERIFY2(list == listVar.value<CAirlineIcaoCodeList>(), "Variant list converted back compares equal");
147 }
148
149 void CTestVariantAndMap::matches()
150 {
152 const CTestMatcher matcher {};
153 const CVariant matcherVariant = CVariant::fromValue(matcher);
154 QVERIFY2(matcherVariant.matches(CVariant()), "Variant provides access to stored object's matches() method");
155 }
156
157 void CTestVariantAndMap::valueMap()
158 {
159 // ATC station
160 CCoordinateGeodetic geoPos =
161 CCoordinateGeodetic::fromWgs84("48° 21′ 13″ N", "11° 47′ 09″ E", { 1487, CLengthUnit::ft() });
162 CAtcStation station1(CCallsign("eddm_twr"), CUser("123456", "Joe Doe"),
163 CFrequency(118.7, CFrequencyUnit::MHz()), geoPos, CLength(50, CLengthUnit::km()), false);
164
165 // value maps
166 CPropertyIndexVariantMap vmWildcard(true);
167 CPropertyIndexVariantMap vmNoWildcard(false);
168 CPropertyIndexVariantMap vm;
169 CPropertyIndexVariantMap vmCopy(vmWildcard);
170
171 // Remark: Shortcoming here, as the callsign will be automatically set for user in station
172 // I have to set this as well, otherwise, no match.
173 vm.addValue(CAtcStation::IndexController, CUser("123456", "Joe Doe", CCallsign("EDDM_TWR")));
174
175 // compare
176
177 QVERIFY2(vmWildcard.matches(station1), "Station should be equal to wildcard");
178 QVERIFY2(!vmNoWildcard.matches(station1), "Station should not be equal to empty list");
179 QVERIFY2(vm.matches(station1), "Controller should match");
180 QVERIFY2(vmWildcard == vmCopy, "Maps should be equal");
181 QVERIFY2(qHash(vmWildcard) == qHash(vmCopy), "Hashs should be equal (simple)");
182
183 vm.addValue(CAtcStation::IndexFrequency, CFrequency(118.7, CFrequencyUnit::MHz()));
184 vm.addValue(CAtcStation::IndexPosition, geoPos);
185 vmCopy = vm;
186 QVERIFY2(qHash(vm) == qHash(vmCopy), "Hashs should be equal (detailed)");
187 vmCopy.setWildcard(!vm.isWildcard());
188 QVERIFY2(qHash(vm) != qHash(vmCopy), "Hashs should not be equal (detailed)");
189 }
190} // namespace MiscTest
191
194
195#include "testvariantandmap.moc"
196
static CFrequencyUnit MHz()
Megahertz.
Definition units.h:380
Variant and map related tests.
void addValue(const CPropertyIndex &index, const CVariant &value)
Add a value.
bool isWildcard() const
Wildcard, only relevant when used in search.
bool matches(const T &value) const
True if this map matches the value.
size_type size() const
Returns number of elements in the sequence.
Definition sequence.h:273
Wrapper around QVariant which provides transparent access to CValueObject methods of the contained ob...
Definition variant.h:69
static CVariant fromValue(T &&value)
Construct a variant from a value.
Definition variant.h:139
T value() const
Return the value converted to the type T.
Definition variant.h:154
bool convert(int typeId)
Convert this variant to the type with the given metatype ID and return true if successful.
bool matches(const CVariant &event) const
If this is an event subscription, return true if it matches the given event.
bool canConvert(int typeId) const
True if this variant can be converted to the type with the given metatype ID.
static CCoordinateGeodetic fromWgs84(const QString &latitudeWgs84, const QString &longitudeWgs84, const aviation::CAltitude &geodeticHeight={})
Coordinate by WGS84 position data.
CRTP class template from which a derived class can inherit common methods dealing with the metatype o...
static CLengthUnit km()
Kilometer km.
Definition units.h:165
static CLengthUnit m()
Meter m.
Definition units.h:140
static CLengthUnit ft()
Foot ft.
Definition units.h:157
int compare(T a, T b)
Compare arithmetic values.
Free functions in swift::misc.
void registerMetadata()
Register all relevant metadata in Misc.
size_t qHash(const std::string &key, uint seed)
std::string qHash
Definition metaclass.h:84
QObject(QObject *parent)
Q_OBJECTQ_OBJECT
QVERIFY(condition)
QVERIFY2(condition, message)
#define SWIFTTEST_APPLESS_MAIN(TestObject)
Implements a main() function that executes all tests in TestObject without instantiating a QApplicati...
Definition test.h:39