2016-05-26 21:23:19 +02:00
|
|
|
/*
|
2017-09-27 23:45:52 +00:00
|
|
|
* Copyright (C) 2016, 2017 Internet Systems Consortium, Inc. ("ISC")
|
2016-05-26 21:23:19 +02:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2016-05-26 21:23:19 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* ! \file */
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <atf-c.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <isc/hash.h>
|
|
|
|
#include <isc/ht.h>
|
|
|
|
#include <isc/mem.h>
|
2016-05-26 12:36:17 -07:00
|
|
|
#include <isc/print.h>
|
2017-09-27 16:02:02 +10:00
|
|
|
#include <isc/string.h>
|
2016-05-26 21:23:19 +02:00
|
|
|
#include <isc/util.h>
|
|
|
|
|
2017-10-20 16:36:07 +11:00
|
|
|
#include <inttypes.h>
|
|
|
|
|
2016-05-26 21:23:19 +02:00
|
|
|
static void *
|
|
|
|
default_memalloc(void *arg, size_t size) {
|
|
|
|
UNUSED(arg);
|
|
|
|
if (size == 0U)
|
|
|
|
size = 1;
|
|
|
|
return (malloc(size));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
default_memfree(void *arg, void *ptr) {
|
|
|
|
UNUSED(arg);
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
2017-10-20 16:04:59 +11:00
|
|
|
static void test_ht_full(int bits, uintptr_t count) {
|
2016-05-26 21:23:19 +02:00
|
|
|
isc_ht_t *ht = NULL;
|
|
|
|
isc_result_t result;
|
|
|
|
isc_mem_t *mctx = NULL;
|
2017-10-20 16:04:59 +11:00
|
|
|
uintptr_t i;
|
2016-05-26 21:23:19 +02:00
|
|
|
|
|
|
|
result = isc_mem_createx2(0, 0, default_memalloc, default_memfree,
|
|
|
|
NULL, &mctx, 0);
|
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
|
|
|
|
2017-10-19 13:08:31 +11:00
|
|
|
result = isc_ht_init(&ht, mctx, bits);
|
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
|
|
|
ATF_REQUIRE(ht != NULL);
|
|
|
|
|
2016-05-26 21:23:19 +02:00
|
|
|
for (i = 1; i < count; i++) {
|
|
|
|
/*
|
2017-09-27 16:02:02 +10:00
|
|
|
* Note: snprintf() is followed with strlcat()
|
|
|
|
* to ensure we are always filling the 16 byte key.
|
2016-05-26 21:23:19 +02:00
|
|
|
*/
|
|
|
|
unsigned char key[16];
|
2017-09-27 17:57:07 +10:00
|
|
|
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
2017-09-27 16:02:02 +10:00
|
|
|
strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
|
2016-05-26 21:23:19 +02:00
|
|
|
result = isc_ht_add(ht, key, 16, (void *) i);
|
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i < count; i++) {
|
|
|
|
unsigned char key[16];
|
|
|
|
void *f = NULL;
|
2017-09-27 17:57:07 +10:00
|
|
|
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
2017-09-27 16:02:02 +10:00
|
|
|
strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
|
2016-05-26 21:23:19 +02:00
|
|
|
result = isc_ht_find(ht, key, 16, &f);
|
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
2017-10-20 16:04:59 +11:00
|
|
|
ATF_REQUIRE_EQ(i, (uintptr_t) f);
|
2016-05-26 21:23:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i < count; i++) {
|
|
|
|
unsigned char key[16];
|
2017-09-27 17:57:07 +10:00
|
|
|
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
2017-09-27 16:02:02 +10:00
|
|
|
strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
|
2016-05-26 21:23:19 +02:00
|
|
|
result = isc_ht_add(ht, key, 16, (void *) i);
|
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_EXISTS);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i < count; i++) {
|
|
|
|
char key[64];
|
2017-09-27 16:02:02 +10:00
|
|
|
/*
|
|
|
|
* Note: the key size is now strlen(key) which is bigger
|
|
|
|
* then the keys added above.
|
|
|
|
*/
|
2017-09-27 17:57:07 +10:00
|
|
|
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
2017-09-27 16:02:02 +10:00
|
|
|
strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
|
2016-05-26 21:23:19 +02:00
|
|
|
result = isc_ht_add(ht, (const unsigned char *) key,
|
|
|
|
strlen(key), (void *) i);
|
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i < count; i++) {
|
|
|
|
unsigned char key[16];
|
|
|
|
void *f = NULL;
|
2017-09-27 16:02:02 +10:00
|
|
|
/*
|
|
|
|
* Note: case of KEY is now in capitals,
|
|
|
|
*/
|
2017-09-27 17:57:07 +10:00
|
|
|
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
2017-09-27 16:02:02 +10:00
|
|
|
strlcat((char *)key, " KEY of a raw hashtable!!", sizeof(key));
|
2016-05-26 21:23:19 +02:00
|
|
|
result = isc_ht_find(ht, key, 16, &f);
|
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_NOTFOUND);
|
|
|
|
ATF_REQUIRE_EQ(f, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i < count; i++) {
|
|
|
|
char key[64];
|
|
|
|
void *f = NULL;
|
2017-09-27 17:57:07 +10:00
|
|
|
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
2017-09-27 16:02:02 +10:00
|
|
|
strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
|
2016-05-26 21:23:19 +02:00
|
|
|
result = isc_ht_find(ht, (const unsigned char *) key,
|
|
|
|
strlen(key), &f);
|
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
|
|
|
ATF_REQUIRE_EQ(f, (void *) i);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i < count; i++) {
|
|
|
|
unsigned char key[16];
|
|
|
|
void *f = NULL;
|
2017-09-27 17:57:07 +10:00
|
|
|
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
2017-09-27 16:02:02 +10:00
|
|
|
strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
|
2016-05-26 21:23:19 +02:00
|
|
|
result = isc_ht_delete(ht, key, 16);
|
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
|
|
|
result = isc_ht_find(ht, key, 16, &f);
|
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_NOTFOUND);
|
|
|
|
ATF_REQUIRE_EQ(f, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i < count; i++) {
|
|
|
|
unsigned char key[16];
|
2017-09-27 16:02:02 +10:00
|
|
|
/*
|
|
|
|
* Note: upper case KEY.
|
|
|
|
*/
|
2017-09-27 17:57:07 +10:00
|
|
|
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
2017-09-27 16:02:02 +10:00
|
|
|
strlcat((char *)key, " KEY of a raw hashtable!!", sizeof(key));
|
2016-05-26 21:23:19 +02:00
|
|
|
result = isc_ht_add(ht, key, 16, (void *) i);
|
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i < count; i++) {
|
|
|
|
char key[64];
|
|
|
|
void *f = NULL;
|
2017-09-27 17:57:07 +10:00
|
|
|
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
2017-09-27 16:02:02 +10:00
|
|
|
strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
|
2016-05-26 21:23:19 +02:00
|
|
|
result = isc_ht_delete(ht, (const unsigned char *) key,
|
|
|
|
strlen(key));
|
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
|
|
|
result = isc_ht_find(ht, (const unsigned char *) key,
|
|
|
|
strlen(key), &f);
|
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_NOTFOUND);
|
|
|
|
ATF_REQUIRE_EQ(f, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (i = 1; i < count; i++) {
|
|
|
|
unsigned char key[16];
|
|
|
|
void *f = NULL;
|
2017-09-27 16:02:02 +10:00
|
|
|
/*
|
|
|
|
* Note: case of KEY is now in capitals,
|
|
|
|
*/
|
2017-09-27 17:57:07 +10:00
|
|
|
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
2017-09-27 16:02:02 +10:00
|
|
|
strlcat((char *)key, " KEY of a raw hashtable!!", sizeof(key));
|
2016-05-26 21:23:19 +02:00
|
|
|
result = isc_ht_find(ht, key, 16, &f);
|
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
2017-10-20 16:04:59 +11:00
|
|
|
ATF_REQUIRE_EQ(i, (uintptr_t) f);
|
2016-05-26 21:23:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i < count; i++) {
|
|
|
|
unsigned char key[16];
|
|
|
|
void *f = NULL;
|
2017-09-27 17:57:07 +10:00
|
|
|
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
2017-09-27 16:02:02 +10:00
|
|
|
strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
|
2016-05-26 21:23:19 +02:00
|
|
|
result = isc_ht_find(ht, key, 16, &f);
|
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_NOTFOUND);
|
|
|
|
ATF_REQUIRE_EQ(f, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_ht_destroy(&ht);
|
|
|
|
ATF_REQUIRE_EQ(ht, NULL);
|
|
|
|
}
|
|
|
|
|
2016-05-31 23:01:53 +02:00
|
|
|
static void test_ht_iterator() {
|
2016-05-26 21:23:19 +02:00
|
|
|
isc_ht_t *ht = NULL;
|
|
|
|
isc_result_t result;
|
|
|
|
isc_mem_t *mctx = NULL;
|
2016-05-31 23:01:53 +02:00
|
|
|
isc_ht_iter_t * iter = NULL;
|
2017-10-20 16:04:59 +11:00
|
|
|
uintptr_t i;
|
|
|
|
void *v;
|
|
|
|
uintptr_t count = 10000;
|
2016-05-31 23:01:53 +02:00
|
|
|
isc_uint32_t walked;
|
|
|
|
unsigned char key[16];
|
|
|
|
unsigned char *tkey;
|
|
|
|
size_t tksize;
|
2016-05-26 21:23:19 +02:00
|
|
|
|
|
|
|
result = isc_mem_createx2(0, 0, default_memalloc, default_memfree,
|
|
|
|
NULL, &mctx, 0);
|
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
result = isc_ht_init(&ht, mctx, 16);
|
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
|
|
|
ATF_REQUIRE(ht != NULL);
|
|
|
|
for (i = 1; i <= count; i++) {
|
|
|
|
/*
|
|
|
|
* Note that the string we're snprintfing is always > 16 bytes
|
|
|
|
* so we are always filling the key.
|
|
|
|
*/
|
2017-09-27 17:57:07 +10:00
|
|
|
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
2017-09-27 16:02:02 +10:00
|
|
|
strlcat((char *)key, "key of a raw hashtable!!", sizeof(key));
|
2016-05-26 21:23:19 +02:00
|
|
|
result = isc_ht_add(ht, key, 16, (void *) i);
|
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
walked = 0;
|
2016-05-31 23:01:53 +02:00
|
|
|
result = isc_ht_iter_create(ht, &iter);
|
2016-05-26 21:23:19 +02:00
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
|
|
|
|
2016-05-31 23:01:53 +02:00
|
|
|
for (result = isc_ht_iter_first(iter);
|
|
|
|
result == ISC_R_SUCCESS;
|
|
|
|
result = isc_ht_iter_next(iter))
|
|
|
|
{
|
2017-10-20 16:04:59 +11:00
|
|
|
isc_ht_iter_current(iter, &v);
|
2016-05-31 23:01:53 +02:00
|
|
|
isc_ht_iter_currentkey(iter, &tkey, &tksize);
|
|
|
|
ATF_REQUIRE_EQ(tksize, 16);
|
2017-10-20 16:04:59 +11:00
|
|
|
i = (uintptr_t)v;
|
|
|
|
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
2017-09-27 16:02:02 +10:00
|
|
|
strlcat((char *)key, "key of a raw hashtable!!", sizeof(key));
|
2016-05-31 23:01:53 +02:00
|
|
|
ATF_REQUIRE_EQ(memcmp(key, tkey, 16), 0);
|
|
|
|
walked++;
|
|
|
|
}
|
|
|
|
ATF_REQUIRE_EQ(walked, count);
|
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_NOMORE);
|
2016-05-26 21:23:19 +02:00
|
|
|
|
2016-05-31 23:01:53 +02:00
|
|
|
/* erase odd */
|
2016-05-26 21:23:19 +02:00
|
|
|
walked = 0;
|
2016-05-31 23:01:53 +02:00
|
|
|
result = isc_ht_iter_first(iter);
|
|
|
|
while (result == ISC_R_SUCCESS) {
|
2017-10-20 16:04:59 +11:00
|
|
|
isc_ht_iter_current(iter, &v);
|
2016-05-31 23:01:53 +02:00
|
|
|
isc_ht_iter_currentkey(iter, &tkey, &tksize);
|
|
|
|
ATF_REQUIRE_EQ(tksize, 16);
|
2017-10-20 16:04:59 +11:00
|
|
|
i = (uintptr_t)v;
|
|
|
|
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
2017-09-27 16:02:02 +10:00
|
|
|
strlcat((char *)key, "key of a raw hashtable!!", sizeof(key));
|
2016-05-31 23:01:53 +02:00
|
|
|
ATF_REQUIRE_EQ(memcmp(key, tkey, 16), 0);
|
2017-10-20 16:04:59 +11:00
|
|
|
if ((uintptr_t)v % 2 == 0) {
|
2016-05-31 23:01:53 +02:00
|
|
|
result = isc_ht_iter_delcurrent_next(iter);
|
|
|
|
} else {
|
|
|
|
result = isc_ht_iter_next(iter);
|
|
|
|
}
|
|
|
|
walked++;
|
|
|
|
}
|
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_NOMORE);
|
2016-05-26 21:23:19 +02:00
|
|
|
ATF_REQUIRE_EQ(walked, count);
|
|
|
|
|
2016-05-31 23:01:53 +02:00
|
|
|
/* erase even */
|
2016-05-26 21:23:19 +02:00
|
|
|
walked = 0;
|
2016-05-31 23:01:53 +02:00
|
|
|
result = isc_ht_iter_first(iter);
|
|
|
|
while (result == ISC_R_SUCCESS) {
|
2017-10-20 16:04:59 +11:00
|
|
|
isc_ht_iter_current(iter, &v);
|
2016-05-31 23:01:53 +02:00
|
|
|
isc_ht_iter_currentkey(iter, &tkey, &tksize);
|
|
|
|
ATF_REQUIRE_EQ(tksize, 16);
|
2017-10-20 16:04:59 +11:00
|
|
|
i = (uintptr_t)v;
|
|
|
|
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
2017-09-27 16:02:02 +10:00
|
|
|
strlcat((char *)key, "key of a raw hashtable!!", sizeof(key));
|
2016-05-31 23:01:53 +02:00
|
|
|
ATF_REQUIRE_EQ(memcmp(key, tkey, 16), 0);
|
2017-10-20 16:04:59 +11:00
|
|
|
if ((uintptr_t)v % 2 == 1) {
|
2016-05-31 23:01:53 +02:00
|
|
|
result = isc_ht_iter_delcurrent_next(iter);
|
|
|
|
} else {
|
|
|
|
result = isc_ht_iter_next(iter);
|
|
|
|
}
|
|
|
|
walked++;
|
|
|
|
}
|
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_NOMORE);
|
2016-05-26 21:23:19 +02:00
|
|
|
ATF_REQUIRE_EQ(walked, count/2);
|
|
|
|
|
|
|
|
walked = 0;
|
2016-05-31 23:01:53 +02:00
|
|
|
for (result = isc_ht_iter_first(iter);
|
|
|
|
result == ISC_R_SUCCESS;
|
|
|
|
result = isc_ht_iter_next(iter))
|
|
|
|
{
|
|
|
|
walked++;
|
|
|
|
}
|
|
|
|
|
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_NOMORE);
|
2016-05-26 21:23:19 +02:00
|
|
|
ATF_REQUIRE_EQ(walked, 0);
|
|
|
|
|
|
|
|
isc_ht_destroy(&ht);
|
|
|
|
ATF_REQUIRE_EQ(ht, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
ATF_TC(isc_ht_20);
|
|
|
|
ATF_TC_HEAD(isc_ht_20, tc) {
|
|
|
|
atf_tc_set_md_var(tc, "descr", "20 bit, 2M elements test");
|
|
|
|
}
|
|
|
|
|
|
|
|
ATF_TC_BODY(isc_ht_20, tc) {
|
|
|
|
UNUSED(tc);
|
|
|
|
test_ht_full(20, 2000000);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ATF_TC(isc_ht_8);
|
|
|
|
ATF_TC_HEAD(isc_ht_8, tc) {
|
|
|
|
atf_tc_set_md_var(tc, "descr", "8 bit, 20000 elements crowded test");
|
|
|
|
}
|
|
|
|
|
|
|
|
ATF_TC_BODY(isc_ht_8, tc) {
|
|
|
|
UNUSED(tc);
|
|
|
|
test_ht_full(8, 20000);
|
|
|
|
}
|
|
|
|
|
|
|
|
ATF_TC(isc_ht_1);
|
|
|
|
ATF_TC_HEAD(isc_ht_1, tc) {
|
|
|
|
atf_tc_set_md_var(tc, "descr", "1 bit, 100 elements corner case test");
|
|
|
|
}
|
|
|
|
|
|
|
|
ATF_TC_BODY(isc_ht_1, tc) {
|
|
|
|
UNUSED(tc);
|
|
|
|
test_ht_full(1, 100);
|
|
|
|
}
|
|
|
|
|
2016-06-01 15:19:35 +02:00
|
|
|
/* xxxwpk we should limit the size of hashtable, 32bit doesn't make sense */
|
|
|
|
#if 0
|
2016-05-26 21:23:19 +02:00
|
|
|
ATF_TC(isc_ht_32);
|
|
|
|
ATF_TC_HEAD(isc_ht_32, tc) {
|
|
|
|
atf_tc_set_md_var(tc, "descr", "32 bit, 10000 elements corner case test");
|
|
|
|
}
|
|
|
|
|
|
|
|
ATF_TC_BODY(isc_ht_32, tc) {
|
|
|
|
UNUSED(tc);
|
|
|
|
test_ht_full(32, 10000);
|
|
|
|
}
|
2016-06-01 15:19:35 +02:00
|
|
|
#endif
|
2016-05-26 21:23:19 +02:00
|
|
|
|
2016-05-31 23:01:53 +02:00
|
|
|
ATF_TC(isc_ht_iterator);
|
|
|
|
ATF_TC_HEAD(isc_ht_iterator, tc) {
|
|
|
|
atf_tc_set_md_var(tc, "descr", "hashtable iterator");
|
2016-05-26 21:23:19 +02:00
|
|
|
}
|
|
|
|
|
2016-05-31 23:01:53 +02:00
|
|
|
ATF_TC_BODY(isc_ht_iterator, tc) {
|
2016-05-26 21:23:19 +02:00
|
|
|
UNUSED(tc);
|
2016-05-31 23:01:53 +02:00
|
|
|
test_ht_iterator();
|
2016-05-26 21:23:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Main
|
|
|
|
*/
|
|
|
|
ATF_TP_ADD_TCS(tp) {
|
|
|
|
ATF_TP_ADD_TC(tp, isc_ht_20);
|
|
|
|
ATF_TP_ADD_TC(tp, isc_ht_8);
|
|
|
|
ATF_TP_ADD_TC(tp, isc_ht_1);
|
2016-06-01 15:19:35 +02:00
|
|
|
/* ATF_TP_ADD_TC(tp, isc_ht_32); */
|
2016-05-31 23:01:53 +02:00
|
|
|
ATF_TP_ADD_TC(tp, isc_ht_iterator);
|
2016-05-26 21:23:19 +02:00
|
|
|
return (atf_no_error());
|
|
|
|
}
|