From 193cece43049e27f72b28c1611e49bff403246e7 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Tue, 16 Aug 2016 11:02:01 +0200 Subject: [PATCH] tests --- test/CMakeLists.txt | 9 +++ test/uvw/async.cpp | 6 +- test/uvw/check.cpp | 6 +- test/uvw/idle.cpp | 8 +-- test/uvw/loop.cpp | 2 +- test/uvw/prepare.cpp | 8 +-- test/uvw/timer.cpp | 129 +++++++++++++++++++++++++++++++++++++++++++ test/uvw/work.cpp | 12 ++-- 8 files changed, 159 insertions(+), 21 deletions(-) create mode 100644 test/uvw/timer.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 7e84185f..b6648859 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -26,6 +26,7 @@ set(TARGET_CHECK check) set(TARGET_IDLE idle) set(TARGET_LOOP loop) set(TARGET_PREPARE prepare) +set(TARGET_TIMER timer) set(TARGET_WORK work) # Test TARGET_MAIN @@ -76,6 +77,14 @@ target_include_directories(${TARGET_PREPARE} PRIVATE ${COMMON_INCLUDE_DIRS}) target_link_libraries(${TARGET_PREPARE} PRIVATE ${COMMON_LINK_LIBS}) add_test(NAME ${TARGET_PREPARE} COMMAND ${TARGET_PREPARE}) +# Test TARGET_TIMER + +set(TARGET_TIMER_SOURCES uvw/timer.cpp) +add_executable(${TARGET_TIMER} ${TARGET_TIMER_SOURCES}) +target_include_directories(${TARGET_TIMER} PRIVATE ${COMMON_INCLUDE_DIRS}) +target_link_libraries(${TARGET_TIMER} PRIVATE ${COMMON_LINK_LIBS}) +add_test(NAME ${TARGET_TIMER} COMMAND ${TARGET_TIMER}) + # Test TARGET_WORK set(TARGET_WORK_SOURCES uvw/work.cpp) diff --git a/test/uvw/async.cpp b/test/uvw/async.cpp index ad0d1392..2396db4d 100644 --- a/test/uvw/async.cpp +++ b/test/uvw/async.cpp @@ -9,12 +9,12 @@ TEST(Async, Send) { bool checkErrorEvent = false; bool checkAsyncEvent = false; - handle->on([&checkErrorEvent](const auto &, auto &){ + handle->on([&checkErrorEvent](const auto &, auto &) { ASSERT_FALSE(checkErrorEvent); checkErrorEvent = true; }); - handle->on([&checkAsyncEvent](const auto &, auto &handle){ + handle->on([&checkAsyncEvent](const auto &, auto &handle) { ASSERT_FALSE(checkAsyncEvent); checkAsyncEvent = true; handle.close(); @@ -37,7 +37,7 @@ TEST(Async, Fake) { auto loop = uvw::Loop::getDefault(); auto handle = loop->resource(); - auto l = [](const auto &, auto &){ ASSERT_FALSE(true); }; + auto l = [](const auto &, auto &) { ASSERT_FALSE(true); }; handle->on(l); handle->on(l); diff --git a/test/uvw/check.cpp b/test/uvw/check.cpp index 0b89c5d9..45cdc2c4 100644 --- a/test/uvw/check.cpp +++ b/test/uvw/check.cpp @@ -9,12 +9,12 @@ TEST(Check, StartAndStop) { bool checkErrorEvent = false; bool checkCheckEvent = false; - handle->on([&checkErrorEvent](const auto &, auto &){ + handle->on([&checkErrorEvent](const auto &, auto &) { ASSERT_FALSE(checkErrorEvent); checkErrorEvent = true; }); - handle->on([&checkCheckEvent](const auto &, auto &handle){ + handle->on([&checkCheckEvent](const auto &, auto &handle) { ASSERT_FALSE(checkCheckEvent); checkCheckEvent = true; handle.stop(); @@ -38,7 +38,7 @@ TEST(Check, Fake) { auto loop = uvw::Loop::getDefault(); auto handle = loop->resource(); - auto l = [](const auto &, auto &){ ASSERT_FALSE(true); }; + auto l = [](const auto &, auto &) { ASSERT_FALSE(true); }; handle->on(l); handle->on(l); diff --git a/test/uvw/idle.cpp b/test/uvw/idle.cpp index e1c95448..33211379 100644 --- a/test/uvw/idle.cpp +++ b/test/uvw/idle.cpp @@ -9,12 +9,12 @@ TEST(Idle, StartAndStop) { bool checkErrorEvent = false; bool checkIdleEvent = false; - handle->on([&checkErrorEvent](const auto &, auto &){ + handle->on([&checkErrorEvent](const auto &, auto &) { ASSERT_FALSE(checkErrorEvent); checkErrorEvent = true; }); - handle->on([&checkIdleEvent](const auto &, auto &handle){ + handle->on([&checkIdleEvent](const auto &, auto &handle) { ASSERT_FALSE(checkIdleEvent); checkIdleEvent = true; handle.stop(); @@ -27,7 +27,7 @@ TEST(Idle, StartAndStop) { ASSERT_TRUE(handle->active()); ASSERT_FALSE(handle->closing()); - loop->run(); + loop->run(); ASSERT_FALSE(checkErrorEvent); ASSERT_TRUE(checkIdleEvent); @@ -38,7 +38,7 @@ TEST(Idle, Fake) { auto loop = uvw::Loop::getDefault(); auto handle = loop->resource(); - auto l = [](const auto &, auto &){ ASSERT_FALSE(true); }; + auto l = [](const auto &, auto &) { ASSERT_FALSE(true); }; handle->on(l); handle->on(l); diff --git a/test/uvw/loop.cpp b/test/uvw/loop.cpp index 1d2ab667..a91f345f 100644 --- a/test/uvw/loop.cpp +++ b/test/uvw/loop.cpp @@ -13,7 +13,7 @@ TEST(Loop, PartiallyDone) { auto loop = uvw::Loop::create(); auto handle = loop->resource(); - auto req = loop->resource([](){}); + auto req = loop->resource([]() {}); auto err = [](const auto &, auto &) { ASSERT_TRUE(false); }; diff --git a/test/uvw/prepare.cpp b/test/uvw/prepare.cpp index e71ae752..679f58e6 100644 --- a/test/uvw/prepare.cpp +++ b/test/uvw/prepare.cpp @@ -9,12 +9,12 @@ TEST(Prepare, StartAndStop) { bool checkErrorEvent = false; bool checkPrepareEvent = false; - handle->on([&checkErrorEvent](const auto &, auto &){ + handle->on([&checkErrorEvent](const auto &, auto &) { ASSERT_FALSE(checkErrorEvent); checkErrorEvent = true; }); - handle->on([&checkPrepareEvent](const auto &, auto &handle){ + handle->on([&checkPrepareEvent](const auto &, auto &handle) { ASSERT_FALSE(checkPrepareEvent); checkPrepareEvent = true; handle.stop(); @@ -27,7 +27,7 @@ TEST(Prepare, StartAndStop) { ASSERT_TRUE(handle->active()); ASSERT_FALSE(handle->closing()); - loop->run(); + loop->run(); ASSERT_FALSE(checkErrorEvent); ASSERT_TRUE(checkPrepareEvent); @@ -38,7 +38,7 @@ TEST(Prepare, Fake) { auto loop = uvw::Loop::getDefault(); auto handle = loop->resource(); - auto l = [](const auto &, auto &){ ASSERT_FALSE(true); }; + auto l = [](const auto &, auto &) { ASSERT_FALSE(true); }; handle->on(l); handle->on(l); diff --git a/test/uvw/timer.cpp b/test/uvw/timer.cpp new file mode 100644 index 00000000..7f9ffaf7 --- /dev/null +++ b/test/uvw/timer.cpp @@ -0,0 +1,129 @@ +#include +#include + + +TEST(Timer, StartAndStop) { + auto loop = uvw::Loop::getDefault(); + auto handleNoRepeat = loop->resource(); + auto handleRepeat = loop->resource(); + + bool checkErrorEvent = false; + bool checkTimerNoRepeatEvent = false; + bool checkTimerRepeatEvent = false; + + handleNoRepeat->on([&checkErrorEvent](const auto &, auto &) { + ASSERT_FALSE(checkErrorEvent); + checkErrorEvent = true; + }); + + handleRepeat->on([&checkErrorEvent](const auto &, auto &) { + ASSERT_FALSE(checkErrorEvent); + checkErrorEvent = true; + }); + + handleNoRepeat->on([&checkTimerNoRepeatEvent](const auto &, auto &handle) { + ASSERT_FALSE(checkTimerNoRepeatEvent); + checkTimerNoRepeatEvent = true; + handle.stop(); + handle.close(); + ASSERT_TRUE(handle.closing()); + }); + + handleRepeat->on([&checkTimerRepeatEvent](const auto &, auto &handle) { + if(checkTimerRepeatEvent) { + handle.stop(); + handle.close(); + ASSERT_TRUE(handle.closing()); + } else { + checkTimerRepeatEvent = true; + ASSERT_FALSE(handle.closing()); + } + }); + + handleNoRepeat->start(uvw::TimerHandle::Time{0}, uvw::TimerHandle::Time{0}); + handleRepeat->start(uvw::TimerHandle::Time{0}, uvw::TimerHandle::Time{1}); + + ASSERT_TRUE(handleNoRepeat->active()); + ASSERT_FALSE(handleNoRepeat->closing()); + + ASSERT_TRUE(handleRepeat->active()); + ASSERT_FALSE(handleRepeat->closing()); + + loop->run(); + + ASSERT_FALSE(checkErrorEvent); + ASSERT_TRUE(checkTimerNoRepeatEvent); + ASSERT_TRUE(checkTimerRepeatEvent); +} + + +TEST(Timer, Again) { + auto loop = uvw::Loop::getDefault(); + auto handle = loop->resource(); + + bool checkErrorEvent = false; + bool checkTimerEvent = true; + + handle->on([&checkErrorEvent](const auto &, auto &) { + ASSERT_FALSE(checkErrorEvent); + checkErrorEvent = true; + }); + + handle->on([&checkTimerEvent](const auto &, auto &handle) { + static bool guard = false; + + if(guard) { + handle.stop(); + handle.close(); + checkTimerEvent = true; + ASSERT_TRUE(handle.closing()); + } else { + guard = true; + handle.again(); + ASSERT_EQ(handle.repeat(), uvw::TimerHandle::Time{1}); + ASSERT_FALSE(handle.closing()); + } + }); + + ASSERT_NO_THROW(handle->again()); + ASSERT_FALSE(handle->active()); + ASSERT_TRUE(checkErrorEvent); + + checkErrorEvent = false; + handle->start(uvw::TimerHandle::Time{0}, uvw::TimerHandle::Time{1}); + + ASSERT_TRUE(handle->active()); + ASSERT_FALSE(handle->closing()); + + loop->run(); + + ASSERT_FALSE(checkErrorEvent); + ASSERT_TRUE(checkTimerEvent); +} + + +TEST(Timer, Repeat) { + auto loop = uvw::Loop::getDefault(); + auto handle = loop->resource(); + + ASSERT_NO_THROW(handle->repeat(uvw::TimerHandle::Time{42})); + ASSERT_EQ(handle->repeat(), uvw::TimerHandle::Time{42}); +} + + +TEST(Timer, Fake) { + auto loop = uvw::Loop::getDefault(); + auto handle = loop->resource(); + + auto l = [](const auto &, auto &) { ASSERT_FALSE(true); }; + handle->on(l); + handle->on(l); + + handle->start(uvw::TimerHandle::Time{0}, uvw::TimerHandle::Time{0}); + handle->close(); + + ASSERT_FALSE(handle->active()); + ASSERT_TRUE(handle->closing()); + + loop->run(); +} diff --git a/test/uvw/work.cpp b/test/uvw/work.cpp index 3f95960f..6cce150e 100644 --- a/test/uvw/work.cpp +++ b/test/uvw/work.cpp @@ -9,17 +9,17 @@ TEST(Work, RunTask) { bool checkWorkEvent = false; bool checkTask = false; - auto req = loop->resource([&checkTask](){ + auto req = loop->resource([&checkTask]() { ASSERT_FALSE(checkTask); checkTask = true; }); - req->on([&checkWorkEvent](const auto &, auto &){ + req->on([&checkWorkEvent](const auto &, auto &) { ASSERT_FALSE(checkWorkEvent); checkWorkEvent = true; }); - req->on([&checkErrorEvent](const auto &, auto &){ + req->on([&checkErrorEvent](const auto &, auto &) { ASSERT_FALSE(checkErrorEvent); checkErrorEvent = true; }); @@ -39,17 +39,17 @@ TEST(Work, Cancellation) { bool checkWorkEvent = false; bool checkTask = false; - auto req = loop->resource([&checkTask](){ + auto req = loop->resource([&checkTask]() { ASSERT_FALSE(checkTask); checkTask = true; }); - req->on([&checkWorkEvent](const auto &, auto &){ + req->on([&checkWorkEvent](const auto &, auto &) { ASSERT_FALSE(checkWorkEvent); checkWorkEvent = true; }); - req->on([&checkErrorEvent](const auto &, auto &){ + req->on([&checkErrorEvent](const auto &, auto &) { ASSERT_FALSE(checkErrorEvent); checkErrorEvent = true; });