testdriver-after-test.inc
Go to the documentation of this file.
00001 /** 00002 * @file testdriver-after-test.inc 00003 * @brief Default implementation of test driver. 00004 * 00005 * This file is included in the test driver generated by the CMake command 00006 * <a href="http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:create_test_sourcelist"> 00007 * create_test_sourcelist()</a> directly after the call to the test main function. 00008 * It performs regression testing if requested. 00009 * 00010 * This file is a modified version of the itkTestDriverBeforeTest.inc file 00011 * which is part of the TestKernel module of the ITK 4 project. 00012 * 00013 * Copyright (c) 2011 University of Pennsylvania.<br /> 00014 * Copyright Insight Software Consortium.<br /> 00015 * All rights reserved. 00016 * 00017 * Licensed under the Apache License, Version 2.0 (the "License"); 00018 * you may not use this file except in compliance with the License. 00019 * You may obtain a copy of the License at 00020 * 00021 * http://www.apache.org/licenses/LICENSE-2.0.txt 00022 * 00023 * Unless required by applicable law or agreed to in writing, software 00024 * distributed under the License is distributed on an "AS IS" BASIS, 00025 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00026 * See the License for the specific language governing permissions and 00027 * limitations under the License. 00028 * 00029 * Contact: SBIA Group <sbia-software at uphs.upenn.edu> 00030 */ 00031 00032 //int main(int, char*) 00033 //{ 00034 // #include <sbia/basis/testdriver-before-test.inc> -> try { 00035 // [...] 00036 00037 // revert redirection of output 00038 if (oldcoutbuf) { 00039 cout.rdbuf(oldcoutbuf); 00040 redirectstream.close(); 00041 } 00042 // perform regression tests of output image(s) 00043 if (result == 0) { 00044 if (!regression_tests.empty() && verbose.getValue() > 0) { 00045 cout << "Performing regression tests" << endl; 00046 } 00047 for (size_t i = 0; i < regression_tests.size(); i++) { 00048 const char* test_file = regression_tests[i].test_file.c_str(); 00049 const char* baseline_file = regression_tests[i].baseline_file.c_str(); 00050 if (regression_tests[i].method == BINARY_DIFF) { 00051 int status = binary_diff(test_file, baseline_file); 00052 if (status == -1) { 00053 cerr << "No test output file found given file path " << test_file << "!" << endl; 00054 status = 1; 00055 } else if (status == -2) { 00056 cerr << "No baseline file found given file path " << baseline_file << "!" << endl; 00057 status = 1; 00058 } else if (status != 0) { 00059 cerr << "Files " << test_file << " and " << baseline_file << " differ!" << endl; 00060 } 00061 result += status; 00062 } else if (regression_tests[i].method == DIFF_LINES) { 00063 int status = text_diff_lines(test_file, baseline_file, regression_tests[i].max_number_of_differences); 00064 if (status == -1) { 00065 cerr << "No test output file found given file path " << test_file << "!" << endl; 00066 status = 1; 00067 } else if (status == -2) { 00068 cerr << "No baseline file found given file path " << baseline_file << "!" << endl; 00069 status = 1; 00070 } else if (status != 0) { 00071 cerr << "Files " << test_file << " and " << baseline_file << " differ by more than the allowed " << regression_tests[i].max_number_of_differences << " lines!" << endl; 00072 } 00073 result += status; 00074 } else if (regression_tests[i].method == COMPARE_IMAGES) { 00075 vector<string> baseline_files = get_baseline_filenames(baseline_file); 00076 string bestmatch = baseline_file; 00077 int beststatus = numeric_limits<int>::max(); 00078 if (baseline_files.empty()) { 00079 bestmatch += " not found"; 00080 cerr << "No baseline images found given file path " << baseline_file << "!" << endl; 00081 } else { 00082 for (size_t j = 0; j < baseline_files.size(); j++) { 00083 int status = image_regression_test( 00084 test_file, 00085 baseline_files[j].c_str(), 00086 regression_tests[i].intensity_tolerance, 00087 regression_tests[i].max_number_of_differences, 00088 regression_tests[i].tolerance_radius, 00089 regression_tests[i].orientation_insensitive); 00090 if (status < beststatus) { 00091 bestmatch = baseline_files[j]; 00092 beststatus = status; 00093 } 00094 if (beststatus == 0) { 00095 // perfect test result 00096 break; 00097 } 00098 } 00099 // if the best we can do still has errors... 00100 if (beststatus != 0) { 00101 image_regression_test( 00102 test_file, 00103 baseline_files[0].c_str(), 00104 regression_tests[i].intensity_tolerance, 00105 regression_tests[i].max_number_of_differences, 00106 regression_tests[i].tolerance_radius, 00107 regression_tests[i].orientation_insensitive, 00108 1); // ...generate error images 00109 } 00110 } 00111 // output the matching baseline for submission to the dashboard 00112 cout << "<DartMeasurement name=\"BaselineImageName\" type=\"text/string\">"; 00113 cout << os::path::basename(bestmatch); 00114 cout << "</DartMeasurement>" << std::endl; 00115 result += beststatus; 00116 } else { 00117 cerr << "Invalid test method: " << regression_tests[i].method << "! Check testdriver implementation." << endl; 00118 } 00119 } 00120 // empty current working directory 00121 if (clean_cwd_after_test.getValue()) { 00122 os::emptydir(os::getcwd().c_str()); 00123 } 00124 } 00125 00126 // catch any exceptions 00127 } catch (const exception& e) { 00128 cerr << "Test driver caught an exception:\n"; 00129 cerr << e.what() << "\n"; 00130 result = -1; 00131 } catch (...) { 00132 cerr << "Test driver caught an unknown exception!!!\n"; 00133 result = -1; 00134 } 00135 //} end of main()