swift
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 
11 #include <QDBusArgument>
12 #include <QDateTime>
13 #include <QObject>
14 #include <QTest>
15 
16 #include "test.h"
17 
20 #include "misc/aviation/callsign.h"
24 #include "misc/network/user.h"
25 #include "misc/pq/frequency.h"
26 #include "misc/pq/length.h"
28 #include "misc/pq/units.h"
30 #include "misc/registermetadata.h"
31 #include "misc/variant.h"
32 #include "misc/variantlist.h"
33 
34 using namespace swift::misc;
35 using namespace swift::misc::aviation;
36 using namespace swift::misc::physical_quantities;
37 using namespace swift::misc::geo;
38 using namespace swift::misc::network;
39 
40 namespace MiscTest
41 {
43  class CTestVariantAndMap : public QObject
44  {
45  Q_OBJECT
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 {}; }
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 
79 Q_DECLARE_METATYPE(MiscTest::CTestMatcher)
80 
81 namespace 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::from(station1), "Station should be equal (CVariant)");
117  QVERIFY2(CVariant::from(station1) == station1qv, "Station should be equal (CVariant)");
118  QVERIFY2(CVariant::from(station2) == station1qv, "Station should be equal (CVariant)");
119  QVERIFY2(CVariant::from(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 variant = CVariant::from(ints);
130  QVERIFY2(variant.canConvert<CVariantList>(), "Variant containing list can convert to CVariantList");
131  QVERIFY2(variant.convert(qMetaTypeId<CVariantList>()), "Variant containing list can convert to CVariantList");
132  const CVariantList variantInts = variant.to<CVariantList>();
133  QVERIFY2(ints.size() == variantInts.size(), "Variant list has same size as original list");
134  QVERIFY2(ints[0] == variantInts[0].to<int>(), "Variant list has same element");
135  QVERIFY2(variant.canConvert<CSequence<int>>(), "Variant containing can convert back");
136  QVERIFY2(ints == variant.to<CSequence<int>>(), "Variant list converted back compares equal");
137 
138  const CAirlineIcaoCodeList list { CAirlineIcaoCode("BAW"), CAirlineIcaoCode("DLH"), CAirlineIcaoCode("AAL") };
139  variant = CVariant::from(list);
140  QVERIFY2(variant.canConvert<CVariantList>(), "Variant containing list can convert to CVariantList");
141  QVERIFY2(variant.convert(qMetaTypeId<CVariantList>()), "Variant containing list can convert to CVariantList");
142  CVariantList variantList = variant.to<CVariantList>();
143  QVERIFY2(list.size() == variantList.size(), "Variant list has same size as original list");
144  QVERIFY2(list[0] == variantList[0].to<CAirlineIcaoCode>(), "Variant list has same element");
145  QVERIFY2(variant.canConvert<CAirlineIcaoCodeList>(), "Variant containing can convert back");
146  QVERIFY2(list == variant.to<CAirlineIcaoCodeList>(), "Variant list converted back compares equal");
147  }
148 
149  void CTestVariantAndMap::matches()
150  {
151  CTestMatcher::registerMetadata();
152  const CTestMatcher matcher {};
153  const CVariant variant = CVariant::from(matcher);
154  QVERIFY2(variant.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);
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 
Variant and map related tests.
Specialized value object compliant map for variants, based on indexes.
bool matches(const T &value) const
True if this map matches the value.
void addValue(const CPropertyIndex &index, const CVariant &value)
Add a value.
bool isWildcard() const
Wildcard, only relevant when used in search.
Generic sequential container with value semantics.
Definition: sequence.h:86
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:66
T to() const
Synonym for value().
Definition: variant.h:176
static CVariant fromValue(T &&value)
Construct a variant from a value.
Definition: variant.h:139
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.
static CVariant from(T &&value)
Synonym for fromValue().
Definition: variant.h:147
bool canConvert(int typeId) const
True if this variant can be converted to the type with the given metatype ID.
Value object encapsulating a list of variants.
Definition: variantlist.h:29
Value object for ICAO classification.
Value object encapsulating a list of ICAO codes.
Value object encapsulating information about an ATC station.
Definition: atcstation.h:38
Value object encapsulating information of a callsign.
Definition: callsign.h:30
CRTP class template from which a derived class can inherit common methods dealing with the metatype o...
Definition: mixinmetatype.h:29
Value object encapsulating information of a user.
Definition: user.h:28
Physical unit length (length)
Definition: length.h:18
int compare(T a, T b)
Compare arithmetic values.
const QDBusArgument & operator>>(const QDBusArgument &arg, std::string &s)
Operator for std::string from QDBusArgument.
Definition: dbus.cpp:14
size_t qHash(const std::string &key, uint seed)
std::string qHash
Definition: metaclass.h:86
Free functions in swift::misc.
void registerMetadata()
Register all relevant metadata in Misc.
QDebug operator<<(QDebug d, const CRange< I > &range)
Streaming operators for CRange to qDebug.
Definition: range.h:298
SWIFTTEST_APPLESS_MAIN(MiscTest::CTestVariantAndMap)
main