tests/test_hash_key.c

changeset 1402
6fa42f7e2624
parent 890
54565fd74e74
equal deleted inserted replaced
1401:a76249f50237 1402:6fa42f7e2624
39 CxHashKey bytes_key = cx_hash_key_bytes((const unsigned char*)str, len); 39 CxHashKey bytes_key = cx_hash_key_bytes((const unsigned char*)str, len);
40 CxHashKey obj_key = cx_hash_key(str, len); 40 CxHashKey obj_key = cx_hash_key(str, len);
41 CxHashKey cxstr_key = cx_hash_key_cxstr(cx_str(str)); 41 CxHashKey cxstr_key = cx_hash_key_cxstr(cx_str(str));
42 42
43 CX_TEST_DO { 43 CX_TEST_DO {
44 CX_TEST_ASSERT(bytes_key.hash == 1269566022);
44 CX_TEST_ASSERT(str_key.hash == bytes_key.hash); 45 CX_TEST_ASSERT(str_key.hash == bytes_key.hash);
45 CX_TEST_ASSERT(obj_key.hash == bytes_key.hash); 46 CX_TEST_ASSERT(obj_key.hash == bytes_key.hash);
46 CX_TEST_ASSERT(cxstr_key.hash == bytes_key.hash); 47 CX_TEST_ASSERT(cxstr_key.hash == bytes_key.hash);
47 CX_TEST_ASSERT(str_key.len == len); 48 CX_TEST_ASSERT(str_key.len == len);
49 CX_TEST_ASSERT(str_key.data == str);
48 CX_TEST_ASSERT(cxstr_key.len == len); 50 CX_TEST_ASSERT(cxstr_key.len == len);
51 CX_TEST_ASSERT(cxstr_key.data == str);
52 CX_TEST_ASSERT(obj_key.len == len);
53 CX_TEST_ASSERT(obj_key.data == str);
49 CX_TEST_ASSERT(bytes_key.len == len); 54 CX_TEST_ASSERT(bytes_key.len == len);
55 CX_TEST_ASSERT(bytes_key.data == str);
56 }
57 }
58
59 CX_TEST(test_hash_key_int_functions) {
60 uint32_t a = 0xabc01337u;
61 uint64_t b = 0xabc0133747110815ull;
62
63 CxHashKey ak = cx_hash_key_u32(a);
64 CxHashKey bk = cx_hash_key_u64(b);
65
66 CX_TEST_DO {
67 CX_TEST_ASSERT(ak.data == NULL);
68 CX_TEST_ASSERT(ak.len == 0);
69 CX_TEST_ASSERT(ak.hash == 3897006249);
70 CX_TEST_ASSERT(bk.data == NULL);
71 CX_TEST_ASSERT(bk.len == 0);
72 CX_TEST_ASSERT(bk.hash == 17452435587688253422ull);
73 }
74 }
75
76 CX_TEST(test_hash_key_macro) {
77 const char *str = "my key";
78 size_t len = strlen(str);
79 uint32_t a = 0xabc01337u;
80 uint64_t b = 0xabc0133747110815ull;
81
82 CxHashKey str_key = CX_HASH_KEY(str);
83 CxHashKey bytes_key = CX_HASH_KEY((const unsigned char*)str);
84 CxHashKey mutstr_key = CX_HASH_KEY(cx_mutstr((char*)str));
85 CxHashKey cxstr_key = CX_HASH_KEY(cx_str(str));
86 CxHashKey ak = CX_HASH_KEY(a);
87 CxHashKey bk = CX_HASH_KEY(b);
88
89 CX_TEST_DO {
90 CX_TEST_ASSERT(bytes_key.hash == 1269566022);
91 CX_TEST_ASSERT(str_key.hash == bytes_key.hash);
92 CX_TEST_ASSERT(cxstr_key.hash == bytes_key.hash);
93 CX_TEST_ASSERT(mutstr_key.hash == bytes_key.hash);
94 CX_TEST_ASSERT(str_key.len == len);
95 CX_TEST_ASSERT(str_key.data == str);
96 CX_TEST_ASSERT(cxstr_key.len == len);
97 CX_TEST_ASSERT(cxstr_key.data == str);
50 CX_TEST_ASSERT(bytes_key.len == len); 98 CX_TEST_ASSERT(bytes_key.len == len);
51 CX_TEST_ASSERT(str_key.data == str); 99 CX_TEST_ASSERT(bytes_key.data == str);
100 CX_TEST_ASSERT(ak.data == NULL);
101 CX_TEST_ASSERT(ak.len == 0);
102 CX_TEST_ASSERT(ak.hash == 3897006249);
103 CX_TEST_ASSERT(bk.data == NULL);
104 CX_TEST_ASSERT(bk.len == 0);
105 CX_TEST_ASSERT(bk.hash == 17452435587688253422ull);
52 } 106 }
53 } 107 }
54 108
55 CX_TEST(test_hash_key_empty_string) { 109 CX_TEST(test_hash_key_empty_string) {
56 const char *str = ""; 110 const char *str = "";
88 142
89 CxTestSuite *cx_test_suite_hash_key(void) { 143 CxTestSuite *cx_test_suite_hash_key(void) {
90 CxTestSuite *suite = cx_test_suite_new("hash_key"); 144 CxTestSuite *suite = cx_test_suite_new("hash_key");
91 145
92 cx_test_register(suite, test_hash_key_functions); 146 cx_test_register(suite, test_hash_key_functions);
147 cx_test_register(suite, test_hash_key_int_functions);
93 cx_test_register(suite, test_hash_key_empty_string); 148 cx_test_register(suite, test_hash_key_empty_string);
94 cx_test_register(suite, test_hash_key_null); 149 cx_test_register(suite, test_hash_key_null);
95 150
96 return suite; 151 return suite;
97 } 152 }

mercurial