summaryrefslogtreecommitdiffstats
path: root/pceplib/test/pcep_utils_counters_test.c
blob: 2cad9a64438f21a905e32a10343f2ada7f6fcc31 (plain)
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
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
 * This file is part of the PCEPlib, a PCEP protocol library.
 *
 * Copyright (C) 2020 Volta Networks https://voltanet.io/
 *
 * Author : Brady Johnson <brady@voltanet.io>
 *
 */


#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <assert.h>
#include <stdlib.h>

#include <CUnit/CUnit.h>

#include "pcep_utils_counters.h"
#include "pcep_utils_counters_test.h"


void test_create_counters_group()
{
	const char group_name[] = "group";
	uint16_t num_subgroups = 10;

	struct counters_group *group =
		create_counters_group(NULL, num_subgroups);
	CU_ASSERT_PTR_NULL(group);

	group = create_counters_group(group_name, MAX_COUNTER_GROUPS + 1);
	CU_ASSERT_PTR_NULL(group);

	group = create_counters_group(group_name, num_subgroups);
	CU_ASSERT_PTR_NOT_NULL(group);
	assert(group != NULL);

	CU_ASSERT_EQUAL(group->num_subgroups, 0);
	CU_ASSERT_EQUAL(group->max_subgroups, num_subgroups);
	CU_ASSERT_EQUAL(strcmp(group->counters_group_name, group_name), 0);

	delete_counters_group(group);
}

void test_create_counters_subgroup()
{
	const char subgroup_name[] = "subgroup";
	uint16_t subgroup_id = 10;
	uint16_t num_counters = 20;

	struct counters_subgroup *subgroup =
		create_counters_subgroup(NULL, subgroup_id, num_counters);
	CU_ASSERT_PTR_NULL(subgroup);

	subgroup = create_counters_subgroup(
		subgroup_name, MAX_COUNTER_GROUPS + 1, num_counters);
	CU_ASSERT_PTR_NULL(subgroup);

	subgroup = create_counters_subgroup(subgroup_name, subgroup_id,
					    MAX_COUNTERS + 1);
	CU_ASSERT_PTR_NULL(subgroup);

	subgroup = create_counters_subgroup(subgroup_name, subgroup_id,
					    num_counters);
	CU_ASSERT_PTR_NOT_NULL(subgroup);
	assert(subgroup != NULL);

	CU_ASSERT_EQUAL(subgroup->subgroup_id, subgroup_id);
	CU_ASSERT_EQUAL(subgroup->num_counters, 0);
	CU_ASSERT_EQUAL(subgroup->max_counters, num_counters);
	CU_ASSERT_EQUAL(strcmp(subgroup->counters_subgroup_name, subgroup_name),
			0);

	delete_counters_subgroup(subgroup);
}

void test_add_counters_subgroup()
{
	struct counters_group *group = create_counters_group("group", 1);
	struct counters_subgroup *subgroup1 =
		create_counters_subgroup("subgroup", 0, 5);
	struct counters_subgroup *subgroup2 =
		create_counters_subgroup("subgroup", 1, 5);

	CU_ASSERT_FALSE(add_counters_subgroup(NULL, NULL));
	CU_ASSERT_FALSE(add_counters_subgroup(NULL, subgroup1));
	CU_ASSERT_FALSE(add_counters_subgroup(group, NULL));

	CU_ASSERT_EQUAL(group->num_subgroups, 0);
	CU_ASSERT_TRUE(add_counters_subgroup(group, subgroup1));
	CU_ASSERT_EQUAL(group->num_subgroups, 1);
	/* Cant add more than num_subgroups to the group */
	CU_ASSERT_FALSE(add_counters_subgroup(group, subgroup2));

	CU_ASSERT_PTR_NOT_NULL(find_subgroup(group, 0));
	CU_ASSERT_PTR_NULL(find_subgroup(group, 1));

	delete_counters_group(group);
	delete_counters_subgroup(subgroup2);
}

void test_create_subgroup_counter()
{
	uint16_t counter_id = 1;
	char counter_name[] = "my counter";
	struct counters_subgroup *subgroup =
		create_counters_subgroup("subgroup", 1, 2);

	CU_ASSERT_FALSE(
		create_subgroup_counter(NULL, counter_id, counter_name));
	CU_ASSERT_FALSE(create_subgroup_counter(subgroup, counter_id + 1,
						counter_name));
	CU_ASSERT_FALSE(create_subgroup_counter(subgroup, counter_id, NULL));
	CU_ASSERT_EQUAL(subgroup->num_counters, 0);
	CU_ASSERT_TRUE(
		create_subgroup_counter(subgroup, counter_id, counter_name));
	CU_ASSERT_EQUAL(subgroup->num_counters, 1);

	delete_counters_subgroup(subgroup);
}

