unit1616.c 2.5 KB

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