Fixing static libs build on Windows
[profile/ivi/opencv.git] / modules / ts / include / opencv2 / ts / ts.hpp
1 #ifndef __OPENCV_GTESTCV_HPP__
2 #define __OPENCV_GTESTCV_HPP__
3
4 #if HAVE_CVCONFIG_H
5 #include "cvconfig.h"
6 #endif
7 #ifndef GTEST_CREATE_SHARED_LIBRARY
8 #ifdef BUILD_SHARED_LIBS
9 #define GTEST_LINKED_AS_SHARED_LIBRARY 1
10 #endif
11 #endif
12
13 #ifdef ANDROID
14 # include <android/api-level.h>
15 # define GTEST_HAS_CLONE (__ANDROID_API__ > 7)
16 # define GTEST_HAS_POSIX_RE (__ANDROID_API__ > 7)
17 # define GTEST_HAS_STD_WSTRING _GLIBCXX_USE_WCHAR_T
18 #endif
19
20 #include <stdarg.h> // for va_list
21
22 #if _MSC_VER >= 1200
23 #pragma warning( disable: 4251 4275 4355 4127 )
24 #endif
25
26 #include "opencv2/ts/ts_gtest.h"
27 #include "opencv2/core/core.hpp"
28
29 namespace cvtest
30 {
31
32 using std::vector;
33 using std::string;
34 using cv::RNG;
35 using cv::Mat;
36 using cv::Scalar;
37 using cv::Size;
38 using cv::Point;
39 using cv::Rect;
40
41 class CV_EXPORTS TS;
42     
43 CV_EXPORTS int64 readSeed(const char* str);
44     
45 CV_EXPORTS void randUni( RNG& rng, Mat& a, const Scalar& param1, const Scalar& param2 );
46
47 inline unsigned randInt( RNG& rng )
48 {
49     return (unsigned)rng;
50 }
51
52 inline  double randReal( RNG& rng )
53 {
54     return (double)rng;
55 }
56     
57     
58 CV_EXPORTS const char* getTypeName( int type );
59 CV_EXPORTS int typeByName( const char* type_name );
60
61 CV_EXPORTS string vec2str(const string& sep, const int* v, size_t nelems);
62     
63 inline int clipInt( int val, int min_val, int max_val )
64 {
65     if( val < min_val )
66         val = min_val;
67     if( val > max_val )
68         val = max_val;
69     return val;
70 }
71
72 CV_EXPORTS double getMinVal(int depth);
73 CV_EXPORTS double getMaxVal(int depth);
74     
75 CV_EXPORTS Size randomSize(RNG& rng, double maxSizeLog);
76 CV_EXPORTS void randomSize(RNG& rng, int minDims, int maxDims, double maxSizeLog, vector<int>& sz);    
77 CV_EXPORTS int randomType(RNG& rng, int typeMask, int minChannels, int maxChannels);
78 CV_EXPORTS Mat randomMat(RNG& rng, Size size, int type, double minVal, double maxVal, bool useRoi);
79 CV_EXPORTS Mat randomMat(RNG& rng, const vector<int>& size, int type, double minVal, double maxVal, bool useRoi);
80 CV_EXPORTS void add(const Mat& a, double alpha, const Mat& b, double beta,
81                       Scalar gamma, Mat& c, int ctype, bool calcAbs=false);
82 CV_EXPORTS void multiply(const Mat& a, const Mat& b, Mat& c, double alpha=1);
83 CV_EXPORTS void divide(const Mat& a, const Mat& b, Mat& c, double alpha=1);
84
85 CV_EXPORTS void convert(const Mat& src, Mat& dst, int dtype, double alpha=1, double beta=0);
86 CV_EXPORTS void copy(const Mat& src, Mat& dst, const Mat& mask=Mat(), bool invertMask=false);
87 CV_EXPORTS void set(Mat& dst, const Scalar& gamma, const Mat& mask=Mat());
88     
89 // working with multi-channel arrays
90 CV_EXPORTS void extract( const Mat& a, Mat& plane, int coi );
91 CV_EXPORTS void insert( const Mat& plane, Mat& a, int coi );
92
93 // checks that the array does not have NaNs and/or Infs and all the elements are
94 // within [min_val,max_val). idx is the index of the first "bad" element.
95 CV_EXPORTS int check( const Mat& data, double min_val, double max_val, vector<int>* idx );
96     
97 // modifies values that are close to zero
98 CV_EXPORTS void  patchZeros( Mat& mat, double level );
99     
100 CV_EXPORTS void transpose(const Mat& src, Mat& dst);
101 CV_EXPORTS void erode(const Mat& src, Mat& dst, const Mat& _kernel, Point anchor=Point(-1,-1),
102                       int borderType=IPL_BORDER_CONSTANT, const Scalar& borderValue=Scalar());
103 CV_EXPORTS void dilate(const Mat& src, Mat& dst, const Mat& _kernel, Point anchor=Point(-1,-1),
104                        int borderType=IPL_BORDER_CONSTANT, const Scalar& borderValue=Scalar());
105 CV_EXPORTS void filter2D(const Mat& src, Mat& dst, int ddepth, const Mat& kernel,
106                          Point anchor, double delta, int borderType,
107                          const Scalar& borderValue=Scalar());
108 CV_EXPORTS void copyMakeBorder(const Mat& src, Mat& dst, int top, int bottom, int left, int right,
109                                int borderType, const Scalar& borderValue=Scalar());
110 CV_EXPORTS Mat calcSobelKernel2D( int dx, int dy, int apertureSize, int origin=0 );
111 CV_EXPORTS Mat calcLaplaceKernel2D( int aperture_size );
112     
113 CV_EXPORTS void initUndistortMap( const Mat& a, const Mat& k, Size sz, Mat& mapx, Mat& mapy );
114     
115 CV_EXPORTS void minMaxLoc(const Mat& src, double* minval, double* maxval,
116                           vector<int>* minloc, vector<int>* maxloc, const Mat& mask=Mat());
117 CV_EXPORTS double norm(const Mat& src, int normType, const Mat& mask=Mat());
118 CV_EXPORTS double norm(const Mat& src1, const Mat& src2, int normType, const Mat& mask=Mat());
119 CV_EXPORTS Scalar mean(const Mat& src, const Mat& mask=Mat());
120     
121 CV_EXPORTS bool cmpUlps(const Mat& data, const Mat& refdata, int expMaxDiff, double* realMaxDiff, vector<int>* idx);
122     
123 // compares two arrays. max_diff is the maximum actual difference,
124 // success_err_level is maximum allowed difference, idx is the index of the first
125 // element for which difference is >success_err_level
126 // (or index of element with the maximum difference)
127 CV_EXPORTS int cmpEps( const Mat& data, const Mat& refdata, double* max_diff,
128                        double success_err_level, vector<int>* idx,
129                        bool element_wise_relative_error );
130     
131 // a wrapper for the previous function. in case of error prints the message to log file.
132 CV_EXPORTS int cmpEps2( TS* ts, const Mat& data, const Mat& refdata, double success_err_level,
133                         bool element_wise_relative_error, const char* desc );
134     
135 CV_EXPORTS int cmpEps2_64f( TS* ts, const double* val, const double* refval, int len,
136                         double eps, const char* param_name );
137     
138 CV_EXPORTS void logicOp(const Mat& src1, const Mat& src2, Mat& dst, char c);
139 CV_EXPORTS void logicOp(const Mat& src, const Scalar& s, Mat& dst, char c);
140 CV_EXPORTS void min(const Mat& src1, const Mat& src2, Mat& dst);
141 CV_EXPORTS void min(const Mat& src, double s, Mat& dst);    
142 CV_EXPORTS void max(const Mat& src1, const Mat& src2, Mat& dst);
143 CV_EXPORTS void max(const Mat& src, double s, Mat& dst);    
144     
145 CV_EXPORTS void compare(const Mat& src1, const Mat& src2, Mat& dst, int cmpop);
146 CV_EXPORTS void compare(const Mat& src, double s, Mat& dst, int cmpop);    
147 CV_EXPORTS void gemm(const Mat& src1, const Mat& src2, double alpha,
148                      const Mat& src3, double beta, Mat& dst, int flags);
149     CV_EXPORTS void transform( const Mat& src, Mat& dst, const Mat& transmat, const Mat& shift );
150 CV_EXPORTS double crossCorr(const Mat& src1, const Mat& src2);
151
152 struct CV_EXPORTS MatInfo
153 {
154     MatInfo(const Mat& _m) : m(&_m) {}
155     const Mat* m;
156 };
157
158 CV_EXPORTS std::ostream& operator << (std::ostream& out, const MatInfo& m);    
159     
160 struct CV_EXPORTS MatComparator
161 {
162 public:
163     MatComparator(double maxdiff, int context);
164     
165     ::testing::AssertionResult operator()(const char* expr1, const char* expr2,
166                                           const Mat& m1, const Mat& m2);
167     
168     double maxdiff;
169     double realmaxdiff;
170     vector<int> loc0;
171     int context;
172 };
173
174
175
176 class BaseTest;
177 class TS;
178
179 class CV_EXPORTS BaseTest
180 {
181 public:
182     // constructor(s) and destructor
183     BaseTest();
184     virtual ~BaseTest();
185
186     // the main procedure of the test
187     virtual void run( int start_from );
188
189     // the wrapper for run that cares of exceptions
190     virtual void safe_run( int start_from=0 );
191
192     const string& get_name() const { return name; }
193
194     // returns true if and only if the different test cases do not depend on each other
195     // (so that test system could get right to a problematic test case)
196     virtual bool can_do_fast_forward();
197
198     // deallocates all the memory.
199     // called by init() (before initialization) and by the destructor
200     virtual void clear();
201
202 protected:
203     int test_case_count; // the total number of test cases
204
205     // read test params
206     virtual int read_params( CvFileStorage* fs );
207
208     // returns the number of tests or -1 if it is unknown a-priori
209     virtual int get_test_case_count();
210
211     // prepares data for the next test case. rng seed is updated by the function
212     virtual int prepare_test_case( int test_case_idx );
213
214     // checks if the test output is valid and accurate
215     virtual int validate_test_results( int test_case_idx );
216
217     // calls the tested function. the method is called from run_test_case()
218     virtual void run_func(); // runs tested func(s)
219
220     // updates progress bar
221     virtual int update_progress( int progress, int test_case_idx, int count, double dt );
222
223     // finds test parameter
224     const CvFileNode* find_param( CvFileStorage* fs, const char* param_name );
225
226     // name of the test (it is possible to locate a test by its name)
227     string name;
228
229     // pointer to the system that includes the test
230     TS* ts;
231 };
232
233
234 /*****************************************************************************************\
235 *                               Information about a failed test                           *
236 \*****************************************************************************************/
237
238 struct TestInfo
239 {
240     TestInfo();
241     
242     // pointer to the test
243     BaseTest* test;
244
245     // failure code (CV_FAIL*)
246     int code;
247
248     // seed value right before the data for the failed test case is prepared.
249     uint64 rng_seed;
250     
251     // seed value right before running the test
252     uint64 rng_seed0;
253
254     // index of test case, can be then passed to BaseTest::proceed_to_test_case()
255     int test_case_idx;
256 };
257
258 /*****************************************************************************************\
259 *                                 Base Class for test system                              *
260 \*****************************************************************************************/
261
262 // common parameters:
263 struct CV_EXPORTS TSParams
264 {
265     TSParams();
266     
267     // RNG seed, passed to and updated by every test executed.
268     uint64 rng_seed;
269     
270     // whether to use IPP, MKL etc. or not
271     bool use_optimized;
272     
273     // extensivity of the tests, scale factor for test_case_count
274     double test_case_count_scale;
275 };
276
277     
278 class CV_EXPORTS TS
279 {
280 public:
281     // constructor(s) and destructor
282     TS();
283     virtual ~TS();
284
285     enum
286     {
287         NUL=0,
288         SUMMARY_IDX=0,
289         SUMMARY=1 << SUMMARY_IDX,
290         LOG_IDX=1,
291         LOG=1 << LOG_IDX,
292         CSV_IDX=2,
293         CSV=1 << CSV_IDX,
294         CONSOLE_IDX=3,
295         CONSOLE=1 << CONSOLE_IDX,
296         MAX_IDX=4
297     };
298
299     static TS* ptr();
300     
301     // initialize test system before running the first test
302     virtual void init( const string& modulename );
303     
304     // low-level printing functions that are used by individual tests and by the system itself
305     virtual void printf( int streams, const char* fmt, ... );
306     virtual void vprintf( int streams, const char* fmt, va_list arglist );
307
308     // updates the context: current test, test case, rng state
309     virtual void update_context( BaseTest* test, int test_case_idx, bool update_ts_context );
310
311     const TestInfo* get_current_test_info() { return &current_test_info; }
312
313     // sets information about a failed test
314     virtual void set_failed_test_info( int fail_code );
315     
316     virtual void set_gtest_status();
317
318     // test error codes
319     enum
320     {
321         // everything is Ok
322         OK=0,
323
324         // generic error: stub value to be used
325         // temporarily if the error's cause is unknown
326         FAIL_GENERIC=-1,
327
328         // the test is missing some essential data to proceed further
329         FAIL_MISSING_TEST_DATA=-2,
330
331         // the tested function raised an error via cxcore error handler
332         FAIL_ERROR_IN_CALLED_FUNC=-3,
333
334         // an exception has been raised;
335         // for memory and arithmetic exception
336         // there are two specialized codes (see below...)
337         FAIL_EXCEPTION=-4,
338
339         // a memory exception
340         // (access violation, access to missed page, stack overflow etc.)
341         FAIL_MEMORY_EXCEPTION=-5,
342
343         // arithmetic exception (overflow, division by zero etc.)
344         FAIL_ARITHM_EXCEPTION=-6,
345
346         // the tested function corrupted memory (no exception have been raised)
347         FAIL_MEMORY_CORRUPTION_BEGIN=-7,
348         FAIL_MEMORY_CORRUPTION_END=-8,
349
350         // the tested function (or test ifself) do not deallocate some memory
351         FAIL_MEMORY_LEAK=-9,
352
353         // the tested function returned invalid object, e.g. matrix, containing NaNs,
354         // structure with NULL or out-of-range fields (while it should not)
355         FAIL_INVALID_OUTPUT=-10,
356
357         // the tested function returned valid object, but it does not match to
358         // the original (or produced by the test) object
359         FAIL_MISMATCH=-11,
360
361         // the tested function returned valid object (a single number or numerical array),
362         // but it differs too much from the original (or produced by the test) object
363         FAIL_BAD_ACCURACY=-12,
364
365         // the tested function hung. Sometimes, can be determined by unexpectedly long
366         // processing time (in this case there should be possibility to interrupt such a function
367         FAIL_HANG=-13,
368
369         // unexpected responce on passing bad arguments to the tested function
370         // (the function crashed, proceed succesfully (while it should not), or returned
371         // error code that is different from what is expected)
372         FAIL_BAD_ARG_CHECK=-14,
373
374         // the test data (in whole or for the particular test case) is invalid
375         FAIL_INVALID_TEST_DATA=-15,
376
377         // the test has been skipped because it is not in the selected subset of the tests to run,
378         // because it has been run already within the same run with the same parameters, or because
379         // of some other reason and this is not considered as an error.
380         // Normally TS::run() (or overrided method in the derived class) takes care of what
381         // needs to be run, so this code should not occur.
382         SKIPPED=1
383     };
384
385     // get file storage
386     CvFileStorage* get_file_storage();
387
388     // get RNG to generate random input data for a test
389     RNG& get_rng() { return rng; }
390
391     // returns the current error code
392     int get_err_code() { return current_test_info.code; }
393
394     // returns the test extensivity scale
395     double get_test_case_count_scale() { return params.test_case_count_scale; }
396
397     const string& get_data_path() const { return data_path; }
398
399     // returns textual description of failure code
400     static string str_from_code( int code );
401     
402 protected:
403
404     // these are allocated within a test to try keep them valid in case of stack corruption
405     RNG rng;
406
407     // information about the current test
408     TestInfo current_test_info;
409         
410     // the path to data files used by tests
411     string data_path;
412     
413     TSParams params;
414     std::string output_buf[MAX_IDX];
415 };
416
417
418 /*****************************************************************************************\
419 *            Subclass of BaseTest for testing functions that process dense arrays           *
420 \*****************************************************************************************/
421
422 class CV_EXPORTS ArrayTest : public BaseTest
423 {
424 public:
425     // constructor(s) and destructor
426     ArrayTest();
427     virtual ~ArrayTest();
428
429     virtual void clear();
430
431 protected:
432
433     virtual int read_params( CvFileStorage* fs );
434     virtual int prepare_test_case( int test_case_idx );
435     virtual int validate_test_results( int test_case_idx );
436
437     virtual void prepare_to_validation( int test_case_idx );
438     virtual void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
439     virtual void fill_array( int test_case_idx, int i, int j, Mat& arr );
440     virtual void get_minmax_bounds( int i, int j, int type, Scalar& low, Scalar& high );
441     virtual double get_success_error_level( int test_case_idx, int i, int j );
442
443     bool cvmat_allowed;
444     bool iplimage_allowed;
445     bool optional_mask;
446     bool element_wise_relative_error;
447
448     int min_log_array_size;
449     int max_log_array_size;
450
451     enum { INPUT, INPUT_OUTPUT, OUTPUT, REF_INPUT_OUTPUT, REF_OUTPUT, TEMP, MASK, MAX_ARR };
452
453     vector<vector<void*> > test_array;
454     vector<vector<Mat> > test_mat;
455     float buf[4];
456 };
457
458
459 class CV_EXPORTS BadArgTest : public BaseTest
460 {
461 public:
462     // constructor(s) and destructor
463     BadArgTest();
464     virtual ~BadArgTest();
465
466 protected:
467     virtual int run_test_case( int expected_code, const string& descr );
468     virtual void run_func(void) = 0;
469     int test_case_idx;
470     int progress;
471     double t, freq;   
472
473     template<class F>
474     int run_test_case( int expected_code, const string& _descr, F f)
475     {
476         double new_t = (double)cv::getTickCount(), dt;
477         if( test_case_idx < 0 )
478         {
479             test_case_idx = 0;
480             progress = 0;
481             dt = 0;
482         }
483         else
484         {
485             dt = (new_t - t)/(freq*1000);
486             t = new_t;
487         }
488         progress = update_progress(progress, test_case_idx, 0, dt);
489         
490         int errcount = 0;
491         bool thrown = false;
492         const char* descr = _descr.c_str() ? _descr.c_str() : "";
493
494         try
495         {
496             f();
497         }
498         catch(const cv::Exception& e)
499         {
500             thrown = true;
501             if( e.code != expected_code )
502             {
503                 ts->printf(TS::LOG, "%s (test case #%d): the error code %d is different from the expected %d\n",
504                     descr, test_case_idx, e.code, expected_code);
505                 errcount = 1;
506             }
507         }
508         catch(...)
509         {
510             thrown = true;
511             ts->printf(TS::LOG, "%s  (test case #%d): unknown exception was thrown (the function has likely crashed)\n",
512                        descr, test_case_idx);
513             errcount = 1;
514         }
515         if(!thrown)
516         {
517             ts->printf(TS::LOG, "%s  (test case #%d): no expected exception was thrown\n",
518                        descr, test_case_idx);
519             errcount = 1;
520         }
521         test_case_idx++;
522         
523         return errcount;
524     }
525 };
526     
527 struct CV_EXPORTS DefaultRngAuto
528 {
529     const uint64 old_state;
530     
531     DefaultRngAuto() : old_state(cv::theRNG().state) { cv::theRNG().state = (uint64)-1; }
532     ~DefaultRngAuto() { cv::theRNG().state = old_state; }
533     
534     DefaultRngAuto& operator=(const DefaultRngAuto&);
535 };
536     
537 }
538
539 // fills c with zeros
540 CV_EXPORTS void cvTsZero( CvMat* c, const CvMat* mask=0 );
541
542 // copies a to b (whole matrix or only the selected region)
543 CV_EXPORTS void cvTsCopy( const CvMat* a, CvMat* b, const CvMat* mask=0 );
544
545 // converts one array to another
546 CV_EXPORTS void  cvTsConvert( const CvMat* src, CvMat* dst );
547
548 CV_EXPORTS void cvTsGEMM( const CvMat* a, const CvMat* b, double alpha,
549                          const CvMat* c, double beta, CvMat* d, int flags );
550
551 #define CV_TEST_MAIN(resourcesubdir) \
552 int main(int argc, char **argv) \
553 { \
554     cvtest::TS::ptr()->init(resourcesubdir); \
555     ::testing::InitGoogleTest(&argc, argv); \
556     return RUN_ALL_TESTS(); \
557 }
558
559 #endif
560
561 #include "ts_perf.hpp"