Skip to content

Commit

Permalink
core/irq: Add C++ wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
jenswet authored and maribu committed Jan 13, 2023
1 parent 89ef35f commit a9c5987
Show file tree
Hide file tree
Showing 11 changed files with 653 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/fff/Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ INCLUDES += -I$(PKGDIRBASE)/fff
# There's nothing to build in this package, it's used as a header only library.
# So it's declared as a pseudo-module
PSEUDOMODULES += fff

# Tests don't need pedantic. Pedantic throws errors in variadic macros when compiling for C++
CXXEXFLAGS += -Wno-pedantic
34 changes: 34 additions & 0 deletions sys/include/cppunit.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2021 Jens Wetterich <jens@wetterich-net.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup sys
* @defgroup unittests_cpp C++ Unittests
* @brief RIOT unit tests for C++
* @details The C++ unit test framework syntax is loosely based on
* GoogleTest, but offers far less functionality.
* For mocking the package @ref pkg_fff can be used.
* @{
* @file
* @brief RIOT unit tests for C++
* @details The C++ unit test framework syntax is loosely based on GoogleTest,
* but offers far less functionality.
* For mocking the package @ref pkg_fff can be used.
* @author Jens Wetterich <jens@wetterich-net.de>
*
*/
#ifndef CPPUNIT_H
#define CPPUNIT_H
#if __cplusplus >= 201103L || defined(DOXYGEN)
#include "cppunit/cppunit_base.hpp"
#include "cppunit/cppunit_expect.hpp"
#include "cppunit/cppunit_fff.hpp"
#else
#error This library needs C++11 and newer
#endif
#endif
/** @} */
191 changes: 191 additions & 0 deletions sys/include/cppunit/cppunit_base.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
/*
* Copyright (C) 2021 Jens Wetterich <jens@wetterich-net.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @addtogroup unittests_cpp
* @{
* @file
* @brief RIOT unit tests for C++ base classes and macros
* @author Jens Wetterich <jens@wetterich-net.de>
*
*/
#ifndef CPPUNIT_BASE_H
#define CPPUNIT_BASE_H
#if __cplusplus >= 201103L || defined(DOXYGEN)
#include <array>
#include <cstdio>
#include <cstring>
#include <type_traits>
#ifndef CPPUNIT_SUITE_CNT
/**
* @brief Maximum amount of tests in one test suite
*/
#define CPPUNIT_SUITE_CNT (10)
#endif
/**
* @brief RIOT C++ namespace
*/
namespace riot {
/**
* @brief namespace for cpp unit tests
*/
namespace testing {
/**
* @brief Test base class
* @details Should not be instantiated directly.
* @sa #TEST(suite, name) macro
*/
class test {
private:
bool suc = true; ///< indicates success of the test after running
public:
/**
* @brief Run the test
* @details Should not be called directly, only via macros
* @sa #RUN_SUITE(name)
* @return whether the test completed without errors
*/
virtual bool run() = 0;
/**
* @brief Indicate failure during test run
* @details Should be called by assertions macros
* @sa #EXPECT_EQ(expected, actual, msg)
* @sa #EXPECT_STREQ(expected, actual, msg)
* @sa #EXPECT_FFF_CALL_COUNT(name, count)
* @sa #EXPECT_FFF_CALL_PARAMS(mock, ...)
*/
void fail() {
suc = false;
}
/**
* @brief Check whether the test completed successfully
* @return whether the test completed without errors
*/
bool success() const {
return suc;
}
};
/**
* @brief Test suite base class
* @details Should not be instantiated directly.
* To customize a test suite with custom set_up and tear down methods,
* inherit from this class and override set_up() and/or tear_down().
* They will before / after every test.
* @sa #TEST_SUITE(name) macro
* @sa #TEST_SUITE_F(suite, name) macro
* @sa test_suite::set_up()
* @sa test_suite::tear_down()
*/
class test_suite {
protected:
bool suc = true; ///< Indicates success of all tests after running the suite
std::array<test*, CPPUNIT_SUITE_CNT> tests{}; ///< array of tests to run
public:
/**
* @brief method to run before each test
*/
virtual void set_up() {
}
/**
* @brief method to run after each test
*/
virtual void tear_down() {
}
/**
* @brief get the name of the test suite
* @return name string
*/
virtual const char* get_name() const {
return "";
};
/**
* @brief Run all tests in the suite
*/
virtual void run() {
printf("----\nStarting Test suite %s\n", get_name());
for (auto test : tests) {
if (test) {
suc = test->run() && suc;
}
}
printf("Suite %s completed: %s\n----\n", get_name(), suc ? "SUCCESS" : "FAILURE");
}
/**
* @brief Run all tests in the suite
*/
void addTest(test* test) {
for (int i = 0; i < CPPUNIT_SUITE_CNT; i++) {
if (!tests[i]) {
tests[i] = test;
break;
}
}
}
};
}// namespace testing
}// namespace riot
/**
* @brief Run the test suite \a name
* @hideinitializer
* @param[in] name Name of the suite
*/
#define RUN_SUITE(name) test_suite_##name.run();

/**
* @brief Instantiate a test suite with custom test fixture
* @hideinitializer
* @param[in] suite Name of the custom test fixture class
* @param[in] name Instantiation name of the suite
*/
#define TEST_SUITE_F(suite, name) \
static_assert(sizeof(#suite) > 1, "test fixture class must not be empty"); \
static_assert(sizeof(#name) > 1, "test_suite name must not be empty"); \
class test_suite_##name : public suite { \
const char* get_name() const override { \
return #name; \
}; \
}; \
test_suite_##name test_suite_##name;

/**
* @brief Instantiate a test suite
* @hideinitializer
* @param[in] name Instantiation name of the suite
*/
#define TEST_SUITE(name) TEST_SUITE_F(::riot::testing::test_suite, name)

/**
* @brief Begin the definition of a test
* @details Insert the test body after the macro
* @hideinitializer
* @param[in] suite Name of the suite to add the test to
* @param[in] name Instantiation name of the test
*/
#define TEST(suite, name) \
class Test_##name : public ::riot::testing::test { \
private: \
void test_body(); \
\
public: \
Test_##name() { \
test_suite_##suite.addTest(this); \
} \
bool run() { \
test_suite_##suite.set_up(); \
printf("Running test " #name "...\n"); \
test_body(); \
printf("Test " #name ": %s\n", success() ? "SUCCESS" : "FAILURE"); \
test_suite_##suite.tear_down(); \
return success(); \
}; \
}; \
void Test_##name::test_body()
#else
#error This library needs C++11 and newer
#endif
#endif
/** @} */
63 changes: 63 additions & 0 deletions sys/include/cppunit/cppunit_expect.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (C) 2021 Jens Wetterich <jens@wetterich-net.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @addtogroup unittests_cpp
* @{
* @file
* @brief RIOT unit tests for C++ assertion macros
* @author Jens Wetterich <jens@wetterich-net.de>
*
*/
#ifndef CPPUNIT_EXPECT_H
#define CPPUNIT_EXPECT_H
#if __cplusplus >= 201103L || defined(DOXYGEN)
/**
* @brief Expect equality of the \a actual and \a expected value
* @hideinitializer
* @param[in] expected Expected value
* @param[in] actual Actual value
* @param[in] msg Message to print in case of failure
*/
#define EXPECT_EQ(expected, actual, msg) \
static_assert(std::is_integral<decltype(expected)>::value, \
"EXPECT_EQ requires an integral type "); \
if ((actual) != (expected)) { \
fail(); \
if (std::is_same<decltype(expected), bool>::value) { \
printf("Expected: %s, actual: %s\n" msg "\n", (expected) ? "true" : "false", \
(actual) ? "true" : "false"); \
} \
else if (std::is_unsigned<decltype(expected)>::value) { \
printf("Expected: %u, actual: %u\n" msg "\n", static_cast<unsigned>(expected), \
static_cast<unsigned>(actual)); \
} \
else { \
printf("Expected: %d, actual: %d\n" msg "\n", static_cast<int>(expected), \
static_cast<int>(actual)); \
} \
}
/**
* @brief Expect string equality of the \a actual and \a expected value
* @details Interprets both values as const char* string
* @hideinitializer
* @param[in] expected Expected value
* @param[in] actual Actual value
* @param[in] msg Message to print in case of failure
*/
#define EXPECT_STREQ(expected, actual, msg) \
auto expected_str = static_cast<const char*>(expected); \
auto actual_str = static_cast<const char*>(actual); \
if (strcmp(expected_str, actual_str) != 0) { \
fail(); \
printf(msg " not equal! Expected: %s, actual: %s\n", expected_str, actual_str); \
}
#else
#error This library needs C++11 and newer
#endif
#endif
/** @} */
Loading

0 comments on commit a9c5987

Please # to comment.