2021-05-16 21:43:02 -04:00
|
|
|
/*
|
2022-03-22 04:20:51 -04:00
|
|
|
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
2021-05-16 21:43:02 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2022-03-22 04:20:51 -04:00
|
|
|
#include <stdexcept>
|
2020-01-10 03:04:35 -05:00
|
|
|
#include <typeinfo>
|
|
|
|
#include "unity.h"
|
2022-03-31 06:16:19 -04:00
|
|
|
#include "unity_test_utils.h"
|
2020-01-10 03:04:35 -05:00
|
|
|
|
2022-03-22 04:20:51 -04:00
|
|
|
/* Note: When first exception (in system) is thrown this test produces memory leaks report (~300 bytes):
|
|
|
|
- 8 bytes are allocated by __cxa_get_globals() to keep __cxa_eh_globals
|
|
|
|
- 16 bytes are allocated by pthread_setspecific() which is called by __cxa_get_globals() to init TLS var for __cxa_eh_globals
|
|
|
|
- 88 bytes are allocated by pthread_setspecific() to init internal lock
|
|
|
|
- some more memory...
|
|
|
|
*/
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
#define LEAKS (300)
|
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S2
|
|
|
|
#define LEAKS (800)
|
|
|
|
#elif CONFIG_IDF_TARGET_ESP32C3
|
|
|
|
#define LEAKS (700)
|
|
|
|
#else
|
|
|
|
#error "unknown target in CXX tests, can't set leaks threshold"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
extern "C" void setUp()
|
|
|
|
{
|
2022-03-31 06:16:19 -04:00
|
|
|
unity_utils_set_leak_level(0);
|
|
|
|
unity_utils_record_free_mem();
|
2022-03-22 04:20:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void tearDown()
|
|
|
|
{
|
2022-03-31 06:16:19 -04:00
|
|
|
unity_utils_evaluate_leaks();
|
2022-03-22 04:20:51 -04:00
|
|
|
}
|
2020-01-10 03:04:35 -05:00
|
|
|
|
|
|
|
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<DerivedB*>(base);
|
|
|
|
TEST_ASSERT_EQUAL(derived, nullptr);
|
|
|
|
delete base;
|
|
|
|
derived = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("dynamic cast works", "[cxx]")
|
|
|
|
{
|
|
|
|
Base *base = new DerivedA();
|
|
|
|
DerivedA *derived = dynamic_cast<DerivedA*>(base);
|
|
|
|
TEST_ASSERT_EQUAL(derived, base);
|
|
|
|
delete base;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("typeid of dynamic objects works", "[cxx]")
|
|
|
|
{
|
|
|
|
Base *base = new DerivedA();
|
|
|
|
DerivedA *derived = dynamic_cast<DerivedA*>(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());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("unsuccessful dynamic cast on reference throws exception", "[cxx]")
|
|
|
|
{
|
2022-03-31 06:16:19 -04:00
|
|
|
unity_utils_set_leak_level(LEAKS);
|
2020-01-10 03:04:35 -05:00
|
|
|
bool thrown = false;
|
|
|
|
DerivedA derived_a;
|
|
|
|
Base &base = derived_a;
|
|
|
|
try {
|
2024-03-05 01:07:05 -05:00
|
|
|
DerivedB &derived_b = dynamic_cast<DerivedB &>(base);
|
2020-01-10 03:04:35 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-03-22 04:20:51 -04:00
|
|
|
extern "C" void app_main(void)
|
|
|
|
{
|
|
|
|
printf("CXX RTTI TEST\n");
|
|
|
|
unity_run_menu();
|
|
|
|
}
|