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 "munit.h"
31
32 #include "nghttp2_map.h"
33
34 static const MunitTest tests[] = {
35 munit_void_test(test_nghttp2_map),
36 munit_void_test(test_nghttp2_map_functional),
37 munit_void_test(test_nghttp2_map_each_free),
38 munit_void_test(test_nghttp2_map_clear),
39 munit_test_end(),
40 };
41
42 const MunitSuite map_suite = {
43 "/map", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE,
44 };
45
46 typedef struct strentry {
47 nghttp2_map_key_type key;
48 const char *str;
49 } strentry;
50
strentry_init(strentry * entry,nghttp2_map_key_type key,const char * str)51 static void strentry_init(strentry *entry, nghttp2_map_key_type key,
52 const char *str) {
53 entry->key = key;
54 entry->str = str;
55 }
56
test_nghttp2_map(void)57 void test_nghttp2_map(void) {
58 strentry foo, FOO, bar, baz, shrubbery;
59 nghttp2_map map;
60 nghttp2_map_init(&map, nghttp2_mem_default());
61
62 strentry_init(&foo, 1, "foo");
63 strentry_init(&FOO, 1, "FOO");
64 strentry_init(&bar, 2, "bar");
65 strentry_init(&baz, 3, "baz");
66 strentry_init(&shrubbery, 4, "shrubbery");
67
68 assert_int(0, ==, nghttp2_map_insert(&map, foo.key, &foo));
69 assert_string_equal("foo", ((strentry *)nghttp2_map_find(&map, 1))->str);
70 assert_size(1, ==, nghttp2_map_size(&map));
71
72 assert_int(NGHTTP2_ERR_INVALID_ARGUMENT, ==,
73 nghttp2_map_insert(&map, FOO.key, &FOO));
74
75 assert_size(1, ==, nghttp2_map_size(&map));
76 assert_string_equal("foo", ((strentry *)nghttp2_map_find(&map, 1))->str);
77
78 assert_int(0, ==, nghttp2_map_insert(&map, bar.key, &bar));
79 assert_size(2, ==, nghttp2_map_size(&map));
80
81 assert_int(0, ==, nghttp2_map_insert(&map, baz.key, &baz));
82 assert_size(3, ==, nghttp2_map_size(&map));
83
84 assert_int(0, ==, nghttp2_map_insert(&map, shrubbery.key, &shrubbery));
85 assert_size(4, ==, nghttp2_map_size(&map));
86
87 assert_string_equal("baz", ((strentry *)nghttp2_map_find(&map, 3))->str);
88
89 nghttp2_map_remove(&map, 3);
90 assert_size(3, ==, nghttp2_map_size(&map));
91 assert_null(nghttp2_map_find(&map, 3));
92
93 nghttp2_map_remove(&map, 1);
94 assert_size(2, ==, nghttp2_map_size(&map));
95 assert_null(nghttp2_map_find(&map, 1));
96
97 /* Erasing non-existent entry */
98 nghttp2_map_remove(&map, 1);
99 assert_size(2, ==, nghttp2_map_size(&map));
100 assert_null(nghttp2_map_find(&map, 1));
101
102 assert_string_equal("bar", ((strentry *)nghttp2_map_find(&map, 2))->str);
103 assert_string_equal("shrubbery",
104 ((strentry *)nghttp2_map_find(&map, 4))->str);
105
106 nghttp2_map_free(&map);
107 }
108
shuffle(int * a,int n)109 static void shuffle(int *a, int n) {
110 int i;
111 for (i = n - 1; i >= 1; --i) {
112 size_t j = (size_t)((double)(i + 1) * rand() / (RAND_MAX + 1.0));
113 int t = a[j];
114 a[j] = a[i];
115 a[i] = t;
116 }
117 }
118
eachfun(void * data,void * ptr)119 static int eachfun(void *data, void *ptr) {
120 (void)data;
121 (void)ptr;
122
123 return 0;
124 }
125
126 #define NUM_ENT 6000
127 static strentry arr[NUM_ENT];
128 static int order[NUM_ENT];
129
test_nghttp2_map_functional(void)130 void test_nghttp2_map_functional(void) {
131 nghttp2_map map;
132 int i;
133 strentry *ent;
134
135 nghttp2_map_init(&map, nghttp2_mem_default());
136 for (i = 0; i < NUM_ENT; ++i) {
137 strentry_init(&arr[i], (nghttp2_map_key_type)(i + 1), "foo");
138 order[i] = i + 1;
139 }
140 /* insertion */
141 shuffle(order, NUM_ENT);
142 for (i = 0; i < NUM_ENT; ++i) {
143 ent = &arr[order[i] - 1];
144 assert_int(0, ==, nghttp2_map_insert(&map, ent->key, ent));
145 }
146
147 assert_size(NUM_ENT, ==, nghttp2_map_size(&map));
148
149 /* traverse */
150 nghttp2_map_each(&map, eachfun, NULL);
151 /* find */
152 shuffle(order, NUM_ENT);
153 for (i = 0; i < NUM_ENT; ++i) {
154 assert_not_null(nghttp2_map_find(&map, (nghttp2_map_key_type)order[i]));
155 }
156 /* remove */
157 for (i = 0; i < NUM_ENT; ++i) {
158 assert_int(0, ==, nghttp2_map_remove(&map, (nghttp2_map_key_type)order[i]));
159 }
160
161 /* each_free (but no op function for testing purpose) */
162 for (i = 0; i < NUM_ENT; ++i) {
163 strentry_init(&arr[i], (nghttp2_map_key_type)(i + 1), "foo");
164 }
165 /* insert once again */
166 for (i = 0; i < NUM_ENT; ++i) {
167 ent = &arr[i];
168 assert_int(0, ==, nghttp2_map_insert(&map, ent->key, ent));
169 }
170 nghttp2_map_each_free(&map, eachfun, NULL);
171 nghttp2_map_free(&map);
172 }
173
entry_free(void * data,void * ptr)174 static int entry_free(void *data, void *ptr) {
175 const nghttp2_mem *mem = ptr;
176
177 mem->free(data, NULL);
178 return 0;
179 }
180
test_nghttp2_map_each_free(void)181 void test_nghttp2_map_each_free(void) {
182 const nghttp2_mem *mem = nghttp2_mem_default();
183 strentry *foo = mem->malloc(sizeof(strentry), NULL),
184 *bar = mem->malloc(sizeof(strentry), NULL),
185 *baz = mem->malloc(sizeof(strentry), NULL),
186 *shrubbery = mem->malloc(sizeof(strentry), NULL);
187 nghttp2_map map;
188 nghttp2_map_init(&map, nghttp2_mem_default());
189
190 strentry_init(foo, 1, "foo");
191 strentry_init(bar, 2, "bar");
192 strentry_init(baz, 3, "baz");
193 strentry_init(shrubbery, 4, "shrubbery");
194
195 nghttp2_map_insert(&map, foo->key, foo);
196 nghttp2_map_insert(&map, bar->key, bar);
197 nghttp2_map_insert(&map, baz->key, baz);
198 nghttp2_map_insert(&map, shrubbery->key, shrubbery);
199
200 nghttp2_map_each_free(&map, entry_free, (void *)mem);
201 nghttp2_map_free(&map);
202 }
203
test_nghttp2_map_clear(void)204 void test_nghttp2_map_clear(void) {
205 nghttp2_mem *mem = nghttp2_mem_default();
206 nghttp2_map map;
207 strentry foo;
208
209 strentry_init(&foo, 1, "foo");
210
211 nghttp2_map_init(&map, mem);
212
213 assert_int(0, ==, nghttp2_map_insert(&map, foo.key, &foo));
214
215 nghttp2_map_clear(&map);
216
217 assert_size(0, ==, nghttp2_map_size(&map));
218
219 nghttp2_map_free(&map);
220 }
221