xref: /curl/tests/unit/unit1616.c (revision c6655f70)
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 * elem)35 static void mydtor(void *elem)
36 {
37   int *ptr = (int *)elem;
38   free(ptr);
39 }
40 
unit_setup(void)41 static CURLcode unit_setup(void)
42 {
43   Curl_hash_offt_init(&hash_static, 15, 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, *v;
54   int *value2;
55   int *nodep;
56 
57   curl_off_t key = 20;
58   curl_off_t key2 = 25;
59 
60 
61   value = malloc(sizeof(int));
62   abort_unless(value != NULL, "Out of memory");
63   *value = 199;
64   nodep = Curl_hash_offt_set(&hash_static, key, value);
65   if(!nodep)
66     free(value);
67   abort_unless(nodep, "insertion into hash failed");
68   v = Curl_hash_offt_get(&hash_static, key);
69   abort_unless(v == value, "lookup present entry failed");
70   v = Curl_hash_offt_get(&hash_static, key2);
71   abort_unless(!v, "lookup missing entry failed");
72   Curl_hash_clean(&hash_static);
73 
74   /* Attempt to add another key/value pair */
75   value2 = malloc(sizeof(int));
76   abort_unless(value2 != NULL, "Out of memory");
77   *value2 = 204;
78   nodep = Curl_hash_offt_set(&hash_static, key2, value2);
79   if(!nodep)
80     free(value2);
81   abort_unless(nodep, "insertion into hash failed");
82   v = Curl_hash_offt_get(&hash_static, key2);
83   abort_unless(v == value2, "lookup present entry failed");
84   v = Curl_hash_offt_get(&hash_static, key);
85   abort_unless(!v, "lookup missing entry failed");
86 
87 UNITTEST_STOP
88