-
Notifications
You must be signed in to change notification settings - Fork 7
/
Drill 4
275 lines (225 loc) · 6.07 KB
/
Drill 4
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// -----------------------------------------------------------------------------
// https://lptcp.blogspot.com/
//
/*
Chapter 26 - Drill 4
Repeat these tests for sequences of strings, such as
{ Bohr Darwin Einstein Lavoisier Newton Turing }.
*/
// https://github.com/l-paz91/principles-practice/
// -----------------------------------------------------------------------------
//--INCLUDES--//
#include "std_lib_facilities.h"
// -----------------------------------------------------------------------------
template<typename T>
struct Test
{
vector<T> mSeq;
string mLabel;
T mVal;
bool mRes;
};
// -----------------------------------------------------------------------------
template<typename T>
istream& operator>>(istream& pInputStream, Test<T>& pTest)
{
char c;
pInputStream >> c;
if (pInputStream.eof())
{
return pInputStream;
}
if (c != '{')
{
error("Test::operator>> test string should open with { ");
return pInputStream;
}
pInputStream >> pTest.mLabel;
pInputStream >> pTest.mVal;
pInputStream >> c;
if (c != '{')
{
error("Test::operator>> test string sequence should open with { ");
return pInputStream;
}
T i;
while (pInputStream >> i)
{
if (to_string(i) == "}")
{
pInputStream.putback(i[0]);
break;
}
pTest.mSeq.push_back(i);
}
pInputStream.clear();
pInputStream >> c;
if (c != '}')
{
error("Test::operator>> test string sequence should end with } ");
return pInputStream;
}
pInputStream >> pTest.mRes;
pInputStream >> c;
return pInputStream;
}
// -----------------------------------------------------------------------------
template<typename T>
ostream& operator<<(ostream& pOutputStream, const Test<T>& pTest)
{
pOutputStream << "{ " << pTest.mLabel << " " << pTest.mVal << " {";
for (int i : pTest.mSeq)
pOutputStream << " " << i;
pOutputStream << "} " << pTest.mRes << " }";
return pOutputStream;
}
// -----------------------------------------------------------------------------
template<typename T>
void readInTestsFromFile(const string& pFilename, vector<Test<T>>& pTestVector)
{
ifstream testFile(pFilename);
if (testFile.is_open())
{
string line;
//cout << "Original tests from file: \n";
while (getline(testFile, line))
{
//cout << line << '\n';
istringstream testLineInput(line);
Test<T> tempTest;
testLineInput >> tempTest;
pTestVector.push_back(tempTest);
}
testFile.close();
}
else
{
error("readInTestsFromFile:: Couldn't open file!");
}
}
// -----------------------------------------------------------------------------
template<typename T>
int testAll(istream& pInputStream)
{
int errorCount = 0;
for (Test<T> t; pInputStream >> t;)
{
// sort sequence first
sort(t.mSeq);
bool result = binary_search(t.mSeq.begin(), t.mSeq.end(), t.mVal);
if (result != t.mRes)
{
cout << "Failure: test " << t.mLabel
<< "Looking for: " << t.mVal << ", test should be: " << t.mRes
<< " binary_search: " << t.mSeq.size() << " elements, val == " << t.mVal
<< " -> " << t.mRes << '\n';
++errorCount;
}
}
return errorCount;
}
// -----------------------------------------------------------------------------
// write a tests description with the label pLabel to cout
// generate a sequence of n elements starting at pBase
// the average distance between elements is uniformly distributed in [0:pSpread)
void makeTest(ofstream& pOutputStream, const string& pLabel, int pNumOfTests, int pBase, int pSpread)
{
vector<string> vec;
string element;
string value = randString(pNumOfTests - pBase); // make search value
pOutputStream << "{ " << pLabel << " " << value << " { ";
for (int i = 0; i < pNumOfTests; ++i)
{
element += randString(pSpread);
vec.push_back(element);
}
bool found = false;
for (int i = 0; i < pNumOfTests; ++i)
{
// print elements and see if value is found
if (vec[i] == value)
{
found = true;
}
pOutputStream << vec[i] << " ";
}
pOutputStream << " } " << found << " }\n";
}
// -----------------------------------------------------------------------------
template<typename T>
void drillA()
{
const string filename = "drillATests.txt";
ofstream exampleTests(filename);
// make a few random tests
int numOfTests = 2;
for (int i = 0; i < numOfTests; ++i)
{
string label = "drillA";
makeTest(exampleTests, label + to_string(i), 5000, 0, 1);
}
exampleTests.close();
ifstream inputStream(filename);
int searchResult = testAll<T>(inputStream);
if (searchResult == 0)
{
cout << "No errors found.\n";
}
}
// -----------------------------------------------------------------------------
template<typename T>
void drillB()
{
const string filename = "drillBTests.txt";
ofstream exampleTests(filename);
// make a few random tests
int numOfTests = 10;
for (int i = 0; i < numOfTests; ++i)
{
string label = "drillB";
makeTest(exampleTests, label + to_string(i), randint(500), 0, randint(50));
}
exampleTests.close();
ifstream inputStream(filename);
int searchResult = testAll<T>(inputStream);
if (searchResult == 0)
{
cout << "No errors found.\n";
}
}
// -----------------------------------------------------------------------------
template<typename T>
void drillC()
{
const string filename = "drillCTests.txt";
ofstream exampleTests(filename);
// make a few random tests
int numOfTests = 10;
for (int i = 0; i < numOfTests; ++i)
{
string label = "drillC";
makeTest(exampleTests, label + to_string(i), 10, 0, 1);
}
exampleTests.close();
ifstream inputStream(filename);
int searchResult = testAll<T>(inputStream);
if (searchResult == 0)
{
cout << "No errors found.\n";
}
}
// -----------------------------------------------------------------------------
int main()
{
drillA<string>();
drillB<string>();
drillC<string>();
const string filename = "drillDTests.txt";
ifstream inputStream(filename);
int searchResult = testAll<string>(inputStream);
pressKeyToContinue();
return 0;
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------