1 /*
2 * nghttp2 - HTTP/2 C Library
3 *
4 * Copyright (c) 2017 ngtcp2 contributors
5 * Copyright (c) 2012 nghttp2 contributors
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26 #include "nghttp2_map_test.h"
27
28 #include <stdio.h>
29
30 #include <CUnit/CUnit.h>
31
32 #include "nghttp2_map.h"
33
34 typedef struct strentry {
35 nghttp2_map_key_type key;
36 const char *str;
37 } strentry;
38
strentry_init(strentry * entry,nghttp2_map_key_type key,const char * str)39 static void strentry_init(strentry *entry, nghttp2_map_key_type key,
40 const char *str) {
41 entry->key = key;
42 entry->str = str;
43 }
44
test_nghttp2_map(void)45 void test_nghttp2_map(void) {
46 strentry foo, FOO, bar, baz, shrubbery;
47 nghttp2_map map;
48 nghttp2_map_init(&map, nghttp2_mem_default());
49
50 strentry_init(&foo, 1, "foo");
51 strentry_init(&FOO, 1, "FOO");
52 strentry_init(&bar, 2, "bar");
53 strentry_init(&baz, 3, "baz");
54 strentry_init(&shrubbery, 4, "shrubbery");
55
56 CU_ASSERT(0 == nghttp2_map_insert(&map, foo.key, &foo));
57 CU_ASSERT(strcmp("foo", ((strentry *)nghttp2_map_find(&map, 1))->str) == 0);
58 CU_ASSERT(1 == nghttp2_map_size(&map));
59
60 CU_ASSERT(NGHTTP2_ERR_INVALID_ARGUMENT ==
61 nghttp2_map_insert(&map, FOO.key, &FOO));
62
63 CU_ASSERT(1 == nghttp2_map_size(&map));
64 CU_ASSERT(strcmp("foo", ((strentry *)nghttp2_map_find(&map, 1))->str) == 0);
65
66 CU_ASSERT(0 == nghttp2_map_insert(&map, bar.key, &bar));
67 CU_ASSERT(2 == nghttp2_map_size(&map));
68
69 CU_ASSERT(0 == nghttp2_map_insert(&map, baz.key, &baz));
70 CU_ASSERT(3 == nghttp2_map_size(&map));
71
72 CU_ASSERT(0 == nghttp2_map_insert(&map, shrubbery.key, &shrubbery));
73 CU_ASSERT(4 == nghttp2_map_size(&map));
74
75 CU_ASSERT(strcmp("baz", ((strentry *)nghttp2_map_find(&map, 3))->str) == 0);
76
77 nghttp2_map_remove(&map, 3);
78 CU_ASSERT(3 == nghttp2_map_size(&map));
79 CU_ASSERT(NULL == nghttp2_map_find(&map, 3));
80
81 nghttp2_map_remove(&map, 1);
82 CU_ASSERT(2 == nghttp2_map_size(&map));
83 CU_ASSERT(NULL == nghttp2_map_find(&map, 1));
84
85 /* Erasing non-existent entry */
86 nghttp2_map_remove(&map, 1);
87 CU_ASSERT(2 == nghttp2_map_size(&map));
88 CU_ASSERT(NULL == nghttp2_map_find(&map, 1));
89
90 CU_ASSERT(strcmp("bar", ((strentry *)nghttp2_map_find(&map, 2))->str) == 0);
91 CU_ASSERT(strcmp("shrubbery", ((strentry *)nghttp2_map_find(&map, 4))->str) ==
92 0);
93
94 nghttp2_map_free(&map);
95 }
96
shuffle(int * a,int n)97 static void shuffle(int *a, int n) {
98 int i;
99 for (i = n - 1; i >= 1; --i) {
100 size_t j = (size_t)((double)(i + 1) * rand() / (RAND_MAX + 1.0));
101 int t = a[j];
102 a[j] = a[i];
103 a[i] = t;
104 }
105 }
106
eachfun(void * data,void * ptr)107 static int eachfun(void *data, void *ptr) {
108 (void)data;
109 (void)ptr;
110
111 return 0;
112 }
113
114 #define NUM_ENT 6000
115 static strentry arr[NUM_ENT];
116 static int order[NUM_ENT];
117
test_nghttp2_map_functional(void)118 void test_nghttp2_map_functional(void) {
119 nghttp2_map map;
120 int i;
121 strentry *ent;
122
123 nghttp2_map_init(&map, nghttp2_mem_default());
124 for (i = 0; i < NUM_ENT; ++i) {
125 strentry_init(&arr[i], (nghttp2_map_key_type)(i + 1), "foo");
126 order[i] = i + 1;
127 }
128 /* insertion */
129 shuffle(order, NUM_ENT);
130 for (i = 0; i < NUM_ENT; ++i) {
131 ent = &arr[order[i] - 1];
132 CU_ASSERT(0 == nghttp2_map_insert(&map, ent->key, ent));
133 }
134
135 CU_ASSERT(NUM_ENT == nghttp2_map_size(&map));
136
137 /* traverse */
138 nghttp2_map_each(&map, eachfun, NULL);
139 /* find */
140 shuffle(order, NUM_ENT);
141 for (i = 0; i < NUM_ENT; ++i) {
142 CU_ASSERT(NULL != nghttp2_map_find(&map, (nghttp2_map_key_type)order[i]));
143 }
144 /* remove */
145 for (i = 0; i < NUM_ENT; ++i) {
146 CU_ASSERT(0 == nghttp2_map_remove(&map, (nghttp2_map_key_type)order[i]));
147 }
148
149 /* each_free (but no op function for testing purpose) */
150 for (i = 0; i < NUM_ENT; ++i) {
151 strentry_init(&arr[i], (nghttp2_map_key_type)(i + 1), "foo");
152 }
153 /* insert once again */
154 for (i = 0; i < NUM_ENT; ++i) {
155 ent = &arr[i];
156 CU_ASSERT(0 == nghttp2_map_insert(&map, ent->key, ent));
157 }
158 nghttp2_map_each_free(&map, eachfun, NULL);
159 nghttp2_map_free(&map);
160 }
161
entry_free(void * data,void * ptr)162 static int entry_free(void *data, void *ptr) {
163 const nghttp2_mem *mem = ptr;
164
165 mem->free(data, NULL);
166 return 0;
167 }
168
test_nghttp2_map_each_free(void)169 void test_nghttp2_map_each_free(void) {
170 const nghttp2_mem *mem = nghttp2_mem_default();
171 strentry *foo = mem->malloc(sizeof(strentry), NULL),
172 *bar = mem->malloc(sizeof(strentry), NULL),
173 *baz = mem->malloc(sizeof(strentry), NULL),
174 *shrubbery = mem->malloc(sizeof(strentry), NULL);
175 nghttp2_map map;
176 nghttp2_map_init(&map, nghttp2_mem_default());
177
178 strentry_init(foo, 1, "foo");
179 strentry_init(bar, 2, "bar");
180 strentry_init(baz, 3, "baz");
181 strentry_init(shrubbery, 4, "shrubbery");
182
183 nghttp2_map_insert(&map, foo->key, foo);
184 nghttp2_map_insert(&map, bar->key, bar);
185 nghttp2_map_insert(&map, baz->key, baz);
186 nghttp2_map_insert(&map, shrubbery->key, shrubbery);
187
188 nghttp2_map_each_free(&map, entry_free, (void *)mem);
189 nghttp2_map_free(&map);
190 }
191
test_nghttp2_map_clear(void)192 void test_nghttp2_map_clear(void) {
193 nghttp2_mem *mem = nghttp2_mem_default();
194 nghttp2_map map;
195 strentry foo;
196
197 strentry_init(&foo, 1, "foo");
198
199 nghttp2_map_init(&map, mem);
200
201 CU_ASSERT(0 == nghttp2_map_insert(&map, foo.key, &foo));
202
203 nghttp2_map_clear(&map);
204
205 CU_ASSERT(0 == nghttp2_map_size(&map));
206
207 nghttp2_map_free(&map);
208 }
209