esp-idf/components/cxx/test/test_cxx.cpp

320 lines
7.2 KiB
C++
Raw Normal View History

#include <vector>
#include <numeric>
#include <stdexcept>
#include <string>
#include "unity.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
global: move the soc component out of the common list This MR removes the common dependency from every IDF components to the SOC component. Currently, in the ``idf_functions.cmake`` script, we include the header path of SOC component by default for all components. But for better code organization (or maybe also benifits to the compiling speed), we may remove the dependency to SOC components for most components except the driver and kernel related components. In CMAKE, we have two kinds of header visibilities (set by include path visibility): (Assume component A --(depends on)--> B, B is the current component) 1. public (``COMPONENT_ADD_INCLUDEDIRS``): means this path is visible to other depending components (A) (visible to A and B) 2. private (``COMPONENT_PRIV_INCLUDEDIRS``): means this path is only visible to source files inside the component (visible to B only) and we have two kinds of depending ways: (Assume component A --(depends on)--> B --(depends on)--> C, B is the current component) 1. public (```COMPONENT_REQUIRES```): means B can access to public include path of C. All other components rely on you (A) will also be available for the public headers. (visible to A, B) 2. private (``COMPONENT_PRIV_REQUIRES``): means B can access to public include path of C, but don't propagate this relation to other components (A). (visible to B) 1. remove the common requirement in ``idf_functions.cmake``, this makes the SOC components invisible to all other components by default. 2. if a component (for example, DRIVER) really needs the dependency to SOC, add a private dependency to SOC for it. 3. some other components that don't really depends on the SOC may still meet some errors saying "can't find header soc/...", this is because it's depended component (DRIVER) incorrectly include the header of SOC in its public headers. Moving all this kind of #include into source files, or private headers 4. Fix the include requirements for some file which miss sufficient #include directives. (Previously they include some headers by the long long long header include link) This is a breaking change. Previous code may depends on the long include chain. You may need to include the following headers for some files after this commit: - soc/soc.h - soc/soc_memory_layout.h - driver/gpio.h - esp_sleep.h The major broken include chain includes: 1. esp_system.h no longer includes esp_sleep.h. The latter includes driver/gpio.h and driver/touch_pad.h. 2. ets_sys.h no longer includes soc/soc.h 3. freertos/portmacro.h no longer includes soc/soc_memory_layout.h some peripheral headers no longer includes their hw related headers, e.g. rom/gpio.h no longer includes soc/gpio_pins.h and soc/gpio_reg.h BREAKING CHANGE
2019-04-03 01:17:38 -04:00
#include "soc/soc.h"
TEST_CASE("can use new and delete", "[cxx]")
{
int* int_p = new int(10);
delete int_p;
int* int_array = new int[10];
delete[] int_array;
}
class Base
{
public:
virtual ~Base() {}
virtual void foo() = 0;
};
class Derived : public Base
{
public:
virtual void foo() { }
};
TEST_CASE("can call virtual functions", "[cxx]")
{
Derived d;
Base& b = static_cast<Base&>(d);
b.foo();
}
TEST_CASE("can use std::vector", "[cxx]")
{
std::vector<int> v(10, 1);
v[0] = 42;
TEST_ASSERT_EQUAL(51, std::accumulate(std::begin(v), std::end(v), 0));
}
/* Note: When first exception (in system) is thrown this test produces memory leaks report (~500 bytes):
- 392 bytes (can vary) as libunwind allocates memory to keep stack frames info to handle exceptions.
This info is kept until global destructors are called by __do_global_dtors_aux()
- 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
*/
2019-04-24 09:02:25 -04:00
#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
TEST_CASE("c++ exceptions work", "[cxx] [exceptions] [leaks=816]")
{
int thrown_value;
try {
throw 20;
} catch (int e) {
thrown_value = e;
}
TEST_ASSERT_EQUAL(20, thrown_value);
printf("OK?\n");
}
TEST_CASE("c++ bool exception", "[cxx] [exceptions] [leaks=816]")
{
bool thrown_value = false;
try {
throw true;
} catch (bool e) {
thrown_value = e;
}
TEST_ASSERT_EQUAL(true, thrown_value);
printf("OK?\n");
}
TEST_CASE("c++ void exception", "[cxx] [exceptions] [leaks=816]")
{
void* thrown_value = 0;
try {
throw (void*) 47;
} catch (void* e) {
thrown_value = e;
}
TEST_ASSERT_EQUAL(47, thrown_value);
printf("OK?\n");
}
TEST_CASE("c++ uint64_t exception", "[cxx] [exceptions] [leaks=816]")
{
uint64_t thrown_value = 0;
try {
throw (uint64_t) 47;
} catch (uint64_t e) {
thrown_value = e;
}
TEST_ASSERT_EQUAL(47, thrown_value);
printf("OK?\n");
}
TEST_CASE("c++ char exception", "[cxx] [exceptions] [leaks=816]")
{
char thrown_value = '0';
try {
throw '/';
} catch (char e) {
thrown_value = e;
}
TEST_ASSERT_EQUAL('/', thrown_value);
printf("OK?\n");
}
TEST_CASE("c++ wchar exception", "[cxx] [exceptions] [leaks=816]")
{
wchar_t thrown_value = 0;
try {
throw (wchar_t) 47;
} catch (wchar_t e) {
thrown_value = e;
}
TEST_ASSERT_EQUAL((wchar_t) 47, thrown_value);
printf("OK?\n");
}
TEST_CASE("c++ float exception", "[cxx] [exceptions] [leaks=816]")
{
float thrown_value = 0;
try {
throw 23.5f;
} catch (float e) {
thrown_value = e;
}
TEST_ASSERT_EQUAL(23.5, thrown_value);
printf("OK?\n");
}
TEST_CASE("c++ double exception", "[cxx] [exceptions] [leaks=816]")
{
double thrown_value = 0;
try {
throw 23.5d;
} catch (double e) {
thrown_value = e;
}
TEST_ASSERT_EQUAL(23.5d, thrown_value);
printf("OK?\n");
}
TEST_CASE("c++ const char* exception", "[cxx] [exceptions] [leaks=816]")
{
const char *thrown_value = 0;
try {
throw "Hi :)";
} catch (const char *e) {
thrown_value = e;
}
TEST_ASSERT_EQUAL_STRING("Hi :)", thrown_value);
printf("OK?\n");
}
struct NonExcTypeThrowee {
int value;
public:
NonExcTypeThrowee(int value) : value(value) { }
};
TEST_CASE("c++ any class exception", "[cxx] [exceptions] [leaks=816]")
{
int thrown_value = 0;
try {
throw NonExcTypeThrowee(47);
} catch (NonExcTypeThrowee &e) {
thrown_value = e.value;
}
TEST_ASSERT_EQUAL(47, thrown_value);
printf("OK?\n");
}
struct ExcTypeThrowee : public std::exception {
int value;
public:
ExcTypeThrowee(int value) : value(value) { }
};
TEST_CASE("c++ std::exception child", "[cxx] [exceptions] [leaks=816]")
{
int thrown_value = 0;
try {
throw ExcTypeThrowee(47);
} catch (ExcTypeThrowee &e) {
thrown_value = e.value;
}
TEST_ASSERT_EQUAL(47, thrown_value);
printf("OK?\n");
}
TEST_CASE("c++ exceptions emergency pool", "[cxx] [exceptions] [ignore]")
{
void **p, **pprev = NULL;
int thrown_value = 0;
// throw first exception to ensure that all initial allocations are made
try
{
throw 33;
}
catch (int e)
{
thrown_value = e;
}
TEST_ASSERT_EQUAL(33, thrown_value);
// consume all dynamic memory
while ((p = (void **)malloc(sizeof(void *)))) {
if (pprev) {
*p = pprev;
} else {
*p = NULL;
}
pprev = p;
}
try
{
throw 20;
}
catch (int e)
{
thrown_value = e;
printf("Got exception %d\n", thrown_value);
}
2019-04-24 09:02:25 -04:00
#if CONFIG_COMPILER_CXX_EXCEPTIONS_EMG_POOL_SIZE > 0
// free all memory
while (pprev) {
p = (void **)(*pprev);
free(pprev);
pprev = p;
}
TEST_ASSERT_EQUAL(20, thrown_value);
#else
// if emergency pool is disabled we should never get here,
// expect abort() due to lack of memory for new exception
TEST_ASSERT_TRUE(0 == 1);
#endif
}
2019-04-24 09:02:25 -04:00
#else // !CONFIG_COMPILER_CXX_EXCEPTIONS
TEST_CASE("std::out_of_range exception when -fno-exceptions", "[cxx][reset=abort,SW_CPU_RESET]")
{
std::vector<int> v(10);
v.at(20) = 42;
TEST_FAIL_MESSAGE("Unreachable because we are aborted on the line above");
}
TEST_CASE("std::bad_alloc exception when -fno-exceptions", "[cxx][reset=abort,SW_CPU_RESET]")
{
std::string s = std::string(2000000000, 'a');
(void)s;
TEST_FAIL_MESSAGE("Unreachable because we are aborted on the line above");
}
#endif
/* These test cases pull a lot of code from libstdc++ and are disabled for now
*/
#if 0
#include <iostream>
#include <functional>
TEST_CASE("can use iostreams", "[cxx]")
{
std::cout << "hello world";
}
TEST_CASE("can call std::function and bind", "[cxx]")
{
int outer = 1;
std::function<int(int)> fn = [&outer](int x) -> int {
return x + outer;
};
outer = 5;
TEST_ASSERT_EQUAL(6, fn(1));
auto bound = std::bind(fn, outer);
outer = 10;
TEST_ASSERT_EQUAL(15, bound());
}
#endif
/* Tests below are done in the compile time, don't actually get run. */
/* Check whether a enumerator flag can be used in C++ */
template<typename T> __attribute__((unused)) static void test_binary_operators()
{
T flag1 = (T)0;
T flag2 = (T)0;
flag1 = ~flag1;
flag1 = flag1 | flag2;
flag1 = flag1 & flag2;
flag1 = flag1 ^ flag2;
flag1 = flag1 >> 2;
flag1 = flag1 << 2;
flag1 |= flag2;
flag1 &= flag2;
flag1 ^= flag2;
flag1 >>= 2;
flag1 <<= 2;
}
//Add more types here. If any flags cannot pass the build, use FLAG_ATTR in esp_attr.h
#include "hal/timer_types.h"
template void test_binary_operators<timer_intr_t>();