1 1.1 christos /* $NetBSD: hash_test.c,v 1.3 2025/01/26 16:25:49 christos Exp $ */ 2 1.1 christos 3 1.1 christos /* 4 1.1 christos * Copyright (C) Internet Systems Consortium, Inc. ("ISC") 5 1.1 christos * 6 1.1 christos * SPDX-License-Identifier: MPL-2.0 7 1.1 christos * 8 1.1 christos * This Source Code Form is subject to the terms of the Mozilla Public 9 1.1 christos * License, v. 2.0. If a copy of the MPL was not distributed with this 10 1.1 christos * file, you can obtain one at https://mozilla.org/MPL/2.0/. 11 1.1 christos * 12 1.1 christos * See the COPYRIGHT file distributed with this work for additional 13 1.1 christos * information regarding copyright ownership. 14 1.1 christos */ 15 1.1 christos 16 1.1 christos #include <inttypes.h> 17 1.1 christos #include <sched.h> /* IWYU pragma: keep */ 18 1.1 christos #include <setjmp.h> 19 1.1 christos #include <stdarg.h> 20 1.1 christos #include <stddef.h> 21 1.1 christos #include <stdio.h> 22 1.1 christos #include <stdlib.h> 23 1.1 christos #include <string.h> 24 1.1 christos #include <unistd.h> 25 1.1 christos 26 1.1 christos #define UNIT_TESTING 27 1.1 christos #include <cmocka.h> 28 1.1 christos 29 1.1 christos #include <isc/buffer.h> 30 1.1 christos #include <isc/hash.h> 31 1.1 christos #include <isc/hex.h> 32 1.1 christos #include <isc/region.h> 33 1.1 christos #include <isc/string.h> 34 1.1 christos #include <isc/util.h> 35 1.1 christos 36 1.1 christos #include <tests/isc.h> 37 1.1 christos 38 1.1 christos /* Hash function test */ 39 1.3 christos ISC_RUN_TEST_IMPL(isc_hash32) { 40 1.3 christos uint32_t h1; 41 1.3 christos uint32_t h2; 42 1.1 christos 43 1.3 christos /* Immutability of hash function */ 44 1.3 christos h1 = isc_hash32(NULL, 0, true); 45 1.3 christos h2 = isc_hash32(NULL, 0, true); 46 1.3 christos 47 1.3 christos assert_int_equal(h1, h2); 48 1.3 christos 49 1.3 christos /* Hash function characteristics */ 50 1.3 christos h1 = isc_hash32("Hello world", 12, true); 51 1.3 christos h2 = isc_hash32("Hello world", 12, true); 52 1.3 christos 53 1.3 christos assert_int_equal(h1, h2); 54 1.3 christos 55 1.3 christos /* Case */ 56 1.3 christos h1 = isc_hash32("Hello world", 12, false); 57 1.3 christos h2 = isc_hash32("heLLo WorLd", 12, false); 58 1.3 christos 59 1.3 christos assert_int_equal(h1, h2); 60 1.3 christos 61 1.3 christos /* Unequal */ 62 1.3 christos h1 = isc_hash32("Hello world", 12, true); 63 1.3 christos h2 = isc_hash32("heLLo WorLd", 12, true); 64 1.3 christos 65 1.3 christos assert_int_not_equal(h1, h2); 66 1.3 christos } 67 1.3 christos 68 1.3 christos /* Hash function test */ 69 1.3 christos ISC_RUN_TEST_IMPL(isc_hash64) { 70 1.3 christos uint64_t h1; 71 1.3 christos uint64_t h2; 72 1.1 christos 73 1.1 christos /* Immutability of hash function */ 74 1.3 christos h1 = isc_hash64(NULL, 0, true); 75 1.3 christos h2 = isc_hash64(NULL, 0, true); 76 1.1 christos 77 1.1 christos assert_int_equal(h1, h2); 78 1.1 christos 79 1.1 christos /* Hash function characteristics */ 80 1.3 christos h1 = isc_hash64("Hello world", 12, true); 81 1.3 christos h2 = isc_hash64("Hello world", 12, true); 82 1.1 christos 83 1.1 christos assert_int_equal(h1, h2); 84 1.1 christos 85 1.1 christos /* Case */ 86 1.3 christos h1 = isc_hash64("Hello world", 12, false); 87 1.3 christos h2 = isc_hash64("heLLo WorLd", 12, false); 88 1.1 christos 89 1.1 christos assert_int_equal(h1, h2); 90 1.1 christos 91 1.1 christos /* Unequal */ 92 1.3 christos h1 = isc_hash64("Hello world", 12, true); 93 1.3 christos h2 = isc_hash64("heLLo WorLd", 12, true); 94 1.1 christos 95 1.1 christos assert_int_not_equal(h1, h2); 96 1.1 christos } 97 1.1 christos 98 1.1 christos /* Hash function initializer test */ 99 1.1 christos ISC_RUN_TEST_IMPL(isc_hash_initializer) { 100 1.3 christos uint64_t h1; 101 1.3 christos uint64_t h2; 102 1.1 christos 103 1.3 christos h1 = isc_hash64("Hello world", 12, true); 104 1.3 christos h2 = isc_hash64("Hello world", 12, true); 105 1.1 christos 106 1.1 christos assert_int_equal(h1, h2); 107 1.1 christos 108 1.1 christos isc_hash_set_initializer(isc_hash_get_initializer()); 109 1.1 christos 110 1.1 christos /* Hash value must not change */ 111 1.3 christos h2 = isc_hash64("Hello world", 12, true); 112 1.1 christos 113 1.1 christos assert_int_equal(h1, h2); 114 1.1 christos } 115 1.1 christos 116 1.1 christos ISC_TEST_LIST_START 117 1.1 christos 118 1.3 christos ISC_TEST_ENTRY(isc_hash32) 119 1.3 christos ISC_TEST_ENTRY(isc_hash64) 120 1.1 christos ISC_TEST_ENTRY(isc_hash_initializer) 121 1.1 christos 122 1.1 christos ISC_TEST_LIST_END 123 1.1 christos 124 1.1 christos ISC_TEST_MAIN 125