Common/Event: Add unit tests
This commit is contained in:
parent
e7640d5367
commit
411fbe8416
|
@ -1,4 +1,5 @@
|
|||
add_executable(common-tests
|
||||
event_tests.cpp
|
||||
rectangle_tests.cpp
|
||||
)
|
||||
|
||||
|
|
|
@ -44,6 +44,7 @@
|
|||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="..\..\dep\googletest\src\gtest_main.cc" />
|
||||
<ClCompile Include="event_tests.cpp" />
|
||||
<ClCompile Include="rectangle_tests.cpp" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
|
|
|
@ -3,5 +3,6 @@
|
|||
<ItemGroup>
|
||||
<ClCompile Include="..\..\dep\googletest\src\gtest_main.cc" />
|
||||
<ClCompile Include="rectangle_tests.cpp" />
|
||||
<ClCompile Include="event_tests.cpp" />
|
||||
</ItemGroup>
|
||||
</Project>
|
|
@ -0,0 +1,79 @@
|
|||
#include "common/event.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
TEST(Event, InitialStateUnsignaled)
|
||||
{
|
||||
Common::Event e;
|
||||
ASSERT_FALSE(e.TryWait(1));
|
||||
}
|
||||
|
||||
TEST(Event, SignalOnSameThread)
|
||||
{
|
||||
Common::Event e;
|
||||
e.Signal();
|
||||
ASSERT_TRUE(e.TryWait(1));
|
||||
}
|
||||
|
||||
TEST(Event, SignalOnSecondThread)
|
||||
{
|
||||
Common::Event e;
|
||||
|
||||
std::thread thr([&e]() { e.Signal(); });
|
||||
e.Wait();
|
||||
thr.join();
|
||||
}
|
||||
|
||||
TEST(Event, SignalOnSecondThreadWithDelay)
|
||||
{
|
||||
Common::Event e;
|
||||
std::atomic_bool fl{false};
|
||||
|
||||
std::thread thr([&e, &fl]() {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
e.Signal();
|
||||
fl.store(true);
|
||||
});
|
||||
ASSERT_FALSE(fl.load());
|
||||
e.Wait();
|
||||
ASSERT_TRUE(fl.load());
|
||||
thr.join();
|
||||
}
|
||||
|
||||
TEST(Event, ResetAfterSignaling)
|
||||
{
|
||||
Common::Event e;
|
||||
|
||||
e.Signal();
|
||||
e.Reset();
|
||||
|
||||
ASSERT_FALSE(e.TryWait(1));
|
||||
}
|
||||
|
||||
TEST(Event, WaitForMultiple)
|
||||
{
|
||||
Common::Event e1, e2;
|
||||
e1.Signal();
|
||||
e2.Signal();
|
||||
|
||||
Common::Event* events[] = { &e1, &e2 };
|
||||
Common::Event::WaitForMultiple(events, countof(events));
|
||||
}
|
||||
|
||||
TEST(Event, AutoReset)
|
||||
{
|
||||
Common::Event e(true);
|
||||
e.Signal();
|
||||
ASSERT_TRUE(e.TryWait(1));
|
||||
ASSERT_FALSE(e.TryWait(1));
|
||||
}
|
||||
|
||||
TEST(Event, NoAutoReset)
|
||||
{
|
||||
Common::Event e(false);
|
||||
e.Signal();
|
||||
ASSERT_TRUE(e.TryWait(1));
|
||||
ASSERT_TRUE(e.TryWait(1));
|
||||
}
|
|
@ -50,7 +50,7 @@ void Event::Reset()
|
|||
ResetEvent(reinterpret_cast<HANDLE>(m_event_handle));
|
||||
}
|
||||
|
||||
void Event::WaitForMultipleEvents(Event** events, u32 num_events)
|
||||
void Event::WaitForMultiple(Event** events, u32 num_events)
|
||||
{
|
||||
DebugAssert(num_events > 0);
|
||||
|
||||
|
@ -120,7 +120,7 @@ void Event::Reset()
|
|||
;
|
||||
}
|
||||
|
||||
void Event::WaitForMultipleEvents(Event** events, u32 num_events)
|
||||
void Event::WaitForMultiple(Event** events, u32 num_events)
|
||||
{
|
||||
DebugAssert(num_events > 0);
|
||||
|
||||
|
@ -130,6 +130,9 @@ void Event::WaitForMultipleEvents(Event** events, u32 num_events)
|
|||
{
|
||||
pd.fd = events[i]->m_pipe_fds[0];
|
||||
poll(&pd, 1, -1);
|
||||
|
||||
if (events[i]->m_auto_reset)
|
||||
events[i]->Reset();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@ public:
|
|||
void Wait();
|
||||
bool TryWait(u32 timeout_in_ms);
|
||||
|
||||
static void WaitForMultipleEvents(Event** events, u32 num_events);
|
||||
static void WaitForMultiple(Event** events, u32 num_events);
|
||||
|
||||
private:
|
||||
#ifdef WIN32
|
||||
|
|
Loading…
Reference in New Issue