/
SimpleITKTestHarness.h
306 lines (248 loc) · 8.2 KB
/
SimpleITKTestHarness.h
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
/*=========================================================================
*
* Copyright Insight Software Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef __SimpleITKTestHarness_h
#define __SimpleITKTestHarness_h
#include <ostream>
#include <vector>
#include <iterator>
#include <algorithm>
// This is needed before the gtest include for lookup of the operator
// to work with clang 5.1
inline std::ostream& operator<< (std::ostream& os, const std::vector<double>& v)
{
if ( v.empty() )
{
return os << "[ ]";
}
os << "[ ";
std::copy( v.begin(), v.end()-1, std::ostream_iterator<double>(os, ", ") );
return os << v.back() << " ]";
}
#include <string>
#include <vector>
#include <gtest/gtest.h>
#include <SimpleITKTestHarnessPaths.h>
#include <itksys/SystemTools.hxx>
#include <itksys/Process.h>
#include "sitkImage.h"
#include "sitkImageFileReader.h"
#include "sitkHashImageFilter.h"
#include "sitkCommand.h"
// Class to help us find test data
//
/*
* DataFinder maintains several directory paths. It also
* helps us find executables.
*
* Set/GetDirectory -- Test Data directory
* should be ITK/Testing/Data
* Set/GetOutputDirectory -- Temporary directory
* SimpleITK-build/Testing/Temporary
* Set/GetRuntimeDirectory -- Where built executables are found
* SimpleITK-build/bin/$(OutDir)/
* Set/GetLibraryDirectory -- Where built libraries are found
* SimpleITK-build/lib
* GetOutputFile -- File in the temp directory
* GetBuildDirectory -- SimpleITK-build
* FindExecutable -- Attempts to find an executable
* Returns GetExecutableDirectory + "/" + filename
*/
class DataFinder
{
public:
DataFinder ()
{
mDirectory = TEST_HARNESS_DATA_DIRECTORY;
mOutputDirectory = TEST_HARNESS_TEMP_DIRECTORY;
mRuntimeDirectory = RUNTIME_OUTPUT_PATH;
mLibraryDirectory = LIBRARY_OUTPUT_PATH;
};
void SetDirectory ( const char* dir )
{
mDirectory = dir;
};
void SetDirectory ( std::string dir )
{
mDirectory = dir;
};
void SetRuntimeDirectoryFromArgv0 ( std::string argv0 ) ;
void SetOutputDirectory ( std::string dir )
{
mOutputDirectory = dir;
};
std::string GetDirectory () const { return mDirectory; };
std::string GetOutputDirectory () const { return mOutputDirectory; };
std::string GetOutputFile ( std::string filename )
const { return mOutputDirectory + "/" + filename; };
std::string GetRuntimeDirectory () const { return mRuntimeDirectory; }
std::string GetLibraryDirectory () const { return mLibraryDirectory; }
std::string GetBuildDirectory () const { return std::string ( SIMPLEITK_BINARY_DIR ); }
std::string GetPathSeparator ()
{
#ifdef WIN32
return ";";
#else
return ":";
#endif
}
std::vector<std::string> FindExecutable ( std::string exe );
std::vector<std::string> GetLuaExecutable ();
std::vector<std::string> GetTclExecutable ();
std::vector<std::string> GetPythonExecutable ();
std::vector<std::string> GetRubyExecutable ();
std::vector<std::string> GetRExecutable ();
std::vector<std::string> GetJavaExecutable ();
std::vector<std::string> GetCSharpCompiler ();
std::vector<std::string> GetCSharpInterpreter ();
std::string GetCSharpBinaryDirectory () { return std::string ( CSHARP_BINARY_DIRECTORY ); }
std::string GetSourceDirectory () { return std::string ( SIMPLEITK_SOURCE_DIR ); }
bool FileExists ( const std::string &filename )
{
return itksys::SystemTools::FileExists ( filename.c_str() );
}
std::string GetFile ( const std::string &filename )
{
std::string name;
name = mDirectory + "/" + filename;
if (this->FileExists(name))
return name;
// fall back to source code path
name = std::string(SIMPLEITK_SOURCE_DIR) + "/Testing/Data/" + filename;
return name;
};
protected:
std::string mDirectory;
std::string mOutputDirectory;
std::string mRuntimeDirectory;
std::string mLibraryDirectory;
};
extern DataFinder dataFinder;
/// Class for running external programs
class ExternalProgramRunner : public testing::Test
{
public:
// check an image file that it matches the expected hash
void CheckImageHash ( const std::string &fileName, const std::string &hash );
// Return the separator
static std::string GetPathSeparator ()
{
#ifdef WIN32
return ";";
#else
return ":";
#endif
}
/// \brief Set an environment variable
void SetEnvironment ( std::string key, std::string value );
/** \brief Run the command line specified in the list of arguments.
* Call FAIL if the executable fails returning -1, return the value returned by the
* process otherwise.
*/
int RunExecutable ( std::vector<std::string> CommandLine, bool showOutput = false );
};
class Python : public ExternalProgramRunner { };
class Lua : public ExternalProgramRunner { };
class Java : public ExternalProgramRunner { };
class Tcl : public ExternalProgramRunner { };
class R : public ExternalProgramRunner { };
class Ruby : public ExternalProgramRunner { };
class CSharp : public ExternalProgramRunner { };
/** Base Command Class which holds onto a process object
*/
class ProcessObjectCommand
: public itk::simple::Command
{
public:
ProcessObjectCommand(itk::simple::ProcessObject &po);
protected:
itk::simple::ProcessObject &m_Process;
};
/** Print the progress to the std::cout
*/
class ProgressUpdate
: public ProcessObjectCommand
{
public:
ProgressUpdate(itk::simple::ProcessObject &po);
virtual void Execute( );
float m_Progress;
};
/** Command which will invoke ProcessObject::Abort when AbortAt
* progress is reached.
*/
class AbortAtCommand
: public ProcessObjectCommand
{
public:
AbortAtCommand(itk::simple::ProcessObject &po, float abortAt);
virtual void Execute( );
float m_AbortAt;
};
/** Command which counts the number of time the command has been
* invoked.
*/
class CountCommand
: public ProcessObjectCommand
{
public:
CountCommand(itk::simple::ProcessObject &po);
virtual void Execute( );
int m_Count;
};
namespace
{
inline std::vector<double> v2(double v1, double v2)
{
std::vector<double> temp(2);
temp[0]=v1;temp[1]=v2;
return temp;
}
inline std::vector<double> v3(double v1, double v2, double v3)
{
std::vector<double> temp(3);
temp[0]=v1;temp[1]=v2;temp[2]=v3;
return temp;
}
inline std::vector<double> v4(double v1, double v2, double v3, double v4)
{
std::vector<double> temp(4);
temp[0]=v1;temp[1]=v2;temp[2]=v3;temp[3]=v4;
return temp;
}
inline std::vector<double> v9(double v1, double v2, double v3,
double v4, double v5, double v6,
double v7, double v8, double v9)
{
std::vector<double> temp(9);
temp[0]=v1;temp[1]=v2;temp[2]=v3;
temp[3]=v4;temp[4]=v5;temp[5]=v6;
temp[6]=v7;temp[7]=v8;temp[8]=v9;
return temp;
}
}
::testing::AssertionResult VectorDoubleRMSPredFormat(const char* expr1,
const char* expr2,
const char* rms_error_expr,
const std::vector<double> &val1,
const std::vector<double> &val2,
double rms_error);
#define EXPECT_VECTOR_DOUBLE_NEAR(val1, val2, rms_error) \
EXPECT_PRED_FORMAT3(VectorDoubleRMSPredFormat, \
val1, val2, rms_error)
#include "sitkImageCompare.h"
#endif