summaryrefslogtreecommitdiff
path: root/ARMFCaptureD3D/ConfigFile/tester.cpp
blob: ff82c27811d84720ab56a61bace465f48de598f1 (plain)
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// tester.cpp
// Program to test ConfigFile class

#include <string>
#include <iostream>
#include "ConfigFile.h"
#include "Triplet.h"

using std::string;
using std::cout;
using std::endl;

string title = "";
bool success = true;
int errors = 0;

void announce( const string& name )
{
	title = name;
	success = true;
	cout << "Test " << title << ":" << endl;
}

void judge()
{
	if( success )
	{
		cout << "Passed " << title << "." << endl << endl;
		return;
	}
	cout << "Error - Failed " << title << "." << endl << endl;
	++errors;
}

void evaluate( const bool& test )
{
	success = success && test;
}

int main( void )
{
	cout << "This is a test of the ConfigFile class." << endl;
	
	try {
	
	ConfigFile cf( "test.inp" );
	
	cout << "Here is the configuration read from test.inp:" << endl;
	cout << cf << endl;
	
	// Test reading of basic types
	
	announce("reading of basic types");
	
	int valInt = cf.read<int>( "integer", 0 );
	cout << "Value of 'integer' is " << valInt << endl;
	evaluate( valInt == 7 );
	
	double valDbl = cf.read<double>( "double", 0.0 );
	cout << "Value of 'double' is " << valDbl << endl;
	evaluate( valDbl == 1.99 );
	
	bool valBoo = cf.read<bool>( "boolean", false );
	cout << "Value of 'boolean' is " << valBoo << endl;
	evaluate( valBoo );
	
	string valStr = cf.read<string>( "string", "nothing" );
	cout << "Value of 'string' is " << valStr << endl;
	evaluate( valStr == "one fine day" );
	
	judge();
	
	// Test reading by different methods
	
	announce("reading by different methods");
	
	int methodExplicit = cf.read<int>( "integer" );
	cout << "Read integer explicitly as " << methodExplicit << endl;
	evaluate( methodExplicit == 7 );
	
	int methodDefault = cf.read( "integer", 0 );
	cout << "Read integer with default as " << methodDefault << endl;
	evaluate( methodDefault == 7 );
	
	int valInto = 0;
	bool methodInto = cf.readInto( valInto, "integer" );
	cout << "Read integer into variable as " << valInto << endl;
	evaluate( methodInto );
	
	methodInto = cf.readInto( valInto, "integer", 0 );
	cout << "Read integer into variable with default as " << valInto << endl;
	evaluate( methodInto );
	
	judge();
	
	// Test interpretation as different types
	
	announce("interpretation as different types");
	
	string typeStr = cf.read<string>( "weight", "nothing" );
	cout << "Value of weight as a string is " << typeStr << endl;
	evaluate( typeStr == "2.5 kg" );
	
	double typeDbl = cf.read<double>( "weight", 0.0 );
	cout << "Value of weight as a double is " << typeDbl << endl;
	evaluate( typeDbl == 2.5 );
	
	int typeInt = cf.read<int>( "weight", 0 );
	cout << "Value of weight as an integer is " << typeInt << endl;
	evaluate( typeInt == 2 );
	
	judge();
	
	// Test reading of user-defined types
	
	announce("reading of user-defined types");
	
	Triplet trip = cf.read<Triplet>( "triplets" );
	cout << "First Triplet in 'triplets' is " << trip << endl;
	evaluate( trip.a==1 && trip.b==2 && trip.c==3 );
	
	judge();
	
	// Test reading of repeated keys
	
	announce("reading of repeated keys");
	
	int repeat = cf.read<int>( "repeated" );
	cout << "Value of 'repeated' is " << repeat << endl;
	evaluate( repeat == 2 );
	
	judge();
	
	// Test case-sensitivity of key recognition
	
	announce("case-sensitivity of key recognition");
	
	int oneStall = cf.read<int>( "oneStall" );
	cout << "Value of oneStall is " << oneStall << endl;
	evaluate( oneStall == 1 );
	
	int onesTall = cf.read<int>( "onesTall" );
	cout << "Value of onesTall is " << onesTall << endl;
	evaluate( onesTall == 111 );
	
	judge();
	
	// Test recognition of keys with embedded spaces
	
	announce("recognition of keys with embedded spaces");
	
	bool spaceInKey = cf.read<bool>( "space key", false );
	cout << "Value of 'space key' is " << spaceInKey << endl;
	evaluate( spaceInKey );
	
	judge();
	
	// Test legality of all-space values
	
	announce("legality of all-space values");
	
	string noValue = cf.read<string>( "noValue", "something" );
	cout << "Value of 'noValue' is " << noValue << endl;
	evaluate( noValue == "" );
	
	judge();
	
	// Test legality of all-space keys
	
	announce("legality of all-space keys");
	
	int spaceKey = cf.read<int>( "" );
	cout << "Value of nothing is " << spaceKey << endl;
	evaluate( spaceKey == 5 );
	
	judge();
	
	// Test reading of values that include a delimiter
	
	announce("reading of values that include a delimiter");
	
	string equation = cf.read<string>( "equation" );
	cout << "Value of 'equation' is " << equation << endl;
	evaluate( equation == "y = mx + b" );
	
	judge();
	
	// Test termination of multiple-line values by blank lines
	
	announce("termination of multiple-line values by blank lines");
	
	string multiPause = cf.read<string>( "multilinePause" );
	cout << "Value of 'multilinePause' is " << multiPause << endl;
	evaluate( multiPause.find("third")  != string::npos &&
	          multiPause.find("fourth") == string::npos );
	
	judge();
	
	// Test continuation of multiple-line values after comments
	
	announce("continuation of multiple-line values after comments");
	
	string multiComment = cf.read<string>( "multilineComment" );
	cout << "Value of 'multilineComment' is " << multiComment << endl;
	evaluate( multiComment.find("fourth") != string::npos );
	
	judge();
	
	// Test skipping of commented lines in multiple-line values
	
	announce("skipping of commented lines in multiple-line values");
	
	string multiSkip = cf.read<string>( "multilineSkip" );
	cout << "Value of 'multilineSkip' is " << multiSkip << endl;
	evaluate( multiSkip.find("third")  == string::npos &&
	          multiSkip.find("fourth") != string::npos );
	
	judge();
	
	// Test skipping of assignments within comments
	
	announce("skipping of assignments within comments");
	
	int postComment = cf.read<int>( "postComment", 0 );
	cout << "Value of 'postComment' is " << postComment << endl;
	evaluate( postComment == 0 );
	
	judge();
	
	// Test alternative delimiters
	
	announce("alternative delimiters");
	string cfDelim = cf.getDelimiter();
	
	int atDelimiter = cf.read<int>( "atDelimiter", 0 );
	cout << "Value of 'atDelimiter' with '" << cfDelim;
	cout << "' delimiter is " << atDelimiter << endl; 
	evaluate( atDelimiter == 0 );
	
	ConfigFile atcf( "test.inp", "@" );
	atDelimiter = atcf.read<int>( "atDelimiter", 0 );
	cout << "Value of 'atDelimiter' with '" << atcf.getDelimiter();
	cout << "' delimiter is " << atDelimiter << endl; 
	evaluate( atDelimiter == 7 );
	
	judge();
	
	// Test alternative comment separators
	
	announce("alternative comment separators");
	string cfComm = cf.getComment();
	
	int altComment = cf.read<int>( "! alternateComment", 0 );
	cout << "Value of '! alternateComment' with '" << cfComm;
	cout << "' comment separator is " << altComment << endl; 
	evaluate( altComment == 9 );
	
	altComment = cf.read<int>( "alternateComment", 0 );
	cout << "Value of 'alternateComment'   with '" << cfComm;
	cout << "' comment separator is " << altComment << endl; 
	evaluate( altComment == 0 );
	
	ConfigFile excf( "test.inp", cf.getDelimiter(), "!" );
	altComment = excf.read<int>( "! alternateComment", 0 );
	cout << "Value of '! alternateComment' with '" << excf.getComment();
	cout << "' comment separator is " << altComment << endl; 
	evaluate( altComment == 0 );
	
	altComment = excf.read<int>( "alternateComment", 0 );
	cout << "Value of 'alternateComment'   with '" << excf.getComment();
	cout << "' comment separator is " << altComment << endl; 
	evaluate( altComment == 0 );
	
	judge();
	
	// Test legality of a space as a delimiter
	
	announce("legality of a space as a delimiter");
	
	int spaceDelimiter = cf.read<int>( "spaceDelimiter", 0 );
	cout << "Value of 'spaceDelimiter' with '" << cfDelim;
	cout << "' delimiter is " << spaceDelimiter << endl; 
	evaluate( spaceDelimiter == 0 );
	
	ConfigFile spcf( "test.inp", " " );
	spaceDelimiter = spcf.read<int>( "spaceDelimiter", 0 );
	cout << "Value of 'spaceDelimiter' with '" << spcf.getDelimiter();
	cout << "' delimiter is " << spaceDelimiter << endl; 
	evaluate( spaceDelimiter == 7 );
	
	judge();
	
	// Test interaction of assignments with end of file sentry
	
	announce("interaction of assignments with end of file sentry");
	
	string endStr = cf.read<string>( "end" );
	cout << "Value of 'end' with '" << cf.getSentry();
	cout << "' sentry is " << endStr << endl;
	evaluate( endStr == "before uncommented sentry" );
	
	ConfigFile eofcf( "test.inp", cfDelim, cfComm, "" );
	endStr = eofcf.read<string>( "end" );
	cout << "Value of 'end' with '" << eofcf.getSentry();
	cout << "' sentry is " << endStr << endl;
	evaluate( endStr == "before EOF" );
	judge();
	
	// Report results
	
	} catch( ConfigFile::file_not_found& e ) {
		cout << "Error - File '" << e.filename << "' not found.";
		cout << endl << endl;
		++errors;
	} catch( ConfigFile::key_not_found& e ) {
		cout << "Error - Key '" << e.key << "' not found.";
		cout << endl << endl;
		++errors;
	}
	
	if( errors > 0 )
	{
		cout << "Failed " << errors << " tests of ConfigFile.\n";
		cout << "Please send a copy of this output to wagnerr@umich.edu.\n";
		return 1;
	}
	
	cout << "Passed all tests of ConfigFile." << endl;
	
	return 0;
}