Debian Codegraph @

tests/test_subgraphs.cpp

raw
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("subgraphs in directed and undirected graphs with different layout "
13 "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 const auto rc = gvLayout(gvc, g, engine.c_str());
46 REQUIRE(rc == 0);
47 }
48
49 char *result = nullptr;
50 size_t length = 0;
51 {
52 const auto rc = gvRenderData(gvc, g, "svg", &result, &length);
53 REQUIRE(rc == 0);
54 }
55 REQUIRE(result != nullptr);
56 REQUIRE(length > 0);
57
58 SVGAnalyzer svg_analyzer{result};
59
60 const std::size_t num_nodes = num_subgraphs * 2;
61 const std::size_t num_edges = engine == "patchwork" ? 0 : num_subgraphs;
62 const std::size_t num_arrowheads = directed_graph ? num_edges : 0;
63
64 const std::size_t num_svgs = 1;
65 const std::size_t num_groups = 1 + num_nodes + num_edges;
66 const std::size_t num_ellipses = engine == "patchwork" ? 0 : num_nodes;
67 const std::size_t num_polygons =
68 1 + (engine == "patchwork" ? num_nodes : 0) + num_arrowheads;
69 const std::size_t num_paths = num_edges;
70 const std::size_t num_titles = num_nodes + num_edges;
71
72 CHECK(svg_analyzer.num_svgs() == num_svgs);
73 CHECK(svg_analyzer.num_groups() == num_groups);
74 CHECK(svg_analyzer.num_circles() == 0);
75 CHECK(svg_analyzer.num_ellipses() == num_ellipses);
76 CHECK(svg_analyzer.num_lines() == 0);
77 CHECK(svg_analyzer.num_paths() == num_paths);
78 CHECK(svg_analyzer.num_polygons() == num_polygons);
79 CHECK(svg_analyzer.num_polylines() == 0);
80 CHECK(svg_analyzer.num_rects() == 0);
81 CHECK(svg_analyzer.num_titles() == num_titles);
82
83 gvFreeRenderData(result);
84 gvFreeLayout(gvc, g);
85 agclose(g);
86 gvFreeContext(gvc);
87 }