-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathHash-test.c
126 lines (100 loc) · 2.72 KB
/
Hash-test.c
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
// Hash-test.c
// unit tests
#include <assert.h>
#include <inttypes.h>
#include "Str.h"
#include "UnitTest.h"
#include "Hash.h"
////////////////////////////////////////////////////////////////////////////////
// static functions
////////////////////////////////////////////////////////////////////////////////
uint32_t hashInt(void *keyP, uint32_t tableSize)
{
assert(keyP);
int *valueP = (int *) keyP;
int result = (*valueP) / tableSize;
if (result < 0)
result += tableSize;
return result;
}
int compareInt(void *key1P, void *key2P)
{
assert(key1P);
assert(key2P);
int *value1P = (int *) key1P;
int *value2P = (int *) key2P;
if ((*value1P) == (*value2P))
return 1;
else
return 0;
}
////////////////////////////////////////////////////////////////////////////////
// testFree
////////////////////////////////////////////////////////////////////////////////
static void testFree()
{
Hash_T h0 = Hash_new(0);
Hash_free(&h0);
EXPECT_NULL(h0);
Hash_T h1 = Hash_new(1);
Hash_free(&h1);
EXPECT_NULL(h1);
Hash_T h2 = Hash_new(2);
Hash_free(&h2);
EXPECT_NULL(h2);
}
////////////////////////////////////////////////////////////////////////////////
// testInsert
////////////////////////////////////////////////////////////////////////////////
static void testInsert()
{
// test general case
Hash_T h0 = Hash_new(0);
EXPECT_EQ_UINT32(0, h0->nElements);
EXPECT_EQ_UINT32(0, h0->tableSize);
int key1 = 123;
char* value1 = "1";
h0 = Hash_insert(h0, Atom_newFromInt64(key1), value1);
EXPECT_NOT_NULL(h0);
EXPECT_EQ_UINT32(1, h0->nElements);
EXPECT_EQ_UINT32(1, h0->tableSize);
int key2 = -27;
char value2[] = "value2";
h0 = Hash_insert(h0, Atom_newFromInt64(key2), value2);
EXPECT_NOT_NULL(h0);
EXPECT_EQ_UINT32(2, h0->nElements);
EXPECT_EQ_UINT32(3, h0->tableSize);
char * key3 = "abc";
int16_t value3 = 1056;
Str_T s = Str_newFromInt16(value3);
h0 = Hash_insert(h0, Atom_newFromString(key3), Str_str(s));
EXPECT_NOT_NULL(h0);
EXPECT_EQ_UINT32(3, h0->nElements);
EXPECT_EQ_UINT32(3, h0->tableSize);
Str_free(&s);
Hash_free(&h0);
EXPECT_NULL(h0);
}
////////////////////////////////////////////////////////////////////////////////
// testNew
////////////////////////////////////////////////////////////////////////////////
static void testNew()
{
Hash_T h0 = Hash_new(0);
EXPECT_NOT_NULL(h0);
Hash_T h1 = Hash_new(1);
EXPECT_NOT_NULL(h1);
Hash_T h2 = Hash_new(2);
EXPECT_NOT_NULL(h2);
}
////////////////////////////////////////////////////////////////////////////////
// main
////////////////////////////////////////////////////////////////////////////////
int
main(int argc, char **argv)
{
testNew();
testFree();
testInsert();
UnitTest_report();
}