From 92b0919f563dc0d55bedbec05ce31092ffb033b5 Mon Sep 17 00:00:00 2001 From: Noel Eck Date: Fri, 2 Mar 2018 11:45:15 -0800 Subject: [PATCH] gtest: Added Google Test Added Google Test for unit testing. Currently NOT required by UPM CMake. * Added a test fixture for the utilities library. * Fixed bug in delay methods provided by utilities library. Signed-off-by: Noel Eck --- src/utilities/upm_utilities.c | 15 +++-- tests/CMakeLists.txt | 3 + tests/unit/CMakeLists.txt | 12 ++++ tests/unit/utilities/utilities_tests.cxx | 70 ++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 tests/unit/CMakeLists.txt create mode 100644 tests/unit/utilities/utilities_tests.cxx diff --git a/src/utilities/upm_utilities.c b/src/utilities/upm_utilities.c index 0eadf2f0..39e0a123 100644 --- a/src/utilities/upm_utilities.c +++ b/src/utilities/upm_utilities.c @@ -52,8 +52,9 @@ static struct aqi { void upm_delay(unsigned int time) { - if (time <= 0) - time = 1; + /* Return if time == 0 */ + if (!time) + return; #if defined(UPM_PLATFORM_LINUX) @@ -92,8 +93,9 @@ void upm_delay(unsigned int time) void upm_delay_ms(unsigned int time) { - if (time <= 0) - time = 1; + /* Return if time == 0 */ + if (!time) + return; #if defined(UPM_PLATFORM_LINUX) @@ -128,8 +130,9 @@ void upm_delay_ms(unsigned int time) void upm_delay_us(unsigned int time) { - if (time <= 0) - time = 1; + /* Return if time == 0 */ + if (!time) + return; #if defined(UPM_PLATFORM_LINUX) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index ed0af9a2..b1221622 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -82,3 +82,6 @@ endif (BUILDSWIGPYTHON AND PYTHON3INTERP_FOUND) # -DUPMBASEDIR=${CMAKE_SOURCE_DIR} # -P ${CMAKE_SOURCE_DIR}/tests/runjsontest.cmake) #endif(NPM_EXECUTABLE) + +# Unit tests +add_subdirectory (unit) diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt new file mode 100644 index 00000000..4b0a6301 --- /dev/null +++ b/tests/unit/CMakeLists.txt @@ -0,0 +1,12 @@ +# For now, Google Test is NOT required */ +find_package(GTest) + +# If not found, print a status message and return +if(NOT GTEST_FOUND) + message(STATUS "Install Google Test to enable additional unit testing") + return () +endif() + +add_executable(utilities_tests utilities/utilities_tests.cxx) +target_link_libraries(utilities_tests utilities GTest::GTest GTest::Main) +gtest_add_tests(utilities_tests "" AUTO) diff --git a/tests/unit/utilities/utilities_tests.cxx b/tests/unit/utilities/utilities_tests.cxx new file mode 100644 index 00000000..bd572f59 --- /dev/null +++ b/tests/unit/utilities/utilities_tests.cxx @@ -0,0 +1,70 @@ +#include "gtest/gtest.h" +#include "upm_utilities.h" +#include "upm_utilities.hpp" + +/* Utilities test fixture */ +class utilities_unit : public ::testing::Test +{ + protected: + /* One-time setup logic if needed */ + utilities_unit() {} + + /* One-time tear-down logic if needed */ + virtual ~utilities_unit() {} + + /* Per-test setup logic if needed */ + virtual void SetUp() {} + + /* Per-test tear-down logic if needed */ + virtual void TearDown() {} +}; + +/* Test the second delay method */ +TEST_F(utilities_unit, test_upm_delay) +{ + upm_clock_t clock; + upm_clock_init(&clock); + + /* Test a corner case */ + upm_delay(0); + upm_delay(1); + + /* +- check for 1s +/- 1ms */ + ASSERT_NEAR(upm_elapsed_ms(&clock), 1000, 1); +} + +/* Test the millisecond delay method */ +TEST_F(utilities_unit, test_upm_delay_ms) +{ + upm_clock_t clock; + upm_clock_init(&clock); + + /* Test a corner case */ + upm_delay_ms(0); + upm_delay_ms(50); + + /* +- check for 50ms +/- 1ms */ + ASSERT_NEAR(upm_elapsed_ms(&clock), 50, 1); +} + +/* Test the microsecond delay method */ +TEST_F(utilities_unit, test_upm_delay_us) +{ + upm_clock_t clock; + upm_clock_init(&clock); + + /* Test a corner case */ + upm_delay_us(0); + upm_delay_us(1000); + + /* +- check for 1000us +/- 100us */ + ASSERT_NEAR(upm_elapsed_us(&clock), 1000, 100); +} + +/* Currently no need for a custom main (use gtest's) +int main(int argc, char **argv) +{ + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} +*/