test_misc.cpp 24 KB

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