aboutsummaryrefslogtreecommitdiff
path: root/src/tests/hashtest2.cpp
blob: 9ac75d5dc539169096180f483d1a6d76cb774797 (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
#include "hash.h"
#include <string.h>

typedef struct CC
{
	const char *sKey;
	const char *sData;
} CC;

char *c(const char *s)
{
	int l = strlen(s);
	char *o = new char[l+1];
	o[l] = '\0';
	memcpy(o, s, l);

	return o;
}

CC *mkCC(const char *k, const char *d)
{
	CC *o = new CC;
	o->sKey = c(k);
	o->sData = c(d);
	return o;
}

void klCC(CC *c)
{
	delete[] c->sKey;
	delete[] c->sData;
	delete c;
}

typedef Hash<const char *, CC *> CCHash;

int main()
{
	char buf1[200];
	char buf2[200];
	CCHash h;
	CC *tmp;

	for(int i=0;i<10;i++)
	{
		sprintf(buf1, "key_%d", i);
		sprintf(buf2, "data_%d", i);
		tmp = mkCC(buf1, buf2);
		h[tmp->sKey] = tmp;
	}

	for(int i=0;i<12;i++)
	{
		sprintf(buf1, "key_%d", i);
		if(h.has(buf1))
		{
			tmp = h[buf1];
			printf("GOT %s = %s\n", tmp->sKey, tmp->sData);
		}
		else
			printf("%s is not in the table.\n", buf1);
	}

	CCHash::iterator it = h.begin();
	for(;it!=h.end();it++)
	{
		tmp = (*it).second;
		klCC(tmp);
	}

	return 0;
}