• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define TESTHASH(arch, nbytes, seed, str, expected) {               \
28     char *input = str;                                              \
29     uint32_t hash[4];                                               \
30     char buf[33];                                                   \
31     murmur3_ ## arch ## _ ## nbytes(input, strlen(input), (seed), hash);  \
32     hex ## nbytes(hash, buf);                                         \
33     CU_ASSERT_STRING_EQUAL(buf, expected)                           \
34 }
35 
36   TESTHASH(x86, 32, 1234, "Hello, world!", "faf6cdb3");
37   TESTHASH(x86, 32, 4321, "Hello, world!", "bf505788");
38   TESTHASH(x86, 32, 1234, "xxxxxxxxxxxxxxxxxxxxxxxxxxxx", "8905ac28");
39   TESTHASH(x86, 32, 1234, "", "0f2cc00b");
40 
41   TESTHASH(x86, 128, 123, "Hello, world!", "61c9129e5a1aacd7a41621629e37c886");
42   TESTHASH(x86, 128, 321, "Hello, world!", "d5fbdcb3c26c4193045880c5a7170f0f");
43   TESTHASH(x86, 128, 123, "xxxxxxxxxxxxxxxxxxxxxxxxxxxx", "5e40bab278825a164cf929d31fec6047");
44   TESTHASH(x86, 128, 123, "", "fedc524526f3e79926f3e79926f3e799");
45 
46   TESTHASH(x64, 128, 123, "Hello, world!", "8743acad421c8c73d373c3f5f19732fd");
47   TESTHASH(x64, 128, 321, "Hello, world!", "f86d4004ca47f42bb9546c7979200aee");
48   TESTHASH(x64, 128, 123, "xxxxxxxxxxxxxxxxxxxxxxxxxxxx", "becf7e04dbcf74637751664ef66e73e0");
49   TESTHASH(x64, 128, 123, "", "4cd9597081679d1abd92f8784bace33d");
50 }
51 
test_basic_crud_str(void)52 static void test_basic_crud_str(void) {
53   char kbuf[64];
54   char vbuf[64];
55   IWHMAP *hm = iwhmap_create_str(iwhmap_kv_free);
56   CU_ASSERT_PTR_NOT_NULL_FATAL(hm);
57   for (int i = 0; i < 10000; ++i) {
58     snprintf(kbuf, sizeof(kbuf), "key%d", i);
59     snprintf(vbuf, sizeof(vbuf), "value%d", i);
60     iwrc rc = iwhmap_put(hm, strdup(kbuf), strdup(vbuf));
61     CU_ASSERT_EQUAL_FATAL(rc, 0);
62   }
63   for (int i = 0; i < 10000; ++i) {
64     snprintf(kbuf, sizeof(kbuf), "key%d", i);
65     snprintf(vbuf, sizeof(vbuf), "value%d", i);
66     const char *vp = iwhmap_get(hm, kbuf);
67     CU_ASSERT_PTR_NOT_NULL_FATAL(vp);
68     CU_ASSERT_STRING_EQUAL(vbuf, vp);
69     if (i % 2 == 0) {
70       iwhmap_remove(hm, kbuf);
71     }
72   }
73   CU_ASSERT_EQUAL(iwhmap_count(hm), 5000);
74   for (int i = 0; i < 10000; ++i) {
75     if ((i % 2) == 0) {
76       continue;
77     }
78     snprintf(kbuf, sizeof(kbuf), "key%d", i);
79     snprintf(vbuf, sizeof(vbuf), "value%d", i);
80     const char *vp = iwhmap_get(hm, kbuf);
81     CU_ASSERT_PTR_NOT_NULL_FATAL(vp);
82     CU_ASSERT_STRING_EQUAL(vbuf, vp);
83     if (i % 3 == 0) {
84       iwhmap_remove(hm, kbuf);
85     }
86   }
87   CU_ASSERT_EQUAL(iwhmap_count(hm), 3333);
88 
89   // TODO: finish tests
90   iwhmap_destroy(hm);
91 }
92 
test_lru1(void)93 static void test_lru1(void) {
94   IWHMAP *hm = iwhmap_create_u32(0);
95   CU_ASSERT_PTR_NOT_NULL_FATAL(hm);
96 
97   // Init LRU mode max 2 records in map
98   iwhmap_lru_init(hm, iwhmap_lru_eviction_max_count, (void*) (uintptr_t) 2UL);
99 
100   iwrc rc = iwhmap_put_u32(hm, 1, (void*) 1L);
101   CU_ASSERT_EQUAL_FATAL(rc, 0);
102   long val = (intptr_t) iwhmap_get_u64(hm, 1);
103   CU_ASSERT_EQUAL(val, 1L);
104 
105   rc -= iwhmap_put_u32(hm, 2, (void*) 2L);
106   CU_ASSERT_EQUAL_FATAL(rc, 0);
107   val = (intptr_t) iwhmap_get_u64(hm, 1);
108   CU_ASSERT_EQUAL(val, 1L);
109   val = (intptr_t) iwhmap_get_u64(hm, 2);
110   CU_ASSERT_EQUAL(val, 2L);
111 
112   rc = iwhmap_put_u32(hm, 3, (void*) 3L);
113   CU_ASSERT_EQUAL_FATAL(rc, 0);
114   val = (intptr_t) iwhmap_get_u64(hm, 1);
115   CU_ASSERT_EQUAL(val, 0L);
116   val = (intptr_t) iwhmap_get_u64(hm, 3);
117   CU_ASSERT_EQUAL(val, 3L);
118   val = (intptr_t) iwhmap_get_u64(hm, 2);
119   CU_ASSERT_EQUAL(val, 2L);
120 
121   rc = iwhmap_put_u32(hm, 4, (void*) 4L);
122   val = (intptr_t) iwhmap_get_u64(hm, 3);
123   CU_ASSERT_EQUAL(val, 0);
124   CU_ASSERT_EQUAL(iwhmap_count(hm), 2);
125 
126   iwhmap_destroy(hm);
127 }
128 
test_lru2(void)129 static void test_lru2(void) {
130   iwrc rc = 0;
131   IWHMAP *hm = iwhmap_create_u32(0);
132   CU_ASSERT_PTR_NOT_NULL_FATAL(hm);
133   iwhmap_lru_init(hm, iwhmap_lru_eviction_max_count, (void*) (uintptr_t) 1024UL);
134   for (int i = 0; i < 2048; ++i) {
135     rc = iwhmap_put_u32(hm, i + 1, (void*) (intptr_t) i);
136     CU_ASSERT_EQUAL_FATAL(rc, 0);
137   }
138   uint32_t val = iwhmap_count(hm);
139   CU_ASSERT_EQUAL(val, 1024);
140 
141   iwhmap_destroy(hm);
142 }
143 
main()144 int main() {
145   CU_pSuite pSuite = NULL;
146 
147   /* Initialize the CUnit test registry */
148   if (CUE_SUCCESS != CU_initialize_registry()) {
149     return CU_get_error();
150   }
151 
152   /* Add a suite to the registry */
153   pSuite = CU_add_suite("iwhmap_test1", init_suite, clean_suite);
154 
155   if (NULL == pSuite) {
156     CU_cleanup_registry();
157     return CU_get_error();
158   }
159 
160   /* Add the tests to the suite */
161   if (  (NULL == CU_add_test(pSuite, "test_murmur_hash", test_murmur_hash))
162      || (NULL == CU_add_test(pSuite, "test_basic_crud_str", test_basic_crud_str))
163      || (NULL == CU_add_test(pSuite, "test_lru1", test_lru1))
164      || (NULL == CU_add_test(pSuite, "test_lru2", test_lru2))) {
165     CU_cleanup_registry();
166     return CU_get_error();
167   }
168 
169   /* Run all tests using the CUnit Basic interface */
170   CU_basic_set_mode(CU_BRM_VERBOSE);
171   CU_basic_run_tests();
172   int ret = CU_get_error() || CU_get_number_of_failures();
173   CU_cleanup_registry();
174   return ret;
175 }
176