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