File: ConfigurationParserTest.cpp

package info (click to toggle)
clutils 20031216-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,348 kB
  • ctags: 540
  • sloc: sh: 7,191; cpp: 4,128; makefile: 192
file content (300 lines) | stat: -rw-r--r-- 7,715 bytes parent folder | download
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
// Copyright (c) 2000-2002 Clifton Labs, Inc.  
// All rights reserved.

// Clifton Labs MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF 
// THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE, OR NON-INFRINGEMENT.  Clifton Labs SHALL NOT BE LIABLE
// FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
// RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
// DERIVATIVES.

// By using or copying this Software, Licensee agrees to abide by the
// intellectual property laws, and all other applicable laws of the
// U.S., and the terms of this license.

// Authors: Dale E. Martin   dmartin@cliftonlabs.com

#include <clutils/Debug.h>
#include <clutils/tokens.h>
#include <clutils/ConfigurationParserHandle.h>
#include <clutils/StringUtilities.h>
#include "ConfigurationParserTest.h"

#define CHECK(x) if( !(x) ){ retval = -1; goto end; }

ConfigurationParserTest *
ConfigurationParserTest::instance(){
  static ConfigurationParserTest *myConfigurationParserTest = new ConfigurationParserTest();
  
  return myConfigurationParserTest;
}


int
ConfigurationParserTest::checkConfig( const ConfigurationScope &outerScope ){
  int retval = 0;

  if( checkOuterScope( outerScope ) != 0 ){
    retval = -1;
    goto end;
  }

  if( checkDebugChoice( outerScope ) != 0 ){
    retval = -1;
    goto end;
  }

  if( checkSimulatorsScope( outerScope ) != 0 ){
    retval = -1;
    goto end;
  }

 end:
  return retval;
}

int
ConfigurationParserTest::checkOuterScope( const ConfigurationScope &outerScope ){
  int retval = 0;
  const vector<const ConfigurationChoice *> *outerChoices = 0;
  const vector<const ConfigurationScope *> *outerScopes = 0;


  if( outerScope.getScopeName()  != "" ){
    retval = -1;
    goto end;
  }

  outerChoices = outerScope.getNestedChoices();
  if( outerChoices == 0 || outerChoices->size() != 1 ){
    retval = -1;
    goto end;
  }

  outerScopes = outerScope.getNestedScopes();
  if( outerScopes == 0 || outerScopes->size() != 1 ){
    retval = -1;
    goto end;
  }

 end:
  delete outerChoices;
  return retval;
}

int
ConfigurationParserTest::checkDebugChoice( const ConfigurationScope &outerScope ){
  int retval = 0;
  const ConfigurationChoice *debugChoice = 0;
  const ConfigurationValue *debugValue = 0;
  const StringConfigurationValue *stringDebugValue = 0;

  
  debugChoice = outerScope.findChoice("debug");
  if( debugChoice == 0 ){
    retval = -1;
    goto end;
  }
  debugChoice = outerScope.findChoice("Debug");
  if( debugChoice == 0 ){
    retval = -1;
    goto end;
  }
  debugValue = debugChoice->getConfigurationValue();
  if( debugValue == 0 || dynamic_cast<const StringConfigurationValue *>(debugValue) == 0 ){
    retval = -1;
    goto end;
  }
  stringDebugValue = dynamic_cast<const StringConfigurationValue *>(debugValue);
  if( stringDebugValue->getStringValue() != "true" ){
    retval = -1;
    goto end;
  }

 end:
  return retval;
}

int
ConfigurationParserTest::checkSimulatorsScope( const ConfigurationScope &outerScope ){
  int retval = 0;

  const ConfigurationScope *simulatorsScope = 0;
  const ConfigurationScope *testSimulatorScope = 0;
  const vector<const ConfigurationScope *> *nestedScopes = 0;

  simulatorsScope = outerScope.findScope("simulators");
  CHECK( simulatorsScope != 0 );

  nestedScopes = simulatorsScope->getNestedScopes();
  CHECK( nestedScopes != 0 || nestedScopes->size() == 1 );

  testSimulatorScope = simulatorsScope->findScope("testsimulator");
  CHECK( testSimulatorScope != 0 );

  CHECK( checkTestSimulatorScope( *testSimulatorScope ) == 0 );

 end:
  delete nestedScopes;
  return retval;
}

