test_cxx.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. static const char* TAG = "cxx";
  12. TEST_CASE("can use new and delete", "[cxx]")
  13. {
  14. int* int_p = new int(10);
  15. delete int_p;
  16. int* int_array = new int[10];
  17. delete[] int_array;
  18. }
  19. class Base
  20. {
  21. public:
  22. virtual ~Base() {}
  23. virtual void foo() = 0;
  24. };
  25. class Derived : public Base
  26. {
  27. public:
  28. virtual void foo() { }
  29. };
  30. TEST_CASE("can call virtual functions", "[cxx]")
  31. {
  32. Derived d;
  33. Base& b = static_cast<Base&>(d);
  34. b.foo();
  35. }
  36. class NonPOD
  37. {
  38. public:
  39. NonPOD(int a_) : a(a_) { }
  40. int a;
  41. };
  42. static int non_pod_test_helper(int new_val)
  43. {
  44. static NonPOD non_pod(42);
  45. int ret = non_pod.a;
  46. non_pod.a = new_val;
  47. return ret;
  48. }
  49. TEST_CASE("can use static initializers for non-POD types", "[cxx]")
  50. {
  51. TEST_ASSERT_EQUAL(42, non_pod_test_helper(1));
  52. TEST_ASSERT_EQUAL(1, non_pod_test_helper(0));
  53. }
  54. TEST_CASE("can use std::vector", "[cxx]")
  55. {
  56. std::vector<int> v(10, 1);
  57. v[0] = 42;
  58. TEST_ASSERT_EQUAL(51, std::accumulate(std::begin(v), std::end(v), 0));
  59. }
  60. /*
  61. * This test exercises static initialization guards for two objects.
  62. * For each object, 4 tasks are created which attempt to perform static initialization.
  63. * We check that constructor runs only once for each object.
  64. */
  65. static SemaphoreHandle_t s_slow_init_sem = NULL;
  66. template<int obj>
  67. class SlowInit
  68. {
  69. public:
  70. SlowInit(int arg) {
  71. ESP_LOGD(TAG, "init obj=%d start, arg=%d\n", obj, arg);
  72. vTaskDelay(300/portTICK_PERIOD_MS);
  73. TEST_ASSERT_EQUAL(-1, mInitBy);
  74. TEST_ASSERT_EQUAL(0, mInitCount);
  75. mInitBy = arg;
  76. ++mInitCount;
  77. ESP_LOGD(TAG, "init obj=%d done\n", obj);
  78. }
  79. static void task(void* arg) {
  80. int taskId = reinterpret_cast<int>(arg);
  81. ESP_LOGD(TAG, "obj=%d before static init, task=%d\n", obj, taskId);
  82. static SlowInit slowinit(taskId);
  83. ESP_LOGD(TAG, "obj=%d after static init, task=%d\n", obj, taskId);
  84. xSemaphoreGive(s_slow_init_sem);
  85. vTaskDelete(NULL);
  86. }
  87. private:
  88. static int mInitBy;
  89. static int mInitCount;
  90. };
  91. template<> int SlowInit<1>::mInitBy = -1;
  92. template<> int SlowInit<1>::mInitCount = 0;
  93. template<> int SlowInit<2>::mInitBy = -1;
  94. template<> int SlowInit<2>::mInitCount = 0;
  95. template<int obj>
  96. static int start_slow_init_task(int id, int affinity)
  97. {
  98. return xTaskCreatePinnedToCore(&SlowInit<obj>::task, "slow_init", 2048,
  99. reinterpret_cast<void*>(id), 3, NULL, affinity) ? 1 : 0;
  100. }
  101. TEST_CASE("static initialization guards work as expected", "[cxx]")
  102. {
  103. s_slow_init_sem = xSemaphoreCreateCounting(10, 0);
  104. TEST_ASSERT_NOT_NULL(s_slow_init_sem);
  105. int task_count = 0;
  106. // four tasks competing for static initialization of one object
  107. task_count += start_slow_init_task<1>(0, PRO_CPU_NUM);
  108. #if portNUM_PROCESSORS == 2
  109. task_count += start_slow_init_task<1>(1, APP_CPU_NUM);
  110. #endif
  111. task_count += start_slow_init_task<1>(2, PRO_CPU_NUM);
  112. task_count += start_slow_init_task<1>(3, tskNO_AFFINITY);
  113. // four tasks competing for static initialization of another object
  114. task_count += start_slow_init_task<2>(0, PRO_CPU_NUM);
  115. #if portNUM_PROCESSORS == 2
  116. task_count += start_slow_init_task<2>(1, APP_CPU_NUM);
  117. #endif
  118. task_count += start_slow_init_task<2>(2, PRO_CPU_NUM);
  119. task_count += start_slow_init_task<2>(3, tskNO_AFFINITY);
  120. // All tasks should
  121. for (int i = 0; i < task_count; ++i) {
  122. TEST_ASSERT_TRUE(xSemaphoreTake(s_slow_init_sem, 500/portTICK_PERIOD_MS));
  123. }
  124. vSemaphoreDelete(s_slow_init_sem);
  125. vTaskDelay(10); // Allow tasks to clean up, avoids race with leak detector
  126. }
  127. struct GlobalInitTest
  128. {
  129. GlobalInitTest() : index(order++) {
  130. }
  131. int index;
  132. static int order;
  133. };
  134. int GlobalInitTest::order = 0;
  135. GlobalInitTest g_init_test1;
  136. GlobalInitTest g_init_test2;
  137. GlobalInitTest g_init_test3;
  138. TEST_CASE("global initializers run in the correct order", "[cxx]")
  139. {
  140. TEST_ASSERT_EQUAL(0, g_init_test1.index);
  141. TEST_ASSERT_EQUAL(1, g_init_test2.index);
  142. TEST_ASSERT_EQUAL(2, g_init_test3.index);
  143. }
  144. struct StaticInitTestBeforeScheduler
  145. {
  146. StaticInitTestBeforeScheduler()
  147. {
  148. static int first_init_order = getOrder();
  149. index = first_init_order;
  150. }
  151. int getOrder()
  152. {
  153. return order++;
  154. }
  155. int index;
  156. static int order;
  157. };
  158. int StaticInitTestBeforeScheduler::order = 1;
  159. StaticInitTestBeforeScheduler g_static_init_test1;
  160. StaticInitTestBeforeScheduler g_static_init_test2;
  161. StaticInitTestBeforeScheduler g_static_init_test3;
  162. TEST_CASE("before scheduler has started, static initializers work correctly", "[cxx]")
  163. {
  164. TEST_ASSERT_EQUAL(1, g_static_init_test1.index);
  165. TEST_ASSERT_EQUAL(1, g_static_init_test2.index);
  166. TEST_ASSERT_EQUAL(1, g_static_init_test3.index);
  167. TEST_ASSERT_EQUAL(2, StaticInitTestBeforeScheduler::order);
  168. }
  169. #ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
  170. TEST_CASE("c++ exceptions work", "[cxx]")
  171. {
  172. /* Note: When first exception (in system) is thrown this test produces memory leaks report (~500 bytes):
  173. - 392 bytes (can vary) as libunwind allocates memory to keep stack frames info to handle exceptions.
  174. This info is kept until global destructors are called by __do_global_dtors_aux()
  175. - 8 bytes are allocated by __cxa_get_globals() to keep __cxa_eh_globals
  176. - 16 bytes are allocated by pthread_setspecific() which is called by __cxa_get_globals() to init TLS var for __cxa_eh_globals
  177. - 88 bytes are allocated by pthread_setspecific() to init internal lock
  178. */
  179. int thrown_value;
  180. try
  181. {
  182. throw 20;
  183. }
  184. catch (int e)
  185. {
  186. thrown_value = e;
  187. }
  188. TEST_ASSERT_EQUAL(20, thrown_value);
  189. printf("OK?\n");
  190. }
  191. TEST_CASE("c++ exceptions emergency pool", "[cxx] [ignore]")
  192. {
  193. /* Note: When first exception (in system) is thrown this test produces memory leaks report (~500 bytes):
  194. - 392 bytes (can vary) as libunwind allocates memory to keep stack frames info to handle exceptions.
  195. This info is kept until global destructors are called by __do_global_dtors_aux()
  196. - 8 bytes are allocated by __cxa_get_globals() to keep __cxa_eh_globals
  197. - 16 bytes are allocated by pthread_setspecific() which is called by __cxa_get_globals() to init TLS var for __cxa_eh_globals
  198. - 88 bytes are allocated by pthread_setspecific() to init internal lock
  199. */
  200. void **p, **pprev = NULL;
  201. int thrown_value = 0;
  202. // throw first exception to ensure that all initial allocations are made
  203. try
  204. {
  205. throw 33;
  206. }
  207. catch (int e)
  208. {
  209. thrown_value = e;
  210. }
  211. TEST_ASSERT_EQUAL(33, thrown_value);
  212. // consume all dynamic memory
  213. while ((p = (void **)malloc(sizeof(void *)))) {
  214. if (pprev) {
  215. *p = pprev;
  216. } else {
  217. *p = NULL;
  218. }
  219. pprev = p;
  220. }
  221. try
  222. {
  223. throw 20;
  224. }
  225. catch (int e)
  226. {
  227. thrown_value = e;
  228. printf("Got exception %d\n", thrown_value);
  229. }
  230. #if CONFIG_COMPILER_CXX_EXCEPTIONS_EMG_POOL_SIZE > 0
  231. // free all memory
  232. while (pprev) {
  233. p = (void **)(*pprev);
  234. free(pprev);
  235. pprev = p;
  236. }
  237. TEST_ASSERT_EQUAL(20, thrown_value);
  238. #else
  239. // if emergency pool is disabled we should never get here,
  240. // expect abort() due to lack of memory for new exception
  241. TEST_ASSERT_TRUE(0 == 1);
  242. #endif
  243. }
  244. #else // !CONFIG_COMPILER_CXX_EXCEPTIONS
  245. TEST_CASE("std::out_of_range exception when -fno-exceptions", "[cxx][reset=abort,SW_CPU_RESET]")
  246. {
  247. std::vector<int> v(10);
  248. v.at(20) = 42;
  249. TEST_FAIL_MESSAGE("Unreachable because we are aborted on the line above");
  250. }
  251. TEST_CASE("std::bad_alloc exception when -fno-exceptions", "[cxx][reset=abort,SW_CPU_RESET]")
  252. {
  253. std::string s = std::string(2000000000, 'a');
  254. (void)s;
  255. TEST_FAIL_MESSAGE("Unreachable because we are aborted on the line above");
  256. }
  257. #endif
  258. /* These test cases pull a lot of code from libstdc++ and are disabled for now
  259. */
  260. #if 0
  261. #include <iostream>
  262. #include <functional>
  263. TEST_CASE("can use iostreams", "[cxx]")
  264. {
  265. std::cout << "hello world";
  266. }
  267. TEST_CASE("can call std::function and bind", "[cxx]")
  268. {
  269. int outer = 1;
  270. std::function<int(int)> fn = [&outer](int x) -> int {
  271. return x + outer;
  272. };
  273. outer = 5;
  274. TEST_ASSERT_EQUAL(6, fn(1));
  275. auto bound = std::bind(fn, outer);
  276. outer = 10;
  277. TEST_ASSERT_EQUAL(15, bound());
  278. }
  279. #endif