]> granicus.if.org Git - esp-idf/blob - components/cxx/test/test_cxx.cpp
743a6869ded0076305b34723b35e31d3aeab58b2
[esp-idf] / components / cxx / test / test_cxx.cpp
1 #include <vector>
2 #include <algorithm>
3 #include "unity.h"
4 #include "esp_log.h"
5 #include "freertos/FreeRTOS.h"
6 #include "freertos/task.h"
7 #include "freertos/semphr.h"
8
9 static const char* TAG = "cxx";
10
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
19 class Base
20 {
21 public:
22     virtual ~Base() {}
23     virtual void foo() = 0; 
24 };
25
26 class Derived : public Base
27 {
28 public:
29     virtual void foo() { }
30 };
31
32 TEST_CASE("can call virtual functions", "[cxx]")
33 {
34     Derived d;
35     Base& b = static_cast<Base&>(d);
36     b.foo();
37 }
38
39 class NonPOD
40 {
41 public:
42     NonPOD(int a_) : a(a_) { }
43     int a;
44 };
45
46 static int non_pod_test_helper(int new_val)
47 {
48     static NonPOD non_pod(42);
49     int ret = non_pod.a;
50     non_pod.a = new_val;
51     return ret;
52 }
53
54 TEST_CASE("can use static initializers for non-POD types", "[cxx]")
55 {
56     TEST_ASSERT_EQUAL(42, non_pod_test_helper(1));
57     TEST_ASSERT_EQUAL(1, non_pod_test_helper(0));
58 }
59
60 TEST_CASE("can use std::vector", "[cxx]")
61 {
62     std::vector<int> v(10, 1);
63     v[0] = 42;
64     TEST_ASSERT_EQUAL(51, std::accumulate(std::begin(v), std::end(v), 0));
65 }
66
67 /*
68  * This test exercises static initialization guards for two objects.
69  * For each object, 4 tasks are created which attempt to perform static initialization.
70  * We check that constructor runs only once for each object.
71  */
72
73 static SemaphoreHandle_t s_slow_init_sem = NULL;
74
75 template<int obj>
76 class SlowInit
77 {
78 public:
79     SlowInit(int arg) {
80         ESP_LOGD(TAG, "init obj=%d start, arg=%d\n", obj, arg);
81         vTaskDelay(300/portTICK_PERIOD_MS);
82         TEST_ASSERT_EQUAL(-1, mInitBy);
83         TEST_ASSERT_EQUAL(0, mInitCount);
84         mInitBy = arg;
85         ++mInitCount;
86         ESP_LOGD(TAG, "init obj=%d done\n", obj);
87     }
88
89     static void task(void* arg) {
90         int taskId = reinterpret_cast<int>(arg);
91         ESP_LOGD(TAG, "obj=%d before static init, task=%d\n", obj, taskId);
92         static SlowInit slowinit(taskId);
93         ESP_LOGD(TAG, "obj=%d after static init, task=%d\n", obj, taskId);
94         xSemaphoreGive(s_slow_init_sem);
95         vTaskDelete(NULL);
96     }
97 private:
98     static int mInitBy;
99     static int mInitCount;
100 };
101
102 template<> int SlowInit<1>::mInitBy = -1;
103 template<> int SlowInit<1>::mInitCount = 0;
104 template<> int SlowInit<2>::mInitBy = -1;
105 template<> int SlowInit<2>::mInitCount = 0;
106
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
114 TEST_CASE("static initialization guards work as expected", "[cxx]")
115 {
116     s_slow_init_sem = xSemaphoreCreateCounting(10, 0);
117     TEST_ASSERT_NOT_NULL(s_slow_init_sem);
118     // four tasks competing for static initialization of one object
119     start_slow_init_task<1>(0, PRO_CPU_NUM);
120     start_slow_init_task<1>(1, APP_CPU_NUM);
121     start_slow_init_task<1>(2, PRO_CPU_NUM);
122     start_slow_init_task<1>(3, tskNO_AFFINITY);
123
124     // four tasks competing for static initialization of another object
125     start_slow_init_task<2>(0, PRO_CPU_NUM);
126     start_slow_init_task<2>(1, APP_CPU_NUM);
127     start_slow_init_task<2>(2, PRO_CPU_NUM);
128     start_slow_init_task<2>(3, tskNO_AFFINITY);
129
130     // All tasks should
131     for (int i = 0; i < 8; ++i) {
132         TEST_ASSERT_TRUE(xSemaphoreTake(s_slow_init_sem, 500/portTICK_PERIOD_MS));
133     }
134     vSemaphoreDelete(s_slow_init_sem);
135
136     vTaskDelay(10); // Allow tasks to clean up, avoids race with leak detector
137 }
138
139 struct GlobalInitTest
140 {
141     GlobalInitTest() : index(order++) {
142     }
143     int index;
144     static int order;
145 };
146
147 int GlobalInitTest::order = 0;
148
149 GlobalInitTest g_init_test1;
150 GlobalInitTest g_init_test2;
151 GlobalInitTest g_init_test3;
152
153 TEST_CASE("global initializers run in the correct order", "[cxx]")
154 {
155     TEST_ASSERT_EQUAL(0, g_init_test1.index);
156     TEST_ASSERT_EQUAL(1, g_init_test2.index);
157     TEST_ASSERT_EQUAL(2, g_init_test3.index);
158 }
159
160 struct StaticInitTestBeforeScheduler
161 {
162     StaticInitTestBeforeScheduler()
163     {
164         static int first_init_order = getOrder();
165         index = first_init_order;
166     }
167
168     int getOrder()
169     {
170         return order++;
171     }
172
173     int index;
174     static int order;
175 };
176
177 int StaticInitTestBeforeScheduler::order = 1;
178
179 StaticInitTestBeforeScheduler g_static_init_test1;
180 StaticInitTestBeforeScheduler g_static_init_test2;
181 StaticInitTestBeforeScheduler g_static_init_test3;
182
183 TEST_CASE("before scheduler has started, static initializers work correctly", "[cxx]")
184 {
185     TEST_ASSERT_EQUAL(1, g_static_init_test1.index);
186     TEST_ASSERT_EQUAL(1, g_static_init_test2.index);
187     TEST_ASSERT_EQUAL(1, g_static_init_test3.index);
188     TEST_ASSERT_EQUAL(2, StaticInitTestBeforeScheduler::order);
189 }
190
191 #ifdef CONFIG_CXX_EXCEPTIONS
192
193 TEST_CASE("c++ exceptions work", "[cxx]")
194 {
195     /* Note: This test currently trips the memory leak threshold
196        as libunwind allocates ~4KB of data on first exception. */
197     int thrown_value;
198     try
199     {
200         throw 20;
201     }
202     catch (int e)
203     {
204         thrown_value = e;
205     }
206     TEST_ASSERT_EQUAL(20, thrown_value);
207     printf("OK?\n");
208 }
209
210 #endif
211
212 /* These test cases pull a lot of code from libstdc++ and are disabled for now
213  */
214 #if 0
215 #include <iostream>
216 #include <functional>
217
218 TEST_CASE("can use iostreams", "[cxx]")
219 {
220     std::cout << "hello world";
221 }
222
223 TEST_CASE("can call std::function and bind", "[cxx]")
224 {
225     int outer = 1;
226     std::function<int(int)> fn = [&outer](int x) -> int {
227         return x + outer;
228     };
229     outer = 5;
230     TEST_ASSERT_EQUAL(6, fn(1));
231
232     auto bound = std::bind(fn, outer);
233     outer = 10;
234     TEST_ASSERT_EQUAL(15, bound());
235 }
236
237 #endif
238