f08142899a49d42f11f0c1e2263eee1cd552ee02
[platform/upstream/opencv.git] / modules / ts / include / opencv2 / ts.hpp
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 class SkipTestException: public cv::Exception
184 {
185 public:
186     int dummy; // workaround for MacOSX Xcode 7.3 bug (don't make class "empty")
187     SkipTestException() : dummy(0) {}
188     SkipTestException(const cv::String& message) : dummy(0) { this->msg = message; }
189 };
190
191 /** Apply tag to the current test
192
193 Automatically apply corresponding additional tags (for example, 4K => FHD => HD => VGA).
194
195 If tag is in skip list, then SkipTestException is thrown
196 */
197 void applyTestTag(const std::string& tag);
198
199 /** Run postponed checks of applied test tags
200
201 If tag is in skip list, then SkipTestException is thrown
202 */
203 void checkTestTags();
204
205 void applyTestTag_(const std::string& tag);
206
207 static inline void applyTestTag(const std::string& tag1, const std::string& tag2)
208 { applyTestTag_(tag1); applyTestTag_(tag2); checkTestTags(); }
209 static inline void applyTestTag(const std::string& tag1, const std::string& tag2, const std::string& tag3)
210 { applyTestTag_(tag1); applyTestTag_(tag2); applyTestTag_(tag3); checkTestTags(); }
211 static inline void applyTestTag(const std::string& tag1, const std::string& tag2, const std::string& tag3, const std::string& tag4)
212 { applyTestTag_(tag1); applyTestTag_(tag2); applyTestTag_(tag3); applyTestTag_(tag4); checkTestTags(); }
213
214
215 class TS;
216
217 int64 readSeed(const char* str);
218
219 void randUni( RNG& rng, Mat& a, const Scalar& param1, const Scalar& param2 );
220
221 inline unsigned randInt( RNG& rng )
222 {
223     return (unsigned)rng;
224 }
225
226 inline  double randReal( RNG& rng )
227 {
228     return (double)rng;
229 }
230
231
232 const char* getTypeName( int type );
233 int typeByName( const char* type_name );
234
235 string vec2str(const string& sep, const int* v, size_t nelems);
236
237 inline int clipInt( int val, int min_val, int max_val )
238 {
239     if( val < min_val )
240         val = min_val;
241     if( val > max_val )
242         val = max_val;
243     return val;
244 }
245
246 double getMinVal(int depth);
247 double getMaxVal(int depth);
248
249 Size randomSize(RNG& rng, double maxSizeLog);
250 void randomSize(RNG& rng, int minDims, int maxDims, double maxSizeLog, vector<int>& sz);
251 int randomType(RNG& rng, int typeMask, int minChannels, int maxChannels);
252 Mat randomMat(RNG& rng, Size size, int type, double minVal, double maxVal, bool useRoi);
253 Mat randomMat(RNG& rng, const vector<int>& size, int type, double minVal, double maxVal, bool useRoi);
254 void add(const Mat& a, double alpha, const Mat& b, double beta,
255                       Scalar gamma, Mat& c, int ctype, bool calcAbs=false);
256 void multiply(const Mat& a, const Mat& b, Mat& c, double alpha=1);
257 void divide(const Mat& a, const Mat& b, Mat& c, double alpha=1);
258
259 void convert(const Mat& src, cv::OutputArray dst, int dtype, double alpha=1, double beta=0);
260 void copy(const Mat& src, Mat& dst, const Mat& mask=Mat(), bool invertMask=false);
261 void set(Mat& dst, const Scalar& gamma, const Mat& mask=Mat());
262
263 // working with multi-channel arrays
264 void extract( const Mat& a, Mat& plane, int coi );
265 void insert( const Mat& plane, Mat& a, int coi );
266
267 // checks that the array does not have NaNs and/or Infs and all the elements are
268 // within [min_val,max_val). idx is the index of the first "bad" element.
269 int check( const Mat& data, double min_val, double max_val, vector<int>* idx );
270
271 // modifies values that are close to zero
272 void  patchZeros( Mat& mat, double level );
273
274 void transpose(const Mat& src, Mat& dst);
275 void erode(const Mat& src, Mat& dst, const Mat& _kernel, Point anchor=Point(-1,-1),
276                       int borderType=0, const Scalar& borderValue=Scalar());
277 void dilate(const Mat& src, Mat& dst, const Mat& _kernel, Point anchor=Point(-1,-1),
278                        int borderType=0, const Scalar& borderValue=Scalar());
279 void filter2D(const Mat& src, Mat& dst, int ddepth, const Mat& kernel,
280                          Point anchor, double delta, int borderType,
281                          const Scalar& borderValue=Scalar());
282 void copyMakeBorder(const Mat& src, Mat& dst, int top, int bottom, int left, int right,
283                                int borderType, const Scalar& borderValue=Scalar());
284 Mat calcSobelKernel2D( int dx, int dy, int apertureSize, int origin=0 );
285 Mat calcLaplaceKernel2D( int aperture_size );
286
287 void initUndistortMap( const Mat& a, const Mat& k, Size sz, Mat& mapx, Mat& mapy );
288
289 void minMaxLoc(const Mat& src, double* minval, double* maxval,
290                           vector<int>* minloc, vector<int>* maxloc, const Mat& mask=Mat());
291 double norm(InputArray src, int normType, InputArray mask=noArray());
292 double norm(InputArray src1, InputArray src2, int normType, InputArray mask=noArray());
293 Scalar mean(const Mat& src, const Mat& mask=Mat());
294 double PSNR(InputArray src1, InputArray src2);
295
296 bool cmpUlps(const Mat& data, const Mat& refdata, int expMaxDiff, double* realMaxDiff, vector<int>* idx);
297
298 // compares two arrays. max_diff is the maximum actual difference,
299 // success_err_level is maximum allowed difference, idx is the index of the first
300 // element for which difference is >success_err_level
301 // (or index of element with the maximum difference)
302 int cmpEps( const Mat& data, const Mat& refdata, double* max_diff,
303                        double success_err_level, vector<int>* idx,
304                        bool element_wise_relative_error );
305
306 // a wrapper for the previous function. in case of error prints the message to log file.
307 int cmpEps2( TS* ts, const Mat& data, const Mat& refdata, double success_err_level,
308                         bool element_wise_relative_error, const char* desc );
309
310 int cmpEps2_64f( TS* ts, const double* val, const double* refval, int len,
311                         double eps, const char* param_name );
312
313 void logicOp(const Mat& src1, const Mat& src2, Mat& dst, char c);
314 void logicOp(const Mat& src, const Scalar& s, Mat& dst, char c);
315 void min(const Mat& src1, const Mat& src2, Mat& dst);
316 void min(const Mat& src, double s, Mat& dst);
317 void max(const Mat& src1, const Mat& src2, Mat& dst);
318 void max(const Mat& src, double s, Mat& dst);
319
320 void compare(const Mat& src1, const Mat& src2, Mat& dst, int cmpop);
321 void compare(const Mat& src, double s, Mat& dst, int cmpop);
322 void gemm(const Mat& src1, const Mat& src2, double alpha,
323                      const Mat& src3, double beta, Mat& dst, int flags);
324 void transform( const Mat& src, Mat& dst, const Mat& transmat, const Mat& shift );
325 double crossCorr(const Mat& src1, const Mat& src2);
326 void threshold( const Mat& src, Mat& dst, double thresh, double maxval, int thresh_type );
327 void minMaxIdx( InputArray _img, double* minVal, double* maxVal,
328                     Point* minLoc, Point* maxLoc, InputArray _mask );
329
330 struct MatInfo
331 {
332     MatInfo(const Mat& _m) : m(&_m) {}
333     const Mat* m;
334 };
335
336 std::ostream& operator << (std::ostream& out, const MatInfo& m);
337
338 struct MatComparator
339 {
340 public:
341     MatComparator(double maxdiff, int context);
342
343     ::testing::AssertionResult operator()(const char* expr1, const char* expr2,
344                                           const Mat& m1, const Mat& m2);
345
346     double maxdiff;
347     double realmaxdiff;
348     vector<int> loc0;
349     int context;
350 };
351
352
353
354 class BaseTest;
355 class TS;
356
357 class BaseTest
358 {
359 public:
360     // constructor(s) and destructor
361     BaseTest();
362     virtual ~BaseTest();
363
364     // the main procedure of the test
365     virtual void run( int start_from );
366
367     // the wrapper for run that cares of exceptions
368     virtual void safe_run( int start_from=0 );
369
370     const string& get_name() const { return name; }
371
372     // returns true if and only if the different test cases do not depend on each other
373     // (so that test system could get right to a problematic test case)
374     virtual bool can_do_fast_forward();
375
376     // deallocates all the memory.
377     // called by init() (before initialization) and by the destructor
378     virtual void clear();
379
380 protected:
381     int test_case_count; // the total number of test cases
382
383     // read test params
384     virtual int read_params( CvFileStorage* fs );
385
386     // returns the number of tests or -1 if it is unknown a-priori
387     virtual int get_test_case_count();
388
389     // prepares data for the next test case. rng seed is updated by the function
390     virtual int prepare_test_case( int test_case_idx );
391
392     // checks if the test output is valid and accurate
393     virtual int validate_test_results( int test_case_idx );
394
395     // calls the tested function. the method is called from run_test_case()
396     virtual void run_func(); // runs tested func(s)
397
398     // updates progress bar
399     virtual int update_progress( int progress, int test_case_idx, int count, double dt );
400
401     // finds test parameter
402     const CvFileNode* find_param( CvFileStorage* fs, const char* param_name );
403
404     // name of the test (it is possible to locate a test by its name)
405     string name;
406
407     // pointer to the system that includes the test
408     TS* ts;
409 };
410
411
412 /*****************************************************************************************\
413 *                               Information about a failed test                           *
414 \*****************************************************************************************/
415
416 struct TestInfo
417 {
418     TestInfo();
419
420     // pointer to the test
421     BaseTest* test;
422
423     // failure code (TS::FAIL_*)
424     int code;
425
426     // seed value right before the data for the failed test case is prepared.
427     uint64 rng_seed;
428
429     // seed value right before running the test
430     uint64 rng_seed0;
431
432     // index of test case, can be then passed to BaseTest::proceed_to_test_case()
433     int test_case_idx;
434 };
435
436 /*****************************************************************************************\
437 *                                 Base Class for test system                              *
438 \*****************************************************************************************/
439
440 // common parameters:
441 struct TSParams
442 {
443     TSParams();
444
445     // RNG seed, passed to and updated by every test executed.
446     uint64 rng_seed;
447
448     // whether to use IPP, MKL etc. or not
449     bool use_optimized;
450
451     // extensivity of the tests, scale factor for test_case_count
452     double test_case_count_scale;
453 };
454
455
456 class TS
457 {
458     TS();
459     virtual ~TS();
460 public:
461
462     enum
463     {
464         NUL=0,
465         SUMMARY_IDX=0,
466         SUMMARY=1 << SUMMARY_IDX,
467         LOG_IDX=1,
468         LOG=1 << LOG_IDX,
469         CSV_IDX=2,
470         CSV=1 << CSV_IDX,
471         CONSOLE_IDX=3,
472         CONSOLE=1 << CONSOLE_IDX,
473         MAX_IDX=4
474     };
475
476     static TS* ptr();
477
478     // initialize test system before running the first test
479     virtual void init( const string& modulename );
480
481     // low-level printing functions that are used by individual tests and by the system itself
482     virtual void printf( int streams, const char* fmt, ... );
483     virtual void vprintf( int streams, const char* fmt, va_list arglist );
484
485     // updates the context: current test, test case, rng state
486     virtual void update_context( BaseTest* test, int test_case_idx, bool update_ts_context );
487
488     const TestInfo* get_current_test_info() { return &current_test_info; }
489
490     // sets information about a failed test
491     virtual void set_failed_test_info( int fail_code );
492
493     virtual void set_gtest_status();
494
495     // test error codes
496     enum FailureCode
497     {
498         // everything is Ok
499         OK=0,
500
501         // generic error: stub value to be used
502         // temporarily if the error's cause is unknown
503         FAIL_GENERIC=-1,
504
505         // the test is missing some essential data to proceed further
506         FAIL_MISSING_TEST_DATA=-2,
507
508         // the tested function raised an error via cxcore error handler
509         FAIL_ERROR_IN_CALLED_FUNC=-3,
510
511         // an exception has been raised;
512         // for memory and arithmetic exception
513         // there are two specialized codes (see below...)
514         FAIL_EXCEPTION=-4,
515
516         // a memory exception
517         // (access violation, access to missed page, stack overflow etc.)
518         FAIL_MEMORY_EXCEPTION=-5,
519
520         // arithmetic exception (overflow, division by zero etc.)
521         FAIL_ARITHM_EXCEPTION=-6,
522
523         // the tested function corrupted memory (no exception have been raised)
524         FAIL_MEMORY_CORRUPTION_BEGIN=-7,
525         FAIL_MEMORY_CORRUPTION_END=-8,
526
527         // the tested function (or test itself) do not deallocate some memory
528         FAIL_MEMORY_LEAK=-9,
529
530         // the tested function returned invalid object, e.g. matrix, containing NaNs,
531         // structure with NULL or out-of-range fields (while it should not)
532         FAIL_INVALID_OUTPUT=-10,
533
534         // the tested function returned valid object, but it does not match
535         // the original (or produced by the test) object
536         FAIL_MISMATCH=-11,
537
538         // the tested function returned valid object (a single number or numerical array),
539         // but it differs too much from the original (or produced by the test) object
540         FAIL_BAD_ACCURACY=-12,
541
542         // the tested function hung. Sometimes, it can be determined by unexpectedly long
543         // processing time (in this case there should be possibility to interrupt such a function
544         FAIL_HANG=-13,
545
546         // unexpected response on passing bad arguments to the tested function
547         // (the function crashed, proceed successfully (while it should not), or returned
548         // error code that is different from what is expected)
549         FAIL_BAD_ARG_CHECK=-14,
550
551         // the test data (in whole or for the particular test case) is invalid
552         FAIL_INVALID_TEST_DATA=-15,
553
554         // the test has been skipped because it is not in the selected subset of the tests to run,
555         // because it has been run already within the same run with the same parameters, or because
556         // of some other reason and this is not considered as an error.
557         // Normally TS::run() (or overridden method in the derived class) takes care of what
558         // needs to be run, so this code should not occur.
559         SKIPPED=1
560     };
561
562     // get RNG to generate random input data for a test
563     RNG& get_rng() { return rng; }
564
565     // returns the current error code
566     TS::FailureCode get_err_code() { return TS::FailureCode(current_test_info.code); }
567
568     // returns the test extensivity scale
569     double get_test_case_count_scale() { return params.test_case_count_scale; }
570
571     const string& get_data_path() const { return data_path; }
572
573     // returns textual description of failure code
574     static string str_from_code( const TS::FailureCode code );
575
576     std::vector<std::string> data_search_path;
577     std::vector<std::string> data_search_subdir;
578 protected:
579
580     // these are allocated within a test to try to keep them valid in case of stack corruption
581     RNG rng;
582
583     // information about the current test
584     TestInfo current_test_info;
585
586     // the path to data files used by tests
587     string data_path;
588
589     TSParams params;
590     std::string output_buf[MAX_IDX];
591 };
592
593
594 /*****************************************************************************************\
595 *            Subclass of BaseTest for testing functions that process dense arrays           *
596 \*****************************************************************************************/
597
598 class ArrayTest : public BaseTest
599 {
600 public:
601     // constructor(s) and destructor
602     ArrayTest();
603     virtual ~ArrayTest();
604
605     virtual void clear() CV_OVERRIDE;
606
607 protected:
608
609     virtual int read_params( CvFileStorage* fs ) CV_OVERRIDE;
610     virtual int prepare_test_case( int test_case_idx ) CV_OVERRIDE;
611     virtual int validate_test_results( int test_case_idx ) CV_OVERRIDE;
612
613     virtual void prepare_to_validation( int test_case_idx );
614     virtual void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
615     virtual void fill_array( int test_case_idx, int i, int j, Mat& arr );
616     virtual void get_minmax_bounds( int i, int j, int type, Scalar& low, Scalar& high );
617     virtual double get_success_error_level( int test_case_idx, int i, int j );
618
619     bool cvmat_allowed;
620     bool iplimage_allowed;
621     bool optional_mask;
622     bool element_wise_relative_error;
623
624     int min_log_array_size;
625     int max_log_array_size;
626
627     enum { INPUT, INPUT_OUTPUT, OUTPUT, REF_INPUT_OUTPUT, REF_OUTPUT, TEMP, MASK, MAX_ARR };
628
629     vector<vector<void*> > test_array;
630     vector<vector<Mat> > test_mat;
631     float buf[4];
632 };
633
634
635 class BadArgTest : public BaseTest
636 {
637 public:
638     // constructor(s) and destructor
639     BadArgTest();
640     virtual ~BadArgTest();
641
642 protected:
643     virtual int run_test_case( int expected_code, const string& descr );
644     virtual void run_func(void) CV_OVERRIDE = 0;
645     int test_case_idx;
646
647     template<class F>
648     int run_test_case( int expected_code, const string& _descr, F f)
649     {
650         int errcount = 0;
651         bool thrown = false;
652         const char* descr = _descr.c_str() ? _descr.c_str() : "";
653
654         try
655         {
656             f();
657         }
658         catch(const cv::Exception& e)
659         {
660             thrown = true;
661             if( e.code != expected_code )
662             {
663                 ts->printf(TS::LOG, "%s (test case #%d): the error code %d is different from the expected %d\n",
664                     descr, test_case_idx, e.code, expected_code);
665                 errcount = 1;
666             }
667         }
668         catch(...)
669         {
670             thrown = true;
671             ts->printf(TS::LOG, "%s  (test case #%d): unknown exception was thrown (the function has likely crashed)\n",
672                        descr, test_case_idx);
673             errcount = 1;
674         }
675         if(!thrown)
676         {
677             ts->printf(TS::LOG, "%s  (test case #%d): no expected exception was thrown\n",
678                        descr, test_case_idx);
679             errcount = 1;
680         }
681         test_case_idx++;
682
683         return errcount;
684     }
685 };
686
687 extern uint64 param_seed;
688
689 struct DefaultRngAuto
690 {
691     const uint64 old_state;
692
693     DefaultRngAuto() : old_state(cv::theRNG().state) { cv::theRNG().state = cvtest::param_seed; }
694     ~DefaultRngAuto() { cv::theRNG().state = old_state; }
695
696     DefaultRngAuto& operator=(const DefaultRngAuto&);
697 };
698
699
700 // test images generation functions
701 void fillGradient(Mat& img, int delta = 5);
702 void smoothBorder(Mat& img, const Scalar& color, int delta = 3);
703
704 // Utility functions
705
706 void addDataSearchPath(const std::string& path);
707 void addDataSearchSubDirectory(const std::string& subdir);
708
709 /*! @brief Try to find requested data file
710
711   Search directories:
712
713   0. TS::data_search_path (search sub-directories are not used)
714   1. OPENCV_TEST_DATA_PATH environment variable
715   2. One of these:
716      a. OpenCV testdata based on build location: "./" + "share/OpenCV/testdata"
717      b. OpenCV testdata at install location: CMAKE_INSTALL_PREFIX + "share/OpenCV/testdata"
718
719   Search sub-directories:
720
721   - addDataSearchSubDirectory()
722   - modulename from TS::init()
723
724  */
725 std::string findDataFile(const std::string& relative_path, bool required = true);
726
727 /*! @brief Try to find requested data directory
728 @sa findDataFile
729  */
730 std::string findDataDirectory(const std::string& relative_path, bool required = true);
731
732 // Test definitions
733
734 class SystemInfoCollector : public testing::EmptyTestEventListener
735 {
736 private:
737     virtual void OnTestProgramStart(const testing::UnitTest&);
738 };
739
740 #ifndef __CV_TEST_EXEC_ARGS
741 #if defined(_MSC_VER) && (_MSC_VER <= 1400)
742 #define __CV_TEST_EXEC_ARGS(...)    \
743     while (++argc >= (--argc,-1)) {__VA_ARGS__; break;} /*this ugly construction is needed for VS 2005*/
744 #else
745 #define __CV_TEST_EXEC_ARGS(...)    \
746     __VA_ARGS__;
747 #endif
748 #endif
749
750 void parseCustomOptions(int argc, char **argv);
751
752 #define CV_TEST_INIT0_NOOP (void)0
753
754 #define CV_TEST_MAIN(resourcesubdir, ...) CV_TEST_MAIN_EX(resourcesubdir, NOOP, __VA_ARGS__)
755
756 #define CV_TEST_MAIN_EX(resourcesubdir, INIT0, ...) \
757 int main(int argc, char **argv) \
758 { \
759     CV_TRACE_FUNCTION(); \
760     { CV_TRACE_REGION("INIT"); \
761     using namespace cvtest; \
762     TS* ts = TS::ptr(); \
763     ts->init(resourcesubdir); \
764     __CV_TEST_EXEC_ARGS(CV_TEST_INIT0_ ## INIT0) \
765     ::testing::InitGoogleTest(&argc, argv); \
766     ::testing::UnitTest::GetInstance()->listeners().Append(new SystemInfoCollector); \
767     __CV_TEST_EXEC_ARGS(__VA_ARGS__) \
768     parseCustomOptions(argc, argv); \
769     } \
770     return RUN_ALL_TESTS(); \
771 }
772
773 // This usually only makes sense in perf tests with several implementations,
774 // some of which are not available.
775 #define CV_TEST_FAIL_NO_IMPL() do { \
776     ::testing::Test::RecordProperty("custom_status", "noimpl"); \
777     FAIL() << "No equivalent implementation."; \
778 } while (0)
779
780 } //namespace cvtest
781
782 #include "opencv2/ts/ts_perf.hpp"
783
784 namespace cvtest {
785 using perf::MatDepth;
786 using perf::MatType;
787 }
788
789 #ifdef WINRT
790 #ifndef __FSTREAM_EMULATED__
791 #define __FSTREAM_EMULATED__
792 #include <stdlib.h>
793 #include <fstream>
794 #include <sstream>
795
796 #undef ifstream
797 #undef ofstream
798 #define ifstream ifstream_emulated
799 #define ofstream ofstream_emulated
800
801 namespace std {
802
803 class ifstream : public stringstream
804 {
805     FILE* f;
806 public:
807     ifstream(const char* filename, ios_base::openmode mode = ios_base::in)
808         : f(NULL)
809     {
810         string modeStr("r");
811         printf("Open file (read): %s\n", filename);
812         if (mode & ios_base::binary)
813             modeStr += "b";
814         f = fopen(filename, modeStr.c_str());
815
816         if (f == NULL)
817         {
818             printf("Can't open file: %s\n", filename);
819             return;
820         }
821         fseek(f, 0, SEEK_END);
822         size_t sz = ftell(f);
823         if (sz > 0)
824         {
825             char* buf = (char*) malloc(sz);
826             fseek(f, 0, SEEK_SET);
827             if (fread(buf, 1, sz, f) == sz)
828             {
829                 this->str(std::string(buf, sz));
830             }
831             free(buf);
832         }
833     }
834
835     ~ifstream() { close(); }
836     bool is_open() const { return f != NULL; }
837     void close()
838     {
839         if (f)
840             fclose(f);
841         f = NULL;
842         this->str("");
843     }
844 };
845
846 class ofstream : public stringstream
847 {
848     FILE* f;
849 public:
850     ofstream(const char* filename, ios_base::openmode mode = ios_base::out)
851     : f(NULL)
852     {
853         open(filename, mode);
854     }
855     ~ofstream() { close(); }
856     void open(const char* filename, ios_base::openmode mode = ios_base::out)
857     {
858         string modeStr("w+");
859         if (mode & ios_base::trunc)
860             modeStr = "w";
861         if (mode & ios_base::binary)
862             modeStr += "b";
863         f = fopen(filename, modeStr.c_str());
864         printf("Open file (write): %s\n", filename);
865         if (f == NULL)
866         {
867             printf("Can't open file (write): %s\n", filename);
868             return;
869         }
870     }
871     bool is_open() const { return f != NULL; }
872     void close()
873     {
874         if (f)
875         {
876             fwrite(reinterpret_cast<const char *>(this->str().c_str()), this->str().size(), 1, f);
877             fclose(f);
878         }
879         f = NULL;
880         this->str("");
881     }
882 };
883
884 } // namespace std
885 #endif // __FSTREAM_EMULATED__
886 #endif // WINRT
887
888
889 namespace opencv_test {
890 using namespace cvtest;
891 using namespace cv;
892
893 #ifdef CV_CXX11
894 #define CVTEST_GUARD_SYMBOL(name) \
895     class required_namespace_specificatin_here_for_symbol_ ## name {}; \
896     using name = required_namespace_specificatin_here_for_symbol_ ## name;
897 #else
898 #define CVTEST_GUARD_SYMBOL(name) /* nothing */
899 #endif
900
901 CVTEST_GUARD_SYMBOL(norm)
902 CVTEST_GUARD_SYMBOL(add)
903 CVTEST_GUARD_SYMBOL(multiply)
904 CVTEST_GUARD_SYMBOL(divide)
905 CVTEST_GUARD_SYMBOL(transpose)
906 CVTEST_GUARD_SYMBOL(copyMakeBorder)
907 CVTEST_GUARD_SYMBOL(filter2D)
908 CVTEST_GUARD_SYMBOL(compare)
909 CVTEST_GUARD_SYMBOL(minMaxIdx)
910 CVTEST_GUARD_SYMBOL(threshold)
911
912 extern bool required_opencv_test_namespace;  // compilation check for non-refactored tests
913 }
914
915 #endif // OPENCV_TS_HPP