Private
Public Access
2
0

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 <noel.eck@intel.com>
This commit is contained in:
Noel Eck
2018-03-12 10:37:56 -07:00
parent 76c8b9a76e
commit 467cf5132d
5 changed files with 210 additions and 4 deletions

View File

@@ -39,7 +39,7 @@ services:
- BUILDSWIG=OFF - BUILDSWIG=OFF
- INSTALLTOOLS=OFF - INSTALLTOOLS=OFF
- JSONPLAT=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: doc:
extends: all extends: all
@@ -117,13 +117,13 @@ services:
environment: environment:
- BUILDSWIG=ON - BUILDSWIG=ON
- BUILDSWIGPYTHON=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: python3:
extends: python2 extends: python2
environment: environment:
- USEPYTHON3TESTS=ON - 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: java:
extends: base extends: base
@@ -131,7 +131,7 @@ services:
environment: environment:
- BUILDSWIG=ON - BUILDSWIG=ON
- BUILDSWIGJAVA=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: android:
extends: java extends: java

View File

@@ -36,3 +36,6 @@ if (BUILDSWIGPYTHON)
message (STATUS "Could not run tests since python interpreter or python bindings not built") message (STATUS "Could not run tests since python interpreter or python bindings not built")
endif () endif ()
endif () endif ()
# Add mraa unit tests
add_subdirectory(unit)

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

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

View File

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

View File

@@ -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<mraa::Gpio>("GPIO-0");
ASSERT_EQ(0, sg0->getPin());
}
}