-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_HashTable.cpp
More file actions
132 lines (106 loc) · 3.87 KB
/
Copy pathtest_HashTable.cpp
File metadata and controls
132 lines (106 loc) · 3.87 KB
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
#include "catch.hpp"
#include "ExpandableLinkedHashTable.hpp"
#include "memory_leak.h"
#include <utility>
#include <string>
// ʹÓà std::pair<int, std::string> ¶ø²»ÊÇ TestStruct
typedef std::pair<int, std::string> KeyValuePair;
// Define a hash specialization for KeyValuePair (std::pair<int, std::string>)
//namespace std {
// template <>
// struct hash<KeyValuePair> {
// size_t operator()(const KeyValuePair& p) const {
// return hash<int>()(p.first);
// }
// };
//};
TEST_CASE("Constructor and basic operations") {
ExpandableLinkedHashTable<int, KeyValuePair> hashTable;
SECTION("Initial size and capacity") {
REQUIRE(hashTable.getSize() == 16);
REQUIRE(hashTable.getCapcity() == 0);
}
SECTION("Insert and Search operations") {
KeyValuePair kv = { 1, "one" };
REQUIRE(hashTable.Insert(kv));
REQUIRE(hashTable.Search(kv.first));
}
SECTION("Remove operation") {
KeyValuePair kv = { 2, "two" };
hashTable.Insert(kv);
KeyValuePair removed;
REQUIRE(hashTable.Remove(kv.first, removed));
REQUIRE(removed == kv);
REQUIRE_FALSE(hashTable.Search(kv.first));
}
}
TEST_CASE("Resizing of hash table") {
ExpandableLinkedHashTable<int, KeyValuePair> hashTable(2, 1.0); // Smaller size for testing
SECTION("Table resizes correctly") {
for (int i = 0; i < 3; ++i) {
hashTable.Insert({ i, "value" + std::to_string(i) });
}
REQUIRE(hashTable.getSize() > 2); // Table should have resized
}
}
TEST_CASE("Collision handling") {
ExpandableLinkedHashTable<int, KeyValuePair> hashTable;
SECTION("Handling collisions") {
KeyValuePair kv1 = { 3, "three" };
KeyValuePair kv2 = { 3, "another three" }; // Same key as kv1
hashTable.Insert(kv1);
hashTable.Insert(kv2);
REQUIRE(hashTable.getBucketSize(hashTable.getBucket(kv1.first)) == 1);
}
}
TEST_CASE("Clearing the hash table") {
ExpandableLinkedHashTable<int, KeyValuePair> hashTable;
SECTION("Clear operation") {
hashTable.Insert({ 4, "four" });
hashTable.Insert({ 5, "five" });
hashTable.Clear();
REQUIRE(hashTable.getCapcity() == 0);
}
}
TEST_CASE("Large and Complex Test") {
ExpandableLinkedHashTable<int, KeyValuePair> hashTable(2, 0.75);
SECTION("Insertion and Search for a Large Dataset") {
const int dataSize = 10000;
for (int i = 0; i < dataSize; ++i) {
KeyValuePair kv = { i, "value" + std::to_string(i) };
hashTable.Insert(kv);
}
REQUIRE(hashTable.getCapcity() == dataSize);
for (int i = 0; i < dataSize; ++i) {
REQUIRE(hashTable.Search(2) == true);
}
REQUIRE_FALSE(hashTable.Search(dataSize));
}
SECTION("Remove operation for a Large Dataset") {
const int dataSize = 10000;
for (int i = 0; i < dataSize; ++i) {
KeyValuePair kv = { i, "value" + std::to_string(i) };
REQUIRE(hashTable.Insert(kv));
}
REQUIRE(hashTable.getCapcity() == dataSize);
for (int i = 0; i < dataSize; ++i) {
KeyValuePair removed;
REQUIRE(hashTable.Remove(i, removed));
REQUIRE(removed.first == i);
}
REQUIRE(hashTable.getCapcity() == 0);
}
SECTION("Stress Test: Insertion, Resizing, and Collision Handling") {
const int dataSize = 100000;
for (int i = 0; i < dataSize; ++i) {
KeyValuePair kv = { i, "value" + std::to_string(i) };
REQUIRE(hashTable.Insert(kv));
}
REQUIRE(hashTable.getCapcity() == dataSize);
for (int i = 0; i < dataSize; ++i) {
REQUIRE(hashTable.Search(i));
}
REQUIRE_FALSE(hashTable.Search(dataSize));
// At this point, the hash table should have resized multiple times.
}
}