forked from CS-PCC/CS8_Assignments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_test.cpp
executable file
·256 lines (218 loc) · 7.72 KB
/
basic_test.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include "gtest/gtest.h"
#include <iostream>
#include <iomanip>
//------------------------------------------------------------------------------
//Files we are testing:
#include "../../includes/hash/chained_hash.h"
#include "../../includes/hash/hash_record.h"
//------------------------------------------------------------------------------
using namespace std;
//------------------------------------------------------------------------------
// COPY BASIC_TEST INTO THIS FILE.
// AND THEN,
// DO NOT EDIT THIS FILE ANY FURTHER
//------------------------------------------------------------------------------
int random(int low, int high)
{
// call srand in main if you want better random numbers.
return static_cast<int>(low + (rand() % static_cast<int>((high - low + 1))));
}
bool basic_test(bool debug=false)
{
cout << "ChainedHash Test" << endl << endl;
// Default constructor
ChainedHash<HashRecord> chained_hash;
// Initial HashRecord
HashRecord hash_record;
// Init
bool found = false;
int initial_size = chained_hash.size();
// Insert to hash
for (int i = 0; i < chained_hash.capacity() * 0.75; i++)
{
int r = random(100, 999);
chained_hash.insert(HashRecord(r, string("item__") + to_string(r)));
}
// Insert non-prime key
chained_hash.insert(HashRecord(42, string("This is the meaning of life? A NON-PRIME number? @_@")));
chained_hash.insert(HashRecord(52, string("This is the meaning of life? A NON-PRIME number? !_!")));
cout << "chained_hash after all the insertions: " << endl << chained_hash << endl;
// Report of operations
cout << "ChainedHash:" << endl;
cout << " capacity: " << chained_hash.capacity() << endl;
cout << " initial size: " << initial_size << endl;
cout << " final size: " << chained_hash.size() << endl;
cout << " is_present(42): " << boolalpha << chained_hash.is_present(42) << endl;
cout << " is_present(52): " << boolalpha << chained_hash.is_present(52) << endl;
// remove key
chained_hash.remove(52);
cout << " is_present(52): " << boolalpha << chained_hash.is_present(52) << endl;
// find existing key
chained_hash.find(42, found, hash_record);
cout << " find(42): " << boolalpha << found << " :: " << hash_record << endl;
// find non-existing key
chained_hash.find(52, found, hash_record);
cout << " find(52): " << boolalpha << found << " :: " << hash_record << endl;
cout << endl;
cout << "---------------------------------------------------------------" << endl;
cout << "The print anatomy: "<< endl;
cout << "[003] [345 : item__345][668 : item__668][801 : item__801]" << endl<<endl;
cout << ". . . . . . . . . . . . . . . . . . " << endl;
cout << "[003]: table index." << endl;
cout << "[345 : item__345][668 : item__668]...: records inserted: inorder()" << endl;
cout << "---------------------------------------------------------------" << endl << endl;
if (!found) {
cout << "52 was removed successfully: " << endl << chained_hash << endl;
cout << "---------------------------------------------------------------" << endl;
} else {
cout << "FAILED to remove 52" << endl;
}
// Copy constructor
// replace key:42
chained_hash.insert(HashRecord(42, string("This is the meaning of life? A NON-PRIME number? >_<")));
ChainedHash<HashRecord> chained_hash_2(chained_hash);
cout << "chained_hash_2 copy from chained_hash: " << endl << chained_hash_2 << endl;
cout << "---------------------------------------------------------------" << endl;
// Assignment operator
// remove key:42
chained_hash_2.remove(42);
ChainedHash<HashRecord> chained_hash_3;
chained_hash_3 = chained_hash_2;
cout << "chained_hash_3 copy from chained_hash_2: " << endl << chained_hash_3 << endl;
cout << "---------------------------------------------------------------" << endl;
return true;
}
//Lord help me!
const bool debug = false;
TEST(TEST_CHAINED_HASH, BasicTest)
{
bool success = basic_test(debug);
EXPECT_EQ(success, true);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
std::cout<<"\n\n----------running basic_test.cpp---------\n\n"<<std::endl;
return RUN_ALL_TESTS();
}
/*
includes
├── avl_tree
│ └── avl_tree.h
├── binary_tree
│ └── binary_tree.h
└── hash
├── chained_hash.h
└── hash_record.h
3 directories, 4 files
----------running basic_test.cpp---------
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from TEST_CHAINED_HASH
[ RUN ] TEST_CHAINED_HASH.BasicTest
ChainedHash Test
chained_hash after all the insertions:
[000] [209:item__209][342:item__342]
[001]
[002] [230:item__230]
[003] [573:item__573][592:item__592][858:item__858][972:item__972]
[004] [42:This is the meaning of life? A NON-PRIME number? @_@][707:item__707]
[005] [423:item__423][765:item__765]
[006]
[007]
[008] [844:item__844]
[009]
[010]
[011]
[012] [449:item__449][487:item__487]
[013] [640:item__640]
[014] [52:This is the meaning of life? A NON-PRIME number? !_!]
[015]
[016]
[017] [378:item__378]
[018]
ChainedHash:
capacity: 19
initial size: 0
final size: 17
is_present(42): true
is_present(52): true
is_present(52): false
find(42): true :: [42:This is the meaning of life? A NON-PRIME number? @_@]
find(52): false :: [-1:]
---------------------------------------------------------------
The print anatomy:
[003] [345 : item__345][668 : item__668][801 : item__801]
. . . . . . . . . . . . . . . . . .
[003]: table index.
[345 : item__345][668 : item__668]...: records inserted: inorder()
---------------------------------------------------------------
52 was removed successfully:
[000] [209:item__209][342:item__342]
[001]
[002] [230:item__230]
[003] [573:item__573][592:item__592][858:item__858][972:item__972]
[004] [42:This is the meaning of life? A NON-PRIME number? @_@][707:item__707]
[005] [423:item__423][765:item__765]
[006]
[007]
[008] [844:item__844]
[009]
[010]
[011]
[012] [449:item__449][487:item__487]
[013] [640:item__640]
[014]
[015]
[016]
[017] [378:item__378]
[018]
---------------------------------------------------------------
chained_hash_2 copy from chained_hash:
[000] [209:item__209][342:item__342]
[001]
[002] [230:item__230]
[003] [573:item__573][592:item__592][858:item__858][972:item__972]
[004] [42:This is the meaning of life? A NON-PRIME number? >_<][707:item__707]
[005] [423:item__423][765:item__765]
[006]
[007]
[008] [844:item__844]
[009]
[010]
[011]
[012] [449:item__449][487:item__487]
[013] [640:item__640]
[014]
[015]
[016]
[017] [378:item__378]
[018]
---------------------------------------------------------------
chained_hash_3 copy from chained_hash_2:
[000] [209:item__209][342:item__342]
[001]
[002] [230:item__230]
[003] [573:item__573][592:item__592][858:item__858][972:item__972]
[004] [707:item__707]
[005] [423:item__423][765:item__765]
[006]
[007]
[008] [844:item__844]
[009]
[010]
[011]
[012] [449:item__449][487:item__487]
[013] [640:item__640]
[014]
[015]
[016]
[017] [378:item__378]
[018]
---------------------------------------------------------------
[ OK ] TEST_CHAINED_HASH.BasicTest (1 ms)
[----------] 1 test from TEST_CHAINED_HASH (1 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (1 ms total)
[ PASSED ] 1 test.
*/