test_cxx.cpp 7.4 KB

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