/
test_cxx.cpp
210 lines (178 loc) · 5.03 KB
/
test_cxx.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <functional>
#include <vector>
#include <algorithm>
#include <iostream>
#include "unity.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
static const char* TAG = "cxx";
TEST_CASE("can use new and delete", "[cxx]")
{
int* int_p = new int(10);
delete int_p;
int* int_array = new int[10];
delete[] int_array;
}
class Base
{
public:
virtual ~Base() {}
virtual void foo() = 0;
};
class Derived : public Base
{
public:
virtual void foo() { }
};
TEST_CASE("can call virtual functions", "[cxx]")
{
Derived d;
Base& b = static_cast<Base&>(d);
b.foo();
}
class NonPOD
{
public:
NonPOD(int a_) : a(a_) { }
int a;
};
static int non_pod_test_helper(int new_val)
{
static NonPOD non_pod(42);
int ret = non_pod.a;
non_pod.a = new_val;
return ret;
}
TEST_CASE("can use static initializers for non-POD types", "[cxx]")
{
TEST_ASSERT_EQUAL(42, non_pod_test_helper(1));
TEST_ASSERT_EQUAL(1, non_pod_test_helper(0));
}
TEST_CASE("can call std::function and bind", "[cxx]")
{
int outer = 1;
std::function<int(int)> fn = [&outer](int x) -> int {
return x + outer;
};
outer = 5;
TEST_ASSERT_EQUAL(6, fn(1));
auto bound = std::bind(fn, outer);
outer = 10;
TEST_ASSERT_EQUAL(15, bound());
}
TEST_CASE("can use std::vector", "[cxx]")
{
std::vector<int> v(10, 1);
v[0] = 42;
TEST_ASSERT_EQUAL(51, std::accumulate(std::begin(v), std::end(v), 0));
}
/*
* This test exercises static initialization guards for two objects.
* For each object, 4 tasks are created which attempt to perform static initialization.
* We check that constructor runs only once for each object.
*/
static SemaphoreHandle_t s_slow_init_sem = NULL;
template<int obj>
class SlowInit
{
public:
SlowInit(int arg) {
ESP_LOGD(TAG, "init obj=%d start, arg=%d\n", obj, arg);
vTaskDelay(300/portTICK_PERIOD_MS);
TEST_ASSERT_EQUAL(-1, mInitBy);
TEST_ASSERT_EQUAL(0, mInitCount);
mInitBy = arg;
++mInitCount;
ESP_LOGD(TAG, "init obj=%d done\n", obj);
}
static void task(void* arg) {
int taskId = reinterpret_cast<int>(arg);
ESP_LOGD(TAG, "obj=%d before static init, task=%d\n", obj, taskId);
static SlowInit slowinit(taskId);
ESP_LOGD(TAG, "obj=%d after static init, task=%d\n", obj, taskId);
xSemaphoreGive(s_slow_init_sem);
vTaskDelay(10);
vTaskDelete(NULL);
}
private:
static int mInitBy;
static int mInitCount;
};
template<> int SlowInit<1>::mInitBy = -1;
template<> int SlowInit<1>::mInitCount = 0;
template<> int SlowInit<2>::mInitBy = -1;
template<> int SlowInit<2>::mInitCount = 0;
template<int obj>
static void start_slow_init_task(int id, int affinity)
{
xTaskCreatePinnedToCore(&SlowInit<obj>::task, "slow_init", 2048,
reinterpret_cast<void*>(id), 3, NULL, affinity);
}
TEST_CASE("static initialization guards work as expected", "[cxx]")
{
s_slow_init_sem = xSemaphoreCreateCounting(10, 0);
TEST_ASSERT_NOT_NULL(s_slow_init_sem);
// four tasks competing for static initialization of one object
start_slow_init_task<1>(0, PRO_CPU_NUM);
start_slow_init_task<1>(1, APP_CPU_NUM);
start_slow_init_task<1>(2, PRO_CPU_NUM);
start_slow_init_task<1>(3, tskNO_AFFINITY);
// four tasks competing for static initialization of another object
start_slow_init_task<2>(0, PRO_CPU_NUM);
start_slow_init_task<2>(1, APP_CPU_NUM);
start_slow_init_task<2>(2, PRO_CPU_NUM);
start_slow_init_task<2>(3, tskNO_AFFINITY);
// All tasks should
for (int i = 0; i < 8; ++i) {
TEST_ASSERT_TRUE(xSemaphoreTake(s_slow_init_sem, 500/portTICK_PERIOD_MS));
}
vSemaphoreDelete(s_slow_init_sem);
}
struct GlobalInitTest
{
GlobalInitTest() : index(order++) {
}
int index;
static int order;
};
int GlobalInitTest::order = 0;
GlobalInitTest g_init_test1;
GlobalInitTest g_init_test2;
GlobalInitTest g_init_test3;
TEST_CASE("global initializers run in the correct order", "[cxx]")
{
TEST_ASSERT_EQUAL(0, g_init_test1.index);
TEST_ASSERT_EQUAL(1, g_init_test2.index);
TEST_ASSERT_EQUAL(2, g_init_test3.index);
}
struct StaticInitTestBeforeScheduler
{
StaticInitTestBeforeScheduler()
{
static int first_init_order = getOrder();
index = first_init_order;
}
int getOrder()
{
return order++;
}
int index;
static int order;
};
int StaticInitTestBeforeScheduler::order = 1;
StaticInitTestBeforeScheduler g_static_init_test1;
StaticInitTestBeforeScheduler g_static_init_test2;
StaticInitTestBeforeScheduler g_static_init_test3;
TEST_CASE("before scheduler has started, static initializers work correctly", "[cxx]")
{
TEST_ASSERT_EQUAL(1, g_static_init_test1.index);
TEST_ASSERT_EQUAL(1, g_static_init_test2.index);
TEST_ASSERT_EQUAL(1, g_static_init_test3.index);
TEST_ASSERT_EQUAL(2, StaticInitTestBeforeScheduler::order);
}
TEST_CASE("can use iostreams", "[cxx]")
{
std::cout << "hello world";
}