include/opencv2/ts/ts.hpp
Go to the documentation of this file.
00001 #ifndef __OPENCV_GTESTCV_HPP__
00002 #define __OPENCV_GTESTCV_HPP__
00003 
00004 #if HAVE_CVCONFIG_H
00005 #include "cvconfig.h"
00006 #endif
00007 #ifndef GTEST_CREATE_SHARED_LIBRARY
00008 #ifdef BUILD_SHARED_LIBS
00009 #define GTEST_LINKED_AS_SHARED_LIBRARY 1
00010 #endif
00011 #endif
00012 
00013 #ifdef ANDROID
00014 # include <android/api-level.h>
00015 # define GTEST_HAS_CLONE (__ANDROID_API__ > 7 && !defined __i386__)
00016 # define GTEST_HAS_POSIX_RE (__ANDROID_API__ > 7)
00017 # if defined _GLIBCXX_USE_WCHAR_T && _GLIBCXX_USE_WCHAR_T
00018 #  define GTEST_HAS_STD_WSTRING 1
00019 # else
00020 #  define GTEST_HAS_STD_WSTRING 0
00021 #endif
00022 #endif
00023 
00024 #include <stdarg.h> // for va_list
00025 
00026 #ifdef _MSC_VER
00027 #pragma warning( disable: 4127 )
00028 #endif
00029 
00030 #define GTEST_DONT_DEFINE_FAIL      0
00031 #define GTEST_DONT_DEFINE_SUCCEED   0
00032 #define GTEST_DONT_DEFINE_ASSERT_EQ 0
00033 #define GTEST_DONT_DEFINE_ASSERT_NE 0
00034 #define GTEST_DONT_DEFINE_ASSERT_LE 0
00035 #define GTEST_DONT_DEFINE_ASSERT_LT 0
00036 #define GTEST_DONT_DEFINE_ASSERT_GE 0
00037 #define GTEST_DONT_DEFINE_ASSERT_GT 0
00038 #define GTEST_DONT_DEFINE_TEST      0
00039 
00040 #include "opencv2/ts/ts_gtest.h"
00041 
00042 #ifndef GTEST_USES_SIMPLE_RE
00043 #  define GTEST_USES_SIMPLE_RE 0
00044 #endif
00045 #ifndef GTEST_USES_POSIX_RE
00046 #  define GTEST_USES_POSIX_RE 0
00047 #endif
00048 
00049 #include "opencv2/core/core.hpp"
00050 
00051 namespace cvtest
00052 {
00053 
00054 using std::vector;
00055 using std::string;
00056 using cv::RNG;
00057 using cv::Mat;
00058 using cv::Scalar;
00059 using cv::Size;
00060 using cv::Point;
00061 using cv::Rect;
00062 
00063 class CV_EXPORTS TS;
00064 
00065 CV_EXPORTS int64 readSeed(const char* str);
00066 
00067 CV_EXPORTS void randUni( RNG& rng, Mat& a, const Scalar& param1, const Scalar& param2 );
00068 
00069 inline unsigned randInt( RNG& rng )
00070 {
00071     return (unsigned)rng;
00072 }
00073 
00074 inline  double randReal( RNG& rng )
00075 {
00076     return (double)rng;
00077 }
00078 
00079 
00080 CV_EXPORTS const char* getTypeName( int type );
00081 CV_EXPORTS int typeByName( const char* type_name );
00082 
00083 CV_EXPORTS string vec2str(const string& sep, const int* v, size_t nelems);
00084 
00085 inline int clipInt( int val, int min_val, int max_val )
00086 {
00087     if( val < min_val )
00088         val = min_val;
00089     if( val > max_val )
00090         val = max_val;
00091     return val;
00092 }
00093 
00094 CV_EXPORTS double getMinVal(int depth);
00095 CV_EXPORTS double getMaxVal(int depth);
00096 
00097 CV_EXPORTS Size randomSize(RNG& rng, double maxSizeLog);
00098 CV_EXPORTS void randomSize(RNG& rng, int minDims, int maxDims, double maxSizeLog, vector<int>& sz);
00099 CV_EXPORTS int randomType(RNG& rng, int typeMask, int minChannels, int maxChannels);
00100 CV_EXPORTS Mat randomMat(RNG& rng, Size size, int type, double minVal, double maxVal, bool useRoi);
00101 CV_EXPORTS Mat randomMat(RNG& rng, const vector<int>& size, int type, double minVal, double maxVal, bool useRoi);
00102 CV_EXPORTS void add(const Mat& a, double alpha, const Mat& b, double beta,
00103                       Scalar gamma, Mat& c, int ctype, bool calcAbs=false);
00104 CV_EXPORTS void multiply(const Mat& a, const Mat& b, Mat& c, double alpha=1);
00105 CV_EXPORTS void divide(const Mat& a, const Mat& b, Mat& c, double alpha=1);
00106 
00107 CV_EXPORTS void convert(const Mat& src, Mat& dst, int dtype, double alpha=1, double beta=0);
00108 CV_EXPORTS void copy(const Mat& src, Mat& dst, const Mat& mask=Mat(), bool invertMask=false);
00109 CV_EXPORTS void set(Mat& dst, const Scalar& gamma, const Mat& mask=Mat());
00110 
00111 // working with multi-channel arrays
00112 CV_EXPORTS void extract( const Mat& a, Mat& plane, int coi );
00113 CV_EXPORTS void insert( const Mat& plane, Mat& a, int coi );
00114 
00115 // checks that the array does not have NaNs and/or Infs and all the elements are
00116 // within [min_val,max_val). idx is the index of the first "bad" element.
00117 CV_EXPORTS int check( const Mat& data, double min_val, double max_val, vector<int>* idx );
00118 
00119 // modifies values that are close to zero
00120 CV_EXPORTS void  patchZeros( Mat& mat, double level );
00121 
00122 CV_EXPORTS void transpose(const Mat& src, Mat& dst);
00123 CV_EXPORTS void erode(const Mat& src, Mat& dst, const Mat& _kernel, Point anchor=Point(-1,-1),
00124                       int borderType=IPL_BORDER_CONSTANT, const Scalar& borderValue=Scalar());
00125 CV_EXPORTS void dilate(const Mat& src, Mat& dst, const Mat& _kernel, Point anchor=Point(-1,-1),
00126                        int borderType=IPL_BORDER_CONSTANT, const Scalar& borderValue=Scalar());
00127 CV_EXPORTS void filter2D(const Mat& src, Mat& dst, int ddepth, const Mat& kernel,
00128                          Point anchor, double delta, int borderType,
00129                          const Scalar& borderValue=Scalar());
00130 CV_EXPORTS void copyMakeBorder(const Mat& src, Mat& dst, int top, int bottom, int left, int right,
00131                                int borderType, const Scalar& borderValue=Scalar());
00132 CV_EXPORTS Mat calcSobelKernel2D( int dx, int dy, int apertureSize, int origin=0 );
00133 CV_EXPORTS Mat calcLaplaceKernel2D( int aperture_size );
00134 
00135 CV_EXPORTS void initUndistortMap( const Mat& a, const Mat& k, Size sz, Mat& mapx, Mat& mapy );
00136 
00137 CV_EXPORTS void minMaxLoc(const Mat& src, double* minval, double* maxval,
00138                           vector<int>* minloc, vector<int>* maxloc, const Mat& mask=Mat());
00139 CV_EXPORTS double norm(const Mat& src, int normType, const Mat& mask=Mat());
00140 CV_EXPORTS double norm(const Mat& src1, const Mat& src2, int normType, const Mat& mask=Mat());
00141 CV_EXPORTS Scalar mean(const Mat& src, const Mat& mask=Mat());
00142 
00143 CV_EXPORTS bool cmpUlps(const Mat& data, const Mat& refdata, int expMaxDiff, double* realMaxDiff, vector<int>* idx);
00144 
00145 // compares two arrays. max_diff is the maximum actual difference,
00146 // success_err_level is maximum allowed difference, idx is the index of the first
00147 // element for which difference is >success_err_level
00148 // (or index of element with the maximum difference)
00149 CV_EXPORTS int cmpEps( const Mat& data, const Mat& refdata, double* max_diff,
00150                        double success_err_level, vector<int>* idx,
00151                        bool element_wise_relative_error );
00152 
00153 // a wrapper for the previous function. in case of error prints the message to log file.
00154 CV_EXPORTS int cmpEps2( TS* ts, const Mat& data, const Mat& refdata, double success_err_level,
00155                         bool element_wise_relative_error, const char* desc );
00156 
00157 CV_EXPORTS int cmpEps2_64f( TS* ts, const double* val, const double* refval, int len,
00158                         double eps, const char* param_name );
00159 
00160 CV_EXPORTS void logicOp(const Mat& src1, const Mat& src2, Mat& dst, char c);
00161 CV_EXPORTS void logicOp(const Mat& src, const Scalar& s, Mat& dst, char c);
00162 CV_EXPORTS void min(const Mat& src1, const Mat& src2, Mat& dst);
00163 CV_EXPORTS void min(const Mat& src, double s, Mat& dst);
00164 CV_EXPORTS void max(const Mat& src1, const Mat& src2, Mat& dst);
00165 CV_EXPORTS void max(const Mat& src, double s, Mat& dst);
00166 
00167 CV_EXPORTS void compare(const Mat& src1, const Mat& src2, Mat& dst, int cmpop);
00168 CV_EXPORTS void compare(const Mat& src, double s, Mat& dst, int cmpop);
00169 CV_EXPORTS void gemm(const Mat& src1, const Mat& src2, double alpha,
00170                      const Mat& src3, double beta, Mat& dst, int flags);
00171     CV_EXPORTS void transform( const Mat& src, Mat& dst, const Mat& transmat, const Mat& shift );
00172 CV_EXPORTS double crossCorr(const Mat& src1, const Mat& src2);
00173 
00174 struct CV_EXPORTS MatInfo
00175 {
00176     MatInfo(const Mat& _m) : m(&_m) {}
00177     const Mat* m;
00178 };
00179 
00180 CV_EXPORTS std::ostream& operator << (std::ostream& out, const MatInfo& m);
00181 
00182 struct CV_EXPORTS MatComparator
00183 {
00184 public:
00185     MatComparator(double maxdiff, int context);
00186 
00187     ::testing::AssertionResult operator()(const char* expr1, const char* expr2,
00188                                           const Mat& m1, const Mat& m2);
00189 
00190     double maxdiff;
00191     double realmaxdiff;
00192     vector<int> loc0;
00193     int context;
00194 };
00195 
00196 
00197 
00198 class BaseTest;
00199 class TS;
00200 
00201 class CV_EXPORTS BaseTest
00202 {
00203 public:
00204     // constructor(s) and destructor
00205     BaseTest();
00206     virtual ~BaseTest();
00207 
00208     // the main procedure of the test
00209     virtual void run( int start_from );
00210 
00211     // the wrapper for run that cares of exceptions
00212     virtual void safe_run( int start_from=0 );
00213 
00214     const string& get_name() const { return name; }
00215 
00216     // returns true if and only if the different test cases do not depend on each other
00217     // (so that test system could get right to a problematic test case)
00218     virtual bool can_do_fast_forward();
00219 
00220     // deallocates all the memory.
00221     // called by init() (before initialization) and by the destructor
00222     virtual void clear();
00223 
00224 protected:
00225     int test_case_count; // the total number of test cases
00226 
00227     // read test params
00228     virtual int read_params( CvFileStorage* fs );
00229 
00230     // returns the number of tests or -1 if it is unknown a-priori
00231     virtual int get_test_case_count();
00232 
00233     // prepares data for the next test case. rng seed is updated by the function
00234     virtual int prepare_test_case( int test_case_idx );
00235 
00236     // checks if the test output is valid and accurate
00237     virtual int validate_test_results( int test_case_idx );
00238 
00239     // calls the tested function. the method is called from run_test_case()
00240     virtual void run_func(); // runs tested func(s)
00241 
00242     // updates progress bar
00243     virtual int update_progress( int progress, int test_case_idx, int count, double dt );
00244 
00245     // finds test parameter
00246     const CvFileNode* find_param( CvFileStorage* fs, const char* param_name );
00247 
00248     // name of the test (it is possible to locate a test by its name)
00249     string name;
00250 
00251     // pointer to the system that includes the test
00252     TS* ts;
00253 };
00254 
00255 
00256 /*****************************************************************************************\
00257 *                               Information about a failed test                           *
00258 \*****************************************************************************************/
00259 
00260 struct TestInfo
00261 {
00262     TestInfo();
00263 
00264     // pointer to the test
00265     BaseTest* test;
00266 
00267     // failure code (CV_FAIL*)
00268     int code;
00269 
00270     // seed value right before the data for the failed test case is prepared.
00271     uint64 rng_seed;
00272 
00273     // seed value right before running the test
00274     uint64 rng_seed0;
00275 
00276     // index of test case, can be then passed to BaseTest::proceed_to_test_case()
00277     int test_case_idx;
00278 };
00279 
00280 /*****************************************************************************************\
00281 *                                 Base Class for test system                              *
00282 \*****************************************************************************************/
00283 
00284 // common parameters:
00285 struct CV_EXPORTS TSParams
00286 {
00287     TSParams();
00288 
00289     // RNG seed, passed to and updated by every test executed.
00290     uint64 rng_seed;
00291 
00292     // whether to use IPP, MKL etc. or not
00293     bool use_optimized;
00294 
00295     // extensivity of the tests, scale factor for test_case_count
00296     double test_case_count_scale;
00297 };
00298 
00299 
00300 class CV_EXPORTS TS
00301 {
00302 public:
00303     // constructor(s) and destructor
00304     TS();
00305     virtual ~TS();
00306 
00307     enum
00308     {
00309         NUL=0,
00310         SUMMARY_IDX=0,
00311         SUMMARY=1 << SUMMARY_IDX,
00312         LOG_IDX=1,
00313         LOG=1 << LOG_IDX,
00314         CSV_IDX=2,
00315         CSV=1 << CSV_IDX,
00316         CONSOLE_IDX=3,
00317         CONSOLE=1 << CONSOLE_IDX,
00318         MAX_IDX=4
00319     };
00320 
00321     static TS* ptr();
00322 
00323     // initialize test system before running the first test
00324     virtual void init( const string& modulename );
00325 
00326     // low-level printing functions that are used by individual tests and by the system itself
00327     virtual void printf( int streams, const char* fmt, ... );
00328     virtual void vprintf( int streams, const char* fmt, va_list arglist );
00329 
00330     // updates the context: current test, test case, rng state
00331     virtual void update_context( BaseTest* test, int test_case_idx, bool update_ts_context );
00332 
00333     const TestInfo* get_current_test_info() { return &current_test_info; }
00334 
00335     // sets information about a failed test
00336     virtual void set_failed_test_info( int fail_code );
00337 
00338     virtual void set_gtest_status();
00339 
00340     // test error codes
00341     enum
00342     {
00343         // everything is Ok
00344         OK=0,
00345 
00346         // generic error: stub value to be used
00347         // temporarily if the error's cause is unknown
00348         FAIL_GENERIC=-1,
00349 
00350         // the test is missing some essential data to proceed further
00351         FAIL_MISSING_TEST_DATA=-2,
00352 
00353         // the tested function raised an error via cxcore error handler
00354         FAIL_ERROR_IN_CALLED_FUNC=-3,
00355 
00356         // an exception has been raised;
00357         // for memory and arithmetic exception
00358         // there are two specialized codes (see below...)
00359         FAIL_EXCEPTION=-4,
00360 
00361         // a memory exception
00362         // (access violation, access to missed page, stack overflow etc.)
00363         FAIL_MEMORY_EXCEPTION=-5,
00364 
00365         // arithmetic exception (overflow, division by zero etc.)
00366         FAIL_ARITHM_EXCEPTION=-6,
00367 
00368         // the tested function corrupted memory (no exception have been raised)
00369         FAIL_MEMORY_CORRUPTION_BEGIN=-7,
00370         FAIL_MEMORY_CORRUPTION_END=-8,
00371 
00372         // the tested function (or test ifself) do not deallocate some memory
00373         FAIL_MEMORY_LEAK=-9,
00374 
00375         // the tested function returned invalid object, e.g. matrix, containing NaNs,
00376         // structure with NULL or out-of-range fields (while it should not)
00377         FAIL_INVALID_OUTPUT=-10,
00378 
00379         // the tested function returned valid object, but it does not match to
00380         // the original (or produced by the test) object
00381         FAIL_MISMATCH=-11,
00382 
00383         // the tested function returned valid object (a single number or numerical array),
00384         // but it differs too much from the original (or produced by the test) object
00385         FAIL_BAD_ACCURACY=-12,
00386 
00387         // the tested function hung. Sometimes, can be determined by unexpectedly long
00388         // processing time (in this case there should be possibility to interrupt such a function
00389         FAIL_HANG=-13,
00390 
00391         // unexpected responce on passing bad arguments to the tested function
00392         // (the function crashed, proceed succesfully (while it should not), or returned
00393         // error code that is different from what is expected)
00394         FAIL_BAD_ARG_CHECK=-14,
00395 
00396         // the test data (in whole or for the particular test case) is invalid
00397         FAIL_INVALID_TEST_DATA=-15,
00398 
00399         // the test has been skipped because it is not in the selected subset of the tests to run,
00400         // because it has been run already within the same run with the same parameters, or because
00401         // of some other reason and this is not considered as an error.
00402         // Normally TS::run() (or overrided method in the derived class) takes care of what
00403         // needs to be run, so this code should not occur.
00404         SKIPPED=1
00405     };
00406 
00407     // get file storage
00408     CvFileStorage* get_file_storage();
00409 
00410     // get RNG to generate random input data for a test
00411     RNG& get_rng() { return rng; }
00412 
00413     // returns the current error code
00414     int get_err_code() { return current_test_info.code; }
00415 
00416     // returns the test extensivity scale
00417     double get_test_case_count_scale() { return params.test_case_count_scale; }
00418 
00419     const string& get_data_path() const { return data_path; }
00420 
00421     // returns textual description of failure code
00422     static string str_from_code( int code );
00423 
00424 protected:
00425 
00426     // these are allocated within a test to try keep them valid in case of stack corruption
00427     RNG rng;
00428 
00429     // information about the current test
00430     TestInfo current_test_info;
00431 
00432     // the path to data files used by tests
00433     string data_path;
00434 
00435     TSParams params;
00436     std::string output_buf[MAX_IDX];
00437 };
00438 
00439 
00440 /*****************************************************************************************\
00441 *            Subclass of BaseTest for testing functions that process dense arrays           *
00442 \*****************************************************************************************/
00443 
00444 class CV_EXPORTS ArrayTest : public BaseTest
00445 {
00446 public:
00447     // constructor(s) and destructor
00448     ArrayTest();
00449     virtual ~ArrayTest();
00450 
00451     virtual void clear();
00452 
00453 protected:
00454 
00455     virtual int read_params( CvFileStorage* fs );
00456     virtual int prepare_test_case( int test_case_idx );
00457     virtual int validate_test_results( int test_case_idx );
00458 
00459     virtual void prepare_to_validation( int test_case_idx );
00460     virtual void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
00461     virtual void fill_array( int test_case_idx, int i, int j, Mat& arr );
00462     virtual void get_minmax_bounds( int i, int j, int type, Scalar& low, Scalar& high );
00463     virtual double get_success_error_level( int test_case_idx, int i, int j );
00464 
00465     bool cvmat_allowed;
00466     bool iplimage_allowed;
00467     bool optional_mask;
00468     bool element_wise_relative_error;
00469 
00470     int min_log_array_size;
00471     int max_log_array_size;
00472 
00473     enum { INPUT, INPUT_OUTPUT, OUTPUT, REF_INPUT_OUTPUT, REF_OUTPUT, TEMP, MASK, MAX_ARR };
00474 
00475     vector<vector<void*> > test_array;
00476     vector<vector<Mat> > test_mat;
00477     float buf[4];
00478 };
00479 
00480 
00481 class CV_EXPORTS BadArgTest : public BaseTest
00482 {
00483 public:
00484     // constructor(s) and destructor
00485     BadArgTest();
00486     virtual ~BadArgTest();
00487 
00488 protected:
00489     virtual int run_test_case( int expected_code, const string& descr );
00490     virtual void run_func(void) = 0;
00491     int test_case_idx;
00492     int progress;
00493     double t, freq;
00494 
00495     template<class F>
00496     int run_test_case( int expected_code, const string& _descr, F f)
00497     {
00498         double new_t = (double)cv::getTickCount(), dt;
00499         if( test_case_idx < 0 )
00500         {
00501             test_case_idx = 0;
00502             progress = 0;
00503             dt = 0;
00504         }
00505         else
00506         {
00507             dt = (new_t - t)/(freq*1000);
00508             t = new_t;
00509         }
00510         progress = update_progress(progress, test_case_idx, 0, dt);
00511 
00512         int errcount = 0;
00513         bool thrown = false;
00514         const char* descr = _descr.c_str() ? _descr.c_str() : "";
00515 
00516         try
00517         {
00518             f();
00519         }
00520         catch(const cv::Exception& e)
00521         {
00522             thrown = true;
00523             if( e.code != expected_code )
00524             {
00525                 ts->printf(TS::LOG, "%s (test case #%d): the error code %d is different from the expected %d\n",
00526                     descr, test_case_idx, e.code, expected_code);
00527                 errcount = 1;
00528             }
00529         }
00530         catch(...)
00531         {
00532             thrown = true;
00533             ts->printf(TS::LOG, "%s  (test case #%d): unknown exception was thrown (the function has likely crashed)\n",
00534                        descr, test_case_idx);
00535             errcount = 1;
00536         }
00537         if(!thrown)
00538         {
00539             ts->printf(TS::LOG, "%s  (test case #%d): no expected exception was thrown\n",
00540                        descr, test_case_idx);
00541             errcount = 1;
00542         }
00543         test_case_idx++;
00544 
00545         return errcount;
00546     }
00547 };
00548 
00549 struct CV_EXPORTS DefaultRngAuto
00550 {
00551     const uint64 old_state;
00552 
00553     DefaultRngAuto() : old_state(cv::theRNG().state) { cv::theRNG().state = (uint64)-1; }
00554     ~DefaultRngAuto() { cv::theRNG().state = old_state; }
00555 
00556     DefaultRngAuto& operator=(const DefaultRngAuto&);
00557 };
00558 
00559 }
00560 
00561 // fills c with zeros
00562 CV_EXPORTS void cvTsZero( CvMat* c, const CvMat* mask=0 );
00563 
00564 // copies a to b (whole matrix or only the selected region)
00565 CV_EXPORTS void cvTsCopy( const CvMat* a, CvMat* b, const CvMat* mask=0 );
00566 
00567 // converts one array to another
00568 CV_EXPORTS void  cvTsConvert( const CvMat* src, CvMat* dst );
00569 
00570 CV_EXPORTS void cvTsGEMM( const CvMat* a, const CvMat* b, double alpha,
00571                          const CvMat* c, double beta, CvMat* d, int flags );
00572 
00573 #define CV_TEST_MAIN(resourcesubdir) \
00574 int main(int argc, char **argv) \
00575 { \
00576     cvtest::TS::ptr()->init(resourcesubdir); \
00577     ::testing::InitGoogleTest(&argc, argv); \
00578     return RUN_ALL_TESTS(); \
00579 }
00580 
00581 #endif
00582 
00583 #include "ts_perf.hpp"