catching OpenCL double not supported exceptions
[profile/ivi/opencv.git] / modules / ocl / test / utility.hpp
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 #ifndef __OPENCV_TEST_UTILITY_HPP__
43 #define __OPENCV_TEST_UTILITY_HPP__
44
45 #define LOOP_TIMES 1
46
47 #define MWIDTH 256
48 #define MHEIGHT 256
49
50 #define MIN_VALUE 171
51 #define MAX_VALUE 357
52
53 namespace cvtest {
54
55 //void showDiff(cv::InputArray gold, cv::InputArray actual, double eps);
56
57 cv::ocl::oclMat createMat_ocl(cv::RNG& rng, Size size, int type, bool useRoi);
58 cv::ocl::oclMat loadMat_ocl(cv::RNG& rng, const Mat& m, bool useRoi);
59
60 // This function test if gpu_rst matches cpu_rst.
61 // If the two vectors are not equal, it will return the difference in vector size
62 // Else it will return (total diff of each cpu and gpu rects covered pixels)/(total cpu rects covered pixels)
63 // The smaller, the better matched
64 double checkRectSimilarity(cv::Size sz, std::vector<cv::Rect>& ob1, std::vector<cv::Rect>& ob2);
65
66
67 //! read image from testdata folder.
68 cv::Mat readImage(const std::string &fileName, int flags = cv::IMREAD_COLOR);
69 cv::Mat readImageType(const std::string &fname, int type);
70
71 double checkNorm(const cv::Mat &m);
72 double checkNorm(const cv::Mat &m1, const cv::Mat &m2);
73 double checkSimilarity(const cv::Mat &m1, const cv::Mat &m2);
74
75 #define EXPECT_MAT_NORM(mat, eps) \
76 { \
77     EXPECT_LE(checkNorm(cv::Mat(mat)), eps) \
78 }
79
80 #define EXPECT_MAT_NEAR(mat1, mat2, eps) \
81 { \
82    ASSERT_EQ(mat1.type(), mat2.type()); \
83    ASSERT_EQ(mat1.size(), mat2.size()); \
84    EXPECT_LE(checkNorm(cv::Mat(mat1), cv::Mat(mat2)), eps); \
85 }
86
87 #define EXPECT_MAT_SIMILAR(mat1, mat2, eps) \
88 { \
89     ASSERT_EQ(mat1.type(), mat2.type()); \
90     ASSERT_EQ(mat1.size(), mat2.size()); \
91     EXPECT_LE(checkSimilarity(cv::Mat(mat1), cv::Mat(mat2)), eps); \
92 }
93
94
95 using perf::MatDepth;
96 using perf::MatType;
97
98 //! return vector with types from specified range.
99 std::vector<MatType> types(int depth_start, int depth_end, int cn_start, int cn_end);
100
101 //! return vector with all types (depth: CV_8U-CV_64F, channels: 1-4).
102 const std::vector<MatType> &all_types();
103
104 class Inverse
105 {
106 public:
107     inline Inverse(bool val = false) : val_(val) {}
108
109     inline operator bool() const
110     {
111         return val_;
112     }
113
114 private:
115     bool val_;
116 };
117
118 void PrintTo(const Inverse &useRoi, std::ostream *os);
119
120 #define OCL_RNG_SEED 123456
121
122 template <typename T>
123 struct TSTestWithParam : public ::testing::TestWithParam<T>
124 {
125     cv::RNG rng;
126
127     TSTestWithParam()
128     {
129         rng = cv::RNG(OCL_RNG_SEED);
130     }
131
132     int randomInt(int minVal, int maxVal)
133     {
134         return rng.uniform(minVal, maxVal);
135     }
136
137     double randomDouble(double minVal, double maxVal)
138     {
139         return rng.uniform(minVal, maxVal);
140     }
141
142     double randomDoubleLog(double minVal, double maxVal)
143     {
144         double logMin = log((double)minVal + 1);
145         double logMax = log((double)maxVal + 1);
146         double pow = rng.uniform(logMin, logMax);
147         double v = exp(pow) - 1;
148         CV_Assert(v >= minVal && (v < maxVal || (v == minVal && v == maxVal)));
149         return v;
150     }
151
152     Size randomSize(int minVal, int maxVal)
153     {
154 #if 1
155         return cv::Size((int)randomDoubleLog(minVal, maxVal), (int)randomDoubleLog(minVal, maxVal));
156 #else
157         return cv::Size(randomInt(minVal, maxVal), randomInt(minVal, maxVal));
158 #endif
159     }
160
161     Size randomSize(int minValX, int maxValX, int minValY, int maxValY)
162     {
163 #if 1
164         return cv::Size(randomDoubleLog(minValX, maxValX), randomDoubleLog(minValY, maxValY));
165 #else
166         return cv::Size(randomInt(minVal, maxVal), randomInt(minVal, maxVal));
167 #endif
168     }
169
170     Scalar randomScalar(double minVal, double maxVal)
171     {
172         return Scalar(randomDouble(minVal, maxVal), randomDouble(minVal, maxVal), randomDouble(minVal, maxVal), randomDouble(minVal, maxVal));
173     }
174
175     Mat randomMat(Size size, int type, double minVal, double maxVal, bool useRoi = false)
176     {
177         RNG dataRng(rng.next());
178         return cvtest::randomMat(dataRng, size, type, minVal, maxVal, useRoi);
179     }
180
181     struct Border
182     {
183         int top, bot, lef, rig;
184     };
185
186     Border randomBorder(int minValue = 0, int maxValue = MAX_VALUE)
187     {
188         Border border = {
189                 (int)randomDoubleLog(minValue, maxValue),
190                 (int)randomDoubleLog(minValue, maxValue),
191                 (int)randomDoubleLog(minValue, maxValue),
192                 (int)randomDoubleLog(minValue, maxValue)
193         };
194         return border;
195     }
196
197     void randomSubMat(Mat& whole, Mat& subMat, const Size& roiSize, const Border& border, int type, double minVal, double maxVal)
198     {
199         Size wholeSize = Size(roiSize.width + border.lef + border.rig, roiSize.height + border.top + border.bot);
200         whole = randomMat(wholeSize, type, minVal, maxVal, false);
201         subMat = whole(Rect(border.lef, border.top, roiSize.width, roiSize.height));
202     }
203
204     void generateOclMat(cv::ocl::oclMat& whole, cv::ocl::oclMat& subMat, const Mat& wholeMat, const Size& roiSize, const Border& border)
205     {
206         whole = wholeMat;
207         subMat = whole(Rect(border.lef, border.top, roiSize.width, roiSize.height));
208     }
209 };
210
211 #define PARAM_TEST_CASE(name, ...) struct name : public TSTestWithParam< std::tr1::tuple< __VA_ARGS__ > >
212
213 #define GET_PARAM(k) std::tr1::get< k >(GetParam())
214
215 #define ALL_TYPES testing::ValuesIn(all_types())
216 #define TYPES(depth_start, depth_end, cn_start, cn_end) testing::ValuesIn(types(depth_start, depth_end, cn_start, cn_end))
217
218 #define DIFFERENT_SIZES testing::Values(cv::Size(128, 128), cv::Size(113, 113), cv::Size(1300, 1300))
219
220 #define IMAGE_CHANNELS testing::Values(Channels(1), Channels(3), Channels(4))
221 #ifndef IMPLEMENT_PARAM_CLASS
222 #define IMPLEMENT_PARAM_CLASS(name, type) \
223     class name \
224     { \
225     public: \
226         name ( type arg = type ()) : val_(arg) {} \
227         operator type () const {return val_;} \
228     private: \
229         type val_; \
230     }; \
231     inline void PrintTo( name param, std::ostream* os) \
232     { \
233         *os << #name <<  "(" << testing::PrintToString(static_cast< type >(param)) << ")"; \
234     }
235
236 IMPLEMENT_PARAM_CLASS(Channels, int)
237 #endif // IMPLEMENT_PARAM_CLASS
238
239 } // namespace cvtest
240
241 enum {FLIP_BOTH = 0, FLIP_X = 1, FLIP_Y = -1};
242 CV_ENUM(FlipCode, FLIP_BOTH, FLIP_X, FLIP_Y)
243
244 CV_ENUM(CmpCode, CMP_EQ, CMP_GT, CMP_GE, CMP_LT, CMP_LE, CMP_NE)
245 CV_ENUM(NormCode, NORM_INF, NORM_L1, NORM_L2, NORM_TYPE_MASK, NORM_RELATIVE, NORM_MINMAX)
246 CV_ENUM(ReduceOp, CV_REDUCE_SUM, CV_REDUCE_AVG, CV_REDUCE_MAX, CV_REDUCE_MIN)
247 CV_ENUM(MorphOp, MORPH_OPEN, MORPH_CLOSE, MORPH_GRADIENT, MORPH_TOPHAT, MORPH_BLACKHAT)
248 CV_ENUM(ThreshOp, THRESH_BINARY, THRESH_BINARY_INV, THRESH_TRUNC, THRESH_TOZERO, THRESH_TOZERO_INV)
249 CV_ENUM(Interpolation, INTER_NEAREST, INTER_LINEAR, INTER_CUBIC)
250 CV_ENUM(Border, BORDER_REFLECT101, BORDER_REPLICATE, BORDER_CONSTANT, BORDER_REFLECT, BORDER_WRAP)
251 CV_ENUM(TemplateMethod, TM_SQDIFF, TM_SQDIFF_NORMED, TM_CCORR, TM_CCORR_NORMED, TM_CCOEFF, TM_CCOEFF_NORMED)
252
253 CV_FLAGS(GemmFlags, GEMM_1_T, GEMM_2_T, GEMM_3_T);
254 CV_FLAGS(WarpFlags, INTER_NEAREST, INTER_LINEAR, INTER_CUBIC, WARP_INVERSE_MAP)
255 CV_FLAGS(DftFlags, DFT_INVERSE, DFT_SCALE, DFT_ROWS, DFT_COMPLEX_OUTPUT, DFT_REAL_OUTPUT)
256
257 # define OCL_TEST_P(test_case_name, test_name) \
258     class GTEST_TEST_CLASS_NAME_(test_case_name, test_name) : \
259         public test_case_name { \
260     public: \
261         GTEST_TEST_CLASS_NAME_(test_case_name, test_name)() { } \
262         virtual void TestBody(); \
263         void OCLTestBody(); \
264     private: \
265         static int AddToRegistry() \
266         { \
267             ::testing::UnitTest::GetInstance()->parameterized_test_registry(). \
268               GetTestCasePatternHolder<test_case_name>(\
269                   #test_case_name, __FILE__, __LINE__)->AddTestPattern(\
270                       #test_case_name, \
271                       #test_name, \
272                       new ::testing::internal::TestMetaFactory< \
273                           GTEST_TEST_CLASS_NAME_(test_case_name, test_name)>()); \
274             return 0; \
275         } \
276     \
277         static int gtest_registering_dummy_; \
278         GTEST_DISALLOW_COPY_AND_ASSIGN_(\
279             GTEST_TEST_CLASS_NAME_(test_case_name, test_name)); \
280     }; \
281     \
282     int GTEST_TEST_CLASS_NAME_(test_case_name, \
283                              test_name)::gtest_registering_dummy_ = \
284       GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::AddToRegistry(); \
285     \
286     void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::TestBody() \
287     { \
288         try \
289         { \
290             OCLTestBody(); \
291         } \
292         catch (const cv::Exception & ex) \
293         { \
294             if (ex.code != CV_OpenCLDoubleNotSupported) \
295                throw; \
296             else \
297                 std::cout << "Test skipped (selected device does not support double)" << std::endl; \
298         } \
299     } \
300     \
301     void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::OCLTestBody()
302
303 #endif // __OPENCV_TEST_UTILITY_HPP__