|
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 char const* str = "my key"; |
|
36 size_t len = strlen(str); |
|
37 |
|
38 CxHashKey str_key = cx_hash_key_str(str); |
|
39 CxHashKey bytes_key = cx_hash_key_bytes((unsigned char const*)str, len); |
|
40 CxHashKey obj_key = cx_hash_key(str, len); |
|
41 CxHashKey cxstr_key = cx_hash_key_cxstr(cx_str(str)); |
|
42 |
|
43 CX_TEST_DO { |
|
44 CX_TEST_ASSERT(str_key.hash == bytes_key.hash); |
|
45 CX_TEST_ASSERT(obj_key.hash == bytes_key.hash); |
|
46 CX_TEST_ASSERT(cxstr_key.hash == bytes_key.hash); |
|
47 CX_TEST_ASSERT(str_key.len == len); |
|
48 CX_TEST_ASSERT(cxstr_key.len == len); |
|
49 CX_TEST_ASSERT(bytes_key.len == len); |
|
50 CX_TEST_ASSERT(bytes_key.len == len); |
|
51 CX_TEST_ASSERT(str_key.data == str); |
|
52 } |
|
53 } |
|
54 |
|
55 CX_TEST(test_hash_key_empty_string) { |
|
56 char const* str = ""; |
|
57 |
|
58 CxHashKey str_key = cx_hash_key_str(str); |
|
59 CxHashKey bytes_key = cx_hash_key_bytes((unsigned char const*) str, 0); |
|
60 CxHashKey obj_key = cx_hash_key(str, 0); |
|
61 |
|
62 CX_TEST_DO { |
|
63 CX_TEST_ASSERT(bytes_key.hash == 4152238450u); |
|
64 CX_TEST_ASSERT(str_key.hash == 4152238450u); |
|
65 CX_TEST_ASSERT(obj_key.hash == 4152238450u); |
|
66 CX_TEST_ASSERT(str_key.len == 0); |
|
67 CX_TEST_ASSERT(bytes_key.len == 0); |
|
68 CX_TEST_ASSERT(bytes_key.len == 0); |
|
69 CX_TEST_ASSERT(str_key.data == str); |
|
70 } |
|
71 } |
|
72 |
|
73 CX_TEST(test_hash_key_null) { |
|
74 CxHashKey str_key = cx_hash_key_str(NULL); |
|
75 CxHashKey bytes_key = cx_hash_key_bytes(NULL, 0); |
|
76 CxHashKey obj_key = cx_hash_key(NULL, 0); |
|
77 |
|
78 CX_TEST_DO { |
|
79 CX_TEST_ASSERT(bytes_key.hash == 1574210520u); |
|
80 CX_TEST_ASSERT(str_key.hash == 1574210520u); |
|
81 CX_TEST_ASSERT(obj_key.hash == 1574210520u); |
|
82 CX_TEST_ASSERT(str_key.len == 0); |
|
83 CX_TEST_ASSERT(bytes_key.len == 0); |
|
84 CX_TEST_ASSERT(bytes_key.len == 0); |
|
85 CX_TEST_ASSERT(str_key.data == NULL); |
|
86 } |
|
87 } |
|
88 |
|
89 CxTestSuite *cx_test_suite_hash_key(void) { |
|
90 CxTestSuite *suite = cx_test_suite_new("hash_key"); |
|
91 |
|
92 cx_test_register(suite, test_hash_key_functions); |
|
93 cx_test_register(suite, test_hash_key_empty_string); |
|
94 cx_test_register(suite, test_hash_key_null); |
|
95 |
|
96 return suite; |
|
97 } |