From: J. Ali Harlow Date: Fri, 17 Apr 2009 20:09:55 +0000 (+0100) Subject: Add hashtable test to expose confusion as to whether 0 is a valid value or not X-Git-Tag: 0.1~21 X-Git-Url: http://project.juiblex.co.uk/git/?a=commitdiff_plain;h=7587ee6ce83410faa662114a3da08ca78cb3bd03;p=razor2.git%2F.git Add hashtable test to expose confusion as to whether 0 is a valid value or not --- diff --git a/librazor/Makefile.am b/librazor/Makefile.am index 68cb766..1df2de0 100644 --- a/librazor/Makefile.am +++ b/librazor/Makefile.am @@ -12,8 +12,9 @@ INCLUDES = \ -DPACKAGE_LIB_DIR=\""$(libdir)"\" lib_LTLIBRARIES = librazor.la +check_PROGRAMS = test-hashtable if HAVE_LUA - check_PROGRAMS = test-lua + check_PROGRAMS += test-lua endif librazorincludedir = $(includedir)/razor @@ -41,12 +42,17 @@ endif librazor_la_LIBADD = $(ZLIB_LIBS) $(LUA_LIBS) ../gl/libgnu.la $(EXTRA_LIBS) librazor_la_LDFLAGS = -no-undefined +test_hashtable_SOURCES = test-hashtable.c +test_hashtable_LDADD = types.lo ../gl/libgnu.la $(EXTRA_LIBS) + +TESTS = test-hashtable + if HAVE_LUA test_lua_SOURCES = test-lua.c test_lua_LDADD = lua.lo util.lo $(LUA_LIBS) ../gl/libgnu.la \ $(EXTRA_LIBS) - TESTS = test-lua + TESTS += test-lua endif EXTRA_DIST = \ diff --git a/librazor/test-hashtable.c b/librazor/test-hashtable.c new file mode 100644 index 0000000..fc7bff3 --- /dev/null +++ b/librazor/test-hashtable.c @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2009 J. Ali Harlow + * + * This program 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; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" + +#include +#include +#include +#include "razor-internal.h" + +uint32_t invalid; +struct array pool; +struct hashtable table; + +static int test_key(const char *key) +{ + uint32_t value; + + value = hashtable_insert(&table, key); + if (value == invalid) { + fprintf(stderr, + "%s: hashtable_insert returned invalid value\n", key); + return 1; + } else if (value >= pool.size) { + fprintf(stderr, "%s: hashtable_insert returned value outside " + "pool\n", key); + return 1; + } else if (pool.size - value <= strlen(key) || + strcmp(pool.data + value, key)) { + fprintf(stderr, "%s: value returned by hashtable_insert does " + "not match key\n", key); + return 1; + } else if (hashtable_lookup(&table, key) != value) { + fprintf(stderr, "%s: hashtable_lookup didn't return expected " + "value\n", key); + return 1; + } else if (hashtable_tokenize(&table, key) != value) { + fprintf(stderr, "%s: hashtable_tokenize didn't return expected " + "value\n", key); + return 1; + } + return 0; +} + +int main(int argc, char *argv[]) +{ + int errors = 0; + + array_init(&pool); + hashtable_init(&table, &pool); + invalid = hashtable_lookup(&table, "non-existant"); + + errors += test_key("key1"); + errors += test_key("key2"); + + hashtable_release(&table); + array_release(&pool); + + exit(errors ? 1 : 0); +}