1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/librazor/test-hashtable.c Wed Apr 22 16:27:11 2009 +0100
1.3 @@ -0,0 +1,75 @@
1.4 +/*
1.5 + * Copyright (C) 2009 J. Ali Harlow <ali@juiblex.co.uk>
1.6 + *
1.7 + * This program is free software; you can redistribute it and/or modify
1.8 + * it under the terms of the GNU General Public License as published by
1.9 + * the Free Software Foundation; either version 2 of the License, or
1.10 + * (at your option) any later version.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License along
1.18 + * with this program; if not, write to the Free Software Foundation, Inc.,
1.19 + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1.20 + */
1.21 +
1.22 +#include "config.h"
1.23 +
1.24 +#include <stdlib.h>
1.25 +#include <stdio.h>
1.26 +#include <string.h>
1.27 +#include "razor-internal.h"
1.28 +
1.29 +uint32_t invalid;
1.30 +struct array pool;
1.31 +struct hashtable table;
1.32 +
1.33 +static int test_key(const char *key)
1.34 +{
1.35 + uint32_t value;
1.36 +
1.37 + value = hashtable_insert(&table, key);
1.38 + if (value == invalid) {
1.39 + fprintf(stderr,
1.40 + "%s: hashtable_insert returned invalid value\n", key);
1.41 + return 1;
1.42 + } else if (value >= pool.size) {
1.43 + fprintf(stderr, "%s: hashtable_insert returned value outside "
1.44 + "pool\n", key);
1.45 + return 1;
1.46 + } else if (pool.size - value <= strlen(key) ||
1.47 + strcmp(pool.data + value, key)) {
1.48 + fprintf(stderr, "%s: value returned by hashtable_insert does "
1.49 + "not match key\n", key);
1.50 + return 1;
1.51 + } else if (hashtable_lookup(&table, key) != value) {
1.52 + fprintf(stderr, "%s: hashtable_lookup didn't return expected "
1.53 + "value\n", key);
1.54 + return 1;
1.55 + } else if (hashtable_tokenize(&table, key) != value) {
1.56 + fprintf(stderr, "%s: hashtable_tokenize didn't return expected "
1.57 + "value\n", key);
1.58 + return 1;
1.59 + }
1.60 + return 0;
1.61 +}
1.62 +
1.63 +int main(int argc, char *argv[])
1.64 +{
1.65 + int errors = 0;
1.66 +
1.67 + array_init(&pool);
1.68 + hashtable_init(&table, &pool);
1.69 + invalid = hashtable_lookup(&table, "non-existant");
1.70 +
1.71 + errors += test_key("key1");
1.72 + errors += test_key("key2");
1.73 +
1.74 + hashtable_release(&table);
1.75 + array_release(&pool);
1.76 +
1.77 + exit(errors ? 1 : 0);
1.78 +}