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