test_cxx.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. /* Note: When first exception (in system) is thrown this test produces memory leaks report (~500 bytes):
  170. - 392 bytes (can vary) as libunwind allocates memory to keep stack frames info to handle exceptions.
  171. This info is kept until global destructors are called by __do_global_dtors_aux()
  172. - 8 bytes are allocated by __cxa_get_globals() to keep __cxa_eh_globals
  173. - 16 bytes are allocated by pthread_setspecific() which is called by __cxa_get_globals() to init TLS var for __cxa_eh_globals
  174. - 88 bytes are allocated by pthread_setspecific() to init internal lock
  175. */
  176. #ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
  177. TEST_CASE("c++ exceptions work", "[cxx] [exceptions] [leaks=800]")
  178. {
  179. int thrown_value;
  180. try {
  181. throw 20;
  182. } catch (int e) {
  183. thrown_value = e;
  184. }
  185. TEST_ASSERT_EQUAL(20, thrown_value);
  186. printf("OK?\n");
  187. }
  188. TEST_CASE("c++ bool exception", "[cxx] [exceptions] [leaks=800]")
  189. {
  190. bool thrown_value = false;
  191. try {
  192. throw true;
  193. } catch (bool e) {
  194. thrown_value = e;
  195. }
  196. TEST_ASSERT_EQUAL(true, thrown_value);
  197. printf("OK?\n");
  198. }
  199. TEST_CASE("c++ void exception", "[cxx] [exceptions] [leaks=800]")
  200. {
  201. void* thrown_value = 0;
  202. try {
  203. throw (void*) 47;
  204. } catch (void* e) {
  205. thrown_value = e;
  206. }
  207. TEST_ASSERT_EQUAL(47, thrown_value);
  208. printf("OK?\n");
  209. }
  210. TEST_CASE("c++ uint64_t exception", "[cxx] [exceptions] [leaks=800]")
  211. {
  212. uint64_t thrown_value = 0;
  213. try {
  214. throw (uint64_t) 47;
  215. } catch (uint64_t e) {
  216. thrown_value = e;
  217. }
  218. TEST_ASSERT_EQUAL(47, thrown_value);
  219. printf("OK?\n");
  220. }
  221. TEST_CASE("c++ char exception", "[cxx] [exceptions] [leaks=800]")
  222. {
  223. char thrown_value = '0';
  224. try {
  225. throw '/';
  226. } catch (char e) {
  227. thrown_value = e;
  228. }
  229. TEST_ASSERT_EQUAL('/', thrown_value);
  230. printf("OK?\n");
  231. }
  232. TEST_CASE("c++ wchar exception", "[cxx] [exceptions] [leaks=800]")
  233. {
  234. wchar_t thrown_value = 0;
  235. try {
  236. throw (wchar_t) 47;
  237. } catch (wchar_t e) {
  238. thrown_value = e;
  239. }
  240. TEST_ASSERT_EQUAL((wchar_t) 47, thrown_value);
  241. printf("OK?\n");
  242. }
  243. TEST_CASE("c++ float exception", "[cxx] [exceptions] [leaks=800]")
  244. {
  245. float thrown_value = 0;
  246. try {
  247. throw 23.5f;
  248. } catch (float e) {
  249. thrown_value = e;
  250. }
  251. TEST_ASSERT_EQUAL(23.5, thrown_value);
  252. printf("OK?\n");
  253. }
  254. TEST_CASE("c++ double exception", "[cxx] [exceptions] [leaks=800]")
  255. {
  256. double thrown_value = 0;
  257. try {
  258. throw 23.5d;
  259. } catch (double e) {
  260. thrown_value = e;
  261. }
  262. TEST_ASSERT_EQUAL(23.5d, thrown_value);
  263. printf("OK?\n");
  264. }
  265. TEST_CASE("c++ const char* exception", "[cxx] [exceptions] [leaks=800]")
  266. {
  267. const char *thrown_value = 0;
  268. try {
  269. throw "Hi :)";
  270. } catch (const char *e) {
  271. thrown_value = e;
  272. }
  273. TEST_ASSERT_EQUAL_STRING("Hi :)", thrown_value);
  274. printf("OK?\n");
  275. }
  276. struct NonExcTypeThrowee {
  277. int value;
  278. public:
  279. NonExcTypeThrowee(int value) : value(value) { }
  280. };
  281. TEST_CASE("c++ any class exception", "[cxx] [exceptions] [leaks=800]")
  282. {
  283. int thrown_value = 0;
  284. try {
  285. throw NonExcTypeThrowee(47);
  286. } catch (NonExcTypeThrowee &e) {
  287. thrown_value = e.value;
  288. }
  289. TEST_ASSERT_EQUAL(47, thrown_value);
  290. printf("OK?\n");
  291. }
  292. struct ExcTypeThrowee : public std::exception {
  293. int value;
  294. public:
  295. ExcTypeThrowee(int value) : value(value) { }
  296. };
  297. TEST_CASE("c++ std::exception child", "[cxx] [exceptions] [leaks=800]")
  298. {
  299. int thrown_value = 0;
  300. try {
  301. throw ExcTypeThrowee(47);
  302. } catch (ExcTypeThrowee &e) {
  303. thrown_value = e.value;
  304. }
  305. TEST_ASSERT_EQUAL(47, thrown_value);
  306. printf("OK?\n");
  307. }
  308. TEST_CASE("c++ exceptions emergency pool", "[cxx] [exceptions] [ignore]")
  309. {
  310. void **p, **pprev = NULL;
  311. int thrown_value = 0;
  312. // throw first exception to ensure that all initial allocations are made
  313. try
  314. {
  315. throw 33;
  316. }
  317. catch (int e)
  318. {
  319. thrown_value = e;
  320. }
  321. TEST_ASSERT_EQUAL(33, thrown_value);
  322. // consume all dynamic memory
  323. while ((p = (void **)malloc(sizeof(void *)))) {
  324. if (pprev) {
  325. *p = pprev;
  326. } else {
  327. *p = NULL;
  328. }
  329. pprev = p;
  330. }
  331. try
  332. {
  333. throw 20;
  334. }
  335. catch (int e)
  336. {
  337. thrown_value = e;
  338. printf("Got exception %d\n", thrown_value);
  339. }
  340. #if CONFIG_COMPILER_CXX_EXCEPTIONS_EMG_POOL_SIZE > 0
  341. // free all memory
  342. while (pprev) {
  343. p = (void **)(*pprev);
  344. free(pprev);
  345. pprev = p;
  346. }
  347. TEST_ASSERT_EQUAL(20, thrown_value);
  348. #else
  349. // if emergency pool is disabled we should never get here,
  350. // expect abort() due to lack of memory for new exception
  351. TEST_ASSERT_TRUE(0 == 1);
  352. #endif
  353. }
  354. #else // !CONFIG_COMPILER_CXX_EXCEPTIONS
  355. TEST_CASE("std::out_of_range exception when -fno-exceptions", "[cxx][reset=abort,SW_CPU_RESET]")
  356. {
  357. std::vector<int> v(10);
  358. v.at(20) = 42;
  359. TEST_FAIL_MESSAGE("Unreachable because we are aborted on the line above");
  360. }
  361. TEST_CASE("std::bad_alloc exception when -fno-exceptions", "[cxx][reset=abort,SW_CPU_RESET]")
  362. {
  363. std::string s = std::string(2000000000, 'a');
  364. (void)s;
  365. TEST_FAIL_MESSAGE("Unreachable because we are aborted on the line above");
  366. }
  367. #endif
  368. /* These test cases pull a lot of code from libstdc++ and are disabled for now
  369. */
  370. #if 0
  371. #include <iostream>
  372. #include <functional>
  373. TEST_CASE("can use iostreams", "[cxx]")
  374. {
  375. std::cout << "hello world";
  376. }
  377. TEST_CASE("can call std::function and bind", "[cxx]")
  378. {
  379. int outer = 1;
  380. std::function<int(int)> fn = [&outer](int x) -> int {
  381. return x + outer;
  382. };
  383. outer = 5;
  384. TEST_ASSERT_EQUAL(6, fn(1));
  385. auto bound = std::bind(fn, outer);
  386. outer = 10;
  387. TEST_ASSERT_EQUAL(15, bound());
  388. }
  389. #endif
  390. /* Tests below are done in the compile time, don't actually get run. */
  391. /* Check whether a enumerator flag can be used in C++ */
  392. template<typename T> __attribute__((unused)) static void test_binary_operators()
  393. {
  394. T flag1 = (T)0;
  395. T flag2 = (T)0;
  396. flag1 = ~flag1;
  397. flag1 = flag1 | flag2;
  398. flag1 = flag1 & flag2;
  399. flag1 = flag1 ^ flag2;
  400. flag1 = flag1 >> 2;
  401. flag1 = flag1 << 2;
  402. flag1 |= flag2;
  403. flag1 &= flag2;
  404. flag1 ^= flag2;
  405. flag1 >>= 2;
  406. flag1 <<= 2;
  407. }
  408. //Add more types here. If any flags cannot pass the build, use FLAG_ATTR in esp_attr.h
  409. #include "hal/timer_types.h"
  410. template void test_binary_operators<timer_intr_t>();