gapi_gcompiled_tests.cpp 7.0 KB

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