swift
testxplane.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (C) 2018 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
7 
8 #include <QTest>
9 
10 #include "test.h"
11 
12 #include "misc/directoryutils.h"
14 #include "misc/simulation/settings/xswiftbussettingsqtfree.inc"
16 #include "misc/swiftdirectories.h"
17 
18 using namespace swift::misc;
19 using namespace swift::misc::simulation::xplane::qtfreeutils;
20 using namespace swift::misc::simulation::settings;
21 
22 namespace MiscTest
23 {
25  class CTestXPlane : public QObject
26  {
27  Q_OBJECT
28 
29  private slots:
30  void getFileNameTest();
31  void getDirNameTest();
32  void getBaseNameTest();
33  void splitTest();
34  void acfPropertiesTest();
35  void xSwiftBusSettingsTest();
36  void qtFreeUtils();
37  };
38 
39  void CTestXPlane::getFileNameTest()
40  {
41  QCOMPARE(QString::fromStdString(getFileName("c:\\foo\\bar\\test.txt")), QString("test.txt"));
42  QCOMPARE(QString::fromStdString(getFileName("c:/foo/bar/test.txt")), QString("test.txt"));
43  QCOMPARE(QString::fromStdString(getFileName("/foo/bar/test.txt")), QString("test.txt"));
44  QCOMPARE(QString::fromStdString(getFileName("foo/test.txt")), QString("test.txt"));
45  QCOMPARE(QString::fromStdString(getFileName("test.txt")), QString("test.txt"));
46  QCOMPARE(QString::fromStdString(getFileName("foo/test.txt.txt")), QString("test.txt.txt"));
47  QCOMPARE(QString::fromStdString(getFileName("foo/bar")), QString("bar"));
48  }
49 
50  void CTestXPlane::getDirNameTest()
51  {
52  QCOMPARE(QString::fromStdString(getDirName("c:\\foo\\bar\\test.txt")), QString("bar"));
53  QCOMPARE(QString::fromStdString(getDirName("c:/foo/bar/test.txt")), QString("bar"));
54  QCOMPARE(QString::fromStdString(getDirName("/foo/bar/test.txt")), QString("bar"));
55  QCOMPARE(QString::fromStdString(getDirName("foo/test.txt")), QString("foo"));
56  QCOMPARE(QString::fromStdString(getDirName("test.txt")), QString());
57  QCOMPARE(QString::fromStdString(getDirName("foo/test.txt.txt")), QString("foo"));
58  }
59 
60  void CTestXPlane::getBaseNameTest()
61  {
62  QCOMPARE(QString::fromStdString(getBaseName("c:\\foo\\bar\\test.txt")), QString("test"));
63  QCOMPARE(QString::fromStdString(getBaseName("c:/foo/bar/test.txt")), QString("test"));
64  QCOMPARE(QString::fromStdString(getBaseName("/foo/bar/test.txt")), QString("test"));
65  QCOMPARE(QString::fromStdString(getBaseName("foo/test.txt")), QString("test"));
66  QCOMPARE(QString::fromStdString(getBaseName("test.txt")), QString("test"));
67  QCOMPARE(QString::fromStdString(getBaseName("foo/test.txt.txt")), QString("test"));
68  }
69 
70  void CTestXPlane::splitTest()
71  {
72  const std::string sentence("This is a test!");
73  std::vector<std::string> result = split(sentence);
74  QVERIFY(result.size() == 4);
75  QCOMPARE(QString::fromStdString(result[0]), QString("This"));
76  QCOMPARE(QString::fromStdString(result[3]), QString("test!"));
77 
78  std::vector<std::string> result2 = split(sentence, 1);
79  QVERIFY(result2.size() == 2);
80  QCOMPARE(QString::fromStdString(result2[0]), QString("This"));
81  QCOMPARE(QString::fromStdString(result2[1]), QString("is a test!"));
82 
83  std::vector<std::string> result3 = split(sentence, 0, ".");
84  QVERIFY(result3.size() == 1);
85  QCOMPARE(QString::fromStdString(result3[0]), QString("This is a test!"));
86 
87  std::vector<std::string> result4 = split("");
88  QVERIFY(result4.size() == 1);
89  QCOMPARE(QString::fromStdString(result4[0]), QString(""));
90  }
91 
92  void CTestXPlane::acfPropertiesTest()
93  {
94  QString testAcfFile = CSwiftDirectories::testFilesDirectory() + "/testaircraft.acf";
95  AcfProperties acfProperties = extractAcfProperties(testAcfFile.toStdString());
96  QCOMPARE(QString::fromStdString(acfProperties.aircraftIcaoCode), QString("BE58"));
97  QCOMPARE(QString::fromStdString(acfProperties.modelDescription), QString("[ACF] Beechcraft Baron B58"));
98  QCOMPARE(QString::fromStdString(acfProperties.author), QString("swift project"));
99  QCOMPARE(QString::fromStdString(acfProperties.modelName), QString("Beechcraft Baron 58"));
100  QCOMPARE(QString::fromStdString(acfProperties.modelString), QString("swift project Beechcraft Baron 58"));
101  }
102 
103  void CTestXPlane::xSwiftBusSettingsTest()
104  {
105  CXSwiftBusSettings s = CXSwiftBusSettings::defaultValue();
106  s.setMaxPlanes(33);
107  s.setMaxDrawDistanceNM(11.11);
108  s.setDrawingLabels(false);
110  s.setNightTextureModeQt("FOO");
111  s.setCurrentUtcTime();
112 
113  QVERIFY2(s.getNightTextureModeQt() == "foo", "Expect lower case");
114  QString json = s.toXSwiftBusJsonStringQt();
115  qDebug() << json;
116  qDebug() << s.toQString();
117 
118  CXSwiftBusSettings s2(json);
119  QCOMPARE(s.getMaxDrawDistanceNM(), s2.getMaxDrawDistanceNM());
120  QCOMPARE(s.getMaxPlanes(), s2.getMaxPlanes());
121  QCOMPARE(s.isDrawingLabels(), s2.isDrawingLabels());
122  QCOMPARE(s.getDBusServerAddressQt(), s2.getDBusServerAddressQt());
123  QCOMPARE(s.getFollowAircraftDistanceM(), s2.getFollowAircraftDistanceM());
124  QCOMPARE(s.getMSecsSinceEpoch(), s2.getMSecsSinceEpoch());
125  QVERIFY2(s2.getNightTextureModeQt() == "foo", "Expect lower case foo");
126 
128  json = s.toXSwiftBusJsonStringQt();
129  s2 = CXSwiftBusSettings(json);
130  QCOMPARE(CDBusServer::sessionBusAddress(), s2.getDBusServerAddressQt());
131 
132  // standard value object test
133  json = s.toJsonString(); // standard swift/Qt
134  qDebug() << json;
135 
136  s2.fromJson(json);
137  QCOMPARE(s.getMaxDrawDistanceNM(), s2.getMaxDrawDistanceNM());
138  QCOMPARE(s.getMaxPlanes(), s2.getMaxPlanes());
139  QCOMPARE(s.isDrawingLabels(), s2.isDrawingLabels());
140  QCOMPARE(s.getDBusServerAddressQt(), s2.getDBusServerAddressQt());
141  QVERIFY2(s2.getNightTextureModeQt() == "foo", "Expect lower case foo");
142  }
143 
144  void CTestXPlane::qtFreeUtils()
145  {
146  double vOut;
147  vOut = normalizeValue(77.0, 0.0, 360.0);
148  QVERIFY2(qFuzzyCompare(77.0, vOut), "Wrong normalize 0-360");
149 
150  vOut = normalizeValue(361.0, 0.0, 360.0);
151  QVERIFY2(qFuzzyCompare(1.0, vOut), "Wrong normalize 0-360");
152 
153  vOut = normalizeValue(-10.0, 0.0, 360.0);
154  QVERIFY2(qFuzzyCompare(350.0, vOut), "Wrong normalize 0-360");
155 
156  vOut = normalizeValue(-370.0, 0.0, 360.0);
157  QVERIFY2(qFuzzyCompare(350.0, vOut), "Wrong normalize 0-360");
158 
159  vOut = normalizeValue(-180.0, 0.0, 360.0);
160  QVERIFY2(qFuzzyCompare(180.0, vOut), "Wrong normalize 0-360");
161 
162  vOut = normalizeValue(-10.0, -180.0, 180.0);
163  QVERIFY2(qFuzzyCompare(-10.0, vOut), "Wrong normalize +-180");
164 
165  vOut = normalizeValue(100.0, -180.0, 180.0);
166  QVERIFY2(qFuzzyCompare(100.0, vOut), "Wrong normalize +-180");
167 
168  vOut = normalizeValue(190.0, -180.0, 180.0);
169  QVERIFY2(qFuzzyCompare(-170.0, vOut), "Wrong normalize +-180");
170 
171  vOut = normalizeValue(360.0, -180.0, 180.0);
172  QVERIFY2(qFuzzyCompare(0, vOut), "Wrong normalize +-180");
173 
174  vOut = normalizeValue(-190, -180.0, 180.0);
175  QVERIFY2(qFuzzyCompare(170, vOut), "Wrong normalize +-180");
176  }
177 } // namespace MiscTest
178 
181 
182 #include "testxplane.moc"
183 
X-Plane utils test.
Definition: testxplane.cpp:26
static const QString & sessionBusAddress()
Address denoting a session bus server.
Definition: dbusserver.cpp:319
static const QString & testFilesDirectory()
Where test files are located.
qint64 getMSecsSinceEpoch() const
Timestamp as ms value.
QString toJsonString(QJsonDocument::JsonFormat format=QJsonDocument::Indented) const
Convenience function JSON as string.
Definition: mixinjson.h:146
QString toQString(bool i18n=false) const
Cast as QString.
Definition: mixinstring.h:76
void setDBusServerAddressQt(const QString &dBusAddress)
Set DBus server.
virtual void setCurrentUtcTime()
Sets both timestamps.
void setNightTextureModeQt(const QString &nightTexture)
The the night texture mode.
QString getNightTextureModeQt() const
The the night texture mode.
bool isDrawingLabels() const
Get whether the plugin draws type and callsign labels above aircraft.
bool setMaxPlanes(int planes)
Set the maximum number of aircraft.
double getMaxDrawDistanceNM() const
Set the maximum distance at which to draw aircraft (nautical miles).
void setDrawingLabels(bool drawing)
Set whether the plugin draws type and callsign labels above aircraft.
int getFollowAircraftDistanceM() const
Get follow aircraft distance.
bool setMaxDrawDistanceNM(double nauticalMiles)
Set the maximum distance at which to draw aircraft (nautical miles).
int getMaxPlanes() const
Get the maximum number of aircraft.
bool setFollowAircraftDistanceM(int meters)
Set follow aircraft distance.
Free functions in swift::misc.
std::string getBaseName(const std::string &filePath)
Get the base name of the file.
Definition: qtfreeutils.h:45
AcfProperties extractAcfProperties(const std::string &filePath)
Extract ACF properties from an aircraft file.
Definition: qtfreeutils.h:170
std::string getFileName(const std::string &filePath)
Get filename (including all extensions) from a filePath.
Definition: qtfreeutils.h:23
double normalizeValue(const double value, const double start, const double end)
Normalize value to range start -> end (like for +-180degrees)
Definition: qtfreeutils.h:72
std::string getDirName(const std::string &filePath)
Get the name of the parent directory.
Definition: qtfreeutils.h:32
std::vector< std::string > split(const std::string &str, size_t maxSplitCount=0, const std::string &delimiter=" ")
Split string by delimiter and maxSplitCount times.
Definition: qtfreeutils.h:55
SWIFTTEST_MAIN(MiscTest::CTestXPlane)
main