From eec03027be7969041ba53b2d454c81d496ac5a5c Mon Sep 17 00:00:00 2001 From: Arthur Sonzogni Date: Sun, 25 Mar 2018 14:48:28 +0200 Subject: [PATCH] Make PlanarGraph correct and add tests. --- src/environment.h | 2 - src/input_output_test.cpp | 2 + src/translator/planar_graph/PlanarGraph.cpp | 95 +++++++------------ src/translator/planar_graph/PlanarGraph.g4 | 2 +- src/translator/planar_graph/PlanarGraph.h | 5 +- test/LineNumberUnicode/empty/output | 2 + test/LineNumberUnicode/hello-world/input | 8 ++ test/LineNumberUnicode/hello-world/output | 10 ++ test/PlanarGraph/arrow-style/input | 8 ++ test/PlanarGraph/arrow-style/output | 27 ++++++ test/PlanarGraph/cycle-3/input | 1 + test/PlanarGraph/cycle-3/output | 9 ++ test/PlanarGraph/cycle-4/input | 1 + test/PlanarGraph/cycle-4/output | 12 +++ test/PlanarGraph/cycle-5/input | 1 + test/PlanarGraph/cycle-5/output | 15 +++ test/PlanarGraph/k-3-3/input | 9 ++ test/PlanarGraph/k-3-3/output | 1 + test/PlanarGraph/k-5/input | 1 + test/PlanarGraph/k-5/output | 1 + test/PlanarGraph/linear-3/input | 1 + test/PlanarGraph/linear-3/output | 9 ++ test/PlanarGraph/linear-4/input | 1 + test/PlanarGraph/linear-4/output | 12 +++ test/PlanarGraph/linear-5/input | 1 + test/PlanarGraph/linear-5/output | 15 +++ .../non-biconnected-inside-long/input | 5 + .../non-biconnected-inside-long/output | 36 +++++++ test/PlanarGraph/non-biconnected-inside/input | 5 + .../PlanarGraph/non-biconnected-inside/output | 18 ++++ test/PlanarGraph/numpad/input | 7 ++ test/PlanarGraph/numpad/output | 27 ++++++ 32 files changed, 282 insertions(+), 67 deletions(-) delete mode 100644 src/environment.h create mode 100644 test/LineNumberUnicode/hello-world/input create mode 100644 test/LineNumberUnicode/hello-world/output create mode 100644 test/PlanarGraph/arrow-style/input create mode 100644 test/PlanarGraph/arrow-style/output create mode 100644 test/PlanarGraph/cycle-3/input create mode 100644 test/PlanarGraph/cycle-3/output create mode 100644 test/PlanarGraph/cycle-4/input create mode 100644 test/PlanarGraph/cycle-4/output create mode 100644 test/PlanarGraph/cycle-5/input create mode 100644 test/PlanarGraph/cycle-5/output create mode 100644 test/PlanarGraph/k-3-3/input create mode 100644 test/PlanarGraph/k-3-3/output create mode 100644 test/PlanarGraph/k-5/input create mode 100644 test/PlanarGraph/k-5/output create mode 100644 test/PlanarGraph/linear-3/input create mode 100644 test/PlanarGraph/linear-3/output create mode 100644 test/PlanarGraph/linear-4/input create mode 100644 test/PlanarGraph/linear-4/output create mode 100644 test/PlanarGraph/linear-5/input create mode 100644 test/PlanarGraph/linear-5/output create mode 100644 test/PlanarGraph/non-biconnected-inside-long/input create mode 100644 test/PlanarGraph/non-biconnected-inside-long/output create mode 100644 test/PlanarGraph/non-biconnected-inside/input create mode 100644 test/PlanarGraph/non-biconnected-inside/output create mode 100644 test/PlanarGraph/numpad/input create mode 100644 test/PlanarGraph/numpad/output diff --git a/src/environment.h b/src/environment.h deleted file mode 100644 index 3de85e0..0000000 --- a/src/environment.h +++ /dev/null @@ -1,2 +0,0 @@ -const char* test_directory = "/home/sonzogna/programmation/real/Diagon/src/test"; -const char* git_version = "21"; diff --git a/src/input_output_test.cpp b/src/input_output_test.cpp index cf7455f..dc870e5 100644 --- a/src/input_output_test.cpp +++ b/src/input_output_test.cpp @@ -10,6 +10,7 @@ int main(int, const char**) { int result = 0; std::string path = test_directory; + std::cout << "test_directory = " << test_directory; for (auto& dir : std::experimental::filesystem::directory_iterator(path)) { std::string type = dir.path().filename(); std::cout << "Testing " << type << std::endl; @@ -35,6 +36,7 @@ int main(int, const char**) { std::cout << translator->Output() << std::endl; std::cout << "---[Expected]----------------" << std::endl; std::cout << output_string << std::endl; + std::cout << "---------------------" << std::endl; } } } diff --git a/src/translator/planar_graph/PlanarGraph.cpp b/src/translator/planar_graph/PlanarGraph.cpp index 1c150a8..972d21e 100644 --- a/src/translator/planar_graph/PlanarGraph.cpp +++ b/src/translator/planar_graph/PlanarGraph.cpp @@ -47,8 +47,6 @@ struct PlanarGraph::DrawnEdge { int y_up; int y_down; void Draw(Screen& screen, PlanarGraph& graph) { - if (graph.arrow_style[vertex_up][vertex_down] == ArrowStyle::NONE) - return; int top = 3 * y_up - 1; int bottom = 3 * y_down + 3; @@ -153,19 +151,6 @@ PlanarGraph::Arrow PlanarGraph::ReadArrow( return Arrow::RIGHT; } -std::wstring PlanarGraph::ArrowToString(PlanarGraph::Arrow arrow) { - switch (arrow) { - case Arrow::RIGHT: - return L"->"; - case Arrow::NONE: - return L"--"; - case Arrow::LEFT_RIGHT: - return L"<->"; - case Arrow::LEFT: - return L"<-"; - } -} - void InitializeEdgeIndex(Graph& graph) { boost::property_map::type e_index = boost::get(boost::edge_index, graph); @@ -175,15 +160,6 @@ void InitializeEdgeIndex(Graph& graph) { boost::put(e_index, *ei, edge_count++); } -void Print(Graph& graph) { - std::cout << "-----" << std::endl; - auto edges = boost::edges(graph); - for (auto edge = edges.first; edge != edges.second; ++edge) { - std::cout << boost::source(*edge, graph) << "->" << boost::target(*edge, graph) << std::endl; - } - std::cout << "-----" << std::endl; -} - bool ComputePlanarEmbedding(const Graph& graph, Embedding& embedding) { embedding = Embedding(boost::num_vertices(graph)); return boost::boyer_myrvold_planarity_test( @@ -191,13 +167,7 @@ bool ComputePlanarEmbedding(const Graph& graph, Embedding& embedding) { boost::boyer_myrvold_params::embedding = embedding.data()); } -void PlanarGraph::Write() { - - if (id_to_name.size() <= 2) - return; - - int num_vertices = id_to_name.size(); - +void PlanarGraph::ComputeArrowStyle() { // Compute ArrowStyle for(auto& v : vertex) { switch(v.arrow) { @@ -219,15 +189,22 @@ void PlanarGraph::Write() { break; } } +} + +void PlanarGraph::Write() { + ComputeArrowStyle(); + + if (id_to_name.size() <= 2) { + return; + } + + int num_vertices = id_to_name.size(); // Create a graph. - std::cout << id_to_name.size() << " nodes and " << num_vertices<< " vertex" << std::endl; - Graph graph(id_to_name.size()); + Graph graph(num_vertices); for (auto& it : vertex) add_edge(it.from, it.to, graph); InitializeEdgeIndex(graph); - Graph initial_graph(graph); - Print(graph); // Make it connected. boost::make_connected(graph); @@ -236,9 +213,10 @@ void PlanarGraph::Write() { // Make it biconnected. Embedding embedding; bool is_planar = ComputePlanarEmbedding(graph, embedding); - std::cout << "is_planar = " << is_planar << std::endl; - if (!is_planar) + if (!is_planar) { + output_ = "Graph is not planar.\n"; return; + } boost::make_biconnected_planar(graph, embedding.data()); InitializeEdgeIndex(graph); @@ -254,14 +232,11 @@ void PlanarGraph::Write() { boost::planar_canonical_ordering(graph, embedding.data(), std::back_inserter(ordering)); - std::cout << "Num vertices = " << num_vertices << std::endl; + //std::cout << "Num vertices = " << num_vertices << std::endl; std::vector inverse_ordering(num_vertices); for(int i = 0; i DrawNode = [&](int i) -> void { @@ -357,7 +323,7 @@ void PlanarGraph::Write() { int child_left = -1; int child_right = -1; // Draw our childrens. - for (auto j : down_children[i]) { + for (auto j : children[i]) { DrawNode(j); auto& child = drawn_vertices[j]; @@ -367,6 +333,12 @@ void PlanarGraph::Write() { edge.y_up = y[i] + 1; edge.y_down = y[j] - 1; + // There are no real edge between those two vertex. in this case, do not + // draw anything. + if (arrow_style[i][j] == ArrowStyle::NONE) { + continue; + } + // Make the child bigger if we need to. child.right = std::max(child.right, x[y[j]-1]+3); x[y[j]] = child.right; @@ -386,7 +358,6 @@ void PlanarGraph::Write() { child_left = edge.x - 1; } child_right = edge.x + 1; - } // Draw ourself. @@ -401,10 +372,12 @@ void PlanarGraph::Write() { // Mark ourself as drawn. is_drawn[i] = true; }; + for (int i : ordering) { if (y[i] == 0) { DrawNode(i); } } + Draw(); } diff --git a/src/translator/planar_graph/PlanarGraph.g4 b/src/translator/planar_graph/PlanarGraph.g4 index 1138bc2..3c90752 100644 --- a/src/translator/planar_graph/PlanarGraph.g4 +++ b/src/translator/planar_graph/PlanarGraph.g4 @@ -1,6 +1,6 @@ grammar PlanarGraph; -graph: edges (EOL edges)* EOF; +graph: EOL* edges (EOL+ edges)* EOL* EOF; edges: node (arrow node)+; arrow: RIGHT_ARROW | NONE_ARROW | LEFT_RIGHT_ARROW | LEFT_ARROW; node: ID | STRING; diff --git a/src/translator/planar_graph/PlanarGraph.h b/src/translator/planar_graph/PlanarGraph.h index e79c7b8..007c008 100644 --- a/src/translator/planar_graph/PlanarGraph.h +++ b/src/translator/planar_graph/PlanarGraph.h @@ -66,10 +66,9 @@ class PlanarGraph : public Translator { void ReadEdges(PlanarGraphParser::EdgesContext* edges); int ReadNode(PlanarGraphParser::NodeContext* node); Arrow ReadArrow(PlanarGraphParser::ArrowContext* arrow); + void Write(); - std::wstring ArrowToString(Arrow arrow); - std::vector> ConnectedComponent( - const std::vector& vertex); + void ComputeArrowStyle(); //---------------------------------------------------------------------------- }; diff --git a/test/LineNumberUnicode/empty/output b/test/LineNumberUnicode/empty/output index e69de29..404b26f 100644 --- a/test/LineNumberUnicode/empty/output +++ b/test/LineNumberUnicode/empty/output @@ -0,0 +1,2 @@ +┌┬┐ +└┴┘ diff --git a/test/LineNumberUnicode/hello-world/input b/test/LineNumberUnicode/hello-world/input new file mode 100644 index 0000000..208e097 --- /dev/null +++ b/test/LineNumberUnicode/hello-world/input @@ -0,0 +1,8 @@ +#include +using namespace std; + +int main() +{ + cout << "Hello, World!"; + return 0; +} diff --git a/test/LineNumberUnicode/hello-world/output b/test/LineNumberUnicode/hello-world/output new file mode 100644 index 0000000..b977663 --- /dev/null +++ b/test/LineNumberUnicode/hello-world/output @@ -0,0 +1,10 @@ +┌─┬──────────────────────────┐ +│1│#include │ +│2│using namespace std; │ +│3│ │ +│4│int main() │ +│5│{ │ +│6│ cout << "Hello, World!";│ +│7│ return 0; │ +│8│} │ +└─┴──────────────────────────┘ diff --git a/test/PlanarGraph/arrow-style/input b/test/PlanarGraph/arrow-style/input new file mode 100644 index 0000000..2d25347 --- /dev/null +++ b/test/PlanarGraph/arrow-style/input @@ -0,0 +1,8 @@ +0 -- 1 +1 -> 2 +2 <- 3 +3 <-> 4 +0 -- 1' +1 -> 2' +2 <- 3' +3 <-> 4' diff --git a/test/PlanarGraph/arrow-style/output b/test/PlanarGraph/arrow-style/output new file mode 100644 index 0000000..e8fddea --- /dev/null +++ b/test/PlanarGraph/arrow-style/output @@ -0,0 +1,27 @@ +┌───┐ +│ 0 │ +└┬─┬┘ + │┌┴─────────┐ + ││ 1 │ + │└┬────────┬┘ + │┌▽─┐ │ + ││2'│ │ + │└──┘ │ + │┌─────┐ │ + ││ 3 │ │ + │└┬─△─△┘ │ + │ │ │ │┌──┐│ + │ │ │ ││3'││ + │ │ │ │└┬─┘│ + │ │ │┌▽┐│ │ + │ │ ││4││ │ + │ │ │└─┘│ │ + │ │┌▽─┐ │ │ + │ ││4'│ │ │ + │ │└──┘ │ │ + │┌▽─────▽──▽─┐ + ││ 2 │ + │└───────────┘ +┌┴─┐ +│1'│ +└──┘ diff --git a/test/PlanarGraph/cycle-3/input b/test/PlanarGraph/cycle-3/input new file mode 100644 index 0000000..a2c0575 --- /dev/null +++ b/test/PlanarGraph/cycle-3/input @@ -0,0 +1 @@ +0 -- 1 -- 2 -- 0 diff --git a/test/PlanarGraph/cycle-3/output b/test/PlanarGraph/cycle-3/output new file mode 100644 index 0000000..68aa03c --- /dev/null +++ b/test/PlanarGraph/cycle-3/output @@ -0,0 +1,9 @@ +┌───┐ +│ 0 │ +└┬─┬┘ + │┌┴┐ + ││1│ + │└┬┘ +┌┴─┴┐ +│ 2 │ +└───┘ diff --git a/test/PlanarGraph/cycle-4/input b/test/PlanarGraph/cycle-4/input new file mode 100644 index 0000000..8bb81a0 --- /dev/null +++ b/test/PlanarGraph/cycle-4/input @@ -0,0 +1 @@ +0 -- 1 -- 2 -- 3 -- 0 diff --git a/test/PlanarGraph/cycle-4/output b/test/PlanarGraph/cycle-4/output new file mode 100644 index 0000000..4e1999d --- /dev/null +++ b/test/PlanarGraph/cycle-4/output @@ -0,0 +1,12 @@ +┌───┐ +│ 0 │ +└┬─┬┘ + │┌┴┐ + ││1│ + │└┬┘ +┌┴┐│ +│3││ +└┬┘│ +┌┴─┴─┐ +│ 2 │ +└────┘ diff --git a/test/PlanarGraph/cycle-5/input b/test/PlanarGraph/cycle-5/input new file mode 100644 index 0000000..53d6278 --- /dev/null +++ b/test/PlanarGraph/cycle-5/input @@ -0,0 +1 @@ +0 -- 1 -- 2 -- 3 -- 4 -- 0 diff --git a/test/PlanarGraph/cycle-5/output b/test/PlanarGraph/cycle-5/output new file mode 100644 index 0000000..d362013 --- /dev/null +++ b/test/PlanarGraph/cycle-5/output @@ -0,0 +1,15 @@ +┌───┐ +│ 0 │ +└┬─┬┘ + │┌┴┐ + ││1│ + │└┬┘ +┌┴┐│ +│4││ +└┬┘│ +┌┴┐│ +│3││ +└┬┘│ +┌┴─┴─┐ +│ 2 │ +└────┘ diff --git a/test/PlanarGraph/k-3-3/input b/test/PlanarGraph/k-3-3/input new file mode 100644 index 0000000..81857df --- /dev/null +++ b/test/PlanarGraph/k-3-3/input @@ -0,0 +1,9 @@ +A1 -- B1 +A1 -- B2 +A1 -- B3 +A2 -- B1 +A2 -- B2 +A2 -- B3 +A3 -- B1 +A3 -- B2 +A3 -- B3 diff --git a/test/PlanarGraph/k-3-3/output b/test/PlanarGraph/k-3-3/output new file mode 100644 index 0000000..44bac35 --- /dev/null +++ b/test/PlanarGraph/k-3-3/output @@ -0,0 +1 @@ +Graph is not planar. diff --git a/test/PlanarGraph/k-5/input b/test/PlanarGraph/k-5/input new file mode 100644 index 0000000..dc77544 --- /dev/null +++ b/test/PlanarGraph/k-5/input @@ -0,0 +1 @@ +0 -- 1 -- 2 -- 3 -- 4 -- 0 -- 2 -- 4 -- 1 -- 3 -- 0 diff --git a/test/PlanarGraph/k-5/output b/test/PlanarGraph/k-5/output new file mode 100644 index 0000000..44bac35 --- /dev/null +++ b/test/PlanarGraph/k-5/output @@ -0,0 +1 @@ +Graph is not planar. diff --git a/test/PlanarGraph/linear-3/input b/test/PlanarGraph/linear-3/input new file mode 100644 index 0000000..4bbf1ce --- /dev/null +++ b/test/PlanarGraph/linear-3/input @@ -0,0 +1 @@ +0 -- 1 -- 2 diff --git a/test/PlanarGraph/linear-3/output b/test/PlanarGraph/linear-3/output new file mode 100644 index 0000000..f19f0a2 --- /dev/null +++ b/test/PlanarGraph/linear-3/output @@ -0,0 +1,9 @@ +┌─┐ +│0│ +└┬┘ +┌┴┐ +│1│ +└┬┘ +┌┴┐ +│2│ +└─┘ diff --git a/test/PlanarGraph/linear-4/input b/test/PlanarGraph/linear-4/input new file mode 100644 index 0000000..a5f0b63 --- /dev/null +++ b/test/PlanarGraph/linear-4/input @@ -0,0 +1 @@ +0 -- 1 -- 2 -- 3 diff --git a/test/PlanarGraph/linear-4/output b/test/PlanarGraph/linear-4/output new file mode 100644 index 0000000..9b3df80 --- /dev/null +++ b/test/PlanarGraph/linear-4/output @@ -0,0 +1,12 @@ + ┌─┐ + │0│ + └┬┘ + ┌┴┐ + │1│ + └┬┘ +┌─┐│ +│3││ +└┬┘│ +┌┴─┴─┐ +│ 2 │ +└────┘ diff --git a/test/PlanarGraph/linear-5/input b/test/PlanarGraph/linear-5/input new file mode 100644 index 0000000..396e6a1 --- /dev/null +++ b/test/PlanarGraph/linear-5/input @@ -0,0 +1 @@ +0 -- 1 -- 2 -- 3 -- 4 diff --git a/test/PlanarGraph/linear-5/output b/test/PlanarGraph/linear-5/output new file mode 100644 index 0000000..37127a8 --- /dev/null +++ b/test/PlanarGraph/linear-5/output @@ -0,0 +1,15 @@ + ┌─┐ + │0│ + └┬┘ + ┌┴┐ + │1│ + └┬┘ +┌───┐│ +│ 3 ││ +└┬─┬┘│ + │┌┴┐│ + ││4││ + │└─┘│ +┌┴───┴─┐ +│ 2 │ +└──────┘ diff --git a/test/PlanarGraph/non-biconnected-inside-long/input b/test/PlanarGraph/non-biconnected-inside-long/input new file mode 100644 index 0000000..4384286 --- /dev/null +++ b/test/PlanarGraph/non-biconnected-inside-long/input @@ -0,0 +1,5 @@ +0 -- 1 +0 -- 2 +1 -- 3 +2 -- 3 +0 -- 4 -- 5 -- 6 -- 7 -- 8 -- 9 -- 10 -- 11 diff --git a/test/PlanarGraph/non-biconnected-inside-long/output b/test/PlanarGraph/non-biconnected-inside-long/output new file mode 100644 index 0000000..e18d040 --- /dev/null +++ b/test/PlanarGraph/non-biconnected-inside-long/output @@ -0,0 +1,36 @@ +┌────────────────┐ +│ 0 │ +└┬───────────┬──┬┘ + │ │ ┌┴┐ + │ │ │1│ + │ │ └┬┘ + │ ┌┴┐ │ + │ │4│ │ + │ └┬┘ │ + │┌────────┐ │ │ + ││ 6 │ │ │ + │└┬──────┬┘ │ │ + │ │ ┌┴┐ │ │ + │ │ │7│ │ │ + │ │ └┬┘ │ │ + │ │┌───┐ │ │ │ + │ ││ 9 │ │ │ │ + │ │└┬─┬┘ │ │ │ + │ │ │┌┴─┐│ │ │ + │ │ ││10││ │ │ + │ │ │└┬─┘│ │ │ + │ │ │┌┴─┐│ │ │ + │ │ ││11││ │ │ + │ │ │└──┘│ │ │ + │ │┌┴────┴─┐│ │ + │ ││ 8 ││ │ + │ │└───────┘│ │ + │┌┴─────────┴─┐│ + ││ 5 ││ + │└────────────┘│ +┌┴┐ │ +│2│ │ +└┬┘ │ +┌┴──────────────┴─┐ +│ 3 │ +└─────────────────┘ diff --git a/test/PlanarGraph/non-biconnected-inside/input b/test/PlanarGraph/non-biconnected-inside/input new file mode 100644 index 0000000..72b0ffd --- /dev/null +++ b/test/PlanarGraph/non-biconnected-inside/input @@ -0,0 +1,5 @@ +0 -- 1 +0 -- 2 +1 -- 3 +2 -- 3 +0 -- 4 -- 5 diff --git a/test/PlanarGraph/non-biconnected-inside/output b/test/PlanarGraph/non-biconnected-inside/output new file mode 100644 index 0000000..52e9023 --- /dev/null +++ b/test/PlanarGraph/non-biconnected-inside/output @@ -0,0 +1,18 @@ +┌─────┐ +│ 0 │ +└┬─┬─┬┘ + │ │┌┴┐ + │ ││1│ + │ │└┬┘ + │┌┴┐│ + ││4││ + │└┬┘│ + │┌┴┐│ + ││5││ + │└─┘│ +┌┴┐ │ +│2│ │ +└┬┘ │ +┌┴───┴─┐ +│ 3 │ +└──────┘ diff --git a/test/PlanarGraph/numpad/input b/test/PlanarGraph/numpad/input new file mode 100644 index 0000000..c640ac5 --- /dev/null +++ b/test/PlanarGraph/numpad/input @@ -0,0 +1,7 @@ +1 -- 2 -- 3 +4 -- 5 -- 6 +7 -- 8 -- 9 + +1 -- 4 -- 7 +2 -- 5 -- 8 +3 -- 6 -- 9 diff --git a/test/PlanarGraph/numpad/output b/test/PlanarGraph/numpad/output new file mode 100644 index 0000000..89037c7 --- /dev/null +++ b/test/PlanarGraph/numpad/output @@ -0,0 +1,27 @@ +┌─────┐ +│ 1 │ +└┬───┬┘ + │ ┌┴────┐ + │ │ 2 │ + │ └┬───┬┘ +┌┴──┐│ │ +│ 4 ││ │ +└┬─┬┘│ │ + │┌┴─┴─┐ │ + ││ 5 │ │ + │└┬──┬┘ │ +┌┴┐│ │ │ +│7││ │ │ +└┬┘│ │ │ +┌┴─┴─┐│ │ +│ 8 ││ │ +└┬───┘│ │ +┌┴┐ │ │ +│9│ │ │ +└┬┘ │ │ +┌┴────┴─┐│ +│ 6 ││ +└┬──────┘│ +┌┴───────┴─┐ +│ 3 │ +└──────────┘