unit1616.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 void mydtor(void *elem)
  31. {
  32. int *ptr = (int *)elem;
  33. free(ptr);
  34. }
  35. static CURLcode unit_setup(void)
  36. {
  37. Curl_hash_offt_init(&hash_static, 15, mydtor);
  38. return CURLE_OK;
  39. }
  40. static void unit_stop(void)
  41. {
  42. Curl_hash_destroy(&hash_static);
  43. }
  44. UNITTEST_START
  45. int *value, *v;
  46. int *value2;
  47. int *nodep;
  48. curl_off_t key = 20;
  49. curl_off_t key2 = 25;
  50. value = malloc(sizeof(int));
  51. abort_unless(value != NULL, "Out of memory");
  52. *value = 199;
  53. nodep = Curl_hash_offt_set(&hash_static, key, value);
  54. if(!nodep)
  55. free(value);
  56. abort_unless(nodep, "insertion into hash failed");
  57. v = Curl_hash_offt_get(&hash_static, key);
  58. abort_unless(v == value, "lookup present entry failed");
  59. v = Curl_hash_offt_get(&hash_static, key2);
  60. abort_unless(!v, "lookup missing entry failed");
  61. Curl_hash_clean(&hash_static);
  62. /* Attempt to add another key/value pair */
  63. value2 = malloc(sizeof(int));
  64. abort_unless(value2 != NULL, "Out of memory");
  65. *value2 = 204;
  66. nodep = Curl_hash_offt_set(&hash_static, key2, value2);
  67. if(!nodep)
  68. free(value2);
  69. abort_unless(nodep, "insertion into hash failed");
  70. v = Curl_hash_offt_get(&hash_static, key2);
  71. abort_unless(v == value2, "lookup present entry failed");
  72. v = Curl_hash_offt_get(&hash_static, key);
  73. abort_unless(!v, "lookup missing entry failed");
  74. UNITTEST_STOP