| 1 | /* |
| 2 | * Copyright (C) 2016 Edward d'Auvergne |
| 3 | * |
| 4 | * This file is part of the program FlightGear. |
| 5 | * |
| 6 | * This program is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | |
| 21 | #include <algorithm> |
| 22 | #include <memory> |
| 23 | |
| 24 | #include "test_event.hxx" |
| 25 | |
| 26 | using namespace std; |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | SGEventMgr* global_eventManager = nullptr; |
| 31 | double global_realTime = 0.0; |
| 32 | |
| 33 | // class which behaves similarly to TimerObj in NasalSys |
| 34 | class FakeNasalTimer |
| 35 | { |
| 36 | public: |
| 37 | FakeNasalTimer(string n) : _name(n) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | void invoke() |
| 42 | { |
| 43 | ++_invokeCount; |
| 44 | |
| 45 | if (_expectedTime > 0.0) { |
| 46 | CPPUNIT_ASSERT_DOUBLES_EQUAL(_expectedTime, global_realTime, 0.1); |
| 47 | } |
| 48 | |
| 49 | // rescheudle base don the next time |
| 50 | if (!_rescheduleTimes.empty()) { |
| 51 | const auto t = _rescheduleTimes.front(); |
| 52 | _rescheduleTimes.pop_front(); |
| 53 | |
| 54 | // TimerObj does this, eugh |
| 55 | if (global_eventManager) { |
| 56 | global_eventManager->removeTask(_name); |
| 57 | start(t); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void start(double interval) |
| 63 | { |
| 64 | if (!global_eventManager) |
| 65 | return; |
| 66 | |
| 67 | global_eventManager->addTask(_name, [this](){ this->invoke(); }, |
| 68 | interval, interval /* delay */, false); |
| 69 | |
| 70 | _expectedTime = global_realTime + interval; |
| 71 | } |
| 72 | |
| 73 | string _name; |
| 74 | deque<double> _rescheduleTimes; |
| 75 | int _invokeCount = 0; |
| 76 | double _expectedTime = 0.0; |
| 77 | }; |
| 78 | |
| 79 | } // namespace |
| 80 | |
| 81 | // Set up function for each test. |
| 82 | void SimgearEventTests::setUp() |
| 83 | { |
| 84 | _eventManager = new SGEventMgr; |
| 85 | global_eventManager = _eventManager; |
| 86 | _eventManager->init(); |
| 87 | |
| 88 | _realTimeProp = new SGPropertyNode{}; |
| 89 | _eventManager->setRealtimeProperty(_realTimeProp); |
| 90 | } |
| 91 | |
| 92 | // Clean up after each test. |
| 93 | void SimgearEventTests::tearDown() |
| 94 | { |
| 95 | global_eventManager = nullptr; |
| 96 | } |
| 97 | |
| 98 | void SimgearEventTests::runForTestTime(double totalTime, double updateHz, double simTimeScaling) |
| 99 | { |
| 100 | const double dt = 1.0 / updateHz; |
| 101 | const int updateCount = static_cast<int>(totalTime * updateHz); |
| 102 | const double simDt = dt * simTimeScaling; |
| 103 | _realTimeProp->setDoubleValue(dt); |
| 104 | global_realTime = 0.0; |
| 105 | |
| 106 | for (int c = 0; c < updateCount; ++c) { |
| 107 | const double newRealTime = global_realTime + dt; |
| 108 | global_realTime = newRealTime; |
| 109 | _eventManager->update(simDt); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void SimgearEventTests::testTaskRescheduleDuringRun() |
| 114 | { |
| 115 | auto t1 = std::make_unique<FakeNasalTimer>("timer_aaa"); |
| 116 | t1->_rescheduleTimes.resize(10); |
| 117 | fill(t1->_rescheduleTimes.begin(), t1->_rescheduleTimes.end(), 0.5); |
| 118 | t1->_rescheduleTimes.push_back(30.0); // large final value |
| 119 | |
| 120 | global_realTime = 0.0; |
| 121 | t1->start(1.0); |
| 122 | runForTestTime(8.0, 20); |
| 123 | |
| 124 | CPPUNIT_ASSERT_EQUAL(11, t1->_invokeCount); |
| 125 | } |
| 126 | |
| 127 | void SimgearEventTests::testTaskRescheduleDuringRun2() |
| 128 | { |
| 129 | auto t1 = std::make_unique<FakeNasalTimer>("timer_bbb"); |
| 130 | const std::initializer_list<double> times = { |
| 131 | 0.02, 0.05, 0.02, 0.1, 0.02, 0.125, 0.05, 0.02}; |
| 132 | t1->_rescheduleTimes = times; |
| 133 | t1->_rescheduleTimes.push_back(30.0); // large final value |
| 134 | |
| 135 | global_realTime = 0.0; |
| 136 | t1->start(1.0); |
| 137 | |
| 138 | // running at 60Hz, check we don't miss any |
| 139 | runForTestTime(8.0, 60); |
| 140 | |
| 141 | CPPUNIT_ASSERT_EQUAL(9, t1->_invokeCount); |
| 142 | } |