Skip to content

Commit 60bc961

Browse files
committed
split each test case
1 parent e9cc229 commit 60bc961

File tree

1 file changed

+170
-121
lines changed

1 file changed

+170
-121
lines changed

modules/cudev/test/test_nd.cu

+170-121
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,6 @@ using namespace cv;
44
using namespace cv::cuda;
55
using namespace cvtest;
66

7-
using ElemTypes = ::testing::Types<
8-
Vec<uchar, 1>, Vec<uchar, 2>, Vec<uchar, 3>, Vec<uchar, 4>, // CV_8U
9-
Vec<schar, 1>, Vec<schar, 2>, Vec<schar, 3>, Vec<schar, 4>, // CV_8S
10-
Vec<ushort, 1>, Vec<ushort, 2>, Vec<ushort, 3>, Vec<ushort, 4>, // CV_16U
11-
Vec<short, 1>, Vec<short, 2>, Vec<short, 3>, Vec<short, 4>, // CV_16S
12-
Vec<int, 1>, Vec<int, 2>, Vec<int, 3>, Vec<int, 4>, // CV_32S
13-
Vec<float, 1>, Vec<float, 2>, Vec<float, 3>, Vec<float, 4>, // CV_32F
14-
Vec<double, 1>, Vec<double, 2>, Vec<double, 3>, Vec<double, 4>, //CV_64F
15-
Vec<float16_t, 1>, Vec<float16_t, 2>, Vec<float16_t, 3>, Vec<float16_t, 4> // CV_16F
16-
>;
17-
187
template <typename ElemType>
198
class GpuMatNDTest : public ::testing::Test
209
{
@@ -24,175 +13,235 @@ public:
2413
static constexpr int cn = DataType<ElemType>::channels;
2514
using SizeArray = GpuMatND::SizeArray;
2615

27-
static void doTest(const SizeArray& size)
16+
static MatType RandomMat(const SizeArray& size)
2817
{
29-
const int dims = static_cast<int>(size.size());
18+
const auto dims = static_cast<int>(size.size());
3019

31-
MatType gold(dims, size.data());
32-
MatType dst;
20+
MatType ret(dims, size.data());
3321

34-
// init gold with random data
35-
for (ElemType& elem : gold)
22+
for (ElemType& elem : ret)
3623
for (int i = 0; i < cn; ++i)
3724
elem[i] = static_cast<CnType>(std::rand());
3825

39-
GpuMatND gmat;
26+
return ret;
27+
}
4028

41-
{
42-
// simple upload, download test for GpuMatND
29+
static std::vector<Range> RandomRange(const SizeArray& size)
30+
{
31+
const auto dims = static_cast<int>(size.size());
4332

44-
gmat.upload(gold);
45-
gmat.download(dst);
33+
std::vector<Range> ret;
4634

47-
EXPECT_TRUE(std::equal(gold.begin(), gold.end(), dst.begin()));
48-
}
35+
const auto margin = std::rand() % 2 + 1; // 1 or 2
4936

50-
std::vector<Range> ranges;
5137
for (int s : size)
52-
if (s > 2)
53-
ranges.emplace_back(1, s-1);
38+
if (s > margin * 2)
39+
ret.emplace_back(margin, s-margin);
5440
else
55-
ranges.push_back(Range::all());
41+
ret.push_back(Range::all());
5642

5743
if (dims == 1)
5844
{
59-
std::vector<Range> ranges_1dfix = ranges;
60-
ranges_1dfix.push_back(Range::all());
61-
6245
// Mat expects two ranges even in this case
63-
ranges = std::move(ranges_1dfix);
46+
ret.push_back(Range::all());
6447
}
6548

66-
MatType goldSub = gold(ranges);
49+
return ret;
50+
}
6751

68-
{
69-
// upload partial mat, download it, and compare
52+
static std::vector<Range> RandomRange2D(const SizeArray& size)
53+
{
54+
const auto dims = static_cast<int>(size.size());
7055

71-
gmat.upload(goldSub);
72-
gmat.download(dst);
56+
std::vector<Range> ret = RandomRange(size);
7357

74-
EXPECT_TRUE(std::equal(goldSub.begin(), goldSub.end(), dst.begin()));
58+
for (int i = 0; i < dims - 2; ++i)
59+
{
60+
const int start = std::rand() % size[i];
61+
ret[i] = Range(start, start + 1);
7562
}
7663

77-
{
78-
// upload full mat, extract partial mat from it, download it, and compare
64+
return ret;
65+
}
7966

80-
gmat.upload(gold);
81-
gmat = gmat(ranges);
82-
gmat.download(dst);
67+
static void doTest1(const SizeArray& size)
68+
{
69+
const MatType gold = RandomMat(size);
8370

84-
EXPECT_TRUE(std::equal(goldSub.begin(), goldSub.end(), dst.begin()));
85-
}
71+
MatType dst;
72+
GpuMatND gmat;
8673

87-
if (std::is_same<CnType, float16_t>::value)
88-
{
89-
// The rest of the test uses GpuMat::convertTo
90-
// but currently it is not implemented for CV_16F
91-
return;
92-
}
74+
// simple upload, download test for GpuMatND
75+
gmat.upload(gold);
76+
gmat.download(dst);
77+
EXPECT_TRUE(std::equal(gold.begin(), gold.end(), dst.begin()));
78+
}
9379

94-
{
95-
// Test GpuMatND to GpuMat conversion:
80+
static void doTest2(const SizeArray& size)
81+
{
82+
const MatType gold = RandomMat(size);
83+
const std::vector<Range> ranges = RandomRange(size);
84+
const MatType goldSub = gold(ranges);
85+
86+
MatType dst;
87+
GpuMatND gmat;
88+
89+
// upload partial mat, download it, and compare
90+
gmat.upload(goldSub);
91+
gmat.download(dst);
92+
EXPECT_TRUE(std::equal(goldSub.begin(), goldSub.end(), dst.begin()));
9693

97-
// extract a 2D-plane and set its elements in the extracted region to 1
98-
// compare the values of the full mat between Mat and GpuMatND
94+
// upload full mat, extract partial mat from it, download it, and compare
95+
gmat.upload(gold);
96+
gmat = gmat(ranges);
97+
gmat.download(dst);
98+
EXPECT_TRUE(std::equal(goldSub.begin(), goldSub.end(), dst.begin()));
99+
}
99100

100-
gmat.upload(gold);
101+
static void doTest3(const SizeArray& size)
102+
{
103+
if (std::is_same<CnType, float16_t>::value) // GpuMat::convertTo is not implemented for CV_16F
104+
return;
101105

102-
for (int i = 0; i < dims - 2; ++i)
103-
if (size[i] > 1)
104-
ranges[i] = Range(1, 2);
105-
else
106-
ranges[i] = Range::all();
106+
const MatType gold = RandomMat(size);
107+
const std::vector<Range> ranges = RandomRange2D(size);
107108

108-
GpuMat plane = gmat(ranges).createGpuMatHeader();
109+
MatType dst;
110+
GpuMatND gmat;
109111

110-
EXPECT_TRUE(!plane.refcount); // plane points to externally allocated memory(a part of gmat)
112+
// Test GpuMatND to GpuMat conversion:
113+
// extract a 2D-plane and set its elements in the extracted region to 1
114+
// compare the values of the full mat between Mat and GpuMatND
111115

112-
const GpuMat dummy = plane.clone();
116+
gmat.upload(gold);
117+
GpuMat plane = gmat(ranges).createGpuMatHeader();
118+
EXPECT_TRUE(!plane.refcount); // plane points to externally allocated memory(a part of gmat)
113119

114-
EXPECT_TRUE(dummy.refcount); // dummy is clone()-ed from plane, so it manages its memory
120+
const GpuMat dummy = plane.clone();
121+
EXPECT_TRUE(dummy.refcount); // dummy is clone()-ed from plane, so it manages its memory
115122

116-
// currently, plane(GpuMat) points to a sub-matrix of gmat(GpuMatND)
117-
// in this case, dummy and plane have same size and type,
118-
// so plane does not get reallocated inside convertTo,
119-
// so this convertTo sets a sub-matrix region of gmat to 1
120-
dummy.convertTo(plane, -1, 0, 1);
123+
// currently, plane(GpuMat) points to a sub-matrix of gmat(GpuMatND)
124+
// in this case, dummy and plane have same size and type,
125+
// so plane does not get reallocated inside convertTo,
126+
// so this convertTo sets a sub-matrix region of gmat to 1
127+
dummy.convertTo(plane, -1, 0, 1);
128+
EXPECT_TRUE(!plane.refcount); // plane still points to externally allocated memory(a part of gmat)
121129

122-
EXPECT_TRUE(!plane.refcount); // plane still points to externally allocated memory(a part of gmat)
130+
gmat.download(dst);
123131

124-
gmat.download(dst);
132+
// set a sub-matrix region of gold to 1
133+
Mat plane_ = gold(ranges);
134+
const Mat dummy_ = plane_.clone();
135+
dummy_.convertTo(plane_, -1, 0, 1);
125136

126-
// set a sub-matrix region of gold to 1
127-
Mat plane_ = gold(ranges);
128-
const Mat dummy_ = plane_.clone();
129-
dummy_.convertTo(plane_, -1, 0, 1);
137+
EXPECT_TRUE(std::equal(gold.begin(), gold.end(), dst.begin()));
138+
}
130139

131-
EXPECT_TRUE(std::equal(gold.begin(), gold.end(), dst.begin()));
132-
}
140+
static void doTest4(const SizeArray& size)
141+
{
142+
if (std::is_same<CnType, float16_t>::value) // GpuMat::convertTo is not implemented for CV_16F
143+
return;
133144

134-
{
135-
// Test handling external memory
145+
const MatType gold = RandomMat(size);
146+
const std::vector<Range> ranges = RandomRange2D(size);
136147

137-
GpuMatND external(gmat.size, gmat.type(), gmat.getDevicePtr(), {gmat.step.begin(), gmat.step.end() - 1});
148+
MatType dst;
149+
GpuMatND gmat;
138150

139-
for (int i = 0; i < dims - 2; ++i)
140-
ranges[i] = Range(0, 1);
151+
// Test handling external memory
152+
gmat.upload(gold);
153+
const GpuMatND external(gmat.size, gmat.type(), gmat.getDevicePtr(), {gmat.step.begin(), gmat.step.end() - 1});
141154

142-
// set a sub-matrix region of external to 2
143-
GpuMat plane = external(ranges).createGpuMatHeader();
144-
const GpuMat dummy = plane.clone();
145-
dummy.convertTo(plane, -1, 0, 2);
146-
external.download(dst);
155+
// set a sub-matrix region of external to 2
156+
GpuMat plane = external(ranges).createGpuMatHeader();
157+
const GpuMat dummy = plane.clone();
158+
dummy.convertTo(plane, -1, 0, 2);
159+
external.download(dst);
147160

148-
// set a sub-matrix region of gold to 2
149-
Mat plane_ = gold(ranges);
150-
const Mat dummy_ = plane_.clone();
151-
dummy_.convertTo(plane_, -1, 0, 2);
161+
// set a sub-matrix region of gold to 2
162+
Mat plane_ = gold(ranges);
163+
const Mat dummy_ = plane_.clone();
164+
dummy_.convertTo(plane_, -1, 0, 2);
152165

153-
EXPECT_TRUE(std::equal(gold.begin(), gold.end(), dst.begin()));
154-
}
166+
EXPECT_TRUE(std::equal(gold.begin(), gold.end(), dst.begin()));
167+
}
155168

156-
{
157-
// Upload a sub-mat, add 1 to a sub-region of the sub-mat, download, and compare
169+
static void doTest5(const SizeArray& size)
170+
{
171+
if (std::is_same<CnType, float16_t>::value) // GpuMat::convertTo is not implemented for CV_16F
172+
return;
158173

159-
gmat.upload(gold(ranges));
174+
const MatType gold = RandomMat(size);
175+
const std::vector<Range> ranges = RandomRange(size);
176+
MatType goldSub = gold(ranges);
160177

161-
MatType gold_sub = gold(ranges);
178+
MatType dst;
179+
GpuMatND gmat;
162180

163-
for (int i = 0; i < dims; ++i)
164-
if (gmat.size[i] > 2)
165-
ranges[i] = Range(1, 3);
166-
else
167-
ranges[i] = Range::all();
181+
// Upload a sub-mat, set a sub-region of the sub-mat to 3, download, and compare
182+
gmat.upload(goldSub);
183+
const std::vector<Range> rangesInRanges = RandomRange2D(gmat.size);
168184

169-
GpuMat plane = gmat(ranges).createGpuMatHeader();
170-
const GpuMat dummy = plane.clone();
171-
dummy.convertTo(plane, -1, 1, 1);
172-
gmat.download(dst);
185+
GpuMat plane = gmat(rangesInRanges).createGpuMatHeader();
186+
const GpuMat dummy = plane.clone();
187+
dummy.convertTo(plane, -1, 0, 3);
188+
gmat.download(dst);
173189

174-
Mat plane_ = gold_sub(ranges);
175-
const Mat dummy_ = plane_.clone();
176-
dummy_.convertTo(plane_, -1, 1, 1);
190+
Mat plane_ = goldSub(rangesInRanges);
191+
const Mat dummy_ = plane_.clone();
192+
dummy_.convertTo(plane_, -1, 0, 3);
177193

178-
EXPECT_TRUE(std::equal(gold_sub.begin(), gold_sub.end(), dst.begin()));
179-
}
194+
EXPECT_TRUE(std::equal(goldSub.begin(), goldSub.end(), dst.begin()));
180195
}
181196
};
182197

198+
using ElemTypes = ::testing::Types<
199+
Vec<uchar, 1>, Vec<uchar, 2>, Vec<uchar, 3>, Vec<uchar, 4>, // CV_8U
200+
Vec<schar, 1>, Vec<schar, 2>, Vec<schar, 3>, Vec<schar, 4>, // CV_8S
201+
Vec<ushort, 1>, Vec<ushort, 2>, Vec<ushort, 3>, Vec<ushort, 4>, // CV_16U
202+
Vec<short, 1>, Vec<short, 2>, Vec<short, 3>, Vec<short, 4>, // CV_16S
203+
Vec<int, 1>, Vec<int, 2>, Vec<int, 3>, Vec<int, 4>, // CV_32S
204+
Vec<float, 1>, Vec<float, 2>, Vec<float, 3>, Vec<float, 4>, // CV_32F
205+
Vec<double, 1>, Vec<double, 2>, Vec<double, 3>, Vec<double, 4>, //CV_64F
206+
Vec<float16_t, 1>, Vec<float16_t, 2>, Vec<float16_t, 3>, Vec<float16_t, 4> // CV_16F
207+
>;
208+
209+
using SizeArray = GpuMatND::SizeArray;
210+
211+
#define DIFFERENT_SIZES_ND std::vector<SizeArray>{ \
212+
SizeArray{2, 1}, SizeArray{3, 2, 1}, SizeArray{1, 3, 2, 1}, SizeArray{2, 1, 3, 2, 1}, SizeArray{3, 2, 1, 3, 2, 1}, \
213+
SizeArray{1}, SizeArray{1, 1}, SizeArray{1, 1, 1}, SizeArray{1, 1, 1, 1}, \
214+
SizeArray{4}, SizeArray{4, 4}, SizeArray{4, 4, 4}, SizeArray{4, 4, 4, 4}, \
215+
SizeArray{11}, SizeArray{13, 11}, SizeArray{17, 13, 11}, SizeArray{19, 17, 13, 11}}
216+
183217
TYPED_TEST_CASE(GpuMatNDTest, ElemTypes);
184218

