stats_test.c revision 1.1 1 /* $NetBSD: stats_test.c,v 1.1 2024/02/21 21:54:54 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 isc_result_t result;
38
39 UNUSED(state);
40
41 result = isc_stats_create(mctx, &stats, 4);
42 assert_int_equal(result, ISC_R_SUCCESS);
43 assert_int_equal(isc_stats_ncounters(stats), 4);
44
45 /* Default all 0. */
46 for (int i = 0; i < isc_stats_ncounters(stats); i++) {
47 assert_int_equal(isc_stats_get_counter(stats, i), 0);
48 }
49
50 /* Test increment. */
51 for (int i = 0; i < isc_stats_ncounters(stats); i++) {
52 isc_stats_increment(stats, i);
53 assert_int_equal(isc_stats_get_counter(stats, i), 1);
54 isc_stats_increment(stats, i);
55 assert_int_equal(isc_stats_get_counter(stats, i), 2);
56 }
57
58 /* Test decrement. */
59 for (int i = 0; i < isc_stats_ncounters(stats); i++) {
60 isc_stats_decrement(stats, i);
61 assert_int_equal(isc_stats_get_counter(stats, i), 1);
62 isc_stats_decrement(stats, i);
63 assert_int_equal(isc_stats_get_counter(stats, i), 0);
64 }
65
66 /* Test set. */
67 for (int i = 0; i < isc_stats_ncounters(stats); i++) {
68 isc_stats_set(stats, i, i);
69 assert_int_equal(isc_stats_get_counter(stats, i), i);
70 }
71
72 /* Test update if greater. */
73 for (int i = 0; i < isc_stats_ncounters(stats); i++) {
74 isc_stats_update_if_greater(stats, i, i);
75 assert_int_equal(isc_stats_get_counter(stats, i), i);
76 isc_stats_update_if_greater(stats, i, i + 1);
77 assert_int_equal(isc_stats_get_counter(stats, i), i + 1);
78 }
79
80 /* Test resize. */
81 isc_stats_resize(&stats, 3);
82 assert_int_equal(isc_stats_ncounters(stats), 4);
83 isc_stats_resize(&stats, 4);
84 assert_int_equal(isc_stats_ncounters(stats), 4);
85 isc_stats_resize(&stats, 5);
86 assert_int_equal(isc_stats_ncounters(stats), 5);
87
88 /* Existing counters are retained */
89 for (int i = 0; i < isc_stats_ncounters(stats); i++) {
90 uint32_t expect = i + 1;
91 if (i == 4) {
92 expect = 0;
93 }
94 assert_int_equal(isc_stats_get_counter(stats, i), expect);
95 }
96
97 isc_stats_detach(&stats);
98 }
99
100 ISC_TEST_LIST_START
101
102 ISC_TEST_ENTRY(isc_stats_basic)
103
104 ISC_TEST_LIST_END
105
106 ISC_TEST_MAIN
107