test_cxx.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include <functional>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <iostream>
  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 call std::function and bind", "[cxx]")
  54. {
  55. int outer = 1;
  56. std::function<int(int)> fn = [&outer](int x) -> int {
  57. return x + outer;
  58. };
  59. outer = 5;
  60. TEST_ASSERT_EQUAL(6, fn(1));
  61. auto bound = std::bind(fn, outer);
  62. outer = 10;
  63. TEST_ASSERT_EQUAL(15, bound());
  64. }
  65. TEST_CASE("can use std::vector", "[cxx]")
  66. {
  67. std::vector<int> v(10, 1);
  68. v[0] = 42;
  69. TEST_ASSERT_EQUAL(51, std::accumulate(std::begin(v), std::end(v), 0));
  70. }
  71. /*
  72. * This test exercises static initialization guards for two objects.
  73. * For each object, 4 tasks are created which attempt to perform static initialization.
  74. * We check that constructor runs only once for each object.
  75. */
  76. static SemaphoreHandle_t s_slow_init_sem = NULL;
  77. template<int obj>
  78. class SlowInit
  79. {
  80. public:
  81. SlowInit(int arg) {
  82. ESP_LOGD(TAG, "init obj=%d start, arg=%d\n", obj, arg);
  83. vTaskDelay(300/portTICK_PERIOD_MS);
  84. TEST_ASSERT_EQUAL(-1, mInitBy);
  85. TEST_ASSERT_EQUAL(0, mInitCount);
  86. mInitBy = arg;
  87. ++mInitCount;
  88. ESP_LOGD(TAG, "init obj=%d done\n", obj);
  89. }
  90. static void task(void* arg) {
  91. int taskId = reinterpret_cast<int>(arg);
  92. ESP_LOGD(TAG, "obj=%d before static init, task=%d\n", obj, taskId);
  93. static SlowInit slowinit(taskId);
  94. ESP_LOGD(TAG, "obj=%d after static init, task=%d\n", obj, taskId);
  95. xSemaphoreGive(s_slow_init_sem);
  96. vTaskDelay(10);
  97. vTaskDelete(NULL);
  98. }
  99. private:
  100. static int mInitBy;
  101. static int mInitCount;
  102. };
  103. template<> int SlowInit<1>::mInitBy = -1;
  104. template<> int SlowInit<1>::mInitCount = 0;
  105. template<> int SlowInit<2>::mInitBy = -1;
  106. template<> int SlowInit<2>::mInitCount = 0;
  107. template<int obj>
  108. static void start_slow_init_task(int id, int affinity)
  109. {
  110. xTaskCreatePinnedToCore(&SlowInit<obj>::task, "slow_init", 2048,
  111. reinterpret_cast<void*>(id), 3, NULL, affinity);
  112. }
  113. TEST_CASE("static initialization guards work as expected", "[cxx]")
  114. {
  115. s_slow_init_sem = xSemaphoreCreateCounting(10, 0);
  116. TEST_ASSERT_NOT_NULL(s_slow_init_sem);
  117. // four tasks competing for static initialization of one object
  118. start_slow_init_task<1>(0, PRO_CPU_NUM);
  119. start_slow_init_task<1>(1, APP_CPU_NUM);
  120. start_slow_init_task<1>(2, PRO_CPU_NUM);
  121. start_slow_init_task<1>(3, tskNO_AFFINITY);
  122. // four tasks competing for static initialization of another object
  123. start_slow_init_task<2>(0, PRO_CPU_NUM);
  124. start_slow_init_task<2>(1, APP_CPU_NUM);
  125. start_slow_init_task<2>(2, PRO_CPU_NUM);
  126. start_slow_init_task<2>(3, tskNO_AFFINITY);
  127. // All tasks should
  128. for (int i = 0; i < 8; ++i) {
  129. TEST_ASSERT_TRUE(xSemaphoreTake(s_slow_init_sem, 500/portTICK_PERIOD_MS));
  130. }
  131. vSemaphoreDelete(s_slow_init_sem);
  132. }
  133. struct GlobalInitTest
  134. {
  135. GlobalInitTest() : index(order++) {
  136. }
  137. int index;
  138. static int order;
  139. };
  140. int GlobalInitTest::order = 0;
  141. GlobalInitTest g_init_test1;
  142. GlobalInitTest g_init_test2;
  143. GlobalInitTest g_init_test3;
  144. TEST_CASE("global initializers run in the correct order", "[cxx]")
  145. {
  146. TEST_ASSERT_EQUAL(0, g_init_test1.index);
  147. TEST_ASSERT_EQUAL(1, g_init_test2.index);
  148. TEST_ASSERT_EQUAL(2, g_init_test3.index);
  149. }
  150. struct StaticInitTestBeforeScheduler
  151. {
  152. StaticInitTestBeforeScheduler()
  153. {
  154. static int first_init_order = getOrder();
  155. index = first_init_order;
  156. }
  157. int getOrder()
  158. {
  159. return order++;
  160. }
  161. int index;
  162. static int order;
  163. };
  164. int StaticInitTestBeforeScheduler::order = 1;
  165. StaticInitTestBeforeScheduler g_static_init_test1;
  166. StaticInitTestBeforeScheduler g_static_init_test2;
  167. StaticInitTestBeforeScheduler g_static_init_test3;
  168. TEST_CASE("before scheduler has started, static initializers work correctly", "[cxx]")
  169. {
  170. TEST_ASSERT_EQUAL(1, g_static_init_test1.index);
  171. TEST_ASSERT_EQUAL(1, g_static_init_test2.index);
  172. TEST_ASSERT_EQUAL(1, g_static_init_test3.index);
  173. TEST_ASSERT_EQUAL(2, StaticInitTestBeforeScheduler::order);
  174. }
  175. TEST_CASE("can use iostreams", "[cxx]")
  176. {
  177. std::cout << "hello world";
  178. }