185219
TYPED_TEST(GpuMatNDTest, Test1)
186220
{
187-
using SizeArray = GpuMatND::SizeArray;
221+
for (auto& size : DIFFERENT_SIZES_ND)
222+
GpuMatNDTest<TypeParam>::doTest1(size);
223+
}
224+
225+
TYPED_TEST(GpuMatNDTest, Test2)
226+
{
227+
for (auto& size : DIFFERENT_SIZES_ND)
228+
GpuMatNDTest<TypeParam>::doTest2(size);
229+
}
230+
231+
TYPED_TEST(GpuMatNDTest, Test3)
232+
{
233+
for (auto& size : DIFFERENT_SIZES_ND)
234+
GpuMatNDTest<TypeParam>::doTest3(size);
235+
}
188236

189-
std::vector<SizeArray> sizes = {
190-
SizeArray{1}, SizeArray{1, 1}, SizeArray{1, 1, 1}, SizeArray{1, 1, 1, 1},
191-
SizeArray{2}, SizeArray{2, 1}, SizeArray{1, 2}, SizeArray{1, 2, 1}, SizeArray{2, 1, 2}, SizeArray{2, 2, 2},
192-
SizeArray{64}, SizeArray{64, 64}, SizeArray{16, 64, 64}, SizeArray{4, 16, 64, 64}, SizeArray{2, 4, 16, 64, 64},
193-
SizeArray{129}, SizeArray{127, 129}, SizeArray{1, 127, 129}, SizeArray{3, 1, 127, 129}, SizeArray{7, 3, 1, 127, 129},
194-
};
237+
TYPED_TEST(GpuMatNDTest, Test4)
238+
{
239+
for (auto& size : DIFFERENT_SIZES_ND)
240+
GpuMatNDTest<TypeParam>::doTest4(size);
241+
}
195242

196-
for (auto& size : sizes)
197-
GpuMatNDTest<TypeParam>::doTest(size);
243+
TYPED_TEST(GpuMatNDTest, Test5)
244+
{
245+
for (auto& size : DIFFERENT_SIZES_ND)
246+
GpuMatNDTest<TypeParam>::doTest5(size);
198247
}

0 commit comments

Comments
 (0)