From 790ef21a6005894d55468dd92a548f8ac6d82f72 Mon Sep 17 00:00:00 2001 From: Youssef Kashef Date: Fri, 5 Jun 2015 11:46:16 +0200 Subject: [PATCH] add unit test around Mat::push_back() add template specialization Mat::push_back() for MatExpr paramters extend push_back MatExpr to mat in unit test cast to object instead of reference test with multi-row MatExpr input --- modules/core/include/opencv2/core/mat.inl.hpp | 6 +++ modules/core/test/test_mat.cpp | 46 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/modules/core/include/opencv2/core/mat.inl.hpp b/modules/core/include/opencv2/core/mat.inl.hpp index 3779b83f8..c27152c5d 100644 --- a/modules/core/include/opencv2/core/mat.inl.hpp +++ b/modules/core/include/opencv2/core/mat.inl.hpp @@ -1096,6 +1096,12 @@ void Mat::push_back(const Mat_<_Tp>& m) push_back((const Mat&)m); } +template<> inline +void Mat::push_back(const MatExpr& expr) +{ + push_back(static_cast(expr)); +} + ///////////////////////////// MatSize //////////////////////////// inline diff --git a/modules/core/test/test_mat.cpp b/modules/core/test/test_mat.cpp index 897ac40a4..4bd5aa499 100644 --- a/modules/core/test/test_mat.cpp +++ b/modules/core/test/test_mat.cpp @@ -1189,6 +1189,52 @@ TEST(Core_Mat, reshape_1942) ASSERT_EQ(1, cn); } +TEST(Core_Mat, push_back) +{ + Mat a = (Mat_(1,2) << 3.4884074f, 1.4159607f); + Mat b = (Mat_(1,2) << 0.78737736f, 2.3456569f); + + a.push_back(b); + + ASSERT_EQ(2, a.cols); + ASSERT_EQ(2, a.rows); + + ASSERT_FLOAT_EQ(3.4884074f, a.at(0, 0)); + ASSERT_FLOAT_EQ(1.4159607f, a.at(0, 1)); + ASSERT_FLOAT_EQ(0.78737736f, a.at(1, 0)); + ASSERT_FLOAT_EQ(2.3456569f, a.at(1, 1)); + + Mat c = (Mat_(2,2) << -0.88010466f, 0.3009364f, 2.22399974f, -5.45933905f); + + ASSERT_EQ(c.rows, a.cols); + + a.push_back(c.t()); + + ASSERT_EQ(2, a.cols); + ASSERT_EQ(4, a.rows); + + ASSERT_FLOAT_EQ(3.4884074f, a.at(0, 0)); + ASSERT_FLOAT_EQ(1.4159607f, a.at(0, 1)); + ASSERT_FLOAT_EQ(0.78737736f, a.at(1, 0)); + ASSERT_FLOAT_EQ(2.3456569f, a.at(1, 1)); + ASSERT_FLOAT_EQ(-0.88010466f, a.at(2, 0)); + ASSERT_FLOAT_EQ(2.22399974f, a.at(2, 1)); + ASSERT_FLOAT_EQ(0.3009364f, a.at(3, 0)); + ASSERT_FLOAT_EQ(-5.45933905f, a.at(3, 1)); + + a.push_back(Mat::ones(2, 2, CV_32FC1)); + + ASSERT_EQ(6, a.rows); + + for(int row=4; row(row, col)); + } + } +} + TEST(Core_Mat, copyNx1ToVector) { cv::Mat_ src(5, 1);