-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
215 lines (192 loc) · 5.41 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include "HashTable.h"
int unitTests();
int main()
{
int score = unitTests();
istream_iterator<string> eos1;
cout << "Loading the complete works of Shakespeare into a vector..." << endl;
ifstream f("shakespeare.txt");
if(!f)
{
cout << "Failed opening shakespeare.txt" << endl;
return 1;
}
istream_iterator<string> iit(f);
vector<string> vShake(iit, eos1);
cout << vShake.size() << " words loaded." << endl;
cout << "Loading the English dictionary into the hash table..." << endl;
ifstream f2("words_alpha.txt");
if(!f2)
{
cout << "Failed opening words_alpha.txt" << endl;
return 1;
}
istream_iterator<string> iit2(f2);
HashTable<string> ht1(iit2, eos1);
cout << "Hash table loaded." << endl;
int foundCount = 0;
auto t1 = system_clock::now();
for(const string& str : vShake)
{
auto it = ht1.find(str);
if(it != ht1.end()) foundCount++;
}
auto t2 = system_clock::now();
cout << foundCount << " out of " << vShake.size() << " total words matched.\n";
cout << "Required number of matches: " << 519831 << endl;
long long int ms = std::chrono::duration_cast<std::chrono::milliseconds>(t2-t1).count();
cout << "Time to search hash table: " << ms << "ms" << endl;
cout << "Required time: < 400ms" << endl;
if(ms < 400 && foundCount == 519831)
{
cout << "passed. +5 points" << endl;
score += 5;
}
else
{
cout << "Failed." << endl;
}
cout << "Final score: " << score << "/15" << endl;
ofstream scoreOfs("score.txt");
scoreOfs << score << endl;
return 0;
}
int unitTests()
{
int score = 0;
cout << "Testing one insert and one find..." << endl;
HashTable<string> h1(3);
h1.insert("hello");
auto it1 = h1.find("hello");
auto itN = h1.find("nope");
if(it1 != h1.end() && itN == h1.end())
{
cout << "passed. +2 points" << endl;
score+=2;
}
else
{
cout << "failed." << endl;
}
cout << "Testing iterator access operator (*)..." << endl;
if(*it1 == "hello")
{
cout << "passed. +1 point" << endl;
score++;
}
else
{
cout << "failed." << endl;
}
cout << "Testing multiple inserts with collisions..." << endl;
h1.insert("world");
h1.insert("I");
h1.insert("Love");
h1.insert("C++");
auto it2 = h1.find("world");
auto it3 = h1.find("I");
auto it4 = h1.find("Love");
auto it5 = h1.find("C++");
if(*it1 == "hello" && *it2 == "world" && *it3 == "I" && *it4 == "Love" && *it5 == "C++" && itN == h1.end())
{
cout << "passed. +1 point" << endl;
score+=1;
}
else
{
cout << *it1 << *it2 << *it3 << *it4 << *it5 << endl;
cout << "failed." << endl;
}
cout << "Testing multiple inserts with collisions, different hash table..." << endl;
HashTable<string> h2(65535);
h2.insert("hello");
h2.insert("world");
h2.insert("I");
h2.insert("Love");
h2.insert("C++");
it1 = h2.find("hello");
it2 = h2.find("world");
it3 = h2.find("I");
it4 = h2.find("Love");
it5 = h2.find("C++");
if(*it1 == "hello" && *it2 == "world" && *it3 == "I" && *it4 == "Love" && *it5 == "C++" && itN == h1.end())
{
cout << "passed. +1 point" << endl;
score+=1;
}
else
{
cout << *it1 << *it2 << *it3 << *it4 << *it5 << endl;
cout << "failed." << endl;
}
cout << "Testing begin iterator..." << endl;
it1 = h1.begin();
it2 = h2.begin();
if(*it1 == "hello" && *it2 == "I")
{
cout << "passed. +1 point" << endl;
score++;
}
else
{
cout << "failed." << endl;
}
cout << "Testing one execution of ++ operator..." << endl;
++it1;
++it2;
if(*it1 == "I" && *it2 == "C++")
{
cout << "passed. +1 point" << endl;
score++;
}
else
{
cout << "failed." << endl;
}
cout << "Testing execution of ++ operator until it reaches end..." << endl;
bool passed = true;
++it1;
++it2;
if(!(*it1 == "Love" && *it2 == "Love")) passed = false;
++it1;
++it2;
if(!(*it1 == "C++" && *it2 == "world")) passed = false;
++it1;
++it2;
if(!(*it1 == "world" && *it2 == "hello")) passed = false;
++it1;
++it2;
if(!(it1 == h1.end() && it2 == h2.end())) passed = false;
if(passed)
{
cout << "passed. +2 points" << endl;
score+=2;
}
else
{
cout << "failed." << endl;
}
cout << "Testing ostream operator..." << endl;
ofstream testOfs("testOut.txt");
testOfs << h1 << h2 << endl;
testOfs.close();
vector<string> ans = {"hello","I","Love","C++","world","I","C++","Love","world","hello"};
istream_iterator<string> eos;
ifstream ifs("testOut.txt");
istream_iterator<string> iis(ifs);
vector<string> tst(iis, eos);
if(ans == tst)
{
cout << "passed. +1 point" << endl;
score++;
}
else
{
cout << "failed." << endl;
}
ifs.close();
ofstream scoreOfs("score.txt");
scoreOfs << score << endl;
cout << "Score: " << score << endl;
return score;
}