24 #include "otbConfigure.h"
30 #include "itkMultiThreader.h"
37 #include "itkMersenneTwisterRandomVariateGenerator.h"
42 #define REGISTER_TEST(test) \
43 extern int test(int, char* []); \
44 StringToTestFunctionMap[#test] = test
49 std::cout <<
"Tests available:\n";
54 std::cout << i <<
". " << j->first <<
"\n";
65 itk::Statistics::MersenneTwisterRandomVariateGenerator::GetInstance()->SetSeed(121212);
71 bool CheckOption(
char* arg,
const char* token,
int ac,
int min)
73 if (strcmp(arg, token) == 0)
81 std::cerr <<
"Can't parse token '" << token <<
"', need at least " << min - 1 <<
" arguments after.\n";
88 int main(
int ac,
char* av[])
90 bool lFlagRegression(
false);
91 std::vector<double> toleranceDiffValues;
92 std::vector<double> toleranceOgrValues;
93 std::vector<double> toleranceMetaValues;
94 std::vector<double> toleranceAsciiValues;
95 bool lIgnoreOrder(
false);
96 double epsilonBoundary(0.0);
97 double lToleranceRatio(0.0);
100 StringList baselineFilenamesBinary;
101 StringList testFilenamesBinary;
102 StringList baselineFilenamesMetaData;
103 StringList testFilenamesMetaData;
104 StringList baselineFilenamesOgr;
105 StringList testFilenamesOgr;
107 StringList baselineFilenamesImage;
108 StringList testFilenamesImage;
109 StringList baselineFilenamesAscii;
110 StringList testFilenamesAscii;
111 StringList ignoredLines;
112 ignoredLines.clear();
120 std::string testToRun;
124 std::cout <<
"To launch a test, enter its number: ";
136 std::cerr << testNum <<
" is not a valid test number\n";
139 testToRun = j->first;
144 while (strncmp(av[1],
"--", 2) == 0 && ac0 > ac)
149 int numThreads = atoi(av[2]);
150 itk::MultiThreader::SetGlobalDefaultNumberOfThreads(numThreads);
154 else if (
CheckOption(av[1],
"--without-threads", ac, 1))
156 itk::MultiThreader::SetGlobalDefaultNumberOfThreads(1);
160 else if (
CheckOption(av[1],
"--ignore-order", ac, 1))
166 else if (
CheckOption(av[1],
"--epsilon-boundary", ac, 2))
168 epsilonBoundary = atof(av[2]);
172 else if (
CheckOption(av[1],
"--compare-image", ac, 4))
174 lFlagRegression =
true;
175 toleranceDiffValues.push_back((
double)(::atof(av[2])));
176 baselineFilenamesImage.push_back(av[3]);
177 testFilenamesImage.push_back(av[4]);
181 else if (
CheckOption(av[1],
"--compare-n-images", ac, 3))
183 lFlagRegression =
true;
185 unsigned int nbComparisons = (
unsigned int)(::atoi(av[3]));
186 double tol = (double)(::atof(av[2]));
187 toleranceDiffValues.reserve(toleranceDiffValues.size() + nbComparisons);
188 baselineFilenamesImage.reserve(baselineFilenamesImage.size() + nbComparisons);
189 testFilenamesImage.reserve(testFilenamesImage.size() + nbComparisons);
191 for (
unsigned int i = 0; i < nbComparisons; ++i)
193 toleranceDiffValues.push_back(tol);
194 baselineFilenamesImage.push_back(av[4 + 2 * i]);
195 testFilenamesImage.push_back(av[5 + 2 * i]);
197 av += 3 + 2 * nbComparisons;
198 ac -= 3 + 2 * nbComparisons;
200 else if (
CheckOption(av[1],
"--compare-binary", ac, 3))
202 lFlagRegression =
true;
203 baselineFilenamesBinary.reserve(1);
204 testFilenamesBinary.reserve(1);
205 baselineFilenamesBinary.push_back(av[2]);
206 testFilenamesBinary.push_back(av[3]);
210 else if (
CheckOption(av[1],
"--compare-n-binary", ac, 2))
212 lFlagRegression =
true;
213 unsigned int nbComparisons = (
unsigned int)(::atoi(av[2]));
214 baselineFilenamesBinary.reserve(nbComparisons);
215 testFilenamesBinary.reserve(nbComparisons);
217 for (
unsigned int i = 0; i < nbComparisons; ++i)
219 baselineFilenamesBinary.push_back(av[3 + 2 * i]);
220 testFilenamesBinary.push_back(av[4 + 2 * i]);
222 av += 2 + 2 * nbComparisons;
223 ac -= 2 + 2 * nbComparisons;
227 else if (
CheckOption(av[1],
"--compare-ascii", ac, 4))
229 lFlagRegression =
true;
230 toleranceAsciiValues.push_back((
double)(::atof(av[2])));
231 baselineFilenamesAscii.push_back(av[3]);
232 testFilenamesAscii.push_back(av[4]);
236 else if (
CheckOption(av[1],
"--compare-n-ascii", ac, 3))
238 lFlagRegression =
true;
239 double tol = (double)(::atof(av[2]));
241 unsigned int nbComparisons = (
unsigned int)(::atoi(av[3]));
242 baselineFilenamesAscii.reserve(baselineFilenamesAscii.size() + nbComparisons);
243 testFilenamesAscii.reserve(testFilenamesAscii.size() + nbComparisons);
245 for (
unsigned int i = 0; i < nbComparisons; ++i)
247 toleranceAsciiValues.push_back(tol);
248 baselineFilenamesAscii.push_back(av[4 + 2 * i]);
249 testFilenamesAscii.push_back(av[5 + 2 * i]);
251 av += 3 + 2 * nbComparisons;
252 ac -= 3 + 2 * nbComparisons;
254 else if (
CheckOption(av[1],
"--ignore-lines-with", ac, 2))
256 unsigned int nbIgnoredLines = (
unsigned int)(::atoi(av[2]));
257 for (
unsigned int i = 0; i < nbIgnoredLines; ++i)
259 ignoredLines.push_back(av[3 + i]);
261 av += 2 + nbIgnoredLines;
262 ac -= 2 + nbIgnoredLines;
266 else if (
CheckOption(av[1],
"--compare-metadata", ac, 4))
268 lFlagRegression =
true;
269 toleranceMetaValues.push_back((
double)(::atof(av[2])));
270 baselineFilenamesMetaData.push_back(av[3]);
271 testFilenamesMetaData.push_back(av[4]);
275 else if (
CheckOption(av[1],
"--compare-ogr", ac, 4))
277 lFlagRegression =
true;
278 toleranceOgrValues.push_back((
double)(::atof(av[2])));
279 baselineFilenamesOgr.push_back(av[3]);
280 testFilenamesOgr.push_back(av[4]);
284 else if (
CheckOption(av[1],
"--tolerance-ratio", ac, 2))
286 lToleranceRatio = (double)(::atof(av[2]));
303 int result = EXIT_SUCCESS;
308 std::cerr <<
"Failure: no test identified for '" << testToRun <<
"'\n";
318 result = (*f)(ac - 1, av + 1);
319 if (result != EXIT_SUCCESS)
321 std::cout <<
"-> Test EXIT FAILURE (" << result <<
")." << std::endl;
322 itkGenericExceptionMacro(<<
"Function returns EXIT_FAILURE (not from regression, failure inside the test)");
325 catch (
const std::exception& e)
327 std::cerr <<
"otbTestMain '" << testToRun <<
"': exception caught:" << std::endl;
328 std::cerr << e.what() << std::endl;
329 result = EXIT_FAILURE;
333 std::cerr <<
"otbTestMain '" << testToRun <<
"': unknown exception caught!" << std::endl;
334 result = EXIT_FAILURE;
337 bool checkBaseline =
true;
338 if (result != EXIT_SUCCESS)
340 checkBaseline =
false;
347 std::cout <<
" -> Test EXIT SUCCESS." << std::endl;
348 if (lFlagRegression ==
false)
350 std::cout <<
"------------- No control baseline tests -------------" << std::endl;
357 std::cout <<
"------------- Start control baseline tests -------------" << std::endl;
360 testHelper->SetIgnoreLineOrder(lIgnoreOrder);
361 testHelper->SetToleranceRatio(lToleranceRatio);
362 if (epsilonBoundary != 0.0)
364 testHelper->SetEpsilonBoundaryChecking(epsilonBoundary);
368 if ((baselineFilenamesImage.size() > 0) && (testFilenamesImage.size() > 0))
370 result += testHelper->RegressionTestAllImages(baselineFilenamesImage, testFilenamesImage, toleranceDiffValues);
375 if ((baselineFilenamesMetaData.size() > 0) && (testFilenamesMetaData.size() > 0))
377 result += testHelper->RegressionTestAllMetaData(baselineFilenamesMetaData, testFilenamesMetaData, toleranceMetaValues);
382 if ((baselineFilenamesAscii.size() > 0) && (testFilenamesAscii.size() > 0))
385 result += testHelper->RegressionTestAllDiff(baselineFilenamesAscii, testFilenamesAscii, toleranceAsciiValues, ignoredLines);
390 if ((baselineFilenamesBinary.size() > 0) && (testFilenamesBinary.size() > 0))
392 result += testHelper->RegressionTestAllBinary(baselineFilenamesBinary, testFilenamesBinary);
397 if ((baselineFilenamesOgr.size() > 0) && (testFilenamesOgr.size() > 0))
399 result += testHelper->RegressionTestAllOgr(baselineFilenamesOgr, testFilenamesOgr, toleranceOgrValues);
401 std::cout <<
"------------- End control baseline tests -------------" << std::endl;
403 catch (itk::ExceptionObject& e)
405 std::cerr <<
"otbTestMain 'control baseline test': ITK Exception thrown:" << std::endl;
406 std::cerr << e.GetFile() <<
":" << e.GetLine() <<
":" << std::endl;
407 std::cerr << e.GetDescription() << std::endl;
410 catch (std::bad_alloc& err)
412 std::cerr <<
"otbTestMain 'control baseline test': Exception bad_alloc thrown: " << std::endl;
413 std::cerr << (
char*)err.what() << std::endl;
416 catch (
const std::exception& e)
418 std::cerr <<
"otbTestMain 'control baseline test': std::exception thrown:" << std::endl;
419 std::cerr << e.what() << std::endl;
424 std::cerr <<
"otbTestMain 'control baseline test': Unknown exception thrown !" << std::endl;