forked from newrelic/newrelic-php-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_segment_children.c
337 lines (268 loc) · 12.1 KB
/
test_segment_children.c
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#include "nr_axiom.h"
#include "nr_segment.h"
#include "nr_segment_children.h"
#include "tlib_main.h"
static void test_segment_children_init(void) {
nr_segment_children_t children;
nr_segment_children_init(&children);
tlib_pass_if_bool_equal(
"is_packed must be true for an empty children structure", true,
children.is_packed);
tlib_pass_if_size_t_equal(
"count must be zero for an empty children structure", 0,
children.packed.count);
}
static void test_segment_children_deinit(void) {
nr_segment_children_t children;
nr_segment_t segment;
nr_segment_children_init(&children);
nr_segment_children_add(&children, &segment);
nr_segment_children_deinit(&children);
tlib_pass_if_bool_equal("is_packed must be true after deinit occurs", true,
children.is_packed);
tlib_pass_if_size_t_equal("count must be zero after deinit occurs", 0,
children.packed.count);
nr_segment_children_init(&children);
nr_segment_children_add(&children, &segment);
nr_segment_children_migrate_to_vector(&children);
nr_segment_children_deinit(&children);
tlib_pass_if_bool_equal("is_packed must be true after deinit occurs", true,
children.is_packed);
tlib_pass_if_size_t_equal("count must be zero after deinit occurs", 0,
children.packed.count);
}
static void test_segment_children_size_invalid(void) {
tlib_pass_if_size_t_equal("NULL children have 0 size", 0,
nr_segment_children_size(NULL));
}
static void test_segment_children_size(nr_segment_children_t* children,
const size_t count) {
size_t i;
nr_segment_t* segments
= (nr_segment_t*)nr_alloca(count * sizeof(nr_segment_t));
for (i = 0; i < count; i++) {
tlib_pass_if_size_t_equal("size must be equal to the number of children", i,
nr_segment_children_size(children));
tlib_pass_if_bool_equal("adding a child should succeed", true,
nr_segment_children_add(children, &segments[i]));
}
tlib_pass_if_size_t_equal("size must be equal to the number of children",
count, nr_segment_children_size(children));
}
static void test_segment_children_size_packed(void) {
nr_segment_children_t children;
nr_segment_children_init(&children);
test_segment_children_size(&children, NR_SEGMENT_CHILDREN_PACKED_LIMIT);
tlib_pass_if_bool_equal("children structure is packed", true,
children.is_packed);
nr_segment_children_deinit(&children);
}
static void test_segment_children_size_vector(void) {
nr_segment_children_t children;
nr_segment_children_init(&children);
test_segment_children_size(&children, NR_SEGMENT_CHILDREN_PACKED_LIMIT + 1);
tlib_pass_if_bool_equal("children structure is not packed", false,
children.is_packed);
nr_segment_children_deinit(&children);
}
static void test_segment_children_get_invalid(void) {
nr_segment_children_t children;
nr_segment_t segment = {.parent = NULL};
tlib_pass_if_null("NULL children have no children",
nr_segment_children_get(NULL, 0));
nr_segment_children_init(&children);
tlib_pass_if_null("empty children have no children to get",
nr_segment_children_get(&children, 0));
tlib_pass_if_null("empty children have no children to get",
nr_segment_children_get(&children, 1));
nr_segment_children_add(&children, &segment);
tlib_pass_if_null("out of range indices will return NULL",
nr_segment_children_get(&children, 1));
nr_segment_children_deinit(&children);
}
static void test_segment_children_get(nr_segment_children_t* children,
const size_t count) {
size_t i;
nr_segment_t* segments
= (nr_segment_t*)nr_alloca(count * sizeof(nr_segment_t));
for (i = 0; i < count; i++) {
tlib_pass_if_bool_equal("adding a child should succeed", true,
nr_segment_children_add(children, &segments[i]));
}
for (i = 0; i < count; i++) {
tlib_pass_if_ptr_equal("get must return the correct element", &segments[i],
nr_segment_children_get(children, i));
}
}
static void test_segment_children_get_packed(void) {
nr_segment_children_t children;
nr_segment_children_init(&children);
test_segment_children_get(&children, NR_SEGMENT_CHILDREN_PACKED_LIMIT);
tlib_pass_if_bool_equal("children structure is packed", true,
children.is_packed);
nr_segment_children_deinit(&children);
}
static void test_segment_children_get_vector(void) {
nr_segment_children_t children;
nr_segment_children_init(&children);
test_segment_children_get(&children, NR_SEGMENT_CHILDREN_PACKED_LIMIT + 1);
tlib_pass_if_bool_equal("children structure is not packed", false,
children.is_packed);
nr_segment_children_deinit(&children);
}
static void test_segment_children_add_invalid(void) {
nr_segment_children_t children;
nr_segment_t child = {.parent = NULL};
nr_segment_children_init(&children);
tlib_pass_if_bool_equal("adding to a NULL children should fail", false,
nr_segment_children_add(NULL, &child));
tlib_pass_if_bool_equal("adding a NULL child should fail", false,
nr_segment_children_add(&children, NULL));
nr_segment_children_deinit(&children);
}
static void test_segment_children_remove_invalid(void) {
nr_segment_children_t children;
nr_segment_t child = {.parent = NULL};
nr_segment_children_init(&children);
tlib_pass_if_bool_equal("removing from NULL children should fail", false,
nr_segment_children_remove(NULL, &child));
tlib_pass_if_bool_equal("removing a NULL child should fail", false,
nr_segment_children_remove(&children, NULL));
tlib_pass_if_bool_equal("removing from empty children should fail", false,
nr_segment_children_remove(&children, &child));
nr_segment_children_deinit(&children);
}
static void test_segment_children_remove(nr_segment_children_t* children,
size_t count) {
size_t i;
nr_segment_t* segments
= (nr_segment_t*)nr_alloca((count + 1) * sizeof(nr_segment_t));
for (i = 0; i < count; i++) {
tlib_pass_if_bool_equal("adding a child should succeed", true,
nr_segment_children_add(children, &segments[i]));
}
tlib_pass_if_bool_equal(
"removing a non-existent element should fail", false,
nr_segment_children_remove(children, &segments[count]));
tlib_pass_if_bool_equal(
"removing the last element should succeed", true,
nr_segment_children_remove(children, &segments[count - 1]));
tlib_pass_if_size_t_equal("removing the last element should change the size",
count - 1, nr_segment_children_size(children));
for (i = 0; i < count - 1; i++) {
tlib_pass_if_bool_equal("removing an element should succeed", true,
nr_segment_children_remove(children, &segments[i]));
tlib_pass_if_size_t_equal("removing an element should decrement the size",
count - 2 - i,
nr_segment_children_size(children));
}
}
static void test_segment_children_remove_packed(void) {
nr_segment_children_t children;
nr_segment_children_init(&children);
test_segment_children_remove(&children, NR_SEGMENT_CHILDREN_PACKED_LIMIT);
tlib_pass_if_bool_equal("segment children is packed", true,
children.is_packed);
nr_segment_children_deinit(&children);
}
static void test_segment_children_remove_vector(void) {
nr_segment_children_t children;
nr_segment_children_init(&children);
test_segment_children_remove(&children, NR_SEGMENT_CHILDREN_PACKED_LIMIT * 2);
tlib_pass_if_bool_equal("segment children is not packed", false,
children.is_packed);
nr_segment_children_deinit(&children);
}
static void test_segment_children_reparent_invalid(void) {
nr_segment_children_t children;
nr_segment_t segment = {.parent = NULL};
tlib_pass_if_bool_equal("NULL children cannot be reparented", false,
nr_segment_children_reparent(NULL, &segment));
tlib_pass_if_bool_equal("children cannot be reparented onto a NULL segment",
false, nr_segment_children_reparent(&children, NULL));
}
static void test_segment_children_reparent(nr_segment_children_t* children,
size_t count) {
size_t i;
nr_segment_t parent;
nr_segment_t* segments
= (nr_segment_t*)nr_alloca(count * sizeof(nr_segment_t));
nr_segment_children_init(&parent.children);
for (i = 0; i < count; i++) {
tlib_pass_if_bool_equal("adding a child should succeed", true,
nr_segment_children_add(children, &segments[i]));
}
tlib_pass_if_bool_equal("reparenting children should succeed", true,
nr_segment_children_reparent(children, &parent));
tlib_pass_if_size_t_equal(
"the original children struct should have no children left in it", 0,
nr_segment_children_size(children));
tlib_pass_if_size_t_equal("the new parent should have all the children",
count, nr_segment_children_size(&parent.children));
for (i = 0; i < count; i++) {
tlib_pass_if_ptr_equal("the child should have the new ", &parent,
segments[i].parent);
}
nr_segment_children_deinit(&parent.children);
}
static void test_segment_children_reparent_packed(void) {
nr_segment_children_t children;
nr_segment_children_init(&children);
test_segment_children_reparent(&children, NR_SEGMENT_CHILDREN_PACKED_LIMIT);
nr_segment_children_deinit(&children);
}
static void test_segment_children_reparent_vector(void) {
nr_segment_children_t children;
nr_segment_children_init(&children);
test_segment_children_reparent(&children,
NR_SEGMENT_CHILDREN_PACKED_LIMIT * 2);
nr_segment_children_deinit(&children);
}
static void test_segment_children_vector_shrink(void) {
nr_segment_children_t children;
nr_segment_t parent = {0};
nr_segment_t segments[9];
size_t i;
nr_segment_children_init(&children);
nr_segment_children_init(&parent.children);
nr_segment_children_add(&parent.children, &segments[0]);
nr_segment_children_add(&parent.children, &segments[1]);
for (i = 2; i < sizeof(segments) / sizeof(segments[0]); i++) {
nr_segment_children_add(&children, &segments[i]);
}
nr_segment_children_reparent(&children, &parent);
tlib_pass_if_bool_equal("parent children not packed", false,
parent.children.is_packed);
for (i = 0; i < sizeof(segments) / sizeof(segments[0]); i++) {
nr_segment_children_remove(&parent.children, &segments[i]);
tlib_pass_if_bool_equal("is_packed must stay false after once set to false",
false, parent.children.is_packed);
}
nr_segment_children_deinit(&parent.children);
nr_segment_children_deinit(&children);
}
tlib_parallel_info_t parallel_info = {.suggested_nthreads = 2, .state_size = 0};
void test_main(void* p NRUNUSED) {
test_segment_children_init();
test_segment_children_deinit();
test_segment_children_size_invalid();
test_segment_children_size_packed();
test_segment_children_size_vector();
test_segment_children_get_invalid();
test_segment_children_get_packed();
test_segment_children_get_vector();
// This is the only add test because the size and get tests very thoroughly
// exercise the normal operation of nr_segment_children_add() already.
test_segment_children_add_invalid();
test_segment_children_remove_invalid();
test_segment_children_remove_packed();
test_segment_children_remove_vector();
test_segment_children_reparent_invalid();
test_segment_children_reparent_packed();
test_segment_children_reparent_vector();
test_segment_children_vector_shrink();
}