1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
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.
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.
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.
42 #include "precomp.hpp"
43 #include <opencv2/core/utils/configuration.private.hpp>
45 #include "opencv2/core/core_c.h"
72 #if defined _WIN32 || defined WINCE
76 # include <sys/stat.h>
81 #define DUMP_CONFIG_PROPERTY(propertyName, propertyValue) \
83 std::stringstream ssName, ssValue;\
84 ssName << propertyName;\
85 ssValue << (propertyValue); \
86 ::testing::Test::RecordProperty(ssName.str(), ssValue.str()); \
89 #define DUMP_MESSAGE_STDOUT(msg) \
91 std::cout << msg << std::endl; \
94 #include "opencv2/core/opencl/opencl_info.hpp"
96 #include "opencv2/core/utils/allocator_stats.hpp"
97 namespace cv { namespace ocl {
98 cv::utils::AllocatorStatisticsInterface& getOpenCLAllocatorStatistics();
100 #endif // HAVE_OPENCL
102 #include "opencv2/core/utils/allocator_stats.hpp"
104 CV_EXPORTS cv::utils::AllocatorStatisticsInterface& getAllocatorStatistics();
107 #include "opencv_tests_config.hpp"
109 #include "ts_tags.hpp"
111 #if defined(__GNUC__) && defined(__linux__)
113 size_t malloc_peak(void) __attribute__((weak));
114 void malloc_reset_peak(void) __attribute__((weak));
117 static size_t (*malloc_peak)(void) = 0;
118 static void (*malloc_reset_peak)(void) = 0;
121 namespace opencv_test {
122 bool required_opencv_test_namespace = false; // compilation check for non-refactored tests
128 details::SkipTestExceptionBase::SkipTestExceptionBase(bool handlingTags)
132 testTagIncreaseSkipCount("skip_other", true, true);
135 details::SkipTestExceptionBase::SkipTestExceptionBase(const cv::String& message, bool handlingTags)
138 testTagIncreaseSkipCount("skip_other", true, true);
142 uint64 param_seed = 0x12345678; // real value is passed via parseCustomOptions function
144 static std::string path_join(const std::string& prefix, const std::string& subpath)
146 CV_Assert(subpath.empty() || subpath[0] != '/');
149 bool skipSlash = prefix.size() > 0 ? (prefix[prefix.size()-1] == '/' || prefix[prefix.size()-1] == '\\') : false;
150 std::string path = prefix + (skipSlash ? "" : "/") + subpath;
156 /*****************************************************************************************\
157 * Exception and memory handlers *
158 \*****************************************************************************************/
160 // a few platform-dependent declarations
164 static void SEHTranslator( unsigned int /*u*/, EXCEPTION_POINTERS* pExp )
166 TS::FailureCode code = TS::FAIL_EXCEPTION;
167 switch( pExp->ExceptionRecord->ExceptionCode )
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;
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;
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;
201 static const int tsSigId[] = { SIGSEGV, SIGBUS, SIGFPE, SIGILL, SIGABRT, -1 };
203 static jmp_buf tsJmpMark;
205 static void signalHandler( int sig_code )
207 TS::FailureCode code = TS::FAIL_EXCEPTION;
211 code = TS::FAIL_ARITHM_EXCEPTION;
215 code = TS::FAIL_ARITHM_EXCEPTION;
218 code = TS::FAIL_EXCEPTION;
221 longjmp( tsJmpMark, (int)code );
227 // reads 16-digit hexadecimal number (i.e. 64-bit integer)
228 int64 readSeed( const char* str )
231 if( str && strlen(str) == 16 )
233 for( int i = 0; str[i]; i++ )
235 int c = tolower(str[i]);
239 (str[i] < 'a' ? str[i] - '0' : str[i] - 'a' + 10);
246 /*****************************************************************************************\
247 * Base Class for Tests *
248 \*****************************************************************************************/
253 test_case_count = -1;
256 BaseTest::~BaseTest()
261 void BaseTest::clear()
266 cv::FileNode BaseTest::find_param( const cv::FileStorage& fs, const char* param_name )
268 cv::FileNode node = fs[get_name()];
269 return node[param_name];
273 int BaseTest::read_params( const cv::FileStorage& )
279 bool BaseTest::can_do_fast_forward()
285 void BaseTest::safe_run( int start_from )
288 ts->update_context( 0, -1, true );
289 ts->update_context( this, -1, true );
291 if( !::testing::GTEST_FLAG(catch_exceptions) )
298 int _code = setjmp( tsJmpMark );
302 throw TS::FailureCode(_code);
307 catch (const cv::Exception& exc)
309 const char* errorStr = cvErrorStr(exc.code);
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);
318 ts->set_failed_test_info( TS::FAIL_ERROR_IN_CALLED_FUNC );
320 catch (const TS::FailureCode& fc)
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);
325 ts->set_failed_test_info( fc );
329 ts->printf(TS::LOG, "Unknown failure\n");
331 ts->set_failed_test_info( TS::FAIL_EXCEPTION );
335 ts->set_gtest_status();
339 void BaseTest::run( int start_from )
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;
348 for( test_case_idx = ff && start_from >= 0 ? start_from : 0;
349 count < 0 || test_case_idx < count; test_case_idx++ )
351 ts->update_context( this, test_case_idx, ff );
352 progress = update_progress( progress, test_case_idx, count, (double)(t1 - t_start)/(freq*1000) );
354 code = prepare_test_case( test_case_idx );
355 if( code < 0 || ts->get_err_code() < 0 )
363 if( ts->get_err_code() < 0 )
366 if( validate_test_results( test_case_idx ) < 0 || ts->get_err_code() < 0 )
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());
378 void BaseTest::run_func(void)
384 int BaseTest::get_test_case_count(void)
386 return test_case_count;
390 int BaseTest::prepare_test_case( int )
396 int BaseTest::validate_test_results( int )
402 int BaseTest::update_progress( int progress, int test_case_idx, int count, double dt )
404 int width = 60 - (int)get_name().size();
407 int t = cvRound( ((double)test_case_idx * width)/count );
410 ts->printf( TS::CONSOLE, "." );
414 else if( cvRound(dt) > progress )
416 ts->printf( TS::CONSOLE, "." );
417 progress = cvRound(dt);
424 void BaseTest::dump_test_case(int test_case_idx, std::ostream* out)
426 *out << "test_case_idx = " << test_case_idx << std::endl;
430 BadArgTest::BadArgTest()
434 // oldErrorCbkData = 0;
437 BadArgTest::~BadArgTest(void)
441 int BadArgTest::run_test_case( int expected_code, const string& _descr )
445 const char* descr = _descr.c_str() ? _descr.c_str() : "";
451 catch(const cv::Exception& e)
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.
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);
466 ts->printf(TS::LOG, "%s (test case #%d): unknown exception was thrown (the function has likely crashed)\n",
467 descr, test_case_idx);
473 ts->printf(TS::LOG, "%s (test case #%d): no expected exception was thrown\n",
474 descr, test_case_idx);
482 /*****************************************************************************************\
483 * Base Class for Test System *
484 \*****************************************************************************************/
486 /******************************** Constructors/Destructors ******************************/
490 rng_seed = (uint64)-1;
491 use_optimized = true;
492 test_case_count_scale = 1;
500 rng_seed = rng_seed0 = 0;
515 string TS::str_from_code( const TS::FailureCode code )
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";
538 return "Generic/Unknown";
541 static int tsErrorCallback( int status, const char* func_name, const char* err_msg, const char* file_name, int line, void* 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);
549 /************************************** Running tests **********************************/
551 void TS::init( const string& modulename )
553 data_search_subdir.push_back(modulename);
555 char* datapath_dir = getenv("OPENCV_TEST_DATA_PATH");
557 char* datapath_dir = OPENCV_TEST_DATA_PATH;
562 data_path = path_join(path_join(datapath_dir, modulename), "");
565 cv::redirectError((cv::ErrorCallback)tsErrorCallback, this);
567 if( ::testing::GTEST_FLAG(catch_exceptions) )
571 _set_se_translator( SEHTranslator );
574 for( int i = 0; tsSigId[i] >= 0; i++ )
575 signal( tsSigId[i], signalHandler );
582 _set_se_translator( 0 );
585 for( int i = 0; tsSigId[i] >= 0; i++ )
586 signal( tsSigId[i], SIG_DFL );
590 if( params.use_optimized == 0 )
591 cv::setUseOptimized(false);
593 rng = RNG(params.rng_seed);
597 void TS::set_gtest_status()
599 TS::FailureCode code = get_err_code();
604 sprintf(seedstr, "%08x%08x", (unsigned)(current_test_info.rng_seed>>32),
605 (unsigned)(current_test_info.rng_seed));
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";
616 FAIL() << "\n\tfailure reason: " << str_from_code(code) <<
617 "\n\ttest case #" << current_test_info.test_case_idx <<
618 "\n\tseed: " << seedstr << logs;
622 void TS::update_context( BaseTest* test, int test_case_idx, bool update_ts_context )
624 if( current_test_info.test != test )
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;
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;
641 void TS::set_failed_test_info( int fail_code )
643 if( current_test_info.code >= 0 )
644 current_test_info.code = TS::FailureCode(fail_code);
647 #if defined _MSC_VER && _MSC_VER < 1400
649 #define vsnprintf _vsnprintf
652 void TS::vprintf( int streams, const char* fmt, va_list l )
655 vsnprintf( str, sizeof(str)-1, fmt, l );
657 for( int i = 0; i < MAX_IDX; i++ )
658 if( (streams & (1 << i)) )
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.
670 void TS::printf( int streams, const char* fmt, ... )
676 vprintf( streams, fmt, l );
688 void fillGradient(Mat& img, int delta)
690 const int ch = img.channels();
691 CV_Assert(!img.empty() && img.depth() == CV_8U && ch <= 4);
695 for(r=0; r<img.rows; r++)
698 int valR = (kR<=n) ? delta*kR : delta*(2*n-kR);
699 for(c=0; c<img.cols; c++)
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];
710 void smoothBorder(Mat& img, const Scalar& color, int delta)
712 const int ch = img.channels();
713 CV_Assert(!img.empty() && img.depth() == CV_8U && ch <= 4);
718 int nR = std::min(n, (img.rows+1)/2), nC = std::min(n, (img.cols+1)/2);
723 double k1 = r*delta/100., k2 = 1-k1;
724 for(c=0; c<img.cols; 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]);
731 for(c=0; c<img.cols; c++)
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]);
740 for(r=0; r<img.rows; r++)
744 double k1 = c*delta/100., k2 = 1-k1;
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]);
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]);
762 bool test_ipp_check = false;
764 void checkIppStatus()
768 int status = cv::ipp::getIppStatus();
769 EXPECT_LE(0, status) << cv::ipp::getIppErrorLocation().c_str();
773 static bool checkTestData = cv::utils::getConfigurationParameterBool("OPENCV_TEST_REQUIRE_DATA", false);
774 bool skipUnstableTests = false;
775 bool runBigDataTests = false;
777 int debugLevel = (int)cv::utils::getConfigurationParameterSizeT("OPENCV_TEST_DEBUG", 0);
780 static size_t memory_usage_base = 0;
781 static uint64_t memory_usage_base_opencv = 0;
783 static uint64_t memory_usage_base_opencl = 0;
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
795 memory_usage_base = malloc_peak(); // equal to malloc_current()
798 cv::utils::AllocatorStatisticsInterface& ocv_stats = cv::getAllocatorStatistics();
799 ocv_stats.resetPeakUsage();
800 memory_usage_base_opencv = ocv_stats.getCurrentUsage();
804 cv::utils::AllocatorStatisticsInterface& ocl_stats = cv::ocl::getOpenCLAllocatorStatistics();
805 ocl_stats.resetPeakUsage();
806 memory_usage_base_opencl = ocl_stats.getCurrentUsage();
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
819 size_t peak = malloc_peak();
820 memory_usage = peak - memory_usage_base;
823 CV_LOG_INFO(NULL, "Memory_usage (malloc): " << memory_usage << " (base=" << memory_usage_base << ")");
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;
835 CV_LOG_INFO(NULL, "Memory_usage (OpenCV): " << ocv_memory_usage << " (base=" << memory_usage_base_opencv << " current=" << ocv_stats.getCurrentUsage() << ")");
837 if (memory_usage == 0) // external profiler has higher priority (and accuracy)
838 memory_usage = ocv_memory_usage;
841 uint64_t ocl_memory_usage = 0, ocl_peak = 0;
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)
848 CV_LOG_INFO(NULL, "Memory_usage (OpenCL): " << ocl_memory_usage << " (base=" << memory_usage_base_opencl << " current=" << ocl_stats.getCurrentUsage() << ")");
850 ::testing::Test::RecordProperty("ocl_memory_usage",
851 cv::format("%llu", (unsigned long long)ocl_memory_usage));
854 uint64_t ocl_memory_usage = 0;
856 if (malloc_peak // external memory profiler is available
857 || ocv_peak > 0 // or enabled OpenCV builtin allocation statistics
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)));
868 bool checkBigDataTests()
870 if (!runBigDataTests)
872 testTagIncreaseSkipCount("skip_bigdata", true, true);
873 printf("[ SKIP ] BigData tests are disabled\n");
879 void parseCustomOptions(int argc, char **argv)
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)}"
890 "{ h help |false |print help info }"
893 cv::CommandLineParser parser(argc, argv, command_line_keys);
894 if (parser.get<bool>("help"))
896 std::cout << "\nAvailable options besides google test option: \n";
897 parser.printMessage();
900 test_ipp_check = parser.get<bool>("test_ipp_check");
903 test_ipp_check = getenv("OPENCV_IPP_CHECK") != NULL;
905 test_ipp_check = false;
908 param_seed = parser.get<unsigned int>("test_seed");
910 testThreads = parser.get<int>("test_threads");
912 skipUnstableTests = parser.get<bool>("skip_unstable");
913 runBigDataTests = parser.get<bool>("test_bigdata");
914 if (parser.has("test_debug"))
916 cv::String s = parser.get<cv::String>("test_debug");
917 if (s.empty() || s == "true")
920 debugLevel = parser.get<int>("test_debug");
922 if (parser.has("test_require_data"))
923 checkTestData = parser.get<bool>("test_require_data");
925 activateTestTags(parser);
928 static bool isDirectory(const std::string& path)
930 #if defined _WIN32 || defined WINCE
931 WIN32_FILE_ATTRIBUTE_DATA all_attrs;
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);
938 BOOL status = ::GetFileAttributesExA(path.c_str(), GetFileExInfoStandard, &all_attrs);
940 DWORD attributes = all_attrs.dwFileAttributes;
941 return status && ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
944 if (0 != stat(path.c_str(), &s))
946 return S_ISDIR(s.st_mode);
950 void addDataSearchPath(const std::string& path)
952 if (isDirectory(path))
953 TS::ptr()->data_search_path.push_back(path);
955 void addDataSearchSubDirectory(const std::string& subdir)
957 TS::ptr()->data_search_subdir.push_back(subdir);
960 static std::string findData(const std::string& relative_path, bool required, bool findDirectory)
962 #define CHECK_FILE_WITH_PREFIX(prefix, result) \
965 std::string path = path_join(prefix, relative_path); \
966 /*printf("Trying %s\n", path.c_str());*/ \
969 if (isDirectory(path)) \
974 FILE* f = fopen(path.c_str(), "rb"); \
982 #define TEST_TRY_FILE_WITH_PREFIX(prefix) \
984 std::string result__; \
985 CHECK_FILE_WITH_PREFIX(prefix, result__); \
986 if (!result__.empty()) \
991 const std::vector<std::string>& search_path = TS::ptr()->data_search_path;
992 for(size_t i = search_path.size(); i > 0; i--)
994 const std::string& prefix = search_path[i - 1];
995 TEST_TRY_FILE_WITH_PREFIX(prefix);
998 const std::vector<std::string>& search_subdir = TS::ptr()->data_search_subdir;
1001 char* datapath_dir = getenv("OPENCV_TEST_DATA_PATH");
1003 char* datapath_dir = OPENCV_TEST_DATA_PATH;
1006 std::string datapath;
1009 datapath = datapath_dir;
1010 //CV_Assert(isDirectory(datapath) && "OPENCV_TEST_DATA_PATH is specified but it doesn't exist");
1011 if (isDirectory(datapath))
1013 for(size_t i = search_subdir.size(); i > 0; i--)
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())
1021 static bool checkOptionalFlag = cv::utils::getConfigurationParameterBool("OPENCV_TEST_CHECK_OPTIONAL_DATA", false);
1022 if (checkOptionalFlag)
1024 std::cout << "TEST ERROR: Don't use 'optional' findData() for " << relative_path << std::endl;
1025 CV_Assert(required || result_.empty());
1028 if (!result_.empty())
1033 #ifdef OPENCV_TEST_DATA_INSTALL_PATH
1034 datapath = path_join("./", OPENCV_TEST_DATA_INSTALL_PATH);
1035 if (isDirectory(datapath))
1037 for(size_t i = search_subdir.size(); i > 0; i--)
1039 const std::string& subdir = search_subdir[i - 1];
1040 std::string prefix = path_join(datapath, subdir);
1041 TEST_TRY_FILE_WITH_PREFIX(prefix);
1044 #ifdef OPENCV_INSTALL_PREFIX
1047 datapath = path_join(OPENCV_INSTALL_PREFIX, OPENCV_TEST_DATA_INSTALL_PATH);
1048 if (isDirectory(datapath))
1050 for(size_t i = search_subdir.size(); i > 0; i--)
1052 const std::string& subdir = search_subdir[i - 1];
1053 std::string prefix = path_join(datapath, subdir);
1054 TEST_TRY_FILE_WITH_PREFIX(prefix);
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()));
1066 std::string findDataFile(const std::string& relative_path, bool required)
1068 return findData(relative_path, required, false);
1071 std::string findDataDirectory(const std::string& relative_path, bool required)
1073 return findData(relative_path, required, true);
1076 inline static std::string getSnippetFromConfig(const std::string & start, const std::string & end)
1078 const std::string buildInfo = cv::getBuildInformation();
1079 size_t pos1 = buildInfo.find(start);
1080 if (pos1 != std::string::npos)
1082 pos1 += start.length();
1083 pos1 = buildInfo.find_first_not_of(" \t\n\r", pos1);
1085 size_t pos2 = buildInfo.find(end, pos1);
1086 if (pos2 != std::string::npos)
1088 pos2 = buildInfo.find_last_not_of(" \t\n\r", pos2);
1090 if (pos1 != std::string::npos && pos2 != std::string::npos && pos1 < pos2)
1092 return buildInfo.substr(pos1, pos2 - pos1 + 1);
1094 return 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())
1102 ::testing::Test::RecordProperty(property, value);
1103 std::cout << msg << ": " << (value.empty() ? std::string("N/A") : value) << std::endl;
1104 if (!build_value.empty())
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;
1113 #define CV_TEST_BUILD_CONFIG "Debug"
1115 #define CV_TEST_BUILD_CONFIG "Release"
1118 void SystemInfoCollector::OnTestProgramStart(const testing::UnitTest&)
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)
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;
1133 recordPropertyVerbose("cv_cpu_features", "CPU features", cv::getCPUFeaturesLine());
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()));
1140 cv::dumpOpenCLInformation();
1144 } //namespace cvtest