mirror of
https://github.com/isc-projects/bind9.git
synced 2026-05-28 04:34:54 -04:00
convert ht_test
This commit is contained in:
parent
3bf9a1e567
commit
c701f036ea
3 changed files with 108 additions and 105 deletions
|
|
@ -9,7 +9,7 @@ tap_test_program{name='file_test'}
|
|||
atf_test_program{name='hash_test'}
|
||||
tap_test_program{name='heap_test'}
|
||||
tap_test_program{name='hmac_test'}
|
||||
atf_test_program{name='ht_test'}
|
||||
tap_test_program{name='ht_test'}
|
||||
tap_test_program{name='lex_test'}
|
||||
tap_test_program{name='md_test'}
|
||||
atf_test_program{name='mem_test'}
|
||||
|
|
|
|||
|
|
@ -96,8 +96,9 @@ hmac_test@EXEEXT@: hmac_test.@O@ ${ISCDEPLIBS}
|
|||
hmac_test.@O@ ${ISCLIBS} ${LIBS} ${CMOCKA_LIBS}
|
||||
|
||||
ht_test@EXEEXT@: ht_test.@O@ ${ISCDEPLIBS}
|
||||
${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \
|
||||
ht_test.@O@ ${ISCLIBS} ${LIBS}
|
||||
${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${CMOCKA_CFLAGS} \
|
||||
${LDFLAGS} -o $@ ht_test.@O@ \
|
||||
${ISCLIBS} ${LIBS} ${CMOCKA_LIBS}
|
||||
|
||||
lex_test@EXEEXT@: lex_test.@O@ ${ISCDEPLIBS}
|
||||
${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${CMOCKA_CFLAGS} \
|
||||
|
|
|
|||
|
|
@ -9,16 +9,21 @@
|
|||
* information regarding copyright ownership.
|
||||
*/
|
||||
|
||||
/* ! \file */
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <atf-c.h>
|
||||
#if HAVE_CMOCKA
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <setjmp.h>
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define UNIT_TESTING
|
||||
#include <cmocka.h>
|
||||
|
||||
#include <isc/hash.h>
|
||||
#include <isc/ht.h>
|
||||
|
|
@ -30,8 +35,9 @@
|
|||
static void *
|
||||
default_memalloc(void *arg, size_t size) {
|
||||
UNUSED(arg);
|
||||
if (size == 0U)
|
||||
if (size == 0U) {
|
||||
size = 1;
|
||||
}
|
||||
return (malloc(size));
|
||||
}
|
||||
|
||||
|
|
@ -41,7 +47,8 @@ default_memfree(void *arg, void *ptr) {
|
|||
free(ptr);
|
||||
}
|
||||
|
||||
static void test_ht_full(int bits, uintptr_t count) {
|
||||
static void
|
||||
test_ht_full(int bits, uintptr_t count) {
|
||||
isc_ht_t *ht = NULL;
|
||||
isc_result_t result;
|
||||
isc_mem_t *mctx = NULL;
|
||||
|
|
@ -49,11 +56,11 @@ static void test_ht_full(int bits, uintptr_t count) {
|
|||
|
||||
result = isc_mem_createx(0, 0, default_memalloc, default_memfree,
|
||||
NULL, &mctx, 0);
|
||||
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
|
||||
result = isc_ht_init(&ht, mctx, bits);
|
||||
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
||||
ATF_REQUIRE(ht != NULL);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
assert_non_null(ht);
|
||||
|
||||
for (i = 1; i < count; i++) {
|
||||
/*
|
||||
|
|
@ -64,7 +71,7 @@ static void test_ht_full(int bits, uintptr_t count) {
|
|||
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
||||
strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
|
||||
result = isc_ht_add(ht, key, 16, (void *) i);
|
||||
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
}
|
||||
|
||||
for (i = 1; i < count; i++) {
|
||||
|
|
@ -73,8 +80,8 @@ static void test_ht_full(int bits, uintptr_t count) {
|
|||
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
||||
strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
|
||||
result = isc_ht_find(ht, key, 16, &f);
|
||||
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
||||
ATF_REQUIRE_EQ(i, (uintptr_t) f);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
assert_ptr_equal((void *) i, (uintptr_t) f);
|
||||
}
|
||||
|
||||
for (i = 1; i < count; i++) {
|
||||
|
|
@ -82,7 +89,7 @@ static void test_ht_full(int bits, uintptr_t count) {
|
|||
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
||||
strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
|
||||
result = isc_ht_add(ht, key, 16, (void *) i);
|
||||
ATF_REQUIRE_EQ(result, ISC_R_EXISTS);
|
||||
assert_int_equal(result, ISC_R_EXISTS);
|
||||
}
|
||||
|
||||
for (i = 1; i < count; i++) {
|
||||
|
|
@ -95,7 +102,7 @@ static void test_ht_full(int bits, uintptr_t count) {
|
|||
strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
|
||||
result = isc_ht_add(ht, (const unsigned char *) key,
|
||||
strlen(key), (void *) i);
|
||||
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
}
|
||||
|
||||
for (i = 1; i < count; i++) {
|
||||
|
|
@ -107,8 +114,8 @@ static void test_ht_full(int bits, uintptr_t count) {
|
|||
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
||||
strlcat((char *)key, " KEY of a raw hashtable!!", sizeof(key));
|
||||
result = isc_ht_find(ht, key, 16, &f);
|
||||
ATF_REQUIRE_EQ(result, ISC_R_NOTFOUND);
|
||||
ATF_REQUIRE_EQ(f, NULL);
|
||||
assert_int_equal(result, ISC_R_NOTFOUND);
|
||||
assert_null(f);
|
||||
}
|
||||
|
||||
for (i = 1; i < count; i++) {
|
||||
|
|
@ -118,8 +125,8 @@ static void test_ht_full(int bits, uintptr_t count) {
|
|||
strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
|
||||
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);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
assert_ptr_equal(f, (void *) i);
|
||||
}
|
||||
|
||||
for (i = 1; i < count; i++) {
|
||||
|
|
@ -128,10 +135,10 @@ static void test_ht_full(int bits, uintptr_t count) {
|
|||
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
||||
strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
|
||||
result = isc_ht_delete(ht, key, 16);
|
||||
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
result = isc_ht_find(ht, key, 16, &f);
|
||||
ATF_REQUIRE_EQ(result, ISC_R_NOTFOUND);
|
||||
ATF_REQUIRE_EQ(f, NULL);
|
||||
assert_int_equal(result, ISC_R_NOTFOUND);
|
||||
assert_null(f);
|
||||
}
|
||||
|
||||
for (i = 1; i < count; i++) {
|
||||
|
|
@ -142,7 +149,7 @@ static void test_ht_full(int bits, uintptr_t count) {
|
|||
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
||||
strlcat((char *)key, " KEY of a raw hashtable!!", sizeof(key));
|
||||
result = isc_ht_add(ht, key, 16, (void *) i);
|
||||
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
}
|
||||
|
||||
for (i = 1; i < count; i++) {
|
||||
|
|
@ -152,11 +159,11 @@ static void test_ht_full(int bits, uintptr_t count) {
|
|||
strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
|
||||
result = isc_ht_delete(ht, (const unsigned char *) key,
|
||||
strlen(key));
|
||||
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
||||
assert_int_equal(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);
|
||||
assert_int_equal(result, ISC_R_NOTFOUND);
|
||||
assert_null(f);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -169,8 +176,8 @@ static void test_ht_full(int bits, uintptr_t count) {
|
|||
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
||||
strlcat((char *)key, " KEY of a raw hashtable!!", sizeof(key));
|
||||
result = isc_ht_find(ht, key, 16, &f);
|
||||
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
||||
ATF_REQUIRE_EQ(i, (uintptr_t) f);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
assert_ptr_equal((void *) i, (uintptr_t) f);
|
||||
}
|
||||
|
||||
for (i = 1; i < count; i++) {
|
||||
|
|
@ -179,15 +186,18 @@ static void test_ht_full(int bits, uintptr_t count) {
|
|||
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
||||
strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
|
||||
result = isc_ht_find(ht, key, 16, &f);
|
||||
ATF_REQUIRE_EQ(result, ISC_R_NOTFOUND);
|
||||
ATF_REQUIRE_EQ(f, NULL);
|
||||
assert_int_equal(result, ISC_R_NOTFOUND);
|
||||
assert_null(f);
|
||||
}
|
||||
|
||||
isc_ht_destroy(&ht);
|
||||
ATF_REQUIRE_EQ(ht, NULL);
|
||||
assert_null(ht);
|
||||
|
||||
isc_mem_detach(&mctx);
|
||||
}
|
||||
|
||||
static void test_ht_iterator() {
|
||||
static void
|
||||
test_ht_iterator() {
|
||||
isc_ht_t *ht = NULL;
|
||||
isc_result_t result;
|
||||
isc_mem_t *mctx = NULL;
|
||||
|
|
@ -202,11 +212,11 @@ static void test_ht_iterator() {
|
|||
|
||||
result = isc_mem_createx(0, 0, default_memalloc, default_memfree,
|
||||
NULL, &mctx, 0);
|
||||
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
|
||||
result = isc_ht_init(&ht, mctx, 16);
|
||||
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
||||
ATF_REQUIRE(ht != NULL);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
assert_non_null(ht);
|
||||
for (i = 1; i <= count; i++) {
|
||||
/*
|
||||
* Note that the string we're snprintfing is always > 16 bytes
|
||||
|
|
@ -215,12 +225,12 @@ static void test_ht_iterator() {
|
|||
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
||||
strlcat((char *)key, "key of a raw hashtable!!", sizeof(key));
|
||||
result = isc_ht_add(ht, key, 16, (void *) i);
|
||||
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
}
|
||||
|
||||
walked = 0;
|
||||
result = isc_ht_iter_create(ht, &iter);
|
||||
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
|
||||
for (result = isc_ht_iter_first(iter);
|
||||
result == ISC_R_SUCCESS;
|
||||
|
|
@ -228,15 +238,15 @@ static void test_ht_iterator() {
|
|||
{
|
||||
isc_ht_iter_current(iter, &v);
|
||||
isc_ht_iter_currentkey(iter, &tkey, &tksize);
|
||||
ATF_REQUIRE_EQ(tksize, 16);
|
||||
assert_int_equal(tksize, 16);
|
||||
i = (uintptr_t)v;
|
||||
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
||||
strlcat((char *)key, "key of a raw hashtable!!", sizeof(key));
|
||||
ATF_REQUIRE_EQ(memcmp(key, tkey, 16), 0);
|
||||
assert_memory_equal(key, tkey, 16);
|
||||
walked++;
|
||||
}
|
||||
ATF_REQUIRE_EQ(walked, count);
|
||||
ATF_REQUIRE_EQ(result, ISC_R_NOMORE);
|
||||
assert_int_equal(walked, count);
|
||||
assert_int_equal(result, ISC_R_NOMORE);
|
||||
|
||||
/* erase odd */
|
||||
walked = 0;
|
||||
|
|
@ -244,11 +254,11 @@ static void test_ht_iterator() {
|
|||
while (result == ISC_R_SUCCESS) {
|
||||
isc_ht_iter_current(iter, &v);
|
||||
isc_ht_iter_currentkey(iter, &tkey, &tksize);
|
||||
ATF_REQUIRE_EQ(tksize, 16);
|
||||
assert_int_equal(tksize, 16);
|
||||
i = (uintptr_t)v;
|
||||
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
||||
strlcat((char *)key, "key of a raw hashtable!!", sizeof(key));
|
||||
ATF_REQUIRE_EQ(memcmp(key, tkey, 16), 0);
|
||||
assert_memory_equal(key, tkey, 16);
|
||||
if ((uintptr_t)v % 2 == 0) {
|
||||
result = isc_ht_iter_delcurrent_next(iter);
|
||||
} else {
|
||||
|
|
@ -256,8 +266,8 @@ static void test_ht_iterator() {
|
|||
}
|
||||
walked++;
|
||||
}
|
||||
ATF_REQUIRE_EQ(result, ISC_R_NOMORE);
|
||||
ATF_REQUIRE_EQ(walked, count);
|
||||
assert_int_equal(result, ISC_R_NOMORE);
|
||||
assert_int_equal(walked, count);
|
||||
|
||||
/* erase even */
|
||||
walked = 0;
|
||||
|
|
@ -265,11 +275,11 @@ static void test_ht_iterator() {
|
|||
while (result == ISC_R_SUCCESS) {
|
||||
isc_ht_iter_current(iter, &v);
|
||||
isc_ht_iter_currentkey(iter, &tkey, &tksize);
|
||||
ATF_REQUIRE_EQ(tksize, 16);
|
||||
assert_int_equal(tksize, 16);
|
||||
i = (uintptr_t)v;
|
||||
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
||||
strlcat((char *)key, "key of a raw hashtable!!", sizeof(key));
|
||||
ATF_REQUIRE_EQ(memcmp(key, tkey, 16), 0);
|
||||
assert_memory_equal(key, tkey, 16);
|
||||
if ((uintptr_t)v % 2 == 1) {
|
||||
result = isc_ht_iter_delcurrent_next(iter);
|
||||
} else {
|
||||
|
|
@ -277,8 +287,8 @@ static void test_ht_iterator() {
|
|||
}
|
||||
walked++;
|
||||
}
|
||||
ATF_REQUIRE_EQ(result, ISC_R_NOMORE);
|
||||
ATF_REQUIRE_EQ(walked, count/2);
|
||||
assert_int_equal(result, ISC_R_NOMORE);
|
||||
assert_int_equal(walked, count/2);
|
||||
|
||||
walked = 0;
|
||||
for (result = isc_ht_iter_first(iter);
|
||||
|
|
@ -288,75 +298,67 @@ static void test_ht_iterator() {
|
|||
walked++;
|
||||
}
|
||||
|
||||
ATF_REQUIRE_EQ(result, ISC_R_NOMORE);
|
||||
ATF_REQUIRE_EQ(walked, 0);
|
||||
assert_int_equal(result, ISC_R_NOMORE);
|
||||
assert_int_equal(walked, 0);
|
||||
|
||||
isc_ht_iter_destroy(&iter);
|
||||
assert_null(iter);
|
||||
|
||||
isc_ht_destroy(&ht);
|
||||
ATF_REQUIRE_EQ(ht, NULL);
|
||||
assert_null(ht);
|
||||
|
||||
isc_mem_detach(&mctx);
|
||||
}
|
||||
|
||||
ATF_TC(isc_ht_20);
|
||||
ATF_TC_HEAD(isc_ht_20, tc) {
|
||||
atf_tc_set_md_var(tc, "descr", "20 bit, 200K elements test");
|
||||
}
|
||||
|
||||
ATF_TC_BODY(isc_ht_20, tc) {
|
||||
UNUSED(tc);
|
||||
/* 20 bit, 200K elements test */
|
||||
static void
|
||||
isc_ht_20(void **state) {
|
||||
UNUSED(state);
|
||||
test_ht_full(20, 200000);
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
/* 8 bit, 20000 elements crowded test */
|
||||
static void
|
||||
isc_ht_8(void **state) {
|
||||
UNUSED(state);
|
||||
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);
|
||||
/* 8 bit, 100 elements corner case test */
|
||||
static void
|
||||
isc_ht_1(void **state) {
|
||||
UNUSED(state);
|
||||
test_ht_full(1, 100);
|
||||
}
|
||||
|
||||
/* xxxwpk we should limit the size of hashtable, 32bit doesn't make sense */
|
||||
#if 0
|
||||
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);
|
||||
}
|
||||
#endif
|
||||
|
||||
ATF_TC(isc_ht_iterator);
|
||||
ATF_TC_HEAD(isc_ht_iterator, tc) {
|
||||
atf_tc_set_md_var(tc, "descr", "hashtable iterator");
|
||||
}
|
||||
|
||||
ATF_TC_BODY(isc_ht_iterator, tc) {
|
||||
UNUSED(tc);
|
||||
/* test hashtable iterator */
|
||||
static void
|
||||
isc_ht_iterator_test(void **state) {
|
||||
UNUSED(state);
|
||||
test_ht_iterator();
|
||||
}
|
||||
|
||||
/*
|
||||
* 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);
|
||||
/* ATF_TP_ADD_TC(tp, isc_ht_32); */
|
||||
ATF_TP_ADD_TC(tp, isc_ht_iterator);
|
||||
return (atf_no_error());
|
||||
int
|
||||
main(void) {
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(isc_ht_20),
|
||||
cmocka_unit_test(isc_ht_8),
|
||||
cmocka_unit_test(isc_ht_1),
|
||||
cmocka_unit_test(isc_ht_iterator_test),
|
||||
};
|
||||
|
||||
return (cmocka_run_group_tests(tests, NULL, NULL));
|
||||
}
|
||||
|
||||
#else /* HAVE_CMOCKA */
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int
|
||||
main(void) {
|
||||
printf("1..0 # Skipped: cmocka not available\n");
|
||||
return (0);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Reference in a new issue