test_cxx_general.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <vector>
  7. #include <numeric>
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "freertos/semphr.h"
  11. #include "esp_log.h"
  12. #include "unity.h"
  13. #include "unity_test_utils.h"
  14. #include "soc/soc.h"
  15. extern "C" void setUp()
  16. {
  17. unity_utils_set_leak_level(0);
  18. unity_utils_record_free_mem();
  19. }
  20. extern "C" void tearDown()
  21. {
  22. unity_utils_evaluate_leaks();
  23. }
  24. static const char* TAG = "cxx";
  25. class NonPOD
  26. {
  27. public:
  28. NonPOD(int a_) : a(a_) { }
  29. int a;
  30. };
  31. static int non_pod_test_helper(int new_val)
  32. {
  33. static NonPOD non_pod(42);
  34. int ret = non_pod.a;
  35. non_pod.a = new_val;
  36. return ret;
  37. }
  38. // Will fail if run twice
  39. TEST_CASE("can use static initializers for non-POD types", "[restart_init]")
  40. {
  41. unity_utils_set_leak_level(300);
  42. TEST_ASSERT_EQUAL(42, non_pod_test_helper(1));
  43. TEST_ASSERT_EQUAL(1, non_pod_test_helper(0));
  44. }
  45. /*
  46. * This test exercises static initialization guards for two objects.
  47. * For each object, 4 tasks are created which attempt to perform static initialization.
  48. * We check that constructor runs only once for each object.
  49. */
  50. static SemaphoreHandle_t s_slow_init_sem = NULL;
  51. template<int obj>
  52. class SlowInit
  53. {
  54. public:
  55. SlowInit(int arg) {
  56. ESP_LOGD(TAG, "init obj=%d start, arg=%d", obj, arg);
  57. vTaskDelay(300/portTICK_PERIOD_MS);
  58. TEST_ASSERT_EQUAL(-1, mInitBy);
  59. TEST_ASSERT_EQUAL(0, mInitCount);
  60. mInitBy = arg;
  61. ++mInitCount;
  62. ESP_LOGD(TAG, "init obj=%d done", obj);
  63. }
  64. static void task(void* arg) {
  65. int taskId = reinterpret_cast<int>(arg);
  66. ESP_LOGD(TAG, "obj=%d before static init, task=%d", obj, taskId);
  67. static SlowInit slowinit(taskId);
  68. ESP_LOGD(TAG, "obj=%d after static init, task=%d", obj, taskId);
  69. xSemaphoreGive(s_slow_init_sem);
  70. vTaskDelete(NULL);
  71. }
  72. private:
  73. static int mInitBy;
  74. static int mInitCount;
  75. };
  76. template<> int SlowInit<1>::mInitBy = -1;
  77. template<> int SlowInit<1>::mInitCount = 0;
  78. template<> int SlowInit<2>::mInitBy = -1;
  79. template<> int SlowInit<2>::mInitCount = 0;
  80. template<int obj>
  81. static int start_slow_init_task(int id, int affinity)
  82. {
  83. return xTaskCreatePinnedToCore(&SlowInit<obj>::task, "slow_init", 2048,
  84. reinterpret_cast<void*>(id), 3, NULL, affinity) ? 1 : 0;
  85. }
  86. TEST_CASE("static initialization guards work as expected", "[misc]")
  87. {
  88. unity_utils_set_leak_level(300);
  89. s_slow_init_sem = xSemaphoreCreateCounting(10, 0);
  90. TEST_ASSERT_NOT_NULL(s_slow_init_sem);
  91. int task_count = 0;
  92. // four tasks competing for static initialization of one object
  93. task_count += start_slow_init_task<1>(0, PRO_CPU_NUM);
  94. #if portNUM_PROCESSORS == 2
  95. task_count += start_slow_init_task<1>(1, APP_CPU_NUM);
  96. #endif
  97. task_count += start_slow_init_task<1>(2, PRO_CPU_NUM);
  98. task_count += start_slow_init_task<1>(3, tskNO_AFFINITY);
  99. // four tasks competing for static initialization of another object
  100. task_count += start_slow_init_task<2>(0, PRO_CPU_NUM);
  101. #if portNUM_PROCESSORS == 2
  102. task_count += start_slow_init_task<2>(1, APP_CPU_NUM);
  103. #endif
  104. task_count += start_slow_init_task<2>(2, PRO_CPU_NUM);
  105. task_count += start_slow_init_task<2>(3, tskNO_AFFINITY);
  106. // All tasks should
  107. for (int i = 0; i < task_count; ++i) {
  108. TEST_ASSERT_TRUE(xSemaphoreTake(s_slow_init_sem, 500/portTICK_PERIOD_MS));
  109. }
  110. vSemaphoreDelete(s_slow_init_sem);
  111. vTaskDelay(10); // Allow tasks to clean up, avoids race with leak detector
  112. }
  113. struct GlobalInitTest
  114. {
  115. GlobalInitTest() : index(order++) {
  116. }
  117. int index;
  118. static int order;
  119. };
  120. int GlobalInitTest::order = 0;
  121. GlobalInitTest g_init_test1;
  122. GlobalInitTest g_init_test2;
  123. GlobalInitTest g_init_test3;
  124. TEST_CASE("global initializers run in the correct order", "[misc]")
  125. {
  126. TEST_ASSERT_EQUAL(0, g_init_test1.index);
  127. TEST_ASSERT_EQUAL(1, g_init_test2.index);
  128. TEST_ASSERT_EQUAL(2, g_init_test3.index);
  129. }
  130. struct StaticInitTestBeforeScheduler
  131. {
  132. StaticInitTestBeforeScheduler()
  133. {
  134. static int first_init_order = getOrder();
  135. index = first_init_order;
  136. }
  137. int getOrder()
  138. {
  139. return order++;
  140. }
  141. int index;
  142. static int order;
  143. };
  144. int StaticInitTestBeforeScheduler::order = 1;
  145. StaticInitTestBeforeScheduler g_static_init_test1;
  146. StaticInitTestBeforeScheduler g_static_init_test2;
  147. StaticInitTestBeforeScheduler g_static_init_test3;
  148. TEST_CASE("before scheduler has started, static initializers work correctly", "[misc]")
  149. {
  150. TEST_ASSERT_EQUAL(1, g_static_init_test1.index);
  151. TEST_ASSERT_EQUAL(1, g_static_init_test2.index);
  152. TEST_ASSERT_EQUAL(1, g_static_init_test3.index);
  153. TEST_ASSERT_EQUAL(2, StaticInitTestBeforeScheduler::order);
  154. }
  155. struct PriorityInitTest
  156. {
  157. PriorityInitTest()
  158. {
  159. index = getOrder();
  160. }
  161. int getOrder()
  162. {
  163. return order++;
  164. }
  165. int index;
  166. static int order;
  167. };
  168. int PriorityInitTest::order = 0;
  169. // init_priority objects are initialized from the lowest to the heighest priority number
  170. // Default init_priority is always the lowest (highest priority number)
  171. PriorityInitTest g_static_init_priority_test2;
  172. PriorityInitTest g_static_init_priority_test1 __attribute__((init_priority(1000)));
  173. PriorityInitTest g_static_init_priority_test0 __attribute__((init_priority(999)));
  174. TEST_CASE("init_priority extension works", "[misc]")
  175. {
  176. TEST_ASSERT_EQUAL(0, g_static_init_priority_test0.index);
  177. TEST_ASSERT_EQUAL(1, g_static_init_priority_test1.index);
  178. TEST_ASSERT_EQUAL(2, g_static_init_priority_test2.index);
  179. }
  180. TEST_CASE("can use new and delete", "[misc]")
  181. {
  182. int* int_p = new int(10);
  183. delete int_p;
  184. int* int_array = new int[10];
  185. delete[] int_array;
  186. }
  187. class Base
  188. {
  189. public:
  190. virtual ~Base() = default;
  191. virtual void foo() = 0;
  192. };
  193. class Derived : public Base
  194. {
  195. public:
  196. virtual void foo() { }
  197. };
  198. TEST_CASE("can call virtual functions", "[misc]")
  199. {
  200. Derived d;
  201. Base& b = static_cast<Base&>(d);
  202. b.foo();
  203. }
  204. TEST_CASE("can use std::vector", "[misc]")
  205. {
  206. std::vector<int> v(10, 1);
  207. v[0] = 42;
  208. TEST_ASSERT_EQUAL(51, std::accumulate(std::begin(v), std::end(v), 0));
  209. }
  210. /* These test cases pull a lot of code from libstdc++ and are disabled for now
  211. */
  212. #if 0
  213. #include <iostream>
  214. #include <functional>
  215. TEST_CASE("can use iostreams", "[misc]")
  216. {
  217. std::cout << "hello world";
  218. }
  219. TEST_CASE("can call std::function and bind", "[misc]")
  220. {
  221. int outer = 1;
  222. std::function<int(int)> fn = [&outer](int x) -> int {
  223. return x + outer;
  224. };
  225. outer = 5;
  226. TEST_ASSERT_EQUAL(6, fn(1));
  227. auto bound = std::bind(fn, outer);
  228. outer = 10;
  229. TEST_ASSERT_EQUAL(15, bound());
  230. }
  231. #endif
  232. static void recur_and_smash_cxx()
  233. {
  234. static int cnt;
  235. volatile uint8_t buf[50];
  236. volatile int num = sizeof(buf)+10;
  237. if (cnt++ < 1) {
  238. recur_and_smash_cxx();
  239. }
  240. for (int i = 0; i < num; i++) {
  241. buf[i] = 0;
  242. }
  243. }
  244. TEST_CASE("stack smashing protection CXX", "[stack_smash]")
  245. {
  246. recur_and_smash_cxx();
  247. }
  248. extern "C" void app_main(void)
  249. {
  250. printf("CXX GENERAL TEST\n");
  251. unity_run_menu();
  252. }
  253. /* Tests below are done in the compile time, don't actually get run. */
  254. /* Check whether a enumerator flag can be used in C++ */
  255. template<typename T> __attribute__((unused)) static void test_binary_operators()
  256. {
  257. T flag1 = (T)0;
  258. T flag2 = (T)0;
  259. flag1 = ~flag1;
  260. flag1 = flag1 | flag2;
  261. flag1 = flag1 & flag2;
  262. flag1 = flag1 ^ flag2;
  263. flag1 = flag1 >> 2;
  264. flag1 = flag1 << 2;
  265. flag1 |= flag2;
  266. flag1 &= flag2;
  267. flag1 ^= flag2;
  268. flag1 >>= 2;
  269. flag1 <<= 2;
  270. }
  271. //Add more types here. If any flags cannot pass the build, use FLAG_ATTR in esp_attr.h