void test_delete_counters_group()
{
	struct counters_group *group = create_counters_group("group", 1);

	CU_ASSERT_FALSE(delete_counters_group(NULL));
	CU_ASSERT_TRUE(delete_counters_group(group));
}

void test_delete_counters_subgroup()
{
	struct counters_subgroup *subgroup =
		create_counters_subgroup("subgroup", 1, 1);

	CU_ASSERT_FALSE(delete_counters_subgroup(NULL));
	CU_ASSERT_TRUE(delete_counters_subgroup(subgroup));
}

void test_reset_group_counters()
{
	uint16_t subgroup_id = 1;
	uint16_t counter_id = 1;
	struct counters_group *group = create_counters_group("group", 10);
	struct counters_subgroup *subgroup =
		create_counters_subgroup("subgroup", subgroup_id, 10);
	create_subgroup_counter(subgroup, counter_id, "counter");
	add_counters_subgroup(group, subgroup);

	struct counter *counter = subgroup->counters[counter_id];
	counter->counter_value = 100;

	CU_ASSERT_FALSE(reset_group_counters(NULL));
	CU_ASSERT_TRUE(reset_group_counters(group));
	CU_ASSERT_EQUAL(counter->counter_value, 0);

	delete_counters_group(group);
}

void test_reset_subgroup_counters()
{
	uint16_t counter_id = 1;
	struct counters_subgroup *subgroup =
		create_counters_subgroup("subgroup", 1, 10);
	create_subgroup_counter(subgroup, counter_id, "counter");

	struct counter *counter = subgroup->counters[counter_id];
	counter->counter_value = 100;

	CU_ASSERT_FALSE(reset_subgroup_counters(NULL));
	CU_ASSERT_TRUE(reset_subgroup_counters(subgroup));
	CU_ASSERT_EQUAL(counter->counter_value, 0);

	delete_counters_subgroup(subgroup);
}

void test_increment_counter()
{
	uint16_t subgroup_id = 1;
	uint16_t counter_id = 1;
	struct counters_group *group = create_counters_group("group", 10);
	struct counters_subgroup *subgroup =
		create_counters_subgroup("subgroup", subgroup_id, 10);
	create_subgroup_counter(subgroup, counter_id, "counter");
	add_counters_subgroup(group, subgroup);

	struct counter *counter = subgroup->counters[counter_id];
	counter->counter_value = 100;

	CU_ASSERT_FALSE(increment_counter(NULL, subgroup_id, counter_id));
	CU_ASSERT_FALSE(increment_counter(group, 100, counter_id));
	CU_ASSERT_FALSE(increment_counter(group, subgroup_id, 123));
	CU_ASSERT_TRUE(increment_counter(group, subgroup_id, counter_id));
	CU_ASSERT_EQUAL(counter->counter_value, 101);
	CU_ASSERT_EQUAL(subgroup_counters_total(subgroup), 101);

	delete_counters_group(group);
}

void test_increment_subgroup_counter()
{
	int counter_id = 1;
	uint32_t counter_value = 100;
	struct counters_subgroup *subgroup =
		create_counters_subgroup("subgroup", 1, 10);
	create_subgroup_counter(subgroup, counter_id, "counter");

	struct counter *counter = subgroup->counters[counter_id];
	counter->counter_value = counter_value;

	CU_ASSERT_FALSE(increment_subgroup_counter(NULL, counter_id));
	CU_ASSERT_FALSE(increment_subgroup_counter(subgroup, counter_id + 1));
	CU_ASSERT_TRUE(increment_subgroup_counter(subgroup, counter_id));
	CU_ASSERT_EQUAL(counter->counter_value, counter_value + 1);

	delete_counters_subgroup(subgroup);
}

void test_dump_counters_group_to_log()
{
	uint16_t subgroup_id = 1;
	uint16_t counter_id = 1;
	struct counters_group *group = create_counters_group("group", 10);
	struct counters_subgroup *subgroup =
		create_counters_subgroup("subgroup", subgroup_id, 10);
	create_subgroup_counter(subgroup, counter_id, "counter");
	add_counters_subgroup(group, subgroup);

	CU_ASSERT_FALSE(dump_counters_group_to_log(NULL));
	CU_ASSERT_TRUE(dump_counters_group_to_log(group));

	delete_counters_group(group);
}

void test_dump_counters_subgroup_to_log()
{
	uint16_t subgroup_id = 1;
	uint16_t counter_id = 1;
	struct counters_subgroup *subgroup =
		create_counters_subgroup("subgroup", subgroup_id, 10);
	create_subgroup_counter(subgroup, counter_id, "counter");

	CU_ASSERT_FALSE(dump_counters_subgroup_to_log(NULL));
	CU_ASSERT_TRUE(dump_counters_subgroup_to_log(subgroup));

	delete_counters_subgroup(subgroup);
}