-
Notifications
You must be signed in to change notification settings - Fork 4
/
TestHash.cpp
127 lines (118 loc) · 2.62 KB
/
TestHash.cpp
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <stdio.h>
#include "hash.h"
int main()
{
static const int N = 5000;
IntHash<double> hash;
int i;
puts("Add & find...");
for (i=0; i<N; ++i)
{
double *d = new double;
*d = (double)i;
hash.insert(i, d);
double *f = hash.find(i);
if (!f)
{
printf("not found: %d\n", i);
return -1;
}
if (*f != *d)
{
printf("wrong item: %g != %g\n", *d, *f);
return -1;
}
}
if (hash.size() != N)
{
printf("wrong size: %d\n", hash.size());
return -1;
}
puts("Iterate...");
IntHash<double>::Iterator iter(hash);
i = 0;
double *d;
while ((d = iter.getValue()) != 0)
{
++i;
if (iter.getKey() != (int)*d ||
iter.getValue() != d)
{
printf("braindead iter\n");
return -1;
}
iter.next();
}
if (i != N)
{
printf("wrong iter count: %d\n", i);
return -1;
}
puts("Find & remove...");
for (i=0; i<N; ++i)
{
double *f = hash.find(i);
if (!f)
{
printf("not found: %d\n", i);
return -1;
}
if (i != (int)*f)
{
printf("wrong item: %d != %g\n", i, *f);
return -1;
}
double *r = hash.remove(i);
if (r != f)
printf("wrong item removed: %g != %g\n", *r, *f);
delete f;
}
if (hash.size() != 0)
{
printf("wrong size: %d\n", hash.size());
return -1;
}
puts("Add & find another batch of values...");
for (i=0; i<N; ++i)
{
double *d = new double;
*d = (double)i;
hash.insert(i, d);
double *f = hash.find(i);
if (!f)
{
printf("not found: %d\n", i);
return -1;
}
if (*f != *d)
{
printf("wrong item: %g != %g\n", *d, *f);
return -1;
}
}
if (hash.size() != N)
{
printf("wrong size: %d\n", hash.size());
return -1;
}
puts("Delete via iterator");
IntHash<double>::Iterator iter2(hash);
// Delete all the channels from the Channel Table
while ((d = iter2.getValue()) !=0)
{
double *r = iter2.remove();
if (r != d)
{
printf("wrong item: %g != %g\n", *r, *d);
return -1;
}
delete r;
}
if (hash.size() != 0)
{
printf("wrong size: %d\n", hash.size());
return -1;
}
puts("OK.");
return 0;
}