test_cxx.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. #include <vector>
  2. #include <numeric>
  3. #include <stdexcept>
  4. #include <string>
  5. #include "unity.h"
  6. #include "esp_log.h"
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/task.h"
  9. #include "freertos/semphr.h"
  10. #include "soc/soc.h"
  11. TEST_CASE("can use new and delete", "[cxx]")
  12. {
  13. int* int_p = new int(10);
  14. delete int_p;
  15. int* int_array = new int[10];
  16. delete[] int_array;
  17. }
  18. class Base
  19. {
  20. public:
  21. virtual ~Base() {}
  22. virtual void foo() = 0;
  23. };
  24. class Derived : public Base
  25. {
  26. public:
  27. virtual void foo() { }
  28. };
  29. TEST_CASE("can call virtual functions", "[cxx]")
  30. {
  31. Derived d;
  32. Base& b = static_cast<Base&>(d);
  33. b.foo();
  34. }
  35. TEST_CASE("can use std::vector", "[cxx]")
  36. {
  37. std::vector<int> v(10, 1);
  38. v[0] = 42;
  39. TEST_ASSERT_EQUAL(51, std::accumulate(std::begin(v), std::end(v), 0));
  40. }
  41. /* Note: When first exception (in system) is thrown this test produces memory leaks report (~300 bytes):
  42. - 8 bytes are allocated by __cxa_get_globals() to keep __cxa_eh_globals
  43. - 16 bytes are allocated by pthread_setspecific() which is called by __cxa_get_globals() to init TLS var for __cxa_eh_globals
  44. - 88 bytes are allocated by pthread_setspecific() to init internal lock
  45. - some more memory...
  46. */
  47. #ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
  48. #if CONFIG_IDF_TARGET_ESP32
  49. #define LEAKS "300"
  50. #elif CONFIG_IDF_TARGET_ESP32S2
  51. #define LEAKS "800"
  52. #elif CONFIG_IDF_TARGET_ESP32C3
  53. #define LEAKS "600"
  54. #else
  55. #error "unknown target in CXX tests, can't set leaks threshold"
  56. #endif
  57. TEST_CASE("c++ exceptions work", "[cxx] [exceptions] [leaks=" LEAKS "]")
  58. {
  59. int thrown_value;
  60. try {
  61. throw 20;
  62. } catch (int e) {
  63. thrown_value = e;
  64. }
  65. TEST_ASSERT_EQUAL(20, thrown_value);
  66. printf("OK?\n");
  67. }
  68. TEST_CASE("c++ bool exception", "[cxx] [exceptions] [leaks=" LEAKS "]")
  69. {
  70. bool thrown_value = false;
  71. try {
  72. throw true;
  73. } catch (bool e) {
  74. thrown_value = e;
  75. }
  76. TEST_ASSERT_EQUAL(true, thrown_value);
  77. printf("OK?\n");
  78. }
  79. TEST_CASE("c++ void exception", "[cxx] [exceptions] [leaks=" LEAKS "]")
  80. {
  81. void* thrown_value = 0;
  82. try {
  83. throw (void*) 47;
  84. } catch (void* e) {
  85. thrown_value = e;
  86. }
  87. TEST_ASSERT_EQUAL(47, thrown_value);
  88. printf("OK?\n");
  89. }
  90. TEST_CASE("c++ uint64_t exception", "[cxx] [exceptions] [leaks=" LEAKS "]")
  91. {
  92. uint64_t thrown_value = 0;
  93. try {
  94. throw (uint64_t) 47;
  95. } catch (uint64_t e) {
  96. thrown_value = e;
  97. }
  98. TEST_ASSERT_EQUAL(47, thrown_value);
  99. printf("OK?\n");
  100. }
  101. TEST_CASE("c++ char exception", "[cxx] [exceptions] [leaks=" LEAKS "]")
  102. {
  103. char thrown_value = '0';
  104. try {
  105. throw '/';
  106. } catch (char e) {
  107. thrown_value = e;
  108. }
  109. TEST_ASSERT_EQUAL('/', thrown_value);
  110. printf("OK?\n");
  111. }
  112. TEST_CASE("c++ wchar exception", "[cxx] [exceptions] [leaks=" LEAKS "]")
  113. {
  114. wchar_t thrown_value = 0;
  115. try {
  116. throw (wchar_t) 47;
  117. } catch (wchar_t e) {
  118. thrown_value = e;
  119. }
  120. TEST_ASSERT_EQUAL((wchar_t) 47, thrown_value);
  121. printf("OK?\n");
  122. }
  123. TEST_CASE("c++ float exception", "[cxx] [exceptions] [leaks=" LEAKS "]")
  124. {
  125. float thrown_value = 0;
  126. try {
  127. throw 23.5f;
  128. } catch (float e) {
  129. thrown_value = e;
  130. }
  131. TEST_ASSERT_EQUAL(23.5, thrown_value);
  132. printf("OK?\n");
  133. }
  134. TEST_CASE("c++ double exception", "[cxx] [exceptions] [leaks=" LEAKS "]")
  135. {
  136. double thrown_value = 0;
  137. try {
  138. throw 23.5d;
  139. } catch (double e) {
  140. thrown_value = e;
  141. }
  142. TEST_ASSERT_EQUAL(23.5d, thrown_value);
  143. printf("OK?\n");
  144. }
  145. TEST_CASE("c++ const char* exception", "[cxx] [exceptions] [leaks=" LEAKS "]")
  146. {
  147. const char *thrown_value = 0;
  148. try {
  149. throw "Hi :)";
  150. } catch (const char *e) {
  151. thrown_value = e;
  152. }
  153. TEST_ASSERT_EQUAL_STRING("Hi :)", thrown_value);
  154. printf("OK?\n");
  155. }
  156. struct NonExcTypeThrowee {
  157. int value;
  158. public:
  159. NonExcTypeThrowee(int value) : value(value) { }
  160. };
  161. TEST_CASE("c++ any class exception", "[cxx] [exceptions] [leaks=" LEAKS "]")
  162. {
  163. int thrown_value = 0;
  164. try {
  165. throw NonExcTypeThrowee(47);
  166. } catch (NonExcTypeThrowee &e) {
  167. thrown_value = e.value;
  168. }
  169. TEST_ASSERT_EQUAL(47, thrown_value);
  170. printf("OK?\n");
  171. }
  172. struct ExcTypeThrowee : public std::exception {
  173. int value;
  174. public:
  175. ExcTypeThrowee(int value) : value(value) { }
  176. };
  177. TEST_CASE("c++ std::exception child", "[cxx] [exceptions] [leaks=" LEAKS "]")
  178. {
  179. int thrown_value = 0;
  180. try {
  181. throw ExcTypeThrowee(47);
  182. } catch (ExcTypeThrowee &e) {
  183. thrown_value = e.value;
  184. }
  185. TEST_ASSERT_EQUAL(47, thrown_value);
  186. printf("OK?\n");
  187. }
  188. TEST_CASE("c++ exceptions emergency pool", "[cxx] [exceptions] [ignore] [leaks=" LEAKS "]")
  189. {
  190. void **p, **pprev = NULL;
  191. int thrown_value = 0;
  192. // throw first exception to ensure that all initial allocations are made
  193. try
  194. {
  195. throw 33;
  196. }
  197. catch (int e)
  198. {
  199. thrown_value = e;
  200. }
  201. TEST_ASSERT_EQUAL(33, thrown_value);
  202. // consume all dynamic memory
  203. while ((p = (void **)malloc(sizeof(void *)))) {
  204. if (pprev) {
  205. *p = pprev;
  206. } else {
  207. *p = NULL;
  208. }
  209. pprev = p;
  210. }
  211. try
  212. {
  213. throw 20;
  214. }
  215. catch (int e)
  216. {
  217. thrown_value = e;
  218. printf("Got exception %d\n", thrown_value);
  219. }
  220. #if CONFIG_COMPILER_CXX_EXCEPTIONS_EMG_POOL_SIZE > 0
  221. // free all memory
  222. while (pprev) {
  223. p = (void **)(*pprev);
  224. free(pprev);
  225. pprev = p;
  226. }
  227. TEST_ASSERT_EQUAL(20, thrown_value);
  228. #else
  229. // if emergency pool is disabled we should never get here,
  230. // expect abort() due to lack of memory for new exception
  231. TEST_ASSERT_TRUE(0 == 1);
  232. #endif
  233. }
  234. #else // !CONFIG_COMPILER_CXX_EXCEPTIONS
  235. TEST_CASE("std::out_of_range exception when -fno-exceptions", "[cxx][reset=abort,SW_CPU_RESET]")
  236. {
  237. std::vector<int> v(10);
  238. v.at(20) = 42;
  239. TEST_FAIL_MESSAGE("Unreachable because we are aborted on the line above");
  240. }
  241. TEST_CASE("std::bad_alloc exception when -fno-exceptions", "[cxx][reset=abort,SW_CPU_RESET]")
  242. {
  243. std::string s = std::string(2000000000, 'a');
  244. (void)s;
  245. TEST_FAIL_MESSAGE("Unreachable because we are aborted on the line above");
  246. }
  247. #endif
  248. /* These test cases pull a lot of code from libstdc++ and are disabled for now
  249. */
  250. #if 0
  251. #include <iostream>
  252. #include <functional>
  253. TEST_CASE("can use iostreams", "[cxx]")
  254. {
  255. std::cout << "hello world";
  256. }
  257. TEST_CASE("can call std::function and bind", "[cxx]")
  258. {
  259. int outer = 1;
  260. std::function<int(int)> fn = [&outer](int x) -> int {
  261. return x + outer;
  262. };
  263. outer = 5;
  264. TEST_ASSERT_EQUAL(6, fn(1));
  265. auto bound = std::bind(fn, outer);
  266. outer = 10;
  267. TEST_ASSERT_EQUAL(15, bound());
  268. }
  269. #endif
  270. /* Tests below are done in the compile time, don't actually get run. */
  271. /* Check whether a enumerator flag can be used in C++ */
  272. template<typename T> __attribute__((unused)) static void test_binary_operators()
  273. {
  274. T flag1 = (T)0;
  275. T flag2 = (T)0;
  276. flag1 = ~flag1;
  277. flag1 = flag1 | flag2;
  278. flag1 = flag1 & flag2;
  279. flag1 = flag1 ^ flag2;
  280. flag1 = flag1 >> 2;
  281. flag1 = flag1 << 2;
  282. flag1 |= flag2;
  283. flag1 &= flag2;
  284. flag1 ^= flag2;
  285. flag1 >>= 2;
  286. flag1 <<= 2;
  287. }
  288. //Add more types here. If any flags cannot pass the build, use FLAG_ATTR in esp_attr.h
  289. #include "hal/timer_types.h"
  290. template void test_binary_operators<timer_intr_t>();