| 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 |
/* ! \file */ |
| 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/heap.h> |
| 28 |
#include <isc/mem.h> |
| 29 |
#include <isc/util.h> |
| 30 |
|
| 31 |
#include <tests/isc.h> |
| 32 |
|
| 33 |
struct e { |
| 34 |
unsigned int value; |
| 35 |
unsigned int index; |
| 36 |
}; |
| 37 |
|
| 38 |
static bool |
| 39 |
compare(void *p1, void *p2) { |
| 40 |
struct e *e1 = p1; |
| 41 |
struct e *e2 = p2; |
| 42 |
|
| 43 |
return e1->value < e2->value; |
| 44 |
} |
| 45 |
|
| 46 |
static void |
| 47 |
idx(void *p, unsigned int i) { |
| 48 |
struct e *e = p; |
| 49 |
|
| 50 |
e->index = i; |
| 51 |
} |
| 52 |
|
| 53 |
/* test isc_heap_delete() */ |
| 54 |
ISC_RUN_TEST_IMPL(isc_heap_delete) { |
| 55 |
isc_heap_t *heap = NULL; |
| 56 |
struct e e1 = { 100, 0 }; |
| 57 |
|
| 58 |
UNUSED(state); |
| 59 |
|
| 60 |
isc_heap_create(mctx, compare, idx, 0, &heap); |
| 61 |
assert_non_null(heap); |
| 62 |
|
| 63 |
isc_heap_insert(heap, &e1); |
| 64 |
assert_int_equal(e1.index, 1); |
| 65 |
|
| 66 |
isc_heap_delete(heap, e1.index); |
| 67 |
assert_int_equal(e1.index, 0); |
| 68 |
|
| 69 |
isc_heap_destroy(&heap); |
| 70 |
assert_null(heap); |
| 71 |
} |
| 72 |
|
| 73 |
ISC_TEST_LIST_START |
| 74 |
|
| 75 |
ISC_TEST_ENTRY(isc_heap_delete) |
| 76 |
|
| 77 |
ISC_TEST_LIST_END |
| 78 |
|
| 79 |
ISC_TEST_MAIN |