From 467cf5132d13f13fbdf0c0f293916bb8a6e70690 Mon Sep 17 00:00:00 2001 From: Noel Eck Date: Mon, 12 Mar 2018 10:37:56 -0700 Subject: [PATCH] gtest: Added Google Test Added Google Test for unit testing. Currently NOT required by MRAA CMake. * Added a test fixture for mraa common C header methods. * Added a test fixture for mraa common C++ header methods. Signed-off-by: Noel Eck --- docker-compose.yaml | 8 +-- tests/CMakeLists.txt | 3 + tests/unit/CMakeLists.txt | 28 ++++++++ tests/unit/api/api_common_h_unit.cxx | 97 ++++++++++++++++++++++++++ tests/unit/api/api_common_hpp_unit.cxx | 78 +++++++++++++++++++++ 5 files changed, 210 insertions(+), 4 deletions(-) create mode 100644 tests/unit/CMakeLists.txt create mode 100644 tests/unit/api/api_common_h_unit.cxx create mode 100644 tests/unit/api/api_common_hpp_unit.cxx diff --git a/docker-compose.yaml b/docker-compose.yaml index dd119ce..d8161a9 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -39,7 +39,7 @@ services: - BUILDSWIG=OFF - INSTALLTOOLS=OFF - JSONPLAT=OFF - command: bash -c "./scripts/run-cmake.sh && cd build && make" + command: bash -c "./scripts/run-cmake.sh && cd build && make && ctest -R unit --output-on-failure" doc: extends: all @@ -117,13 +117,13 @@ services: environment: - BUILDSWIG=ON - BUILDSWIGPYTHON=ON - command: bash -c "./scripts/run-cmake.sh && cd build && make _python2-mraa && ctest --output-on-failure" + command: bash -c "./scripts/run-cmake.sh && cd build && make _python2-mraa tests-unit && ctest --output-on-failure" python3: extends: python2 environment: - USEPYTHON3TESTS=ON - command: bash -c "./scripts/run-cmake.sh && cd build && make _python3-mraa && ctest --output-on-failure" + command: bash -c "./scripts/run-cmake.sh && cd build && make _python3-mraa tests-unit && ctest --output-on-failure" java: extends: base @@ -131,7 +131,7 @@ services: environment: - BUILDSWIG=ON - BUILDSWIGJAVA=ON - command: bash -c "./scripts/run-cmake.sh && cd build && make mraajava && ctest --output-on-failure" + command: bash -c "./scripts/run-cmake.sh && cd build && make mraajava tests-unit && ctest --output-on-failure" android: extends: java diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a20531c..a8444ad 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -36,3 +36,6 @@ if (BUILDSWIGPYTHON) message (STATUS "Could not run tests since python interpreter or python bindings not built") endif () endif () + +# Add mraa unit tests +add_subdirectory(unit) diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt new file mode 100644 index 0000000..f29e6b6 --- /dev/null +++ b/tests/unit/CMakeLists.txt @@ -0,0 +1,28 @@ +# 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() + +# Unit tests - C common header methods +add_executable(api_common_h_unit api/api_common_h_unit.cxx) +target_link_libraries(api_common_h_unit GTest::GTest GTest::Main mraa) +target_include_directories(api_common_h_unit + PRIVATE "${CMAKE_SOURCE_DIR}" "${CMAKE_SOURCE_DIR}/api" "${CMAKE_SOURCE_DIR}/api/mraa") +gtest_add_tests(api_common_h_unit "" AUTO) + +# Unit tests - C++ common header methods +add_executable(api_common_hpp_unit api/api_common_hpp_unit.cxx) +target_link_libraries(api_common_hpp_unit GTest::GTest GTest::Main mraa) +target_include_directories(api_common_hpp_unit PRIVATE "${CMAKE_SOURCE_DIR}/api") +gtest_add_tests(api_common_hpp_unit "" AUTO) + +# Add a custom target for unit tests +add_custom_target(tests-unit ALL + DEPENDS + api_common_h_unit + api_common_hpp_unit + COMMENT "mraa unit test collection") diff --git a/tests/unit/api/api_common_h_unit.cxx b/tests/unit/api/api_common_h_unit.cxx new file mode 100644 index 0000000..055c7fb --- /dev/null +++ b/tests/unit/api/api_common_h_unit.cxx @@ -0,0 +1,97 @@ +#include "gtest/gtest.h" +#include "mraa/common.h" +#include "include/mraa_internal_types.h" + +/* MRAA API common test fixture */ +class api_common_h_unit : public ::testing::Test +{ + protected: + /* One-time setup logic if needed */ + api_common_h_unit() {} + + /* One-time tear-down logic if needed */ + virtual ~api_common_h_unit() {} + + /* Per-test setup logic if needed */ + virtual void SetUp() {} + + /* Per-test tear-down logic if needed */ + virtual void TearDown() {} +}; + +/* Test for a successful mraa_init */ +TEST_F(api_common_h_unit, test_libmraa_init_doa) +{ + ASSERT_EQ(MRAA_SUCCESS, mraa_init()); +} + +/* Test for a basic mraa init/deinit */ +TEST_F(api_common_h_unit, test_libmraa_init_deinit) +{ + ASSERT_EQ(MRAA_SUCCESS, mraa_init()); + mraa_deinit(); +} + +/* Test the C exposed common methods */ +TEST_F(api_common_h_unit, test_libmraa_common_methods) +{ + ASSERT_EQ(MRAA_SUCCESS, mraa_init()); + + /* Test log level settings */ + for (int lvl = 0; lvl <= 7;lvl++) + ASSERT_EQ(MRAA_SUCCESS, mraa_set_log_level(lvl)) << "Set loglevel: " << lvl; + ASSERT_EQ(MRAA_ERROR_INVALID_PARAMETER, mraa_set_log_level(8)); + + /* Mock platform tests */ + if (mraa_get_platform_type() == MRAA_MOCK_PLATFORM) + { + /* Pin 0 is a valid pin */ + ASSERT_TRUE(mraa_pin_mode_test(0, MRAA_PIN_VALID)); + + /* Pin 0 is a GPIO */ + ASSERT_TRUE(mraa_pin_mode_test(0, MRAA_PIN_GPIO)); + + /* Mock platform defaults to 12 bits */ + ASSERT_EQ(12, mraa_adc_raw_bits()); + + /* Which better equal the bitsize for platform 0 */ + ASSERT_EQ(mraa_get_platform_adc_raw_bits(0), mraa_adc_raw_bits()); + + /* Raw bits get shifted to supported bits */ + ASSERT_EQ(10, mraa_adc_supported_bits()); + + /* Check the mock platform name */ + ASSERT_STREQ("MRAA mock platform", mraa_get_platform_name()); + + /* No versioning for the mock platform */ + ASSERT_STREQ(NULL, mraa_get_platform_version(0)); + + /* Currently 10 pins in the mock platform */ + ASSERT_EQ(10, mraa_get_pin_count()); + + /* Test the other pin/bus counts for the mock platform */ + EXPECT_EQ(1, mraa_get_aio_count()); + EXPECT_EQ(1, mraa_get_gpio_count()); + EXPECT_EQ(1, mraa_get_i2c_bus_count()); + EXPECT_EQ(0, mraa_get_i2c_bus_id(0)); + EXPECT_EQ(0, mraa_get_pwm_count()); + EXPECT_EQ(1, mraa_get_spi_bus_count()); + EXPECT_EQ(1, mraa_get_uart_count()); + + /* Test pin to name method/s */ + ASSERT_STREQ("GPIO0", mraa_get_pin_name(0)); + + /* Test the lookup method/s */ + ASSERT_EQ(0, mraa_gpio_lookup("GPIO0")); + + /* MOCK does NOT have a subplatform */ + ASSERT_FALSE(mraa_has_sub_platform()); + + /* Test the string init methods (via the internal struct type) */ + struct _gpio* sg0 = (struct _gpio*)mraa_init_io("GPIO-0"); + ASSERT_EQ(0, sg0->pin); + } + + /* Set the priority of this process */ + //EXPECT_EQ(40, mraa_set_priority(40)); +} diff --git a/tests/unit/api/api_common_hpp_unit.cxx b/tests/unit/api/api_common_hpp_unit.cxx new file mode 100644 index 0000000..bbfd579 --- /dev/null +++ b/tests/unit/api/api_common_hpp_unit.cxx @@ -0,0 +1,78 @@ +#include "gtest/gtest.h" +#include "mraa.hpp" + +/* MRAA API common test fixture */ +class api_common_hpp_unit : public ::testing::Test +{ + protected: + /* One-time setup logic if needed */ + api_common_hpp_unit() {} + + /* One-time tear-down logic if needed */ + virtual ~api_common_hpp_unit() {} + + /* Per-test setup logic if needed */ + virtual void SetUp() {} + + /* Per-test tear-down logic if needed */ + virtual void TearDown() {} +}; + +/* Test the C++ exposed common methods */ +TEST_F(api_common_hpp_unit, test_libmraa_common_methods) +{ + ASSERT_EQ(mraa::SUCCESS, mraa::init()); + + /* Test log level settings */ + for (int lvl = 0; lvl <= 7;lvl++) + ASSERT_EQ(mraa::SUCCESS, mraa::setLogLevel(lvl)) << "Set loglevel: " << lvl; + ASSERT_EQ(mraa::ERROR_INVALID_PARAMETER, mraa::setLogLevel(8)); + + /* Mock platform tests */ + if (mraa::getPlatformType() == mraa::MOCK_PLATFORM) + { + /* Pin 0 is a valid pin */ + ASSERT_TRUE(mraa::pinModeTest(0, mraa::PIN_VALID)); + + /* Pin 0 is a GPIO */ + ASSERT_TRUE(mraa::pinModeTest(0, mraa::PIN_GPIO)); + + /* Mock platform defaults to 12 bits */ + ASSERT_EQ(12, mraa::adcRawBits()); + + /* Raw bits get shifted to supported bits */ + ASSERT_EQ(10, mraa::adcSupportedBits()); + + /* Check the mock platform name */ + ASSERT_EQ("MRAA mock platform", mraa::getPlatformName()); + + /* No versioning for the mock platform */ + ASSERT_EQ("", mraa::getPlatformVersion(0)); + + /* Currently 10 pins in the mock platform */ + ASSERT_EQ(10, mraa::getPinCount()); + + /* Test the other pin/bus counts for the mock platform */ + /* Missing equivalent C++ methods */ + //EXPECT_EQ(1, mraa_get_aio_count()); + //EXPECT_EQ(1, mraa_get_gpio_count()); + EXPECT_EQ(1, mraa::getI2cBusCount()); + EXPECT_EQ(0, mraa::getI2cBusId(0)); + //EXPECT_EQ(0, mraa_get_pwm_count()); + //EXPECT_EQ(1, mraa_get_spi_bus_count()); + EXPECT_EQ(1, mraa::getUartCount()); + + /* Test pin to name method/s */ + ASSERT_EQ("GPIO0", mraa::getPinName(0)); + + /* Test the lookup method/s */ + ASSERT_EQ(0, mraa::getGpioLookup("GPIO0")); + + /* MOCK does NOT have a subplatform */ + ASSERT_FALSE(mraa::hasSubPlatform()); + + /* Test the string init methods (via the internal struct type) */ + mraa::Gpio* sg0 = mraa::initIo("GPIO-0"); + ASSERT_EQ(0, sg0->getPin()); + } +}