Debian Codegraph @

test_suite/unit_tests/Main/test_timeManager.cxx

raw symbols SCIP
1 // Written by James Turner, started 2021.
2 //
3 // Copyright (C) 2021 James Turner
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 #include "config.h"
20
21 #include "test_timeManager.hxx"
22
23 #include "test_suite/FGTestApi/NavDataCache.hxx"
24 #include "test_suite/FGTestApi/TestPilot.hxx"
25 #include "test_suite/FGTestApi/testGlobals.hxx"
26
27 #include <simgear/io/iostreams/sgstream.hxx>
28 #include <simgear/misc/sg_dir.hxx>
29 #include <simgear/props/props_io.hxx>
30 #include <simgear/timing/sg_time.hxx>
31
32 #include "Main/fg_props.hxx"
33 #include "Main/globals.hxx"
34 #include <Airports/airport.hxx>
35 #include <Time/TimeManager.hxx>
36
37 using namespace flightgear;
38
39
40 // Set up function for each test.
41 void TimeManagerTests::setUp()
42 {
43 FGTestApi::setUp::initTestGlobals("timeManager");
44 FGTestApi::setUp::initNavDataCache();
45 }
46
47
48 // Clean up after each test.
49 void TimeManagerTests::tearDown()
50 {
51 FGTestApi::tearDown::shutdownTestGlobals();
52 }
53
54
55 void TimeManagerTests::testBasic()
56 {
57 auto timeManager = globals->get_subsystem<TimeManager>();
58
59 // set standard values
60 fgSetBool("/sim/freeze", false);
61 fgSetBool("/sim/sceneryloaded", true);
62 fgSetDouble("/sim/model-hz", 120.0);
63
64 timeManager->bind();
65 timeManager->init();
66 timeManager->postinit();
67
68 double simDt, realDt;
69 // first run: values are zero
70 timeManager->computeTimeDeltas(simDt, realDt);
71 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, simDt, 1.0e-6);
72 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, realDt, 1.0e-6);
73
74 // manually modify the 'last time' to check delta computation
75 timeManager->_lastStamp = SGTimeStamp::now() - SGTimeStamp::fromMSec(25);
76 timeManager->computeTimeDeltas(simDt, realDt);
77 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.025, simDt, 1.0e-3);
78 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.025, realDt, 1.0e-3);
79
80 timeManager->update(simDt);
81 }
82
83 void TimeManagerTests::testFreezeUnfreeze()
84 {
85 auto timeManager = globals->get_subsystem<TimeManager>();
86
87 // set standard values
88 fgSetBool("/sim/freeze/clock", false);
89 fgSetBool("/sim/sceneryloaded", true);
90 fgSetDouble("/sim/model-hz", 120.0);
91
92 timeManager->postinit();
93
94 double simDt, realDt;
95 // first run: values are zero
96 timeManager->computeTimeDeltas(simDt, realDt);
97
98 // test hack: force dtRemainder to zero so we aren't affected by the
99 // system -> stread clock offset in our test assertions. Without this,
100 // depending on exaclty when thetest suite is run, we would get different
101 // results
102 timeManager->_dtRemainder = 0.0;
103
104 SGTimeStamp n;
105
106 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, simDt, 1.0e-6);
107 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, realDt, 1.0e-6);
108
109 timeManager->_lastStamp = SGTimeStamp::now() - SGTimeStamp::fromMSec(15);
110 timeManager->computeTimeDeltas(simDt, realDt);
111 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.008333, simDt, 1.0e-5);
112 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.008333, realDt, 1.0e-5);
113
114 fgSetBool("/sim/freeze/clock", true);
115 timeManager->_lastStamp = SGTimeStamp::now() - SGTimeStamp::fromMSec(20);
116 timeManager->computeTimeDeltas(simDt, realDt);
117 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, simDt, 1.0e-5); // sim time should not advance
118 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.025, realDt, 1.0e-5);
119 }
120
121 void TimeManagerTests::testTimeZones()
122 {
123 auto timeManager = globals->get_subsystem<TimeManager>();
124
125 auto vabb = fgFindAirportID("VABB");
126 FGTestApi::setPositionAndStabilise(vabb->geod());
127
128
129 // set standard values
130 fgSetBool("/sim/freeze", false);
131 fgSetBool("/sim/sceneryloaded", true);
132 fgSetDouble("/sim/model-hz", 120.0);
133
134 timeManager->postinit();
135
136 // fake Unix time by setting this; it will then
137 // set the 'current unix time' passed to SGTime
138 const auto testDate = 314611200L;
139 fgSetInt("/sim/time/cur-time-override", testDate);
140
141 timeManager->update(0.0);
142
143 CPPUNIT_ASSERT_EQUAL((time_t)19800, globals->get_time_params()->get_local_offset());
144
145 auto gmt = globals->get_time_params()->getGmt();
146 CPPUNIT_ASSERT_EQUAL(79, gmt->tm_year);
147 CPPUNIT_ASSERT_EQUAL(11, gmt->tm_mon);
148 CPPUNIT_ASSERT_EQUAL(21, gmt->tm_mday);
149
150
151 // relocate to somewhere, check the time values update
152
153 auto eddf = FGAirport::getByIdent("EDDF");
154 FGTestApi::setPositionAndStabilise(eddf->geod());
155 timeManager->reposition();
156 timeManager->update(0.0);
157
158 CPPUNIT_ASSERT_EQUAL((time_t)3600, globals->get_time_params()->get_local_offset());
159
160
161 auto zbaa = FGAirport::getByIdent("ZBAA");
162 FGTestApi::setPositionAndStabilise(zbaa->geod());
163 timeManager->reposition();
164 timeManager->update(0.0);
165
166 CPPUNIT_ASSERT_EQUAL((time_t)28800, globals->get_time_params()->get_local_offset());
167 }
168
169 void TimeManagerTests::testETCTimeZones()
170 {
171 auto timeManager = globals->get_subsystem<TimeManager>();
172
173 auto phto = fgFindAirportID("PHTO");
174 FGTestApi::setPositionAndStabilise(phto->geod());
175
176 timeManager->postinit();
177
178 // fake Unix time by setting this; it will then
179 // set the 'current unix time' passed to SGTime
180 const auto testDate = 314611200L;
181 fgSetInt("/sim/time/cur-time-override", testDate);
182
183 FGTestApi::setPositionAndStabilise(phto->geod());
184 timeManager->reposition();
185 timeManager->update(0.0);
186
187 SGPropertyNode_ptr tzNameNode = fgGetNode("/sim/time/local-timezone", true);
188
189 CPPUNIT_ASSERT_EQUAL((time_t)-36000, globals->get_time_params()->get_local_offset());
190 CPPUNIT_ASSERT_EQUAL("Pacific/Honolulu"s, tzNameNode->getStringValue());
191
192 auto pilot = SGSharedPtr<FGTestApi::TestPilot>(new FGTestApi::TestPilot);
193 pilot->setSpeedKts(1000);
194 pilot->setCourseTrue(320.0);
195
196 bool ok = FGTestApi::runForTimeWithCheck(600.0, [tzNameNode]() {
197 const auto tz = tzNameNode->getStringValue();
198 return tz == "Etc/GMT+10"s;
199 });
200 CPPUNIT_ASSERT(ok);
201
202 ok = FGTestApi::runForTimeWithCheck(600.0, [tzNameNode]() {
203 const auto tz = tzNameNode->getStringValue();
204 return tz == "Pacific/Honolulu"s;
205 });
206 CPPUNIT_ASSERT(ok);
207 }
208
209 void TimeManagerTests::testSpecifyTimeOffset()
210 {
211 // disabled for now since this code depends on epehmeris as well
212 // to define sun position
213 return;
214
215 auto timeManager = globals->get_subsystem<TimeManager>();
216
217 // set standard values
218 fgSetBool("/sim/freeze", false);
219 fgSetBool("/sim/sceneryloaded", true);
220 fgSetDouble("/sim/model-hz", 120.0);
221
222 timeManager->postinit();
223
224 const auto testDate = 314611200L;
225 fgSetInt("/sim/time/cur-time-override", testDate);
226
227 auto uudd = fgFindAirportID("UUDD");
228 FGTestApi::setPositionAndStabilise(uudd->geod());
229
230 timeManager->setTimeOffset("dawn", 0);
231
232 timeManager->update(0.0);
233
234 auto localTime = globals->get_time_params()->get_cur_time();
235
236 CPPUNIT_ASSERT_EQUAL((time_t) 0, localTime);
237 }