diff options
Diffstat (limited to 'test/utest.cpp')
-rw-r--r-- | test/utest.cpp | 100 |
1 files changed, 51 insertions, 49 deletions
diff --git a/test/utest.cpp b/test/utest.cpp index a023cde..5fb870f 100644 --- a/test/utest.cpp +++ b/test/utest.cpp @@ -1,63 +1,65 @@ -#include <iostream> +#include "evdev.hpp" +#include "monitor.hpp" + +#include <linux/input.h> #include <sys/types.h> + #include <chrono> +#include <iostream> #include <string> -#include <linux/input.h> + #include <gtest/gtest.h> -#include "monitor.hpp" -#include "evdev.hpp" using namespace phosphor::gpio; // Exit helper. Ideally should be class but need // this to be used inside a static method. -bool completed {}; +bool completed{}; class GpioTest : public ::testing::Test { - public: - static constexpr auto DEVICE = "/tmp/test_fifo"; + public: + static constexpr auto DEVICE = "/tmp/test_fifo"; - // systemd event handler - sd_event* events; + // systemd event handler + sd_event* events; - // Really needed just for the constructor - decltype(input_event::code) code = 10; + // Really needed just for the constructor + decltype(input_event::code) code = 10; - // Really needed just for the constructor - decltype(input_event::value) value = 10; + // Really needed just for the constructor + decltype(input_event::value) value = 10; - // Need this so that events can be initialized. - int rc; + // Need this so that events can be initialized. + int rc; - // Gets called as part of each TEST_F construction - GpioTest() - : rc(sd_event_default(&events)) - { - // Check for successful creation of event handler - EXPECT_GE(rc, 0); + // Gets called as part of each TEST_F construction + GpioTest() : rc(sd_event_default(&events)) + { + // Check for successful creation of event handler + EXPECT_GE(rc, 0); - // FIFO created to simulate data available - EXPECT_EQ(0, mknod(DEVICE, S_IFIFO|0666, 0)); - } + // FIFO created to simulate data available + EXPECT_EQ(0, mknod(DEVICE, S_IFIFO | 0666, 0)); + } - // Gets called as part of each TEST_F destruction - ~GpioTest() - { - EXPECT_EQ(0, remove(DEVICE)); + // Gets called as part of each TEST_F destruction + ~GpioTest() + { + EXPECT_EQ(0, remove(DEVICE)); - events = sd_event_unref(events); - EXPECT_EQ(events, nullptr); - } + events = sd_event_unref(events); + EXPECT_EQ(events, nullptr); + } - // Callback handler on data - static int callbackHandler(sd_event_source* es, int fd, - uint32_t revents, void* userData) - { - std::cout <<"Event fired" << std::endl; - completed = true; - return 0; - } + // Callback handler on data + static int callbackHandler(sd_event_source* es, int fd, uint32_t revents, + void* userData) + { + std::cout << "Event fired" << std::endl; + completed = true; + return 0; + } }; /** @brief Makes sure that event never comes for 3 seconds @@ -66,20 +68,20 @@ TEST_F(GpioTest, noEventIn3Seconds) { using namespace std::chrono; - phosphor::gpio::EventPtr eventP { events }; + phosphor::gpio::EventPtr eventP{events}; events = nullptr; const std::string emptyTarget = ""; - Monitor gpio(DEVICE, code, value, emptyTarget, - eventP, false, callbackHandler, false); + Monitor gpio(DEVICE, code, value, emptyTarget, eventP, false, + callbackHandler, false); // Waiting 3 seconds and check if the completion status is set int count = 0; - while(count < 3) + while (count < 3) { // Returns -0- on timeout and positive number on dispatch auto sleepTime = duration_cast<microseconds>(seconds(1)); - if(!sd_event_run(eventP.get(), sleepTime.count())) + if (!sd_event_run(eventP.get(), sleepTime.count())) { count++; } @@ -95,16 +97,16 @@ TEST_F(GpioTest, pumpDataAndExpectCallBack) { using namespace std::chrono; - phosphor::gpio::EventPtr eventP { events }; + phosphor::gpio::EventPtr eventP{events}; events = nullptr; const std::string emptyTarget = ""; - Monitor gpio(DEVICE, code, value, emptyTarget, - eventP, false, callbackHandler, false); + Monitor gpio(DEVICE, code, value, emptyTarget, eventP, false, + callbackHandler, false); // Pump the data in the middle int count = 0; - while(count < 2 && !completed) + while (count < 2 && !completed) { if (count == 1) { @@ -114,7 +116,7 @@ TEST_F(GpioTest, pumpDataAndExpectCallBack) // Returns -0- on timeout auto sleepTime = duration_cast<microseconds>(seconds(1)); - if(!sd_event_run(eventP.get(), sleepTime.count())) + if (!sd_event_run(eventP.get(), sleepTime.count())) { count++; } |