Debian Codegraph @

Testing/kwsTestMain.h

raw
1 /*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18 // Put kwssys/Configure.h first for proper compiler warning suppression:
19 #include <kwssys/Configure.h>
20
21 // This file is used to create TestDriver executables
22 // These executables are able to register a function pointer to a string name
23 // in a lookup table. By including this file, it creates a main function
24 // that calls RegisterTests() then looks up the function pointer for the test
25 // specified on the command line.
26 #include <map>
27 #include <string>
28 #include <iostream>
29 #include <fstream>
30 #include <string.h> // strcmp
31 #include <stdlib.h> // atoi
32 #include "kwssys/SystemTools.hxx"
33
34 #define ITK_TEST_DIMENSION_MAX 6
35
36 typedef int (*MainFuncPointer)(int , char* [] );
37 std::map<std::string, MainFuncPointer> StringToTestFunctionMap;
38
39 #define REGISTER_TEST(test) \
40 extern int test(int, char* [] ); \
41 StringToTestFunctionMap[#test] = test
42
43 int RegressionTestImage (const char *, const char *, int);
44 std::map<std::string,int> RegressionTestBaselines (char *);
45
46 void RegisterTests();
47 static void PrintAvailableTests()
48 {
49 std::cout << "Available tests:\n";
50 std::map<std::string, MainFuncPointer>::iterator j = StringToTestFunctionMap.begin();
51 int i = 0;
52 while(j != StringToTestFunctionMap.end())
53 {
54 std::cout << i << ". " << j->first << "\n";
55 ++i;
56 ++j;
57 }
58 }
59
60 /** Compare the baseline and the testfile */
61 static int Compare(const char* baseline, const char* testfile)
62 {
63 // Load the baseline
64 std::ifstream baselineFile;
65 baselineFile.open(baseline, std::ios::binary | std::ios::in);
66 if(!baselineFile.is_open())
67 {
68 std::cout << "Cannot open baselineFile for reading: " << baseline << std::endl;
69 return 1;
70 }
71
72 baselineFile.seekg(0,std::ios::end);
73 unsigned long fileSize = baselineFile.tellg();
74 baselineFile.seekg(0,std::ios::beg);
75
76 char* buf = new char[fileSize+1];
77 baselineFile.read(buf,fileSize);
78 buf[fileSize] = 0;
79 std::string baselineBuffer(buf);
80 baselineBuffer.resize(fileSize);
81 delete [] buf;
82 baselineFile.close();
83
84 // Load the testfile
85 std::ifstream testfileFile;
86 testfileFile.open(testfile, std::ios::binary | std::ios::in);
87 if(!testfileFile.is_open())
88 {
89 std::cout << "Cannot open testfileFile for reading: " << testfile << std::endl;
90 return 1;
91 }
92
93 testfileFile.seekg(0,std::ios::end);
94 fileSize = testfileFile.tellg();
95 testfileFile.seekg(0,std::ios::beg);
96
97 buf = new char[fileSize+1];
98 testfileFile.read(buf,fileSize);
99 buf[fileSize] = 0;
100 std::string testfileBuffer(buf);
101 testfileBuffer.resize(fileSize);
102 delete [] buf;
103 testfileFile.close();
104
105 // Go through the testfile and check if we have the same errors than the
106 // baseline
107 long int posError = static_cast<long int>(testfileBuffer.find("<Error>"));
108 long int posErrorBase = static_cast<long int>(testfileBuffer.find("<Error>"));
109
110 while(posError != -1)
111 {
112 long int posErrorEnd = static_cast<long int>(testfileBuffer.find("</Error>",posError));
113 std::string subError = testfileBuffer.substr(posError,posErrorEnd-posError);
114 long int line1a = static_cast<long int>(subError.find("<SourceLineNumber1>"));
115 long int line1b = static_cast<long int>(subError.find("</SourceLineNumber1>"));
116 long int line2a = static_cast<long int>(subError.find("<SourceLineNumber2>"));
117 long int line2b = static_cast<long int>(subError.find("</SourceLineNumber2>"));
118 long int errorNumbera = static_cast<long int>(subError.find("<ErrorNumber>"));
119 long int errorNumberb = static_cast<long int>(subError.find("</ErrorNumber>"));
120 unsigned int l1 = atoi(subError.substr(line1a+19,line1b-line1a-19).c_str());
121 unsigned int l2 = atoi(subError.substr(line2a+19,line2b-line2a-19).c_str());
122 unsigned int errorNumber = atoi(subError.substr(errorNumbera+13,errorNumberb-errorNumbera-13).c_str());
123
124 // Check the baseline
125 if(posErrorBase == -1)
126 {
127 std::cout << "Error not found" << std::endl;
128 return 1;
129 }
130 long int posErrorEndBase = static_cast<long int>(baselineBuffer.find("</Error>",posErrorBase));
131 std::string subErrorBase = baselineBuffer.substr(posErrorBase,posErrorEndBase-posErrorBase);
132 long int line1aBase = static_cast<long int>(subErrorBase.find("<SourceLineNumber1>"));
133 long int line1bBase = static_cast<long int>(subErrorBase.find("</SourceLineNumber1>"));
134 long int line2aBase = static_cast<long int>(subErrorBase.find("<SourceLineNumber2>"));
135 long int line2bBase = static_cast<long int>(subErrorBase.find("</SourceLineNumber2>"));
136 long int errorNumberaBase = static_cast<long int>(subErrorBase.find("<ErrorNumber>"));
137 long int errorNumberbBase = static_cast<long int>(subErrorBase.find("</ErrorNumber>"));
138 unsigned int l1Base = atoi(subErrorBase.substr(line1aBase+19,line1bBase-line1aBase-19).c_str());
139 unsigned int l2Base = atoi(subErrorBase.substr(line2aBase+19,line2bBase-line2aBase-19).c_str());
140 unsigned int errorNumberBase = atoi(subErrorBase.substr(errorNumberaBase+13,errorNumberbBase-errorNumberaBase-13).c_str());
141
142 if(l1 != l1Base || l2 != l2Base || errorNumber!=errorNumberBase)
143 {
144 std::cout << "Errors differ:" << std::endl;
145 std::cout << "l1 = " << l1 << " v.s. " << l1Base << std::endl;
146 std::cout << "l2 = " << l2 << " v.s. " << l2Base << std::endl;
147 std::cout << "errorNumber = " << errorNumber << " v.s. " << errorNumberBase << std::endl;
148 return 1;
149 }
150
151 posError = static_cast<long int>(testfileBuffer.find("<Error>",posError+1));
152 posErrorBase = static_cast<long int>(baselineBuffer.find("<Error>",posErrorBase+1));
153 }
154
155 // Check that we don't have any other errors in the baseline
156 if(posErrorBase != -1)
157 {
158 std::cout << "The baseline as more errors than the test" << std::endl;
159 return 1;
160 }
161
162 return 0;
163 }
164
165 int main(int ac, char* av[] )
166 {
167 char *baselineFilename = nullptr;
168 char *testFilename = nullptr;
169
170 RegisterTests();
171 std::string testToRun;
172 if(ac < 2)
173 {
174 PrintAvailableTests();
175 std::cout << "To run a test, enter the test number: ";
176 int testNum = 0;
177 std::cin >> testNum;
178 std::map<std::string, MainFuncPointer>::iterator j = StringToTestFunctionMap.begin();
179 int i = 0;
180 while(j != StringToTestFunctionMap.end() && i < testNum)
181 {
182 ++i;
183 ++j;
184 }
185 if(j == StringToTestFunctionMap.end())
186 {
187 std::cerr << testNum << " is an invalid test number\n";
188 return -1;
189 }
190 testToRun = j->first;
191 }
192 else
193 {if (ac > 3 && strcmp(av[1], "--compare") == 0)
194 {
195 baselineFilename = av[2];
196 testFilename = av[3];
197 av += 3;
198 ac -= 3;
199 }
200 testToRun = av[1];
201 }
202 std::map<std::string, MainFuncPointer>::iterator j = StringToTestFunctionMap.find(testToRun);
203 if(j != StringToTestFunctionMap.end())
204 {
205 MainFuncPointer f = j->second;
206 int result;
207 try
208 {
209 // Invoke the test's "main" function.
210 result = (*f)(ac-1, av+1);
211
212 // Make a list of possible baselines
213 if (baselineFilename && testFilename)
214 {
215 result = Compare(baselineFilename,testFilename);
216 }
217 }
218 catch(const std::exception& e)
219 {
220 std::cerr << "Test driver caught an exception:\n";
221 std::cerr << e.what() << "\n";
222 result = -1;
223 }
224 catch(...)
225 {
226 std::cerr << "Test driver caught an unknown exception!!!\n";
227 result = -1;
228 }
229 return result;
230 }
231 PrintAvailableTests();
232 std::cerr << "Failed: " << testToRun << ": No test registered with name " << testToRun << "\n";
233 return -1;
234 }