disabled 'reshape', 'copyTo' test
[profile/ivi/opencv.git] / modules / ts / include / opencv2 / ts / ocl_test.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 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2010-2013, Advanced Micro Devices, Inc., 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 the copyright holders 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 OpenCV Foundation 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_TS_OCL_TEST_HPP__
43 #define __OPENCV_TS_OCL_TEST_HPP__
44
45 #include "opencv2/opencv_modules.hpp"
46
47 #include "opencv2/ts.hpp"
48
49 #include "opencv2/highgui.hpp"
50 #include "opencv2/imgproc.hpp"
51 #include "opencv2/imgproc/types_c.h"
52 #include "opencv2/core/ocl.hpp"
53
54 namespace cvtest {
55 namespace ocl {
56
57 using namespace cv;
58 using namespace testing;
59
60 extern int test_loop_times;
61
62 #define MAX_VALUE 357
63
64 #define EXPECT_MAT_NORM(mat, eps) \
65 { \
66     EXPECT_LE(TestUtils::checkNorm(mat), eps) \
67 }
68
69 #define EXPECT_MAT_NEAR(mat1, mat2, eps) \
70 { \
71     ASSERT_EQ(mat1.type(), mat2.type()); \
72     ASSERT_EQ(mat1.size(), mat2.size()); \
73     EXPECT_LE(TestUtils::checkNorm(mat1, mat2), eps) \
74         << "Size: " << mat1.size() << std::endl; \
75 }
76
77 #define EXPECT_MAT_NEAR_RELATIVE(mat1, mat2, eps) \
78 { \
79     ASSERT_EQ(mat1.type(), mat2.type()); \
80     ASSERT_EQ(mat1.size(), mat2.size()); \
81     EXPECT_LE(TestUtils::checkNormRelative(mat1, mat2), eps) \
82         << "Size: " << mat1.size() << std::endl; \
83 }
84
85 #define OCL_EXPECT_MATS_NEAR(name, eps) \
86 { \
87     EXPECT_MAT_NEAR(name ## _roi, u ## name ## _roi, eps); \
88     int nextValue = rng.next(); \
89     RNG dataRng1(nextValue), dataRng2(nextValue); \
90     dataRng1.fill(name ## _roi, RNG::UNIFORM, Scalar::all(-MAX_VALUE), Scalar::all(MAX_VALUE)); \
91     dataRng2.fill(u ## name ## _roi, RNG::UNIFORM, Scalar::all(-MAX_VALUE), Scalar::all(MAX_VALUE)); \
92     EXPECT_MAT_NEAR(name, u ## name, 0/*FLT_EPSILON*/); \
93 }
94
95 #define OCL_EXPECT_MATS_NEAR_RELATIVE(name, eps) \
96 { \
97     EXPECT_MAT_NEAR_RELATIVE(name ## _roi, u ## name ## _roi, eps); \
98     int nextValue = rng.next(); \
99     RNG dataRng1(nextValue), dataRng2(nextValue); \
100     dataRng1.fill(name ## _roi, RNG::UNIFORM, Scalar::all(-MAX_VALUE), Scalar::all(MAX_VALUE)); \
101     dataRng2.fill(u ## name ## _roi, RNG::UNIFORM, Scalar::all(-MAX_VALUE), Scalar::all(MAX_VALUE)); \
102     EXPECT_MAT_NEAR_RELATIVE(name, u ## name, 0/*FLT_EPSILON*/); \
103 }
104
105 #define EXPECT_MAT_SIMILAR(mat1, mat2, eps) \
106 { \
107     ASSERT_EQ(mat1.type(), mat2.type()); \
108     ASSERT_EQ(mat1.size(), mat2.size()); \
109     EXPECT_LE(checkSimilarity(mat1, mat2), eps) \
110         << "Size: " << mat1.size() << std::endl; \
111 }
112
113 using perf::MatDepth;
114 using perf::MatType;
115
116 #define OCL_RNG_SEED 123456
117
118 struct CV_EXPORTS TestUtils
119 {
120     cv::RNG rng;
121
122     TestUtils()
123     {
124         rng = cv::RNG(OCL_RNG_SEED);
125     }
126
127     int randomInt(int minVal, int maxVal)
128     {
129         return rng.uniform(minVal, maxVal);
130     }
131
132     double randomDouble(double minVal, double maxVal)
133     {
134         return rng.uniform(minVal, maxVal);
135     }
136
137     double randomDoubleLog(double minVal, double maxVal)
138     {
139         double logMin = log((double)minVal + 1);
140         double logMax = log((double)maxVal + 1);
141         double pow = rng.uniform(logMin, logMax);
142         double v = exp(pow) - 1;
143         CV_Assert(v >= minVal && (v < maxVal || (v == minVal && v == maxVal)));
144         return v;
145     }
146
147     Size randomSize(int minVal, int maxVal)
148     {
149 #if 1
150         return cv::Size((int)randomDoubleLog(minVal, maxVal), (int)randomDoubleLog(minVal, maxVal));
151 #else
152         return cv::Size(randomInt(minVal, maxVal), randomInt(minVal, maxVal));
153 #endif
154     }
155
156     Size randomSize(int minValX, int maxValX, int minValY, int maxValY)
157     {
158 #if 1
159         return cv::Size((int)randomDoubleLog(minValX, maxValX), (int)randomDoubleLog(minValY, maxValY));
160 #else
161         return cv::Size(randomInt(minVal, maxVal), randomInt(minVal, maxVal));
162 #endif
163     }
164
165     Scalar randomScalar(double minVal, double maxVal)
166     {
167         return Scalar(randomDouble(minVal, maxVal), randomDouble(minVal, maxVal), randomDouble(minVal, maxVal), randomDouble(minVal, maxVal));
168     }
169
170     Mat randomMat(Size size, int type, double minVal, double maxVal, bool useRoi = false)
171     {
172         RNG dataRng(rng.next());
173         return cvtest::randomMat(dataRng, size, type, minVal, maxVal, useRoi);
174     }
175
176     struct Border
177     {
178         int top, bot, lef, rig;
179     };
180
181     Border randomBorder(int minValue = 0, int maxValue = MAX_VALUE)
182     {
183         Border border = {
184                 (int)randomDoubleLog(minValue, maxValue),
185                 (int)randomDoubleLog(minValue, maxValue),
186                 (int)randomDoubleLog(minValue, maxValue),
187                 (int)randomDoubleLog(minValue, maxValue)
188         };
189         return border;
190     }
191
192     void randomSubMat(Mat& whole, Mat& subMat, const Size& roiSize, const Border& border, int type, double minVal, double maxVal)
193     {
194         Size wholeSize = Size(roiSize.width + border.lef + border.rig, roiSize.height + border.top + border.bot);
195         whole = randomMat(wholeSize, type, minVal, maxVal, false);
196         subMat = whole(Rect(border.lef, border.top, roiSize.width, roiSize.height));
197     }
198
199     // If the two vectors are not equal, it will return the difference in vector size
200     // Else it will return (total diff of each 1 and 2 rects covered pixels)/(total 1 rects covered pixels)
201     // The smaller, the better matched
202     static double checkRectSimilarity(const cv::Size & sz, std::vector<cv::Rect>& ob1, std::vector<cv::Rect>& ob2);
203
204     //! read image from testdata folder.
205     static cv::Mat readImage(const String &fileName, int flags = cv::IMREAD_COLOR);
206     static cv::Mat readImageType(const String &fname, int type);
207
208     static double checkNorm(InputArray m);
209     static double checkNorm(InputArray m1, InputArray m2);
210     static double checkSimilarity(InputArray m1, InputArray m2);
211     static void showDiff(InputArray _src, InputArray _gold, InputArray _actual, double eps, bool alwaysShow);
212
213     static inline double checkNormRelative(InputArray m1, InputArray m2)
214     {
215         return cv::norm(m1.getMat(), m2.getMat(), cv::NORM_INF) /
216                 std::max((double)std::numeric_limits<float>::epsilon(),
217                          (double)std::max(cv::norm(m1.getMat(), cv::NORM_INF), norm(m2.getMat(), cv::NORM_INF)));
218     }
219 };
220
221 #define TEST_DECLARE_INPUT_PARAMETER(name) Mat name, name ## _roi; UMat u ## name, u ## name ## _roi;
222 #define TEST_DECLARE_OUTPUT_PARAMETER(name) TEST_DECLARE_INPUT_PARAMETER(name)
223
224 #define UMAT_UPLOAD_INPUT_PARAMETER(name) \
225 { \
226     name.copyTo(u ## name); \
227     Size _wholeSize; Point ofs; name ## _roi.locateROI(_wholeSize, ofs); \
228     u ## name ## _roi = u ## name(Rect(ofs.x, ofs.y, name ## _roi.size().width, name ## _roi.size().height)); \
229 }
230 #define UMAT_UPLOAD_OUTPUT_PARAMETER(name) UMAT_UPLOAD_INPUT_PARAMETER(name)
231
232 template <typename T>
233 struct CV_EXPORTS TSTestWithParam : public TestUtils, public ::testing::TestWithParam<T>
234 {
235
236 };
237
238 #define PARAM_TEST_CASE(name, ...) struct name : public TSTestWithParam< std::tr1::tuple< __VA_ARGS__ > >
239
240 #define GET_PARAM(k) std::tr1::get< k >(GetParam())
241
242 #ifndef IMPLEMENT_PARAM_CLASS
243 #define IMPLEMENT_PARAM_CLASS(name, type) \
244     class name \
245     { \
246     public: \
247         name ( type arg = type ()) : val_(arg) {} \
248         operator type () const {return val_;} \
249     private: \
250         type val_; \
251     }; \
252     inline void PrintTo( name param, std::ostream* os) \
253     { \
254         *os << #name <<  "(" << testing::PrintToString(static_cast< type >(param)) << ")"; \
255     }
256
257 IMPLEMENT_PARAM_CLASS(Channels, int)
258 #endif // IMPLEMENT_PARAM_CLASS
259
260 #define OCL_TEST_P TEST_P
261 #define OCL_TEST_F(name, ...) typedef name OCL_##name; TEST_F(OCL_##name, __VA_ARGS__)
262 #define OCL_TEST(name, ...) TEST(OCL_##name, __VA_ARGS__)
263
264 #define OCL_OFF(fn) cv::ocl::setUseOpenCL(false); fn
265 #define OCL_ON(fn) cv::ocl::setUseOpenCL(true); fn
266
267 #define OCL_ALL_DEPTHS Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F)
268 #define OCL_ALL_CHANNELS Values(1, 2, 3, 4)
269
270 CV_ENUM(Interpolation, INTER_NEAREST, INTER_LINEAR, INTER_CUBIC, INTER_AREA)
271 CV_ENUM(ThreshOp, THRESH_BINARY, THRESH_BINARY_INV, THRESH_TRUNC, THRESH_TOZERO, THRESH_TOZERO_INV)
272 CV_ENUM(BorderType, BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT, BORDER_WRAP, BORDER_REFLECT_101)
273
274 #define OCL_INSTANTIATE_TEST_CASE_P(prefix, test_case_name, generator) \
275     INSTANTIATE_TEST_CASE_P(OCL_ ## prefix, test_case_name, generator)
276
277 } } // namespace cvtest::ocl
278
279 #endif // __OPENCV_TS_OCL_TEST_HPP__