Debian Codegraph @

tests/isc/stats_test.c

raw
1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * SPDX-License-Identifier: MPL-2.0
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * See the COPYRIGHT file distributed with this work for additional
11 * information regarding copyright ownership.
12 */
13
14 #include <inttypes.h>
15 #include <sched.h> /* IWYU pragma: keep */
16 #include <setjmp.h>
17 #include <stdarg.h>
18 #include <stddef.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 #define UNIT_TESTING
23 #include <cmocka.h>
24
25 #include <isc/mem.h>
26 #include <isc/result.h>
27 #include <isc/stats.h>
28 #include <isc/util.h>
29
30 #include <tests/isc.h>
31
32 /* test stats */
33 ISC_RUN_TEST_IMPL(isc_stats_basic) {
34 isc_stats_t *stats = NULL;
35
36 isc_stats_create(mctx, &stats, 4);
37 assert_int_equal(isc_stats_ncounters(stats), 4);
38
39 /* Default all 0. */
40 for (int i = 0; i < isc_stats_ncounters(stats); i++) {
41 assert_int_equal(isc_stats_get_counter(stats, i), 0);
42 }
43
44 /* Test increment. */
45 for (int i = 0; i < isc_stats_ncounters(stats); i++) {
46 isc_stats_increment(stats, i);
47 assert_int_equal(isc_stats_get_counter(stats, i), 1);
48 isc_stats_increment(stats, i);
49 assert_int_equal(isc_stats_get_counter(stats, i), 2);
50 }
51
52 /* Test decrement. */
53 for (int i = 0; i < isc_stats_ncounters(stats); i++) {
54 isc_stats_decrement(stats, i);
55 assert_int_equal(isc_stats_get_counter(stats, i), 1);
56 isc_stats_decrement(stats, i);
57 assert_int_equal(isc_stats_get_counter(stats, i), 0);
58 }
59
60 /* Test set. */
61 for (int i = 0; i < isc_stats_ncounters(stats); i++) {
62 isc_stats_set(stats, i, i);
63 assert_int_equal(isc_stats_get_counter(stats, i), i);
64 }
65
66 /* Test update if greater. */
67 for (int i = 0; i < isc_stats_ncounters(stats); i++) {
68 isc_stats_update_if_greater(stats, i, i);
69 assert_int_equal(isc_stats_get_counter(stats, i), i);
70 isc_stats_update_if_greater(stats, i, i + 1);
71 assert_int_equal(isc_stats_get_counter(stats, i), i + 1);
72 }
73
74 /* Test resize. */
75 isc_stats_resize(&stats, 3);
76 assert_int_equal(isc_stats_ncounters(stats), 4);
77 isc_stats_resize(&stats, 4);
78 assert_int_equal(isc_stats_ncounters(stats), 4);
79 isc_stats_resize(&stats, 5);
80 assert_int_equal(isc_stats_ncounters(stats), 5);
81
82 /* Existing counters are retained */
83 for (int i = 0; i < isc_stats_ncounters(stats); i++) {
84 uint32_t expect = i + 1;
85 if (i == 4) {
86 expect = 0;
87 }
88 assert_int_equal(isc_stats_get_counter(stats, i), expect);
89 }
90
91 isc_stats_detach(&stats);
92 }
93
94 ISC_TEST_LIST_START
95
96 ISC_TEST_ENTRY(isc_stats_basic)
97
98 ISC_TEST_LIST_END
99
100 ISC_TEST_MAIN