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