2012-01-19 13:36:13 +01:00
|
|
|
/*
|
2014-03-30 12:09:21 +02:00
|
|
|
* nghttp2 - HTTP/2 C Library
|
2012-01-19 13:36:13 +01:00
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* 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 17:19:03 +02:00
|
|
|
#include "nghttp2_map_test.h"
|
2012-01-19 13:36:13 +01:00
|
|
|
|
|
|
|
#include <CUnit/CUnit.h>
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
#include "nghttp2_map.h"
|
2012-01-19 13:36:13 +01:00
|
|
|
|
2012-09-11 17:13:02 +02:00
|
|
|
typedef struct strentry {
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_map_entry map_entry;
|
2012-09-11 17:13:02 +02:00
|
|
|
const char *str;
|
|
|
|
} strentry;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static void strentry_init(strentry *entry, key_type key, const char *str) {
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_map_entry_init(&entry->map_entry, key);
|
2012-09-11 17:13:02 +02:00
|
|
|
entry->str = str;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void test_nghttp2_map(void) {
|
2012-09-11 17:13:02 +02:00
|
|
|
strentry foo, FOO, bar, baz, shrubbery;
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_map map;
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_map_init(&map, nghttp2_mem_default());
|
2012-09-11 17:13:02 +02: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");
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
CU_ASSERT(0 == nghttp2_map_insert(&map, &foo.map_entry));
|
2014-11-27 15:39:04 +01:00
|
|
|
CU_ASSERT(strcmp("foo", ((strentry *)nghttp2_map_find(&map, 1))->str) == 0);
|
2013-07-12 17:19:03 +02:00
|
|
|
CU_ASSERT(1 == nghttp2_map_size(&map));
|
2012-09-11 17:13:02 +02:00
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
CU_ASSERT(NGHTTP2_ERR_INVALID_ARGUMENT ==
|
|
|
|
nghttp2_map_insert(&map, &FOO.map_entry));
|
2012-09-11 17:13:02 +02:00
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
CU_ASSERT(1 == nghttp2_map_size(&map));
|
2014-11-27 15:39:04 +01:00
|
|
|
CU_ASSERT(strcmp("foo", ((strentry *)nghttp2_map_find(&map, 1))->str) == 0);
|
2012-09-11 17:13:02 +02:00
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
CU_ASSERT(0 == nghttp2_map_insert(&map, &bar.map_entry));
|
|
|
|
CU_ASSERT(2 == nghttp2_map_size(&map));
|
2012-09-11 17:13:02 +02:00
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
CU_ASSERT(0 == nghttp2_map_insert(&map, &baz.map_entry));
|
|
|
|
CU_ASSERT(3 == nghttp2_map_size(&map));
|
2012-09-11 17:13:02 +02:00
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
CU_ASSERT(0 == nghttp2_map_insert(&map, &shrubbery.map_entry));
|
|
|
|
CU_ASSERT(4 == nghttp2_map_size(&map));
|
2012-01-19 13:36:13 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
CU_ASSERT(strcmp("baz", ((strentry *)nghttp2_map_find(&map, 3))->str) == 0);
|
2012-09-11 17:13:02 +02:00
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_map_remove(&map, 3);
|
|
|
|
CU_ASSERT(3 == nghttp2_map_size(&map));
|
|
|
|
CU_ASSERT(NULL == nghttp2_map_find(&map, 3));
|
2012-09-11 17:13:02 +02:00
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_map_remove(&map, 1);
|
|
|
|
CU_ASSERT(2 == nghttp2_map_size(&map));
|
|
|
|
CU_ASSERT(NULL == nghttp2_map_find(&map, 1));
|
2012-01-19 13:36:13 +01:00
|
|
|
|
2012-01-25 18:28:58 +01:00
|
|
|
/* Erasing non-existent entry */
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_map_remove(&map, 1);
|
|
|
|
CU_ASSERT(2 == nghttp2_map_size(&map));
|
|
|
|
CU_ASSERT(NULL == nghttp2_map_find(&map, 1));
|
2012-01-25 16:21:31 +01:00
|
|
|
|
2014-11-27 15:39:04 +01: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 13:36:13 +01:00
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_map_free(&map);
|
2012-01-19 13:36:13 +01:00
|
|
|
}
|
2012-09-11 17:13:02 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static void shuffle(int *a, int n) {
|
2012-10-04 14:24:45 +02:00
|
|
|
int i;
|
2014-11-27 15:39:04 +01:00
|
|
|
for (i = n - 1; i >= 1; --i) {
|
2015-09-23 07:41:53 +02:00
|
|
|
size_t j = (size_t)((double)(i + 1) * rand() / (RAND_MAX + 1.0));
|
2012-10-04 14:24:45 +02:00
|
|
|
int t = a[j];
|
|
|
|
a[j] = a[i];
|
|
|
|
a[i] = t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-11 09:46:39 +01:00
|
|
|
static int eachfun(nghttp2_map_entry *entry, void *ptr) {
|
|
|
|
(void)entry;
|
|
|
|
(void)ptr;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-10-04 14:24:45 +02:00
|
|
|
|
|
|
|
#define NUM_ENT 6000
|
2015-09-23 07:41:53 +02:00
|
|
|
static strentry arr[NUM_ENT];
|
|
|
|
static int order[NUM_ENT];
|
2012-10-04 14:24:45 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void test_nghttp2_map_functional(void) {
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_map map;
|
2012-10-04 14:24:45 +02:00
|
|
|
int i;
|
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_map_init(&map, nghttp2_mem_default());
|
2014-11-27 15:39:04 +01:00
|
|
|
for (i = 0; i < NUM_ENT; ++i) {
|
2012-10-04 14:24:45 +02:00
|
|
|
strentry_init(&arr[i], i + 1, "foo");
|
|
|
|
order[i] = i + 1;
|
|
|
|
}
|
2012-11-14 13:00:36 +01:00
|
|
|
/* insertion */
|
2012-10-04 14:24:45 +02:00
|
|
|
shuffle(order, NUM_ENT);
|
2014-11-27 15:39:04 +01:00
|
|
|
for (i = 0; i < NUM_ENT; ++i) {
|
2013-07-12 17:19:03 +02:00
|
|
|
CU_ASSERT(0 == nghttp2_map_insert(&map, &arr[order[i] - 1].map_entry));
|
2012-10-04 14:24:45 +02:00
|
|
|
}
|
2012-11-14 13:00:36 +01:00
|
|
|
/* traverse */
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_map_each(&map, eachfun, NULL);
|
2012-11-14 13:00:36 +01:00
|
|
|
/* find */
|
2012-10-04 14:24:45 +02:00
|
|
|
shuffle(order, NUM_ENT);
|
2014-11-27 15:39:04 +01:00
|
|
|
for (i = 0; i < NUM_ENT; ++i) {
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_map_find(&map, order[i]);
|
2012-10-04 14:24:45 +02:00
|
|
|
}
|
2012-11-14 13:00:36 +01:00
|
|
|
/* remove */
|
2012-10-04 14:24:45 +02:00
|
|
|
shuffle(order, NUM_ENT);
|
2014-11-27 15:39:04 +01:00
|
|
|
for (i = 0; i < NUM_ENT; ++i) {
|
2013-07-12 17:19:03 +02:00
|
|
|
CU_ASSERT(0 == nghttp2_map_remove(&map, order[i]));
|
2012-10-04 14:24:45 +02:00
|
|
|
}
|
|
|
|
|
2012-11-14 13:00:36 +01:00
|
|
|
/* each_free (but no op function for testing purpose) */
|
2014-11-27 15:39:04 +01:00
|
|
|
for (i = 0; i < NUM_ENT; ++i) {
|
2012-10-05 16:41:49 +02:00
|
|
|
strentry_init(&arr[i], i + 1, "foo");
|
|
|
|
}
|
2012-11-14 13:00:36 +01:00
|
|
|
/* insert once again */
|
2014-11-27 15:39:04 +01:00
|
|
|
for (i = 0; i < NUM_ENT; ++i) {
|
2013-07-12 17:19:03 +02:00
|
|
|
CU_ASSERT(0 == nghttp2_map_insert(&map, &arr[i].map_entry));
|
2012-10-05 16:41:49 +02:00
|
|
|
}
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_map_each_free(&map, eachfun, NULL);
|
2013-12-04 16:41:42 +01:00
|
|
|
nghttp2_map_free(&map);
|
2012-10-04 14:24:45 +02:00
|
|
|
}
|
|
|
|
|
2015-03-03 15:23:43 +01:00
|
|
|
static int entry_free(nghttp2_map_entry *entry, void *ptr) {
|
|
|
|
nghttp2_mem *mem = ptr;
|
|
|
|
|
|
|
|
mem->free(entry, NULL);
|
2012-09-11 17:13:02 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void test_nghttp2_map_each_free(void) {
|
2015-03-03 15:23:43 +01:00
|
|
|
nghttp2_mem *mem = nghttp2_mem_default();
|
|
|
|
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 17:19:03 +02:00
|
|
|
nghttp2_map map;
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_map_init(&map, nghttp2_mem_default());
|
2012-09-11 17:13:02 +02:00
|
|
|
|
|
|
|
strentry_init(foo, 1, "foo");
|
|
|
|
strentry_init(bar, 2, "bar");
|
|
|
|
strentry_init(baz, 3, "baz");
|
|
|
|
strentry_init(shrubbery, 4, "shrubbery");
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_map_insert(&map, &foo->map_entry);
|
|
|
|
nghttp2_map_insert(&map, &bar->map_entry);
|
|
|
|
nghttp2_map_insert(&map, &baz->map_entry);
|
|
|
|
nghttp2_map_insert(&map, &shrubbery->map_entry);
|
2012-09-11 17:13:02 +02:00
|
|
|
|
2015-03-03 15:23:43 +01:00
|
|
|
nghttp2_map_each_free(&map, entry_free, mem);
|
2013-12-04 16:41:42 +01:00
|
|
|
nghttp2_map_free(&map);
|
2012-09-11 17:13:02 +02:00
|
|
|
}
|