2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2012 Tatsuhiro Tsujikawa
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:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
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.
25 #include "nghttp2_map_test.h"
27 #include <CUnit/CUnit.h>
29 #include "nghttp2_map.h"
31 typedef struct strentry {
32 nghttp2_map_entry map_entry;
36 static void strentry_init(strentry *entry, key_type key, const char *str) {
37 nghttp2_map_entry_init(&entry->map_entry, key);
41 void test_nghttp2_map(void) {
42 strentry foo, FOO, bar, baz, shrubbery;
44 nghttp2_map_init(&map, nghttp2_mem_default());
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");
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));
56 CU_ASSERT(NGHTTP2_ERR_INVALID_ARGUMENT ==
57 nghttp2_map_insert(&map, &FOO.map_entry));
59 CU_ASSERT(1 == nghttp2_map_size(&map));
60 CU_ASSERT(strcmp("foo", ((strentry *)nghttp2_map_find(&map, 1))->str) == 0);
62 CU_ASSERT(0 == nghttp2_map_insert(&map, &bar.map_entry));
63 CU_ASSERT(2 == nghttp2_map_size(&map));
65 CU_ASSERT(0 == nghttp2_map_insert(&map, &baz.map_entry));
66 CU_ASSERT(3 == nghttp2_map_size(&map));
68 CU_ASSERT(0 == nghttp2_map_insert(&map, &shrubbery.map_entry));
69 CU_ASSERT(4 == nghttp2_map_size(&map));
71 CU_ASSERT(strcmp("baz", ((strentry *)nghttp2_map_find(&map, 3))->str) == 0);
73 nghttp2_map_remove(&map, 3);
74 CU_ASSERT(3 == nghttp2_map_size(&map));
75 CU_ASSERT(NULL == nghttp2_map_find(&map, 3));
77 nghttp2_map_remove(&map, 1);
78 CU_ASSERT(2 == nghttp2_map_size(&map));
79 CU_ASSERT(NULL == nghttp2_map_find(&map, 1));
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));
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) ==
90 nghttp2_map_free(&map);
93 static void shuffle(int *a, int n) {
95 for (i = n - 1; i >= 1; --i) {
96 size_t j = (size_t)((double)(i + 1) * rand() / (RAND_MAX + 1.0));
103 static int eachfun(nghttp2_map_entry *entry, void *ptr) {
111 static strentry arr[NUM_ENT];
112 static int order[NUM_ENT];
114 void test_nghttp2_map_functional(void) {
118 nghttp2_map_init(&map, nghttp2_mem_default());
119 for (i = 0; i < NUM_ENT; ++i) {
120 strentry_init(&arr[i], i + 1, "foo");
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));
129 nghttp2_map_each(&map, eachfun, NULL);
131 shuffle(order, NUM_ENT);
132 for (i = 0; i < NUM_ENT; ++i) {
133 nghttp2_map_find(&map, order[i]);
136 shuffle(order, NUM_ENT);
137 for (i = 0; i < NUM_ENT; ++i) {
138 CU_ASSERT(0 == nghttp2_map_remove(&map, order[i]));
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");
145 /* insert once again */
146 for (i = 0; i < NUM_ENT; ++i) {
147 CU_ASSERT(0 == nghttp2_map_insert(&map, &arr[i].map_entry));
149 nghttp2_map_each_free(&map, eachfun, NULL);
150 nghttp2_map_free(&map);
153 static int entry_free(nghttp2_map_entry *entry, void *ptr) {
154 nghttp2_mem *mem = ptr;
156 mem->free(entry, NULL);
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);
167 nghttp2_map_init(&map, nghttp2_mem_default());
169 strentry_init(foo, 1, "foo");
170 strentry_init(bar, 2, "bar");
171 strentry_init(baz, 3, "baz");
172 strentry_init(shrubbery, 4, "shrubbery");
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);
179 nghttp2_map_each_free(&map, entry_free, mem);
180 nghttp2_map_free(&map);