blob: 9da49a4f89c44f369ef75320a940da0372d9d6aa [file] [log] [blame]
Jacob Erlbeck9732cb42015-10-01 20:43:53 +02001/* tests for statistics */
2/*
3 * (C) 2015 Sysmocom s.m.f.c. GmbH
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23#include <osmocom/core/logging.h>
24#include <osmocom/core/utils.h>
25#include <osmocom/core/stat_item.h>
26
27#include <stdio.h>
28
29static void stat_test(void)
30{
31 enum test_items {
32 TEST_A_ITEM,
33 TEST_B_ITEM,
34 };
35
36 static const struct stat_item_desc item_description[] = {
37 { "item.a", "The A value", "ma", 4, -1 },
38 { "item.b", "The B value", "kb", 7, -1 },
39 };
40
41 static const struct stat_item_group_desc statg_desc = {
42 .group_name_prefix = "test.one",
43 .group_description = "Test number 1",
44 .num_items = ARRAY_SIZE(item_description),
45 .item_desc = item_description,
46 };
47
48 struct stat_item_group *statg =
49 stat_item_group_alloc(NULL, &statg_desc, 0);
50
51 struct stat_item_group *sgrp2;
52 const struct stat_item *sitem1, *sitem2;
53 int rc;
54 int32_t value;
55 int32_t rd_a = 0;
56 int32_t rd_b = 0;
57 int i;
58
59 OSMO_ASSERT(statg != NULL);
60
61 sgrp2 = stat_item_get_group_by_name_idx("test.one", 0);
62 OSMO_ASSERT(sgrp2 == statg);
63
64 sgrp2 = stat_item_get_group_by_name_idx("test.one", 1);
65 OSMO_ASSERT(sgrp2 == NULL);
66
67 sgrp2 = stat_item_get_group_by_name_idx("test.two", 0);
68 OSMO_ASSERT(sgrp2 == NULL);
69
70 sitem1 = stat_item_get_by_name(statg, "item.c");
71 OSMO_ASSERT(sitem1 == NULL);
72
73 sitem1 = stat_item_get_by_name(statg, "item.a");
74 OSMO_ASSERT(sitem1 != NULL);
75 OSMO_ASSERT(sitem1 == statg->items[TEST_A_ITEM]);
76
77 sitem2 = stat_item_get_by_name(statg, "item.b");
78 OSMO_ASSERT(sitem2 != NULL);
79 OSMO_ASSERT(sitem2 != sitem1);
80 OSMO_ASSERT(sitem2 == statg->items[TEST_B_ITEM]);
81
82 value = stat_item_get_last(statg->items[TEST_A_ITEM]);
83 OSMO_ASSERT(value == -1);
84
85 rc = stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
86 OSMO_ASSERT(rc == 0);
87
88 stat_item_set(statg->items[TEST_A_ITEM], 1);
89
90 value = stat_item_get_last(statg->items[TEST_A_ITEM]);
91 OSMO_ASSERT(value == 1);
92
93 rc = stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +020094 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020095 OSMO_ASSERT(value == 1);
96
97 rc = stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
98 OSMO_ASSERT(rc == 0);
99
100 for (i = 2; i <= 32; i++) {
101 stat_item_set(statg->items[TEST_A_ITEM], i);
102 stat_item_set(statg->items[TEST_B_ITEM], 1000 + i);
103
104 rc = stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200105 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200106 OSMO_ASSERT(value == i);
107
108 rc = stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200109 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200110 OSMO_ASSERT(value == 1000 + i);
111 }
112
113 /* Keep 2 in FIFO */
114 stat_item_set(statg->items[TEST_A_ITEM], 33);
115 stat_item_set(statg->items[TEST_B_ITEM], 1000 + 33);
116
117 for (i = 34; i <= 64; i++) {
118 stat_item_set(statg->items[TEST_A_ITEM], i);
119 stat_item_set(statg->items[TEST_B_ITEM], 1000 + i);
120
121 rc = stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200122 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200123 OSMO_ASSERT(value == i-1);
124
125 rc = stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200126 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200127 OSMO_ASSERT(value == 1000 + i-1);
128 }
129
130 rc = stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200131 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200132 OSMO_ASSERT(value == 64);
133
134 rc = stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200135 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200136 OSMO_ASSERT(value == 1000 + 64);
137
138 /* Overrun FIFOs */
139 for (i = 65; i <= 96; i++) {
140 stat_item_set(statg->items[TEST_A_ITEM], i);
141 stat_item_set(statg->items[TEST_B_ITEM], 1000 + i);
142 }
143
144 rc = stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200145 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200146 OSMO_ASSERT(value == 93);
147
148 for (i = 94; i <= 96; i++) {
149 rc = stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200150 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200151 OSMO_ASSERT(value == i);
152 }
153
154 rc = stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200155 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200156 OSMO_ASSERT(value == 1000 + 90);
157
158 for (i = 91; i <= 96; i++) {
159 rc = stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200160 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200161 OSMO_ASSERT(value == 1000 + i);
162 }
163
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200164 /* Test Discard (single item) */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200165 stat_item_set(statg->items[TEST_A_ITEM], 97);
166 rc = stat_item_discard(statg->items[TEST_A_ITEM], &rd_a);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200167 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200168
169 rc = stat_item_discard(statg->items[TEST_A_ITEM], &rd_a);
170 OSMO_ASSERT(rc == 0);
171
172 rc = stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
173 OSMO_ASSERT(rc == 0);
174
175 stat_item_set(statg->items[TEST_A_ITEM], 98);
176 rc = stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200177 OSMO_ASSERT(rc > 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200178 OSMO_ASSERT(value == 98);
179
180 rc = stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
181 OSMO_ASSERT(rc == 0);
182
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200183 /* Test Discard (all items) */
184 stat_item_set(statg->items[TEST_A_ITEM], 99);
185 stat_item_set(statg->items[TEST_A_ITEM], 100);
186 stat_item_set(statg->items[TEST_A_ITEM], 101);
187 stat_item_set(statg->items[TEST_B_ITEM], 99);
188 stat_item_set(statg->items[TEST_B_ITEM], 100);
189
190 rc = stat_item_discard_all(&rd_a);
191 rc = stat_item_discard_all(&rd_b);
192
193 rc = stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
194 OSMO_ASSERT(rc == 0);
195 rc = stat_item_get_next(statg->items[TEST_B_ITEM], &rd_b, &value);
196 OSMO_ASSERT(rc == 0);
197
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200198 stat_item_group_free(statg);
199
200 sgrp2 = stat_item_get_group_by_name_idx("test.one", 0);
201 OSMO_ASSERT(sgrp2 == NULL);
202}
203
204int main(int argc, char **argv)
205{
206 static const struct log_info log_info = {};
207 log_init(&log_info, NULL);
208
209 stat_item_init(NULL);
210
211 stat_test();
212 return 0;
213}