1/*	$NetBSD: heap_test.c,v 1.2 2024/02/21 22:52: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/* ! \file */
17
18#include <inttypes.h>
19#include <sched.h> /* IWYU pragma: keep */
20#include <setjmp.h>
21#include <stdarg.h>
22#include <stddef.h>
23#include <stdlib.h>
24#include <string.h>
25
26#define UNIT_TESTING
27#include <cmocka.h>
28
29#include <isc/heap.h>
30#include <isc/mem.h>
31#include <isc/util.h>
32
33#include <tests/isc.h>
34
35struct e {
36	unsigned int value;
37	unsigned int index;
38};
39
40static bool
41compare(void *p1, void *p2) {
42	struct e *e1 = p1;
43	struct e *e2 = p2;
44
45	return (e1->value < e2->value);
46}
47
48static void
49idx(void *p, unsigned int i) {
50	struct e *e = p;
51
52	e->index = i;
53}
54
55/* test isc_heap_delete() */
56ISC_RUN_TEST_IMPL(isc_heap_delete) {
57	isc_heap_t *heap = NULL;
58	struct e e1 = { 100, 0 };
59
60	UNUSED(state);
61
62	isc_heap_create(mctx, compare, idx, 0, &heap);
63	assert_non_null(heap);
64
65	isc_heap_insert(heap, &e1);
66	assert_int_equal(e1.index, 1);
67
68	isc_heap_delete(heap, e1.index);
69	assert_int_equal(e1.index, 0);
70
71	isc_heap_destroy(&heap);
72	assert_null(heap);
73}
74
75ISC_TEST_LIST_START
76
77ISC_TEST_ENTRY(isc_heap_delete)
78
79ISC_TEST_LIST_END
80
81ISC_TEST_MAIN
82