tests/test_hash_key.c

Wed, 20 Dec 2023 18:13:30 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 20 Dec 2023 18:13:30 +0100
changeset 768
0e1cf2cd500e
permissions
-rw-r--r--

migrate hash_key tests - relates to #342

universe@768 1 /*
universe@768 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@768 3 *
universe@768 4 * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved.
universe@768 5 *
universe@768 6 * Redistribution and use in source and binary forms, with or without
universe@768 7 * modification, are permitted provided that the following conditions are met:
universe@768 8 *
universe@768 9 * 1. Redistributions of source code must retain the above copyright
universe@768 10 * notice, this list of conditions and the following disclaimer.
universe@768 11 *
universe@768 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@768 13 * notice, this list of conditions and the following disclaimer in the
universe@768 14 * documentation and/or other materials provided with the distribution.
universe@768 15 *
universe@768 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@768 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@768 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@768 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@768 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@768 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@768 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@768 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@768 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@768 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@768 26 * POSSIBILITY OF SUCH DAMAGE.
universe@768 27 */
universe@768 28
universe@768 29 #include "cx/test.h"
universe@768 30
universe@768 31 #include "cx/hash_key.h"
universe@768 32 #include "cx/string.h"
universe@768 33
universe@768 34 CX_TEST(test_hash_key_functions) {
universe@768 35 char const* str = "my key";
universe@768 36 size_t len = strlen(str);
universe@768 37
universe@768 38 CxHashKey str_key = cx_hash_key_str(str);
universe@768 39 CxHashKey bytes_key = cx_hash_key_bytes((unsigned char const*)str, len);
universe@768 40 CxHashKey obj_key = cx_hash_key(str, len);
universe@768 41 CxHashKey cxstr_key = cx_hash_key_cxstr(cx_str(str));
universe@768 42
universe@768 43 CX_TEST_DO {
universe@768 44 CX_TEST_ASSERT(str_key.hash == bytes_key.hash);
universe@768 45 CX_TEST_ASSERT(obj_key.hash == bytes_key.hash);
universe@768 46 CX_TEST_ASSERT(cxstr_key.hash == bytes_key.hash);
universe@768 47 CX_TEST_ASSERT(str_key.len == len);
universe@768 48 CX_TEST_ASSERT(cxstr_key.len == len);
universe@768 49 CX_TEST_ASSERT(bytes_key.len == len);
universe@768 50 CX_TEST_ASSERT(bytes_key.len == len);
universe@768 51 CX_TEST_ASSERT(str_key.data == str);
universe@768 52 }
universe@768 53 }
universe@768 54
universe@768 55 CX_TEST(test_hash_key_empty_string) {
universe@768 56 char const* str = "";
universe@768 57
universe@768 58 CxHashKey str_key = cx_hash_key_str(str);
universe@768 59 CxHashKey bytes_key = cx_hash_key_bytes((unsigned char const*) str, 0);
universe@768 60 CxHashKey obj_key = cx_hash_key(str, 0);
universe@768 61
universe@768 62 CX_TEST_DO {
universe@768 63 CX_TEST_ASSERT(bytes_key.hash == 4152238450u);
universe@768 64 CX_TEST_ASSERT(str_key.hash == 4152238450u);
universe@768 65 CX_TEST_ASSERT(obj_key.hash == 4152238450u);
universe@768 66 CX_TEST_ASSERT(str_key.len == 0);
universe@768 67 CX_TEST_ASSERT(bytes_key.len == 0);
universe@768 68 CX_TEST_ASSERT(bytes_key.len == 0);
universe@768 69 CX_TEST_ASSERT(str_key.data == str);
universe@768 70 }
universe@768 71 }
universe@768 72
universe@768 73 CX_TEST(test_hash_key_null) {
universe@768 74 CxHashKey str_key = cx_hash_key_str(NULL);
universe@768 75 CxHashKey bytes_key = cx_hash_key_bytes(NULL, 0);
universe@768 76 CxHashKey obj_key = cx_hash_key(NULL, 0);
universe@768 77
universe@768 78 CX_TEST_DO {
universe@768 79 CX_TEST_ASSERT(bytes_key.hash == 1574210520u);
universe@768 80 CX_TEST_ASSERT(str_key.hash == 1574210520u);
universe@768 81 CX_TEST_ASSERT(obj_key.hash == 1574210520u);
universe@768 82 CX_TEST_ASSERT(str_key.len == 0);
universe@768 83 CX_TEST_ASSERT(bytes_key.len == 0);
universe@768 84 CX_TEST_ASSERT(bytes_key.len == 0);
universe@768 85 CX_TEST_ASSERT(str_key.data == NULL);
universe@768 86 }
universe@768 87 }
universe@768 88
universe@768 89 CxTestSuite *cx_test_suite_hash_key(void) {
universe@768 90 CxTestSuite *suite = cx_test_suite_new("hash_key");
universe@768 91
universe@768 92 cx_test_register(suite, test_hash_key_functions);
universe@768 93 cx_test_register(suite, test_hash_key_empty_string);
universe@768 94 cx_test_register(suite, test_hash_key_null);
universe@768 95
universe@768 96 return suite;
universe@768 97 }

mercurial