unit1603.c 5.9 KB

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