-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
191 lines (184 loc) · 5.21 KB
/
main.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "test.h"
#include "primitivehashmap.h"
#include <iostream>
// List of words that hashes to the same index
// Bucketsize 16:
// song and for, 2 and test and never, 4 and give
// Bucketsize 8:
// ol and FIGHT, sc and to, men and ON
TEST_CASE("PrimitiveHashMap") {
SECTION("Constructor") {
PrimitiveHashMap<int> sut(512);
SECTION("upon construction, has a size of 0") {
REQUIRE(sut.size() == 0);
}
}
SECTION("set") {
PrimitiveHashMap<int> sut(5);
SECTION("when there is capacity, success is returned") {
bool success = sut.set("never", 5);
REQUIRE(success);
}
SECTION("when there is no capacity, false is returned") {
PrimitiveHashMap<int> fail(0);
bool success = fail.set("gonna", 5);
REQUIRE(!success);
}
SECTION("when successful, increases size by 1") {
sut.set("give", 5);
REQUIRE(sut.size() == 1);
}
SECTION("insertion to capacity will make the table full, thus returning false") {
bool success = true;
for (int i = 0; i < 5; ++i) {
success = sut.set(std::to_string(i), 1);
REQUIRE(success);
}
success = sut.set("you",1);
REQUIRE(!success);
}
SECTION("inserting the same key overrides values and keeps size constant") {
sut.set("test", 1);
sut.set("test", 2);
REQUIRE(sut.size() == 1);
}
SECTION("inserting keys that causes collision, adds size by 1") {
sut.set("song", 1);
sut.set("for", 2);
REQUIRE(sut.size() == 2);
sut.set("2", 3);
sut.set("test", 4);
REQUIRE(sut.size() == 4);
}
}
SECTION("get") {
PrimitiveHashMap<int> sut(7);
SECTION("calling get on empty table returns NULL") {
int* actual = sut.get("give");
REQUIRE(actual == NULL);
}
SECTION("calling get on inserted items return the correct value") {
sut.set("me", 1);
sut.set("a", 2);
sut.set("shot", 3);
sut.set("it", 4);
sut.set("will", 5);
sut.set("be", 6);
sut.set("worthwhile", 7);
REQUIRE(*sut.get("me") == 1);
REQUIRE(*sut.get("a") == 2);
REQUIRE(*sut.get("shot") == 3);
REQUIRE(*sut.get("it") == 4);
REQUIRE(*sut.get("will") == 5);
REQUIRE(*sut.get("be") == 6);
REQUIRE(*sut.get("worthwhile") == 7);
}
SECTION("calling get on erased item will return NULL") {
sut.set("hello", 1);
sut.set("world", 2);
sut.erase("hello");
sut.erase("world");
REQUIRE(sut.get("hello") == NULL);
REQUIRE(sut.get("world") == NULL);
}
SECTION("calling get on keys with collisions will retrieve correct values") {
sut.set("song", 1);
sut.set("for", 2);
sut.set("2", 3);
sut.set("test", 4);
sut.set("never", 5);
sut.set("4", 6);
sut.set("give", 7);
REQUIRE(*sut.get("song") == 1);
REQUIRE(*sut.get("for") == 2);
REQUIRE(*sut.get("2") == 3);
REQUIRE(*sut.get("test") == 4);
REQUIRE(*sut.get("never") == 5);
REQUIRE(*sut.get("4") == 6);
REQUIRE(*sut.get("give") == 7);
}
}
SECTION("erase") {
PrimitiveHashMap<int> sut(8);
SECTION("when table is empty, NULL is returned") {
int* actual = sut.erase("hey");
REQUIRE(actual == NULL);
}
SECTION("erasing after inserting with the same key returns the value") {
sut.set("up", 5);
int* actual = sut.erase("up");
REQUIRE(actual != NULL);
if (actual != NULL) {
REQUIRE(*actual == 5);
}
}
SECTION("erasing colliding keys, returns the value deleted and decreases size by 1") {
sut.set ("song", 1);
sut.set("for", 2);
sut.set("2", 3);
sut.set("test", 4);
sut.set("never",5);
int* actual = sut.erase("2");
REQUIRE(actual != NULL);
REQUIRE(*actual == 3);
REQUIRE(sut.size() == 4);
actual = sut.erase("never");
REQUIRE(actual != NULL);
REQUIRE(*actual == 5);
REQUIRE(sut.size() == 3);
actual = sut.erase("song");
REQUIRE(actual != NULL);
REQUIRE(*actual == 1);
REQUIRE(sut.size() == 2);
actual = sut.erase("for");
REQUIRE(actual != NULL);
REQUIRE(*actual == 2);
REQUIRE(sut.size() == 1);
actual = sut.erase("test");
REQUIRE(actual != NULL);
REQUIRE(*actual == 4);
REQUIRE(sut.size() == 0);
}
}
SECTION("load") {
PrimitiveHashMap<int> sut(4);
SECTION("load factor of an empty table is 0.0") {
float actual = sut.load();
REQUIRE(actual == 0.0);
}
SECTION("load factor of one item inserted with bucket 8 is 1/8") {
sut.set("ol", 1);
float expected = static_cast<float>(1)/static_cast<float>(8);
float actual = sut.load();
REQUIRE(actual == expected);
}
SECTION("load factor of two item inserted with bucket 8 is 2/8") {
sut.set("sc", 1);
sut.set("our", 2);
float expected = static_cast<float>(2)/static_cast<float>(8);
float actual = sut.load();
REQUIRE(actual == expected);
}
SECTION("load factor of three item inserted with bucket 8 is 3/8") {
sut.set("men", 1);
sut.set("FIGHT", 2);
sut.set("ON", 3);
float expected = static_cast<float>(3)/static_cast<float>(8);
float actual = sut.load();
REQUIRE(actual == expected);
}
SECTION("load factor of four item inserted with bucket 8 is 4/8") {
sut.set("to", 1);
sut.set("victory", 2);
sut.set("!", 3);
sut.set("!!", 4);
float expected = static_cast<float>(4)/static_cast<float>(8);
float actual = sut.load();
REQUIRE(actual == expected);
}
}
}
int main(int argc, char** argv) {
int result = Catch::Session().run(argc, argv);
return result;
}