17 #include <QJsonObject>
25 #include "../testvalueobject.h"
41 using namespace swift::misc::aviation;
42 using namespace swift::misc::geo;
43 using namespace swift::misc::math;
44 using namespace swift::misc::physical_quantities;
60 void collectionBasics();
61 void sequenceBasics();
66 void dictionaryBasics();
68 void offsetTimestampList();
73 void CTestContainers::collectionBasics()
76 QVERIFY2(c1.
isEmpty(),
"Uninitialized collection is empty");
78 QVERIFY2(c1 == c2,
"Copy of collection is equal");
80 QVERIFY2(c1 != c2,
"Different collections are not equal");
81 QVERIFY2(c1.
size() == 1,
"Collection has expected size");
83 QVERIFY2(c1 == c2,
"Collections with equal elements are equal");
85 QVERIFY2(c1.
isEmpty(),
"Cleared collection is empty");
87 QVERIFY2(c1 != c2,
"Collections with different elements are not equal");
89 QVERIFY2(c1 == c2,
"Copied collection is equal");
92 void CTestContainers::sequenceBasics()
95 QVERIFY2(s1.
isEmpty(),
"Uninitialized sequence is empty");
97 QVERIFY2(s1 == s2,
"Copy of sequence is equal");
99 QVERIFY2(s1 != s2,
"Different sequences are not equal");
100 QVERIFY2(s1.
size() == 1,
"Sequence has expected size");
102 QVERIFY2(s1 == s2,
"Sequences with equal elements are equal");
104 QVERIFY2(s1.
isEmpty(),
"Cleared sequence is empty");
106 QVERIFY2(s1 != s2,
"Sequences with different elements are not equal");
108 QVERIFY2(s1 == s2,
"Copied sequence is equal");
110 QVERIFY2((s1[0] = 1),
"Subscripted element mutation");
111 QVERIFY2(s1[0] == 1,
"Subscripted element has expected value");
112 QVERIFY2(s1.
back() == 1,
"Last element has expected value");
115 void CTestContainers::joinAndSplit()
124 auto joined = s1.
join(s2);
126 QVERIFY2(s1.
size() == 6,
"Combine sequences");
127 QVERIFY2(s1 == joined,
"Combine sequences");
145 QVERIFY2(c1.
makeUnion(c2) == c1,
"Combine collections");
146 QVERIFY2(c2.
makeUnion(c3) == c1,
"Combine collections");
147 QVERIFY2(c1.
intersection(c2) == c2,
"Combine collections");
148 QVERIFY2(c1.
difference(c2) == c3,
"Split collections");
150 QVERIFY2(c1.
size() == 9,
"Combine collections");
152 QVERIFY2(c1.
size() == 6,
"Split collections");
154 QVERIFY2(c1 == c3,
"Split collections");
157 void CTestContainers::findTests()
161 QVERIFY2(found.
isEmpty(),
"Empty found");
165 QVERIFY2(found.
size() == 1,
"found");
168 void CTestContainers::sortTests()
172 const QString &getName()
const {
return name; }
173 int getAge()
const {
return age; }
174 bool operator==(
const Person &other)
const {
return name == other.name && age == other.age; }
178 CSequence<Person> list { {
"Alice", 33 }, {
"Bob", 32 }, {
"Cathy", 32 }, {
"Dave", 31 }, {
"Emily", 31 } };
179 CSequence<Person> sorted { {
"Dave", 31 }, {
"Emily", 31 }, {
"Bob", 32 }, {
"Cathy", 32 }, {
"Alice", 33 } };
180 QVERIFY2(list.sortedBy(&Person::getAge, &Person::getName) == sorted,
"sort by multiple members");
183 void CTestContainers::removeTests()
187 { 1, 5 }, { 5, 9 }, { 1, 2 }, { 8, 9 }, { 4, 5, 6 },
188 { 1, 5, 9 }, { 3, 7 }, { 3, 5, 7 }, base };
189 for (
const auto &subset : subsets)
191 auto copy1 = base, copy2 = base;
193 copy2.removeIfInSubset(subset);
194 QVERIFY2(copy1 == copy2,
"removeIfInSubset");
198 void CTestContainers::dictionaryBasics()
217 QVERIFY2(d1 != d2,
"Inequality operator failed");
218 QVERIFY2(d1 == d3,
"Equality operator failed");
221 QVERIFY2(d1.
size() == 2,
"size() wrong");
222 QVERIFY2(d1.
size() == d1.
count(),
"size() is different to count()");
226 QVERIFY2(d1.
isEmpty(),
"clear failed");
231 auto keys = d1.
keys();
232 QVERIFY2(std::distance(keys.begin(), keys.end()) == 2,
"keys range size wrong");
236 QVERIFY2(keyCollection.
size() == 2,
"keys collection size wrong");
240 QVERIFY2(keySequence.
size() == 2,
"keys sequence size wrong");
244 QVERIFY2(d2.
size() == 1,
"findKeyBy returned wrong container");
246 QVERIFY2(o1.
getName() ==
"Value1",
"findKeyBy returned wrong container");
250 QVERIFY2(d2.
size() == 1,
"findValueBy returned wrong container");
252 QVERIFY2(o1.
getName() ==
"Value1",
"findKeyBy returned wrong container");
265 QVERIFY2(d2.
size() == 1,
"size() wrong");
270 QVERIFY2(d2.
size() == 1,
"size() wrong");
273 QJsonObject jsonObject = d1.
toJson();
276 QVERIFY2(d1 == d4,
"JSON serialization/deserialization failed");
279 void CTestContainers::timestampList()
282 const qint64 ts = QDateTime::currentMSecsSinceEpoch();
284 for (
int i = 0; i < no; ++i)
295 QVERIFY2(ms == ts - 10 * (no - 1),
"Oldest value not first");
299 QVERIFY2(ms == ts,
"Latest value not first");
303 const int maxElements = 8;
305 for (
int i = 0; i < no; ++i)
307 qint64 cTs = ts - 10 * i;
312 if (i > maxElements - 1)
314 QVERIFY2(situations.
size() == maxElements,
"Situations must only contain max.elements");
318 QVERIFY2(situations.
size() == i + 1,
"Element size does not match");
325 no = situations.
size();
326 for (
int i = 0; i < no; ++i)
337 const qint64 t1 = situations[i - 1].getMSecsSinceEpoch();
338 QVERIFY2(t1 == afterTs,
"Wrong expected after");
342 const qint64 t1 = situations[i + 1].getMSecsSinceEpoch();
343 QVERIFY2(t1 == beforeTs,
"Wrong expected before");
348 void CTestContainers::offsetTimestampList()
352 CCoordinateGeodetic::fromWgs84(
"48° 21′ 13″ N",
"11° 47′ 09″ E", { 1487, CLengthUnit::ft() });
357 for (
int i = 0; i < no; ++i)
364 if (CMathUtils::randomBool())
366 ts += CMathUtils::randomInteger(4500, 5500);
371 ts += CMathUtils::randomInteger(900, 1100);
377 QVERIFY2(situations.
size() <= max,
"Wrong size");
383 no = situations.
size();
384 for (
int i = 0; i < no; ++i)
395 const qint64 t1 = situations[i - 1].getAdjustedMSecsSinceEpoch();
396 QVERIFY2(t1 == afterTs,
"Wrong expected after");
400 const qint64 t1 = situations[i + 1].getAdjustedMSecsSinceEpoch();
401 QVERIFY2(t1 == beforeTs,
"Wrong expected before");
410 #include "testcontainers.moc"
CTestContainers(QObject *parent=nullptr)
Constructor.
Generic ordered container with value semantics.
size_type size() const
Returns number of elements in the collection.
CCollection difference(const C &other) const
Returns a collection which contains all the elements from this collection which are not in the other ...
void remove(const T &object)
Efficient remove using the find and erase of the implementation container. Typically O(log n).
iterator insert(const_iterator hint, const T &value)
For compatibility with std::inserter.
CCollection intersection(const C &other) const
Returns a collection which is the intersection of this collection and another.
bool isEmpty() const
Synonym for empty.
CCollection makeUnion(const C &other) const
Returns a collection which is the union of this collection and another container.
iterator push_back(const T &value)
Synonym for insert.
void clear()
Removes all elements in the collection.
Associative container with value semantics, chooses a sensible default implementation container type.
int count(const Key &key) const
Returns the number of items with key.
void removeByKeyIf(Predicate p)
Remove elements for which a given predicate for value returns true.
CDictionary findValueBy(Predicate p) const
Return a copy containing only those elements for which a given predicate returns true.
void clear()
Removes all items from the dictionary.
const Value value(const Key &key) const
Returns the value associated with the key.
void convertFromJson(const QJsonObject &json)
Assign from JSON object.
int size() const
Returns the number of items in the hash.
QJsonObject toJson() const
Cast to JSON object.
CDictionary findKeyBy(Predicate p) const
Return a copy containing only those elements for which the dictionary keys return true for a given pr...
auto keys() const
Return a range of all keys (does not allocate a temporary container)
bool isEmpty() const
Returns true if dictionary is empty.
iterator insert(const Key &key, const Value &value)
Insert new item with key and value.
void removeByValueIf(Predicate p)
Remove elements for which a given predicate for key returns true.
bool containsByValue(Predicate p) const
Return true if there is an element for which a given predicate returns true.
bool containsByKey(Predicate p) const
Return true if there is an element for which a given predicate returns true.
auto findBy(Predicate p) const
Return a copy containing only those elements for which a given predicate returns true.
Generic sequential container with value semantics.
size_type size() const
Returns number of elements in the sequence.
void push_back(const T &value)
Appends an element at the end of the sequence.
void push_frontMaxElements(const T &value, int maxElements)
Insert as first element by keep maxElements.
reference front()
Access the first element.
void clear()
Removes all elements in the sequence.
reference back()
Access the last element.
int removeIfIn(const CSequence &other)
Remove all elements if they are in other.
bool isEmpty() const
Synonym for empty.
CSequence join(const CSequence &other) const
Concatenates two sequences and returns the result.
const QString & getName() const
Get name.
qint64 getMSecsSinceEpoch() const
Timestamp as ms value.
void setMSecsSinceEpoch(qint64 mSecsSinceEpoch)
Timestamp as ms value.
OBJ findObjectBeforeOrDefault(qint64 msSinceEpoch) const
Object before timestamp or default (older)
void sortOldestFirst()
Sort by timestamp.
OBJ findObjectAfterOrDefault(qint64 msSinceEpoch) const
List of objects after msSinceEpoch (newer)
void sortLatestFirst()
Sort by timestamp.
bool hasInvalidTimestamps() const
Has invalid timestamp.
bool isSortedLatestFirst() const
Is completely sorted: latest last.
qint64 getAdjustedMSecsSinceEpoch() const
Timestamp with offset added for interpolation.
void setTimeOffsetMs(qint64 offset)
Milliseconds to add to timestamp for interpolation.
bool containsZeroOrNegativeOffsetTime() const
Any negative or zero offset time?
OBJ findObjectBeforeAdjustedOrDefault(qint64 msSinceEpoch) const
Object before timestamp (older)
void push_frontKeepLatestFirstAdjustOffset(const OBJ &value, bool replaceSameTimestamp=true, int maxElements=-1)
Insert as first element by keeping maxElements and the latest first.
OBJ findObjectAfterAdjustedOrDefault(qint64 msSinceEpoch) const
List of objects after msSinceEpoch (newer)
Value object encapsulating information of an aircraft's situation.
void setCallsign(const CCallsign &callsign)
Corresponding callsign.
void setPosition(const geo::CCoordinateGeodetic &position)
Set position.
List of aircraft situations.
bool isSortedAdjustedLatestFirstWithoutNullPositions() const
Latest first and no null positions?
Value object encapsulating information of a callsign.
const QString & asString() const
Get callsign (normalized)
Value object for a set of callsigns.
Free functions in swift::misc.
void registerMetadata()
Register all relevant metadata in Misc.
SWIFTTEST_APPLESS_MAIN(MiscTest::CTestContainers)
main