1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
22 #include "curlcheck.h"
24 #define ENABLE_CURLX_PRINTF
29 #include "memdebug.h" /* LAST include file */
31 static struct curl_hash hash_static;
32 static const int slots = 3;
34 static void mydtor(void *p)
36 /* Data are statically allocated */
40 static CURLcode unit_setup( void )
42 return Curl_hash_init(&hash_static, slots, Curl_hash_str,
43 Curl_str_key_compare, mydtor);
46 static void unit_stop( void )
48 Curl_hash_destroy(&hash_static);
53 char key2[] = "key2b";
56 char notakey[] = "notakey";
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
135 /* Replace an existing node */
136 nodep = Curl_hash_add(&hash_static, &key1, strlen(key1), ¬akey);
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");
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");
148 Curl_hash_clean(&hash_static);