1 #include "iowow.h"
2 #include "iwcfg.h"
3 #include "iwhmap.h"
4 #include <CUnit/Basic.h>
5
init_suite(void)6 static int init_suite(void) {
7 return iw_init();
8 }
9
clean_suite(void)10 static int clean_suite(void) {
11 return 0;
12 }
13
hex32(uint32_t * hash,char * buf)14 static void hex32(uint32_t *hash, char *buf) {
15 sprintf(buf, "%08x", *hash);
16 }
17
hex128(uint32_t hash[4],char * buf)18 static void hex128(uint32_t hash[4], char *buf) {
19 sprintf(buf, "%08x%08x%08x%08x", hash[0], hash[1], hash[2], hash[3]);
20 }
21
22 void murmur3_x86_32(const void *key, size_t len, uint32_t seed, void *out);
23 void murmur3_x86_128(const void *key, const size_t len, uint32_t seed, void *out);
24 void murmur3_x64_128(const void *key, const size_t len, const uint32_t seed, void *out);
25
test_murmur_hash(void)26 static void test_murmur_hash(void) {
27
28 #define TESTHASH(arch, nbytes, seed, str, expected) { \
29 char *input = str; \
30 uint32_t hash[4]; \
31 char buf[33]; \
32 murmur3_##arch##_##nbytes(input, strlen(input), (seed), hash); \
33 hex##nbytes(hash, buf); \
34 CU_ASSERT_STRING_EQUAL(buf, expected) \
35 }
36
37 TESTHASH(x86, 32, 1234, "Hello, world!", "faf6cdb3");
38 TESTHASH(x86, 32, 4321, "Hello, world!", "bf505788");
39 TESTHASH(x86, 32, 1234, "xxxxxxxxxxxxxxxxxxxxxxxxxxxx", "8905ac28");
40 TESTHASH(x86, 32, 1234, "", "0f2cc00b");
41
42 TESTHASH(x86, 128, 123, "Hello, world!", "61c9129e5a1aacd7a41621629e37c886");
43 TESTHASH(x86, 128, 321, "Hello, world!", "d5fbdcb3c26c4193045880c5a7170f0f");
44 TESTHASH(x86, 128, 123, "xxxxxxxxxxxxxxxxxxxxxxxxxxxx", "5e40bab278825a164cf929d31fec6047");
45 TESTHASH(x86, 128, 123, "", "fedc524526f3e79926f3e79926f3e799");
46
47 TESTHASH(x64, 128, 123, "Hello, world!", "8743acad421c8c73d373c3f5f19732fd");
48 TESTHASH(x64, 128, 321, "Hello, world!", "f86d4004ca47f42bb9546c7979200aee");
49 TESTHASH(x64, 128, 123, "xxxxxxxxxxxxxxxxxxxxxxxxxxxx", "becf7e04dbcf74637751664ef66e73e0");
50 TESTHASH(x64, 128, 123, "", "4cd9597081679d1abd92f8784bace33d");
51 }
52
test_basic_crud_str(void)53 static void test_basic_crud_str(void) {
54 char kbuf[64];
55 char vbuf[64];
56 IWHMAP *hm = iwhmap_create_str(iwhmap_kv_free);
57 CU_ASSERT_PTR_NOT_NULL_FATAL(hm);
58 for (int i = 0; i < 10000; ++i) {
59 snprintf(kbuf, sizeof(kbuf), "key%d", i);
60 snprintf(vbuf, sizeof(vbuf), "value%d", i);
61 iwrc rc = iwhmap_put(hm, strdup(kbuf), strdup(vbuf));
62 CU_ASSERT_EQUAL_FATAL(rc, 0);
63 }
64 for (int i = 0; i < 10000; ++i) {
65 snprintf(kbuf, sizeof(kbuf), "key%d", i);
66 snprintf(vbuf, sizeof(vbuf), "value%d", i);
67 const char *vp = iwhmap_get(hm, kbuf);
68 CU_ASSERT_PTR_NOT_NULL_FATAL(vp);
69 CU_ASSERT_STRING_EQUAL(vbuf, vp);
70 if (i % 2 == 0) {
71 iwhmap_remove(hm, kbuf);
72 }
73 }
74 CU_ASSERT_EQUAL(iwhmap_count(hm), 5000);
75 for (int i = 0; i < 10000; ++i) {
76 if ((i % 2) == 0) {
77 continue;
78 }
79 snprintf(kbuf, sizeof(kbuf), "key%d", i);
80 snprintf(vbuf, sizeof(vbuf), "value%d", i);
81 const char *vp = iwhmap_get(hm, kbuf);
82 CU_ASSERT_PTR_NOT_NULL_FATAL(vp);
83 CU_ASSERT_STRING_EQUAL(vbuf, vp);
84 if (i % 3 == 0) {
85 iwhmap_remove(hm, kbuf);
86 }
87 }
88 CU_ASSERT_EQUAL(iwhmap_count(hm), 3333);
89
90 // todo: finish tests
91
92 iwhmap_destroy(hm);
93 }
94
main()95 int main() {
96 CU_pSuite pSuite = NULL;
97
98 /* Initialize the CUnit test registry */
99 if (CUE_SUCCESS != CU_initialize_registry())
100 return CU_get_error();
101
102 /* Add a suite to the registry */
103 pSuite = CU_add_suite("iwhmap_test1", init_suite, clean_suite);
104
105 if (NULL == pSuite) {
106 CU_cleanup_registry();
107 return CU_get_error();
108 }
109
110 /* Add the tests to the suite */
111 if (
112 (NULL == CU_add_test(pSuite, "test_murmur_hash", test_murmur_hash))
113 || (NULL == CU_add_test(pSuite, "test_basic_crud_str", test_basic_crud_str))
114 ) {
115 CU_cleanup_registry();
116 return CU_get_error();
117 }
118
119 /* Run all tests using the CUnit Basic interface */
120 CU_basic_set_mode(CU_BRM_VERBOSE);
121 CU_basic_run_tests();
122 int ret = CU_get_error() || CU_get_number_of_failures();
123 CU_cleanup_registry();
124 return ret;
125 }
126