2
0

unit1603.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curlcheck.h"
  25. #define ENABLE_CURLX_PRINTF
  26. #include "curlx.h"
  27. #include "hash.h"
  28. #include "memdebug.h" /* LAST include file */
  29. static struct Curl_hash hash_static;
  30. static const size_t slots = 3;
  31. static void mydtor(void *p)
  32. {
  33. /* Data are statically allocated */
  34. (void)p; /* unused */
  35. }
  36. static size_t elem_dtor_calls;
  37. static void my_elem_dtor(void *key, size_t key_len, void *p)
  38. {
  39. (void)p; /* unused */
  40. (void)key; /* unused */
  41. (void)key_len; /* unused */
  42. ++elem_dtor_calls;
  43. }
  44. static CURLcode unit_setup(void)
  45. {
  46. Curl_hash_init(&hash_static, slots, Curl_hash_str,
  47. Curl_str_key_compare, mydtor);
  48. return CURLE_OK;
  49. }
  50. static void unit_stop(void)
  51. {
  52. Curl_hash_destroy(&hash_static);
  53. }
  54. UNITTEST_START
  55. char key1[] = "key1";
  56. char key2[] = "key2b";
  57. char key3[] = "key3";
  58. char key4[] = "key4";
  59. char notakey[] = "notakey";
  60. char *nodep;
  61. int rc;
  62. /* Ensure the key hashes are as expected in order to test both hash
  63. collisions and a full table. Unfortunately, the hashes can vary
  64. between architectures. */
  65. if(Curl_hash_str(key1, strlen(key1), slots) != 1 ||
  66. Curl_hash_str(key2, strlen(key2), slots) != 0 ||
  67. Curl_hash_str(key3, strlen(key3), slots) != 2 ||
  68. Curl_hash_str(key4, strlen(key4), slots) != 1)
  69. fprintf(stderr, "Warning: hashes are not computed as expected on this "
  70. "architecture; test coverage will be less comprehensive\n");
  71. nodep = Curl_hash_add(&hash_static, &key1, strlen(key1), &key1);
  72. fail_unless(nodep, "insertion into hash failed");
  73. nodep = Curl_hash_pick(&hash_static, &key1, strlen(key1));
  74. fail_unless(nodep == key1, "hash retrieval failed");
  75. nodep = Curl_hash_add(&hash_static, &key2, strlen(key2), &key2);
  76. fail_unless(nodep, "insertion into hash failed");
  77. nodep = Curl_hash_pick(&hash_static, &key2, strlen(key2));
  78. fail_unless(nodep == key2, "hash retrieval failed");
  79. nodep = Curl_hash_add(&hash_static, &key3, strlen(key3), &key3);
  80. fail_unless(nodep, "insertion into hash failed");
  81. nodep = Curl_hash_pick(&hash_static, &key3, strlen(key3));
  82. fail_unless(nodep == key3, "hash retrieval failed");
  83. /* The fourth element exceeds the number of slots & collides */
  84. nodep = Curl_hash_add(&hash_static, &key4, strlen(key4), &key4);
  85. fail_unless(nodep, "insertion into hash failed");
  86. nodep = Curl_hash_pick(&hash_static, &key4, strlen(key4));
  87. fail_unless(nodep == key4, "hash retrieval failed");
  88. /* Make sure all elements are still accessible */
  89. nodep = Curl_hash_pick(&hash_static, &key1, strlen(key1));
  90. fail_unless(nodep == key1, "hash retrieval failed");
  91. nodep = Curl_hash_pick(&hash_static, &key2, strlen(key2));
  92. fail_unless(nodep == key2, "hash retrieval failed");
  93. nodep = Curl_hash_pick(&hash_static, &key3, strlen(key3));
  94. fail_unless(nodep == key3, "hash retrieval failed");
  95. nodep = Curl_hash_pick(&hash_static, &key4, strlen(key4));
  96. fail_unless(nodep == key4, "hash retrieval failed");
  97. /* Delete the second of two entries in a bucket */
  98. rc = Curl_hash_delete(&hash_static, &key4, strlen(key4));
  99. fail_unless(rc == 0, "hash delete failed");
  100. nodep = Curl_hash_pick(&hash_static, &key1, strlen(key1));
  101. fail_unless(nodep == key1, "hash retrieval failed");
  102. nodep = Curl_hash_pick(&hash_static, &key4, strlen(key4));
  103. fail_unless(!nodep, "hash retrieval should have failed");
  104. /* Insert that deleted node again */
  105. nodep = Curl_hash_add(&hash_static, &key4, strlen(key4), &key4);
  106. fail_unless(nodep, "insertion into hash failed");
  107. nodep = Curl_hash_pick(&hash_static, &key4, strlen(key4));
  108. fail_unless(nodep == key4, "hash retrieval failed");
  109. /* Delete the first of two entries in a bucket */
  110. rc = Curl_hash_delete(&hash_static, &key1, strlen(key1));
  111. fail_unless(rc == 0, "hash delete failed");
  112. nodep = Curl_hash_pick(&hash_static, &key1, strlen(key1));
  113. fail_unless(!nodep, "hash retrieval should have failed");
  114. nodep = Curl_hash_pick(&hash_static, &key4, strlen(key4));
  115. fail_unless(nodep == key4, "hash retrieval failed");
  116. /* Delete the remaining one of two entries in a bucket */
  117. rc = Curl_hash_delete(&hash_static, &key4, strlen(key4));
  118. fail_unless(rc == 0, "hash delete failed");
  119. nodep = Curl_hash_pick(&hash_static, &key1, strlen(key1));
  120. fail_unless(!nodep, "hash retrieval should have failed");
  121. nodep = Curl_hash_pick(&hash_static, &key4, strlen(key4));
  122. fail_unless(!nodep, "hash retrieval should have failed");
  123. /* Delete an already deleted node */
  124. rc = Curl_hash_delete(&hash_static, &key4, strlen(key4));
  125. fail_unless(rc, "hash delete should have failed");
  126. /* Replace an existing node */
  127. nodep = Curl_hash_add(&hash_static, &key1, strlen(key1), &notakey);
  128. fail_unless(nodep, "insertion into hash failed");
  129. nodep = Curl_hash_pick(&hash_static, &key1, strlen(key1));
  130. fail_unless(nodep == notakey, "hash retrieval failed");
  131. /* Make sure all remaining elements are still accessible */
  132. nodep = Curl_hash_pick(&hash_static, &key2, strlen(key2));
  133. fail_unless(nodep == key2, "hash retrieval failed");
  134. nodep = Curl_hash_pick(&hash_static, &key3, strlen(key3));
  135. fail_unless(nodep == key3, "hash retrieval failed");
  136. /* Add element with own destructor */
  137. nodep = Curl_hash_add2(&hash_static, &key1, strlen(key1), &key1,
  138. my_elem_dtor);
  139. fail_unless(nodep, "add2 insertion into hash failed");
  140. fail_unless(elem_dtor_calls == 0, "element destructor count should be 0");
  141. /* Add it again, should invoke destructor on first */
  142. nodep = Curl_hash_add2(&hash_static, &key1, strlen(key1), &key1,
  143. my_elem_dtor);
  144. fail_unless(nodep, "add2 again, insertion into hash failed");
  145. fail_unless(elem_dtor_calls == 1, "element destructor count should be 1");
  146. /* remove, should invoke destructor */
  147. rc = Curl_hash_delete(&hash_static, &key1, strlen(key1));
  148. fail_unless(rc == 0, "hash delete failed");
  149. fail_unless(elem_dtor_calls == 2, "element destructor count should be 1");
  150. /* Clean up */
  151. Curl_hash_clean(&hash_static);
  152. UNITTEST_STOP