1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
/***********************************************************************
* ftimer-test.cpp - FTimer unit tests *
* *
* This file is part of the FINAL CUT widget toolkit *
* *
* Copyright 2022 Markus Gans *
* *
* FINAL CUT is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of *
* the License, or (at your option) any later version. *
* *
* FINAL CUT is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this program. If not, see *
* <http://www.gnu.org/licenses/>. *
***********************************************************************/
#include <chrono>
#include <thread>
#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/TestFixture.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TestRunner.h>
#include <final/final.h>
namespace test
{
//----------------------------------------------------------------------
// class FTimer_protected
//----------------------------------------------------------------------
class FTimer_protected : public finalcut::FObjectTimer
, public finalcut::FTimer<finalcut::FObject>
{
public:
// Using-declarations
using finalcut::FObjectTimer::addTimer;
using finalcut::FObjectTimer::delAllTimers;
using finalcut::FObjectTimer::delOwnTimers;
using finalcut::FObjectTimer::delTimer;
// Constructor
FTimer_protected() = default;
auto getTimerList() const -> finalcut::FTimer<finalcut::FObject>::FTimerList*
{
return finalcut::FObjectTimer::getTimerList();
}
auto processEvent() -> uInt
{
return finalcut::FObjectTimer::processTimerEvent();
}
void performTimerAction (finalcut::FObject*, finalcut::FEvent*) override
{
std::cout << ".";
fflush(stdout);
count++;
}
// Data member
uInt count{0};
};
//----------------------------------------------------------------------
class FObject_timer : public finalcut::FObject
{
public:
FObject_timer() = default;
auto getValue() const -> int
{
return value;
}
protected:
void onTimer (finalcut::FTimerEvent* ev) override
{
if ( ev->getTimerId() == 1 )
value++;
}
private:
// Data member
int value{0};
};
} // namespace test
//----------------------------------------------------------------------
// class FTimerTest
//----------------------------------------------------------------------
class FTimerTest : public CPPUNIT_NS::TestFixture
{
public:
FTimerTest() = default;
protected:
void classNameTest();
void timeTest();
void timerTest();
void performTimerActionTest();
private:
// Adds code needed to register the test suite
CPPUNIT_TEST_SUITE (FTimerTest);
// Add a methods to the test suite
CPPUNIT_TEST (classNameTest);
CPPUNIT_TEST (timeTest);
CPPUNIT_TEST (timerTest);
CPPUNIT_TEST (performTimerActionTest);
// End of test suite definition
CPPUNIT_TEST_SUITE_END();
};
//----------------------------------------------------------------------
void FTimerTest::classNameTest()
{
finalcut::FTimer<finalcut::FObject> t;
const finalcut::FString& classname = t.getClassName();
CPPUNIT_ASSERT ( classname == "FTimer" );
}
//----------------------------------------------------------------------
void FTimerTest::timeTest()
{
TimeValue time1{};
uInt64 timeout = 750000; // 750 ms
time1 = finalcut::FObjectTimer::getCurrentTime();
CPPUNIT_ASSERT ( ! finalcut::FObjectTimer::isTimeout (time1, timeout) );
sleep(1);
CPPUNIT_ASSERT ( finalcut::FObjectTimer::isTimeout (time1, timeout) );
time1 = TimeValue{}
+ std::chrono::seconds(300)
+ std::chrono::microseconds(2000000);
CPPUNIT_ASSERT ( finalcut::FObjectTimer::isTimeout (time1, timeout) );
}
//----------------------------------------------------------------------
void FTimerTest::timerTest()
{
using finalcut::operator +;
using finalcut::operator -;
using finalcut::operator +=;
using finalcut::operator <;
test::FTimer_protected t1;
test::FTimer_protected t2;
int id1, id2;
CPPUNIT_ASSERT ( t1.getTimerList()->empty() );
id1 = t1.addTimer(300);
CPPUNIT_ASSERT ( ! t1.getTimerList()->empty() );
CPPUNIT_ASSERT ( t1.getTimerList()->size() == 1 );
id2 = t1.addTimer(900);
CPPUNIT_ASSERT ( t1.getTimerList()->size() == 2 );
CPPUNIT_ASSERT ( &t1 != &t2 );
CPPUNIT_ASSERT ( id1 != id2 );
t1.delTimer (id1);
CPPUNIT_ASSERT ( t1.getTimerList()->size() == 1 );
t1.delTimer (id2);
CPPUNIT_ASSERT ( t1.getTimerList()->empty() );
CPPUNIT_ASSERT ( t1.getTimerList()->size() == 0 );
id1 = t1.addTimer(45);
id2 = t1.addTimer(95);
t1.delTimer (id2);
CPPUNIT_ASSERT ( t1.getTimerList()->size() == 1 );
t1.delTimer (id1);
CPPUNIT_ASSERT ( t1.getTimerList()->empty() );
CPPUNIT_ASSERT ( t1.getTimerList()->size() == 0 );
CPPUNIT_ASSERT ( ! t1.delTimer (id1) ); // id double delete
CPPUNIT_ASSERT ( ! t1.delAllTimers() );
t1.addTimer(250);
t1.addTimer(500);
t2.addTimer(750);
t2.addTimer(1000);
CPPUNIT_ASSERT_EQUAL ( t1.getTimerList(), t2.getTimerList() );
CPPUNIT_ASSERT ( t1.getTimerList()->size() == 4 );
CPPUNIT_ASSERT ( t2.getTimerList()->size() == 4 );
t1.delOwnTimers();
CPPUNIT_ASSERT ( t1.getTimerList()->size() == 2 );
CPPUNIT_ASSERT ( t2.getTimerList()->size() == 2 );
t1.addTimer(250);
CPPUNIT_ASSERT ( t1.getTimerList()->size() == 3 );
CPPUNIT_ASSERT ( t2.getTimerList()->size() == 3 );
t2.delAllTimers();
CPPUNIT_ASSERT ( t1.getTimerList()->empty() );
CPPUNIT_ASSERT ( t2.getTimerList()->empty() );
CPPUNIT_ASSERT ( t1.getTimerList()->size() == 0 );
CPPUNIT_ASSERT ( t2.getTimerList()->size() == 0 );
auto tv1 = TimeValue{} + std::chrono::seconds(1321006271);
auto tv2 = TimeValue{} + std::chrono::seconds(27166271);
auto tv2_duration = std::chrono::duration_cast<std::chrono::seconds>(tv2.time_since_epoch());
auto tv_sum = tv1 + tv2_duration;
auto sec_sum = std::chrono::duration_cast<std::chrono::seconds>(tv_sum.time_since_epoch()).count();
CPPUNIT_ASSERT ( sec_sum == 1348172542 );
auto tv_difference = tv1 - tv2_duration;
auto sec_difference = std::chrono::duration_cast<std::chrono::seconds>(tv_difference.time_since_epoch()).count();
CPPUNIT_ASSERT ( sec_difference == 1293840000 );
tv_sum += tv2_duration;
sec_sum = std::chrono::duration_cast<std::chrono::seconds>(tv_sum.time_since_epoch()).count();
CPPUNIT_ASSERT ( sec_sum == 1375338813 );
CPPUNIT_ASSERT ( tv2 < tv1 );
CPPUNIT_ASSERT ( ! (tv1 < tv2) );
CPPUNIT_ASSERT ( tv1 < tv_sum );
CPPUNIT_ASSERT ( ! (tv_sum < tv1) );
CPPUNIT_ASSERT ( tv2 < tv_sum );
CPPUNIT_ASSERT ( ! (tv_sum < tv2) );
CPPUNIT_ASSERT ( tv_difference < tv_sum );
CPPUNIT_ASSERT ( ! (tv_sum < tv_difference) );
tv1 += std::chrono::microseconds(600000);
tv2 += std::chrono::microseconds(600000);
tv2_duration = std::chrono::duration_cast<std::chrono::seconds>(tv2.time_since_epoch());
tv_sum = tv1 + tv2_duration;
auto s_sum = std::chrono::duration_cast<std::chrono::seconds>(tv_sum.time_since_epoch()).count();
CPPUNIT_ASSERT ( s_sum == 1348172542 );
auto us_sum = ( std::chrono::duration_cast<std::chrono::microseconds>(tv_sum.time_since_epoch())
- std::chrono::seconds(1348172542) ).count();
CPPUNIT_ASSERT ( us_sum == 600000 );
auto tv1_sec = std::chrono::duration_cast<std::chrono::seconds>(tv1.time_since_epoch());
auto tv2_sec = std::chrono::duration_cast<std::chrono::seconds>(tv2.time_since_epoch());
tv1 = TimeValue{} + tv1_sec + std::chrono::microseconds(654321);
tv2 = TimeValue{} + tv2_sec + std::chrono::microseconds(123456);
auto tv2_duration_ms = std::chrono::duration_cast<std::chrono::microseconds>(tv2.time_since_epoch());
tv_difference = tv1 - tv2_duration_ms;
sec_difference = std::chrono::duration_cast<std::chrono::seconds>(tv_difference.time_since_epoch()).count();
CPPUNIT_ASSERT ( sec_difference == 1293840000 );
auto usec_difference = ( std::chrono::duration_cast<std::chrono::microseconds>(tv_difference.time_since_epoch())
- std::chrono::seconds(1293840000) ).count();
CPPUNIT_ASSERT ( usec_difference == 530865 );
tv2_sec = std::chrono::duration_cast<std::chrono::seconds>(tv2.time_since_epoch());
tv2 = TimeValue{} + tv2_sec + std::chrono::microseconds(999888);
auto tv2_duration2 = std::chrono::duration_cast<std::chrono::microseconds>(tv2.time_since_epoch());
tv_sum += tv2_duration2;
s_sum = std::chrono::duration_cast<std::chrono::seconds>(tv_sum.time_since_epoch()).count();
CPPUNIT_ASSERT ( s_sum == 1375338814 );
us_sum = ( std::chrono::duration_cast<std::chrono::microseconds>(tv_sum.time_since_epoch())
- std::chrono::seconds(1375338814) ).count();
CPPUNIT_ASSERT ( us_sum == 599888 );
CPPUNIT_ASSERT ( tv2 < tv1 );
CPPUNIT_ASSERT ( ! (tv1 < tv2) );
CPPUNIT_ASSERT ( tv_difference < tv_sum );
CPPUNIT_ASSERT ( ! (tv_sum < tv_difference) );
CPPUNIT_ASSERT ( ! t1.delTimer(0) );
CPPUNIT_ASSERT ( ! t1.delTimer(-1) );
}
//----------------------------------------------------------------------
void FTimerTest::performTimerActionTest()
{
test::FTimer_protected t1;
uInt num_events = 0;
uInt loop = 0;
t1.addTimer(100);
while ( loop < 10 )
{
num_events += t1.processEvent();
// Wait 100 ms
std::this_thread::sleep_for(std::chrono::milliseconds(100));
loop++;
}
CPPUNIT_ASSERT ( loop == 10 );
CPPUNIT_ASSERT ( num_events == 9 );
CPPUNIT_ASSERT ( t1.count == 9 );
test::FObject_timer t2;
CPPUNIT_ASSERT ( t2.getValue() == 0 );
finalcut::FTimerEvent timer_ev (finalcut::Event::Timer, 1);
for (auto x = 0; x < 10; x++)
finalcut::FApplication::sendEvent (&t2, &timer_ev);
CPPUNIT_ASSERT ( t2.getValue() == 10 );
}
// Put the test suite in the registry
CPPUNIT_TEST_SUITE_REGISTRATION (FTimerTest);
// The general unit test main part
#include <main-test.inc>
|