File: test_clusters.cpp

package info (click to toggle)
graphviz 14.0.5-2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 139,388 kB
  • sloc: ansic: 141,938; cpp: 11,957; python: 7,766; makefile: 4,043; yacc: 3,030; xml: 2,972; tcl: 2,495; sh: 1,388; objc: 1,159; java: 560; lex: 423; perl: 243; awk: 156; pascal: 139; php: 58; ruby: 49; cs: 31; sed: 1
file content (99 lines) | stat: -rw-r--r-- 3,472 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <cstddef>
#include <format>
#include <string>

#include <catch2/catch_all.hpp>

#include <cgraph/cgraph.h>
#include <gvc/gvc.h>

#include "svg_analyzer.h"

TEST_CASE("clusters in directed and undirected graphs with different layout "
          "engines") {
  const auto directed_graph = GENERATE(false, true);

  const std::string graph_type = directed_graph ? "digraph" : "graph";
  const std::string edge_op = directed_graph ? "->" : "--";

  const auto num_clusters_in_dot_source = GENERATE(1, 2);

  const std::string dot =
      num_clusters_in_dot_source == 1
          ? std::format("{} {{subgraph cluster_0 {{a {} b}}}}", graph_type,
                        edge_op)
          : std::format("{} {{subgraph cluster_0 {{a {} b}}; subgraph "
                        "cluster_1 {{c {} d }}}}",
                        graph_type, edge_op, edge_op);
  INFO("DOT source: " << dot);

  auto g = agmemread(dot.c_str());
  REQUIRE(g != nullptr);

  const std::string engine = GENERATE("dot",      //
                                      "neato",    //
                                      "fdp",      //
                                      "sfdp",     //
                                      "circo",    //
                                      "twopi",    //
                                      "osage",    //
                                      "patchwork" //
  );
  INFO("Layout engine: " << engine);

  auto gvc = gvContextPlugins(lt_preloaded_symbols, false);

  {
    const auto rc = gvLayout(gvc, g, engine.c_str());
    REQUIRE(rc == 0);
  }

  char *result = nullptr;
  size_t length = 0;
  {
    const auto rc = gvRenderData(gvc, g, "svg", &result, &length);
    REQUIRE(rc == 0);
  }
  REQUIRE(result != nullptr);
  REQUIRE(length > 0);

  SVGAnalyzer svg_analyzer{result};

  const std::size_t num_clusters = engine == "dot" || engine == "fdp" ||
#ifdef IPSEPCOLA
                                           engine == "neato" ||
#endif
                                           engine == "osage" ||
                                           engine == "patchwork"
                                       ? num_clusters_in_dot_source
                                       : 0;
  const std::size_t num_nodes = 2 * num_clusters_in_dot_source;
  const std::size_t num_edges =
      engine == "patchwork" ? 0 : num_clusters_in_dot_source;
  const std::size_t num_arrowheads = directed_graph ? num_edges : 0;

  const std::size_t num_svgs = 1;
  const std::size_t num_groups = 1 + num_clusters + num_nodes + num_edges;
  const std::size_t num_ellipses = engine == "patchwork" ? 0 : num_nodes;
  const std::size_t num_polygons = 1 + num_clusters +
                                   (engine == "patchwork" ? num_nodes : 0) +
                                   num_arrowheads;
  const std::size_t num_paths = num_edges;
  const std::size_t num_titles = num_clusters + num_nodes + num_edges;

  CHECK(svg_analyzer.num_svgs() == num_svgs);
  CHECK(svg_analyzer.num_groups() == num_groups);
  CHECK(svg_analyzer.num_circles() == 0);
  CHECK(svg_analyzer.num_ellipses() == num_ellipses);
  CHECK(svg_analyzer.num_lines() == 0);
  CHECK(svg_analyzer.num_paths() == num_paths);
  CHECK(svg_analyzer.num_polygons() == num_polygons);
  CHECK(svg_analyzer.num_polylines() == 0);
  CHECK(svg_analyzer.num_rects() == 0);
  CHECK(svg_analyzer.num_titles() == num_titles);

  gvFreeRenderData(result);
  gvFreeLayout(gvc, g);
  agclose(g);
  gvFreeContext(gvc);
}