int
ConfigurationParserTest::checkTestSimulatorScope( const ConfigurationScope &testSimulatorScope ){
  int retval = 0;

  const vector<const ConfigurationChoice *> *choices = 0;
  const ConfigurationChoice *modelsChoice = 0;
  const ConfigurationChoice *doublesChoice = 0;
  const ConfigurationChoice *intsChoice = 0;

  choices = testSimulatorScope.getNestedChoices();
  CHECK( choices != 0 || choices->size() == 3 );

  modelsChoice = testSimulatorScope.findChoice( "models" );
  CHECK( checkModelsChoice( modelsChoice ) == 0 );

  doublesChoice = testSimulatorScope.findChoice( "doubles" );
  CHECK( checkDoublesChoice( doublesChoice ) == 0 );

  intsChoice = testSimulatorScope.findChoice( "ints" );
  CHECK( checkIntsChoice( intsChoice ) == 0 );

 end:
  delete choices;
  return retval;
}

int
ConfigurationParserTest::checkModelsChoice( const ConfigurationChoice *modelsChoice ){
  int retval = 0;
  
  const ConfigurationValue *modelsValue = 0;
  const VectorConfigurationValue *modelsVectorValue = 0;
  const vector<const ConfigurationValue *> *values = 0;

  CHECK( modelsChoice != 0 );
    
  modelsValue = modelsChoice->getConfigurationValue();
  CHECK( modelsValue != 0 );

  modelsVectorValue = dynamic_cast<const VectorConfigurationValue *>(modelsValue);
  CHECK( modelsVectorValue != 0 );

  values = modelsVectorValue->getVectorValue();
  CHECK( values != 0 );
  CHECK( values->size() == 4 );

  CHECK( (*values)[0]->getStringValue() == "foo" );
  CHECK( (*values)[1]->getStringValue() == "bar" );
  CHECK( (*values)[2]->getStringValue() == "baz" );
  CHECK( (*values)[3]->getStringValue() == "baz2" );

 end:
  return retval;
}

bool
closeEnough( double one, double two ){
  double bigger, smaller;
  if( one > two ){
    bigger = one;
    smaller = two;
  }
  else{
    bigger = two;
    smaller = one;
  }
  
  return (bigger - smaller < 0.0001);
}

int
ConfigurationParserTest::checkDoublesChoice( const ConfigurationChoice *doublesChoice ){
  int retval = 0;
  
  const ConfigurationValue *doublesValue = 0;
  const VectorConfigurationValue *doublesVectorValue = 0;
  const vector<const ConfigurationValue *> *values = 0;

  CHECK( doublesChoice != 0 );
    
  doublesValue = doublesChoice->getConfigurationValue();
  CHECK( doublesValue != 0 );

  doublesVectorValue = dynamic_cast<const VectorConfigurationValue *>(doublesValue);
  CHECK( doublesVectorValue != 0 );

  values = doublesVectorValue->getVectorValue();
  CHECK( values != 0 );
  CHECK( values->size() == 2 );

  CHECK( closeEnough( (*values)[0]->getDoubleValue(), 2.71 ) );
  CHECK( closeEnough( (*values)[1]->getDoubleValue(), -2.17 ) );

 end:
  return retval;
}

int
ConfigurationParserTest::checkIntsChoice( const ConfigurationChoice *intsChoice ){
  int retval = 0;
  
  const ConfigurationValue *intsValue = 0;
  const VectorConfigurationValue *intsVectorValue = 0;
  const vector<const ConfigurationValue *> *values = 0;

  CHECK( intsChoice != 0 );
    
  intsValue = intsChoice->getConfigurationValue();
  CHECK( intsValue != 0 );

  intsVectorValue = dynamic_cast<const VectorConfigurationValue *>(intsValue);
  CHECK( intsVectorValue != 0 );

  values = intsVectorValue->getVectorValue();
  CHECK( values != 0 );
  CHECK( values->size() == 4 );

  CHECK( (*values)[0]->getIntValue() == -1 );
  CHECK( (*values)[1]->getIntValue() == 0 );
  CHECK( (*values)[2]->getIntValue() == 12 );
  CHECK( (*values)[3]->getIntValue() == 69 );

 end:
  return retval;
}


int 
ConfigurationParserTest::parseFileTest(){
  int retval = 0;

  const string testFileName = "configurationParserTest.cfg";
  const ConfigurationScope *outerScope = ConfigurationParserHandle::parseFile( testFileName );
  
  if( outerScope == 0 ){
    retval = -1;
  }
  else{
    retval = checkConfig( *outerScope );
  }

  return retval;
}

int 
ConfigurationParserTest::regressionTest(){
  int retval = 0;

  clutils::enableDebug();

  retval = parseFileTest();
  if( retval != 0 ){
    goto end;
  }

 end:
  return retval;
}