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