2
0

testhash.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "testutil.h"
  17. #include "fspr.h"
  18. #include "fspr_strings.h"
  19. #include "fspr_general.h"
  20. #include "fspr_pools.h"
  21. #include "fspr_hash.h"
  22. static void dump_hash(fspr_pool_t *p, fspr_hash_t *h, char *str)
  23. {
  24. fspr_hash_index_t *hi;
  25. char *val, *key;
  26. fspr_ssize_t len;
  27. int i = 0;
  28. str[0] = '\0';
  29. for (hi = fspr_hash_first(p, h); hi; hi = fspr_hash_next(hi)) {
  30. fspr_hash_this(hi,(void*) &key, &len, (void*) &val);
  31. fspr_snprintf(str, 8196, "%sKey %s (%" APR_SSIZE_T_FMT ") Value %s\n",
  32. str, key, len, val);
  33. i++;
  34. }
  35. fspr_snprintf(str, 8196, "%s#entries %d\n", str, i);
  36. }
  37. static void sum_hash(fspr_pool_t *p, fspr_hash_t *h, int *pcount, int *keySum, int *valSum)
  38. {
  39. fspr_hash_index_t *hi;
  40. void *val, *key;
  41. int count = 0;
  42. *keySum = 0;
  43. *valSum = 0;
  44. *pcount = 0;
  45. for (hi = fspr_hash_first(p, h); hi; hi = fspr_hash_next(hi)) {
  46. fspr_hash_this(hi, (void*)&key, NULL, &val);
  47. *valSum += *(int *)val;
  48. *keySum += *(int *)key;
  49. count++;
  50. }
  51. *pcount=count;
  52. }
  53. static void hash_make(abts_case *tc, void *data)
  54. {
  55. fspr_hash_t *h = NULL;
  56. h = fspr_hash_make(p);
  57. ABTS_PTR_NOTNULL(tc, h);
  58. }
  59. static void hash_set(abts_case *tc, void *data)
  60. {
  61. fspr_hash_t *h = NULL;
  62. char *result = NULL;
  63. h = fspr_hash_make(p);
  64. ABTS_PTR_NOTNULL(tc, h);
  65. fspr_hash_set(h, "key", APR_HASH_KEY_STRING, "value");
  66. result = fspr_hash_get(h, "key", APR_HASH_KEY_STRING);
  67. ABTS_STR_EQUAL(tc, "value", result);
  68. }
  69. static void hash_reset(abts_case *tc, void *data)
  70. {
  71. fspr_hash_t *h = NULL;
  72. char *result = NULL;
  73. h = fspr_hash_make(p);
  74. ABTS_PTR_NOTNULL(tc, h);
  75. fspr_hash_set(h, "key", APR_HASH_KEY_STRING, "value");
  76. result = fspr_hash_get(h, "key", APR_HASH_KEY_STRING);
  77. ABTS_STR_EQUAL(tc, "value", result);
  78. fspr_hash_set(h, "key", APR_HASH_KEY_STRING, "new");
  79. result = fspr_hash_get(h, "key", APR_HASH_KEY_STRING);
  80. ABTS_STR_EQUAL(tc, "new", result);
  81. }
  82. static void same_value(abts_case *tc, void *data)
  83. {
  84. fspr_hash_t *h = NULL;
  85. char *result = NULL;
  86. h = fspr_hash_make(p);
  87. ABTS_PTR_NOTNULL(tc, h);
  88. fspr_hash_set(h, "same1", APR_HASH_KEY_STRING, "same");
  89. result = fspr_hash_get(h, "same1", APR_HASH_KEY_STRING);
  90. ABTS_STR_EQUAL(tc, "same", result);
  91. fspr_hash_set(h, "same2", APR_HASH_KEY_STRING, "same");
  92. result = fspr_hash_get(h, "same2", APR_HASH_KEY_STRING);
  93. ABTS_STR_EQUAL(tc, "same", result);
  94. }
  95. static unsigned int hash_custom( const char *key, fspr_ssize_t *klen)
  96. {
  97. unsigned int hash = 0;
  98. while( *klen ) {
  99. (*klen) --;
  100. hash = hash * 33 + key[ *klen ];
  101. }
  102. return hash;
  103. }
  104. static void same_value_custom(abts_case *tc, void *data)
  105. {
  106. fspr_hash_t *h = NULL;
  107. char *result = NULL;
  108. h = fspr_hash_make_custom(p, hash_custom);
  109. ABTS_PTR_NOTNULL(tc, h);
  110. fspr_hash_set(h, "same1", 5, "same");
  111. result = fspr_hash_get(h, "same1", 5);
  112. ABTS_STR_EQUAL(tc, "same", result);
  113. fspr_hash_set(h, "same2", 5, "same");
  114. result = fspr_hash_get(h, "same2", 5);
  115. ABTS_STR_EQUAL(tc, "same", result);
  116. }
  117. static void key_space(abts_case *tc, void *data)
  118. {
  119. fspr_hash_t *h = NULL;
  120. char *result = NULL;
  121. h = fspr_hash_make(p);
  122. ABTS_PTR_NOTNULL(tc, h);
  123. fspr_hash_set(h, "key with space", APR_HASH_KEY_STRING, "value");
  124. result = fspr_hash_get(h, "key with space", APR_HASH_KEY_STRING);
  125. ABTS_STR_EQUAL(tc, "value", result);
  126. }
  127. /* This is kind of a hack, but I am just keeping an existing test. This is
  128. * really testing fspr_hash_first, fspr_hash_next, and fspr_hash_this which
  129. * should be tested in three separate tests, but this will do for now.
  130. */
  131. static void hash_traverse(abts_case *tc, void *data)
  132. {
  133. fspr_hash_t *h;
  134. char str[8196];
  135. h = fspr_hash_make(p);
  136. ABTS_PTR_NOTNULL(tc, h);
  137. fspr_hash_set(h, "OVERWRITE", APR_HASH_KEY_STRING, "should not see this");
  138. fspr_hash_set(h, "FOO3", APR_HASH_KEY_STRING, "bar3");
  139. fspr_hash_set(h, "FOO3", APR_HASH_KEY_STRING, "bar3");
  140. fspr_hash_set(h, "FOO1", APR_HASH_KEY_STRING, "bar1");
  141. fspr_hash_set(h, "FOO2", APR_HASH_KEY_STRING, "bar2");
  142. fspr_hash_set(h, "FOO4", APR_HASH_KEY_STRING, "bar4");
  143. fspr_hash_set(h, "SAME1", APR_HASH_KEY_STRING, "same");
  144. fspr_hash_set(h, "SAME2", APR_HASH_KEY_STRING, "same");
  145. fspr_hash_set(h, "OVERWRITE", APR_HASH_KEY_STRING, "Overwrite key");
  146. dump_hash(p, h, str);
  147. ABTS_STR_EQUAL(tc, "Key FOO1 (4) Value bar1\n"
  148. "Key FOO2 (4) Value bar2\n"
  149. "Key OVERWRITE (9) Value Overwrite key\n"
  150. "Key FOO3 (4) Value bar3\n"
  151. "Key SAME1 (5) Value same\n"
  152. "Key FOO4 (4) Value bar4\n"
  153. "Key SAME2 (5) Value same\n"
  154. "#entries 7\n", str);
  155. }
  156. /* This is kind of a hack, but I am just keeping an existing test. This is
  157. * really testing fspr_hash_first, fspr_hash_next, and fspr_hash_this which
  158. * should be tested in three separate tests, but this will do for now.
  159. */
  160. static void summation_test(abts_case *tc, void *data)
  161. {
  162. fspr_hash_t *h;
  163. int sumKeys, sumVal, trySumKey, trySumVal;
  164. int i, j, *val, *key;
  165. h =fspr_hash_make(p);
  166. ABTS_PTR_NOTNULL(tc, h);
  167. sumKeys = 0;
  168. sumVal = 0;
  169. trySumKey = 0;
  170. trySumVal = 0;
  171. for (i = 0; i < 100; i++) {
  172. j = i * 10 + 1;
  173. sumKeys += j;
  174. sumVal += i;
  175. key = fspr_palloc(p, sizeof(int));
  176. *key = j;
  177. val = fspr_palloc(p, sizeof(int));
  178. *val = i;
  179. fspr_hash_set(h, key, sizeof(int), val);
  180. }
  181. sum_hash(p, h, &i, &trySumKey, &trySumVal);
  182. ABTS_INT_EQUAL(tc, 100, i);
  183. ABTS_INT_EQUAL(tc, sumVal, trySumVal);
  184. ABTS_INT_EQUAL(tc, sumKeys, trySumKey);
  185. }
  186. static void delete_key(abts_case *tc, void *data)
  187. {
  188. fspr_hash_t *h = NULL;
  189. char *result = NULL;
  190. h = fspr_hash_make(p);
  191. ABTS_PTR_NOTNULL(tc, h);
  192. fspr_hash_set(h, "key", APR_HASH_KEY_STRING, "value");
  193. fspr_hash_set(h, "key2", APR_HASH_KEY_STRING, "value2");
  194. result = fspr_hash_get(h, "key", APR_HASH_KEY_STRING);
  195. ABTS_STR_EQUAL(tc, "value", result);
  196. result = fspr_hash_get(h, "key2", APR_HASH_KEY_STRING);
  197. ABTS_STR_EQUAL(tc, "value2", result);
  198. fspr_hash_set(h, "key", APR_HASH_KEY_STRING, NULL);
  199. result = fspr_hash_get(h, "key", APR_HASH_KEY_STRING);
  200. ABTS_PTR_EQUAL(tc, NULL, result);
  201. result = fspr_hash_get(h, "key2", APR_HASH_KEY_STRING);
  202. ABTS_STR_EQUAL(tc, "value2", result);
  203. }
  204. static void hash_count_0(abts_case *tc, void *data)
  205. {
  206. fspr_hash_t *h = NULL;
  207. int count;
  208. h = fspr_hash_make(p);
  209. ABTS_PTR_NOTNULL(tc, h);
  210. count = fspr_hash_count(h);
  211. ABTS_INT_EQUAL(tc, 0, count);
  212. }
  213. static void hash_count_1(abts_case *tc, void *data)
  214. {
  215. fspr_hash_t *h = NULL;
  216. int count;
  217. h = fspr_hash_make(p);
  218. ABTS_PTR_NOTNULL(tc, h);
  219. fspr_hash_set(h, "key", APR_HASH_KEY_STRING, "value");
  220. count = fspr_hash_count(h);
  221. ABTS_INT_EQUAL(tc, 1, count);
  222. }
  223. static void hash_count_5(abts_case *tc, void *data)
  224. {
  225. fspr_hash_t *h = NULL;
  226. int count;
  227. h = fspr_hash_make(p);
  228. ABTS_PTR_NOTNULL(tc, h);
  229. fspr_hash_set(h, "key1", APR_HASH_KEY_STRING, "value1");
  230. fspr_hash_set(h, "key2", APR_HASH_KEY_STRING, "value2");
  231. fspr_hash_set(h, "key3", APR_HASH_KEY_STRING, "value3");
  232. fspr_hash_set(h, "key4", APR_HASH_KEY_STRING, "value4");
  233. fspr_hash_set(h, "key5", APR_HASH_KEY_STRING, "value5");
  234. count = fspr_hash_count(h);
  235. ABTS_INT_EQUAL(tc, 5, count);
  236. }
  237. static void overlay_empty(abts_case *tc, void *data)
  238. {
  239. fspr_hash_t *base = NULL;
  240. fspr_hash_t *overlay = NULL;
  241. fspr_hash_t *result = NULL;
  242. int count;
  243. char str[8196];
  244. base = fspr_hash_make(p);
  245. overlay = fspr_hash_make(p);
  246. ABTS_PTR_NOTNULL(tc, base);
  247. ABTS_PTR_NOTNULL(tc, overlay);
  248. fspr_hash_set(base, "key1", APR_HASH_KEY_STRING, "value1");
  249. fspr_hash_set(base, "key2", APR_HASH_KEY_STRING, "value2");
  250. fspr_hash_set(base, "key3", APR_HASH_KEY_STRING, "value3");
  251. fspr_hash_set(base, "key4", APR_HASH_KEY_STRING, "value4");
  252. fspr_hash_set(base, "key5", APR_HASH_KEY_STRING, "value5");
  253. result = fspr_hash_overlay(p, overlay, base);
  254. count = fspr_hash_count(result);
  255. ABTS_INT_EQUAL(tc, 5, count);
  256. dump_hash(p, result, str);
  257. ABTS_STR_EQUAL(tc, "Key key1 (4) Value value1\n"
  258. "Key key2 (4) Value value2\n"
  259. "Key key3 (4) Value value3\n"
  260. "Key key4 (4) Value value4\n"
  261. "Key key5 (4) Value value5\n"
  262. "#entries 5\n", str);
  263. }
  264. static void overlay_2unique(abts_case *tc, void *data)
  265. {
  266. fspr_hash_t *base = NULL;
  267. fspr_hash_t *overlay = NULL;
  268. fspr_hash_t *result = NULL;
  269. int count;
  270. char str[8196];
  271. base = fspr_hash_make(p);
  272. overlay = fspr_hash_make(p);
  273. ABTS_PTR_NOTNULL(tc, base);
  274. ABTS_PTR_NOTNULL(tc, overlay);
  275. fspr_hash_set(base, "base1", APR_HASH_KEY_STRING, "value1");
  276. fspr_hash_set(base, "base2", APR_HASH_KEY_STRING, "value2");
  277. fspr_hash_set(base, "base3", APR_HASH_KEY_STRING, "value3");
  278. fspr_hash_set(base, "base4", APR_HASH_KEY_STRING, "value4");
  279. fspr_hash_set(base, "base5", APR_HASH_KEY_STRING, "value5");
  280. fspr_hash_set(overlay, "overlay1", APR_HASH_KEY_STRING, "value1");
  281. fspr_hash_set(overlay, "overlay2", APR_HASH_KEY_STRING, "value2");
  282. fspr_hash_set(overlay, "overlay3", APR_HASH_KEY_STRING, "value3");
  283. fspr_hash_set(overlay, "overlay4", APR_HASH_KEY_STRING, "value4");
  284. fspr_hash_set(overlay, "overlay5", APR_HASH_KEY_STRING, "value5");
  285. result = fspr_hash_overlay(p, overlay, base);
  286. count = fspr_hash_count(result);
  287. ABTS_INT_EQUAL(tc, 10, count);
  288. dump_hash(p, result, str);
  289. /* I don't know why these are out of order, but they are. I would probably
  290. * consider this a bug, but others should comment.
  291. */
  292. ABTS_STR_EQUAL(tc, "Key base5 (5) Value value5\n"
  293. "Key overlay1 (8) Value value1\n"
  294. "Key overlay2 (8) Value value2\n"
  295. "Key overlay3 (8) Value value3\n"
  296. "Key overlay4 (8) Value value4\n"
  297. "Key overlay5 (8) Value value5\n"
  298. "Key base1 (5) Value value1\n"
  299. "Key base2 (5) Value value2\n"
  300. "Key base3 (5) Value value3\n"
  301. "Key base4 (5) Value value4\n"
  302. "#entries 10\n", str);
  303. }
  304. static void overlay_same(abts_case *tc, void *data)
  305. {
  306. fspr_hash_t *base = NULL;
  307. fspr_hash_t *result = NULL;
  308. int count;
  309. char str[8196];
  310. base = fspr_hash_make(p);
  311. ABTS_PTR_NOTNULL(tc, base);
  312. fspr_hash_set(base, "base1", APR_HASH_KEY_STRING, "value1");
  313. fspr_hash_set(base, "base2", APR_HASH_KEY_STRING, "value2");
  314. fspr_hash_set(base, "base3", APR_HASH_KEY_STRING, "value3");
  315. fspr_hash_set(base, "base4", APR_HASH_KEY_STRING, "value4");
  316. fspr_hash_set(base, "base5", APR_HASH_KEY_STRING, "value5");
  317. result = fspr_hash_overlay(p, base, base);
  318. count = fspr_hash_count(result);
  319. ABTS_INT_EQUAL(tc, 5, count);
  320. dump_hash(p, result, str);
  321. /* I don't know why these are out of order, but they are. I would probably
  322. * consider this a bug, but others should comment.
  323. */
  324. ABTS_STR_EQUAL(tc, "Key base5 (5) Value value5\n"
  325. "Key base1 (5) Value value1\n"
  326. "Key base2 (5) Value value2\n"
  327. "Key base3 (5) Value value3\n"
  328. "Key base4 (5) Value value4\n"
  329. "#entries 5\n", str);
  330. }
  331. abts_suite *testhash(abts_suite *suite)
  332. {
  333. suite = ADD_SUITE(suite)
  334. abts_run_test(suite, hash_make, NULL);
  335. abts_run_test(suite, hash_set, NULL);
  336. abts_run_test(suite, hash_reset, NULL);
  337. abts_run_test(suite, same_value, NULL);
  338. abts_run_test(suite, same_value_custom, NULL);
  339. abts_run_test(suite, key_space, NULL);
  340. abts_run_test(suite, delete_key, NULL);
  341. abts_run_test(suite, hash_count_0, NULL);
  342. abts_run_test(suite, hash_count_1, NULL);
  343. abts_run_test(suite, hash_count_5, NULL);
  344. abts_run_test(suite, hash_traverse, NULL);
  345. abts_run_test(suite, summation_test, NULL);
  346. abts_run_test(suite, overlay_empty, NULL);
  347. abts_run_test(suite, overlay_2unique, NULL);
  348. abts_run_test(suite, overlay_same, NULL);
  349. return suite;
  350. }