aboutsummaryrefslogtreecommitdiff
path: root/src/unit/hashtable/hashtable.cpp
blob: b2e1cf513481d0c21262aa53f6412ecc54cd5f75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <cpptest.h>
#include <string.h>
#include <set>
#include <map>

#include "hashfunctionstring.h"
#include "hashfunctioncasestring.h"
#include "hashfunctionint.h"

class HashFunctionSuite : public Test::Suite
{
public:
	HashFunctionSuite()
	{
		TEST_ADD( HashFunctionSuite::functionString )
		TEST_ADD( HashFunctionSuite::functionCaseString )
		TEST_ADD( HashFunctionSuite::functionInt )
	}

private:
	void functionStringWorker( HashFunction &hf, std::set<unsigned long> &sCodes, char *str, int nLevel, int nMax )
	{
		for( char let = 'A'; let <= 'z'; let += 3 )
		{
			str[nLevel+1] = '\0';
			str[nLevel] = let;
			unsigned long x = hf.hash( str );
			TEST_ASSERT( sCodes.find( x ) == sCodes.end() );
			TEST_ASSERT( hf.cmpIDs( str, str ) );
			sCodes.insert( x );

			if( nLevel < nMax )
				functionStringWorker( hf, sCodes, str, nLevel+1, nMax );
		}
	}
	
	void functionString()
	{
		HashFunctionString hf;
		char str[10];

		std::set<unsigned long> sCodes;

		functionStringWorker( hf, sCodes, str, 0, 3 );
	}
	
	void functionCaseStringWorker( HashFunction &hf, std::map<unsigned long, char *> &sCodes, char *str, int nLevel, int nMax )
	{
		for( char let = 'A'; let <= 'z'; let += 3 )
		{
			str[nLevel+1] = '\0';
			str[nLevel] = let;
			unsigned long x = hf.hash( str );
			std::map<unsigned long, char *>::iterator i = sCodes.find( x );
			if( i == sCodes.end() )
			{
				sCodes[x] = strdup( str );
			}
			else
			{
				TEST_ASSERT( strcasecmp( (*i).second, str ) == 0 );
				TEST_ASSERT( hf.cmpIDs( (*i).second, str ) == true );
			}

			if( nLevel < nMax )
				functionCaseStringWorker( hf, sCodes, str, nLevel+1, nMax );
		}
	}
	
	void functionCaseString()
	{
		HashFunctionCaseString hf;
		char str[10];

		std::map<unsigned long, char *> sCodes;

		functionCaseStringWorker( hf, sCodes, str, 0, 3 );

		std::map<unsigned long, char *>::iterator i;
		for( i = sCodes.begin(); i != sCodes.end(); i++ )
		{
			free( (*i).second );
		}
	}

	void functionInt()
	{
		HashFunctionInt hf;

		for( long i = -100000; i <= 100000; i += 100 )
		{
			TEST_ASSERT( ((long)hf.hash( (void *)i )) == i );
			TEST_ASSERT( ((long)hf.cmpIDs( (void *)i, (void *)i )) );
		}
	}
};

int main( int argc, char *argv[] )
{
	Test::TextOutput output( Test::TextOutput::Verbose );
	HashFunctionSuite ts;
	return ts.run( output ) ? EXIT_SUCCESS : EXIT_FAILURE;
}