| 1 |
#include <cstddef> |
| 2 |
#include <format> |
| 3 |
#include <string> |
| 4 |
|
| 5 |
#include <catch2/catch_all.hpp> |
| 6 |
|
| 7 |
#include <cgraph/cgraph.h> |
| 8 |
#include <gvc/gvc.h> |
| 9 |
|
| 10 |
#include "svg_analyzer.h" |
| 11 |
|
| 12 |
TEST_CASE("subgraph layout in directed and undirected graphs with different " |
| 13 |
"layout engines") { |
| 14 |
const auto directed_graph = GENERATE(false, true); |
| 15 |
|
| 16 |
const std::string graph_type = directed_graph ? "digraph" : "graph"; |
| 17 |
const std::string edge_op = directed_graph ? "->" : "--"; |
| 18 |
|
| 19 |
const auto num_subgraphs = GENERATE(1, 2); |
| 20 |
|
| 21 |
const std::string dot = |
| 22 |
num_subgraphs == 1 |
| 23 |
? std::format("{} {{subgraph s0 {{a {} b}}}}", graph_type, edge_op) |
| 24 |
: std::format( |
| 25 |
"{} {{subgraph s0 {{a {} b}}; subgraph s1 {{c {} d }}}}", |
| 26 |
graph_type, edge_op, edge_op); |
| 27 |
INFO("DOT source: " << dot); |
| 28 |
|
| 29 |
auto g = agmemread(dot.c_str()); |
| 30 |
REQUIRE(g != nullptr); |
| 31 |
|
| 32 |
const std::string engine = GENERATE("dot", // |
| 33 |
"neato", // |
| 34 |
"fdp", // |
| 35 |
"sfdp", // |
| 36 |
"circo", // |
| 37 |
"twopi", // |
| 38 |
"osage", // |
| 39 |
"patchwork" // |
| 40 |
); |
| 41 |
INFO("Layout engine: " << engine); |
| 42 |
|
| 43 |
auto gvc = gvContextPlugins(lt_preloaded_symbols, false); |
| 44 |
|
| 45 |
for (Agraph_t *subg = agfstsubg(g); subg; subg = agnxtsubg(subg)) { |
| 46 |
{ |
| 47 |
const auto rc = gvLayout(gvc, subg, engine.c_str()); |
| 48 |
REQUIRE(rc == 0); |
| 49 |
} |
| 50 |
|
| 51 |
char *result = nullptr; |
| 52 |
size_t length = 0; |
| 53 |
{ |
| 54 |
const auto rc = gvRenderData(gvc, subg, "svg", &result, &length); |
| 55 |
REQUIRE(rc == 0); |
| 56 |
} |
| 57 |
REQUIRE(result != nullptr); |
| 58 |
REQUIRE(length > 0); |
| 59 |
|
| 60 |
SVGAnalyzer svg_analyzer{result}; |
| 61 |
|
| 62 |
const std::size_t num_nodes = 2; |
| 63 |
const std::size_t num_edges = engine == "patchwork" ? 0 : 1; |
| 64 |
const std::size_t num_arrowheads = directed_graph ? num_edges : 0; |
| 65 |
|
| 66 |
const std::size_t num_svgs = 1; |
| 67 |
const std::size_t num_groups = 1 + num_nodes + num_edges; |
| 68 |
const std::size_t num_ellipses = engine == "patchwork" ? 0 : num_nodes; |
| 69 |
const std::size_t num_polygons = |
| 70 |
1 + (engine == "patchwork" ? num_nodes : 0) + num_arrowheads; |
| 71 |
const std::size_t num_paths = num_edges; |
| 72 |
const std::size_t num_titles = 1 + num_nodes + num_edges; |
| 73 |
|
| 74 |
CHECK(svg_analyzer.num_svgs() == num_svgs); |
| 75 |
CHECK(svg_analyzer.num_groups() == num_groups); |
| 76 |
CHECK(svg_analyzer.num_circles() == 0); |
| 77 |
CHECK(svg_analyzer.num_ellipses() == num_ellipses); |
| 78 |
CHECK(svg_analyzer.num_lines() == 0); |
| 79 |
CHECK(svg_analyzer.num_paths() == num_paths); |
| 80 |
CHECK(svg_analyzer.num_polygons() == num_polygons); |
| 81 |
CHECK(svg_analyzer.num_polylines() == 0); |
| 82 |
CHECK(svg_analyzer.num_rects() == 0); |
| 83 |
CHECK(svg_analyzer.num_titles() == num_titles); |
| 84 |
|
| 85 |
gvFreeRenderData(result); |
| 86 |
} |
| 87 |
|
| 88 |
for (Agraph_t *subg = agfstsubg(g); subg; subg = agnxtsubg(subg)) { |
| 89 |
gvFreeLayout(gvc, subg); |
| 90 |
} |
| 91 |
|
| 92 |
agclose(g); |
| 93 |
gvFreeContext(gvc); |
| 94 |
} |