1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-21 19:33:03 +04:00

Merge pull request #26255 from vpisarev:test_for_countnonzero_1d

added comprehensive test for countNonZero run on continuous and discontinuous 1D arrays
This commit is contained in:
Alexander Smorkalov
2024-10-07 17:11:24 +03:00
committed by GitHub
+45
View File
@@ -293,4 +293,49 @@ INSTANTIATE_TEST_CASE_P(Core, CountNonZeroBig,
)
);
typedef testing::TestWithParam<int> CountNonZero1D;
TEST_P(CountNonZero1D, /**/)
{
const int depth = GetParam();
int i, M = 112 + depth, N = 3 + depth;
std::vector<uint8_t> v(M);
int nz_ref = 0;
for (i = 0; i < M; i++) {
v[i] = (uint8_t)(rand() % 7 == 0);
nz_ref += v[i] != 0;
}
Mat mv;
Mat(v).convertTo(mv, depth);
EXPECT_EQ(mv.dims, 1);
size_t esz = mv.elemSize();
// check countNonZero on a vector transformed to Mat inplace, e.g. on 1xM matrix
int nz0 = countNonZero(mv);
EXPECT_EQ(nz0, nz_ref);
// another method to get 1xM matrix, this time 2D matrix
int nz0_ = countNonZero(Mat(Size(M, 1), depth, mv.data));
EXPECT_EQ(nz0_, nz_ref);
// let's now transpose it and get Mx1
Mat m1 = mv.t();
int nz1 = countNonZero(m1);
EXPECT_EQ(nz1, nz_ref);
Mat mwide(M, N, mv.type());
randu(mwide, 0, 3);
int colidx = rand()%N;
Mat mcol = mwide.col(colidx);
EXPECT_EQ(mcol.data, mwide.data + colidx*esz);
// let's now embed this column into a wider matrix
// make sure it's copied inside, not reallocated.
m1.copyTo(mcol);
EXPECT_EQ(mcol.data, mwide.data + colidx*esz);
// now it's not continuous
EXPECT_EQ(mcol.isContinuous(), false);
int nz2 = countNonZero(mcol);
EXPECT_EQ(nz2, nz_ref);
}
INSTANTIATE_TEST_CASE_P(Core, CountNonZero1D,
testing::Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32U, CV_32S, CV_64U, CV_64S, CV_32F, CV_64F, CV_16F, CV_16BF, CV_Bool)
);
}} // namespace