#include "hashtable.h"
#include "hashtable_itr.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
static const int ITEM_COUNT = 4000;
typedef unsigned int uint32_t;
typedef unsigned short uint16_t;
struct key
{
uint32_t one_ip; uint32_t two_ip; uint16_t one_port; uint16_t two_port;
};
struct value
{
char *id;
};
DEFINE_HASHTABLE_INSERT(insert_some, struct key, struct value);
DEFINE_HASHTABLE_SEARCH(search_some, struct key, struct value);
DEFINE_HASHTABLE_REMOVE(remove_some, struct key, struct value);
DEFINE_HASHTABLE_ITERATOR_SEARCH(search_itr_some, struct key);
static unsigned int
hashfromkey(void *ky)
{
struct key *k = (struct key *)ky;
return (((k->one_ip << 17) | (k->one_ip >> 15)) ^ k->two_ip) +
(k->one_port * 17) + (k->two_port * 13 * 29);
}
static int
equalkeys(void *k1, void *k2)
{
return (0 == memcmp(k1,k2,sizeof(struct key)));
}
int
main(int argc, char **argv)
{
struct key *k, *kk;
struct value *v, *found;
struct hashtable *h;
struct hashtable_itr *itr;
int i;
h = create_hashtable(16, hashfromkey, equalkeys);
if (NULL == h) exit(-1);
for (i = 0; i < ITEM_COUNT; i++)
{
k = (struct key *)malloc(sizeof(struct key));
if (NULL == k) {
printf("ran out of memory allocating a key\n");
return 1;
}
k->one_ip = 0xcfccee40 + i;
k->two_ip = 0xcf0cee67 - (5 * i);
k->one_port = 22 + (7 * i);
k->two_port = 5522 - (3 * i);
v = (struct value *)malloc(sizeof(struct value));
v->id = "a value";
if (!insert_some(h,k,v)) exit(-1);
}
printf("After insertion, hashtable contains %u items.\n",
hashtable_count(h));
k = (struct key *)malloc(sizeof(struct key));
if (NULL == k) {
printf("ran out of memory allocating a key\n");
return 1;
}
for (i = 0; i < ITEM_COUNT; i++)
{
k->one_ip = 0xcfccee40 + i;
k->two_ip = 0xcf0cee67 - (5 * i);
k->one_port = 22 + (7 * i);
k->two_port = 5522 - (3 * i);
if (NULL == (found = search_some(h,k))) {
printf("BUG: key not found\n");
}
}
itr = hashtable_iterator(h);
i = 0;
if (hashtable_count(h) > 0)
{
do {
kk = hashtable_iterator_key(itr);
v = hashtable_iterator_value(itr);
i++;
} while (hashtable_iterator_advance(itr));
}
printf("Iterated through %u entries.\n", i);
for (i = 0; i < ITEM_COUNT; i++)
{
k->one_ip = 0xcfccee40 + i;
k->two_ip = 0xcf0cee67 - (5 * i);
k->one_port = 22 + (7 * i);
k->two_port = 5522 - (3 * i);
if (0 == search_itr_some(itr,h,k)) {
printf("BUG: key not found searching with iterator");
}
}
for (i = 0; i < ITEM_COUNT; i++)
{
k->one_ip = 0xcfccee40 + i;
k->two_ip = 0xcf0cee67 - (5 * i);
k->one_port = 22 + (7 * i);
k->two_port = 5522 - (3 * i);
if (NULL == (found = remove_some(h,k))) {
printf("BUG: key not found for removal\n");
}
}
printf("After removal, hashtable contains %u items.\n",
hashtable_count(h));
hashtable_destroy(h, 1);
h = NULL;
free(k);
h = create_hashtable(160, hashfromkey, equalkeys);
if (NULL == h) {
printf("out of memory allocating second hashtable\n");
return 1;
}
for (i = 0; i < ITEM_COUNT; i++)
{
k = (struct key *)malloc(sizeof(struct key));
k->one_ip = 0xcfccee40 + i;
k->two_ip = 0xcf0cee67 - (5 * i);
k->one_port = 22 + (7 * i);
k->two_port = 5522 - (3 * i);
v = (struct value *)malloc(sizeof(struct value));
v->id = "a value";
if (!insert_some(h,k,v))
{
printf("out of memory inserting into second hashtable\n");
return 1;
}
}
printf("After insertion, hashtable contains %u items.\n",
hashtable_count(h));
k = (struct key *)malloc(sizeof(struct key));
if (NULL == k) {
printf("ran out of memory allocating a key\n");
return 1;
}
for (i = ITEM_COUNT - 1; i >= 0; i = i - 7)
{
k->one_ip = 0xcfccee40 + i;
k->two_ip = 0xcf0cee67 - (5 * i);
k->one_port = 22 + (7 * i);
k->two_port = 5522 - (3 * i);
if (0 == search_itr_some(itr, h, k)) {
printf("BUG: key %u not found for search preremoval using iterator\n", i);
return 1;
}
if (0 == hashtable_iterator_remove(itr)) {
printf("BUG: key not found for removal using iterator\n");
return 1;
}
}
free(itr);
for (itr = hashtable_iterator(h);
hashtable_iterator_remove(itr) != 0; ) {
;
}
free(itr);
printf("After removal, hashtable contains %u items.\n",
hashtable_count(h));
hashtable_destroy(h, 1);
free(k);
return 0;
}