Skip to content

Commit 705e4be

Browse files
committed
Make Blaze tests typed, test std::complex
1 parent 568cfcc commit 705e4be

File tree

2 files changed

+142
-113
lines changed

2 files changed

+142
-113
lines changed

tests/blaze_test.cpp

Lines changed: 137 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919
#pragma clang diagnostic pop
2020
#endif
2121

22+
#if defined(__clang__)
23+
// for TYPED_TEST_SUITE
24+
#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
25+
#endif
2226

23-
using VT = double;
24-
27+
using ValueTypes = ::testing::Types<int64_t, float, double, std::complex<double>, long double>;
2528

2629
template <typename MatType>
2730
MatType read_mtx(const std::string& source, typename MatType::ElementType pattern_value = 1) {
@@ -48,9 +51,63 @@ std::string write_mtx(MatType& mat, bool pattern_only=false) {
4851
return oss.str();
4952
}
5053

54+
/**
55+
* Equality check that prints the matrices if they're unequal. Useful for debugging when remote tests fail.
56+
*/
57+
template <typename LHS, typename RHS, typename std::enable_if<blaze::IsMatrix_v<LHS>, int>::type = 0>
58+
bool is_equal(const LHS& lhs, const RHS& rhs) {
59+
if (lhs.rows() != rhs.rows() || lhs.columns() != rhs.columns()) {
60+
return false;
61+
}
5162

63+
bool equal = true;
64+
for (size_t row = 0; row < rhs.rows(); ++row) {
65+
for (size_t col = 0; col < rhs.columns(); ++col) {
66+
if (rhs(row, col) != lhs(row, col)) {
67+
equal = false;
68+
}
69+
}
70+
}
71+
72+
if (!equal) {
73+
std::cout << "Left:" << std::endl << lhs.rows() << "-by-" << lhs.columns() << std::endl << lhs << std::endl;
74+
std::cout << "Right:" << std::endl << rhs.rows() << "-by-" << rhs.columns() << std::endl << rhs << std::endl
75+
<< std::endl;
76+
}
77+
78+
return equal;
79+
}
80+
81+
/**
82+
* Equality check that prints the vectors if they're unequal. Useful for debugging when remote tests fail.
83+
*/
84+
template <typename LHS, typename RHS, typename std::enable_if<blaze::IsVector_v<LHS>, int>::type = 0>
85+
bool is_equal(const LHS& lhs, const RHS& rhs) {
86+
bool equal = true;
87+
if (lhs.size() != rhs.size()) {
88+
equal = false;
89+
}
5290

53-
class BlazeTest : public ::testing::TestWithParam<std::string> {
91+
if (equal) {
92+
for (size_t row = 0; row < rhs.size(); ++row) {
93+
if (rhs[row] != lhs[row]) {
94+
equal = false;
95+
}
96+
}
97+
}
98+
99+
if (!equal) {
100+
std::cout << "Left:" << std::endl << "len=" << lhs.size() << " sparse=" << blaze::IsSparseVector_v<LHS> << std::endl << lhs << std::endl;
101+
std::cout << "Right:" << std::endl << "len=" << rhs.size() << " sparse=" << blaze::IsSparseVector_v<RHS> << std::endl << rhs << std::endl << std::endl;
102+
}
103+
104+
return equal;
105+
}
106+
107+
TYPED_TEST_SUITE(BlazeMatrixTest, ValueTypes);
108+
109+
template <typename VT>
110+
class BlazeMatrixTest : public testing::Test {
54111
public:
55112
void load(const std::string& param) {
56113
std::string load_path = kTestMatrixDir + "/" + param;
@@ -74,81 +131,69 @@ class BlazeTest : public ::testing::TestWithParam<std::string> {
74131

75132
}
76133

77-
/**
78-
* Equality check that prints the matrices if they're unequal. Useful for debugging when remote tests fail.
79-
*/
80-
template <typename LHS, typename RHS>
81-
static bool is_equal(const LHS& lhs, const RHS& rhs) {
82-
if (lhs.rows() != rhs.rows() || lhs.columns() != rhs.columns()) {
83-
return false;
84-
}
85-
86-
bool equal = true;
87-
for (size_t row = 0; row < rhs.rows(); ++row) {
88-
for (size_t col = 0; col < rhs.columns(); ++col) {
89-
if (rhs(row, col) != lhs(row, col)) {
90-
equal = false;
91-
}
92-
}
93-
}
94-
95-
if (!equal) {
96-
std::cout << "Left:" << std::endl << lhs.rows() << "-by-" << lhs.columns() << std::endl << lhs << std::endl;
97-
std::cout << "Right:" << std::endl << rhs.rows() << "-by-" << rhs.columns() << std::endl << rhs << std::endl
98-
<< std::endl;
99-
}
100-
101-
return equal;
102-
}
103-
104134
protected:
105135
blaze::CompressedMatrix<VT, blaze::columnMajor> col_major;
106-
blaze::CompressedMatrix<double, blaze::rowMajor> row_major;
136+
blaze::CompressedMatrix<VT, blaze::rowMajor> row_major;
107137

108138
blaze::DynamicMatrix<VT, blaze::columnMajor> dense_col_major;
109139
blaze::DynamicMatrix<VT, blaze::rowMajor> dense_row_major;
110140
};
111141

112-
TEST_P(BlazeTest, SmallMatrices) {
113-
EXPECT_NO_THROW(load(GetParam()));
142+
TYPED_TEST(BlazeMatrixTest, SmallMatrices) {
143+
auto filenames = std::vector{
144+
"eye3.mtx"
145+
, "row_3by4.mtx"
146+
, "kepner_gilbert_graph.mtx"
147+
, "vector_array.mtx"
148+
, "vector_coordinate.mtx"
149+
, "eye3_pattern.mtx"
150+
};
151+
if (fast_matrix_market::is_complex<TypeParam>::value) {
152+
filenames.emplace_back("eye3_complex.mtx");
153+
filenames.emplace_back("vector_coordinate_complex.mtx");
154+
}
155+
for (auto param : filenames) {
156+
EXPECT_NO_THROW(this->load(param));
157+
158+
auto& col_major = this->col_major;
159+
auto& row_major = this->row_major;
160+
auto& dense_col_major = this->dense_col_major;
161+
auto& dense_row_major = this->dense_row_major;
114162

115-
EXPECT_TRUE(is_equal(col_major, row_major));
163+
EXPECT_TRUE(is_equal(col_major, row_major));
116164

117-
EXPECT_EQ(col_major.nonZeros(), row_major.nonZeros());
165+
EXPECT_EQ(col_major.nonZeros(), row_major.nonZeros());
118166

119-
EXPECT_TRUE(is_equal(dense_col_major, dense_row_major));
120-
EXPECT_TRUE(is_equal(col_major, dense_col_major));
167+
EXPECT_TRUE(is_equal(dense_col_major, dense_row_major));
168+
EXPECT_TRUE(is_equal(col_major, dense_col_major));
121169

122-
// Read/Write into a copy
123-
using Dense = blaze::DynamicMatrix<VT, blaze::rowMajor>;
124-
using Sparse = blaze::CompressedMatrix<VT, blaze::columnMajor>;
125-
EXPECT_TRUE(is_equal(col_major, read_mtx<Sparse>(write_mtx(row_major))));
126-
EXPECT_TRUE(is_equal(col_major, read_mtx<Sparse>(write_mtx(col_major))));
127-
EXPECT_TRUE(is_equal(dense_row_major, read_mtx<Dense>(write_mtx(row_major))));
128-
EXPECT_TRUE(is_equal(dense_row_major, read_mtx<Dense>(write_mtx(dense_row_major))));
129-
EXPECT_TRUE(is_equal(dense_row_major, read_mtx<Dense>(write_mtx(dense_col_major))));
170+
// Read/Write into a copy
171+
using Dense = blaze::DynamicMatrix<TypeParam, blaze::rowMajor>;
172+
using Sparse = blaze::CompressedMatrix<TypeParam, blaze::columnMajor>;
173+
EXPECT_TRUE(is_equal(col_major, read_mtx<Sparse>(write_mtx(row_major))));
174+
EXPECT_TRUE(is_equal(col_major, read_mtx<Sparse>(write_mtx(col_major))));
175+
EXPECT_TRUE(is_equal(dense_row_major, read_mtx<Dense>(write_mtx(row_major))));
176+
EXPECT_TRUE(is_equal(dense_row_major, read_mtx<Dense>(write_mtx(dense_row_major))));
177+
EXPECT_TRUE(is_equal(dense_row_major, read_mtx<Dense>(write_mtx(dense_col_major))));
130178

131-
// write pattern
132-
std::string pattern_mtx = write_mtx(col_major, true);
133-
EXPECT_TRUE(pattern_mtx.find("pattern") > 0); // pattern should appear in the header
179+
// write pattern
180+
std::string pattern_mtx = write_mtx(col_major, true);
181+
EXPECT_TRUE(pattern_mtx.find("pattern") > 0); // pattern should appear in the header
134182

135-
auto sparse_pat = read_mtx<Sparse>(pattern_mtx, 1);
136-
auto dense_pat = read_mtx<Dense>(pattern_mtx, 1);
137-
EXPECT_TRUE(is_equal(dense_pat, sparse_pat));
183+
auto sparse_pat = read_mtx<Sparse>(pattern_mtx, 1);
184+
auto dense_pat = read_mtx<Dense>(pattern_mtx, 1);
185+
EXPECT_TRUE(is_equal(dense_pat, sparse_pat));
186+
}
138187
}
139188

140-
INSTANTIATE_TEST_SUITE_P(
141-
BlazeTest,
142-
BlazeTest,
143-
::testing::Values("eye3.mtx"
144-
, "row_3by4.mtx"
145-
, "kepner_gilbert_graph.mtx"
146-
, "vector_array.mtx"
147-
, "vector_coordinate.mtx"
148-
, "eye3_pattern.mtx"
149-
));
150-
151-
class BlazeVectorTest : public ::testing::TestWithParam<std::string> {
189+
/////////////////////////////////////////////////////////
190+
///// Vectors
191+
/////////////////////////////////////////////////////////
192+
193+
TYPED_TEST_SUITE(BlazeVectorTest, ValueTypes);
194+
195+
template <typename VT>
196+
class BlazeVectorTest : public testing::Test {
152197
public:
153198
void load(const std::string& param) {
154199
std::string load_path = kTestMatrixDir + "/" + param;
@@ -163,64 +208,43 @@ class BlazeVectorTest : public ::testing::TestWithParam<std::string> {
163208
}
164209
}
165210

166-
/**
167-
* Equality check that prints the matrices if they're unequal. Useful for debugging when remote tests fail.
168-
*/
169-
template <typename LHS, typename RHS>
170-
static bool is_equal(const LHS& lhs, const RHS& rhs) {
171-
bool equal = true;
172-
if (lhs.size() != rhs.size()) {
173-
equal = false;
174-
}
175-
176-
if (equal) {
177-
for (size_t row = 0; row < rhs.size(); ++row) {
178-
if (rhs[row] != lhs[row]) {
179-
equal = false;
180-
}
181-
}
182-
}
183-
184-
if (!equal) {
185-
std::cout << "Left:" << std::endl << "len=" << lhs.size() << " sparse=" << blaze::IsSparseVector_v<LHS> << std::endl << lhs << std::endl;
186-
std::cout << "Right:" << std::endl << "len=" << rhs.size() << " sparse=" << blaze::IsSparseVector_v<RHS> << std::endl << rhs << std::endl << std::endl;
187-
}
188-
189-
return equal;
190-
}
191-
192211
protected:
193212
blaze::CompressedVector<VT> sparse;
194213
blaze::DynamicVector<VT> dense;
195214
};
196215

197-
TEST_P(BlazeVectorTest, SmallVectors) {
198-
EXPECT_NO_THROW(load(GetParam()));
216+
TYPED_TEST(BlazeVectorTest, SmallVectors) {
217+
auto filenames = std::vector{
218+
"vector_array.mtx"
219+
, "vector_coordinate.mtx"
220+
};
221+
if (fast_matrix_market::is_complex<TypeParam>::value) {
222+
filenames.emplace_back("vector_coordinate_complex.mtx");
223+
}
224+
for (auto param : filenames) {
225+
EXPECT_NO_THROW(this->load(param));
226+
227+
auto& sparse = this->sparse;
228+
auto& dense = this->dense;
199229

200-
EXPECT_TRUE(is_equal(sparse, dense));
230+
EXPECT_TRUE(is_equal(sparse, dense));
201231

202-
EXPECT_GT(sparse.nonZeros(), 0);
232+
EXPECT_GT(sparse.nonZeros(), 0);
203233

204-
// Read/Write into a copy
205-
using Dense = blaze::DynamicVector<VT>;
206-
using Sparse = blaze::CompressedVector<VT>;
207-
EXPECT_TRUE(is_equal(sparse, read_mtx<Sparse>(write_mtx(sparse))));
208-
EXPECT_TRUE(is_equal(sparse, read_mtx<Sparse>(write_mtx(dense))));
209-
EXPECT_TRUE(is_equal(dense, read_mtx<Dense>(write_mtx(sparse))));
210-
EXPECT_TRUE(is_equal(dense, read_mtx<Dense>(write_mtx(dense))));
234+
// Read/Write into a copy
235+
using Dense = blaze::DynamicVector<TypeParam>;
236+
using Sparse = blaze::CompressedVector<TypeParam>;
237+
EXPECT_TRUE(is_equal(sparse, read_mtx<Sparse>(write_mtx(sparse))));
238+
EXPECT_TRUE(is_equal(sparse, read_mtx<Sparse>(write_mtx(dense))));
239+
EXPECT_TRUE(is_equal(dense, read_mtx<Dense>(write_mtx(sparse))));
240+
EXPECT_TRUE(is_equal(dense, read_mtx<Dense>(write_mtx(dense))));
211241

212-
// write pattern
213-
std::string pattern_mtx = write_mtx(sparse, true);
214-
EXPECT_TRUE(pattern_mtx.find("pattern") > 0); // pattern should appear in the header
242+
// write pattern
243+
std::string pattern_mtx = write_mtx(sparse, true);
244+
EXPECT_TRUE(pattern_mtx.find("pattern") > 0); // pattern should appear in the header
215245

216-
auto sparse_pat = read_mtx<Sparse>(pattern_mtx, 1);
217-
auto dense_pat = read_mtx<Dense>(pattern_mtx, 1);
218-
EXPECT_TRUE(is_equal(dense_pat, sparse_pat));
246+
auto sparse_pat = read_mtx<Sparse>(pattern_mtx, 1);
247+
auto dense_pat = read_mtx<Dense>(pattern_mtx, 1);
248+
EXPECT_TRUE(is_equal(dense_pat, sparse_pat));
249+
}
219250
}
220-
221-
INSTANTIATE_TEST_SUITE_P(
222-
BlazeVectorTest,
223-
BlazeVectorTest,
224-
::testing::Values("vector_array.mtx"
225-
, "vector_coordinate.mtx"
226-
));
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
%%MatrixMarket vector coordinate complex general
2+
4 3
3+
1 101 0
4+
2 202 1
5+
4 404 0

0 commit comments

Comments
 (0)