unit1603.c 5.9 KB

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