Skip to content

Commit

Permalink
Do not print newline after graphs with no edges
Browse files Browse the repository at this point in the history
  • Loading branch information
ifsmirnov committed Feb 5, 2018
1 parent 1674dc3 commit daf2263
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
21 changes: 19 additions & 2 deletions impl/generic_graph_inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,24 +213,41 @@ void GenericGraph::addEdge(int u, int v, const Weight& w) {
void GenericGraph::doPrintEdges(
std::ostream& out, const OutputModifier& mod) const
{
bool pendingEndline = false;
if (mod.printN) {
out << n();
if (mod.printM) {
out << " " << m();
}
out << "\n";
pendingEndline = true;
} else if (mod.printM) {
out << m() << "\n";
out << m();
pendingEndline = true;
}

if (n() == 0) {
return;
}

if (vertexWeights_.hasNonEmpty()) {
auto vertexWeights = prepareWeightArray(vertexWeights_, n());
if (pendingEndline) {
out << "\n";
}
for (int i = 0; i < n(); ++i) {
if (i > 0) {
out << " ";
}
JNGEN_PRINT_NO_MOD(vertexWeights[vertexByLabel(i)]);
}
pendingEndline = true;
}

if (m() == 0) {
return;
}

if (pendingEndline) {
out << "\n";
}

Expand Down
21 changes: 19 additions & 2 deletions jngen.h
Original file line number Diff line number Diff line change
Expand Up @@ -5902,24 +5902,41 @@ void GenericGraph::addEdge(int u, int v, const Weight& w) {
void GenericGraph::doPrintEdges(
std::ostream& out, const OutputModifier& mod) const
{
bool pendingEndline = false;
if (mod.printN) {
out << n();
if (mod.printM) {
out << " " << m();
}
out << "\n";
pendingEndline = true;
} else if (mod.printM) {
out << m() << "\n";
out << m();
pendingEndline = true;
}

if (n() == 0) {
return;
}

if (vertexWeights_.hasNonEmpty()) {
auto vertexWeights = prepareWeightArray(vertexWeights_, n());
if (pendingEndline) {
out << "\n";
}
for (int i = 0; i < n(); ++i) {
if (i > 0) {
out << " ";
}
JNGEN_PRINT_NO_MOD(vertexWeights[vertexByLabel(i)]);
}
pendingEndline = true;
}

if (m() == 0) {
return;
}

if (pendingEndline) {
out << "\n";
}

Expand Down
33 changes: 33 additions & 0 deletions tests/graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,37 @@ BOOST_AUTO_TEST_CASE(various_traits) {
}
}

BOOST_AUTO_TEST_CASE(empty_output) {
setMod().reset();

std::stringstream ss;

ss << Graph::random(10, 0).g().printN().printM().add1() << std::endl;
BOOST_TEST(ss.str() == "10 0\n");
ss.str("");

ss << Graph::random(10, 0).g().printN().add1() << std::endl;
BOOST_TEST(ss.str() == "10\n");
ss.str("");

Graph g = Graph::random(10, 0);
g.setVertexWeights(Array::random(g.n(), 10, 20));
ss << g.printN().printM() << std::endl;
auto s = ss.str();
s.pop_back();
BOOST_TEST(s != "\n");
ss.str("");

g = Graph::empty(5);
g.setVertexWeights(Array::id(5));
ss << g << std::endl;
BOOST_TEST(ss.str() == "0 1 2 3 4\n");
ss.str("");

g = Graph::empty(0);
ss << g.printN().printM() << std::endl;
BOOST_TEST(ss.str() == "0 0\n");
ss.str("");
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit daf2263

Please sign in to comment.