get_filename_component(OPENCV_TEST_DATA_PATH ${OPENCV_TEST_DATA_PATH} ABSOLUTE)
endif()
-if(OPENCV_TEST_DATA_PATH AND NOT OPENCV_TEST_DATA_INSTALL_PATH)
- if(ANDROID)
- ocv_update(OPENCV_TEST_DATA_INSTALL_PATH "sdk/etc/testdata")
- elseif(WIN32)
- ocv_update(OPENCV_TEST_DATA_INSTALL_PATH "testdata")
- else()
- ocv_update(OPENCV_TEST_DATA_INSTALL_PATH "share/OpenCV/testdata")
- endif()
+if(ANDROID)
+ ocv_update(OPENCV_TEST_DATA_INSTALL_PATH "sdk/etc/testdata")
+elseif(WIN32)
+ ocv_update(OPENCV_TEST_DATA_INSTALL_PATH "testdata")
+else()
+ ocv_update(OPENCV_TEST_DATA_INSTALL_PATH "share/OpenCV/testdata")
endif()
if(ANDROID)
endif()
endif()
endfunction()
+
+macro(ocv_add_testdata basedir dest_subdir)
+ if(BUILD_TESTS)
+ cmake_parse_arguments(__TESTDATA "" "COMPONENT" "" ${ARGN})
+ if(NOT CMAKE_CROSSCOMPILING AND NOT INSTALL_TESTS)
+ file(COPY ${basedir}/
+ DESTINATION ${CMAKE_BINARY_DIR}/${OPENCV_TEST_DATA_INSTALL_PATH}/${dest_subdir}
+ ${__TESTDATA_UNPARSED_ARGUMENTS}
+ )
+ endif()
+ if(INSTALL_TESTS)
+ install(DIRECTORY ${basedir}/
+ DESTINATION ${OPENCV_TEST_DATA_INSTALL_PATH}/contrib/text
+ ${ARGN}
+ )
+ endif()
+ endif()
+endmacro()
ocv_glob_module_sources()
ocv_module_include_directories()
ocv_create_module()
+
+# generate config file
+set(OPENCV_TESTS_CONFIG_FILE "${CMAKE_BINARY_DIR}/opencv_tests_config.hpp")
+set(OPENCV_TESTS_CONFIG_STR "")
+if(CMAKE_INSTALL_PREFIX)
+ set(OPENCV_TESTS_CONFIG_STR "${OPENCV_TESTS_CONFIG_STR}
+#define OPENCV_INSTALL_PREFIX \"${CMAKE_INSTALL_PREFIX}\"
+")
+endif()
+if(OPENCV_TEST_DATA_INSTALL_PATH)
+ set(OPENCV_TESTS_CONFIG_STR "${OPENCV_TESTS_CONFIG_STR}
+#define OPENCV_TEST_DATA_INSTALL_PATH \"${OPENCV_TEST_DATA_INSTALL_PATH}\"
+")
+endif()
+if(EXISTS "${OPENCV_TESTS_CONFIG_FILE}")
+ file(READ "${OPENCV_TESTS_CONFIG_FILE}" __content)
+endif()
+if(NOT OPENCV_TESTS_CONFIG_STR STREQUAL "${__content}")
+ file(WRITE "${OPENCV_TESTS_CONFIG_FILE}" "${OPENCV_TESTS_CONFIG_STR}")
+endif()
-#ifndef OPENCV_GTESTCV_HPP
-#define OPENCV_GTESTCV_HPP
+#ifndef OPENCV_TS_HPP
+#define OPENCV_TS_HPP
#include "opencv2/core/cvdef.h"
#include <stdarg.h> // for va_list
using cv::InputArray;
using cv::noArray;
+class SkipTestException: public cv::Exception
+{
+public:
+ int dummy; // workaround for MacOSX Xcode 7.3 bug (don't make class "empty")
+ SkipTestException() : dummy(0) {}
+ SkipTestException(const cv::String& message) : dummy(0) { this->msg = message; }
+};
+
class CV_EXPORTS TS;
CV_EXPORTS int64 readSeed(const char* str);
// returns textual description of failure code
static string str_from_code( const TS::FailureCode code );
+ std::vector<std::string> data_search_path;
+ std::vector<std::string> data_search_subdir;
protected:
// these are allocated within a test to try keep them valid in case of stack corruption
DefaultRngAuto& operator=(const DefaultRngAuto&);
};
-}
-
-namespace cvtest
-{
// test images generation functions
CV_EXPORTS void fillGradient(Mat& img, int delta = 5);
CV_EXPORTS void smoothBorder(Mat& img, const Scalar& color, int delta = 3);
CV_EXPORTS void printVersionInfo(bool useStdOut = true);
-} //namespace cvtest
+
+
+// Utility functions
+
+CV_EXPORTS void addDataSearchPath(const std::string& path);
+CV_EXPORTS void addDataSearchSubDirectory(const std::string& subdir);
+
+/*! @brief Try to find requested data file
+
+ Search directories:
+
+ 0. TS::data_search_path (search sub-directories are not used)
+ 1. OPENCV_TEST_DATA_PATH environment variable
+ 2. One of these:
+ a. OpenCV testdata based on build location: "./" + "share/OpenCV/testdata"
+ b. OpenCV testdata at install location: CMAKE_INSTALL_PREFIX + "share/OpenCV/testdata"
+
+ Search sub-directories:
+
+ - addDataSearchSubDirectory()
+ - modulename from TS::init()
+
+ */
+CV_EXPORTS std::string findDataFile(const std::string& relative_path, bool required = true);
+
#ifndef __CV_TEST_EXEC_ARGS
#if defined(_MSC_VER) && (_MSC_VER <= 1400)
#endif
#ifdef HAVE_OPENCL
-namespace cvtest { namespace ocl {
+namespace ocl {
void dumpOpenCLDevice();
-} }
+}
#define TEST_DUMP_OCL_INFO cvtest::ocl::dumpOpenCLDevice();
#else
#define TEST_DUMP_OCL_INFO
#define CV_TEST_MAIN(resourcesubdir, ...) \
int main(int argc, char **argv) \
{ \
- __CV_TEST_EXEC_ARGS(__VA_ARGS__) \
- cvtest::TS::ptr()->init(resourcesubdir); \
+ using namespace cvtest; \
+ TS* ts = TS::ptr(); \
+ ts->init(resourcesubdir); \
::testing::InitGoogleTest(&argc, argv); \
cvtest::printVersionInfo(); \
TEST_DUMP_OCL_INFO \
+ __CV_TEST_EXEC_ARGS(__VA_ARGS__) \
parseCustomOptions(argc, argv); \
return RUN_ALL_TESTS(); \
}
FAIL() << "No equivalent implementation."; \
} while (0)
-#endif
+} //namespace cvtest
+
+#endif // OPENCV_TS_HPP
#include "opencv2/ts/ts_perf.hpp"
#ifndef OPENCV_TS_EXT_HPP
#define OPENCV_TS_EXT_HPP
+namespace cvtest {
void checkIppStatus();
+}
+
+#define CV_TEST_INIT cv::ipp::setIppStatus(0);
+#define CV_TEST_CLEANUP ::cvtest::checkIppStatus();
+#define CV_TEST_BODY_IMPL \
+ { \
+ try { \
+ CV_TEST_INIT \
+ Body(); \
+ CV_TEST_CLEANUP \
+ } \
+ catch (cvtest::SkipTestException& e) \
+ { \
+ printf("[ SKIP ] %s\n", e.what()); \
+ } \
+ } \
+
#undef TEST
#define TEST(test_case_name, test_name) \
::testing::Test::TearDownTestCase, \
new ::testing::internal::TestFactoryImpl<\
GTEST_TEST_CLASS_NAME_(test_case_name, test_name)>);\
- void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::TestBody() { cv::ipp::setIppStatus(0); Body(); checkIppStatus(); } \
+ void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::TestBody() CV_TEST_BODY_IMPL \
void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::Body()
#undef TEST_F
test_fixture::TearDownTestCase, \
new ::testing::internal::TestFactoryImpl<\
GTEST_TEST_CLASS_NAME_(test_fixture, test_name)>);\
- void GTEST_TEST_CLASS_NAME_(test_fixture, test_name)::TestBody() { cv::ipp::setIppStatus(0); Body(); checkIppStatus(); } \
+ void GTEST_TEST_CLASS_NAME_(test_fixture, test_name)::TestBody() CV_TEST_BODY_IMPL \
void GTEST_TEST_CLASS_NAME_(test_fixture, test_name)::Body()
#undef TEST_P
int GTEST_TEST_CLASS_NAME_(test_case_name, \
test_name)::gtest_registering_dummy_ = \
GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::AddToRegistry(); \
- void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::TestBody() { cv::ipp::setIppStatus(0); Body(); checkIppStatus(); } \
+ void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::TestBody() CV_TEST_BODY_IMPL \
void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::Body()
#endif // OPENCV_TS_EXT_HPP
#include <setjmp.h>
#endif
+// isDirectory
+#if defined WIN32 || defined _WIN32 || defined WINCE
+# include <windows.h>
+#else
+# include <dirent.h>
+# include <sys/stat.h>
+#endif
+
+
+#include "opencv_tests_config.hpp"
+
namespace cvtest
{
+static std::string path_join(const std::string& prefix, const std::string& subpath)
+{
+ CV_Assert(subpath.empty() || subpath[0] != '/');
+ if (prefix.empty())
+ return subpath;
+ bool skipSlash = prefix.size() > 0 ? (prefix[prefix.size()-1] == '/' || prefix[prefix.size()-1] == '\\') : false;
+ std::string path = prefix + (skipSlash ? "" : "/") + subpath;
+ return path;
+}
+
+
+
/*****************************************************************************************\
* Exception and memory handlers *
\*****************************************************************************************/
void TS::init( const string& modulename )
{
+ data_search_subdir.push_back(modulename);
#ifndef WINRT
char* datapath_dir = getenv("OPENCV_TEST_DATA_PATH");
#else
if( datapath_dir )
{
- char buf[1024];
- size_t l = strlen(datapath_dir);
- bool haveSlash = l > 0 && (datapath_dir[l-1] == '/' || datapath_dir[l-1] == '\\');
- sprintf( buf, "%s%s%s/", datapath_dir, haveSlash ? "" : "/", modulename.c_str() );
- data_path = string(buf);
+ data_path = path_join(path_join(datapath_dir, modulename), "");
}
cv::redirectError((cv::ErrorCallback)tsErrorCallback, this);
}
-TS ts;
+static TS ts;
TS* TS::ptr() { return &ts; }
void fillGradient(Mat& img, int delta)
}
}
-} //namespace cvtest
bool test_ipp_check = false;
#endif
}
+
+static bool isDirectory(const std::string& path)
+{
+#if defined WIN32 || defined _WIN32 || defined WINCE
+ WIN32_FILE_ATTRIBUTE_DATA all_attrs;
+#ifdef WINRT
+ wchar_t wpath[MAX_PATH];
+ size_t copied = mbstowcs(wpath, path.c_str(), MAX_PATH);
+ CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1));
+ BOOL status = ::GetFileAttributesExW(wpath, GetFileExInfoStandard, &all_attrs);
+#else
+ BOOL status = ::GetFileAttributesExA(path.c_str(), GetFileExInfoStandard, &all_attrs);
+#endif
+ DWORD attributes = all_attrs.dwFileAttributes;
+ return status && ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
+#else
+ struct stat s;
+ if (0 != stat(path.c_str(), &s))
+ return false;
+ return S_ISDIR(s.st_mode);
+#endif
+}
+
+CV_EXPORTS void addDataSearchPath(const std::string& path)
+{
+ if (isDirectory(path))
+ TS::ptr()->data_search_path.push_back(path);
+}
+CV_EXPORTS void addDataSearchSubDirectory(const std::string& subdir)
+{
+ TS::ptr()->data_search_subdir.push_back(subdir);
+}
+
+std::string findDataFile(const std::string& relative_path, bool required)
+{
+#define TEST_TRY_FILE_WITH_PREFIX(prefix) \
+{ \
+ std::string path = path_join(prefix, relative_path); \
+ /*printf("Trying %s\n", path.c_str());*/ \
+ FILE* f = fopen(path.c_str(), "rb"); \
+ if(f) { \
+ fclose(f); \
+ return path; \
+ } \
+}
+
+ const std::vector<std::string>& search_path = TS::ptr()->data_search_path;
+ for(size_t i = search_path.size(); i > 0; i--)
+ {
+ const std::string& prefix = search_path[i - 1];
+ TEST_TRY_FILE_WITH_PREFIX(prefix);
+ }
+
+ const std::vector<std::string>& search_subdir = TS::ptr()->data_search_subdir;
+
+#ifndef WINRT
+ char* datapath_dir = getenv("OPENCV_TEST_DATA_PATH");
+#else
+ char* datapath_dir = OPENCV_TEST_DATA_PATH;
+#endif
+
+ std::string datapath;
+ if (datapath_dir)
+ {
+ datapath = datapath_dir;
+ //CV_Assert(isDirectory(datapath) && "OPENCV_TEST_DATA_PATH is specified but it doesn't exist");
+ if (isDirectory(datapath))
+ {
+ for(size_t i = search_subdir.size(); i > 0; i--)
+ {
+ const std::string& subdir = search_subdir[i - 1];
+ std::string prefix = path_join(datapath, subdir);
+ TEST_TRY_FILE_WITH_PREFIX(prefix);
+ }
+ }
+ }
+#ifdef OPENCV_TEST_DATA_INSTALL_PATH
+ datapath = path_join("./", OPENCV_TEST_DATA_INSTALL_PATH);
+ if (isDirectory(datapath))
+ {
+ for(size_t i = search_subdir.size(); i > 0; i--)
+ {
+ const std::string& subdir = search_subdir[i - 1];
+ std::string prefix = path_join(datapath, subdir);
+ TEST_TRY_FILE_WITH_PREFIX(prefix);
+ }
+ }
+#ifdef OPENCV_INSTALL_PREFIX
+ else
+ {
+ datapath = path_join(OPENCV_INSTALL_PREFIX, OPENCV_TEST_DATA_INSTALL_PATH);
+ if (isDirectory(datapath))
+ {
+ for(size_t i = search_subdir.size(); i > 0; i--)
+ {
+ const std::string& subdir = search_subdir[i - 1];
+ std::string prefix = path_join(datapath, subdir);
+ TEST_TRY_FILE_WITH_PREFIX(prefix);
+ }
+ }
+ }
+#endif
+#endif
+ if (required)
+ CV_ErrorNoReturn(cv::Error::StsError, cv::format("OpenCV tests: Can't find required data file: %s", relative_path.c_str()));
+ throw SkipTestException(cv::format("OpenCV tests: Can't find data file: %s", relative_path.c_str()));
+}
+
+
+} //namespace cvtest
+
/* End of file. */
# include <sys/time.h>
#endif
+using namespace cvtest;
using namespace perf;
int64 TestBase::timeLimitDefault = 0;
#ifdef ENABLE_INSTRUMENTATION
static int param_instrument;
#endif
+
+namespace cvtest {
extern bool test_ipp_check;
+}
#ifdef HAVE_CUDA
static int param_cuda_device;