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