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 #if HAVE_CMOCKA
17 
18 #include <sched.h> /* IWYU pragma: keep */
19 #include <setjmp.h>
20 #include <stdarg.h>
21 #include <stddef.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #define UNIT_TESTING
26 #include <cmocka.h>
27 
28 #include <isc/heap.h>
29 #include <isc/mem.h>
30 #include <isc/util.h>
31 
32 #include "isctest.h"
33 
34 static int
_setup(void ** state)35 _setup(void **state) {
36 	isc_result_t result;
37 
38 	UNUSED(state);
39 
40 	result = isc_test_begin(NULL, true, 0);
41 	assert_int_equal(result, ISC_R_SUCCESS);
42 
43 	return (0);
44 }
45 
46 static int
_teardown(void ** state)47 _teardown(void **state) {
48 	UNUSED(state);
49 
50 	isc_test_end();
51 
52 	return (0);
53 }
54 
55 struct e {
56 	unsigned int value;
57 	unsigned int index;
58 };
59 
60 static bool
compare(void * p1,void * p2)61 compare(void *p1, void *p2) {
62 	struct e *e1 = p1;
63 	struct e *e2 = p2;
64 
65 	return (e1->value < e2->value);
66 }
67 
68 static void
idx(void * p,unsigned int i)69 idx(void *p, unsigned int i) {
70 	struct e *e = p;
71 
72 	e->index = i;
73 }
74 
75 /* test isc_heap_delete() */
76 static void
isc_heap_delete_test(void ** state)77 isc_heap_delete_test(void **state) {
78 	isc_heap_t *heap = NULL;
79 	isc_result_t result;
80 	struct e e1 = { 100, 0 };
81 
82 	UNUSED(state);
83 
84 	result = isc_heap_create(test_mctx, compare, idx, 0, &heap);
85 	assert_int_equal(result, ISC_R_SUCCESS);
86 	assert_non_null(heap);
87 
88 	isc_heap_insert(heap, &e1);
89 	assert_int_equal(result, ISC_R_SUCCESS);
90 	assert_int_equal(e1.index, 1);
91 
92 	isc_heap_delete(heap, e1.index);
93 	assert_int_equal(e1.index, 0);
94 
95 	isc_heap_destroy(&heap);
96 	assert_null(heap);
97 }
98 
99 int
main(void)100 main(void) {
101 	const struct CMUnitTest tests[] = {
102 		cmocka_unit_test(isc_heap_delete_test),
103 	};
104 
105 	return (cmocka_run_group_tests(tests, _setup, _teardown));
106 }
107 
108 #else /* HAVE_CMOCKA */
109 
110 #include <stdio.h>
111 
112 int
main(void)113 main(void) {
114 	printf("1..0 # Skipped: cmocka not available\n");
115 	return (SKIPPED_TEST_EXIT_CODE);
116 }
117 
118 #endif /* if HAVE_CMOCKA */
119