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 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
|
#include "gtest/gtest.h"
#include <array>
#include <chrono>
#include <thread>
#include <future>
#include <numeric> // std::accumulate
#include <regex> // Used in FormatTime test
#include "sync.h"
#include "common.h"
// This test set requires support for C++14
// * Uses "'" as a separator: 100'000
// * Uses operator"ms" at al from chrono
using namespace std;
using namespace srt::sync;
TEST(SyncDuration, BasicChecks)
{
const steady_clock::duration d = steady_clock::duration();
EXPECT_EQ(d.count(), 0);
EXPECT_TRUE(d == d); // operator==
EXPECT_FALSE(d != d); // operator!=
EXPECT_EQ(d, steady_clock::duration::zero());
EXPECT_EQ(d, microseconds_from(0));
EXPECT_EQ(d, milliseconds_from(0));
EXPECT_EQ(d, seconds_from(0));
EXPECT_EQ(count_milliseconds(d), 0);
EXPECT_EQ(count_microseconds(d), 0);
EXPECT_EQ(count_seconds(d), 0);
const steady_clock::duration a = d + milliseconds_from(120);
EXPECT_EQ(a, milliseconds_from(120));
EXPECT_EQ(count_milliseconds(a), 120);
EXPECT_EQ(count_microseconds(a), 120000);
EXPECT_EQ(count_seconds(a), 0);
}
/// Check operations on (uint32_t + 1)
TEST(SyncDuration, DurationFrom)
{
const int64_t val = int64_t(numeric_limits<uint32_t>::max()) + 1;
const steady_clock::duration us_from = microseconds_from(val);
EXPECT_EQ(count_microseconds(us_from), val);
const steady_clock::duration ms_from = milliseconds_from(val);
EXPECT_EQ(count_milliseconds(ms_from), val);
const steady_clock::duration s_from = seconds_from(val);
EXPECT_EQ(count_seconds(s_from), val);
}
TEST(SyncDuration, RelOperators)
{
const steady_clock::duration a = steady_clock::duration();
EXPECT_EQ(a.count(), 0);
EXPECT_TRUE(a == a); // operator==
EXPECT_FALSE(a != a); // operator!=
EXPECT_FALSE(a > a); // operator>
EXPECT_FALSE(a < a); // operator<
EXPECT_TRUE(a <= a); // operator<=
EXPECT_TRUE(a >= a); // operator>=
const steady_clock::duration b = a + milliseconds_from(120);
EXPECT_FALSE(b == a); // operator==
EXPECT_TRUE(b != a); // operator!=
EXPECT_TRUE(b > a); // operator>
EXPECT_FALSE(a > b); // operator>
EXPECT_FALSE(b < a); // operator<
EXPECT_TRUE(a < b); // operator<
EXPECT_FALSE(b <= a); // operator<=
EXPECT_TRUE(a <= b); // operator<=
EXPECT_TRUE(b >= a); // operator>=
EXPECT_FALSE(a >= b); // operator>=
const steady_clock::duration c = steady_clock::duration(numeric_limits<int64_t>::max());
EXPECT_EQ(c.count(), numeric_limits<int64_t>::max());
const steady_clock::duration d = steady_clock::duration(numeric_limits<int64_t>::min());
EXPECT_EQ(d.count(), numeric_limits<int64_t>::min());
}
TEST(SyncDuration, OperatorMinus)
{
const steady_clock::duration a = seconds_from(5);
const steady_clock::duration b = milliseconds_from(3500);
EXPECT_EQ(count_milliseconds(a - b), 1500);
EXPECT_EQ(count_milliseconds(b - a), -1500);
EXPECT_EQ((a - a).count(), 0);
}
TEST(SyncDuration, OperatorMinusEq)
{
const steady_clock::duration a = seconds_from(5);
const steady_clock::duration b = milliseconds_from(3500);
steady_clock::duration c = a;
EXPECT_EQ(c, a);
c -= b;
EXPECT_EQ(count_milliseconds(c), 1500);
c = b;
EXPECT_EQ(c, b);
c -= a;
EXPECT_EQ(count_milliseconds(c), -1500);
}
TEST(SyncDuration, OperatorPlus)
{
const steady_clock::duration a = seconds_from(5);
const steady_clock::duration b = milliseconds_from(3500);
EXPECT_EQ(count_milliseconds(a + b), 8500);
EXPECT_EQ(count_milliseconds(b + a), 8500);
}
TEST(SyncDuration, OperatorPlusEq)
{
const steady_clock::duration a = seconds_from(5);
const steady_clock::duration b = milliseconds_from(3500);
steady_clock::duration c = a;
EXPECT_EQ(c, a);
c += b;
EXPECT_EQ(count_milliseconds(c), 8500);
c = b;
EXPECT_EQ(c, b);
c += a;
EXPECT_EQ(count_milliseconds(c), 8500);
}
TEST(SyncDuration, OperatorMultInt)
{
const steady_clock::duration a = milliseconds_from(3500);
EXPECT_EQ(count_milliseconds(a), 3500);
EXPECT_EQ(count_milliseconds(a * 2), 7000);
}
TEST(SyncDuration, OperatorMultIntEq)
{
steady_clock::duration a = milliseconds_from(3500);
EXPECT_EQ(count_milliseconds(a), 3500);
a *= 2;
EXPECT_EQ(count_milliseconds(a), 7000);
}
TEST(SyncRandom, GenRandomInt)
{
array<size_t, 64> mn = {};
// Check generated values are in the specified range.
const size_t n = 2048;
for (size_t i = 0; i < n; ++i)
{
const int rand_val = genRandomInt(0, int(mn.size()) - 1);
ASSERT_GE(rand_val, 0);
ASSERT_LT(rand_val, (int) mn.size());
++mn[rand_val];
}
// Check the distribution is more or less uniform.
// 100% uniform if each value is generated (n / (2 * mn.size())) times.
// We expect at least half of that value for a random uniform distribution.
ASSERT_GT(n / (2 * mn.size()), 4u);
const size_t min_value = n / (2 * mn.size()) - 4u; // Subtracting 4 to tolerate possible deviations.
for (size_t i = 0; i < mn.size(); ++i)
{
EXPECT_GE(mn[i], min_value) << "i=" << i << ". Ok-ish if the count is non-zero.";
}
// Uncomment to see the distribution.
//cout << "min value: " << min_value << endl;
//for (size_t i = 0; i < mn.size(); ++i)
//{
// cout << i << '\t';
// for (int j=0; j<mn[i]; ++j) cout << '*';
// cout << '\n';
//}
// Check INT32_MAX
for (size_t i = 0; i < n; ++i)
{
const int rand_val = genRandomInt(INT32_MAX - 1, INT32_MAX);
EXPECT_GE(rand_val, INT32_MAX - 1);
EXPECT_LE(rand_val, INT32_MAX);
}
}
/*****************************************************************************/
/*
* TimePoint tests
*/
/*****************************************************************************/
TEST(SyncTimePoint, DefaultConstructorZero)
{
steady_clock::time_point a;
EXPECT_TRUE(is_zero(a));
}
TEST(SyncTimePoint, RelOperators)
{
const steady_clock::time_point a(steady_clock::time_point::max());
const steady_clock::time_point b(steady_clock::time_point::min());
EXPECT_TRUE(a == a);
EXPECT_FALSE(a == b);
EXPECT_TRUE(a != b);
EXPECT_TRUE(a != b);
EXPECT_TRUE(a >= a);
EXPECT_FALSE(b >= a);
EXPECT_TRUE(a > b);
EXPECT_FALSE(a > a);
EXPECT_TRUE(a <= a);
EXPECT_TRUE(b <= a);
EXPECT_FALSE(a <= b);
EXPECT_FALSE(a < a);
EXPECT_TRUE(b < a);
EXPECT_FALSE(a < b);
}
#ifndef ENABLE_STDCXX_SYNC
TEST(SyncTimePoint, OperatorMinus)
{
const int64_t delta = 1024;
const steady_clock::time_point a(numeric_limits<uint64_t>::max());
const steady_clock::time_point b(numeric_limits<uint64_t>::max() - delta);
EXPECT_EQ((a - b).count(), delta);
EXPECT_EQ((b - a).count(), -delta);
}
TEST(SyncTimePoint, OperatorEq)
{
const int64_t delta = 1024;
const steady_clock::time_point a(numeric_limits<uint64_t>::max() - delta);
const steady_clock::time_point b = a;
EXPECT_EQ(a, b);
}
TEST(SyncTimePoint, OperatorMinusPlusDuration)
{
const int64_t delta = 1024;
const steady_clock::time_point a(numeric_limits<uint64_t>::max());
const steady_clock::time_point b(numeric_limits<uint64_t>::max() - delta);
EXPECT_EQ((a + steady_clock::duration(-delta)), b);
EXPECT_EQ((b + steady_clock::duration(+delta)), a);
EXPECT_EQ((a - steady_clock::duration(+delta)), b);
EXPECT_EQ((b - steady_clock::duration(-delta)), a);
}
TEST(SyncTimePoint, OperatorPlusEqDuration)
{
const int64_t delta = 1024;
const steady_clock::time_point a(numeric_limits<uint64_t>::max());
const steady_clock::time_point b(numeric_limits<uint64_t>::max() - delta);
steady_clock::time_point r = a;
EXPECT_EQ(r, a);
r += steady_clock::duration(-delta);
EXPECT_EQ(r, b);
r = b;
EXPECT_EQ(r, b);
r += steady_clock::duration(+delta);
EXPECT_EQ(r, a);
r = a;
EXPECT_EQ(r, a);
r -= steady_clock::duration(+delta);
EXPECT_EQ((a - steady_clock::duration(+delta)), b);
EXPECT_EQ((b - steady_clock::duration(-delta)), a);
}
TEST(SyncTimePoint, OperatorMinusEqDuration)
{
const int64_t delta = 1024;
const steady_clock::time_point a(numeric_limits<uint64_t>::max());
const steady_clock::time_point b(numeric_limits<uint64_t>::max() - delta);
steady_clock::time_point r = a;
EXPECT_EQ(r, a);
r -= steady_clock::duration(+delta);
EXPECT_EQ(r, b);
r = b;
EXPECT_EQ(r, b);
r -= steady_clock::duration(-delta);
EXPECT_EQ(r, a);
}
#endif
/*****************************************************************************/
/*
* UniqueLock tests
*/
/*****************************************************************************/
TEST(SyncUniqueLock, LockUnlock)
{
Mutex mtx;
UniqueLock lock(mtx);
EXPECT_FALSE(mtx.try_lock());
lock.unlock();
EXPECT_TRUE(mtx.try_lock());
mtx.unlock();
lock.lock();
EXPECT_FALSE(mtx.try_lock());
}
TEST(SyncUniqueLock, Scope)
{
Mutex mtx;
{
UniqueLock lock(mtx);
EXPECT_FALSE(mtx.try_lock());
}
EXPECT_TRUE(mtx.try_lock());
mtx.unlock();
}
/*****************************************************************************/
/*
* SyncEvent tests
*/
/*****************************************************************************/
TEST(SyncEvent, WaitFor)
{
Mutex mutex;
Condition cond;
cond.init();
for (int timeout_us : {50, 100, 500, 1000, 101000, 1001000})
{
const steady_clock::duration timeout = microseconds_from(timeout_us);
UniqueLock lock(mutex);
const steady_clock::time_point start = steady_clock::now();
const bool on_timeout = !cond.wait_for(lock, timeout);
const steady_clock::time_point stop = steady_clock::now();
const steady_clock::duration waittime = stop - start;
const int64_t waittime_us = count_microseconds(waittime);
#if defined(ENABLE_STDCXX_SYNC) || !defined(_WIN32)
// This check somehow fails on AppVeyor Windows VM with VS 2015 and pthreads.
// - SyncEvent::wait_for( 50us) took 6us
// - SyncEvent::wait_for(100us) took 4us
if (on_timeout) {
const int tolerance = timeout_us/1000;
EXPECT_GE(waittime_us, timeout_us - tolerance);
}
#endif
if (on_timeout) {
// Give it 100 times the timeout, as this is
// considered more than "crazy long", whereas we only
// want to check if it has waited a finite amount of time.
EXPECT_LE(waittime_us, 10 * 1001000); // biggest wait value
}
string spurious = on_timeout ? "" : " (SPURIOUS)";
if (timeout_us < 1000)
{
cerr << "SyncEvent::wait_for(" << timeout_us << "us) took "
<< waittime_us << "us" << spurious << endl;
}
else
{
cerr << "SyncEvent::wait_for(" << count_milliseconds(timeout) << " ms) took "
<< (waittime_us / 1000.0) << " ms" << spurious << endl;
}
}
cond.destroy();
}
TEST(SyncEvent, WaitForNotifyOne)
{
Mutex mutex;
Condition cond;
cond.init();
const steady_clock::duration timeout = seconds_from(5);
auto wait_async = [](Condition* cv, Mutex* m, const steady_clock::duration& tmo) {
CUniqueSync cc (*m, *cv);
return cc.wait_for(tmo);
};
auto wait_async_res = async(launch::async, wait_async, &cond, &mutex, timeout);
EXPECT_EQ(wait_async_res.wait_for(chrono::milliseconds(100)), future_status::timeout);
cond.notify_one();
ASSERT_EQ(wait_async_res.wait_for(chrono::milliseconds(100)), future_status::ready);
const bool wait_for_res = wait_async_res.get();
EXPECT_TRUE(wait_for_res) << "Woken up by a notification";
cond.destroy();
}
TEST(SyncEvent, WaitNotifyOne)
{
Mutex mutex;
Condition cond;
cond.init();
auto wait_async = [](Condition* cv, Mutex* m) {
UniqueLock lock(*m);
return cv->wait(lock);
};
auto wait_async_res = async(launch::async, wait_async, &cond, &mutex);
EXPECT_EQ(wait_async_res.wait_for(chrono::milliseconds(100)), future_status::timeout);
cond.notify_one();
ASSERT_EQ(wait_async_res.wait_for(chrono::milliseconds(100)), future_status::ready);
wait_async_res.get();
cond.destroy();
}
TEST(SyncEvent, WaitForTwoNotifyAll)
{
Mutex mutex;
Condition cond;
cond.init();
const steady_clock::duration timeout = seconds_from(3);
auto wait_async = [](Condition* cv, Mutex* m, const steady_clock::duration& tmo) {
UniqueLock lock(*m);
return cv->wait_for(lock, tmo);
};
auto wait_async1_res = async(launch::async, wait_async, &cond, &mutex, timeout);
auto wait_async2_res = async(launch::async, wait_async, &cond, &mutex, timeout);
EXPECT_EQ(wait_async1_res.wait_for(chrono::milliseconds(100)), future_status::timeout);
EXPECT_EQ(wait_async2_res.wait_for(chrono::milliseconds(100)), future_status::timeout);
cond.notify_all();
// Now only one waiting thread should become ready
const future_status status1 = wait_async1_res.wait_for(chrono::milliseconds(100));
const future_status status2 = wait_async2_res.wait_for(chrono::milliseconds(100));
EXPECT_EQ(status1, future_status::ready);
EXPECT_EQ(status2, future_status::ready);
// Expect both threads to wake up by condition
EXPECT_TRUE(wait_async1_res.get());
EXPECT_TRUE(wait_async2_res.get());
cond.destroy();
}
TEST(SyncEvent, WaitForNotifyAll)
{
Mutex mutex;
Condition cond;
cond.init();
const steady_clock::duration timeout = seconds_from(5);
auto wait_async = [](Condition* cv, Mutex* m, const steady_clock::duration& tmo) {
UniqueLock lock(*m);
return cv->wait_for(lock, tmo);
};
auto wait_async_res = async(launch::async, wait_async, &cond, &mutex, timeout);
EXPECT_EQ(wait_async_res.wait_for(chrono::milliseconds(500)), future_status::timeout);
cond.notify_all();
ASSERT_EQ(wait_async_res.wait_for(chrono::milliseconds(500)), future_status::ready);
const bool wait_for_res = wait_async_res.get();
EXPECT_TRUE(wait_for_res) << "Woken up by condition";
cond.destroy();
}
/*****************************************************************************/
/*
* CThread
*/
/*****************************************************************************/
void* dummythread(void* param)
{
auto& thread_finished = *(srt::sync::atomic<bool>*)param;
thread_finished = true;
return nullptr;
}
TEST(SyncThread, Joinable)
{
CThread foo;
srt::sync::atomic<bool> thread_finished;
StartThread(foo, dummythread, (void*)&thread_finished, "DumyThread");
EXPECT_TRUE(foo.joinable());
while (!thread_finished)
{
std::this_thread::sleep_for(chrono::milliseconds(50));
}
EXPECT_TRUE(foo.joinable());
foo.join();
EXPECT_FALSE(foo.joinable());
}
/*****************************************************************************/
/*
* SharedMutex
*/
/*****************************************************************************/
TEST(SharedMutex, LockWriteRead)
{
SharedMutex mut;
mut.lock();
EXPECT_FALSE(mut.try_lock_shared());
}
TEST(SharedMutex, LockReadWrite)
{
SharedMutex mut;
mut.lock_shared();
EXPECT_FALSE(mut.try_lock());
}
TEST(SharedMutex, LockReadTwice)
{
SharedMutex mut;
mut.lock_shared();
mut.lock_shared();
EXPECT_TRUE(mut.try_lock_shared());
}
TEST(SharedMutex, LockWriteTwice)
{
SharedMutex mut;
mut.lock();
EXPECT_FALSE(mut.try_lock());
}
TEST(SharedMutex, LockUnlockWrite)
{
SharedMutex mut;
mut.lock();
EXPECT_FALSE(mut.try_lock());
mut.unlock();
EXPECT_TRUE(mut.try_lock());
}
TEST(SharedMutex, LockUnlockRead)
{
SharedMutex mut;
mut.lock_shared();
EXPECT_FALSE(mut.try_lock());
mut.unlock_shared();
EXPECT_TRUE(mut.try_lock());
}
TEST(SharedMutex, LockedReadCount)
{
SharedMutex mut;
int count = 0;
mut.lock_shared();
count++;
ASSERT_EQ(mut.getReaderCount(), count);
mut.lock_shared();
count++;
ASSERT_EQ(mut.getReaderCount(), count);
mut.unlock_shared();
count--;
ASSERT_EQ(mut.getReaderCount(), count);
mut.unlock_shared();
count--;
ASSERT_EQ(mut.getReaderCount(), count);
EXPECT_TRUE(mut.try_lock());
}
/*****************************************************************************/
/*
* FormatTime
*/
/*****************************************************************************/
#if !defined(__GNUC__) || defined(__clang__) || (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9))
//#if !defined(__GNUC__) || (__GNUC__ > 4)
//#if !defined(__GNUC__) || (__GNUC__ >= 5)
// g++ before 4.9 (?) does not support regex and crashes on execution.
TEST(Sync, FormatTime)
{
auto parse_time = [](const string& timestr) -> long long {
// Example string: 1D 02:10:55.972651 [STD]
const regex rex("([[:digit:]]+D )?([[:digit:]]{2}):([[:digit:]]{2}):([[:digit:]]{2}).([[:digit:]]{6,}) \\[STDY\\]");
std::smatch sm;
EXPECT_TRUE(regex_match(timestr, sm, rex));
EXPECT_LE(sm.size(), 6U);
if (sm.size() != 6 && sm.size() != 5)
return 0;
// Day may be missing if zero
const long long d = sm[1].matched ? std::stoi(sm[1]) : 0;
const long long h = std::stoll(sm[2]);
const long long m = std::stoll(sm[3]);
const long long s = std::stoll(sm[4]);
const long long u = std::stoll(sm[5]);
return u + s * 1000000 + m * 60000000 + h * 60 * 60 * 1000000 + d * 24 * 60 * 60 * 1000000;
};
auto print_timediff = [&parse_time](const string& desc, const string& time, const string& time_base) {
const long long diff = parse_time(time) - parse_time(time_base);
cerr << desc << time << " (" << diff << " us)" << endl;
};
const auto a = steady_clock::now();
const string time1 = FormatTime(a);
const string time2 = FormatTime(a);
const string time3 = FormatTime(a + milliseconds_from(500));
const string time4 = FormatTime(a + seconds_from(1));
const string time5 = FormatTime(a + seconds_from(5));
const string time6 = FormatTime(a + milliseconds_from(-4350));
cerr << "Current time formated: " << time1 << endl;
const long long diff_2_1 = parse_time(time2) - parse_time(time1);
cerr << "Same time formated again: " << time2 << " (" << diff_2_1 << " us)" << endl;
print_timediff("Same time formated again: ", time2, time1);
print_timediff("Time +500 ms formated: ", time3, time1);
print_timediff("Time +1 sec formated: ", time4, time1);
print_timediff("Time +5 sec formated: ", time5, time1);
print_timediff("Time -4350 ms formated: ", time6, time1);
EXPECT_TRUE(time1 == time2);
}
TEST(Sync, FormatTimeSys)
{
auto parse_time = [](const string& timestr) -> long long {
const regex rex("([[:digit:]]{2}):([[:digit:]]{2}):([[:digit:]]{2}).([[:digit:]]{6}) \\[SYST\\]");
std::smatch sm;
EXPECT_TRUE(regex_match(timestr, sm, rex));
EXPECT_EQ(sm.size(), 5U);
if (sm.size() != 5)
return 0;
const long long h = std::stoi(sm[1]);
const long long m = std::stoi(sm[2]);
const long long s = std::stoi(sm[3]);
const long long u = std::stoi(sm[4]);
return u + s * 1000000 + m * 60000000 + h * 60 * 60 * 1000000;
};
auto print_timediff = [&parse_time](const string& desc, const string& time, const string& time_base) {
const long long diff = parse_time(time) - parse_time(time_base);
cerr << desc << time << " (" << diff << " us)" << endl;
};
const steady_clock::time_point a = steady_clock::now();
const string time1 = FormatTimeSys(a);
const string time2 = FormatTimeSys(a);
const string time3 = FormatTimeSys(a + milliseconds_from(500));
const string time4 = FormatTimeSys(a + seconds_from(1));
const string time5 = FormatTimeSys(a + seconds_from(5));
const string time6 = FormatTimeSys(a + milliseconds_from(-4350));
cerr << "Current time formated: " << time1 << endl;
const long long diff_2_1 = parse_time(time2) - parse_time(time1);
cerr << "Same time formated again: " << time2 << " (" << diff_2_1 << " us)" << endl;
print_timediff("Same time formated again: ", time2, time1);
print_timediff("Time +500 ms formated: ", time3, time1);
print_timediff("Time +1 sec formated: ", time4, time1);
print_timediff("Time +5 sec formated: ", time5, time1);
print_timediff("Time -4350 ms formated: ", time6, time1);
EXPECT_TRUE(time1 == time2);
}
#endif
|