UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include "cx/test.h" 30 31 #include "cx/hash_key.h" 32 #include "cx/string.h" 33 34 CX_TEST(test_hash_key_functions) { 35 const char *str = "my key"; 36 const unsigned char *ustr = (const unsigned char *) str; 37 size_t len = strlen(str); 38 39 CxHashKey str_key = cx_hash_key_str(str); 40 CxHashKey ustr_key = cx_hash_key_ustr(ustr); 41 CxHashKey bytes_key = cx_hash_key_bytes(ustr, len); 42 CxHashKey obj_key = cx_hash_key(str, len); 43 CxHashKey cxstr_key = cx_hash_key_cxstr(cx_str(str)); 44 45 CX_TEST_DO { 46 CX_TEST_ASSERT(bytes_key.hash == 1269566022); 47 CX_TEST_ASSERT(str_key.hash == bytes_key.hash); 48 CX_TEST_ASSERT(ustr_key.hash == bytes_key.hash); 49 CX_TEST_ASSERT(obj_key.hash == bytes_key.hash); 50 CX_TEST_ASSERT(cxstr_key.hash == bytes_key.hash); 51 CX_TEST_ASSERT(str_key.len == len); 52 CX_TEST_ASSERT(str_key.data == str); 53 CX_TEST_ASSERT(ustr_key.len == len); 54 CX_TEST_ASSERT(ustr_key.data == ustr); 55 CX_TEST_ASSERT(cxstr_key.len == len); 56 CX_TEST_ASSERT(cxstr_key.data == str); 57 CX_TEST_ASSERT(obj_key.len == len); 58 CX_TEST_ASSERT(obj_key.data == str); 59 CX_TEST_ASSERT(bytes_key.len == len); 60 CX_TEST_ASSERT(bytes_key.data == str); 61 } 62 } 63 64 CX_TEST(test_hash_key_int_functions) { 65 uint32_t a = 0xabc01337u; 66 uint64_t b = 0xabc0133747110815ull; 67 68 CxHashKey ak = cx_hash_key_u32(a); 69 CxHashKey bk = cx_hash_key_u64(b); 70 71 CX_TEST_DO { 72 CX_TEST_ASSERT(ak.data == NULL); 73 CX_TEST_ASSERT(ak.len == 0); 74 CX_TEST_ASSERT(ak.hash == 3897006249); 75 CX_TEST_ASSERT(bk.data == NULL); 76 CX_TEST_ASSERT(bk.len == 0); 77 CX_TEST_ASSERT(bk.hash == 17452435587688253422ull); 78 } 79 } 80 81 CX_TEST(test_hash_key_macro) { 82 const char *str = "my key"; 83 size_t len = strlen(str); 84 uint32_t a = 0xabc01337u; 85 uint64_t b = 0xabc0133747110815ull; 86 87 CxHashKey str_key = CX_HASH_KEY(str); 88 CxHashKey bytes_key = CX_HASH_KEY((const unsigned char*)str); 89 CxHashKey mutstr_key = CX_HASH_KEY(cx_mutstr((char*)str)); 90 CxHashKey cxstr_key = CX_HASH_KEY(cx_str(str)); 91 CxHashKey ak = CX_HASH_KEY(a); 92 CxHashKey bk = CX_HASH_KEY(b); 93 94 CX_TEST_DO { 95 CX_TEST_ASSERT(bytes_key.hash == 1269566022); 96 CX_TEST_ASSERT(str_key.hash == bytes_key.hash); 97 CX_TEST_ASSERT(cxstr_key.hash == bytes_key.hash); 98 CX_TEST_ASSERT(mutstr_key.hash == bytes_key.hash); 99 CX_TEST_ASSERT(str_key.len == len); 100 CX_TEST_ASSERT(str_key.data == str); 101 CX_TEST_ASSERT(cxstr_key.len == len); 102 CX_TEST_ASSERT(cxstr_key.data == str); 103 CX_TEST_ASSERT(bytes_key.len == len); 104 CX_TEST_ASSERT(bytes_key.data == str); 105 CX_TEST_ASSERT(ak.data == NULL); 106 CX_TEST_ASSERT(ak.len == 0); 107 CX_TEST_ASSERT(ak.hash == 3897006249); 108 CX_TEST_ASSERT(bk.data == NULL); 109 CX_TEST_ASSERT(bk.len == 0); 110 CX_TEST_ASSERT(bk.hash == 17452435587688253422ull); 111 } 112 } 113 114 CX_TEST(test_hash_key_cmp) { 115 CxHashKey k1 = CX_HASH_KEY("one key"); 116 CxHashKey k2 = CX_HASH_KEY("another key"); 117 CxHashKey k = CX_HASH_KEY("one key"); 118 CX_TEST_DO { 119 CX_TEST_ASSERT(0 == cx_hash_key_cmp(&k1, &k)); 120 CX_TEST_ASSERT(0 == cx_hash_key_cmp(&k1, &k1)); 121 CX_TEST_ASSERT(0 == cx_hash_key_cmp(&k2, &k2)); 122 CX_TEST_ASSERT(0 != cx_hash_key_cmp(&k2, &k)); 123 CX_TEST_ASSERT(0 != cx_hash_key_cmp(&k, &k2)); 124 CX_TEST_ASSERT(0 != cx_hash_key_cmp(&k1, &k2)); 125 126 // fake a hash collision 127 k1.hash = k2.hash = 4711; 128 // still not equal 129 CX_TEST_ASSERT(0 != cx_hash_key_cmp(&k1, &k2)); 130 } 131 } 132 133 CX_TEST(test_hash_key_empty_string) { 134 const char *str = ""; 135 const unsigned char *ustr = (const unsigned char*)""; 136 137 CxHashKey str_key = cx_hash_key_str(str); 138 CxHashKey ustr_key = cx_hash_key_ustr(ustr); 139 CxHashKey bytes_key = cx_hash_key_bytes((const unsigned char*) str, 0); 140 CxHashKey obj_key = cx_hash_key(str, 0); 141 142 CX_TEST_DO { 143 CX_TEST_ASSERT(bytes_key.hash == 4152238450u); 144 CX_TEST_ASSERT(str_key.hash == 4152238450u); 145 CX_TEST_ASSERT(ustr_key.hash == 4152238450u); 146 CX_TEST_ASSERT(obj_key.hash == 4152238450u); 147 CX_TEST_ASSERT(str_key.len == 0); 148 CX_TEST_ASSERT(ustr_key.len == 0); 149 CX_TEST_ASSERT(bytes_key.len == 0); 150 CX_TEST_ASSERT(bytes_key.len == 0); 151 CX_TEST_ASSERT(str_key.data == str); 152 CX_TEST_ASSERT(ustr_key.data == ustr); 153 } 154 } 155 156 CX_TEST(test_hash_key_null) { 157 CxHashKey str_key = cx_hash_key_str(NULL); 158 CxHashKey ustr_key = cx_hash_key_ustr(NULL); 159 CxHashKey bytes_key = cx_hash_key_bytes(NULL, 0); 160 CxHashKey obj_key = cx_hash_key(NULL, 0); 161 162 CX_TEST_DO { 163 CX_TEST_ASSERT(bytes_key.hash == 1574210520u); 164 CX_TEST_ASSERT(str_key.hash == 1574210520u); 165 CX_TEST_ASSERT(ustr_key.hash == 1574210520u); 166 CX_TEST_ASSERT(obj_key.hash == 1574210520u); 167 CX_TEST_ASSERT(str_key.len == 0); 168 CX_TEST_ASSERT(ustr_key.len == 0); 169 CX_TEST_ASSERT(bytes_key.len == 0); 170 CX_TEST_ASSERT(bytes_key.len == 0); 171 CX_TEST_ASSERT(str_key.data == NULL); 172 CX_TEST_ASSERT(ustr_key.data == NULL); 173 } 174 } 175 176 CxTestSuite *cx_test_suite_hash_key(void) { 177 CxTestSuite *suite = cx_test_suite_new("hash_key"); 178 179 cx_test_register(suite, test_hash_key_functions); 180 cx_test_register(suite, test_hash_key_int_functions); 181 cx_test_register(suite, test_hash_key_macro); 182 cx_test_register(suite, test_hash_key_cmp); 183 cx_test_register(suite, test_hash_key_empty_string); 184 cx_test_register(suite, test_hash_key_null); 185 186 return suite; 187 } 188