From daf22630190c43a8d5a5c61541de7b2cb387c2e6 Mon Sep 17 00:00:00 2001 From: Ivan Smirnov Date: Mon, 5 Feb 2018 20:09:45 +0300 Subject: [PATCH] Do not print newline after graphs with no edges --- impl/generic_graph_inl.h | 21 +++++++++++++++++++-- jngen.h | 21 +++++++++++++++++++-- tests/graph.cpp | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 4 deletions(-) diff --git a/impl/generic_graph_inl.h b/impl/generic_graph_inl.h index b020cdc..2c3fd97 100644 --- a/impl/generic_graph_inl.h +++ b/impl/generic_graph_inl.h @@ -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"; } diff --git a/jngen.h b/jngen.h index 7b4de07..dc2eb31 100644 --- a/jngen.h +++ b/jngen.h @@ -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"; } diff --git a/tests/graph.cpp b/tests/graph.cpp index 6a22118..1ca0d08 100644 --- a/tests/graph.cpp +++ b/tests/graph.cpp @@ -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()