2012-01-19 12:36:13 +00:00
|
|
|
/*
|
2014-03-30 10:09:21 +00:00
|
|
|
* nghttp2 - HTTP/2 C Library
|
2012-01-19 12:36:13 +00:00
|
|
|
*
|
2021-05-13 06:01:58 +00:00
|
|
|
* Copyright (c) 2017 ngtcp2 contributors
|
|
|
|
* Copyright (c) 2012 nghttp2 contributors
|
2012-01-19 12:36:13 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
2013-07-12 15:19:03 +00:00
|
|
|
#include "nghttp2_map_test.h"
|
2012-01-19 12:36:13 +00:00
|
|
|
|
|
|
|
#include <CUnit/CUnit.h>
|
|
|
|
|
2013-07-12 15:19:03 +00:00
|
|
|
#include "nghttp2_map.h"
|
2012-01-19 12:36:13 +00:00
|
|
|
|
2012-09-11 15:13:02 +00:00
|
|
|
typedef struct strentry {
|
2021-05-13 06:01:58 +00:00
|
|
|
nghttp2_map_key_type key;
|
2012-09-11 15:13:02 +00:00
|
|
|
const char *str;
|
|
|
|
} strentry;
|
|
|
|
|
2021-05-13 06:01:58 +00:00
|
|
|
static void strentry_init(strentry *entry, nghttp2_map_key_type key,
|
|
|
|
const char *str) {
|
|
|
|
entry->key = key;
|
2012-09-11 15:13:02 +00:00
|
|
|
entry->str = str;
|
|
|
|
}
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
void test_nghttp2_map(void) {
|
2012-09-11 15:13:02 +00:00
|
|
|
strentry foo, FOO, bar, baz, shrubbery;
|
2013-07-12 15:19:03 +00:00
|
|
|
nghttp2_map map;
|
2014-12-07 14:07:13 +00:00
|
|
|
nghttp2_map_init(&map, nghttp2_mem_default());
|
2012-09-11 15:13:02 +00:00
|
|
|
|
|
|
|
strentry_init(&foo, 1, "foo");
|
|
|
|
strentry_init(&FOO, 1, "FOO");
|
|
|
|
strentry_init(&bar, 2, "bar");
|
|
|
|
strentry_init(&baz, 3, "baz");
|
|
|
|
strentry_init(&shrubbery, 4, "shrubbery");
|
|
|
|
|
2021-05-13 06:01:58 +00:00
|
|
|
CU_ASSERT(0 == nghttp2_map_insert(&map, foo.key, &foo));
|
2014-11-27 14:39:04 +00:00
|
|
|
CU_ASSERT(strcmp("foo", ((strentry *)nghttp2_map_find(&map, 1))->str) == 0);
|
2013-07-12 15:19:03 +00:00
|
|
|
CU_ASSERT(1 == nghttp2_map_size(&map));
|
2012-09-11 15:13:02 +00:00
|
|
|
|
2013-07-12 15:19:03 +00:00
|
|
|
CU_ASSERT(NGHTTP2_ERR_INVALID_ARGUMENT ==
|
2021-05-13 06:01:58 +00:00
|
|
|
nghttp2_map_insert(&map, FOO.key, &FOO));
|
2012-09-11 15:13:02 +00:00
|
|
|
|
2013-07-12 15:19:03 +00:00
|
|
|
CU_ASSERT(1 == nghttp2_map_size(&map));
|
2014-11-27 14:39:04 +00:00
|
|
|
CU_ASSERT(strcmp("foo", ((strentry *)nghttp2_map_find(&map, 1))->str) == 0);
|
2012-09-11 15:13:02 +00:00
|
|
|
|
2021-05-13 06:01:58 +00:00
|
|
|
CU_ASSERT(0 == nghttp2_map_insert(&map, bar.key, &bar));
|
2013-07-12 15:19:03 +00:00
|
|
|
CU_ASSERT(2 == nghttp2_map_size(&map));
|
2012-09-11 15:13:02 +00:00
|
|
|
|
2021-05-13 06:01:58 +00:00
|
|
|
CU_ASSERT(0 == nghttp2_map_insert(&map, baz.key, &baz));
|
2013-07-12 15:19:03 +00:00
|
|
|
CU_ASSERT(3 == nghttp2_map_size(&map));
|
2012-09-11 15:13:02 +00:00
|
|
|
|
2021-05-13 06:01:58 +00:00
|
|
|
CU_ASSERT(0 == nghttp2_map_insert(&map, shrubbery.key, &shrubbery));
|
2013-07-12 15:19:03 +00:00
|
|
|
CU_ASSERT(4 == nghttp2_map_size(&map));
|
2012-01-19 12:36:13 +00:00
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
CU_ASSERT(strcmp("baz", ((strentry *)nghttp2_map_find(&map, 3))->str) == 0);
|
2012-09-11 15:13:02 +00:00
|
|
|
|
2013-07-12 15:19:03 +00:00
|
|
|
nghttp2_map_remove(&map, 3);
|
|
|
|
CU_ASSERT(3 == nghttp2_map_size(&map));
|
|
|
|
CU_ASSERT(NULL == nghttp2_map_find(&map, 3));
|
2012-09-11 15:13:02 +00:00
|
|
|
|
2013-07-12 15:19:03 +00:00
|
|
|
nghttp2_map_remove(&map, 1);
|
|
|
|
CU_ASSERT(2 == nghttp2_map_size(&map));
|
|
|
|
CU_ASSERT(NULL == nghttp2_map_find(&map, 1));
|
2012-01-19 12:36:13 +00:00
|
|
|
|
2012-01-25 17:28:58 +00:00
|
|
|
/* Erasing non-existent entry */
|
2013-07-12 15:19:03 +00:00
|
|
|
nghttp2_map_remove(&map, 1);
|
|
|
|
CU_ASSERT(2 == nghttp2_map_size(&map));
|
|
|
|
CU_ASSERT(NULL == nghttp2_map_find(&map, 1));
|
2012-01-25 15:21:31 +00:00
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
CU_ASSERT(strcmp("bar", ((strentry *)nghttp2_map_find(&map, 2))->str) == 0);
|
|
|
|
CU_ASSERT(strcmp("shrubbery", ((strentry *)nghttp2_map_find(&map, 4))->str) ==
|
|
|
|
0);
|
2012-01-19 12:36:13 +00:00
|
|
|
|
2013-07-12 15:19:03 +00:00
|
|
|
nghttp2_map_free(&map);
|
2012-01-19 12:36:13 +00:00
|
|
|
}
|
2012-09-11 15:13:02 +00:00
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
static void shuffle(int *a, int n) {
|
2012-10-04 12:24:45 +00:00
|
|
|
int i;
|
2014-11-27 14:39:04 +00:00
|
|
|
for (i = n - 1; i >= 1; --i) {
|
2015-09-23 05:41:53 +00:00
|
|
|
size_t j = (size_t)((double)(i + 1) * rand() / (RAND_MAX + 1.0));
|
2012-10-04 12:24:45 +00:00
|
|
|
int t = a[j];
|
|
|
|
a[j] = a[i];
|
|
|
|
a[i] = t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-13 06:01:58 +00:00
|
|
|
static int eachfun(void *data, void *ptr) {
|
|
|
|
(void)data;
|
2017-03-11 08:46:39 +00:00
|
|
|
(void)ptr;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-10-04 12:24:45 +00:00
|
|
|
|
|
|
|
#define NUM_ENT 6000
|
2015-09-23 05:41:53 +00:00
|
|
|
static strentry arr[NUM_ENT];
|
|
|
|
static int order[NUM_ENT];
|
2012-10-04 12:24:45 +00:00
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
void test_nghttp2_map_functional(void) {
|
2013-07-12 15:19:03 +00:00
|
|
|
nghttp2_map map;
|
2012-10-04 12:24:45 +00:00
|
|
|
int i;
|
2021-05-13 06:01:58 +00:00
|
|
|
strentry *ent;
|
2012-10-04 12:24:45 +00:00
|
|
|
|
2014-12-07 14:07:13 +00:00
|
|
|
nghttp2_map_init(&map, nghttp2_mem_default());
|
2014-11-27 14:39:04 +00:00
|
|
|
for (i = 0; i < NUM_ENT; ++i) {
|
2021-05-13 06:01:58 +00:00
|
|
|
strentry_init(&arr[i], (nghttp2_map_key_type)(i + 1), "foo");
|
2012-10-04 12:24:45 +00:00
|
|
|
order[i] = i + 1;
|
|
|
|
}
|
2012-11-14 12:00:36 +00:00
|
|
|
/* insertion */
|
2012-10-04 12:24:45 +00:00
|
|
|
shuffle(order, NUM_ENT);
|
2014-11-27 14:39:04 +00:00
|
|
|
for (i = 0; i < NUM_ENT; ++i) {
|
2021-05-13 06:01:58 +00:00
|
|
|
ent = &arr[order[i] - 1];
|
|
|
|
CU_ASSERT(0 == nghttp2_map_insert(&map, ent->key, ent));
|
2012-10-04 12:24:45 +00:00
|
|
|
}
|
2021-05-13 06:01:58 +00:00
|
|
|
|
|
|
|
CU_ASSERT(NUM_ENT == nghttp2_map_size(&map));
|
|
|
|
|
2012-11-14 12:00:36 +00:00
|
|
|
/* traverse */
|
2013-07-12 15:19:03 +00:00
|
|
|
nghttp2_map_each(&map, eachfun, NULL);
|
2012-11-14 12:00:36 +00:00
|
|
|
/* find */
|
2012-10-04 12:24:45 +00:00
|
|
|
shuffle(order, NUM_ENT);
|
2014-11-27 14:39:04 +00:00
|
|
|
for (i = 0; i < NUM_ENT; ++i) {
|
2021-05-13 06:01:58 +00:00
|
|
|
CU_ASSERT(NULL != nghttp2_map_find(&map, (nghttp2_map_key_type)order[i]));
|
2012-10-04 12:24:45 +00:00
|
|
|
}
|
2012-11-14 12:00:36 +00:00
|
|
|
/* remove */
|
2014-11-27 14:39:04 +00:00
|
|
|
for (i = 0; i < NUM_ENT; ++i) {
|
2021-05-13 06:01:58 +00:00
|
|
|
CU_ASSERT(0 == nghttp2_map_remove(&map, (nghttp2_map_key_type)order[i]));
|
2012-10-04 12:24:45 +00:00
|
|
|
}
|
|
|
|
|
2012-11-14 12:00:36 +00:00
|
|
|
/* each_free (but no op function for testing purpose) */
|
2014-11-27 14:39:04 +00:00
|
|
|
for (i = 0; i < NUM_ENT; ++i) {
|
2021-05-13 06:01:58 +00:00
|
|
|
strentry_init(&arr[i], (nghttp2_map_key_type)(i + 1), "foo");
|
2012-10-05 14:41:49 +00:00
|
|
|
}
|
2012-11-14 12:00:36 +00:00
|
|
|
/* insert once again */
|
2014-11-27 14:39:04 +00:00
|
|
|
for (i = 0; i < NUM_ENT; ++i) {
|
2021-05-13 06:01:58 +00:00
|
|
|
ent = &arr[i];
|
|
|
|
CU_ASSERT(0 == nghttp2_map_insert(&map, ent->key, ent));
|
2012-10-05 14:41:49 +00:00
|
|
|
}
|
2013-07-12 15:19:03 +00:00
|
|
|
nghttp2_map_each_free(&map, eachfun, NULL);
|
2013-12-04 15:41:42 +00:00
|
|
|
nghttp2_map_free(&map);
|
2012-10-04 12:24:45 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 06:01:58 +00:00
|
|
|
static int entry_free(void *data, void *ptr) {
|
|
|
|
const nghttp2_mem *mem = ptr;
|
2015-03-03 14:23:43 +00:00
|
|
|
|
2021-05-13 06:01:58 +00:00
|
|
|
mem->free(data, NULL);
|
2012-09-11 15:13:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
void test_nghttp2_map_each_free(void) {
|
2021-05-13 06:01:58 +00:00
|
|
|
const nghttp2_mem *mem = nghttp2_mem_default();
|
2015-03-03 14:23:43 +00:00
|
|
|
strentry *foo = mem->malloc(sizeof(strentry), NULL),
|
|
|
|
*bar = mem->malloc(sizeof(strentry), NULL),
|
|
|
|
*baz = mem->malloc(sizeof(strentry), NULL),
|
|
|
|
*shrubbery = mem->malloc(sizeof(strentry), NULL);
|
2013-07-12 15:19:03 +00:00
|
|
|
nghttp2_map map;
|
2014-12-07 14:07:13 +00:00
|
|
|
nghttp2_map_init(&map, nghttp2_mem_default());
|
2012-09-11 15:13:02 +00:00
|
|
|
|
|
|
|
strentry_init(foo, 1, "foo");
|
|
|
|
strentry_init(bar, 2, "bar");
|
|
|
|
strentry_init(baz, 3, "baz");
|
|
|
|
strentry_init(shrubbery, 4, "shrubbery");
|
|
|
|
|
2021-05-13 06:01:58 +00:00
|
|
|
nghttp2_map_insert(&map, foo->key, foo);
|
|
|
|
nghttp2_map_insert(&map, bar->key, bar);
|
|
|
|
nghttp2_map_insert(&map, baz->key, baz);
|
|
|
|
nghttp2_map_insert(&map, shrubbery->key, shrubbery);
|
|
|
|
|
|
|
|
nghttp2_map_each_free(&map, entry_free, (void *)mem);
|
|
|
|
nghttp2_map_free(&map);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_nghttp2_map_clear(void) {
|
|
|
|
nghttp2_mem *mem = nghttp2_mem_default();
|
|
|
|
nghttp2_map map;
|
|
|
|
strentry foo;
|
|
|
|
|
|
|
|
strentry_init(&foo, 1, "foo");
|
|
|
|
|
|
|
|
nghttp2_map_init(&map, mem);
|
|
|
|
|
|
|
|
CU_ASSERT(0 == nghttp2_map_insert(&map, foo.key, &foo));
|
|
|
|
|
|
|
|
nghttp2_map_clear(&map);
|
|
|
|
|
|
|
|
CU_ASSERT(0 == nghttp2_map_size(&map));
|
2012-09-11 15:13:02 +00:00
|
|
|
|
2013-12-04 15:41:42 +00:00
|
|
|
nghttp2_map_free(&map);
|
2012-09-11 15:13:02 +00:00
|
|
|
}
|