xref: /netbsd-src/external/mpl/bind/dist/tests/isc/hash_test.c (revision bcda20f65a8566e103791ec395f7f499ef322704)
1*bcda20f6Schristos /*	$NetBSD: hash_test.c,v 1.3 2025/01/26 16:25:49 christos Exp $	*/
28aaca124Schristos 
38aaca124Schristos /*
48aaca124Schristos  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
58aaca124Schristos  *
68aaca124Schristos  * SPDX-License-Identifier: MPL-2.0
78aaca124Schristos  *
88aaca124Schristos  * This Source Code Form is subject to the terms of the Mozilla Public
98aaca124Schristos  * License, v. 2.0. If a copy of the MPL was not distributed with this
108aaca124Schristos  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
118aaca124Schristos  *
128aaca124Schristos  * See the COPYRIGHT file distributed with this work for additional
138aaca124Schristos  * information regarding copyright ownership.
148aaca124Schristos  */
158aaca124Schristos 
168aaca124Schristos #include <inttypes.h>
178aaca124Schristos #include <sched.h> /* IWYU pragma: keep */
188aaca124Schristos #include <setjmp.h>
198aaca124Schristos #include <stdarg.h>
208aaca124Schristos #include <stddef.h>
218aaca124Schristos #include <stdio.h>
228aaca124Schristos #include <stdlib.h>
238aaca124Schristos #include <string.h>
248aaca124Schristos #include <unistd.h>
258aaca124Schristos 
268aaca124Schristos #define UNIT_TESTING
278aaca124Schristos #include <cmocka.h>
288aaca124Schristos 
298aaca124Schristos #include <isc/buffer.h>
308aaca124Schristos #include <isc/hash.h>
318aaca124Schristos #include <isc/hex.h>
328aaca124Schristos #include <isc/region.h>
338aaca124Schristos #include <isc/string.h>
348aaca124Schristos #include <isc/util.h>
358aaca124Schristos 
368aaca124Schristos #include <tests/isc.h>
378aaca124Schristos 
388aaca124Schristos /* Hash function test */
39*bcda20f6Schristos ISC_RUN_TEST_IMPL(isc_hash32) {
40*bcda20f6Schristos 	uint32_t h1;
41*bcda20f6Schristos 	uint32_t h2;
428aaca124Schristos 
438aaca124Schristos 	/* Immutability of hash function */
44*bcda20f6Schristos 	h1 = isc_hash32(NULL, 0, true);
45*bcda20f6Schristos 	h2 = isc_hash32(NULL, 0, true);
468aaca124Schristos 
478aaca124Schristos 	assert_int_equal(h1, h2);
488aaca124Schristos 
498aaca124Schristos 	/* Hash function characteristics */
50*bcda20f6Schristos 	h1 = isc_hash32("Hello world", 12, true);
51*bcda20f6Schristos 	h2 = isc_hash32("Hello world", 12, true);
528aaca124Schristos 
538aaca124Schristos 	assert_int_equal(h1, h2);
548aaca124Schristos 
558aaca124Schristos 	/* Case */
56*bcda20f6Schristos 	h1 = isc_hash32("Hello world", 12, false);
57*bcda20f6Schristos 	h2 = isc_hash32("heLLo WorLd", 12, false);
588aaca124Schristos 
598aaca124Schristos 	assert_int_equal(h1, h2);
608aaca124Schristos 
618aaca124Schristos 	/* Unequal */
62*bcda20f6Schristos 	h1 = isc_hash32("Hello world", 12, true);
63*bcda20f6Schristos 	h2 = isc_hash32("heLLo WorLd", 12, true);
64*bcda20f6Schristos 
65*bcda20f6Schristos 	assert_int_not_equal(h1, h2);
66*bcda20f6Schristos }
67*bcda20f6Schristos 
68*bcda20f6Schristos /* Hash function test */
69*bcda20f6Schristos ISC_RUN_TEST_IMPL(isc_hash64) {
70*bcda20f6Schristos 	uint64_t h1;
71*bcda20f6Schristos 	uint64_t h2;
72*bcda20f6Schristos 
73*bcda20f6Schristos 	/* Immutability of hash function */
74*bcda20f6Schristos 	h1 = isc_hash64(NULL, 0, true);
75*bcda20f6Schristos 	h2 = isc_hash64(NULL, 0, true);
76*bcda20f6Schristos 
77*bcda20f6Schristos 	assert_int_equal(h1, h2);
78*bcda20f6Schristos 
79*bcda20f6Schristos 	/* Hash function characteristics */
80*bcda20f6Schristos 	h1 = isc_hash64("Hello world", 12, true);
81*bcda20f6Schristos 	h2 = isc_hash64("Hello world", 12, true);
82*bcda20f6Schristos 
83*bcda20f6Schristos 	assert_int_equal(h1, h2);
84*bcda20f6Schristos 
85*bcda20f6Schristos 	/* Case */
86*bcda20f6Schristos 	h1 = isc_hash64("Hello world", 12, false);
87*bcda20f6Schristos 	h2 = isc_hash64("heLLo WorLd", 12, false);
88*bcda20f6Schristos 
89*bcda20f6Schristos 	assert_int_equal(h1, h2);
90*bcda20f6Schristos 
91*bcda20f6Schristos 	/* Unequal */
92*bcda20f6Schristos 	h1 = isc_hash64("Hello world", 12, true);
93*bcda20f6Schristos 	h2 = isc_hash64("heLLo WorLd", 12, true);
948aaca124Schristos 
958aaca124Schristos 	assert_int_not_equal(h1, h2);
968aaca124Schristos }
978aaca124Schristos 
988aaca124Schristos /* Hash function initializer test */
998aaca124Schristos ISC_RUN_TEST_IMPL(isc_hash_initializer) {
100*bcda20f6Schristos 	uint64_t h1;
101*bcda20f6Schristos 	uint64_t h2;
1028aaca124Schristos 
103*bcda20f6Schristos 	h1 = isc_hash64("Hello world", 12, true);
104*bcda20f6Schristos 	h2 = isc_hash64("Hello world", 12, true);
1058aaca124Schristos 
1068aaca124Schristos 	assert_int_equal(h1, h2);
1078aaca124Schristos 
1088aaca124Schristos 	isc_hash_set_initializer(isc_hash_get_initializer());
1098aaca124Schristos 
1108aaca124Schristos 	/* Hash value must not change */
111*bcda20f6Schristos 	h2 = isc_hash64("Hello world", 12, true);
1128aaca124Schristos 
1138aaca124Schristos 	assert_int_equal(h1, h2);
1148aaca124Schristos }
1158aaca124Schristos 
1168aaca124Schristos ISC_TEST_LIST_START
1178aaca124Schristos 
118*bcda20f6Schristos ISC_TEST_ENTRY(isc_hash32)
119*bcda20f6Schristos ISC_TEST_ENTRY(isc_hash64)
1208aaca124Schristos ISC_TEST_ENTRY(isc_hash_initializer)
1218aaca124Schristos 
1228aaca124Schristos ISC_TEST_LIST_END
1238aaca124Schristos 
1248aaca124Schristos ISC_TEST_MAIN
125