unit1602.c 2.1 KB

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