librazor/test-hashtable.c
author J. Ali Harlow <ali@juiblex.co.uk>
Wed Apr 29 17:00:01 2009 +0100 (2009-04-29)
changeset 361 2523d03a840e
permissions -rw-r--r--
Add support for preloading lua modules. This is useful both when
providing lua bindings to applications based on librazor and when
producing static binaries using librazor (where otherwise the lua
POSIX library would need to be included as an additional dynamic
object).
ali@357
     1
/*
ali@357
     2
 * Copyright (C) 2009  J. Ali Harlow <ali@juiblex.co.uk>
ali@357
     3
 *
ali@357
     4
 * This program is free software; you can redistribute it and/or modify
ali@357
     5
 * it under the terms of the GNU General Public License as published by
ali@357
     6
 * the Free Software Foundation; either version 2 of the License, or
ali@357
     7
 * (at your option) any later version.
ali@357
     8
 *
ali@357
     9
 * This program is distributed in the hope that it will be useful,
ali@357
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ali@357
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ali@357
    12
 * GNU General Public License for more details.
ali@357
    13
 *
ali@357
    14
 * You should have received a copy of the GNU General Public License along
ali@357
    15
 * with this program; if not, write to the Free Software Foundation, Inc.,
ali@357
    16
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ali@357
    17
 */
ali@357
    18
ali@357
    19
#include "config.h"
ali@357
    20
ali@357
    21
#include <stdlib.h>
ali@357
    22
#include <stdio.h>
ali@357
    23
#include <string.h>
ali@357
    24
#include "razor-internal.h"
ali@357
    25
ali@357
    26
uint32_t invalid;
ali@357
    27
struct array pool;
ali@357
    28
struct hashtable table;
ali@357
    29
ali@357
    30
static int test_key(const char *key)
ali@357
    31
{
ali@357
    32
	uint32_t value;
ali@357
    33
ali@357
    34
	value = hashtable_insert(&table, key);
ali@357
    35
	if (value == invalid) {
ali@357
    36
		fprintf(stderr,
ali@357
    37
			"%s: hashtable_insert returned invalid value\n", key);
ali@357
    38
		return 1;
ali@357
    39
	} else if (value >= pool.size) {
ali@357
    40
		fprintf(stderr, "%s: hashtable_insert returned value outside "
ali@357
    41
			"pool\n", key);
ali@357
    42
		return 1;
ali@357
    43
	} else if (pool.size - value <= strlen(key) ||
ali@357
    44
		   strcmp(pool.data + value, key)) {
ali@357
    45
		fprintf(stderr, "%s: value returned by hashtable_insert does "
ali@357
    46
			"not match key\n", key);
ali@357
    47
		return 1;
ali@357
    48
	} else if (hashtable_lookup(&table, key) != value) {
ali@357
    49
		fprintf(stderr, "%s: hashtable_lookup didn't return expected "
ali@357
    50
			"value\n", key);
ali@357
    51
		return 1;
ali@357
    52
	} else if (hashtable_tokenize(&table, key) != value) {
ali@357
    53
		fprintf(stderr, "%s: hashtable_tokenize didn't return expected "
ali@357
    54
			"value\n", key);
ali@357
    55
		return 1;
ali@357
    56
	}
ali@357
    57
	return 0;
ali@357
    58
}
ali@357
    59
ali@357
    60
int main(int argc, char *argv[])
ali@357
    61
{
ali@357
    62
	int errors = 0;
ali@357
    63
ali@357
    64
	array_init(&pool);
ali@357
    65
	hashtable_init(&table, &pool);
ali@357
    66
	invalid = hashtable_lookup(&table, "non-existant");
ali@357
    67
ali@357
    68
	errors += test_key("key1");
ali@357
    69
	errors += test_key("key2");
ali@357
    70
ali@357
    71
	hashtable_release(&table);
ali@357
    72
	array_release(&pool);
ali@357
    73
ali@357
    74
	exit(errors ? 1 : 0);
ali@357
    75
}