2
0

unit1602.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 *p)
  31. {
  32. int *ptr = (int *)p;
  33. free(ptr);
  34. }
  35. static CURLcode unit_setup(void)
  36. {
  37. Curl_hash_init(&hash_static, 7, Curl_hash_str,
  38. Curl_str_key_compare, mydtor);
  39. return CURLE_OK;
  40. }
  41. static void unit_stop(void)
  42. {
  43. Curl_hash_destroy(&hash_static);
  44. }
  45. UNITTEST_START
  46. int *value;
  47. int *value2;
  48. int *nodep;
  49. size_t klen = sizeof(int);
  50. int key = 20;
  51. int key2 = 25;
  52. value = malloc(sizeof(int));
  53. abort_unless(value != NULL, "Out of memory");
  54. *value = 199;
  55. nodep = Curl_hash_add(&hash_static, &key, klen, value);
  56. if(!nodep)
  57. free(value);
  58. abort_unless(nodep, "insertion into hash failed");
  59. Curl_hash_clean(&hash_static);
  60. /* Attempt to add another key/value pair */
  61. value2 = malloc(sizeof(int));
  62. abort_unless(value2 != NULL, "Out of memory");
  63. *value2 = 204;
  64. nodep = Curl_hash_add(&hash_static, &key2, klen, value2);
  65. if(!nodep)
  66. free(value2);
  67. abort_unless(nodep, "insertion into hash failed");
  68. UNITTEST_STOP