1 #ifndef OPENCV_TS_HPP
2 #define OPENCV_TS_HPP
3
4 #ifndef __OPENCV_TESTS
5 #define __OPENCV_TESTS 1
6 #endif
7
8 #include "opencv2/opencv_modules.hpp"
9
10 #include "opencv2/core.hpp"
11 #include "opencv2/imgproc.hpp"
12 #include "opencv2/imgcodecs.hpp"
13 #include "opencv2/videoio.hpp"
14 #include "opencv2/highgui.hpp"
15
16 #include "opencv2/core/utility.hpp"
17
18 #include "opencv2/core/utils/trace.hpp"
19
20 #include "opencv2/core/hal/hal.hpp"
21
22 #include <stdarg.h> // for va_list
23
24 #include "cvconfig.h"
25
26 #include <cmath>
27 #include <vector>
28 #include <list>
29 #include <map>
30 #include <queue>
31 #include <string>
32 #include <iostream>
33 #include <fstream>
34 #include <iomanip>
35 #include <sstream>
36 #include <cstdio>
37 #include <iterator>
38 #include <limits>
39 #include <algorithm>
40
41
42 #ifndef OPENCV_32BIT_CONFIGURATION
43 # if defined(INTPTR_MAX) && defined(INT32_MAX) && INTPTR_MAX == INT32_MAX
44 # define OPENCV_32BIT_CONFIGURATION 1
45 # elif defined(_WIN32) && !defined(_WIN64)
46 # define OPENCV_32BIT_CONFIGURATION 1
47 # endif
48 #else
49 # if OPENCV_32BIT_CONFIGURATION == 0
50 # undef OPENCV_32BIT_CONFIGURATION
51 # endif
52 #endif
53
54
55
56 // most part of OpenCV tests are fit into 200Mb limit, but some tests are not:
57 // Note: due memory fragmentation real limits are usually lower on 20-25% (400Mb memory usage goes into mem_1Gb class)
58 #define CV_TEST_TAG_MEMORY_512MB "mem_512mb" // used memory: 200..512Mb - enabled by default
59 #define CV_TEST_TAG_MEMORY_1GB "mem_1gb" // used memory: 512Mb..1Gb - enabled by default
60 #define CV_TEST_TAG_MEMORY_2GB "mem_2gb" // used memory: 1..2Gb - enabled by default on 64-bit configuration (32-bit - disabled)
61 #define CV_TEST_TAG_MEMORY_6GB "mem_6gb" // used memory: 2..6Gb - disabled by default
62 #define CV_TEST_TAG_MEMORY_14GB "mem_14gb" // used memory: 6..14Gb - disabled by default
63
64 // Large / huge video streams or complex workloads
65 #define CV_TEST_TAG_LONG "long" // 5+ seconds on modern desktop machine (single thread)
66 #define CV_TEST_TAG_VERYLONG "verylong" // 20+ seconds on modern desktop machine (single thread)
67
68 // Large / huge video streams or complex workloads for debug builds
69 #define CV_TEST_TAG_DEBUG_LONG "debug_long" // 10+ seconds on modern desktop machine (single thread)
70 #define CV_TEST_TAG_DEBUG_VERYLONG "debug_verylong" // 40+ seconds on modern desktop machine (single thread)
71
72 // Lets skip processing of high resolution images via instrumentation tools (valgrind/coverage/sanitizers).
73 // It is enough to run lower resolution (VGA: 640x480) tests.
74 #define CV_TEST_TAG_SIZE_HD "size_hd" // 720p+, enabled
75 #define CV_TEST_TAG_SIZE_FULLHD "size_fullhd" // 1080p+, enabled (disable these tests for valgrind/coverage run)
76 #define CV_TEST_TAG_SIZE_4K "size_4k" // 2160p+, enabled (disable these tests for valgrind/coverage run)
77
78 // Other misc test tags
79 #define CV_TEST_TAG_TYPE_64F "type_64f" // CV_64F, enabled (disable these tests on low power embedded devices)
80
81 // Kernel-based image processing
82 #define CV_TEST_TAG_FILTER_SMALL "filter_small" // Filtering with kernels <= 3x3
83 #define CV_TEST_TAG_FILTER_MEDIUM "filter_medium" // Filtering with kernels: 3x3 < kernel <= 5x5
84 #define CV_TEST_TAG_FILTER_LARGE "filter_large" // Filtering with kernels: 5x5 < kernel <= 9x9
85 #define CV_TEST_TAG_FILTER_HUGE "filter_huge" // Filtering with kernels: > 9x9
86
87 // Other tests categories
88 #define CV_TEST_TAG_OPENCL "opencl" // Tests with OpenCL
89
90
91
92 #ifdef WINRT
93 #pragma warning(disable:4447) // Disable warning 'main' signature found without threading model
94 #endif
95
96 #ifdef _MSC_VER
97 #pragma warning( disable: 4503 ) // decorated name length exceeded, name was truncated
98 #endif
99
100 #define GTEST_DONT_DEFINE_FAIL 0
101 #define GTEST_DONT_DEFINE_SUCCEED 0
102 #define GTEST_DONT_DEFINE_ASSERT_EQ 0
103 #define GTEST_DONT_DEFINE_ASSERT_NE 0
104 #define GTEST_DONT_DEFINE_ASSERT_LE 0
105 #define GTEST_DONT_DEFINE_ASSERT_LT 0
106 #define GTEST_DONT_DEFINE_ASSERT_GE 0
107 #define GTEST_DONT_DEFINE_ASSERT_GT 0
108 #define GTEST_DONT_DEFINE_TEST 0
109
110 #ifndef GTEST_LANG_CXX11
111 #if __cplusplus >= 201103L || (defined(_MSVC_LANG) && !(_MSVC_LANG < 201103))
112 # define GTEST_LANG_CXX11 1
113 # define GTEST_HAS_TR1_TUPLE 0
114 # define GTEST_HAS_COMBINE 1
115 # endif
116 #endif
117
118 #if defined(__OPENCV_BUILD) && defined(__clang__)
119 #pragma clang diagnostic ignored "-Winconsistent-missing-override"
120 #endif
121 #if defined(__OPENCV_BUILD) && defined(__GNUC__) && __GNUC__ >= 5
122 //#pragma GCC diagnostic push
123 #pragma GCC diagnostic ignored "-Wsuggest-override"
124 #endif
125 #include "opencv2/ts/ts_gtest.h"
126 #if defined(__OPENCV_BUILD) && defined(__GNUC__) && __GNUC__ >= 5
127 //#pragma GCC diagnostic pop
128 #endif
129 #include "opencv2/ts/ts_ext.hpp"
130
131 #ifndef GTEST_USES_SIMPLE_RE
132 # define GTEST_USES_SIMPLE_RE 0
133 #endif
134 #ifndef GTEST_USES_POSIX_RE
135 # define GTEST_USES_POSIX_RE 0
136 #endif
137
138 #define PARAM_TEST_CASE(name, ...) struct name : testing::TestWithParam< testing::tuple< __VA_ARGS__ > >
139 #define GET_PARAM(k) testing::get< k >(GetParam())
140
141 namespace cvtest
142 {
143
144 using std::vector;
145 using std::map;
146 using std::string;
147 using std::stringstream;
148 using std::cout;
149 using std::cerr;
150 using std::endl;
151 using std::min;
152 using std::max;
153 using std::numeric_limits;
154 using std::pair;
155 using std::make_pair;
156 using testing::TestWithParam;
157 using testing::Values;
158 using testing::ValuesIn;
159 using testing::Combine;
160
161 using cv::Mat;
162 using cv::Mat_;
163 using cv::UMat;
164 using cv::InputArray;
165 using cv::OutputArray;
166 using cv::noArray;
167
168 using cv::Range;
169 using cv::Point;
170 using cv::Rect;
171 using cv::Size;
172 using cv::Scalar;
173 using cv::RNG;
174
175 // Tuple stuff from Google Tests
176 using testing::get;
177 using testing::make_tuple;
178 using testing::tuple;
179 using testing::tuple_size;
180 using testing::tuple_element;
181
182
183 namespace details {
184 class SkipTestExceptionBase: public cv::Exception
185 {
186 public:
187 SkipTestExceptionBase(bool handlingTags);
188 SkipTestExceptionBase(const cv::String& message, bool handlingTags);
189 };
190 }
191
192 class SkipTestException: public details::SkipTestExceptionBase
193 {
194 public:
195 int dummy; // workaround for MacOSX Xcode 7.3 bug (don't make class "empty")
SkipTestException()196 SkipTestException() : details::SkipTestExceptionBase(false), dummy(0) {}
SkipTestException(const cv::String & message)197 SkipTestException(const cv::String& message) : details::SkipTestExceptionBase(message, false), dummy(0) { }
198 };
199
200 /** Apply tag to the current test
201
202 Automatically apply corresponding additional tags (for example, 4K => FHD => HD => VGA).
203
204 If tag is in skip list, then SkipTestException is thrown
205 */
206 void applyTestTag(const std::string& tag);
207
208 /** Run postponed checks of applied test tags
209
210 If tag is in skip list, then SkipTestException is thrown
211 */
212 void checkTestTags();
213
214 void applyTestTag_(const std::string& tag);
215
applyTestTag(const std::string & tag1,const std::string & tag2)216 static inline void applyTestTag(const std::string& tag1, const std::string& tag2)
217 { applyTestTag_(tag1); applyTestTag_(tag2); checkTestTags(); }
applyTestTag(const std::string & tag1,const std::string & tag2,const std::string & tag3)218 static inline void applyTestTag(const std::string& tag1, const std::string& tag2, const std::string& tag3)
219 { applyTestTag_(tag1); applyTestTag_(tag2); applyTestTag_(tag3); checkTestTags(); }
applyTestTag(const std::string & tag1,const std::string & tag2,const std::string & tag3,const std::string & tag4)220 static inline void applyTestTag(const std::string& tag1, const std::string& tag2, const std::string& tag3, const std::string& tag4)
221 { applyTestTag_(tag1); applyTestTag_(tag2); applyTestTag_(tag3); applyTestTag_(tag4); checkTestTags(); }
applyTestTag(const std::string & tag1,const std::string & tag2,const std::string & tag3,const std::string & tag4,const std::string & tag5)222 static inline void applyTestTag(const std::string& tag1, const std::string& tag2, const std::string& tag3, const std::string& tag4, const std::string& tag5)
223 { applyTestTag_(tag1); applyTestTag_(tag2); applyTestTag_(tag3); applyTestTag_(tag4); applyTestTag_(tag5); checkTestTags(); }
224
225
226 /** Append global skip test tags
227 */
228 void registerGlobalSkipTag(const std::string& skipTag);
registerGlobalSkipTag(const std::string & tag1,const std::string & tag2)229 static inline void registerGlobalSkipTag(const std::string& tag1, const std::string& tag2)
230 { registerGlobalSkipTag(tag1); registerGlobalSkipTag(tag2); }
registerGlobalSkipTag(const std::string & tag1,const std::string & tag2,const std::string & tag3)231 static inline void registerGlobalSkipTag(const std::string& tag1, const std::string& tag2, const std::string& tag3)
232 { registerGlobalSkipTag(tag1); registerGlobalSkipTag(tag2); registerGlobalSkipTag(tag3); }
registerGlobalSkipTag(const std::string & tag1,const std::string & tag2,const std::string & tag3,const std::string & tag4)233 static inline void registerGlobalSkipTag(const std::string& tag1, const std::string& tag2, const std::string& tag3, const std::string& tag4)
234 { registerGlobalSkipTag(tag1); registerGlobalSkipTag(tag2); registerGlobalSkipTag(tag3); registerGlobalSkipTag(tag4); }
registerGlobalSkipTag(const std::string & tag1,const std::string & tag2,const std::string & tag3,const std::string & tag4,const std::string & tag5)235 static inline void registerGlobalSkipTag(const std::string& tag1, const std::string& tag2, const std::string& tag3, const std::string& tag4,
236 const std::string& tag5)
237 {
238 registerGlobalSkipTag(tag1); registerGlobalSkipTag(tag2); registerGlobalSkipTag(tag3); registerGlobalSkipTag(tag4);
239 registerGlobalSkipTag(tag5);
240 }
registerGlobalSkipTag(const std::string & tag1,const std::string & tag2,const std::string & tag3,const std::string & tag4,const std::string & tag5,const std::string & tag6)241 static inline void registerGlobalSkipTag(const std::string& tag1, const std::string& tag2, const std::string& tag3, const std::string& tag4,
242 const std::string& tag5, const std::string& tag6)
243 {
244 registerGlobalSkipTag(tag1); registerGlobalSkipTag(tag2); registerGlobalSkipTag(tag3); registerGlobalSkipTag(tag4);
245 registerGlobalSkipTag(tag5); registerGlobalSkipTag(tag6);
246 }
registerGlobalSkipTag(const std::string & tag1,const std::string & tag2,const std::string & tag3,const std::string & tag4,const std::string & tag5,const std::string & tag6,const std::string & tag7)247 static inline void registerGlobalSkipTag(const std::string& tag1, const std::string& tag2, const std::string& tag3, const std::string& tag4,
248 const std::string& tag5, const std::string& tag6, const std::string& tag7)
249 {
250 registerGlobalSkipTag(tag1); registerGlobalSkipTag(tag2); registerGlobalSkipTag(tag3); registerGlobalSkipTag(tag4);
251 registerGlobalSkipTag(tag5); registerGlobalSkipTag(tag6); registerGlobalSkipTag(tag7);
252 }
253
254
255
256 class TS;
257
258 int64 readSeed(const char* str);
259
260 void randUni( RNG& rng, Mat& a, const Scalar& param1, const Scalar& param2 );
261
randInt(RNG & rng)262 inline unsigned randInt( RNG& rng )
263 {
264 return (unsigned)rng;
265 }
266
randReal(RNG & rng)267 inline double randReal( RNG& rng )
268 {
269 return (double)rng;
270 }
271
272
273 const char* getTypeName( int type );
274 int typeByName( const char* type_name );
275
276 string vec2str(const string& sep, const int* v, size_t nelems);
277
clipInt(int val,int min_val,int max_val)278 inline int clipInt( int val, int min_val, int max_val )
279 {
280 if( val < min_val )
281 val = min_val;
282 if( val > max_val )
283 val = max_val;
284 return val;
285 }
286
287 double getMinVal(int depth);
288 double getMaxVal(int depth);
289
290 Size randomSize(RNG& rng, double maxSizeLog);
291 void randomSize(RNG& rng, int minDims, int maxDims, double maxSizeLog, vector<int>& sz);
292 int randomType(RNG& rng, cv::_OutputArray::DepthMask typeMask, int minChannels, int maxChannels);
293 Mat randomMat(RNG& rng, Size size, int type, double minVal, double maxVal, bool useRoi);
294 Mat randomMat(RNG& rng, const vector<int>& size, int type, double minVal, double maxVal, bool useRoi);
295 void add(const Mat& a, double alpha, const Mat& b, double beta,
296 Scalar gamma, Mat& c, int ctype, bool calcAbs=false);
297 void multiply(const Mat& a, const Mat& b, Mat& c, double alpha=1);
298 void divide(const Mat& a, const Mat& b, Mat& c, double alpha=1);
299
300 void convert(const Mat& src, cv::OutputArray dst, int dtype, double alpha=1, double beta=0);
301 void copy(const Mat& src, Mat& dst, const Mat& mask=Mat(), bool invertMask=false);
302 void set(Mat& dst, const Scalar& gamma, const Mat& mask=Mat());
303
304 // working with multi-channel arrays
305 void extract( const Mat& a, Mat& plane, int coi );
306 void insert( const Mat& plane, Mat& a, int coi );
307
308 // checks that the array does not have NaNs and/or Infs and all the elements are
309 // within [min_val,max_val). idx is the index of the first "bad" element.
310 int check( const Mat& data, double min_val, double max_val, vector<int>* idx );
311
312 // modifies values that are close to zero
313 void patchZeros( Mat& mat, double level );
314
315 void transpose(const Mat& src, Mat& dst);
316 void erode(const Mat& src, Mat& dst, const Mat& _kernel, Point anchor=Point(-1,-1),
317 int borderType=0, const Scalar& borderValue=Scalar());
318 void dilate(const Mat& src, Mat& dst, const Mat& _kernel, Point anchor=Point(-1,-1),
319 int borderType=0, const Scalar& borderValue=Scalar());
320 void filter2D(const Mat& src, Mat& dst, int ddepth, const Mat& kernel,
321 Point anchor, double delta, int borderType,
322 const Scalar& borderValue=Scalar());
323 void copyMakeBorder(const Mat& src, Mat& dst, int top, int bottom, int left, int right,
324 int borderType, const Scalar& borderValue=Scalar());
325 Mat calcSobelKernel2D( int dx, int dy, int apertureSize, int origin=0 );
326 Mat calcLaplaceKernel2D( int aperture_size );
327
328 void initUndistortMap( const Mat& a, const Mat& k, const Mat& R, const Mat& new_a, Size sz, Mat& mapx, Mat& mapy, int map_type );
329 void initInverseRectificationMap( const Mat& a, const Mat& k, const Mat& R, const Mat& new_a, Size sz, Mat& mapx, Mat& mapy, int map_type );
330
331 void minMaxLoc(const Mat& src, double* minval, double* maxval,
332 vector<int>* minloc, vector<int>* maxloc, const Mat& mask=Mat());
333 double norm(InputArray src, int normType, InputArray mask=noArray());
334 double norm(InputArray src1, InputArray src2, int normType, InputArray mask=noArray());
335 Scalar mean(const Mat& src, const Mat& mask=Mat());
336 double PSNR(InputArray src1, InputArray src2);
337
338 bool cmpUlps(const Mat& data, const Mat& refdata, int expMaxDiff, double* realMaxDiff, vector<int>* idx);
339
340 // compares two arrays. max_diff is the maximum actual difference,
341 // success_err_level is maximum allowed difference, idx is the index of the first
342 // element for which difference is >success_err_level
343 // (or index of element with the maximum difference)
344 int cmpEps( const Mat& data, const Mat& refdata, double* max_diff,
345 double success_err_level, vector<int>* idx,
346 bool element_wise_relative_error );
347
348 // a wrapper for the previous function. in case of error prints the message to log file.
349 int cmpEps2( TS* ts, const Mat& data, const Mat& refdata, double success_err_level,
350 bool element_wise_relative_error, const char* desc );
351
352 int cmpEps2_64f( TS* ts, const double* val, const double* refval, int len,
353 double eps, const char* param_name );
354
355 void logicOp(const Mat& src1, const Mat& src2, Mat& dst, char c);
356 void logicOp(const Mat& src, const Scalar& s, Mat& dst, char c);
357 void min(const Mat& src1, const Mat& src2, Mat& dst);
358 void min(const Mat& src, double s, Mat& dst);
359 void max(const Mat& src1, const Mat& src2, Mat& dst);
360 void max(const Mat& src, double s, Mat& dst);
361
362 void compare(const Mat& src1, const Mat& src2, Mat& dst, int cmpop);
363 void compare(const Mat& src, double s, Mat& dst, int cmpop);
364 void gemm(const Mat& src1, const Mat& src2, double alpha,
365 const Mat& src3, double beta, Mat& dst, int flags);
366 void transform( const Mat& src, Mat& dst, const Mat& transmat, const Mat& shift );
367 double crossCorr(const Mat& src1, const Mat& src2);
368 void threshold( const Mat& src, Mat& dst, double thresh, double maxval, int thresh_type );
369 void minMaxIdx( InputArray _img, double* minVal, double* maxVal,
370 Point* minLoc, Point* maxLoc, InputArray _mask );
371
372 struct MatInfo
373 {
MatInfocvtest::MatInfo374 MatInfo(const Mat& _m) : m(&_m) {}
375 const Mat* m;
376 };
377
378 std::ostream& operator << (std::ostream& out, const MatInfo& m);
379
380 struct MatComparator
381 {
382 public:
383 MatComparator(double maxdiff, int context);
384
385 ::testing::AssertionResult operator()(const char* expr1, const char* expr2,
386 const Mat& m1, const Mat& m2);
387
388 double maxdiff;
389 double realmaxdiff;
390 vector<int> loc0;
391 int context;
392 };
393
394
395
396 class BaseTest;
397 class TS;
398
399 class BaseTest
400 {
401 public:
402 // constructor(s) and destructor
403 BaseTest();
404 virtual ~BaseTest();
405
406 // the main procedure of the test
407 virtual void run( int start_from );
408
409 // the wrapper for run that cares of exceptions
410 virtual void safe_run( int start_from=0 );
411
get_name() const412 const string& get_name() const { return name; }
413
414 // returns true if and only if the different test cases do not depend on each other
415 // (so that test system could get right to a problematic test case)
416 virtual bool can_do_fast_forward();
417
418 // deallocates all the memory.
419 // called by init() (before initialization) and by the destructor
420 virtual void clear();
421
422 protected:
423 int test_case_count; // the total number of test cases
424
425 // read test params
426 virtual int read_params( const cv::FileStorage& fs );
427
428 // returns the number of tests or -1 if it is unknown a-priori
429 virtual int get_test_case_count();
430
431 // prepares data for the next test case. rng seed is updated by the function
432 virtual int prepare_test_case( int test_case_idx );
433
434 // checks if the test output is valid and accurate
435 virtual int validate_test_results( int test_case_idx );
436
437 // calls the tested function. the method is called from run_test_case()
438 virtual void run_func(); // runs tested func(s)
439
440 // updates progress bar
441 virtual int update_progress( int progress, int test_case_idx, int count, double dt );
442
443 // dump test case input parameters
444 virtual void dump_test_case(int test_case_idx, std::ostream* out);
445
446 // finds test parameter
447 cv::FileNode find_param( const cv::FileStorage& fs, const char* param_name );
448
449 // name of the test (it is possible to locate a test by its name)
450 string name;
451
452 // pointer to the system that includes the test
453 TS* ts;
454 };
455
456
457 /*****************************************************************************************\
458 * Information about a failed test *
459 \*****************************************************************************************/
460
461 struct TestInfo
462 {
463 TestInfo();
464
465 // pointer to the test
466 BaseTest* test;
467
468 // failure code (TS::FAIL_*)
469 int code;
470
471 // seed value right before the data for the failed test case is prepared.
472 uint64 rng_seed;
473
474 // seed value right before running the test
475 uint64 rng_seed0;
476
477 // index of test case, can be then passed to BaseTest::proceed_to_test_case()
478 int test_case_idx;
479 };
480
481 /*****************************************************************************************\
482 * Base Class for test system *
483 \*****************************************************************************************/
484
485 // common parameters:
486 struct TSParams
487 {
488 TSParams();
489
490 // RNG seed, passed to and updated by every test executed.
491 uint64 rng_seed;
492
493 // whether to use IPP, MKL etc. or not
494 bool use_optimized;
495
496 // extensivity of the tests, scale factor for test_case_count
497 double test_case_count_scale;
498 };
499
500
501 class TS
502 {
503 TS();
504 virtual ~TS();
505 public:
506
507 enum
508 {
509 NUL=0,
510 SUMMARY_IDX=0,
511 SUMMARY=1 << SUMMARY_IDX,
512 LOG_IDX=1,
513 LOG=1 << LOG_IDX,
514 CSV_IDX=2,
515 CSV=1 << CSV_IDX,
516 CONSOLE_IDX=3,
517 CONSOLE=1 << CONSOLE_IDX,
518 MAX_IDX=4
519 };
520
521 static TS* ptr();
522
523 // initialize test system before running the first test
524 virtual void init( const string& modulename );
525
526 // low-level printing functions that are used by individual tests and by the system itself
527 virtual void printf( int streams, const char* fmt, ... );
528 virtual void vprintf( int streams, const char* fmt, va_list arglist );
529
530 // updates the context: current test, test case, rng state
531 virtual void update_context( BaseTest* test, int test_case_idx, bool update_ts_context );
532
get_current_test_info()533 const TestInfo* get_current_test_info() { return ¤t_test_info; }
534
535 // sets information about a failed test
536 virtual void set_failed_test_info( int fail_code );
537
538 virtual void set_gtest_status();
539
540 // test error codes
541 enum FailureCode
542 {
543 // everything is Ok
544 OK=0,
545
546 // generic error: stub value to be used
547 // temporarily if the error's cause is unknown
548 FAIL_GENERIC=-1,
549
550 // the test is missing some essential data to proceed further
551 FAIL_MISSING_TEST_DATA=-2,
552
553 // the tested function raised an error via cxcore error handler
554 FAIL_ERROR_IN_CALLED_FUNC=-3,
555
556 // an exception has been raised;
557 // for memory and arithmetic exception
558 // there are two specialized codes (see below...)
559 FAIL_EXCEPTION=-4,
560
561 // a memory exception
562 // (access violation, access to missed page, stack overflow etc.)
563 FAIL_MEMORY_EXCEPTION=-5,
564
565 // arithmetic exception (overflow, division by zero etc.)
566 FAIL_ARITHM_EXCEPTION=-6,
567
568 // the tested function corrupted memory (no exception have been raised)
569 FAIL_MEMORY_CORRUPTION_BEGIN=-7,
570 FAIL_MEMORY_CORRUPTION_END=-8,
571
572 // the tested function (or test itself) do not deallocate some memory
573 FAIL_MEMORY_LEAK=-9,
574
575 // the tested function returned invalid object, e.g. matrix, containing NaNs,
576 // structure with NULL or out-of-range fields (while it should not)
577 FAIL_INVALID_OUTPUT=-10,
578
579 // the tested function returned valid object, but it does not match
580 // the original (or produced by the test) object
581 FAIL_MISMATCH=-11,
582
583 // the tested function returned valid object (a single number or numerical array),
584 // but it differs too much from the original (or produced by the test) object
585 FAIL_BAD_ACCURACY=-12,
586
587 // the tested function hung. Sometimes, it can be determined by unexpectedly long
588 // processing time (in this case there should be possibility to interrupt such a function
589 FAIL_HANG=-13,
590
591 // unexpected response on passing bad arguments to the tested function
592 // (the function crashed, proceed successfully (while it should not), or returned
593 // error code that is different from what is expected)
594 FAIL_BAD_ARG_CHECK=-14,
595
596 // the test data (in whole or for the particular test case) is invalid
597 FAIL_INVALID_TEST_DATA=-15,
598
599 // the test has been skipped because it is not in the selected subset of the tests to run,
600 // because it has been run already within the same run with the same parameters, or because
601 // of some other reason and this is not considered as an error.
602 // Normally TS::run() (or overridden method in the derived class) takes care of what
603 // needs to be run, so this code should not occur.
604 SKIPPED=1
605 };
606
607 // get RNG to generate random input data for a test
get_rng()608 RNG& get_rng() { return rng; }
609
610 // returns the current error code
get_err_code()611 TS::FailureCode get_err_code() { return TS::FailureCode(current_test_info.code); }
612
613 // returns the test extensivity scale
get_test_case_count_scale()614 double get_test_case_count_scale() { return params.test_case_count_scale; }
615
get_data_path() const616 const string& get_data_path() const { return data_path; }
617
618 // returns textual description of failure code
619 static string str_from_code( const TS::FailureCode code );
620
621 std::vector<std::string> data_search_path;
622 std::vector<std::string> data_search_subdir;
623 protected:
624
625 // these are allocated within a test to try to keep them valid in case of stack corruption
626 RNG rng;
627
628 // information about the current test
629 TestInfo current_test_info;
630
631 // the path to data files used by tests
632 string data_path;
633
634 TSParams params;
635 std::string output_buf[MAX_IDX];
636 };
637
638
639 /*****************************************************************************************\
640 * Subclass of BaseTest for testing functions that process dense arrays *
641 \*****************************************************************************************/
642
643 class ArrayTest : public BaseTest
644 {
645 public:
646 // constructor(s) and destructor
647 ArrayTest();
648 virtual ~ArrayTest();
649
650 virtual void clear() CV_OVERRIDE;
651
652 protected:
653
654 virtual int read_params( const cv::FileStorage& fs ) CV_OVERRIDE;
655 virtual int prepare_test_case( int test_case_idx ) CV_OVERRIDE;
656 virtual int validate_test_results( int test_case_idx ) CV_OVERRIDE;
657
658 virtual void prepare_to_validation( int test_case_idx );
659 virtual void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
660 virtual void fill_array( int test_case_idx, int i, int j, Mat& arr );
661 virtual void get_minmax_bounds( int i, int j, int type, Scalar& low, Scalar& high );
662 virtual double get_success_error_level( int test_case_idx, int i, int j );
663
664 bool cvmat_allowed;
665 bool iplimage_allowed;
666 bool optional_mask;
667 bool element_wise_relative_error;
668
669 int min_log_array_size;
670 int max_log_array_size;
671
672 enum { INPUT, INPUT_OUTPUT, OUTPUT, REF_INPUT_OUTPUT, REF_OUTPUT, TEMP, MASK, MAX_ARR };
673
674 vector<vector<void*> > test_array;
675 vector<vector<Mat> > test_mat;
676 float buf[4];
677 };
678
679
680 class BadArgTest : public BaseTest
681 {
682 public:
683 // constructor(s) and destructor
684 BadArgTest();
685 virtual ~BadArgTest();
686
687 protected:
688 virtual int run_test_case( int expected_code, const string& descr );
689 virtual void run_func(void) CV_OVERRIDE = 0;
690 int test_case_idx;
691
692 template<class F>
run_test_case(int expected_code,const string & _descr,F f)693 int run_test_case( int expected_code, const string& _descr, F f)
694 {
695 int errcount = 0;
696 bool thrown = false;
697 const char* descr = _descr.c_str() ? _descr.c_str() : "";
698
699 try
700 {
701 f();
702 }
703 catch(const cv::Exception& e)
704 {
705 thrown = true;
706 if( e.code != expected_code && e.code != cv::Error::StsAssert && e.code != cv::Error::StsError )
707 {
708 ts->printf(TS::LOG, "%s (test case #%d): the error code %d is different from the expected %d\n",
709 descr, test_case_idx, e.code, expected_code);
710 errcount = 1;
711 }
712 }
713 catch(...)
714 {
715 thrown = true;
716 ts->printf(TS::LOG, "%s (test case #%d): unknown exception was thrown (the function has likely crashed)\n",
717 descr, test_case_idx);
718 errcount = 1;
719 }
720 if(!thrown)
721 {
722 ts->printf(TS::LOG, "%s (test case #%d): no expected exception was thrown\n",
723 descr, test_case_idx);
724 errcount = 1;
725 }
726 test_case_idx++;
727
728 return errcount;
729 }
730 };
731
732 extern uint64 param_seed;
733
734 struct DefaultRngAuto
735 {
736 const uint64 old_state;
737
DefaultRngAutocvtest::DefaultRngAuto738 DefaultRngAuto() : old_state(cv::theRNG().state) { cv::theRNG().state = cvtest::param_seed; }
~DefaultRngAutocvtest::DefaultRngAuto739 ~DefaultRngAuto() { cv::theRNG().state = old_state; }
740
741 DefaultRngAuto& operator=(const DefaultRngAuto&);
742 };
743
744
745 // test images generation functions
746 void fillGradient(Mat& img, int delta = 5);
747 void smoothBorder(Mat& img, const Scalar& color, int delta = 3);
748
749 // Utility functions
750
751 void addDataSearchPath(const std::string& path);
752 void addDataSearchSubDirectory(const std::string& subdir);
753
754 /*! @brief Try to find requested data file
755
756 Search directories:
757
758 0. TS::data_search_path (search sub-directories are not used)
759 1. OPENCV_TEST_DATA_PATH environment variable
760 2. One of these:
761 a. OpenCV testdata based on build location: "./" + "share/OpenCV/testdata"
762 b. OpenCV testdata at install location: CMAKE_INSTALL_PREFIX + "share/OpenCV/testdata"
763
764 Search sub-directories:
765
766 - addDataSearchSubDirectory()
767 - modulename from TS::init()
768
769 */
770 std::string findDataFile(const std::string& relative_path, bool required = true);
771
772 /*! @brief Try to find requested data directory
773 @sa findDataFile
774 */
775 std::string findDataDirectory(const std::string& relative_path, bool required = true);
776
777 // Test definitions
778
779 class SystemInfoCollector : public testing::EmptyTestEventListener
780 {
781 private:
782 virtual void OnTestProgramStart(const testing::UnitTest&);
783 };
784
785 #ifndef __CV_TEST_EXEC_ARGS
786 #if defined(_MSC_VER) && (_MSC_VER <= 1400)
787 #define __CV_TEST_EXEC_ARGS(...) \
788 while (++argc >= (--argc,-1)) {__VA_ARGS__; break;} /*this ugly construction is needed for VS 2005*/
789 #else
790 #define __CV_TEST_EXEC_ARGS(...) \
791 __VA_ARGS__;
792 #endif
793 #endif
794
795 void parseCustomOptions(int argc, char **argv);
796
797 #define CV_TEST_INIT0_NOOP (void)0
798
799 #define CV_TEST_MAIN(resourcesubdir, ...) CV_TEST_MAIN_EX(resourcesubdir, NOOP, __VA_ARGS__)
800
801 #define CV_TEST_MAIN_EX(resourcesubdir, INIT0, ...) \
802 int main(int argc, char **argv) \
803 { \
804 CV_TRACE_FUNCTION(); \
805 { CV_TRACE_REGION("INIT"); \
806 using namespace cvtest; using namespace opencv_test; \
807 TS* ts = TS::ptr(); \
808 ts->init(resourcesubdir); \
809 __CV_TEST_EXEC_ARGS(CV_TEST_INIT0_ ## INIT0) \
810 ::testing::InitGoogleTest(&argc, argv); \
811 ::testing::UnitTest::GetInstance()->listeners().Append(new SystemInfoCollector); \
812 __CV_TEST_EXEC_ARGS(__VA_ARGS__) \
813 parseCustomOptions(argc, argv); \
814 } \
815 return RUN_ALL_TESTS(); \
816 }
817
818 // This usually only makes sense in perf tests with several implementations,
819 // some of which are not available.
820 #define CV_TEST_FAIL_NO_IMPL() do { \
821 ::testing::Test::RecordProperty("custom_status", "noimpl"); \
822 FAIL() << "No equivalent implementation."; \
823 } while (0)
824
825 } //namespace cvtest
826
827 #include "opencv2/ts/ts_perf.hpp"
828
829 namespace cvtest {
830 using perf::MatDepth;
831 using perf::MatType;
832 }
833
834 #ifdef WINRT
835 #ifndef __FSTREAM_EMULATED__
836 #define __FSTREAM_EMULATED__
837 #include <stdlib.h>
838 #include <fstream>
839 #include <sstream>
840
841 #undef ifstream
842 #undef ofstream
843 #define ifstream ifstream_emulated
844 #define ofstream ofstream_emulated
845
846 namespace std {
847
848 class ifstream : public stringstream
849 {
850 FILE* f;
851 public:
ifstream(const char * filename,ios_base::openmode mode=ios_base::in)852 ifstream(const char* filename, ios_base::openmode mode = ios_base::in)
853 : f(NULL)
854 {
855 string modeStr("r");
856 printf("Open file (read): %s\n", filename);
857 if (mode & ios_base::binary)
858 modeStr += "b";
859 f = fopen(filename, modeStr.c_str());
860
861 if (f == NULL)
862 {
863 printf("Can't open file: %s\n", filename);
864 return;
865 }
866 fseek(f, 0, SEEK_END);
867 size_t sz = ftell(f);
868 if (sz > 0)
869 {
870 char* buf = (char*) malloc(sz);
871 fseek(f, 0, SEEK_SET);
872 if (fread(buf, 1, sz, f) == sz)
873 {
874 this->str(std::string(buf, sz));
875 }
876 free(buf);
877 }
878 }
879
~ifstream()880 ~ifstream() { close(); }
is_open() const881 bool is_open() const { return f != NULL; }
close()882 void close()
883 {
884 if (f)
885 fclose(f);
886 f = NULL;
887 this->str("");
888 }
889 };
890
891 class ofstream : public stringstream
892 {
893 FILE* f;
894 public:
ofstream(const char * filename,ios_base::openmode mode=ios_base::out)895 ofstream(const char* filename, ios_base::openmode mode = ios_base::out)
896 : f(NULL)
897 {
898 open(filename, mode);
899 }
~ofstream()900 ~ofstream() { close(); }
open(const char * filename,ios_base::openmode mode=ios_base::out)901 void open(const char* filename, ios_base::openmode mode = ios_base::out)
902 {
903 string modeStr("w+");
904 if (mode & ios_base::trunc)
905 modeStr = "w";
906 if (mode & ios_base::binary)
907 modeStr += "b";
908 f = fopen(filename, modeStr.c_str());
909 printf("Open file (write): %s\n", filename);
910 if (f == NULL)
911 {
912 printf("Can't open file (write): %s\n", filename);
913 return;
914 }
915 }
is_open() const916 bool is_open() const { return f != NULL; }
close()917 void close()
918 {
919 if (f)
920 {
921 fwrite(reinterpret_cast<const char *>(this->str().c_str()), this->str().size(), 1, f);
922 fclose(f);
923 }
924 f = NULL;
925 this->str("");
926 }
927 };
928
929 } // namespace std
930 #endif // __FSTREAM_EMULATED__
931 #endif // WINRT
932
933
934 namespace opencv_test {
935 using namespace cvtest;
936 using namespace cv;
937
938 #ifdef CV_CXX11
939 #define CVTEST_GUARD_SYMBOL(name) \
940 class required_namespace_specificatin_here_for_symbol_ ## name {}; \
941 using name = required_namespace_specificatin_here_for_symbol_ ## name;
942 #else
943 #define CVTEST_GUARD_SYMBOL(name) /* nothing */
944 #endif
945
946 CVTEST_GUARD_SYMBOL(norm)
947 CVTEST_GUARD_SYMBOL(add)
948 CVTEST_GUARD_SYMBOL(multiply)
949 CVTEST_GUARD_SYMBOL(divide)
950 CVTEST_GUARD_SYMBOL(transpose)
951 CVTEST_GUARD_SYMBOL(copyMakeBorder)
952 CVTEST_GUARD_SYMBOL(filter2D)
953 CVTEST_GUARD_SYMBOL(compare)
954 CVTEST_GUARD_SYMBOL(minMaxIdx)
955 CVTEST_GUARD_SYMBOL(threshold)
956
957 extern bool required_opencv_test_namespace; // compilation check for non-refactored tests
958 }
959
960 #endif // OPENCV_TS_HPP
961