* 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>
static void *
default_memalloc(void *arg, size_t size) {
UNUSED(arg);
- if (size == 0U)
+ if (size == 0U) {
size = 1;
+ }
return (malloc(size));
}
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;
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++) {
/*
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++) {
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++) {
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++) {
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++) {
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++) {
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++) {
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++) {
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++) {
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);
}
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++) {
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;
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
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;
{
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;
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 {
}
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;
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 {
}
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);
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);
-ATF_TC(isc_ht_20);
-ATF_TC_HEAD(isc_ht_20, tc) {
- atf_tc_set_md_var(tc, "descr", "20 bit, 200K elements test");
+ isc_mem_detach(&mctx);
}
-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");
+/* test hashtable iterator */
+static void
+isc_ht_iterator_test(void **state) {
+ UNUSED(state);
+ test_ht_iterator();
}
-ATF_TC_BODY(isc_ht_32, tc) {
- UNUSED(tc);
- test_ht_full(32, 10000);
-}
-#endif
+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),
+ };
-ATF_TC(isc_ht_iterator);
-ATF_TC_HEAD(isc_ht_iterator, tc) {
- atf_tc_set_md_var(tc, "descr", "hashtable iterator");
+ return (cmocka_run_group_tests(tests, NULL, NULL));
}
-ATF_TC_BODY(isc_ht_iterator, tc) {
- UNUSED(tc);
- test_ht_iterator();
-}
+#else /* HAVE_CMOCKA */
-/*
- * 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());
+#include <stdio.h>
+
+int
+main(void) {
+ printf("1..0 # Skipped: cmocka not available\n");
+ return (0);
}
+
+#endif