test_misc.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  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. #include "test_precomp.hpp"
  5. #include <cmath>
  6. #include "opencv2/core/utils/logger.hpp"
  7. #include <opencv2/core/utils/fp_control_utils.hpp>
  8. #ifdef CV_CXX11
  9. #include <chrono>
  10. #include <thread>
  11. #endif
  12. namespace opencv_test { namespace {
  13. TEST(Core_OutputArrayCreate, _1997)
  14. {
  15. struct local {
  16. static void create(OutputArray arr, Size submatSize, int type)
  17. {
  18. int sizes[] = {submatSize.width, submatSize.height};
  19. arr.create(sizeof(sizes)/sizeof(sizes[0]), sizes, type);
  20. }
  21. };
  22. Mat mat(Size(512, 512), CV_8U);
  23. Size submatSize = Size(256, 256);
  24. ASSERT_NO_THROW(local::create( mat(Rect(Point(), submatSize)), submatSize, mat.type() ));
  25. }
  26. TEST(Core_SaturateCast, NegativeNotClipped)
  27. {
  28. double d = -1.0;
  29. unsigned int val = cv::saturate_cast<unsigned int>(d);
  30. ASSERT_EQ(0xffffffff, val);
  31. }
  32. template<typename T, typename U>
  33. static double maxAbsDiff(const T &t, const U &u)
  34. {
  35. Mat_<double> d;
  36. absdiff(t, u, d);
  37. double ret;
  38. minMaxLoc(d, NULL, &ret);
  39. return ret;
  40. }
  41. TEST(Core_OutputArrayAssign, _Matxd_Matd)
  42. {
  43. Mat expected = (Mat_<double>(2,3) << 1, 2, 3, .1, .2, .3);
  44. Matx23d actualx;
  45. {
  46. OutputArray oa(actualx);
  47. oa.assign(expected);
  48. }
  49. Mat actual = (Mat) actualx;
  50. EXPECT_LE(maxAbsDiff(expected, actual), 0.0);
  51. }
  52. TEST(Core_OutputArrayAssign, _Matxd_Matf)
  53. {
  54. Mat expected = (Mat_<float>(2,3) << 1, 2, 3, .1, .2, .3);
  55. Matx23d actualx;
  56. {
  57. OutputArray oa(actualx);
  58. oa.assign(expected);
  59. }
  60. Mat actual = (Mat) actualx;
  61. EXPECT_LE(maxAbsDiff(expected, actual), FLT_EPSILON);
  62. }
  63. TEST(Core_OutputArrayAssign, _Matxf_Matd)
  64. {
  65. Mat expected = (Mat_<double>(2,3) << 1, 2, 3, .1, .2, .3);
  66. Matx23f actualx;
  67. {
  68. OutputArray oa(actualx);
  69. oa.assign(expected);
  70. }
  71. Mat actual = (Mat) actualx;
  72. EXPECT_LE(maxAbsDiff(expected, actual), FLT_EPSILON);
  73. }
  74. TEST(Core_OutputArrayAssign, _Matxd_UMatd)
  75. {
  76. Mat expected = (Mat_<double>(2,3) << 1, 2, 3, .1, .2, .3);
  77. UMat uexpected = expected.getUMat(ACCESS_READ);
  78. Matx23d actualx;
  79. {
  80. OutputArray oa(actualx);
  81. oa.assign(uexpected);
  82. }
  83. Mat actual = (Mat) actualx;
  84. EXPECT_LE(maxAbsDiff(expected, actual), 0.0);
  85. }
  86. TEST(Core_OutputArrayAssign, _Matxd_UMatf)
  87. {
  88. Mat expected = (Mat_<float>(2,3) << 1, 2, 3, .1, .2, .3);
  89. UMat uexpected = expected.getUMat(ACCESS_READ);
  90. Matx23d actualx;
  91. {
  92. OutputArray oa(actualx);
  93. oa.assign(uexpected);
  94. }
  95. Mat actual = (Mat) actualx;
  96. EXPECT_LE(maxAbsDiff(expected, actual), FLT_EPSILON);
  97. }
  98. TEST(Core_OutputArrayAssign, _Matxf_UMatd)
  99. {
  100. Mat expected = (Mat_<double>(2,3) << 1, 2, 3, .1, .2, .3);
  101. UMat uexpected = expected.getUMat(ACCESS_READ);
  102. Matx23f actualx;
  103. {
  104. OutputArray oa(actualx);
  105. oa.assign(uexpected);
  106. }
  107. Mat actual = (Mat) actualx;
  108. EXPECT_LE(maxAbsDiff(expected, actual), FLT_EPSILON);
  109. }
  110. int fixedType_handler(OutputArray dst)
  111. {
  112. int type = CV_32FC2; // return points only {x, y}
  113. if (dst.fixedType())
  114. {
  115. type = dst.type();
  116. CV_Assert(type == CV_32FC2 || type == CV_32FC3); // allow points + confidence level: {x, y, confidence}
  117. }
  118. const int N = 100;
  119. dst.create(Size(1, N), type);
  120. Mat m = dst.getMat();
  121. if (m.type() == CV_32FC2)
  122. {
  123. for (int i = 0; i < N; i++)
  124. m.at<Vec2f>(i) = Vec2f((float)i, (float)(i*2));
  125. }
  126. else if (m.type() == CV_32FC3)
  127. {
  128. for (int i = 0; i < N; i++)
  129. m.at<Vec3f>(i) = Vec3f((float)i, (float)(i*2), 1.0f / (i + 1));
  130. }
  131. else
  132. {
  133. CV_Assert(0 && "Internal error");
  134. }
  135. return CV_MAT_CN(type);
  136. }
  137. TEST(Core_OutputArray, FixedType)
  138. {
  139. Mat_<Vec2f> pointsOnly;
  140. int num_pointsOnly = fixedType_handler(pointsOnly);
  141. EXPECT_EQ(2, num_pointsOnly);
  142. Mat_<Vec3f> pointsWithConfidence;
  143. int num_pointsWithConfidence = fixedType_handler(pointsWithConfidence);
  144. EXPECT_EQ(3, num_pointsWithConfidence);
  145. Mat defaultResult;
  146. int num_defaultResult = fixedType_handler(defaultResult);
  147. EXPECT_EQ(2, num_defaultResult);
  148. }
  149. TEST(Core_OutputArrayCreate, _13772)
  150. {
  151. cv::Mat1d mat;
  152. cv::OutputArray o(mat);
  153. ASSERT_NO_THROW(o.create(3, 5, CV_64F, -1, true));
  154. }
  155. TEST(Core_String, find_last_of__with__empty_string)
  156. {
  157. cv::String s;
  158. size_t p = s.find_last_of('q', 0);
  159. // npos is not exported: EXPECT_EQ(cv::String::npos, p);
  160. EXPECT_EQ(std::string::npos, p);
  161. }
  162. TEST(Core_String, end_method_regression)
  163. {
  164. cv::String old_string = "012345";
  165. cv::String new_string(old_string.begin(), old_string.end());
  166. EXPECT_EQ(6u, new_string.size());
  167. }
  168. TEST(Core_Copy, repeat_regression_8972)
  169. {
  170. Mat src = (Mat_<int>(1, 4) << 1, 2, 3, 4);
  171. ASSERT_ANY_THROW({
  172. repeat(src, 5, 1, src);
  173. });
  174. }
  175. class ThrowErrorParallelLoopBody : public cv::ParallelLoopBody
  176. {
  177. public:
  178. ThrowErrorParallelLoopBody(cv::Mat& dst, int i) : dst_(dst), i_(i) {}
  179. ~ThrowErrorParallelLoopBody() {}
  180. void operator()(const cv::Range& r) const
  181. {
  182. for (int i = r.start; i < r.end; i++)
  183. {
  184. CV_Assert(i != i_);
  185. dst_.row(i).setTo(1);
  186. }
  187. }
  188. protected:
  189. Mat dst_;
  190. int i_;
  191. };
  192. TEST(Core_Parallel, propagate_exceptions)
  193. {
  194. Mat dst1(1000, 100, CV_8SC1, Scalar::all(0));
  195. ASSERT_NO_THROW({
  196. parallel_for_(cv::Range(0, dst1.rows), ThrowErrorParallelLoopBody(dst1, -1));
  197. });
  198. Mat dst2(1000, 100, CV_8SC1, Scalar::all(0));
  199. ASSERT_THROW({
  200. parallel_for_(cv::Range(0, dst2.rows), ThrowErrorParallelLoopBody(dst2, dst2.rows / 2));
  201. }, cv::Exception);
  202. }
  203. class FPDenormalsHintCheckerParallelLoopBody : public cv::ParallelLoopBody
  204. {
  205. public:
  206. FPDenormalsHintCheckerParallelLoopBody()
  207. : isOK(true)
  208. {
  209. state_values_to_check = cv::details::saveFPDenormalsState(base_state);
  210. }
  211. ~FPDenormalsHintCheckerParallelLoopBody() {}
  212. void operator()(const cv::Range& r) const
  213. {
  214. CV_UNUSED(r);
  215. cv::details::FPDenormalsModeState state;
  216. if (cv::details::saveFPDenormalsState(state))
  217. {
  218. for (int i = 0; i < state_values_to_check; ++i)
  219. {
  220. if (base_state.reserved[i] != state.reserved[i])
  221. {
  222. CV_LOG_ERROR(NULL, cv::format("FP state[%d] mismatch: base=0x%08x thread=0x%08x", i, base_state.reserved[i], state.reserved[i]));
  223. isOK = false;
  224. cv::details::restoreFPDenormalsState(base_state);
  225. }
  226. }
  227. }
  228. else
  229. {
  230. // FP state is not supported
  231. // no checks
  232. }
  233. #ifdef CV_CXX11
  234. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  235. #endif
  236. }
  237. cv::details::FPDenormalsModeState base_state;
  238. int state_values_to_check;
  239. mutable bool isOK;
  240. };
  241. TEST(Core_Parallel, propagate_fp_denormals_ignore_hint)
  242. {
  243. int nThreads = std::max(1, cv::getNumThreads()) * 3;
  244. for (int i = 0; i < 4; ++i)
  245. {
  246. SCOPED_TRACE(cv::format("Case=%d: FP denormals ignore hint: %s\n", i, ((i & 1) != 0) ? "enable" : "disable"));
  247. FPDenormalsIgnoreHintScope fp_denormals_scope((i & 1) != 0);
  248. FPDenormalsHintCheckerParallelLoopBody job;
  249. ASSERT_NO_THROW({
  250. parallel_for_(cv::Range(0, nThreads), job);
  251. });
  252. EXPECT_TRUE(job.isOK);
  253. }
  254. }
  255. TEST(Core_Version, consistency)
  256. {
  257. // this test verifies that OpenCV version loaded in runtime
  258. // is the same this test has been built with
  259. EXPECT_EQ(CV_VERSION_MAJOR, cv::getVersionMajor());
  260. EXPECT_EQ(CV_VERSION_MINOR, cv::getVersionMinor());
  261. EXPECT_EQ(CV_VERSION_REVISION, cv::getVersionRevision());
  262. EXPECT_EQ(String(CV_VERSION), cv::getVersionString());
  263. }
  264. //
  265. // Test core/check.hpp macros
  266. //
  267. void test_check_eq_1(int value_1, int value_2)
  268. {
  269. CV_CheckEQ(value_1, value_2, "Validation check failed");
  270. }
  271. TEST(Core_Check, testEQ_int_fail)
  272. {
  273. try
  274. {
  275. test_check_eq_1(123, 5678);
  276. FAIL() << "Unreachable code called";
  277. }
  278. catch (const cv::Exception& e)
  279. {
  280. EXPECT_STREQ(e.err.c_str(),
  281. "> Validation check failed (expected: 'value_1 == value_2'), where\n"
  282. "> 'value_1' is 123\n"
  283. "> must be equal to\n"
  284. "> 'value_2' is 5678\n"
  285. );
  286. }
  287. catch (const std::exception& e)
  288. {
  289. FAIL() << "Unexpected C++ exception: " << e.what();
  290. }
  291. catch (...)
  292. {
  293. FAIL() << "Unexpected unknown exception";
  294. }
  295. }
  296. TEST(Core_Check, testEQ_int_pass)
  297. {
  298. EXPECT_NO_THROW(
  299. {
  300. test_check_eq_1(1234, 1234);
  301. });
  302. }
  303. void test_check_eq_2(float value_1, float value_2)
  304. {
  305. CV_CheckEQ(value_1, value_2, "Validation check failed (float)");
  306. }
  307. TEST(Core_Check, testEQ_float_fail)
  308. {
  309. try
  310. {
  311. test_check_eq_2(1234.5f, 1234.55f);
  312. FAIL() << "Unreachable code called";
  313. }
  314. catch (const cv::Exception& e)
  315. {
  316. EXPECT_STREQ(e.err.c_str(),
  317. "> Validation check failed (float) (expected: 'value_1 == value_2'), where\n"
  318. "> 'value_1' is 1234.5\n" // TODO Locale handling (use LC_ALL=C on Linux)
  319. "> must be equal to\n"
  320. "> 'value_2' is 1234.55\n"
  321. );
  322. }
  323. catch (const std::exception& e)
  324. {
  325. FAIL() << "Unexpected C++ exception: " << e.what();
  326. }
  327. catch (...)
  328. {
  329. FAIL() << "Unexpected unknown exception";
  330. }
  331. }
  332. TEST(Core_Check, testEQ_float_pass)
  333. {
  334. EXPECT_NO_THROW(
  335. {
  336. test_check_eq_2(1234.6f, 1234.6f);
  337. });
  338. }
  339. void test_check_eq_3(double value_1, double value_2)
  340. {
  341. CV_CheckEQ(value_1, value_2, "Validation check failed (double)");
  342. }
  343. TEST(Core_Check, testEQ_double_fail)
  344. {
  345. try
  346. {
  347. test_check_eq_3(1234.5, 1234.56);
  348. FAIL() << "Unreachable code called";
  349. }
  350. catch (const cv::Exception& e)
  351. {
  352. EXPECT_STREQ(e.err.c_str(),
  353. "> Validation check failed (double) (expected: 'value_1 == value_2'), where\n"
  354. "> 'value_1' is 1234.5\n" // TODO Locale handling (use LC_ALL=C on Linux)
  355. "> must be equal to\n"
  356. "> 'value_2' is 1234.56\n"
  357. );
  358. }
  359. catch (const std::exception& e)
  360. {
  361. FAIL() << "Unexpected C++ exception: " << e.what();
  362. }
  363. catch (...)
  364. {
  365. FAIL() << "Unexpected unknown exception";
  366. }
  367. }
  368. TEST(Core_Check, testEQ_double_pass)
  369. {
  370. EXPECT_NO_THROW(
  371. {
  372. test_check_eq_3(1234.0f, 1234.0f);
  373. });
  374. }
  375. void test_check_ne_1(int value_1, int value_2)
  376. {
  377. CV_CheckNE(value_1, value_2, "Validation NE check failed");
  378. }
  379. TEST(Core_Check, testNE_int_fail)
  380. {
  381. try
  382. {
  383. test_check_ne_1(123, 123);
  384. FAIL() << "Unreachable code called";
  385. }
  386. catch (const cv::Exception& e)
  387. {
  388. EXPECT_STREQ(e.err.c_str(),
  389. "> Validation NE check failed (expected: 'value_1 != value_2'), where\n"
  390. "> 'value_1' is 123\n"
  391. "> must be not equal to\n"
  392. "> 'value_2' is 123\n"
  393. );
  394. }
  395. catch (const std::exception& e)
  396. {
  397. FAIL() << "Unexpected C++ exception: " << e.what();
  398. }
  399. catch (...)
  400. {
  401. FAIL() << "Unexpected unknown exception";
  402. }
  403. }
  404. TEST(Core_Check, testNE_int_pass)
  405. {
  406. EXPECT_NO_THROW(
  407. {
  408. test_check_ne_1(123, 1234);
  409. });
  410. }
  411. void test_check_le_1(int value_1, int value_2)
  412. {
  413. CV_CheckLE(value_1, value_2, "Validation LE check failed");
  414. }
  415. TEST(Core_Check, testLE_int_fail)
  416. {
  417. try
  418. {
  419. test_check_le_1(1234, 123);
  420. FAIL() << "Unreachable code called";
  421. }
  422. catch (const cv::Exception& e)
  423. {
  424. EXPECT_STREQ(e.err.c_str(),
  425. "> Validation LE check failed (expected: 'value_1 <= value_2'), where\n"
  426. "> 'value_1' is 1234\n"
  427. "> must be less than or equal to\n"
  428. "> 'value_2' is 123\n"
  429. );
  430. }
  431. catch (const std::exception& e)
  432. {
  433. FAIL() << "Unexpected C++ exception: " << e.what();
  434. }
  435. catch (...)
  436. {
  437. FAIL() << "Unexpected unknown exception";
  438. }
  439. }
  440. TEST(Core_Check, testLE_int_pass)
  441. {
  442. EXPECT_NO_THROW(
  443. {
  444. test_check_le_1(1234, 1234);
  445. });
  446. EXPECT_NO_THROW(
  447. {
  448. test_check_le_1(123, 1234);
  449. });
  450. }
  451. void test_check_lt_1(int value_1, int value_2)
  452. {
  453. CV_CheckLT(value_1, value_2, "Validation LT check failed");
  454. }
  455. TEST(Core_Check, testLT_int_fail)
  456. {
  457. try
  458. {
  459. test_check_lt_1(1234, 123);
  460. FAIL() << "Unreachable code called";
  461. }
  462. catch (const cv::Exception& e)
  463. {
  464. EXPECT_STREQ(e.err.c_str(),
  465. "> Validation LT check failed (expected: 'value_1 < value_2'), where\n"
  466. "> 'value_1' is 1234\n"
  467. "> must be less than\n"
  468. "> 'value_2' is 123\n"
  469. );
  470. }
  471. catch (const std::exception& e)
  472. {
  473. FAIL() << "Unexpected C++ exception: " << e.what();
  474. }
  475. catch (...)
  476. {
  477. FAIL() << "Unexpected unknown exception";
  478. }
  479. }
  480. TEST(Core_Check, testLT_int_fail_eq)
  481. {
  482. try
  483. {
  484. test_check_lt_1(123, 123);
  485. FAIL() << "Unreachable code called";
  486. }
  487. catch (const cv::Exception& e)
  488. {
  489. EXPECT_STREQ(e.err.c_str(),
  490. "> Validation LT check failed (expected: 'value_1 < value_2'), where\n"
  491. "> 'value_1' is 123\n"
  492. "> must be less than\n"
  493. "> 'value_2' is 123\n"
  494. );
  495. }
  496. catch (const std::exception& e)
  497. {
  498. FAIL() << "Unexpected C++ exception: " << e.what();
  499. }
  500. catch (...)
  501. {
  502. FAIL() << "Unexpected unknown exception";
  503. }
  504. }
  505. TEST(Core_Check, testLT_int_pass)
  506. {
  507. EXPECT_NO_THROW(
  508. {
  509. test_check_lt_1(123, 1234);
  510. });
  511. }
  512. void test_check_ge_1(int value_1, int value_2)
  513. {
  514. CV_CheckGE(value_1, value_2, "Validation GE check failed");
  515. }
  516. TEST(Core_Check, testGE_int_fail)
  517. {
  518. try
  519. {
  520. test_check_ge_1(123, 1234);
  521. FAIL() << "Unreachable code called";
  522. }
  523. catch (const cv::Exception& e)
  524. {
  525. EXPECT_STREQ(e.err.c_str(),
  526. "> Validation GE check failed (expected: 'value_1 >= value_2'), where\n"
  527. "> 'value_1' is 123\n"
  528. "> must be greater than or equal to\n"
  529. "> 'value_2' is 1234\n"
  530. );
  531. }
  532. catch (const std::exception& e)
  533. {
  534. FAIL() << "Unexpected C++ exception: " << e.what();
  535. }
  536. catch (...)
  537. {
  538. FAIL() << "Unexpected unknown exception";
  539. }
  540. }
  541. TEST(Core_Check, testGE_int_pass)
  542. {
  543. EXPECT_NO_THROW(
  544. {
  545. test_check_ge_1(1234, 1234);
  546. });
  547. EXPECT_NO_THROW(
  548. {
  549. test_check_ge_1(1234, 123);
  550. });
  551. }
  552. void test_check_gt_1(int value_1, int value_2)
  553. {
  554. CV_CheckGT(value_1, value_2, "Validation GT check failed");
  555. }
  556. TEST(Core_Check, testGT_int_fail)
  557. {
  558. try
  559. {
  560. test_check_gt_1(123, 1234);
  561. FAIL() << "Unreachable code called";
  562. }
  563. catch (const cv::Exception& e)
  564. {
  565. EXPECT_STREQ(e.err.c_str(),
  566. "> Validation GT check failed (expected: 'value_1 > value_2'), where\n"
  567. "> 'value_1' is 123\n"
  568. "> must be greater than\n"
  569. "> 'value_2' is 1234\n"
  570. );
  571. }
  572. catch (const std::exception& e)
  573. {
  574. FAIL() << "Unexpected C++ exception: " << e.what();
  575. }
  576. catch (...)
  577. {
  578. FAIL() << "Unexpected unknown exception";
  579. }
  580. }
  581. TEST(Core_Check, testGT_int_fail_eq)
  582. {
  583. try
  584. {
  585. test_check_gt_1(123, 123);
  586. FAIL() << "Unreachable code called";
  587. }
  588. catch (const cv::Exception& e)
  589. {
  590. EXPECT_STREQ(e.err.c_str(),
  591. "> Validation GT check failed (expected: 'value_1 > value_2'), where\n"
  592. "> 'value_1' is 123\n"
  593. "> must be greater than\n"
  594. "> 'value_2' is 123\n"
  595. );
  596. }
  597. catch (const std::exception& e)
  598. {
  599. FAIL() << "Unexpected C++ exception: " << e.what();
  600. }
  601. catch (...)
  602. {
  603. FAIL() << "Unexpected unknown exception";
  604. }
  605. }
  606. TEST(Core_Check, testGT_int_pass)
  607. {
  608. EXPECT_NO_THROW(
  609. {
  610. test_check_gt_1(1234, 123);
  611. });
  612. }
  613. void test_check_MatType_1(int src_type)
  614. {
  615. CV_CheckTypeEQ(src_type, CV_32FC1, "Unsupported source type");
  616. }
  617. TEST(Core_Check, testMatType_pass)
  618. {
  619. EXPECT_NO_THROW(
  620. {
  621. test_check_MatType_1(CV_MAKE_TYPE(CV_32F, 1));
  622. });
  623. }
  624. TEST(Core_Check, testMatType_fail_1)
  625. {
  626. try
  627. {
  628. test_check_MatType_1(CV_8UC1);
  629. FAIL() << "Unreachable code called";
  630. }
  631. catch (const cv::Exception& e)
  632. {
  633. EXPECT_STREQ(e.err.c_str(),
  634. "> Unsupported source type (expected: 'src_type == CV_32FC1'), where\n"
  635. "> 'src_type' is 0 (CV_8UC1)\n"
  636. "> must be equal to\n"
  637. "> 'CV_32FC1' is 5 (CV_32FC1)\n"
  638. );
  639. }
  640. catch (const std::exception& e)
  641. {
  642. FAIL() << "Unexpected C++ exception: " << e.what();
  643. }
  644. catch (...)
  645. {
  646. FAIL() << "Unexpected unknown exception";
  647. }
  648. }
  649. void test_check_MatType_2(int src_type)
  650. {
  651. CV_CheckType(src_type, src_type == CV_32FC1 || src_type == CV_32FC3, "Unsupported src");
  652. }
  653. TEST(Core_Check, testMatType_fail_2)
  654. {
  655. try
  656. {
  657. test_check_MatType_2(CV_8UC1);
  658. FAIL() << "Unreachable code called";
  659. }
  660. catch (const cv::Exception& e)
  661. {
  662. EXPECT_STREQ(e.err.c_str(),
  663. "> Unsupported src:\n"
  664. "> 'src_type == CV_32FC1 || src_type == CV_32FC3'\n"
  665. "> where\n"
  666. "> 'src_type' is 0 (CV_8UC1)\n"
  667. );
  668. }
  669. catch (const std::exception& e)
  670. {
  671. FAIL() << "Unexpected C++ exception: " << e.what();
  672. }
  673. catch (...)
  674. {
  675. FAIL() << "Unexpected unknown exception";
  676. }
  677. }
  678. void test_check_MatDepth_1(int src_depth)
  679. {
  680. CV_CheckDepthEQ(src_depth, CV_32F, "Unsupported source depth");
  681. }
  682. TEST(Core_Check, testMatDepth_pass)
  683. {
  684. EXPECT_NO_THROW(
  685. {
  686. test_check_MatDepth_1(CV_MAKE_TYPE(CV_32F, 1));
  687. });
  688. }
  689. TEST(Core_Check, testMatDepth_fail_1)
  690. {
  691. try
  692. {
  693. test_check_MatDepth_1(CV_8U);
  694. FAIL() << "Unreachable code called";
  695. }
  696. catch (const cv::Exception& e)
  697. {
  698. EXPECT_STREQ(e.err.c_str(),
  699. "> Unsupported source depth (expected: 'src_depth == CV_32F'), where\n"
  700. "> 'src_depth' is 0 (CV_8U)\n"
  701. "> must be equal to\n"
  702. "> 'CV_32F' is 5 (CV_32F)\n"
  703. );
  704. }
  705. catch (const std::exception& e)
  706. {
  707. FAIL() << "Unexpected C++ exception: " << e.what();
  708. }
  709. catch (...)
  710. {
  711. FAIL() << "Unexpected unknown exception";
  712. }
  713. }
  714. void test_check_MatDepth_2(int src_depth)
  715. {
  716. CV_CheckDepth(src_depth, src_depth == CV_32F || src_depth == CV_64F, "Unsupported src");
  717. }
  718. TEST(Core_Check, testMatDepth_fail_2)
  719. {
  720. try
  721. {
  722. test_check_MatDepth_2(CV_8U);
  723. FAIL() << "Unreachable code called";
  724. }
  725. catch (const cv::Exception& e)
  726. {
  727. EXPECT_STREQ(e.err.c_str(),
  728. "> Unsupported src:\n"
  729. "> 'src_depth == CV_32F || src_depth == CV_64F'\n"
  730. "> where\n"
  731. "> 'src_depth' is 0 (CV_8U)\n"
  732. );
  733. }
  734. catch (const std::exception& e)
  735. {
  736. FAIL() << "Unexpected C++ exception: " << e.what();
  737. }
  738. catch (...)
  739. {
  740. FAIL() << "Unexpected unknown exception";
  741. }
  742. }
  743. void test_check_Size_1(const Size& srcSz)
  744. {
  745. CV_Check(srcSz, srcSz == Size(4, 3), "Unsupported src size");
  746. }
  747. TEST(Core_Check, testSize_1)
  748. {
  749. try
  750. {
  751. test_check_Size_1(Size(2, 1));
  752. FAIL() << "Unreachable code called";
  753. }
  754. catch (const cv::Exception& e)
  755. {
  756. EXPECT_STREQ(e.err.c_str(),
  757. "> Unsupported src size:\n"
  758. "> 'srcSz == Size(4, 3)'\n"
  759. "> where\n"
  760. "> 'srcSz' is [2 x 1]\n"
  761. );
  762. }
  763. catch (const std::exception& e)
  764. {
  765. FAIL() << "Unexpected C++ exception: " << e.what();
  766. }
  767. catch (...)
  768. {
  769. FAIL() << "Unexpected unknown exception";
  770. }
  771. }
  772. TEST(Core_Allocation, alignedAllocation)
  773. {
  774. // iterate from size=1 to approximate byte size of 8K 32bpp image buffer
  775. for (int i = 0; i < 200; i++) {
  776. const size_t size = static_cast<size_t>(std::pow(1.091, (double)i));
  777. void * const buf = cv::fastMalloc(size);
  778. ASSERT_NE((uintptr_t)0, (uintptr_t)buf)
  779. << "failed to allocate memory";
  780. ASSERT_EQ((uintptr_t)0, (uintptr_t)buf % CV_MALLOC_ALIGN)
  781. << "memory not aligned to " << CV_MALLOC_ALIGN;
  782. cv::fastFree(buf);
  783. }
  784. }
  785. #if !(defined(__GNUC__) && __GNUC__ < 5) // GCC 4.8 emits: 'is_trivially_copyable' is not a member of 'std'
  786. TEST(Core_Types, trivially_copyable)
  787. {
  788. EXPECT_TRUE(std::is_trivially_copyable<cv::Complexd>::value);
  789. EXPECT_TRUE(std::is_trivially_copyable<cv::Point>::value);
  790. EXPECT_TRUE(std::is_trivially_copyable<cv::Point3f>::value);
  791. EXPECT_TRUE(std::is_trivially_copyable<cv::Size>::value);
  792. EXPECT_TRUE(std::is_trivially_copyable<cv::Range>::value);
  793. EXPECT_TRUE(std::is_trivially_copyable<cv::Rect>::value);
  794. EXPECT_TRUE(std::is_trivially_copyable<cv::RotatedRect>::value);
  795. //EXPECT_TRUE(std::is_trivially_copyable<cv::Scalar>::value); // derived from Vec (Matx)
  796. }
  797. TEST(Core_Types, trivially_copyable_extra)
  798. {
  799. EXPECT_TRUE(std::is_trivially_copyable<cv::KeyPoint>::value);
  800. EXPECT_TRUE(std::is_trivially_copyable<cv::DMatch>::value);
  801. EXPECT_TRUE(std::is_trivially_copyable<cv::TermCriteria>::value);
  802. EXPECT_TRUE(std::is_trivially_copyable<cv::Moments>::value);
  803. }
  804. #endif
  805. template <typename T> class Rect_Test : public testing::Test {};
  806. TYPED_TEST_CASE_P(Rect_Test);
  807. // Reimplement C++11 std::numeric_limits<>::lowest.
  808. template<typename T> T cv_numeric_limits_lowest();
  809. template<> int cv_numeric_limits_lowest<int>() { return INT_MIN; }
  810. template<> float cv_numeric_limits_lowest<float>() { return -FLT_MAX; }
  811. template<> double cv_numeric_limits_lowest<double>() { return -DBL_MAX; }
  812. TYPED_TEST_P(Rect_Test, Overflows) {
  813. typedef Rect_<TypeParam> R;
  814. TypeParam num_max = std::numeric_limits<TypeParam>::max();
  815. TypeParam num_lowest = cv_numeric_limits_lowest<TypeParam>();
  816. EXPECT_EQ(R(0, 0, 10, 10), R(0, 0, 10, 10) & R(0, 0, 10, 10));
  817. EXPECT_EQ(R(5, 6, 4, 3), R(0, 0, 10, 10) & R(5, 6, 4, 3));
  818. EXPECT_EQ(R(5, 6, 3, 2), R(0, 0, 8, 8) & R(5, 6, 4, 3));
  819. // Test with overflowing dimenions.
  820. EXPECT_EQ(R(5, 0, 5, 10), R(0, 0, 10, 10) & R(5, 0, num_max, num_max));
  821. // Test with overflowing dimensions for floats/doubles.
  822. EXPECT_EQ(R(num_max, 0, num_max / 4, 10), R(num_max, 0, num_max / 2, 10) & R(num_max, 0, num_max / 4, 10));
  823. // Test with overflowing coordinates.
  824. EXPECT_EQ(R(), R(20, 0, 10, 10) & R(num_lowest, 0, 10, 10));
  825. EXPECT_EQ(R(), R(20, 0, 10, 10) & R(0, num_lowest, 10, 10));
  826. EXPECT_EQ(R(), R(num_lowest, 0, 10, 10) & R(0, num_lowest, 10, 10));
  827. }
  828. REGISTER_TYPED_TEST_CASE_P(Rect_Test, Overflows);
  829. typedef ::testing::Types<int, float, double> RectTypes;
  830. INSTANTIATE_TYPED_TEST_CASE_P(Negative_Test, Rect_Test, RectTypes);
  831. }} // namespace