Debian Codegraph @

tests/test_clusters.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("clusters 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_clusters_in_dot_source = GENERATE(1, 2);
20
21 const std::string dot =
22 num_clusters_in_dot_source == 1
23 ? std::format("{} {{subgraph cluster_0 {{a {} b}}}}", graph_type,
24 edge_op)
25 : std::format("{} {{subgraph cluster_0 {{a {} b}}; subgraph "
26 "cluster_1 {{c {} d }}}}",
27 graph_type, edge_op, edge_op);
28 INFO("DOT source: " << dot);
29
30 auto g = agmemread(dot.c_str());
31 REQUIRE(g != nullptr);
32
33 const std::string engine = GENERATE("dot", //
34 "neato", //
35 "fdp", //
36 "sfdp", //
37 "circo", //
38 "twopi", //
39 "osage", //
40 "patchwork" //
41 );
42 INFO("Layout engine: " << engine);
43
44 auto gvc = gvContextPlugins(lt_preloaded_symbols, false);
45
46 {
47 const auto rc = gvLayout(gvc, g, 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, g, "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_clusters = engine == "dot" || engine == "fdp" ||
63 #ifdef IPSEPCOLA
64 engine == "neato" ||
65 #endif
66 engine == "osage" ||
67 engine == "patchwork"
68 ? num_clusters_in_dot_source
69 : 0;
70 const std::size_t num_nodes = 2 * num_clusters_in_dot_source;
71 const std::size_t num_edges =
72 engine == "patchwork" ? 0 : num_clusters_in_dot_source;
73 const std::size_t num_arrowheads = directed_graph ? num_edges : 0;
74
75 const std::size_t num_svgs = 1;
76 const std::size_t num_groups = 1 + num_clusters + num_nodes + num_edges;
77 const std::size_t num_ellipses = engine == "patchwork" ? 0 : num_nodes;
78 const std::size_t num_polygons = 1 + num_clusters +
79 (engine == "patchwork" ? num_nodes : 0) +
80 num_arrowheads;
81 const std::size_t num_paths = num_edges;
82 const std::size_t num_titles = num_clusters + num_nodes + num_edges;
83
84 CHECK(svg_analyzer.num_svgs() == num_svgs);
85 CHECK(svg_analyzer.num_groups() == num_groups);
86 CHECK(svg_analyzer.num_circles() == 0);
87 CHECK(svg_analyzer.num_ellipses() == num_ellipses);
88 CHECK(svg_analyzer.num_lines() == 0);
89 CHECK(svg_analyzer.num_paths() == num_paths);
90 CHECK(svg_analyzer.num_polygons() == num_polygons);
91 CHECK(svg_analyzer.num_polylines() == 0);
92 CHECK(svg_analyzer.num_rects() == 0);
93 CHECK(svg_analyzer.num_titles() == num_titles);
94
95 gvFreeRenderData(result);
96 gvFreeLayout(gvc, g);
97 agclose(g);
98 gvFreeContext(gvc);
99 }