/*
 * iterate.c: test program for the hash module.
 *
 * (C) Duncan C. White, 1996-2011 although it seems longer:-)
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#include "hash.h"



void myPrint( FILE *out, key k, value v )
{
	fprintf( out, "%s: %s\n", k, (char *)v );
}


static void myFree( value v )
{
	free( v );
}


static value myCopyValue( value v )
{
	return strdup(v);
}


static hash h1;


void set( hash h, key k, char *v )
{
	hashSet( h, k, myCopyValue(v) );
}


void onerun( void )
{
	h1 = hashCreate( myPrint, myFree, myCopyValue );

	set( h1, "one", "eeny" );
	set( h1, "two", "meeny" );
	set( h1, "three", "miny" );
	set( h1, "four", "mo" );

	set( h1, "one", "solitary posh git" );
	set( h1, "aardvark", "not a herring" );
	set( h1, "three", "quite a few" );

	hash h2 = hashCopy( h1 );

	set( h1, "one", "single solitaire" );
	set( h2, "one", "on me ownses, savvy?" );

	hashFree( h1 );

	hashFree( h2 );
}


int main( int argc, char **argv )
{
	int lim = argc > 1 ? atoi(argv[1]) : 1000;
	int delay = argc > 2 ? atoi(argv[2]) : 0;
	int i;
	printf( "running %d iterations\n", lim );
	for( i=0; i<lim; i++ )
	{
		onerun();
	}
	if( delay > 0 )
	{
		printf( "sleeping for %d seconds\n", delay );
		sleep( delay );
	}
	exit(0);
	return 0;
}
