diff --git a/core/base/mtx_io.cpp b/core/base/mtx_io.cpp index c264a073f31..33c3b07d487 100644 --- a/core/base/mtx_io.cpp +++ b/core/base/mtx_io.cpp @@ -35,6 +35,9 @@ namespace { } +constexpr auto max_streamsize = std::numeric_limits::max(); + + /** * The mtx_io class provides the functionality of reading and writing matrix * market format files. @@ -514,6 +517,8 @@ class mtx_io { GKO_CHECK_STREAM(content, "error when reading matrix entry " + std::to_string(i)); modifier->insert_entry(row - 1, col - 1, entry, data); + // discards rest of the line + content.ignore(max_streamsize, '\n'); } return data; } @@ -569,7 +574,7 @@ class mtx_io { size_type num_cols{}; GKO_CHECK_STREAM( header >> num_rows >> num_cols, - "error when determining matrix size, expected: rows cols nnz"); + "error when determining matrix size, expected: rows cols"); matrix_data data(dim<2>{num_rows, num_cols}); data.nonzeros.reserve(modifier->get_reservation_size( num_rows, num_cols, num_rows * num_cols)); @@ -582,6 +587,8 @@ class mtx_io { std::to_string(row) + " ," + std::to_string(col)); modifier->insert_entry(row, col, entry, data); + // discards rest of the line + content.ignore(max_streamsize, '\n'); } } return data; diff --git a/core/test/base/mtx_io.cpp b/core/test/base/mtx_io.cpp index 66b6766b2d3..8ac1ced0e50 100644 --- a/core/test/base/mtx_io.cpp +++ b/core/test/base/mtx_io.cpp @@ -231,6 +231,32 @@ TEST(MtxReader, ReadsDenseComplexFloatMtxWith64Index) } +TEST(MtxReader, ReadsDenseIgnoresExtraCharactersInRow) +{ + using tpl = gko::matrix_data::nonzero_type; + std::istringstream iss( + "%%MatrixMarket matrix array real general\n" + "2 3 -77\n" + "1.0\n" + "0.0 58\n" + "3.0\n" + "5.0\n" + "2.0\n" + "0.0\n"); + + auto data = gko::read_raw(iss); + + ASSERT_EQ(data.size, gko::dim<2>(2, 3)); + auto& v = data.nonzeros; + ASSERT_EQ(v[0], tpl(0, 0, 1.0)); + ASSERT_EQ(v[1], tpl(0, 1, 3.0)); + ASSERT_EQ(v[2], tpl(0, 2, 2.0)); + ASSERT_EQ(v[3], tpl(1, 0, 0.0)); + ASSERT_EQ(v[4], tpl(1, 1, 5.0)); + ASSERT_EQ(v[5], tpl(1, 2, 0.0)); +} + + TEST(MtxReader, ReadsSparseRealMtx) { using tpl = gko::matrix_data::nonzero_type; @@ -385,7 +411,29 @@ TEST(MtxReader, ReadsSparseComplexHermitianMtx) } -TEST(MtxReader, ReadIgnoresExtraCharacters) +TEST(MtxReader, ReadsSparseIgnoresExtraCharactersInRow) +{ + using tpl = gko::matrix_data::nonzero_type; + std::istringstream iss( + "%%MatrixMarket matrix coordinate real general\n" + "2 3 4 abc\n" + "1 1 1.0 some value\n" + "2 2 5.0 who knows?\n" + "1 2 3.0\n" + "1 3 2.0\n"); + + auto data = gko::read_raw(iss); + + ASSERT_EQ(data.size, gko::dim<2>(2, 3)); + auto& v = data.nonzeros; + ASSERT_EQ(v[0], tpl(0, 0, 1.0)); + ASSERT_EQ(v[1], tpl(0, 1, 3.0)); + ASSERT_EQ(v[2], tpl(0, 2, 2.0)); + ASSERT_EQ(v[3], tpl(1, 1, 5.0)); +} + + +TEST(MtxReader, ReadHeaderIgnoresExtraCharacters) { using tpl = gko::matrix_data::nonzero_type; std::istringstream iss(