aboutsummaryrefslogtreecommitdiffstats
path: root/tests/stats/stats_test.c
blob: f8c7dc002e06540a942e8c26035c5682f9c2f866 (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
/* tests for statistics */
/*
 * (C) 2015 Sysmocom s.m.f.c. GmbH
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include <osmocom/core/logging.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/stat_item.h>

#include <stdio.h>

static void stat_test(void)
{
	enum test_items {
		TEST_A_ITEM,
		TEST_B_ITEM,
	};

	static const struct osmo_stat_item_desc item_description[] = {
		{ "item.a", "The A value", "ma", 4, -1 },
		{ "item.b", "The B value", "kb", 7, -1 },
	};

	static const struct osmo_stat_item_group_desc statg_desc = {
		.group_name_prefix = "test.one",
		.group_description = "Test number 1",
		.num_items = ARRAY_SIZE(item_description),
		.item_desc = item_description,
	};

	struct osmo_stat_item_group *statg =
		osmo_stat_item_group_alloc(NULL, &statg_desc, 0);

	struct osmo_stat_item_group *sgrp2;
	const struct osmo_stat_item *sitem1, *sitem2;
	int rc;
	int32_t value;
	int32_t rd_a = 0;
	int32_t rd_b = 0;
	int i;

	OSMO_ASSERT(statg != NULL);

	sgrp2 = osmo_stat_item_get_group_by_name_idx("test.one", 0);
	OSMO_ASSERT(sgrp2 == statg);

	sgrp2 = osmo_stat_item_get_group_by_name_idx("test.one", 1);
	OSMO_ASSERT(sgrp2 == NULL);

	sgrp2 = osmo_stat_item_get_group_by_name_idx("test.two", 0);
	OSMO_ASSERT(sgrp2 == NULL);

	sitem1 = osmo_stat_item_get_by_name(statg, "item.c");
	OSMO_ASSERT(sitem1 == NULL);

	sitem1 = osmo_stat_item_get_by_name(statg, "item.a");
	OSMO_ASSERT(sitem1 != NULL);
	OSMO_ASSERT(sitem1 == statg->items[TEST_A_ITEM]);

	sitem2 = osmo_stat_item_get_by_name(statg, "item.b");
	OSMO_ASSERT(sitem2 != NULL);
	OSMO_ASSERT(sitem2 != sitem1);
	OSMO_ASSERT(sitem2 == statg->items[TEST_B_ITEM]);

	value = osmo_stat_item_get_last(statg->items[TEST_A_ITEM]);
	OSMO_ASSERT(value == -1);

	rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
	OSMO_ASSERT(rc == 0);

	osmo_stat_item_set(statg->items[TEST_A_ITEM], 1);

	value = osmo_stat_item_get_last(statg->items[TEST_A_ITEM]);
	OSMO_ASSERT(value == 1);

	rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
	OSMO_ASSERT(rc > 0);
	OSMO_ASSERT(value == 1);

	rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
	OSMO_ASSERT(rc == 0);

	for (i = 2; i <= 32; i++) {
		osmo_stat_item_set(statg->items[TEST_A_ITEM], i);
		osmo_stat_item_set(statg->items[TEST_B_ITEM], 1000 + i);

		rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
		OSMO_ASSERT(rc > 0);
		OSMO_ASSERT(value == i);

		rc = osmo_stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value);
		OSMO_ASSERT(rc > 0);
		OSMO_ASSERT(value == 1000 + i);
	}

	/* Keep 2 in FIFO */
	osmo_stat_item_set(statg->items[TEST_A_ITEM], 33);
	osmo_stat_item_set(statg->items[TEST_B_ITEM], 1000 + 33);

	for (i = 34; i <= 64; i++) {
		osmo_stat_item_set(statg->items[TEST_A_ITEM], i);
		osmo_stat_item_set(statg->items[TEST_B_ITEM], 1000 + i);

		rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
		OSMO_ASSERT(rc > 0);
		OSMO_ASSERT(value == i-1);

		rc = osmo_stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value);
		OSMO_ASSERT(rc > 0);
		OSMO_ASSERT(value == 1000 + i-1);
	}

	rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
	OSMO_ASSERT(rc > 0);
	OSMO_ASSERT(value == 64);

	rc = osmo_stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value);
	OSMO_ASSERT(rc > 0);
	OSMO_ASSERT(value == 1000 + 64);

	/* Overrun FIFOs */
	for (i = 65; i <= 96; i++) {
		osmo_stat_item_set(statg->items[TEST_A_ITEM], i);
		osmo_stat_item_set(statg->items[TEST_B_ITEM], 1000 + i);
	}

	rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
	OSMO_ASSERT(rc > 0);
	OSMO_ASSERT(value == 93);

	for (i = 94; i <= 96; i++) {
		rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
		OSMO_ASSERT(rc > 0);
		OSMO_ASSERT(value == i);
	}

	rc = osmo_stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value);
	OSMO_ASSERT(rc > 0);
	OSMO_ASSERT(value == 1000 + 90);

	for (i = 91; i <= 96; i++) {
		rc = osmo_stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value);
		OSMO_ASSERT(rc > 0);
		OSMO_ASSERT(value == 1000 + i);
	}

	/* Test Discard (single item) */
	osmo_stat_item_set(statg->items[TEST_A_ITEM], 97);
	rc = osmo_stat_item_discard(statg->items[TEST_A_ITEM], &rd_a);
	OSMO_ASSERT(rc > 0);

	rc = osmo_stat_item_discard(statg->items[TEST_A_ITEM], &rd_a);
	OSMO_ASSERT(rc == 0);

	rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
	OSMO_ASSERT(rc == 0);

	osmo_stat_item_set(statg->items[TEST_A_ITEM], 98);
	rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
	OSMO_ASSERT(rc > 0);
	OSMO_ASSERT(value == 98);

	rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
	OSMO_ASSERT(rc == 0);

	/* Test Discard (all items) */
	osmo_stat_item_set(statg->items[TEST_A_ITEM], 99);
	osmo_stat_item_set(statg->items[TEST_A_ITEM], 100);
	osmo_stat_item_set(statg->items[TEST_A_ITEM], 101);
	osmo_stat_item_set(statg->items[TEST_B_ITEM], 99);
	osmo_stat_item_set(statg->items[TEST_B_ITEM], 100);

	rc = osmo_stat_item_discard_all(&rd_a);
	rc = osmo_stat_item_discard_all(&rd_b);

	rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
	OSMO_ASSERT(rc == 0);
	rc = osmo_stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value);
	OSMO_ASSERT(rc == 0);

	osmo_stat_item_group_free(statg);

	sgrp2 = osmo_stat_item_get_group_by_name_idx("test.one", 0);
	OSMO_ASSERT(sgrp2 == NULL);
}

int main(int argc, char **argv)
{
	static const struct log_info log_info = {};
	log_init(&log_info, NULL);

	osmo_stat_item_init(NULL);

	stat_test();
	return 0;
}