1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
7  *
8  * See the COPYRIGHT file distributed with this work for additional
9  * information regarding copyright ownership.
10  */
11 
12 /* ! \file */
13 
14 #if HAVE_CMOCKA
15 
16 #include <sched.h> /* IWYU pragma: keep */
17 #include <setjmp.h>
18 #include <stdarg.h>
19 #include <stddef.h>
20 #include <stdlib.h>
21 #include <string.h>
22 
23 #define UNIT_TESTING
24 #include <cmocka.h>
25 
26 #include <isc/heap.h>
27 #include <isc/mem.h>
28 #include <isc/util.h>
29 
30 #include "isctest.h"
31 
32 static int
_setup(void ** state)33 _setup(void **state) {
34 	isc_result_t result;
35 
36 	UNUSED(state);
37 
38 	result = isc_test_begin(NULL, true, 0);
39 	assert_int_equal(result, ISC_R_SUCCESS);
40 
41 	return (0);
42 }
43 
44 static int
_teardown(void ** state)45 _teardown(void **state) {
46 	UNUSED(state);
47 
48 	isc_test_end();
49 
50 	return (0);
51 }
52 
53 struct e {
54 	unsigned int value;
55 	unsigned int index;
56 };
57 
58 static bool
compare(void * p1,void * p2)59 compare(void *p1, void *p2) {
60 	struct e *e1 = p1;
61 	struct e *e2 = p2;
62 
63 	return (e1->value < e2->value);
64 }
65 
66 static void
idx(void * p,unsigned int i)67 idx(void *p, unsigned int i) {
68 	struct e *e = p;
69 
70 	e->index = i;
71 }
72 
73 /* test isc_heap_delete() */
74 static void
isc_heap_delete_test(void ** state)75 isc_heap_delete_test(void **state) {
76 	isc_heap_t *heap = NULL;
77 	isc_result_t result;
78 	struct e e1 = { 100, 0 };
79 
80 	UNUSED(state);
81 
82 	result = isc_heap_create(test_mctx, compare, idx, 0, &heap);
83 	assert_int_equal(result, ISC_R_SUCCESS);
84 	assert_non_null(heap);
85 
86 	isc_heap_insert(heap, &e1);
87 	assert_int_equal(result, ISC_R_SUCCESS);
88 	assert_int_equal(e1.index, 1);
89 
90 	isc_heap_delete(heap, e1.index);
91 	assert_int_equal(e1.index, 0);
92 
93 	isc_heap_destroy(&heap);
94 	assert_null(heap);
95 }
96 
97 int
main(void)98 main(void) {
99 	const struct CMUnitTest tests[] = {
100 		cmocka_unit_test(isc_heap_delete_test),
101 	};
102 
103 	return (cmocka_run_group_tests(tests, _setup, _teardown));
104 }
105 
106 #else /* HAVE_CMOCKA */
107 
108 #include <stdio.h>
109 
110 int
main(void)111 main(void) {
112 	printf("1..0 # Skipped: cmocka not available\n");
113 	return (SKIPPED_TEST_EXIT_CODE);
114 }
115 
116 #endif /* if HAVE_CMOCKA */
117