7951a4c2ff81d1c99ebe84db2372b2294d54a2b8
[platform/upstream/opencv.git] / modules / gapi / test / gapi_gcompiled_tests.cpp
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.
4 //
5 // Copyright (C) 2018 Intel Corporation
6
7
8 #include "test_precomp.hpp"
9
10 namespace opencv_test
11 {
12
13 namespace
14 {
15     static cv::GMat DemoCC(cv::GMat in, cv::GScalar scale)
16     {
17         return cv::gapi::medianBlur(in + in*scale, 3);
18     }
19
20     struct GCompiledValidateMetaTyped: public ::testing::Test
21     {
22         cv::GComputationT<cv::GMat(cv::GMat,cv::GScalar)> m_cc;
23
24         GCompiledValidateMetaTyped() : m_cc(DemoCC)
25         {
26         }
27     };
28
29     struct GCompiledValidateMetaUntyped: public ::testing::Test
30     {
31         cv::GMat in;
32         cv::GScalar scale;
33         cv::GComputation m_ucc;
34
35         GCompiledValidateMetaUntyped() : m_ucc(cv::GIn(in, scale),
36                                                cv::GOut(DemoCC(in, scale)))
37         {
38         }
39     };
40 } // anonymous namespace
41
42 TEST_F(GCompiledValidateMetaTyped, ValidMeta)
43 {
44     cv::Mat in = cv::Mat::eye(cv::Size(128, 32), CV_8UC1);
45     cv::Scalar sc(127);
46
47     auto f = m_cc.compile(cv::descr_of(in),
48                           cv::descr_of(sc));
49
50     // Correct operation when meta is exactly the same
51     cv::Mat out;
52     EXPECT_NO_THROW(f(in, sc, out));
53
54     // Correct operation on next invocation with same meta
55     // taken from different input objects
56     cv::Mat in2 = cv::Mat::zeros(cv::Size(128, 32), CV_8UC1);
57     cv::Scalar sc2(64);
58     cv::Mat out2;
59     EXPECT_NO_THROW(f(in2, sc2, out2));
60 }
61
62 TEST_F(GCompiledValidateMetaTyped, InvalidMeta)
63 {
64     auto f = m_cc.compile(cv::GMatDesc{CV_8U,1,cv::Size(64,32)},
65                           cv::empty_scalar_desc());
66
67     cv::Scalar sc(33);
68     cv::Mat out;
69
70     // 3 channels instead 1
71     cv::Mat in1 = cv::Mat::eye(cv::Size(64,32), CV_8UC3);
72     EXPECT_THROW(f(in1, sc, out), std::logic_error);
73
74     // 32f instead 8u
75     cv::Mat in2 = cv::Mat::eye(cv::Size(64,32), CV_32F);
76     EXPECT_THROW(f(in2, sc, out), std::logic_error);
77
78     // 32x32 instead of 64x32
79     cv::Mat in3 = cv::Mat::eye(cv::Size(32,32), CV_8UC1);
80     EXPECT_THROW(f(in3, sc, out), std::logic_error);
81
82     // All is wrong
83     cv::Mat in4 = cv::Mat::eye(cv::Size(128,64), CV_32FC3);
84     EXPECT_THROW(f(in4, sc, out), std::logic_error);
85 }
86
87 TEST_F(GCompiledValidateMetaUntyped, ValidMeta)
88 {
89     cv::Mat in1 = cv::Mat::eye(cv::Size(128, 32), CV_8UC1);
90     cv::Scalar sc(127);
91
92     auto f = m_ucc.compile(cv::descr_of(in1),
93                            cv::descr_of(sc));
94
95     // Correct operation when meta is exactly the same
96     cv::Mat out1;
97     EXPECT_NO_THROW(f(cv::gin(in1, sc), cv::gout(out1)));
98
99     // Correct operation on next invocation with same meta
100     // taken from different input objects
101     cv::Mat in2 = cv::Mat::zeros(cv::Size(128, 32), CV_8UC1);
102     cv::Scalar sc2(64);
103     cv::Mat out2;
104     EXPECT_NO_THROW(f(cv::gin(in2, sc2), cv::gout(out2)));
105 }
106
107 TEST_F(GCompiledValidateMetaUntyped, InvalidMetaValues)
108 {
109     auto f = m_ucc.compile(cv::GMatDesc{CV_8U,1,cv::Size(64,32)},
110                            cv::empty_scalar_desc());
111
112     cv::Scalar sc(33);
113     cv::Mat out;
114
115     // 3 channels instead 1
116     cv::Mat in1 = cv::Mat::eye(cv::Size(64,32), CV_8UC3);
117     EXPECT_THROW(f(cv::gin(in1, sc), cv::gout(out)), std::logic_error);
118
119     // 32f instead 8u
120     cv::Mat in2 = cv::Mat::eye(cv::Size(64,32), CV_32F);
121     EXPECT_THROW(f(cv::gin(in2, sc), cv::gout(out)), std::logic_error);
122
123     // 32x32 instead of 64x32
124     cv::Mat in3 = cv::Mat::eye(cv::Size(32,32), CV_8UC1);
125     EXPECT_THROW(f(cv::gin(in3, sc), cv::gout(out)), std::logic_error);
126
127     // All is wrong
128     cv::Mat in4 = cv::Mat::eye(cv::Size(128,64), CV_32FC3);
129     EXPECT_THROW(f(cv::gin(in4, sc), cv::gout(out)), std::logic_error);
130 }
131
132 TEST_F(GCompiledValidateMetaUntyped, InvalidMetaShape)
133 {
134     auto f = m_ucc.compile(cv::GMatDesc{CV_8U,1,cv::Size(64,32)},
135                            cv::empty_scalar_desc());
136
137     cv::Mat in1 = cv::Mat::eye(cv::Size(64,32), CV_8UC1);
138     cv::Scalar sc(33);
139     cv::Mat out1;
140
141     // call as f(Mat,Mat) while f(Mat,Scalar) is expected
142     EXPECT_THROW(f(cv::gin(in1, in1), cv::gout(out1)), std::logic_error);
143
144     // call as f(Scalar,Mat) while f(Mat,Scalar) is expected
145     EXPECT_THROW(f(cv::gin(sc, in1), cv::gout(out1)), std::logic_error);
146
147     // call as f(Scalar,Scalar) while f(Mat,Scalar) is expected
148     EXPECT_THROW(f(cv::gin(sc, sc), cv::gout(out1)), std::logic_error);
149 }
150
151 TEST_F(GCompiledValidateMetaUntyped, InvalidMetaNumber)
152 {
153     auto f = m_ucc.compile(cv::GMatDesc{CV_8U,1,cv::Size(64,32)},
154                            cv::empty_scalar_desc());
155
156     cv::Mat in1 = cv::Mat::eye(cv::Size(64,32), CV_8UC1);
157     cv::Scalar sc(33);
158     cv::Mat out1, out2;
159
160     // call as f(Mat,Scalar,Scalar) while f(Mat,Scalar) is expected
161     EXPECT_THROW(f(cv::gin(in1, sc, sc), cv::gout(out1)), std::logic_error);
162
163     // call as f(Scalar,Mat,Scalar) while f(Mat,Scalar) is expected
164     EXPECT_THROW(f(cv::gin(sc, in1, sc), cv::gout(out1)), std::logic_error);
165
166     // call as f(Scalar) while f(Mat,Scalar) is expected
167     EXPECT_THROW(f(cv::gin(sc), cv::gout(out1)), std::logic_error);
168
169     // call as f(Mat,Scalar,[out1],[out2]) while f(Mat,Scalar,[out]) is expected
170     EXPECT_THROW(f(cv::gin(in1, sc), cv::gout(out1, out2)), std::logic_error);
171 }
172
173 } // namespace opencv_test