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 <noel.eck@intel.com>
This commit is contained in:
Noel Eck 2018-03-02 11:45:15 -08:00
parent c54d6de054
commit 92b0919f56
4 changed files with 94 additions and 6 deletions

View File

@ -52,8 +52,9 @@ static struct aqi {
void upm_delay(unsigned int time) void upm_delay(unsigned int time)
{ {
if (time <= 0) /* Return if time == 0 */
time = 1; if (!time)
return;
#if defined(UPM_PLATFORM_LINUX) #if defined(UPM_PLATFORM_LINUX)
@ -92,8 +93,9 @@ void upm_delay(unsigned int time)
void upm_delay_ms(unsigned int time) void upm_delay_ms(unsigned int time)
{ {
if (time <= 0) /* Return if time == 0 */
time = 1; if (!time)
return;
#if defined(UPM_PLATFORM_LINUX) #if defined(UPM_PLATFORM_LINUX)
@ -128,8 +130,9 @@ void upm_delay_ms(unsigned int time)
void upm_delay_us(unsigned int time) void upm_delay_us(unsigned int time)
{ {
if (time <= 0) /* Return if time == 0 */
time = 1; if (!time)
return;
#if defined(UPM_PLATFORM_LINUX) #if defined(UPM_PLATFORM_LINUX)

View File

@ -82,3 +82,6 @@ endif (BUILDSWIGPYTHON AND PYTHON3INTERP_FOUND)
# -DUPMBASEDIR=${CMAKE_SOURCE_DIR} # -DUPMBASEDIR=${CMAKE_SOURCE_DIR}
# -P ${CMAKE_SOURCE_DIR}/tests/runjsontest.cmake) # -P ${CMAKE_SOURCE_DIR}/tests/runjsontest.cmake)
#endif(NPM_EXECUTABLE) #endif(NPM_EXECUTABLE)
# Unit tests
add_subdirectory (unit)

12
tests/unit/CMakeLists.txt Normal file
View File

@ -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)

View File

@ -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();
}
*/