xref: /netbsd-src/external/mpl/bind/dist/tests/isc/hash_test.c (revision bcda20f65a8566e103791ec395f7f499ef322704)
1 /*	$NetBSD: hash_test.c,v 1.3 2025/01/26 16:25:49 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 #include <inttypes.h>
17 #include <sched.h> /* IWYU pragma: keep */
18 #include <setjmp.h>
19 #include <stdarg.h>
20 #include <stddef.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 
26 #define UNIT_TESTING
27 #include <cmocka.h>
28 
29 #include <isc/buffer.h>
30 #include <isc/hash.h>
31 #include <isc/hex.h>
32 #include <isc/region.h>
33 #include <isc/string.h>
34 #include <isc/util.h>
35 
36 #include <tests/isc.h>
37 
38 /* Hash function test */
39 ISC_RUN_TEST_IMPL(isc_hash32) {
40 	uint32_t h1;
41 	uint32_t h2;
42 
43 	/* Immutability of hash function */
44 	h1 = isc_hash32(NULL, 0, true);
45 	h2 = isc_hash32(NULL, 0, true);
46 
47 	assert_int_equal(h1, h2);
48 
49 	/* Hash function characteristics */
50 	h1 = isc_hash32("Hello world", 12, true);
51 	h2 = isc_hash32("Hello world", 12, true);
52 
53 	assert_int_equal(h1, h2);
54 
55 	/* Case */
56 	h1 = isc_hash32("Hello world", 12, false);
57 	h2 = isc_hash32("heLLo WorLd", 12, false);
58 
59 	assert_int_equal(h1, h2);
60 
61 	/* Unequal */
62 	h1 = isc_hash32("Hello world", 12, true);
63 	h2 = isc_hash32("heLLo WorLd", 12, true);
64 
65 	assert_int_not_equal(h1, h2);
66 }
67 
68 /* Hash function test */
69 ISC_RUN_TEST_IMPL(isc_hash64) {
70 	uint64_t h1;
71 	uint64_t h2;
72 
73 	/* Immutability of hash function */
74 	h1 = isc_hash64(NULL, 0, true);
75 	h2 = isc_hash64(NULL, 0, true);
76 
77 	assert_int_equal(h1, h2);
78 
79 	/* Hash function characteristics */
80 	h1 = isc_hash64("Hello world", 12, true);
81 	h2 = isc_hash64("Hello world", 12, true);
82 
83 	assert_int_equal(h1, h2);
84 
85 	/* Case */
86 	h1 = isc_hash64("Hello world", 12, false);
87 	h2 = isc_hash64("heLLo WorLd", 12, false);
88 
89 	assert_int_equal(h1, h2);
90 
91 	/* Unequal */
92 	h1 = isc_hash64("Hello world", 12, true);
93 	h2 = isc_hash64("heLLo WorLd", 12, true);
94 
95 	assert_int_not_equal(h1, h2);
96 }
97 
98 /* Hash function initializer test */
99 ISC_RUN_TEST_IMPL(isc_hash_initializer) {
100 	uint64_t h1;
101 	uint64_t h2;
102 
103 	h1 = isc_hash64("Hello world", 12, true);
104 	h2 = isc_hash64("Hello world", 12, true);
105 
106 	assert_int_equal(h1, h2);
107 
108 	isc_hash_set_initializer(isc_hash_get_initializer());
109 
110 	/* Hash value must not change */
111 	h2 = isc_hash64("Hello world", 12, true);
112 
113 	assert_int_equal(h1, h2);
114 }
115 
116 ISC_TEST_LIST_START
117 
118 ISC_TEST_ENTRY(isc_hash32)
119 ISC_TEST_ENTRY(isc_hash64)
120 ISC_TEST_ENTRY(isc_hash_initializer)
121 
122 ISC_TEST_LIST_END
123 
124 ISC_TEST_MAIN
125