From c8137c5298cdae05562db621f27c53a4a5fbc6c0 Mon Sep 17 00:00:00 2001 From: Jakob Hasse Date: Fri, 10 Jan 2020 16:04:35 +0800 Subject: [PATCH] C++: rtti unit tests, corresponding CI config --- components/cxx/test/test_rtti.cpp | 96 ++++++++++++++++++++++++++++ tools/ci/config/target-test.yml | 2 +- tools/unit-test-app/configs/cxx_rtti | 3 + 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 components/cxx/test/test_rtti.cpp create mode 100644 tools/unit-test-app/configs/cxx_rtti diff --git a/components/cxx/test/test_rtti.cpp b/components/cxx/test/test_rtti.cpp new file mode 100644 index 000000000..d615ee2d7 --- /dev/null +++ b/components/cxx/test/test_rtti.cpp @@ -0,0 +1,96 @@ +#include +#include "unity.h" + +#ifdef CONFIG_COMPILER_CXX_RTTI + +using namespace std; + +class Base { +public: + virtual ~Base() {} ; + virtual char name() = 0; +}; + +class DerivedA : public Base { +public: + char name() override + { + return 'A'; + } +}; + +class DerivedB : public Base { +public: + char name() override + { + return 'B'; + } +}; + +TEST_CASE("unsuccessful dynamic cast on pointer returns nullptr", "[cxx]") +{ + Base *base = new DerivedA(); + DerivedB *derived = dynamic_cast(base); + TEST_ASSERT_EQUAL(derived, nullptr); + delete base; + derived = nullptr; +} + +TEST_CASE("dynamic cast works", "[cxx]") +{ + Base *base = new DerivedA(); + DerivedA *derived = dynamic_cast(base); + TEST_ASSERT_EQUAL(derived, base); + delete base; +} + +TEST_CASE("typeid of dynamic objects works", "[cxx]") +{ + Base *base = new DerivedA(); + DerivedA *derived = dynamic_cast(base); + TEST_ASSERT_EQUAL(typeid(*derived).hash_code(), typeid(*base).hash_code()); + TEST_ASSERT_EQUAL(typeid(*derived).hash_code(), typeid(DerivedA).hash_code()); + delete base; + derived = nullptr; +} + +int dummy_function1(int arg1, double arg2); +int dummy_function2(int arg1, double arg2); + +TEST_CASE("typeid of function works", "[cxx]") +{ + TEST_ASSERT_EQUAL(typeid(dummy_function1).hash_code(), typeid(dummy_function2).hash_code()); +} + +#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS +TEST_CASE("unsuccessful dynamic cast on reference throws exception", "[cxx]") +{ + bool thrown = false; + DerivedA derived_a; + Base &base = derived_a; + try { + DerivedB &derived_b = dynamic_cast(base); + derived_b.name(); // suppress warning + } catch (bad_cast &e) { + thrown = true; + } + TEST_ASSERT(thrown); +} + +TEST_CASE("typeid on nullptr throws bad_typeid", "[cxx]") +{ + Base *base = nullptr; + size_t hash = 0; + bool thrown = false; + try { + hash = typeid(*base).hash_code(); + } catch (bad_typeid &e) { + thrown = true; + } + TEST_ASSERT_EQUAL(0, hash); + TEST_ASSERT(thrown); +} + +#endif // CONFIG_COMPILER_CXX_EXCEPTIONS +#endif // CONFIG_COMPILER_CXX_RTTI + diff --git a/tools/ci/config/target-test.yml b/tools/ci/config/target-test.yml index edf2d4572..eff4e3d8b 100644 --- a/tools/ci/config/target-test.yml +++ b/tools/ci/config/target-test.yml @@ -301,7 +301,7 @@ example_test_012: UT_001: extends: .unit_test_template - parallel: 32 + parallel: 33 tags: - ESP32_IDF - UT_T1_1 diff --git a/tools/unit-test-app/configs/cxx_rtti b/tools/unit-test-app/configs/cxx_rtti new file mode 100644 index 000000000..c3ad83b81 --- /dev/null +++ b/tools/unit-test-app/configs/cxx_rtti @@ -0,0 +1,3 @@ +TEST_COMPONENTS=cxx +CONFIG_COMPILER_CXX_EXCEPTIONS=y +CONFIG_COMPILER_CXX_RTTI=y