1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
8 //
9 //
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
21 //
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
25 //
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41
42 #include "precomp.hpp"
43 #include <opencv2/core/utils/configuration.private.hpp>
44
45 #include "opencv2/core/core_c.h"
46
47 #include <ctype.h>
48 #include <stdarg.h>
49 #include <stdlib.h>
50 #include <fcntl.h>
51 #include <time.h>
52 #if defined _WIN32
53 #include <io.h>
54
55 #include <windows.h>
56 #undef small
57 #undef min
58 #undef max
59 #undef abs
60
61 #ifdef _MSC_VER
62 #include <eh.h>
63 #endif
64
65 #else
66 #include <unistd.h>
67 #include <signal.h>
68 #include <setjmp.h>
69 #endif
70
71 // isDirectory
72 #if defined _WIN32 || defined WINCE
73 # include <windows.h>
74 #else
75 # include <dirent.h>
76 # include <sys/stat.h>
77 #endif
78
79 #ifdef HAVE_OPENCL
80
81 #define DUMP_CONFIG_PROPERTY(propertyName, propertyValue) \
82 do { \
83 std::stringstream ssName, ssValue;\
84 ssName << propertyName;\
85 ssValue << (propertyValue); \
86 ::testing::Test::RecordProperty(ssName.str(), ssValue.str()); \
87 } while (false)
88
89 #define DUMP_MESSAGE_STDOUT(msg) \
90 do { \
91 std::cout << msg << std::endl; \
92 } while (false)
93
94 #include "opencv2/core/opencl/opencl_info.hpp"
95
96 #include "opencv2/core/utils/allocator_stats.hpp"
97 namespace cv { namespace ocl {
98 cv::utils::AllocatorStatisticsInterface& getOpenCLAllocatorStatistics();
99 }}
100 #endif // HAVE_OPENCL
101
102 #include "opencv2/core/utils/allocator_stats.hpp"
103 namespace cv {
104 CV_EXPORTS cv::utils::AllocatorStatisticsInterface& getAllocatorStatistics();
105 }
106
107 #include "opencv_tests_config.hpp"
108
109 #include "ts_tags.hpp"
110
111 #if defined(__GNUC__) && defined(__linux__)
112 extern "C" {
113 size_t malloc_peak(void) __attribute__((weak));
114 void malloc_reset_peak(void) __attribute__((weak));
115 } // extern "C"
116 #else // stubs
117 static size_t (*malloc_peak)(void) = 0;
118 static void (*malloc_reset_peak)(void) = 0;
119 #endif
120
121 namespace opencv_test {
122 bool required_opencv_test_namespace = false; // compilation check for non-refactored tests
123 }
124
125 namespace cvtest
126 {
127
SkipTestExceptionBase(bool handlingTags)128 details::SkipTestExceptionBase::SkipTestExceptionBase(bool handlingTags)
129 {
130 if (!handlingTags)
131 {
132 testTagIncreaseSkipCount("skip_other", true, true);
133 }
134 }
SkipTestExceptionBase(const cv::String & message,bool handlingTags)135 details::SkipTestExceptionBase::SkipTestExceptionBase(const cv::String& message, bool handlingTags)
136 {
137 if (!handlingTags)
138 testTagIncreaseSkipCount("skip_other", true, true);
139 this->msg = message;
140 }
141
142 uint64 param_seed = 0x12345678; // real value is passed via parseCustomOptions function
143
path_join(const std::string & prefix,const std::string & subpath)144 static std::string path_join(const std::string& prefix, const std::string& subpath)
145 {
146 CV_Assert(subpath.empty() || subpath[0] != '/');
147 if (prefix.empty())
148 return subpath;
149 bool skipSlash = prefix.size() > 0 ? (prefix[prefix.size()-1] == '/' || prefix[prefix.size()-1] == '\\') : false;
150 std::string path = prefix + (skipSlash ? "" : "/") + subpath;
151 return path;
152 }
153
154
155
156 /*****************************************************************************************\
157 * Exception and memory handlers *
158 \*****************************************************************************************/
159
160 // a few platform-dependent declarations
161
162 #if defined _WIN32
163 #ifdef _MSC_VER
SEHTranslator(unsigned int,EXCEPTION_POINTERS * pExp)164 static void SEHTranslator( unsigned int /*u*/, EXCEPTION_POINTERS* pExp )
165 {
166 TS::FailureCode code = TS::FAIL_EXCEPTION;
167 switch( pExp->ExceptionRecord->ExceptionCode )
168 {
169 case EXCEPTION_ACCESS_VIOLATION:
170 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
171 case EXCEPTION_DATATYPE_MISALIGNMENT:
172 case EXCEPTION_FLT_STACK_CHECK:
173 case EXCEPTION_STACK_OVERFLOW:
174 case EXCEPTION_IN_PAGE_ERROR:
175 code = TS::FAIL_MEMORY_EXCEPTION;
176 break;
177 case EXCEPTION_FLT_DENORMAL_OPERAND:
178 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
179 case EXCEPTION_FLT_INEXACT_RESULT:
180 case EXCEPTION_FLT_INVALID_OPERATION:
181 case EXCEPTION_FLT_OVERFLOW:
182 case EXCEPTION_FLT_UNDERFLOW:
183 case EXCEPTION_INT_DIVIDE_BY_ZERO:
184 case EXCEPTION_INT_OVERFLOW:
185 code = TS::FAIL_ARITHM_EXCEPTION;
186 break;
187 case EXCEPTION_BREAKPOINT:
188 case EXCEPTION_ILLEGAL_INSTRUCTION:
189 case EXCEPTION_INVALID_DISPOSITION:
190 case EXCEPTION_NONCONTINUABLE_EXCEPTION:
191 case EXCEPTION_PRIV_INSTRUCTION:
192 case EXCEPTION_SINGLE_STEP:
193 code = TS::FAIL_EXCEPTION;
194 }
195 throw code;
196 }
197 #endif
198
199 #else
200
201 static const int tsSigId[] = { SIGSEGV, SIGBUS, SIGFPE, SIGILL, SIGABRT, -1 };
202
203 static jmp_buf tsJmpMark;
204
signalHandler(int sig_code)205 static void signalHandler( int sig_code )
206 {
207 TS::FailureCode code = TS::FAIL_EXCEPTION;
208 switch( sig_code )
209 {
210 case SIGFPE:
211 code = TS::FAIL_ARITHM_EXCEPTION;
212 break;
213 case SIGSEGV:
214 case SIGBUS:
215 code = TS::FAIL_ARITHM_EXCEPTION;
216 break;
217 case SIGILL:
218 code = TS::FAIL_EXCEPTION;
219 }
220
221 longjmp( tsJmpMark, (int)code );
222 }
223
224 #endif
225
226
227 // reads 16-digit hexadecimal number (i.e. 64-bit integer)
readSeed(const char * str)228 int64 readSeed( const char* str )
229 {
230 int64 val = 0;
231 if( str && strlen(str) == 16 )
232 {
233 for( int i = 0; str[i]; i++ )
234 {
235 int c = tolower(str[i]);
236 if( !isxdigit(c) )
237 return 0;
238 val = val * 16 +
239 (str[i] < 'a' ? str[i] - '0' : str[i] - 'a' + 10);
240 }
241 }
242 return val;
243 }
244
245
246 /*****************************************************************************************\
247 * Base Class for Tests *
248 \*****************************************************************************************/
249
BaseTest()250 BaseTest::BaseTest()
251 {
252 ts = TS::ptr();
253 test_case_count = -1;
254 }
255
~BaseTest()256 BaseTest::~BaseTest()
257 {
258 clear();
259 }
260
clear()261 void BaseTest::clear()
262 {
263 }
264
265
find_param(const cv::FileStorage & fs,const char * param_name)266 cv::FileNode BaseTest::find_param( const cv::FileStorage& fs, const char* param_name )
267 {
268 cv::FileNode node = fs[get_name()];
269 return node[param_name];
270 }
271
272
read_params(const cv::FileStorage &)273 int BaseTest::read_params( const cv::FileStorage& )
274 {
275 return 0;
276 }
277
278
can_do_fast_forward()279 bool BaseTest::can_do_fast_forward()
280 {
281 return true;
282 }
283
284
safe_run(int start_from)285 void BaseTest::safe_run( int start_from )
286 {
287 CV_TRACE_FUNCTION();
288 ts->update_context( 0, -1, true );
289 ts->update_context( this, -1, true );
290
291 if( !::testing::GTEST_FLAG(catch_exceptions) )
292 run( start_from );
293 else
294 {
295 try
296 {
297 #if !defined _WIN32
298 int _code = setjmp( tsJmpMark );
299 if( !_code )
300 run( start_from );
301 else
302 throw TS::FailureCode(_code);
303 #else
304 run( start_from );
305 #endif
306 }
307 catch (const cv::Exception& exc)
308 {
309 const char* errorStr = cvErrorStr(exc.code);
310 char buf[1 << 16];
311
312 const char* delim = exc.err.find('\n') == cv::String::npos ? "" : "\n";
313 sprintf( buf, "OpenCV Error:\n\t%s (%s%s) in %s, file %s, line %d",
314 errorStr, delim, exc.err.c_str(), exc.func.size() > 0 ?
315 exc.func.c_str() : "unknown function", exc.file.c_str(), exc.line );
316 ts->printf(TS::LOG, "%s\n", buf);
317
318 ts->set_failed_test_info( TS::FAIL_ERROR_IN_CALLED_FUNC );
319 }
320 catch (const TS::FailureCode& fc)
321 {
322 std::string errorStr = TS::str_from_code(fc);
323 ts->printf(TS::LOG, "General failure:\n\t%s (%d)\n", errorStr.c_str(), fc);
324
325 ts->set_failed_test_info( fc );
326 }
327 catch (...)
328 {
329 ts->printf(TS::LOG, "Unknown failure\n");
330
331 ts->set_failed_test_info( TS::FAIL_EXCEPTION );
332 }
333 }
334
335 ts->set_gtest_status();
336 }
337
338
run(int start_from)339 void BaseTest::run( int start_from )
340 {
341 int test_case_idx, count = get_test_case_count();
342 int64 t_start = cvGetTickCount();
343 double freq = cv::getTickFrequency();
344 bool ff = can_do_fast_forward();
345 int progress = 0, code;
346 int64 t1 = t_start;
347
348 for( test_case_idx = ff && start_from >= 0 ? start_from : 0;
349 count < 0 || test_case_idx < count; test_case_idx++ )
350 {
351 ts->update_context( this, test_case_idx, ff );
352 progress = update_progress( progress, test_case_idx, count, (double)(t1 - t_start)/(freq*1000) );
353
354 code = prepare_test_case( test_case_idx );
355 if( code < 0 || ts->get_err_code() < 0 )
356 return;
357
358 if( code == 0 )
359 continue;
360
361 run_func();
362
363 if( ts->get_err_code() < 0 )
364 return;
365
366 if( validate_test_results( test_case_idx ) < 0 || ts->get_err_code() < 0 )
367 {
368 std::stringstream ss;
369 dump_test_case(test_case_idx, &ss);
370 std::string s = ss.str();
371 ts->printf( TS::LOG, "%s", s.c_str());
372 return;
373 }
374 }
375 }
376
377
run_func(void)378 void BaseTest::run_func(void)
379 {
380 assert(0);
381 }
382
383
get_test_case_count(void)384 int BaseTest::get_test_case_count(void)
385 {
386 return test_case_count;
387 }
388
389
prepare_test_case(int)390 int BaseTest::prepare_test_case( int )
391 {
392 return 0;
393 }
394
395
validate_test_results(int)396 int BaseTest::validate_test_results( int )
397 {
398 return 0;
399 }
400
401
update_progress(int progress,int test_case_idx,int count,double dt)402 int BaseTest::update_progress( int progress, int test_case_idx, int count, double dt )
403 {
404 int width = 60 - (int)get_name().size();
405 if( count > 0 )
406 {
407 int t = cvRound( ((double)test_case_idx * width)/count );
408 if( t > progress )
409 {
410 ts->printf( TS::CONSOLE, "." );
411 progress = t;
412 }
413 }
414 else if( cvRound(dt) > progress )
415 {
416 ts->printf( TS::CONSOLE, "." );
417 progress = cvRound(dt);
418 }
419
420 return progress;
421 }
422
423
dump_test_case(int test_case_idx,std::ostream * out)424 void BaseTest::dump_test_case(int test_case_idx, std::ostream* out)
425 {
426 *out << "test_case_idx = " << test_case_idx << std::endl;
427 }
428
429
BadArgTest()430 BadArgTest::BadArgTest()
431 {
432 test_case_idx = -1;
433 // oldErrorCbk = 0;
434 // oldErrorCbkData = 0;
435 }
436
~BadArgTest(void)437 BadArgTest::~BadArgTest(void)
438 {
439 }
440
run_test_case(int expected_code,const string & _descr)441 int BadArgTest::run_test_case( int expected_code, const string& _descr )
442 {
443 int errcount = 0;
444 bool thrown = false;
445 const char* descr = _descr.c_str() ? _descr.c_str() : "";
446
447 try
448 {
449 run_func();
450 }
451 catch(const cv::Exception& e)
452 {
453 thrown = true;
454 if (e.code != expected_code &&
455 e.code != cv::Error::StsError && e.code != cv::Error::StsAssert // Exact error codes support will be dropped. Checks should provide proper text messages instead.
456 )
457 {
458 ts->printf(TS::LOG, "%s (test case #%d): the error code %d is different from the expected %d\n",
459 descr, test_case_idx, e.code, expected_code);
460 errcount = 1;
461 }
462 }
463 catch(...)
464 {
465 thrown = true;
466 ts->printf(TS::LOG, "%s (test case #%d): unknown exception was thrown (the function has likely crashed)\n",
467 descr, test_case_idx);
468 errcount = 1;
469 }
470
471 if(!thrown)
472 {
473 ts->printf(TS::LOG, "%s (test case #%d): no expected exception was thrown\n",
474 descr, test_case_idx);
475 errcount = 1;
476 }
477 test_case_idx++;
478
479 return errcount;
480 }
481
482 /*****************************************************************************************\
483 * Base Class for Test System *
484 \*****************************************************************************************/
485
486 /******************************** Constructors/Destructors ******************************/
487
TSParams()488 TSParams::TSParams()
489 {
490 rng_seed = (uint64)-1;
491 use_optimized = true;
492 test_case_count_scale = 1;
493 }
494
495
TestInfo()496 TestInfo::TestInfo()
497 {
498 test = 0;
499 code = 0;
500 rng_seed = rng_seed0 = 0;
501 test_case_idx = -1;
502 }
503
504
TS()505 TS::TS()
506 {
507 } // ctor
508
509
~TS()510 TS::~TS()
511 {
512 } // dtor
513
514
str_from_code(const TS::FailureCode code)515 string TS::str_from_code( const TS::FailureCode code )
516 {
517 switch( code )
518 {
519 case OK: return "Ok";
520 case FAIL_GENERIC: return "Generic/Unknown";
521 case FAIL_MISSING_TEST_DATA: return "No test data";
522 case FAIL_INVALID_TEST_DATA: return "Invalid test data";
523 case FAIL_ERROR_IN_CALLED_FUNC: return "cvError invoked";
524 case FAIL_EXCEPTION: return "Hardware/OS exception";
525 case FAIL_MEMORY_EXCEPTION: return "Invalid memory access";
526 case FAIL_ARITHM_EXCEPTION: return "Arithmetic exception";
527 case FAIL_MEMORY_CORRUPTION_BEGIN: return "Corrupted memblock (beginning)";
528 case FAIL_MEMORY_CORRUPTION_END: return "Corrupted memblock (end)";
529 case FAIL_MEMORY_LEAK: return "Memory leak";
530 case FAIL_INVALID_OUTPUT: return "Invalid function output";
531 case FAIL_MISMATCH: return "Unexpected output";
532 case FAIL_BAD_ACCURACY: return "Bad accuracy";
533 case FAIL_HANG: return "Infinite loop(?)";
534 case FAIL_BAD_ARG_CHECK: return "Incorrect handling of bad arguments";
535 default:
536 ;
537 }
538 return "Generic/Unknown";
539 }
540
tsErrorCallback(int status,const char * func_name,const char * err_msg,const char * file_name,int line,void * data)541 static int tsErrorCallback( int status, const char* func_name, const char* err_msg, const char* file_name, int line, void* data )
542 {
543 TS* ts = (TS*)data;
544 const char* delim = std::string(err_msg).find('\n') == std::string::npos ? "" : "\n";
545 ts->printf(TS::LOG, "OpenCV Error:\n\t%s (%s%s) in %s, file %s, line %d\n", cvErrorStr(status), delim, err_msg, func_name[0] != 0 ? func_name : "unknown function", file_name, line);
546 return 0;
547 }
548
549 /************************************** Running tests **********************************/
550
init(const string & modulename)551 void TS::init( const string& modulename )
552 {
553 data_search_subdir.push_back(modulename);
554 #ifndef WINRT
555 char* datapath_dir = getenv("OPENCV_TEST_DATA_PATH");
556 #else
557 char* datapath_dir = OPENCV_TEST_DATA_PATH;
558 #endif
559
560 if( datapath_dir )
561 {
562 data_path = path_join(path_join(datapath_dir, modulename), "");
563 }
564
565 cv::redirectError((cv::ErrorCallback)tsErrorCallback, this);
566
567 if( ::testing::GTEST_FLAG(catch_exceptions) )
568 {
569 #if defined _WIN32
570 #ifdef _MSC_VER
571 _set_se_translator( SEHTranslator );
572 #endif
573 #else
574 for( int i = 0; tsSigId[i] >= 0; i++ )
575 signal( tsSigId[i], signalHandler );
576 #endif
577 }
578 else
579 {
580 #if defined _WIN32
581 #ifdef _MSC_VER
582 _set_se_translator( 0 );
583 #endif
584 #else
585 for( int i = 0; tsSigId[i] >= 0; i++ )
586 signal( tsSigId[i], SIG_DFL );
587 #endif
588 }
589
590 if( params.use_optimized == 0 )
591 cv::setUseOptimized(false);
592
593 rng = RNG(params.rng_seed);
594 }
595
596
set_gtest_status()597 void TS::set_gtest_status()
598 {
599 TS::FailureCode code = get_err_code();
600 if( code >= 0 )
601 return SUCCEED();
602
603 char seedstr[32];
604 sprintf(seedstr, "%08x%08x", (unsigned)(current_test_info.rng_seed>>32),
605 (unsigned)(current_test_info.rng_seed));
606
607 string logs = "";
608 if( !output_buf[SUMMARY_IDX].empty() )
609 logs += "\n-----------------------------------\n\tSUM: " + output_buf[SUMMARY_IDX];
610 if( !output_buf[LOG_IDX].empty() )
611 logs += "\n-----------------------------------\n\tLOG:\n" + output_buf[LOG_IDX];
612 if( !output_buf[CONSOLE_IDX].empty() )
613 logs += "\n-----------------------------------\n\tCONSOLE: " + output_buf[CONSOLE_IDX];
614 logs += "\n-----------------------------------\n";
615
616 FAIL() << "\n\tfailure reason: " << str_from_code(code) <<
617 "\n\ttest case #" << current_test_info.test_case_idx <<
618 "\n\tseed: " << seedstr << logs;
619 }
620
621
update_context(BaseTest * test,int test_case_idx,bool update_ts_context)622 void TS::update_context( BaseTest* test, int test_case_idx, bool update_ts_context )
623 {
624 if( current_test_info.test != test )
625 {
626 for( int i = 0; i <= CONSOLE_IDX; i++ )
627 output_buf[i] = string();
628 rng = RNG(params.rng_seed);
629 current_test_info.rng_seed0 = current_test_info.rng_seed = rng.state;
630 }
631
632 current_test_info.test = test;
633 current_test_info.test_case_idx = test_case_idx;
634 current_test_info.code = 0;
635 cvSetErrStatus( CV_StsOk );
636 if( update_ts_context )
637 current_test_info.rng_seed = rng.state;
638 }
639
640
set_failed_test_info(int fail_code)641 void TS::set_failed_test_info( int fail_code )
642 {
643 if( current_test_info.code >= 0 )
644 current_test_info.code = TS::FailureCode(fail_code);
645 }
646
647 #if defined _MSC_VER && _MSC_VER < 1400
648 #undef vsnprintf
649 #define vsnprintf _vsnprintf
650 #endif
651
vprintf(int streams,const char * fmt,va_list l)652 void TS::vprintf( int streams, const char* fmt, va_list l )
653 {
654 char str[1 << 14];
655 vsnprintf( str, sizeof(str)-1, fmt, l );
656
657 for( int i = 0; i < MAX_IDX; i++ )
658 if( (streams & (1 << i)) )
659 {
660 output_buf[i] += std::string(str);
661 // in the new GTest-based framework we do not use
662 // any output files (except for the automatically generated xml report).
663 // if a test fails, all the buffers are printed, so we do not want to duplicate the information and
664 // thus only add the new information to a single buffer and return from the function.
665 break;
666 }
667 }
668
669
printf(int streams,const char * fmt,...)670 void TS::printf( int streams, const char* fmt, ... )
671 {
672 if( streams )
673 {
674 va_list l;
675 va_start( l, fmt );
676 vprintf( streams, fmt, l );
677 va_end( l );
678 }
679 }
680
681
ptr()682 TS* TS::ptr()
683 {
684 static TS ts;
685 return &ts;
686 }
687
fillGradient(Mat & img,int delta)688 void fillGradient(Mat& img, int delta)
689 {
690 const int ch = img.channels();
691 CV_Assert(!img.empty() && img.depth() == CV_8U && ch <= 4);
692
693 int n = 255 / delta;
694 int r, c, i;
695 for(r=0; r<img.rows; r++)
696 {
697 int kR = r % (2*n);
698 int valR = (kR<=n) ? delta*kR : delta*(2*n-kR);
699 for(c=0; c<img.cols; c++)
700 {
701 int kC = c % (2*n);
702 int valC = (kC<=n) ? delta*kC : delta*(2*n-kC);
703 uchar vals[] = {uchar(valR), uchar(valC), uchar(200*r/img.rows), uchar(255)};
704 uchar *p = img.ptr(r, c);
705 for(i=0; i<ch; i++) p[i] = vals[i];
706 }
707 }
708 }
709
smoothBorder(Mat & img,const Scalar & color,int delta)710 void smoothBorder(Mat& img, const Scalar& color, int delta)
711 {
712 const int ch = img.channels();
713 CV_Assert(!img.empty() && img.depth() == CV_8U && ch <= 4);
714
715 Scalar s;
716 uchar *p = NULL;
717 int n = 100/delta;
718 int nR = std::min(n, (img.rows+1)/2), nC = std::min(n, (img.cols+1)/2);
719
720 int r, c, i;
721 for(r=0; r<nR; r++)
722 {
723 double k1 = r*delta/100., k2 = 1-k1;
724 for(c=0; c<img.cols; c++)
725 {
726 p = img.ptr(r, c);
727 for(i=0; i<ch; i++) s[i] = p[i];
728 s = s * k1 + color * k2;
729 for(i=0; i<ch; i++) p[i] = uchar(s[i]);
730 }
731 for(c=0; c<img.cols; c++)
732 {
733 p = img.ptr(img.rows-r-1, c);
734 for(i=0; i<ch; i++) s[i] = p[i];
735 s = s * k1 + color * k2;
736 for(i=0; i<ch; i++) p[i] = uchar(s[i]);
737 }
738 }
739
740 for(r=0; r<img.rows; r++)
741 {
742 for(c=0; c<nC; c++)
743 {
744 double k1 = c*delta/100., k2 = 1-k1;
745 p = img.ptr(r, c);
746 for(i=0; i<ch; i++) s[i] = p[i];
747 s = s * k1 + color * k2;
748 for(i=0; i<ch; i++) p[i] = uchar(s[i]);
749 }
750 for(c=0; c<n; c++)
751 {
752 double k1 = c*delta/100., k2 = 1-k1;
753 p = img.ptr(r, img.cols-c-1);
754 for(i=0; i<ch; i++) s[i] = p[i];
755 s = s * k1 + color * k2;
756 for(i=0; i<ch; i++) p[i] = uchar(s[i]);
757 }
758 }
759 }
760
761
762 bool test_ipp_check = false;
763
checkIppStatus()764 void checkIppStatus()
765 {
766 if (test_ipp_check)
767 {
768 int status = cv::ipp::getIppStatus();
769 EXPECT_LE(0, status) << cv::ipp::getIppErrorLocation().c_str();
770 }
771 }
772
773 static bool checkTestData = cv::utils::getConfigurationParameterBool("OPENCV_TEST_REQUIRE_DATA", false);
774 bool skipUnstableTests = false;
775 bool runBigDataTests = false;
776 int testThreads = 0;
777 int debugLevel = (int)cv::utils::getConfigurationParameterSizeT("OPENCV_TEST_DEBUG", 0);
778
779
780 static size_t memory_usage_base = 0;
781 static uint64_t memory_usage_base_opencv = 0;
782 #ifdef HAVE_OPENCL
783 static uint64_t memory_usage_base_opencl = 0;
784 #endif
785
testSetUp()786 void testSetUp()
787 {
788 fflush(stdout); fflush(stderr);
789 cv::ipp::setIppStatus(0);
790 cv::theRNG().state = cvtest::param_seed;
791 cv::setNumThreads(cvtest::testThreads);
792 if (malloc_peak) // if memory profiler is available
793 {
794 malloc_reset_peak();
795 memory_usage_base = malloc_peak(); // equal to malloc_current()
796 }
797 {
798 cv::utils::AllocatorStatisticsInterface& ocv_stats = cv::getAllocatorStatistics();
799 ocv_stats.resetPeakUsage();
800 memory_usage_base_opencv = ocv_stats.getCurrentUsage();
801 }
802 #ifdef HAVE_OPENCL
803 {
804 cv::utils::AllocatorStatisticsInterface& ocl_stats = cv::ocl::getOpenCLAllocatorStatistics();
805 ocl_stats.resetPeakUsage();
806 memory_usage_base_opencl = ocl_stats.getCurrentUsage();
807 }
808 #endif
809 checkTestTags();
810 }
811
testTearDown()812 void testTearDown()
813 {
814 ::cvtest::checkIppStatus();
815 uint64_t memory_usage = 0;
816 uint64_t ocv_memory_usage = 0, ocv_peak = 0;
817 if (malloc_peak) // if memory profiler is available
818 {
819 size_t peak = malloc_peak();
820 memory_usage = peak - memory_usage_base;
821 if (peak > 0)
822 {
823 CV_LOG_INFO(NULL, "Memory_usage (malloc): " << memory_usage << " (base=" << memory_usage_base << ")");
824 }
825 }
826 {
827 // core/src/alloc.cpp: #define OPENCV_ALLOC_ENABLE_STATISTICS
828 // handle large buffers via fastAlloc()
829 // (not always accurate on heavy 3rdparty usage, like protobuf)
830 cv::utils::AllocatorStatisticsInterface& ocv_stats = cv::getAllocatorStatistics();
831 ocv_peak = ocv_stats.getPeakUsage();
832 ocv_memory_usage = ocv_peak - memory_usage_base_opencv;
833 if (ocv_peak)
834 {
835 CV_LOG_INFO(NULL, "Memory_usage (OpenCV): " << ocv_memory_usage << " (base=" << memory_usage_base_opencv << " current=" << ocv_stats.getCurrentUsage() << ")");
836 }
837 if (memory_usage == 0) // external profiler has higher priority (and accuracy)
838 memory_usage = ocv_memory_usage;
839 }
840 #ifdef HAVE_OPENCL
841 uint64_t ocl_memory_usage = 0, ocl_peak = 0;
842 {
843 cv::utils::AllocatorStatisticsInterface& ocl_stats = cv::ocl::getOpenCLAllocatorStatistics();
844 ocl_peak = ocl_stats.getPeakUsage();
845 ocl_memory_usage = ocl_peak - memory_usage_base_opencl;
846 if (ocl_memory_usage > 0)
847 {
848 CV_LOG_INFO(NULL, "Memory_usage (OpenCL): " << ocl_memory_usage << " (base=" << memory_usage_base_opencl << " current=" << ocl_stats.getCurrentUsage() << ")");
849 }
850 ::testing::Test::RecordProperty("ocl_memory_usage",
851 cv::format("%llu", (unsigned long long)ocl_memory_usage));
852 }
853 #else
854 uint64_t ocl_memory_usage = 0;
855 #endif
856 if (malloc_peak // external memory profiler is available
857 || ocv_peak > 0 // or enabled OpenCV builtin allocation statistics
858 )
859 {
860 CV_LOG_INFO(NULL, "Memory usage total: " << (memory_usage + ocl_memory_usage));
861 ::testing::Test::RecordProperty("memory_usage",
862 cv::format("%llu", (unsigned long long)memory_usage));
863 ::testing::Test::RecordProperty("total_memory_usage",
864 cv::format("%llu", (unsigned long long)(memory_usage + ocl_memory_usage)));
865 }
866 }
867
checkBigDataTests()868 bool checkBigDataTests()
869 {
870 if (!runBigDataTests)
871 {
872 testTagIncreaseSkipCount("skip_bigdata", true, true);
873 printf("[ SKIP ] BigData tests are disabled\n");
874 return false;
875 }
876 return true;
877 }
878
parseCustomOptions(int argc,char ** argv)879 void parseCustomOptions(int argc, char **argv)
880 {
881 const string command_line_keys = string(
882 "{ ipp test_ipp_check |false |check whether IPP works without failures }"
883 "{ test_seed |809564 |seed for random numbers generator }"
884 "{ test_threads |-1 |the number of worker threads, if parallel execution is enabled}"
885 "{ skip_unstable |false |skip unstable tests }"
886 "{ test_bigdata |false |run BigData tests (>=2Gb) }"
887 "{ test_debug | |0 - no debug (default), 1 - basic test debug information, >1 - extra debug information }"
888 "{ test_require_data |") + (checkTestData ? "true" : "false") + string("|fail on missing non-required test data instead of skip (env:OPENCV_TEST_REQUIRE_DATA)}"
889 CV_TEST_TAGS_PARAMS
890 "{ h help |false |print help info }"
891 );
892
893 cv::CommandLineParser parser(argc, argv, command_line_keys);
894 if (parser.get<bool>("help"))
895 {
896 std::cout << "\nAvailable options besides google test option: \n";
897 parser.printMessage();
898 }
899
900 test_ipp_check = parser.get<bool>("test_ipp_check");
901 if (!test_ipp_check)
902 #ifndef WINRT
903 test_ipp_check = getenv("OPENCV_IPP_CHECK") != NULL;
904 #else
905 test_ipp_check = false;
906 #endif
907
908 param_seed = parser.get<unsigned int>("test_seed");
909
910 testThreads = parser.get<int>("test_threads");
911
912 skipUnstableTests = parser.get<bool>("skip_unstable");
913 runBigDataTests = parser.get<bool>("test_bigdata");
914 if (parser.has("test_debug"))
915 {
916 cv::String s = parser.get<cv::String>("test_debug");
917 if (s.empty() || s == "true")
918 debugLevel = 1;
919 else
920 debugLevel = parser.get<int>("test_debug");
921 }
922 if (parser.has("test_require_data"))
923 checkTestData = parser.get<bool>("test_require_data");
924
925 activateTestTags(parser);
926 }
927
isDirectory(const std::string & path)928 static bool isDirectory(const std::string& path)
929 {
930 #if defined _WIN32 || defined WINCE
931 WIN32_FILE_ATTRIBUTE_DATA all_attrs;
932 #ifdef WINRT
933 wchar_t wpath[MAX_PATH];
934 size_t copied = mbstowcs(wpath, path.c_str(), MAX_PATH);
935 CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1));
936 BOOL status = ::GetFileAttributesExW(wpath, GetFileExInfoStandard, &all_attrs);
937 #else
938 BOOL status = ::GetFileAttributesExA(path.c_str(), GetFileExInfoStandard, &all_attrs);
939 #endif
940 DWORD attributes = all_attrs.dwFileAttributes;
941 return status && ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
942 #else
943 struct stat s;
944 if (0 != stat(path.c_str(), &s))
945 return false;
946 return S_ISDIR(s.st_mode);
947 #endif
948 }
949
addDataSearchPath(const std::string & path)950 void addDataSearchPath(const std::string& path)
951 {
952 if (isDirectory(path))
953 TS::ptr()->data_search_path.push_back(path);
954 }
addDataSearchSubDirectory(const std::string & subdir)955 void addDataSearchSubDirectory(const std::string& subdir)
956 {
957 TS::ptr()->data_search_subdir.push_back(subdir);
958 }
959
findData(const std::string & relative_path,bool required,bool findDirectory)960 static std::string findData(const std::string& relative_path, bool required, bool findDirectory)
961 {
962 #define CHECK_FILE_WITH_PREFIX(prefix, result) \
963 { \
964 result.clear(); \
965 std::string path = path_join(prefix, relative_path); \
966 /*printf("Trying %s\n", path.c_str());*/ \
967 if (findDirectory) \
968 { \
969 if (isDirectory(path)) \
970 result = path; \
971 } \
972 else \
973 { \
974 FILE* f = fopen(path.c_str(), "rb"); \
975 if(f) { \
976 fclose(f); \
977 result = path; \
978 } \
979 } \
980 }
981
982 #define TEST_TRY_FILE_WITH_PREFIX(prefix) \
983 { \
984 std::string result__; \
985 CHECK_FILE_WITH_PREFIX(prefix, result__); \
986 if (!result__.empty()) \
987 return result__; \
988 }
989
990
991 const std::vector<std::string>& search_path = TS::ptr()->data_search_path;
992 for(size_t i = search_path.size(); i > 0; i--)
993 {
994 const std::string& prefix = search_path[i - 1];
995 TEST_TRY_FILE_WITH_PREFIX(prefix);
996 }
997
998 const std::vector<std::string>& search_subdir = TS::ptr()->data_search_subdir;
999
1000 #ifndef WINRT
1001 char* datapath_dir = getenv("OPENCV_TEST_DATA_PATH");
1002 #else
1003 char* datapath_dir = OPENCV_TEST_DATA_PATH;
1004 #endif
1005
1006 std::string datapath;
1007 if (datapath_dir)
1008 {
1009 datapath = datapath_dir;
1010 //CV_Assert(isDirectory(datapath) && "OPENCV_TEST_DATA_PATH is specified but it doesn't exist");
1011 if (isDirectory(datapath))
1012 {
1013 for(size_t i = search_subdir.size(); i > 0; i--)
1014 {
1015 const std::string& subdir = search_subdir[i - 1];
1016 std::string prefix = path_join(datapath, subdir);
1017 std::string result_;
1018 CHECK_FILE_WITH_PREFIX(prefix, result_);
1019 if (!required && !result_.empty())
1020 {
1021 static bool checkOptionalFlag = cv::utils::getConfigurationParameterBool("OPENCV_TEST_CHECK_OPTIONAL_DATA", false);
1022 if (checkOptionalFlag)
1023 {
1024 std::cout << "TEST ERROR: Don't use 'optional' findData() for " << relative_path << std::endl;
1025 CV_Assert(required || result_.empty());
1026 }
1027 }
1028 if (!result_.empty())
1029 return result_;
1030 }
1031 }
1032 }
1033 #ifdef OPENCV_TEST_DATA_INSTALL_PATH
1034 datapath = path_join("./", OPENCV_TEST_DATA_INSTALL_PATH);
1035 if (isDirectory(datapath))
1036 {
1037 for(size_t i = search_subdir.size(); i > 0; i--)
1038 {
1039 const std::string& subdir = search_subdir[i - 1];
1040 std::string prefix = path_join(datapath, subdir);
1041 TEST_TRY_FILE_WITH_PREFIX(prefix);
1042 }
1043 }
1044 #ifdef OPENCV_INSTALL_PREFIX
1045 else
1046 {
1047 datapath = path_join(OPENCV_INSTALL_PREFIX, OPENCV_TEST_DATA_INSTALL_PATH);
1048 if (isDirectory(datapath))
1049 {
1050 for(size_t i = search_subdir.size(); i > 0; i--)
1051 {
1052 const std::string& subdir = search_subdir[i - 1];
1053 std::string prefix = path_join(datapath, subdir);
1054 TEST_TRY_FILE_WITH_PREFIX(prefix);
1055 }
1056 }
1057 }
1058 #endif
1059 #endif
1060 const char* type = findDirectory ? "directory" : "data file";
1061 if (required || checkTestData)
1062 CV_Error(cv::Error::StsError, cv::format("OpenCV tests: Can't find required %s: %s", type, relative_path.c_str()));
1063 throw SkipTestException(cv::format("OpenCV tests: Can't find %s: %s", type, relative_path.c_str()));
1064 }
1065
findDataFile(const std::string & relative_path,bool required)1066 std::string findDataFile(const std::string& relative_path, bool required)
1067 {
1068 return findData(relative_path, required, false);
1069 }
1070
findDataDirectory(const std::string & relative_path,bool required)1071 std::string findDataDirectory(const std::string& relative_path, bool required)
1072 {
1073 return findData(relative_path, required, true);
1074 }
1075
getSnippetFromConfig(const std::string & start,const std::string & end)1076 inline static std::string getSnippetFromConfig(const std::string & start, const std::string & end)
1077 {
1078 const std::string buildInfo = cv::getBuildInformation();
1079 size_t pos1 = buildInfo.find(start);
1080 if (pos1 != std::string::npos)
1081 {
1082 pos1 += start.length();
1083 pos1 = buildInfo.find_first_not_of(" \t\n\r", pos1);
1084 }
1085 size_t pos2 = buildInfo.find(end, pos1);
1086 if (pos2 != std::string::npos)
1087 {
1088 pos2 = buildInfo.find_last_not_of(" \t\n\r", pos2);
1089 }
1090 if (pos1 != std::string::npos && pos2 != std::string::npos && pos1 < pos2)
1091 {
1092 return buildInfo.substr(pos1, pos2 - pos1 + 1);
1093 }
1094 return std::string();
1095 }
1096
recordPropertyVerbose(const std::string & property,const std::string & msg,const std::string & value,const std::string & build_value=std::string ())1097 inline static void recordPropertyVerbose(const std::string & property,
1098 const std::string & msg,
1099 const std::string & value,
1100 const std::string & build_value = std::string())
1101 {
1102 ::testing::Test::RecordProperty(property, value);
1103 std::cout << msg << ": " << (value.empty() ? std::string("N/A") : value) << std::endl;
1104 if (!build_value.empty())
1105 {
1106 ::testing::Test::RecordProperty(property + "_build", build_value);
1107 if (build_value != value)
1108 std::cout << "WARNING: build value differs from runtime: " << build_value << endl;
1109 }
1110 }
1111
1112 #ifdef _DEBUG
1113 #define CV_TEST_BUILD_CONFIG "Debug"
1114 #else
1115 #define CV_TEST_BUILD_CONFIG "Release"
1116 #endif
1117
OnTestProgramStart(const testing::UnitTest &)1118 void SystemInfoCollector::OnTestProgramStart(const testing::UnitTest&)
1119 {
1120 std::cout << "CTEST_FULL_OUTPUT" << std::endl; // Tell CTest not to discard any output
1121 recordPropertyVerbose("cv_version", "OpenCV version", cv::getVersionString(), CV_VERSION);
1122 recordPropertyVerbose("cv_vcs_version", "OpenCV VCS version", getSnippetFromConfig("Version control:", "\n"));
1123 recordPropertyVerbose("cv_build_type", "Build type", getSnippetFromConfig("Configuration:", "\n"), CV_TEST_BUILD_CONFIG);
1124 recordPropertyVerbose("cv_compiler", "Compiler", getSnippetFromConfig("C++ Compiler:", "\n"));
1125 const char* parallelFramework = cv::currentParallelFramework();
1126 if (parallelFramework)
1127 {
1128 ::testing::Test::RecordProperty("cv_parallel_framework", parallelFramework);
1129 int threads = testThreads > 0 ? testThreads : cv::getNumThreads();
1130 ::testing::Test::RecordProperty("cv_parallel_threads", threads);
1131 std::cout << "Parallel framework: " << parallelFramework << " (nthreads=" << threads << ")" << std::endl;
1132 }
1133 recordPropertyVerbose("cv_cpu_features", "CPU features", cv::getCPUFeaturesLine());
1134 #ifdef HAVE_IPP
1135 recordPropertyVerbose("cv_ipp_version", "Intel(R) IPP version", cv::ipp::useIPP() ? cv::ipp::getIppVersion() : "disabled");
1136 if (cv::ipp::useIPP())
1137 recordPropertyVerbose("cv_ipp_features", "Intel(R) IPP features code", cv::format("0x%llx", cv::ipp::getIppTopFeatures()));
1138 #endif
1139 #ifdef HAVE_OPENCL
1140 cv::dumpOpenCLInformation();
1141 #endif
1142 }
1143
1144 } //namespace cvtest
1145
1146 /* End of file. */
1147