2
0

unit1603.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. #include "curlx.h"
  26. #include "hash.h"
  27. #include "memdebug.h" /* LAST include file */
  28. static struct Curl_hash hash_static;
  29. static const size_t slots = 3;
  30. static void mydtor(void *p)
  31. {
  32. /* Data are statically allocated */
  33. (void)p; /* unused */
  34. }
  35. static size_t elem_dtor_calls;
  36. static void my_elem_dtor(void *key, size_t key_len, void *p)
  37. {
  38. (void)p; /* unused */
  39. (void)key; /* unused */
  40. (void)key_len; /* unused */
  41. ++elem_dtor_calls;
  42. }
  43. static CURLcode unit_setup(void)
  44. {
  45. Curl_hash_init(&hash_static, slots, Curl_hash_str,
  46. Curl_str_key_compare, mydtor);
  47. return CURLE_OK;
  48. }
  49. static void unit_stop(void)
  50. {
  51. Curl_hash_destroy(&hash_static);
  52. }
  53. UNITTEST_START
  54. char key1[] = "key1";
  55. char key2[] = "key2b";
  56. char key3[] = "key3";
  57. char key4[] = "key4";
  58. char notakey[] = "notakey";
  59. char *nodep;
  60. int rc;
  61. /* Ensure the key hashes are as expected in order to test both hash
  62. collisions and a full table. Unfortunately, the hashes can vary
  63. between architectures. */
  64. if(Curl_hash_str(key1, strlen(key1), slots) != 1 ||
  65. Curl_hash_str(key2, strlen(key2), slots) != 0 ||
  66. Curl_hash_str(key3, strlen(key3), slots) != 2 ||
  67. Curl_hash_str(key4, strlen(key4), slots) != 1)
  68. fprintf(stderr, "Warning: hashes are not computed as expected on this "
  69. "architecture; test coverage will be less comprehensive\n");
  70. nodep = Curl_hash_add(&hash_static, &key1, strlen(key1), &key1);
  71. fail_unless(nodep, "insertion into hash failed");
  72. nodep = Curl_hash_pick(&hash_static, &key1, strlen(key1));
  73. fail_unless(nodep == key1, "hash retrieval failed");
  74. nodep = Curl_hash_add(&hash_static, &key2, strlen(key2), &key2);
  75. fail_unless(nodep, "insertion into hash failed");
  76. nodep = Curl_hash_pick(&hash_static, &key2, strlen(key2));
  77. fail_unless(nodep == key2, "hash retrieval failed");
  78. nodep = Curl_hash_add(&hash_static, &key3, strlen(key3), &key3);
  79. fail_unless(nodep, "insertion into hash failed");
  80. nodep = Curl_hash_pick(&hash_static, &key3, strlen(key3));
  81. fail_unless(nodep == key3, "hash retrieval failed");
  82. /* The fourth element exceeds the number of slots & collides */
  83. nodep = Curl_hash_add(&hash_static, &key4, strlen(key4), &key4);
  84. fail_unless(nodep, "insertion into hash failed");
  85. nodep = Curl_hash_pick(&hash_static, &key4, strlen(key4));
  86. fail_unless(nodep == key4, "hash retrieval failed");
  87. /* Make sure all elements are still accessible */
  88. nodep = Curl_hash_pick(&hash_static, &key1, strlen(key1));
  89. fail_unless(nodep == key1, "hash retrieval failed");
  90. nodep = Curl_hash_pick(&hash_static, &key2, strlen(key2));
  91. fail_unless(nodep == key2, "hash retrieval failed");
  92. nodep = Curl_hash_pick(&hash_static, &key3, strlen(key3));
  93. fail_unless(nodep == key3, "hash retrieval failed");
  94. nodep = Curl_hash_pick(&hash_static, &key4, strlen(key4));
  95. fail_unless(nodep == key4, "hash retrieval failed");
  96. /* Delete the second of two entries in a bucket */
  97. rc = Curl_hash_delete(&hash_static, &key4, strlen(key4));
  98. fail_unless(rc == 0, "hash delete failed");
  99. nodep = Curl_hash_pick(&hash_static, &key1, strlen(key1));
  100. fail_unless(nodep == key1, "hash retrieval failed");
  101. nodep = Curl_hash_pick(&hash_static, &key4, strlen(key4));
  102. fail_unless(!nodep, "hash retrieval should have failed");
  103. /* Insert that deleted node again */
  104. nodep = Curl_hash_add(&hash_static, &key4, strlen(key4), &key4);
  105. fail_unless(nodep, "insertion into hash failed");
  106. nodep = Curl_hash_pick(&hash_static, &key4, strlen(key4));
  107. fail_unless(nodep == key4, "hash retrieval failed");
  108. /* Delete the first of two entries in a bucket */
  109. rc = Curl_hash_delete(&hash_static, &key1, strlen(key1));
  110. fail_unless(rc == 0, "hash delete failed");
  111. nodep = Curl_hash_pick(&hash_static, &key1, strlen(key1));
  112. fail_unless(!nodep, "hash retrieval should have failed");
  113. nodep = Curl_hash_pick(&hash_static, &key4, strlen(key4));
  114. fail_unless(nodep == key4, "hash retrieval failed");
  115. /* Delete the remaining one of two entries in a bucket */
  116. rc = Curl_hash_delete(&hash_static, &key4, strlen(key4));
  117. fail_unless(rc == 0, "hash delete failed");
  118. nodep = Curl_hash_pick(&hash_static, &key1, strlen(key1));
  119. fail_unless(!nodep, "hash retrieval should have failed");
  120. nodep = Curl_hash_pick(&hash_static, &key4, strlen(key4));
  121. fail_unless(!nodep, "hash retrieval should have failed");
  122. /* Delete an already deleted node */
  123. rc = Curl_hash_delete(&hash_static, &key4, strlen(key4));
  124. fail_unless(rc, "hash delete should have failed");
  125. /* Replace an existing node */
  126. nodep = Curl_hash_add(&hash_static, &key1, strlen(key1), &notakey);
  127. fail_unless(nodep, "insertion into hash failed");
  128. nodep = Curl_hash_pick(&hash_static, &key1, strlen(key1));
  129. fail_unless(nodep == notakey, "hash retrieval failed");
  130. /* Make sure all remaining elements are still accessible */
  131. nodep = Curl_hash_pick(&hash_static, &key2, strlen(key2));
  132. fail_unless(nodep == key2, "hash retrieval failed");
  133. nodep = Curl_hash_pick(&hash_static, &key3, strlen(key3));
  134. fail_unless(nodep == key3, "hash retrieval failed");
  135. /* Add element with own destructor */
  136. nodep = Curl_hash_add2(&hash_static, &key1, strlen(key1), &key1,
  137. my_elem_dtor);
  138. fail_unless(nodep, "add2 insertion into hash failed");
  139. fail_unless(elem_dtor_calls == 0, "element destructor count should be 0");
  140. /* Add it again, should invoke destructor on first */
  141. nodep = Curl_hash_add2(&hash_static, &key1, strlen(key1), &key1,
  142. my_elem_dtor);
  143. fail_unless(nodep, "add2 again, insertion into hash failed");
  144. fail_unless(elem_dtor_calls == 1, "element destructor count should be 1");
  145. /* remove, should invoke destructor */
  146. rc = Curl_hash_delete(&hash_static, &key1, strlen(key1));
  147. fail_unless(rc == 0, "hash delete failed");
  148. fail_unless(elem_dtor_calls == 2, "element destructor count should be 1");
  149. /* Clean up */
  150. Curl_hash_clean(&hash_static);
  151. UNITTEST_STOP