test_exception.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdexcept>
  7. #include "unity.h"
  8. #include "unity_test_utils.h"
  9. /* Note: When first exception (in system) is thrown this test produces memory leaks report (~300 bytes):
  10. - 8 bytes are allocated by __cxa_get_globals() to keep __cxa_eh_globals
  11. - 16 bytes are allocated by pthread_setspecific() which is called by __cxa_get_globals() to init TLS var for __cxa_eh_globals
  12. - 88 bytes are allocated by pthread_setspecific() to init internal lock
  13. - some more memory...
  14. */
  15. #if CONFIG_IDF_TARGET_ESP32
  16. #define LEAKS (300)
  17. #elif CONFIG_IDF_TARGET_ESP32S2
  18. #define LEAKS (800)
  19. #elif CONFIG_IDF_TARGET_ESP32C3
  20. #define LEAKS (700)
  21. #else
  22. #error "unknown target in CXX tests, can't set leaks threshold"
  23. #endif
  24. extern "C" void setUp()
  25. {
  26. unity_utils_record_free_mem();
  27. }
  28. extern "C" void tearDown()
  29. {
  30. unity_utils_evaluate_leaks_direct(LEAKS);
  31. }
  32. TEST_CASE("c++ exceptions work", "[cxx] [exceptions]")
  33. {
  34. int thrown_value;
  35. try {
  36. throw 20;
  37. } catch (int e) {
  38. thrown_value = e;
  39. }
  40. TEST_ASSERT_EQUAL(20, thrown_value);
  41. }
  42. TEST_CASE("c++ bool exception", "[cxx] [exceptions]")
  43. {
  44. bool thrown_value = false;
  45. try {
  46. throw true;
  47. } catch (bool e) {
  48. thrown_value = e;
  49. }
  50. TEST_ASSERT_EQUAL(true, thrown_value);
  51. }
  52. TEST_CASE("c++ void exception", "[cxx] [exceptions]")
  53. {
  54. void* thrown_value = 0;
  55. try {
  56. throw (void*) 47;
  57. } catch (void* e) {
  58. thrown_value = e;
  59. }
  60. TEST_ASSERT_EQUAL(47, thrown_value);
  61. }
  62. TEST_CASE("c++ uint64_t exception", "[cxx] [exceptions]")
  63. {
  64. uint64_t thrown_value = 0;
  65. try {
  66. throw (uint64_t) 47;
  67. } catch (uint64_t e) {
  68. thrown_value = e;
  69. }
  70. TEST_ASSERT_EQUAL(47, thrown_value);
  71. }
  72. TEST_CASE("c++ char exception", "[cxx] [exceptions]")
  73. {
  74. char thrown_value = '0';
  75. try {
  76. throw '/';
  77. } catch (char e) {
  78. thrown_value = e;
  79. }
  80. TEST_ASSERT_EQUAL('/', thrown_value);
  81. }
  82. TEST_CASE("c++ wchar exception", "[cxx] [exceptions]")
  83. {
  84. wchar_t thrown_value = 0;
  85. try {
  86. throw (wchar_t) 47;
  87. } catch (wchar_t e) {
  88. thrown_value = e;
  89. }
  90. TEST_ASSERT_EQUAL((wchar_t) 47, thrown_value);
  91. }
  92. TEST_CASE("c++ float exception", "[cxx] [exceptions]")
  93. {
  94. float thrown_value = 0;
  95. try {
  96. throw 23.5f;
  97. } catch (float e) {
  98. thrown_value = e;
  99. }
  100. TEST_ASSERT_EQUAL(23.5, thrown_value);
  101. }
  102. TEST_CASE("c++ double exception", "[cxx] [exceptions]")
  103. {
  104. double thrown_value = 0;
  105. try {
  106. throw 23.5d;
  107. } catch (double e) {
  108. thrown_value = e;
  109. }
  110. TEST_ASSERT_EQUAL(23.5d, thrown_value);
  111. }
  112. TEST_CASE("c++ const char* exception", "[cxx] [exceptions]")
  113. {
  114. const char *thrown_value = 0;
  115. try {
  116. throw "Hi :)";
  117. } catch (const char *e) {
  118. thrown_value = e;
  119. }
  120. TEST_ASSERT_EQUAL_STRING("Hi :)", thrown_value);
  121. }
  122. struct NonExcTypeThrowee {
  123. int value;
  124. public:
  125. NonExcTypeThrowee(int value) : value(value) { }
  126. };
  127. TEST_CASE("c++ any class exception", "[cxx] [exceptions]")
  128. {
  129. int thrown_value = 0;
  130. try {
  131. throw NonExcTypeThrowee(47);
  132. } catch (NonExcTypeThrowee &e) {
  133. thrown_value = e.value;
  134. }
  135. TEST_ASSERT_EQUAL(47, thrown_value);
  136. }
  137. struct ExcTypeThrowee : public std::exception {
  138. int value;
  139. public:
  140. ExcTypeThrowee(int value) : value(value) { }
  141. };
  142. TEST_CASE("c++ std::exception child", "[cxx] [exceptions]")
  143. {
  144. int thrown_value = 0;
  145. try {
  146. throw ExcTypeThrowee(47);
  147. } catch (ExcTypeThrowee &e) {
  148. thrown_value = e.value;
  149. }
  150. TEST_ASSERT_EQUAL(47, thrown_value);
  151. }
  152. TEST_CASE("c++ exceptions emergency pool", "[cxx] [exceptions]")
  153. {
  154. void **p, **pprev = NULL;
  155. int thrown_value = 0;
  156. // throw first exception to ensure that all initial allocations are made
  157. try
  158. {
  159. throw 33;
  160. }
  161. catch (int e)
  162. {
  163. thrown_value = e;
  164. }
  165. TEST_ASSERT_EQUAL(33, thrown_value);
  166. // consume all dynamic memory
  167. while ((p = (void **)malloc(sizeof(void *)))) {
  168. if (pprev) {
  169. *p = pprev;
  170. } else {
  171. *p = NULL;
  172. }
  173. pprev = p;
  174. }
  175. try
  176. {
  177. throw 20;
  178. }
  179. catch (int e)
  180. {
  181. thrown_value = e;
  182. }
  183. #if CONFIG_COMPILER_CXX_EXCEPTIONS_EMG_POOL_SIZE > 0
  184. // free all memory
  185. while (pprev) {
  186. p = (void **)(*pprev);
  187. free(pprev);
  188. pprev = p;
  189. }
  190. TEST_ASSERT_EQUAL(20, thrown_value);
  191. #else
  192. // if emergency pool is disabled we should never get here,
  193. // expect abort() due to lack of memory for new exception
  194. TEST_ASSERT_TRUE(0 == 1);
  195. #endif
  196. }
  197. extern "C" void app_main(void)
  198. {
  199. printf("CXX EXCEPTION TEST\n");
  200. unity_run_menu();
  201. }