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