xref: /netbsd-src/external/mpl/bind/dist/tests/isc/counter_test.c (revision bcda20f65a8566e103791ec395f7f499ef322704)
1 /*	$NetBSD: counter_test.c,v 1.3 2025/01/26 16:25:49 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/counter.h>
28 #include <isc/result.h>
29 #include <isc/util.h>
30 
31 #include <tests/isc.h>
32 
33 /* test isc_counter object */
34 ISC_RUN_TEST_IMPL(isc_counter) {
35 	isc_result_t result;
36 	isc_counter_t *counter = NULL;
37 	int i;
38 
39 	UNUSED(state);
40 
41 	result = isc_counter_create(mctx, 0, &counter);
42 	assert_int_equal(result, ISC_R_SUCCESS);
43 
44 	for (i = 0; i < 10; i++) {
45 		result = isc_counter_increment(counter);
46 		assert_int_equal(result, ISC_R_SUCCESS);
47 	}
48 
49 	assert_int_equal(isc_counter_used(counter), 10);
50 
51 	isc_counter_setlimit(counter, 15);
52 	for (i = 0; i < 10; i++) {
53 		result = isc_counter_increment(counter);
54 		if (result != ISC_R_SUCCESS) {
55 			break;
56 		}
57 	}
58 
59 	assert_int_equal(isc_counter_used(counter), 15);
60 
61 	isc_counter_detach(&counter);
62 }
63 
64 ISC_TEST_LIST_START
65 
66 ISC_TEST_ENTRY(isc_counter)
67 
68 ISC_TEST_LIST_END
69 
70 ISC_TEST_MAIN
71