Skip to content

Commit

Permalink
[core] reading mm-files discards extra characters in row
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcelKoch committed Jun 19, 2024
1 parent cfa695f commit 9a60b3b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
7 changes: 7 additions & 0 deletions core/base/mtx_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ namespace {
}


constexpr auto max_streamsize = std::numeric_limits<std::streamsize>::max();


/**
* The mtx_io class provides the functionality of reading and writing matrix
* market format files.
Expand Down Expand Up @@ -516,6 +519,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);
content.ignore(max_streamsize,
'\n'); // discards rest of the line
}
return data;
}
Expand Down Expand Up @@ -584,6 +589,8 @@ class mtx_io {
std::to_string(row) + " ," +
std::to_string(col));
modifier->insert_entry(row, col, entry, data);
content.ignore(max_streamsize,
'\n'); // discards rest of the line
}
}
return data;
Expand Down
50 changes: 49 additions & 1 deletion core/test/base/mtx_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,32 @@ TEST(MtxReader, ReadsDenseComplexFloatMtxWith64Index)
}


TEST(MtxReader, ReadsDenseIgnoresExtraCharactersInRow)
{
using tpl = gko::matrix_data<double, gko::int32>::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<double, gko::int32>(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<double, gko::int32>::nonzero_type;
Expand Down Expand Up @@ -390,7 +416,29 @@ TEST(MtxReader, ReadsSparseComplexHermitianMtx)
}


TEST(MtxReader, ReadIgnoresExtraCharacters)
TEST(MtxReader, ReadsSparseIgnoresExtraCharactersInRow)
{
using tpl = gko::matrix_data<double, gko::int32>::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<double, gko::int32>(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<double, gko::int32>::nonzero_type;
std::istringstream iss(
Expand Down

0 comments on commit 9a60b3b

Please sign in to comment.