Add initial tests for hash table.

This commit is contained in:
Vincent Sanders 2015-07-12 11:24:20 +01:00
parent 3f3ac6e909
commit 3862549ed9
2 changed files with 161 additions and 9 deletions

View File

@ -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)

150
test/hashtable.c Normal file
View File

@ -0,0 +1,150 @@
/*
* Copyright 2015 Vincent Sanders <vince@netsurf-browser.org>
* Copyright 2006 Rob Kendrick <rjek@netsurf-browser.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
/**
* \file
* Test hash table operations.
*
* Implementation taken from original test rig in bloom filter code
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <check.h>
#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;
}