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
26 #define ENABLE_CURLX_PRINTF
27 #include "curlx.h"
28
29 #include "hash.h"
30
31 #include "memdebug.h" /* LAST include file */
32
33 static struct Curl_hash hash_static;
34
mydtor(void * p)35 static void mydtor(void *p)
36 {
37 int *ptr = (int *)p;
38 free(ptr);
39 }
40
unit_setup(void)41 static CURLcode unit_setup(void)
42 {
43 Curl_hash_init(&hash_static, 7, Curl_hash_str,
44 Curl_str_key_compare, mydtor);
45 return CURLE_OK;
46 }
47
unit_stop(void)48 static void unit_stop(void)
49 {
50 Curl_hash_destroy(&hash_static);
51 }
52
53 UNITTEST_START
54 int *value;
55 int *value2;
56 int *nodep;
57 size_t klen = sizeof(int);
58
59 int key = 20;
60 int key2 = 25;
61
62
63 value = malloc(sizeof(int));
64 abort_unless(value != NULL, "Out of memory");
65 *value = 199;
66 nodep = Curl_hash_add(&hash_static, &key, klen, value);
67 if(!nodep)
68 free(value);
69 abort_unless(nodep, "insertion into hash failed");
70 Curl_hash_clean(&hash_static);
71
72 /* Attempt to add another key/value pair */
73 value2 = malloc(sizeof(int));
74 abort_unless(value2 != NULL, "Out of memory");
75 *value2 = 204;
76 nodep = Curl_hash_add(&hash_static, &key2, klen, value2);
77 if(!nodep)
78 free(value2);
79 abort_unless(nodep, "insertion into hash failed");
80
81 UNITTEST_STOP
82