Add hashtable test to expose confusion as to whether 0 is a valid value or not
authorJ. Ali Harlow <ali@juiblex.co.uk>
Fri Apr 17 21:09:55 2009 +0100 (2009-04-17)
changeset 35739f8dab73084
parent 356 d842feb64ecb
child 358 13beaca8b75f
Add hashtable test to expose confusion as to whether 0 is a valid value or not
librazor/Makefile.am
librazor/test-hashtable.c
     1.1 --- a/librazor/Makefile.am	Thu Mar 12 20:52:12 2009 +0000
     1.2 +++ b/librazor/Makefile.am	Fri Apr 17 21:09:55 2009 +0100
     1.3 @@ -12,8 +12,9 @@
     1.4  	-DPACKAGE_LIB_DIR=\""$(libdir)"\"
     1.5  
     1.6  lib_LTLIBRARIES = librazor.la
     1.7 +check_PROGRAMS = test-hashtable
     1.8  if HAVE_LUA
     1.9 -  check_PROGRAMS = test-lua
    1.10 +  check_PROGRAMS += test-lua
    1.11  endif
    1.12  
    1.13  librazorincludedir = $(includedir)/razor
    1.14 @@ -41,12 +42,17 @@
    1.15  librazor_la_LIBADD = $(ZLIB_LIBS) $(LUA_LIBS) ../gl/libgnu.la $(EXTRA_LIBS)
    1.16  librazor_la_LDFLAGS = -no-undefined
    1.17  
    1.18 +test_hashtable_SOURCES = test-hashtable.c
    1.19 +test_hashtable_LDADD = types.lo ../gl/libgnu.la $(EXTRA_LIBS)
    1.20 +
    1.21 +TESTS = test-hashtable
    1.22 +
    1.23  if HAVE_LUA
    1.24    test_lua_SOURCES = test-lua.c
    1.25    test_lua_LDADD = lua.lo util.lo $(LUA_LIBS) ../gl/libgnu.la \
    1.26  	$(EXTRA_LIBS)
    1.27  
    1.28 -  TESTS = test-lua
    1.29 +  TESTS += test-lua
    1.30  endif
    1.31  
    1.32  EXTRA_DIST = 			\
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/librazor/test-hashtable.c	Fri Apr 17 21:09:55 2009 +0100
     2.3 @@ -0,0 +1,75 @@
     2.4 +/*
     2.5 + * Copyright (C) 2009  J. Ali Harlow <ali@juiblex.co.uk>
     2.6 + *
     2.7 + * This program is free software; you can redistribute it and/or modify
     2.8 + * it under the terms of the GNU General Public License as published by
     2.9 + * the Free Software Foundation; either version 2 of the License, or
    2.10 + * (at your option) any later version.
    2.11 + *
    2.12 + * This program is distributed in the hope that it will be useful,
    2.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    2.15 + * GNU General Public License for more details.
    2.16 + *
    2.17 + * You should have received a copy of the GNU General Public License along
    2.18 + * with this program; if not, write to the Free Software Foundation, Inc.,
    2.19 + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    2.20 + */
    2.21 +
    2.22 +#include "config.h"
    2.23 +
    2.24 +#include <stdlib.h>
    2.25 +#include <stdio.h>
    2.26 +#include <string.h>
    2.27 +#include "razor-internal.h"
    2.28 +
    2.29 +uint32_t invalid;
    2.30 +struct array pool;
    2.31 +struct hashtable table;
    2.32 +
    2.33 +static int test_key(const char *key)
    2.34 +{
    2.35 +	uint32_t value;
    2.36 +
    2.37 +	value = hashtable_insert(&table, key);
    2.38 +	if (value == invalid) {
    2.39 +		fprintf(stderr,
    2.40 +			"%s: hashtable_insert returned invalid value\n", key);
    2.41 +		return 1;
    2.42 +	} else if (value >= pool.size) {
    2.43 +		fprintf(stderr, "%s: hashtable_insert returned value outside "
    2.44 +			"pool\n", key);
    2.45 +		return 1;
    2.46 +	} else if (pool.size - value <= strlen(key) ||
    2.47 +		   strcmp(pool.data + value, key)) {
    2.48 +		fprintf(stderr, "%s: value returned by hashtable_insert does "
    2.49 +			"not match key\n", key);
    2.50 +		return 1;
    2.51 +	} else if (hashtable_lookup(&table, key) != value) {
    2.52 +		fprintf(stderr, "%s: hashtable_lookup didn't return expected "
    2.53 +			"value\n", key);
    2.54 +		return 1;
    2.55 +	} else if (hashtable_tokenize(&table, key) != value) {
    2.56 +		fprintf(stderr, "%s: hashtable_tokenize didn't return expected "
    2.57 +			"value\n", key);
    2.58 +		return 1;
    2.59 +	}
    2.60 +	return 0;
    2.61 +}
    2.62 +
    2.63 +int main(int argc, char *argv[])
    2.64 +{
    2.65 +	int errors = 0;
    2.66 +
    2.67 +	array_init(&pool);
    2.68 +	hashtable_init(&table, &pool);
    2.69 +	invalid = hashtable_lookup(&table, "non-existant");
    2.70 +
    2.71 +	errors += test_key("key1");
    2.72 +	errors += test_key("key2");
    2.73 +
    2.74 +	hashtable_release(&table);
    2.75 +	array_release(&pool);
    2.76 +
    2.77 +	exit(errors ? 1 : 0);
    2.78 +}