Annotation of embedaddon/curl/tests/unit/unit1603.c, revision 1.1.1.1
1.1 misho 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: {
42: return Curl_hash_init(&hash_static, slots, Curl_hash_str,
43: Curl_str_key_compare, mydtor);
44: }
45:
46: static void unit_stop(void)
47: {
48: Curl_hash_destroy(&hash_static);
49: }
50:
51: UNITTEST_START
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), ¬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");
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 */
148: Curl_hash_clean(&hash_static);
149:
150: UNITTEST_STOP
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>