tests/test_hash_key.c

Fri, 20 Dec 2024 21:09:20 +0100

author
Mike Becker <universe@uap-core.de>
date
Fri, 20 Dec 2024 21:09:20 +0100
changeset 1037
83620ba72cc1
parent 890
54565fd74e74
permissions
-rw-r--r--

generify cxJsonObjGet() - fixes #525

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

mercurial