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