test_cxx.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. #else
  53. #error "unknown target in CXX tests, can't set leaks threshold"
  54. #endif
  55. TEST_CASE("c++ exceptions work", "[cxx] [exceptions] [leaks=" LEAKS "]")
  56. {
  57. int thrown_value;
  58. try {
  59. throw 20;
  60. } catch (int e) {
  61. thrown_value = e;
  62. }
  63. TEST_ASSERT_EQUAL(20, thrown_value);
  64. printf("OK?\n");
  65. }
  66. TEST_CASE("c++ bool exception", "[cxx] [exceptions] [leaks=" LEAKS "]")
  67. {
  68. bool thrown_value = false;
  69. try {
  70. throw true;
  71. } catch (bool e) {
  72. thrown_value = e;
  73. }
  74. TEST_ASSERT_EQUAL(true, thrown_value);
  75. printf("OK?\n");
  76. }
  77. TEST_CASE("c++ void exception", "[cxx] [exceptions] [leaks=" LEAKS "]")
  78. {
  79. void* thrown_value = 0;
  80. try {
  81. throw (void*) 47;
  82. } catch (void* e) {
  83. thrown_value = e;
  84. }
  85. TEST_ASSERT_EQUAL(47, thrown_value);
  86. printf("OK?\n");
  87. }
  88. TEST_CASE("c++ uint64_t exception", "[cxx] [exceptions] [leaks=" LEAKS "]")
  89. {
  90. uint64_t thrown_value = 0;
  91. try {
  92. throw (uint64_t) 47;
  93. } catch (uint64_t e) {
  94. thrown_value = e;
  95. }
  96. TEST_ASSERT_EQUAL(47, thrown_value);
  97. printf("OK?\n");
  98. }
  99. TEST_CASE("c++ char exception", "[cxx] [exceptions] [leaks=" LEAKS "]")
  100. {
  101. char thrown_value = '0';
  102. try {
  103. throw '/';
  104. } catch (char e) {
  105. thrown_value = e;
  106. }
  107. TEST_ASSERT_EQUAL('/', thrown_value);
  108. printf("OK?\n");
  109. }
  110. TEST_CASE("c++ wchar exception", "[cxx] [exceptions] [leaks=" LEAKS "]")
  111. {
  112. wchar_t thrown_value = 0;
  113. try {
  114. throw (wchar_t) 47;
  115. } catch (wchar_t e) {
  116. thrown_value = e;
  117. }
  118. TEST_ASSERT_EQUAL((wchar_t) 47, thrown_value);
  119. printf("OK?\n");
  120. }
  121. TEST_CASE("c++ float exception", "[cxx] [exceptions] [leaks=" LEAKS "]")
  122. {
  123. float thrown_value = 0;
  124. try {
  125. throw 23.5f;
  126. } catch (float e) {
  127. thrown_value = e;
  128. }
  129. TEST_ASSERT_EQUAL(23.5, thrown_value);
  130. printf("OK?\n");
  131. }
  132. TEST_CASE("c++ double exception", "[cxx] [exceptions] [leaks=" LEAKS "]")
  133. {
  134. double thrown_value = 0;
  135. try {
  136. throw 23.5d;
  137. } catch (double e) {
  138. thrown_value = e;
  139. }
  140. TEST_ASSERT_EQUAL(23.5d, thrown_value);
  141. printf("OK?\n");
  142. }
  143. TEST_CASE("c++ const char* exception", "[cxx] [exceptions] [leaks=" LEAKS "]")
  144. {
  145. const char *thrown_value = 0;
  146. try {
  147. throw "Hi :)";
  148. } catch (const char *e) {
  149. thrown_value = e;
  150. }
  151. TEST_ASSERT_EQUAL_STRING("Hi :)", thrown_value);
  152. printf("OK?\n");
  153. }
  154. struct NonExcTypeThrowee {
  155. int value;
  156. public:
  157. NonExcTypeThrowee(int value) : value(value) { }
  158. };
  159. TEST_CASE("c++ any class exception", "[cxx] [exceptions] [leaks=" LEAKS "]")
  160. {
  161. int thrown_value = 0;
  162. try {
  163. throw NonExcTypeThrowee(47);
  164. } catch (NonExcTypeThrowee &e) {
  165. thrown_value = e.value;
  166. }
  167. TEST_ASSERT_EQUAL(47, thrown_value);
  168. printf("OK?\n");
  169. }
  170. struct ExcTypeThrowee : public std::exception {
  171. int value;
  172. public:
  173. ExcTypeThrowee(int value) : value(value) { }
  174. };
  175. TEST_CASE("c++ std::exception child", "[cxx] [exceptions] [leaks=" LEAKS "]")
  176. {
  177. int thrown_value = 0;
  178. try {
  179. throw ExcTypeThrowee(47);
  180. } catch (ExcTypeThrowee &e) {
  181. thrown_value = e.value;
  182. }
  183. TEST_ASSERT_EQUAL(47, thrown_value);
  184. printf("OK?\n");
  185. }
  186. TEST_CASE("c++ exceptions emergency pool", "[cxx] [exceptions] [ignore] [leaks=" LEAKS "]")
  187. {
  188. void **p, **pprev = NULL;
  189. int thrown_value = 0;
  190. // throw first exception to ensure that all initial allocations are made
  191. try
  192. {
  193. throw 33;
  194. }
  195. catch (int e)
  196. {
  197. thrown_value = e;
  198. }
  199. TEST_ASSERT_EQUAL(33, thrown_value);
  200. // consume all dynamic memory
  201. while ((p = (void **)malloc(sizeof(void *)))) {
  202. if (pprev) {
  203. *p = pprev;
  204. } else {
  205. *p = NULL;
  206. }
  207. pprev = p;
  208. }
  209. try
  210. {
  211. throw 20;
  212. }
  213. catch (int e)
  214. {
  215. thrown_value = e;
  216. printf("Got exception %d\n", thrown_value);
  217. }
  218. #if CONFIG_COMPILER_CXX_EXCEPTIONS_EMG_POOL_SIZE > 0
  219. // free all memory
  220. while (pprev) {
  221. p = (void **)(*pprev);
  222. free(pprev);
  223. pprev = p;
  224. }
  225. TEST_ASSERT_EQUAL(20, thrown_value);
  226. #else
  227. // if emergency pool is disabled we should never get here,
  228. // expect abort() due to lack of memory for new exception
  229. TEST_ASSERT_TRUE(0 == 1);
  230. #endif
  231. }
  232. #define TIMEOUT 19
  233. #define RECURSION 19
  234. static esp_timer_handle_t crash_timer;
  235. static uint32_t result = 0;
  236. uint32_t calc_fac(uint32_t n) {
  237. if (n == 1 || n == 0) {
  238. return 1;
  239. } else {
  240. return n * calc_fac(n - 1);
  241. }
  242. }
  243. static void timer_cb(void *arg) {
  244. result = calc_fac(RECURSION);
  245. }
  246. // TODO: Not a unit test, refactor to integration test/system test, etc.
  247. TEST_CASE("frequent interrupts don't interfere with c++ exceptions", "[cxx] [exceptions] [leaks=816]")
  248. {// if exception workaround is disabled, this is almost guaranteed to fail
  249. const esp_timer_create_args_t timer_args {
  250. timer_cb,
  251. NULL,
  252. ESP_TIMER_TASK,
  253. "crash_timer"
  254. };
  255. TEST_ESP_OK(esp_timer_create(&timer_args, &crash_timer));
  256. TEST_ESP_OK(esp_timer_start_periodic(crash_timer, TIMEOUT));
  257. for (int i = 0; i < 500; i++) {
  258. bool thrown_value = false;
  259. try {
  260. throw true;
  261. } catch (bool e) {
  262. thrown_value = e;
  263. }
  264. if (thrown_value) {
  265. printf("ex thrown %d\n", i);
  266. } else {
  267. printf("ex not thrown\n");
  268. TEST_ASSERT(false);
  269. }
  270. }
  271. TEST_ESP_OK(esp_timer_stop(crash_timer));
  272. TEST_ESP_OK(esp_timer_delete(crash_timer));
  273. }
  274. #else // !CONFIG_COMPILER_CXX_EXCEPTIONS
  275. TEST_CASE("std::out_of_range exception when -fno-exceptions", "[cxx][reset=abort,SW_CPU_RESET]")
  276. {
  277. std::vector<int> v(10);
  278. v.at(20) = 42;
  279. TEST_FAIL_MESSAGE("Unreachable because we are aborted on the line above");
  280. }
  281. TEST_CASE("std::bad_alloc exception when -fno-exceptions", "[cxx][reset=abort,SW_CPU_RESET]")
  282. {
  283. std::string s = std::string(2000000000, 'a');
  284. (void)s;
  285. TEST_FAIL_MESSAGE("Unreachable because we are aborted on the line above");
  286. }
  287. #endif
  288. /* These test cases pull a lot of code from libstdc++ and are disabled for now
  289. */
  290. #if 0
  291. #include <iostream>
  292. #include <functional>
  293. TEST_CASE("can use iostreams", "[cxx]")
  294. {
  295. std::cout << "hello world";
  296. }
  297. TEST_CASE("can call std::function and bind", "[cxx]")
  298. {
  299. int outer = 1;
  300. std::function<int(int)> fn = [&outer](int x) -> int {
  301. return x + outer;
  302. };
  303. outer = 5;
  304. TEST_ASSERT_EQUAL(6, fn(1));
  305. auto bound = std::bind(fn, outer);
  306. outer = 10;
  307. TEST_ASSERT_EQUAL(15, bound());
  308. }
  309. #endif
  310. /* Tests below are done in the compile time, don't actually get run. */
  311. /* Check whether a enumerator flag can be used in C++ */
  312. template<typename T> __attribute__((unused)) static void test_binary_operators()
  313. {
  314. T flag1 = (T)0;
  315. T flag2 = (T)0;
  316. flag1 = ~flag1;
  317. flag1 = flag1 | flag2;
  318. flag1 = flag1 & flag2;
  319. flag1 = flag1 ^ flag2;
  320. flag1 = flag1 >> 2;
  321. flag1 = flag1 << 2;
  322. flag1 |= flag2;
  323. flag1 &= flag2;
  324. flag1 ^= flag2;
  325. flag1 >>= 2;
  326. flag1 <<= 2;
  327. }
  328. //Add more types here. If any flags cannot pass the build, use FLAG_ATTR in esp_attr.h
  329. #include "hal/timer_types.h"
  330. template void test_binary_operators<timer_intr_t>();