test_cxx.cpp 8.4 KB

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