unit1603.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 2015 - 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 
24 #define ENABLE_CURLX_PRINTF
25 #include "curlx.h"
26 
27 #include "hash.h"
28 
29 #include "memdebug.h" /* LAST include file */
30 
31 static struct curl_hash hash_static;
32 static const int slots = 3;
33 
34 static void mydtor(void *p)
35 {
36  /* Data are statically allocated */
37  (void)p; /* unused */
38 }
39 
40 static CURLcode unit_setup(void)
41 {
44 }
45 
46 static void unit_stop(void)
47 {
49 }
50 
52  char key1[] = "key1";
53  char key2[] = "key2b";
54  char key3[] = "key3";
55  char key4[] = "key4";
56  char notakey[] = "notakey";
57  char *nodep;
58  int rc;
59 
60  /* Ensure the key hashes are as expected in order to test both hash
61  collisions and a full table. Unfortunately, the hashes can vary
62  between architectures. */
63  if(Curl_hash_str(key1, strlen(key1), slots) != 1 ||
64  Curl_hash_str(key2, strlen(key2), slots) != 0 ||
65  Curl_hash_str(key3, strlen(key3), slots) != 2 ||
66  Curl_hash_str(key4, strlen(key4), slots) != 1)
67  fprintf(stderr, "Warning: hashes are not computed as expected on this "
68  "architecture; test coverage will be less comprehensive\n");
69 
70  nodep = Curl_hash_add(&hash_static, &key1, strlen(key1), &key1);
71  fail_unless(nodep, "insertion into hash failed");
72  nodep = Curl_hash_pick(&hash_static, &key1, strlen(key1));
73  fail_unless(nodep == key1, "hash retrieval failed");
74 
75  nodep = Curl_hash_add(&hash_static, &key2, strlen(key2), &key2);
76  fail_unless(nodep, "insertion into hash failed");
77  nodep = Curl_hash_pick(&hash_static, &key2, strlen(key2));
78  fail_unless(nodep == key2, "hash retrieval failed");
79 
80  nodep = Curl_hash_add(&hash_static, &key3, strlen(key3), &key3);
81  fail_unless(nodep, "insertion into hash failed");
82  nodep = Curl_hash_pick(&hash_static, &key3, strlen(key3));
83  fail_unless(nodep == key3, "hash retrieval failed");
84 
85  /* The fourth element exceeds the number of slots & collides */
86  nodep = Curl_hash_add(&hash_static, &key4, strlen(key4), &key4);
87  fail_unless(nodep, "insertion into hash failed");
88  nodep = Curl_hash_pick(&hash_static, &key4, strlen(key4));
89  fail_unless(nodep == key4, "hash retrieval failed");
90 
91  /* Make sure all elements are still accessible */
92  nodep = Curl_hash_pick(&hash_static, &key1, strlen(key1));
93  fail_unless(nodep == key1, "hash retrieval failed");
94  nodep = Curl_hash_pick(&hash_static, &key2, strlen(key2));
95  fail_unless(nodep == key2, "hash retrieval failed");
96  nodep = Curl_hash_pick(&hash_static, &key3, strlen(key3));
97  fail_unless(nodep == key3, "hash retrieval failed");
98  nodep = Curl_hash_pick(&hash_static, &key4, strlen(key4));
99  fail_unless(nodep == key4, "hash retrieval failed");
100 
101  /* Delete the second of two entries in a bucket */
102  rc = Curl_hash_delete(&hash_static, &key4, strlen(key4));
103  fail_unless(rc == 0, "hash delete failed");
104  nodep = Curl_hash_pick(&hash_static, &key1, strlen(key1));
105  fail_unless(nodep == key1, "hash retrieval failed");
106  nodep = Curl_hash_pick(&hash_static, &key4, strlen(key4));
107  fail_unless(!nodep, "hash retrieval should have failed");
108 
109  /* Insert that deleted node again */
110  nodep = Curl_hash_add(&hash_static, &key4, strlen(key4), &key4);
111  fail_unless(nodep, "insertion into hash failed");
112  nodep = Curl_hash_pick(&hash_static, &key4, strlen(key4));
113  fail_unless(nodep == key4, "hash retrieval failed");
114 
115  /* Delete the first of two entries in a bucket */
116  rc = Curl_hash_delete(&hash_static, &key1, strlen(key1));
117  fail_unless(rc == 0, "hash delete failed");
118  nodep = Curl_hash_pick(&hash_static, &key1, strlen(key1));
119  fail_unless(!nodep, "hash retrieval should have failed");
120  nodep = Curl_hash_pick(&hash_static, &key4, strlen(key4));
121  fail_unless(nodep == key4, "hash retrieval failed");
122 
123  /* Delete the remaining one of two entries in a bucket */
124  rc = Curl_hash_delete(&hash_static, &key4, strlen(key4));
125  fail_unless(rc == 0, "hash delete failed");
126  nodep = Curl_hash_pick(&hash_static, &key1, strlen(key1));
127  fail_unless(!nodep, "hash retrieval should have failed");
128  nodep = Curl_hash_pick(&hash_static, &key4, strlen(key4));
129  fail_unless(!nodep, "hash retrieval should have failed");
130 
131  /* Delete an already deleted node */
132  rc = Curl_hash_delete(&hash_static, &key4, strlen(key4));
133  fail_unless(rc, "hash delete should have failed");
134 
135  /* Replace an existing node */
136  nodep = Curl_hash_add(&hash_static, &key1, strlen(key1), &notakey);
137  fail_unless(nodep, "insertion into hash failed");
138  nodep = Curl_hash_pick(&hash_static, &key1, strlen(key1));
139  fail_unless(nodep == notakey, "hash retrieval failed");
140 
141  /* Make sure all remaining elements are still accessible */
142  nodep = Curl_hash_pick(&hash_static, &key2, strlen(key2));
143  fail_unless(nodep == key2, "hash retrieval failed");
144  nodep = Curl_hash_pick(&hash_static, &key3, strlen(key3));
145  fail_unless(nodep == key3, "hash retrieval failed");
146 
147  /* Clean up */
149 
static void mydtor(void *p)
Definition: unit1603.c:34
char key3[]
Definition: unit1603.c:54
Definition: hash.h:46
#define UNITTEST_START
Definition: curlcheck.h:86
int Curl_hash_init(struct curl_hash *h, int slots, hash_function hfunc, comp_function comparator, curl_hash_dtor dtor)
Definition: hash.c:57
char * nodep
Definition: unit1603.c:57
char key2[]
Definition: unit1603.c:53
int Curl_hash_delete(struct curl_hash *h, void *key, size_t key_len)
Definition: hash.c:140
CURLcode
Definition: curl.h:454
void Curl_hash_destroy(struct curl_hash *h)
Definition: hash.c:208
void Curl_hash_clean(struct curl_hash *h)
Definition: hash.c:226
char key4[]
Definition: unit1603.c:55
const char ** p
Definition: unit1394.c:76
static void unit_stop(void)
Definition: unit1603.c:46
char notakey[]
Definition: unit1603.c:56
#define fail_unless(expr, msg)
Definition: curlcheck.h:32
size_t Curl_str_key_compare(void *k1, size_t key1_len, void *k2, size_t key2_len)
Definition: hash.c:274
#define UNITTEST_STOP
Definition: curlcheck.h:95
int rc
Definition: unit1603.c:58
static const int slots
Definition: unit1603.c:32
size_t Curl_hash_str(void *key, size_t key_length, size_t slots_num)
Definition: hash.c:260
void * Curl_hash_add(struct curl_hash *h, void *key, size_t key_len, void *p)
Definition: hash.c:110
static CURLcode unit_setup(void)
Definition: unit1603.c:40
#define fprintf
Definition: curl_printf.h:41
UNITTEST_START char key1[]
Definition: unit1603.c:52
static struct curl_hash hash_static
Definition: unit1603.c:31
void * Curl_hash_pick(struct curl_hash *h, void *key, size_t key_len)
Definition: hash.c:162


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:16