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 = (int)((double)(i + 1) * rand() / (RAND_MAX + 1.0));
103 static int eachfun(nghttp2_map_entry *entry _U_, void *ptr _U_) { return 0; }
106 strentry arr[NUM_ENT];
109 void test_nghttp2_map_functional(void) {
113 nghttp2_map_init(&map, nghttp2_mem_default());
114 for (i = 0; i < NUM_ENT; ++i) {
115 strentry_init(&arr[i], i + 1, "foo");
119 shuffle(order, NUM_ENT);
120 for (i = 0; i < NUM_ENT; ++i) {
121 CU_ASSERT(0 == nghttp2_map_insert(&map, &arr[order[i] - 1].map_entry));
124 nghttp2_map_each(&map, eachfun, NULL);
126 shuffle(order, NUM_ENT);
127 for (i = 0; i < NUM_ENT; ++i) {
128 nghttp2_map_find(&map, order[i]);
131 shuffle(order, NUM_ENT);
132 for (i = 0; i < NUM_ENT; ++i) {
133 CU_ASSERT(0 == nghttp2_map_remove(&map, order[i]));
136 /* each_free (but no op function for testing purpose) */
137 for (i = 0; i < NUM_ENT; ++i) {
138 strentry_init(&arr[i], i + 1, "foo");
140 /* insert once again */
141 for (i = 0; i < NUM_ENT; ++i) {
142 CU_ASSERT(0 == nghttp2_map_insert(&map, &arr[i].map_entry));
144 nghttp2_map_each_free(&map, eachfun, NULL);
145 nghttp2_map_free(&map);
148 static int entry_free(nghttp2_map_entry *entry, void *ptr _U_) {
153 void test_nghttp2_map_each_free(void) {
154 strentry *foo = malloc(sizeof(strentry)), *bar = malloc(sizeof(strentry)),
155 *baz = malloc(sizeof(strentry)),
156 *shrubbery = malloc(sizeof(strentry));
158 nghttp2_map_init(&map, nghttp2_mem_default());
160 strentry_init(foo, 1, "foo");
161 strentry_init(bar, 2, "bar");
162 strentry_init(baz, 3, "baz");
163 strentry_init(shrubbery, 4, "shrubbery");
165 nghttp2_map_insert(&map, &foo->map_entry);
166 nghttp2_map_insert(&map, &bar->map_entry);
167 nghttp2_map_insert(&map, &baz->map_entry);
168 nghttp2_map_insert(&map, &shrubbery->map_entry);
170 nghttp2_map_each_free(&map, entry_free, NULL);
171 nghttp2_map_free(&map);