universe@768: /* universe@768: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@768: * universe@768: * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved. universe@768: * universe@768: * Redistribution and use in source and binary forms, with or without universe@768: * modification, are permitted provided that the following conditions are met: universe@768: * universe@768: * 1. Redistributions of source code must retain the above copyright universe@768: * notice, this list of conditions and the following disclaimer. universe@768: * universe@768: * 2. Redistributions in binary form must reproduce the above copyright universe@768: * notice, this list of conditions and the following disclaimer in the universe@768: * documentation and/or other materials provided with the distribution. universe@768: * universe@768: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@768: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@768: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@768: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@768: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@768: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@768: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@768: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@768: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@768: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@768: * POSSIBILITY OF SUCH DAMAGE. universe@768: */ universe@768: universe@768: #include "cx/test.h" universe@768: universe@768: #include "cx/hash_key.h" universe@768: #include "cx/string.h" universe@768: universe@768: CX_TEST(test_hash_key_functions) { universe@768: char const* str = "my key"; universe@768: size_t len = strlen(str); universe@768: universe@768: CxHashKey str_key = cx_hash_key_str(str); universe@768: CxHashKey bytes_key = cx_hash_key_bytes((unsigned char const*)str, len); universe@768: CxHashKey obj_key = cx_hash_key(str, len); universe@768: CxHashKey cxstr_key = cx_hash_key_cxstr(cx_str(str)); universe@768: universe@768: CX_TEST_DO { universe@768: CX_TEST_ASSERT(str_key.hash == bytes_key.hash); universe@768: CX_TEST_ASSERT(obj_key.hash == bytes_key.hash); universe@768: CX_TEST_ASSERT(cxstr_key.hash == bytes_key.hash); universe@768: CX_TEST_ASSERT(str_key.len == len); universe@768: CX_TEST_ASSERT(cxstr_key.len == len); universe@768: CX_TEST_ASSERT(bytes_key.len == len); universe@768: CX_TEST_ASSERT(bytes_key.len == len); universe@768: CX_TEST_ASSERT(str_key.data == str); universe@768: } universe@768: } universe@768: universe@768: CX_TEST(test_hash_key_empty_string) { universe@768: char const* str = ""; universe@768: universe@768: CxHashKey str_key = cx_hash_key_str(str); universe@768: CxHashKey bytes_key = cx_hash_key_bytes((unsigned char const*) str, 0); universe@768: CxHashKey obj_key = cx_hash_key(str, 0); universe@768: universe@768: CX_TEST_DO { universe@768: CX_TEST_ASSERT(bytes_key.hash == 4152238450u); universe@768: CX_TEST_ASSERT(str_key.hash == 4152238450u); universe@768: CX_TEST_ASSERT(obj_key.hash == 4152238450u); universe@768: CX_TEST_ASSERT(str_key.len == 0); universe@768: CX_TEST_ASSERT(bytes_key.len == 0); universe@768: CX_TEST_ASSERT(bytes_key.len == 0); universe@768: CX_TEST_ASSERT(str_key.data == str); universe@768: } universe@768: } universe@768: universe@768: CX_TEST(test_hash_key_null) { universe@768: CxHashKey str_key = cx_hash_key_str(NULL); universe@768: CxHashKey bytes_key = cx_hash_key_bytes(NULL, 0); universe@768: CxHashKey obj_key = cx_hash_key(NULL, 0); universe@768: universe@768: CX_TEST_DO { universe@768: CX_TEST_ASSERT(bytes_key.hash == 1574210520u); universe@768: CX_TEST_ASSERT(str_key.hash == 1574210520u); universe@768: CX_TEST_ASSERT(obj_key.hash == 1574210520u); universe@768: CX_TEST_ASSERT(str_key.len == 0); universe@768: CX_TEST_ASSERT(bytes_key.len == 0); universe@768: CX_TEST_ASSERT(bytes_key.len == 0); universe@768: CX_TEST_ASSERT(str_key.data == NULL); universe@768: } universe@768: } universe@768: universe@768: CxTestSuite *cx_test_suite_hash_key(void) { universe@768: CxTestSuite *suite = cx_test_suite_new("hash_key"); universe@768: universe@768: cx_test_register(suite, test_hash_key_functions); universe@768: cx_test_register(suite, test_hash_key_empty_string); universe@768: cx_test_register(suite, test_hash_key_null); universe@768: universe@768: return suite; universe@768: }