unit1602.c 2.1 KB

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