Debian Codegraph @

tests/test_engines.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(
13 "simple directed and undirected graph with different 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 std::string dot = std::format("{} {{a {} b}}", graph_type, edge_op);
20 INFO("DOT source: " << dot);
21
22 auto g = agmemread(dot.c_str());
23 REQUIRE(g != nullptr);
24
25 const std::string engine = GENERATE("dot", //
26 "neato", //
27 "fdp", //
28 "sfdp", //
29 "circo", //
30 "twopi", //
31 "osage", //
32 "patchwork" //
33 );
34 INFO("Layout engine: " << engine);
35
36 auto gvc = gvContextPlugins(lt_preloaded_symbols, false);
37
38 {
39 const auto rc = gvLayout(gvc, g, engine.c_str());
40 REQUIRE(rc == 0);
41 }
42
43 char *result = nullptr;
44 size_t length = 0;
45 {
46 const auto rc = gvRenderData(gvc, g, "svg", &result, &length);
47 REQUIRE(rc == 0);
48 }
49 REQUIRE(result != nullptr);
50 REQUIRE(length > 0);
51
52 SVGAnalyzer svg_analyzer{result};
53
54 const std::size_t num_nodes = 2;
55 const std::size_t num_edges = engine == "patchwork" ? 0 : 1;
56 const std::size_t num_arrowheads = directed_graph ? num_edges : 0;
57
58 const std::size_t num_svgs = 1;
59 const std::size_t num_groups = 1 + num_nodes + num_edges;
60 const std::size_t num_ellipses = engine == "patchwork" ? 0 : num_nodes;
61 const std::size_t num_polygons =
62 1 + (engine == "patchwork" ? num_nodes : 0) + num_arrowheads;
63 const std::size_t num_paths = num_edges;
64 const std::size_t num_titles = num_nodes + num_edges;
65
66 CHECK(svg_analyzer.num_svgs() == num_svgs);
67 CHECK(svg_analyzer.num_groups() == num_groups);
68 CHECK(svg_analyzer.num_circles() == 0);
69 CHECK(svg_analyzer.num_ellipses() == num_ellipses);
70 CHECK(svg_analyzer.num_lines() == 0);
71 CHECK(svg_analyzer.num_paths() == num_paths);
72 CHECK(svg_analyzer.num_polygons() == num_polygons);
73 CHECK(svg_analyzer.num_polylines() == 0);
74 CHECK(svg_analyzer.num_rects() == 0);
75 CHECK(svg_analyzer.num_titles() == num_titles);
76
77 gvFreeRenderData(result);
78 gvFreeLayout(gvc, g);
79 agclose(g);
80 gvFreeContext(gvc);
81 }