1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html.
7 #include "opencv2/core/check.hpp"
11 const char* depthToString(int depth)
13 const char* s = detail::depthToString_(depth);
14 return s ? s : "<invalid depth>";
17 const cv::String typeToString(int type)
19 cv::String s = detail::typeToString_(type);
22 static cv::String invalidType("<invalid type>");
31 static const char* getTestOpPhraseStr(unsigned testOp)
33 static const char* _names[] = { "{custom check}", "equal to", "not equal to", "less than or equal to", "less than", "greater than or equal to", "greater than" };
34 CV_DbgAssert(testOp < CV__LAST_TEST_OP);
35 return testOp < CV__LAST_TEST_OP ? _names[testOp] : "???";
37 static const char* getTestOpMath(unsigned testOp)
39 static const char* _names[] = { "???", "==", "!=", "<=", "<", ">=", ">" };
40 CV_DbgAssert(testOp < CV__LAST_TEST_OP);
41 return testOp < CV__LAST_TEST_OP ? _names[testOp] : "???";
44 const char* depthToString_(int depth)
46 static const char* depthNames[] = { "CV_8U", "CV_8S", "CV_16U", "CV_16S", "CV_32S", "CV_32F", "CV_64F", "CV_USRTYPE1" };
47 return (depth <= CV_USRTYPE1 && depth >= 0) ? depthNames[depth] : NULL;
50 const cv::String typeToString_(int type)
52 int depth = CV_MAT_DEPTH(type);
53 int cn = CV_MAT_CN(type);
54 if (depth >= 0 && depth <= CV_USRTYPE1)
55 return cv::format("%sC%d", depthToString_(depth), cn);
59 template<typename T> static CV_NORETURN
60 void check_failed_auto_(const T& v1, const T& v2, const CheckContext& ctx)
63 ss << ctx.message << " (expected: '" << ctx.p1_str << " " << getTestOpMath(ctx.testOp) << " " << ctx.p2_str << "'), where" << std::endl
64 << " '" << ctx.p1_str << "' is " << v1 << std::endl;
65 if (ctx.testOp != TEST_CUSTOM && ctx.testOp < CV__LAST_TEST_OP)
67 ss << "must be " << getTestOpPhraseStr(ctx.testOp) << std::endl;
69 ss << " '" << ctx.p2_str << "' is " << v2;
70 cv::error(cv::Error::StsError, ss.str(), ctx.func, ctx.file, ctx.line);
72 void check_failed_MatDepth(const int v1, const int v2, const CheckContext& ctx)
75 ss << ctx.message << " (expected: '" << ctx.p1_str << " " << getTestOpMath(ctx.testOp) << " " << ctx.p2_str << "'), where" << std::endl
76 << " '" << ctx.p1_str << "' is " << v1 << " (" << depthToString(v1) << ")" << std::endl;
77 if (ctx.testOp != TEST_CUSTOM && ctx.testOp < CV__LAST_TEST_OP)
79 ss << "must be " << getTestOpPhraseStr(ctx.testOp) << std::endl;
81 ss << " '" << ctx.p2_str << "' is " << v2 << " (" << depthToString(v2) << ")";
82 cv::error(cv::Error::StsError, ss.str(), ctx.func, ctx.file, ctx.line);
84 void check_failed_MatType(const int v1, const int v2, const CheckContext& ctx)
87 ss << ctx.message << " (expected: '" << ctx.p1_str << " " << getTestOpMath(ctx.testOp) << " " << ctx.p2_str << "'), where" << std::endl
88 << " '" << ctx.p1_str << "' is " << v1 << " (" << typeToString(v1) << ")" << std::endl;
89 if (ctx.testOp != TEST_CUSTOM && ctx.testOp < CV__LAST_TEST_OP)
91 ss << "must be " << getTestOpPhraseStr(ctx.testOp) << std::endl;
93 ss << " '" << ctx.p2_str << "' is " << v2 << " (" << typeToString(v2) << ")";
94 cv::error(cv::Error::StsError, ss.str(), ctx.func, ctx.file, ctx.line);
96 void check_failed_MatChannels(const int v1, const int v2, const CheckContext& ctx)
98 check_failed_auto_<int>(v1, v2, ctx);
100 void check_failed_auto(const int v1, const int v2, const CheckContext& ctx)
102 check_failed_auto_<int>(v1, v2, ctx);
104 void check_failed_auto(const size_t v1, const size_t v2, const CheckContext& ctx)
106 check_failed_auto_<size_t>(v1, v2, ctx);
108 void check_failed_auto(const float v1, const float v2, const CheckContext& ctx)
110 check_failed_auto_<float>(v1, v2, ctx);
112 void check_failed_auto(const double v1, const double v2, const CheckContext& ctx)
114 check_failed_auto_<double>(v1, v2, ctx);
118 template<typename T> static CV_NORETURN
119 void check_failed_auto_(const T& v, const CheckContext& ctx)
121 std::stringstream ss;
122 ss << ctx.message << ":" << std::endl
123 << " '" << ctx.p2_str << "'" << std::endl
124 << "where" << std::endl
125 << " '" << ctx.p1_str << "' is " << v;
126 cv::error(cv::Error::StsError, ss.str(), ctx.func, ctx.file, ctx.line);
128 void check_failed_MatDepth(const int v, const CheckContext& ctx)
130 std::stringstream ss;
131 ss << ctx.message << ":" << std::endl
132 << " '" << ctx.p2_str << "'" << std::endl
133 << "where" << std::endl
134 << " '" << ctx.p1_str << "' is " << v << " (" << depthToString(v) << ")";
135 cv::error(cv::Error::StsError, ss.str(), ctx.func, ctx.file, ctx.line);
137 void check_failed_MatType(const int v, const CheckContext& ctx)
139 std::stringstream ss;
140 ss << ctx.message << ":" << std::endl
141 << " '" << ctx.p2_str << "'" << std::endl
142 << "where" << std::endl
143 << " '" << ctx.p1_str << "' is " << v << " (" << typeToString(v) << ")";
144 cv::error(cv::Error::StsError, ss.str(), ctx.func, ctx.file, ctx.line);
146 void check_failed_MatChannels(const int v, const CheckContext& ctx)
148 check_failed_auto_<int>(v, ctx);
150 void check_failed_auto(const int v, const CheckContext& ctx)
152 check_failed_auto_<int>(v, ctx);
154 void check_failed_auto(const size_t v, const CheckContext& ctx)
156 check_failed_auto_<size_t>(v, ctx);
158 void check_failed_auto(const float v, const CheckContext& ctx)
160 check_failed_auto_<float>(v, ctx);
162 void check_failed_auto(const double v, const CheckContext& ctx)
164 check_failed_auto_<double>(v, ctx);