testarghashtable.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*******************************************************************************
  2. * This file is part of the argtable3 library.
  3. *
  4. * Copyright (C) 2013-2019 Tom G. Huang
  5. * <tomghuang@gmail.com>
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * 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. * * Neither the name of STEWART HEITMANN nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT,
  23. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. ******************************************************************************/
  30. #include <string.h>
  31. #include <time.h>
  32. #include <stdio.h>
  33. #include "CuTest.h"
  34. #include "argtable3_private.h"
  35. #if defined(_MSC_VER)
  36. #pragma warning(push)
  37. #pragma warning(disable : 4204)
  38. #pragma warning(disable : 4996)
  39. #endif
  40. static unsigned int hash_key(const void* key) {
  41. char* str = (char*)key;
  42. int c;
  43. unsigned int hash = 5381;
  44. while ((c = *str++) != 0)
  45. hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
  46. return hash;
  47. }
  48. static int equal_keys(const void* key1, const void* key2) {
  49. char* k1 = (char*)key1;
  50. char* k2 = (char*)key2;
  51. return (0 == strcmp(k1, k2));
  52. }
  53. void test_arghashtable_basic_001(CuTest* tc) {
  54. arg_hashtable_t* h = arg_hashtable_create(32, hash_key, equal_keys);
  55. CuAssertTrue(tc, h != 0);
  56. CuAssertIntEquals(tc, arg_hashtable_count(h), 0);
  57. arg_hashtable_destroy(h, 1);
  58. }
  59. void test_arghashtable_basic_002(CuTest* tc) {
  60. arg_hashtable_t* h = arg_hashtable_create(32, hash_key, equal_keys);
  61. CuAssertTrue(tc, h != 0);
  62. CuAssertIntEquals(tc, arg_hashtable_count(h), 0);
  63. char* key_1 = "k1";
  64. char* k_1 = (char*)malloc(strlen(key_1) + 1);
  65. memset(k_1, 0, strlen(key_1) + 1);
  66. strncpy(k_1, key_1, strlen(key_1));
  67. char* value_1 = "v1";
  68. char* v_1 = (char*)malloc(strlen(value_1) + 1);
  69. memset(v_1, 0, strlen(value_1) + 1);
  70. strncpy(v_1, value_1, strlen(value_1));
  71. arg_hashtable_insert(h, k_1, v_1);
  72. CuAssertIntEquals(tc, 1, arg_hashtable_count(h));
  73. arg_hashtable_itr_t* itr = arg_hashtable_itr_create(h);
  74. CuAssertTrue(tc, itr != 0);
  75. CuAssertPtrEquals(tc, k_1, arg_hashtable_itr_key(itr));
  76. CuAssertTrue(tc, strcmp((char*)arg_hashtable_itr_key(itr), key_1) == 0);
  77. CuAssertPtrEquals(tc, v_1, arg_hashtable_itr_value(itr));
  78. CuAssertTrue(tc, strcmp((char*)arg_hashtable_itr_value(itr), value_1) == 0);
  79. arg_hashtable_itr_destroy(itr);
  80. arg_hashtable_destroy(h, 1);
  81. }
  82. void test_arghashtable_basic_003(CuTest* tc) {
  83. arg_hashtable_t* h = arg_hashtable_create(32, hash_key, equal_keys);
  84. CuAssertTrue(tc, h != 0);
  85. CuAssertIntEquals(tc, arg_hashtable_count(h), 0);
  86. char* key_1 = "k1";
  87. char* k_1 = (char*)malloc(strlen(key_1) + 1);
  88. memset(k_1, 0, strlen(key_1) + 1);
  89. strncpy(k_1, key_1, strlen(key_1));
  90. char* value_1 = "v1";
  91. char* v_1 = (char*)malloc(strlen(value_1) + 1);
  92. memset(v_1, 0, strlen(value_1) + 1);
  93. strncpy(v_1, value_1, strlen(value_1));
  94. arg_hashtable_insert(h, k_1, v_1);
  95. CuAssertIntEquals(tc, 1, arg_hashtable_count(h));
  96. char* key_2 = "k2";
  97. char* k_2 = (char*)malloc(strlen(key_2) + 1);
  98. memset(k_2, 0, strlen(key_2) + 1);
  99. strncpy(k_2, key_2, strlen(key_2));
  100. char* value_2 = "v2";
  101. char* v_2 = (char*)malloc(strlen(value_2) + 1);
  102. memset(v_2, 0, strlen(value_2) + 1);
  103. strncpy(v_2, value_2, strlen(value_2));
  104. arg_hashtable_insert(h, k_2, v_2);
  105. CuAssertIntEquals(tc, 2, arg_hashtable_count(h));
  106. arg_hashtable_itr_t* itr = arg_hashtable_itr_create(h);
  107. CuAssertTrue(tc, itr != 0);
  108. int ret = arg_hashtable_itr_advance(itr);
  109. CuAssertTrue(tc, ret != 0);
  110. ret = arg_hashtable_itr_advance(itr);
  111. CuAssertTrue(tc, ret == 0);
  112. arg_hashtable_itr_destroy(itr);
  113. arg_hashtable_destroy(h, 1);
  114. }
  115. void test_arghashtable_basic_004(CuTest* tc) {
  116. arg_hashtable_t* h = arg_hashtable_create(32, hash_key, equal_keys);
  117. CuAssertTrue(tc, h != 0);
  118. CuAssertIntEquals(tc, arg_hashtable_count(h), 0);
  119. char* key_1 = "k1";
  120. char* k_1 = (char*)malloc(strlen(key_1) + 1);
  121. memset(k_1, 0, strlen(key_1) + 1);
  122. strncpy(k_1, key_1, strlen(key_1));
  123. char* value_1 = "v1";
  124. char* v_1 = (char*)malloc(strlen(value_1) + 1);
  125. memset(v_1, 0, strlen(value_1) + 1);
  126. strncpy(v_1, value_1, strlen(value_1));
  127. arg_hashtable_insert(h, k_1, v_1);
  128. CuAssertTrue(tc, h != 0);
  129. CuAssertIntEquals(tc, 1, arg_hashtable_count(h));
  130. arg_hashtable_itr_t* itr = arg_hashtable_itr_create(h);
  131. int ret = arg_hashtable_itr_remove(itr);
  132. CuAssertTrue(tc, ret == 0);
  133. CuAssertIntEquals(tc, 0, arg_hashtable_count(h));
  134. arg_hashtable_itr_destroy(itr);
  135. arg_hashtable_destroy(h, 1);
  136. }
  137. void test_arghashtable_basic_005(CuTest* tc) {
  138. arg_hashtable_t* h = arg_hashtable_create(3, hash_key, equal_keys);
  139. CuAssertTrue(tc, h != 0);
  140. CuAssertIntEquals(tc, arg_hashtable_count(h), 0);
  141. char* key_1 = "k1";
  142. char* k_1 = (char*)malloc(strlen(key_1) + 1);
  143. memset(k_1, 0, strlen(key_1) + 1);
  144. strncpy(k_1, key_1, strlen(key_1));
  145. char* value_1 = "v1";
  146. char* v_1 = (char*)malloc(strlen(value_1) + 1);
  147. memset(v_1, 0, strlen(value_1) + 1);
  148. strncpy(v_1, value_1, strlen(value_1));
  149. arg_hashtable_insert(h, k_1, v_1);
  150. CuAssertTrue(tc, h != 0);
  151. CuAssertIntEquals(tc, 1, arg_hashtable_count(h));
  152. arg_hashtable_remove(h, k_1);
  153. CuAssertIntEquals(tc, 0, arg_hashtable_count(h));
  154. arg_hashtable_destroy(h, 1);
  155. }
  156. void test_arghashtable_basic_006(CuTest* tc) {
  157. arg_hashtable_t* h = arg_hashtable_create(32, hash_key, equal_keys);
  158. CuAssertTrue(tc, h != 0);
  159. CuAssertIntEquals(tc, arg_hashtable_count(h), 0);
  160. char* key_1 = "k1";
  161. char* k_1 = (char*)malloc(strlen(key_1) + 1);
  162. memset(k_1, 0, strlen(key_1) + 1);
  163. strncpy(k_1, key_1, strlen(key_1));
  164. char* value_1 = "v1";
  165. char* v_1 = (char*)malloc(strlen(value_1) + 1);
  166. memset(v_1, 0, strlen(value_1) + 1);
  167. strncpy(v_1, value_1, strlen(value_1));
  168. arg_hashtable_insert(h, k_1, v_1);
  169. CuAssertTrue(tc, arg_hashtable_count(h) == 1);
  170. char* vv = (char*)arg_hashtable_search(h, k_1);
  171. CuAssertTrue(tc, strcmp(vv, v_1) == 0);
  172. arg_hashtable_destroy(h, 1);
  173. }
  174. void test_arghashtable_basic_007(CuTest* tc) {
  175. arg_hashtable_t* h = arg_hashtable_create(32, hash_key, equal_keys);
  176. CuAssertTrue(tc, h != 0);
  177. CuAssertIntEquals(tc, arg_hashtable_count(h), 0);
  178. char* key_1 = "k1";
  179. char* k_1 = (char*)malloc(strlen(key_1) + 1);
  180. memset(k_1, 0, strlen(key_1) + 1);
  181. strncpy(k_1, key_1, strlen(key_1));
  182. char* value_1 = "v1";
  183. char* v_1 = (char*)malloc(strlen(value_1) + 1);
  184. memset(v_1, 0, strlen(value_1) + 1);
  185. strncpy(v_1, value_1, strlen(value_1));
  186. arg_hashtable_insert(h, k_1, v_1);
  187. CuAssertIntEquals(tc, 1, arg_hashtable_count(h));
  188. char* key_2 = "k2";
  189. char* k_2 = (char*)malloc(strlen(key_2) + 1);
  190. memset(k_2, 0, strlen(key_2) + 1);
  191. strncpy(k_2, key_2, strlen(key_2));
  192. char* value_2 = "v2";
  193. char* v_2 = (char*)malloc(strlen(value_2) + 1);
  194. memset(v_2, 0, strlen(value_2) + 1);
  195. strncpy(v_2, value_2, strlen(value_2));
  196. arg_hashtable_insert(h, k_2, v_2);
  197. CuAssertIntEquals(tc, 2, arg_hashtable_count(h));
  198. arg_hashtable_itr_t itr;
  199. int ret = arg_hashtable_itr_search(&itr, h, k_1);
  200. CuAssertTrue(tc, ret != 0);
  201. CuAssertPtrEquals(tc, k_1, arg_hashtable_itr_key(&itr));
  202. CuAssertPtrEquals(tc, v_1, arg_hashtable_itr_value(&itr));
  203. CuAssertTrue(tc, strcmp((char*)arg_hashtable_itr_key(&itr), k_1) == 0);
  204. CuAssertTrue(tc, strcmp((char*)arg_hashtable_itr_value(&itr), v_1) == 0);
  205. arg_hashtable_destroy(h, 1);
  206. }
  207. CuSuite* get_arghashtable_testsuite() {
  208. CuSuite* suite = CuSuiteNew();
  209. SUITE_ADD_TEST(suite, test_arghashtable_basic_001);
  210. SUITE_ADD_TEST(suite, test_arghashtable_basic_002);
  211. SUITE_ADD_TEST(suite, test_arghashtable_basic_003);
  212. SUITE_ADD_TEST(suite, test_arghashtable_basic_004);
  213. SUITE_ADD_TEST(suite, test_arghashtable_basic_005);
  214. SUITE_ADD_TEST(suite, test_arghashtable_basic_006);
  215. SUITE_ADD_TEST(suite, test_arghashtable_basic_007);
  216. return suite;
  217. }
  218. #if defined(_MSC_VER)
  219. #pragma warning(pop)
  220. #endif