From 3862549ed9daf503b854b3851a0d105c611abb29 Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Sun, 12 Jul 2015 11:24:20 +0100 Subject: Add initial tests for hash table. --- test/Makefile | 20 ++++---- test/hashtable.c | 150 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 161 insertions(+), 9 deletions(-) create mode 100644 test/hashtable.c diff --git a/test/Makefile b/test/Makefile index 06f6943cc..f4ddb9d6c 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,19 +1,19 @@ # # NetSurf unit tests -TESTS := nsurl urldbtest nsoption bloom #llcache +TESTS := nsurl urldbtest nsoption bloom hashtable #llcache -# nsurl sources and flags +# nsurl sources nsurl_SRCS := utils/corestrings.c utils/nsurl.c utils/idna.c \ test/log.c test/nsurl.c -# url database test sources and flags +# url database test sources urldbtest_SRCS := content/urldb.c \ utils/idna.c utils/bloom.c utils/nsoption.c utils/nsurl.c \ utils/corestrings.c \ test/log.c test/urldbtest.c -# low level cache sources and flags +# low level cache sources llcache_SRCS := content/fetch.c content/fetchers/curl.c \ content/fetchers/about.c content/fetchers/data.c \ content/fetchers/resource.c content/llcache.c \ @@ -24,12 +24,14 @@ llcache_SRCS := content/fetch.c content/fetchers/curl.c \ utils/utils.c \ test/log.c test/llcache.c -# nsoption test -nsoption_SRCS := utils/nsoption.c \ - test/log.c test/nsoption.c +# nsoption test sources +nsoption_SRCS := utils/nsoption.c test/log.c test/nsoption.c -bloom_SRCS := utils/bloom.c \ - test/bloom.c +# Bloom filter test sources +bloom_SRCS := utils/bloom.c test/bloom.c + +# hash table test sources +hashtable_SRCS := utils/hashtable.c test/log.c test/hashtable.c # Coverage builds need additional flags ifeq ($(MAKECMDGOALS),coverage) diff --git a/test/hashtable.c b/test/hashtable.c new file mode 100644 index 000000000..2939f4f85 --- /dev/null +++ b/test/hashtable.c @@ -0,0 +1,150 @@ +/* + * Copyright 2015 Vincent Sanders + * Copyright 2006 Rob Kendrick + * + * This file is part of NetSurf, http://www.netsurf-browser.org/ + * + * NetSurf is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * NetSurf is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** + * \file + * Test hash table operations. + * + * Implementation taken from original test rig in bloom filter code + */ + +#include +#include +#include +#include +#include + +#include "utils/hashtable.h" + +/* Tests */ + +/** + * Test hash table creation + * + * Create a hash table, add a single entry and test for value retival + * from key. + * + */ +START_TEST(hashtable_create_test) +{ + struct hash_table *ht; + + ht = hash_create(42); + ck_assert(ht != NULL); + + hash_destroy(ht); +} +END_TEST + +/** + * Test hash table simple operation + * + * Create a hash table, add a single entry and test for failed retival + * from not present key. + * + */ +START_TEST(hashtable_negative_test) +{ + struct hash_table *ht; + bool added; + const char *res; + + /* create hash */ + ht = hash_create(42); + ck_assert(ht != NULL); + + /* add entry */ + added = hash_add(ht, "cow", "moo"); + ck_assert(added == true); + + res = hash_get(ht, "sheep"); + ck_assert(res == NULL); + + hash_destroy(ht); +} +END_TEST + +/** + * Test hash table simple operation + * + * Create a hash table, add a single entry and test for sucessful + * retrival of key. + * + */ +START_TEST(hashtable_positive_test) +{ + struct hash_table *ht; + bool added; + const char *res; + + /* create hash */ + ht = hash_create(42); + ck_assert(ht != NULL); + + /* add entry */ + added = hash_add(ht, "cow", "moo"); + ck_assert(added == true); + + res = hash_get(ht, "cow"); + ck_assert(res != NULL); + ck_assert_str_eq(res, "moo"); + + hash_destroy(ht); +} +END_TEST + +/* Suite */ + +Suite *hashtable_suite(void) +{ + Suite *s; + TCase *tc_create; + + s = suite_create("hash table filter"); + + /* Core API */ + tc_create = tcase_create("Core"); + + tcase_add_test(tc_create, hashtable_create_test); + tcase_add_test(tc_create, hashtable_negative_test); + tcase_add_test(tc_create, hashtable_positive_test); + + suite_add_tcase(s, tc_create); + + + + return s; +} + +int main(int argc, char **argv) +{ + int number_failed; + Suite *s; + SRunner *sr; + + s = hashtable_suite(); + + sr = srunner_create(s); + srunner_run_all(sr, CK_ENV); + + number_failed = srunner_ntests_failed(sr); + srunner_free(sr); + + return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; +} -- cgit v1.2.3