43#include <gtest/gtest.h>
57using namespace testing;
103 n1 =
g.insert_node(1);
104 n2 =
g.insert_node(2);
105 n3 =
g.insert_node(3);
106 g.insert_arc(
n1,
n2, 10);
107 g.insert_arc(
n2,
n3, 20);
108 g.insert_arc(
n1,
n3, 30);
153 string result =
out.str();
156 EXPECT_NE(result.find(
"graph {"), string::npos);
158 EXPECT_EQ(result.find(
"digraph {"), string::npos);
165 string result =
out.str();
168 EXPECT_NE(result.find(
"digraph {"), string::npos);
175 string result =
out.str();
178 EXPECT_NE(result.find(
"label = \"1\""), string::npos);
179 EXPECT_NE(result.find(
"label = \"2\""), string::npos);
180 EXPECT_NE(result.find(
"label = \"3\""), string::npos);
187 string result =
out.str();
190 EXPECT_NE(result.find(
"--"), string::npos);
197 string result =
out.str();
200 EXPECT_NE(result.find(
"->"), string::npos);
207 string result =
out.str();
209 EXPECT_NE(result.find(
"rankdir = TB"), string::npos);
216 string result =
out.str();
219 size_t lastBrace = result.rfind(
'}');
220 EXPECT_NE(lastBrace, string::npos);
228 string result =
out.str();
230 EXPECT_NE(result.find(
"label"), string::npos);
231 EXPECT_NE(result.find(
"1"), string::npos);
236 auto arc = g.get_first_arc();
239 string result =
out.str();
241 EXPECT_NE(result.find(
"label"), string::npos);
248 EXPECT_FALSE(dummy(n1));
249 EXPECT_FALSE(dummy(g.get_first_arc()));
260 EXPECT_EQ(shader(n1),
"");
267 EXPECT_EQ(shader(n1),
"SHADOW-NODE");
272 auto arc = g.get_first_arc();
275 EXPECT_EQ(shader(arc),
"ARC");
280 auto arc = g.get_first_arc();
283 EXPECT_EQ(shader(arc),
"SHADOW-ARC");
296 tnode->
get_key() = gnode->get_info();
305 ASSERT_NE(treeRoot,
nullptr);
306 EXPECT_EQ(treeRoot->
get_key(), 1);
318 ASSERT_NE(firstChild,
nullptr);
322 for (
auto* child = firstChild; child !=
nullptr;
323 child = child->get_right_sibling())
326 EXPECT_EQ(childCount, 2);
339 if (node ==
nullptr)
return;
340 values.insert(node->get_key());
341 for (
auto* child = node->get_left_child(); child !=
nullptr;
342 child = child->get_right_sibling())
343 collectValues(child);
346 collectValues(treeRoot);
349 EXPECT_TRUE(values.count(1) > 0);
350 EXPECT_TRUE(values.count(2) > 0);
351 EXPECT_TRUE(values.count(3) > 0);
352 EXPECT_TRUE(values.count(4) > 0);
353 EXPECT_EQ(values.size(), 4u);
361 tree.insert_arc(grandchild,
root, 0);
364 EXPECT_THROW(Converter()(tree,
root), std::domain_error);
371 for (
int i = 0; i < 4; ++i)
376 : graph_to_tree_node<Graph, int, IntConvert>(tree,
root);
379 EXPECT_EQ(
nodes, 4u);
389 static inline long live = 0;
392 Counted_Key() { ++live; }
393 Counted_Key(
const Counted_Key &
k) :
value(
k.
value) { ++live; }
394 Counted_Key & operator = (
const Counted_Key &) =
default;
395 ~Counted_Key() { --live; }
399 struct Failing_Convert
401 static inline int calls = 0;
407 throw std::runtime_error(
"conversion");
408 tnode->
get_key().value = gnode->get_info();
416 Failing_Convert::calls = 0;
419 EXPECT_EQ(Counted_Key::live, 0);
421 Failing_Convert::calls = 0;
422 EXPECT_THROW((graph_to_tree_node<Graph, Counted_Key, Failing_Convert>(tree,
root)),
424 EXPECT_EQ(Counted_Key::live, 0);
438 string result =
out.str();
441 EXPECT_EQ(result.find(
"Root"), 0u);
453 string result =
out.str();
456 EXPECT_NE(result.find(
"Node "), string::npos);
461TEST(GenerateTreeGraphvizTest, EscapesSpecialCharactersInLabels)
472 generate_tree_graphviz<Tree_Node<string>, RawWriter>(
root,
out);
473 const string result =
out.str();
475 EXPECT_NE(result.find(
"label=\"say \\\"hi\\\"\\npath\\\\leaf\""), string::npos);
476 EXPECT_EQ(result.find(
"label=\"say \"hi\""), string::npos);
494 string result =
out.str();
497 EXPECT_FALSE(result.empty());
498 EXPECT_NE(result.find(
"Root"), string::npos);
499 EXPECT_NE(result.find(
"\"1\""), string::npos);
508TEST(EmptyGraphTest, GenerateGraphvizHandlesEmptyGraph)
514 string result =
out.str();
517 EXPECT_NE(result.find(
"graph {"), string::npos);
518 EXPECT_NE(result.find(
"}"), string::npos);
521TEST(SingleNodeGraphTest, GenerateGraphvizHandlesSingleNode)
528 string result =
out.str();
530 EXPECT_NE(result.find(
"42"), string::npos);
533TEST(SingleNodeTreeTest, GraphToTreeNodeHandlesSingleNode)
540 tn->
get_key() = gn->get_info();
547 ASSERT_NE(treeRoot,
nullptr);
548 EXPECT_EQ(treeRoot->
get_key(), 42);
558TEST(TypeTraitsTest, DftWriteConvertsToString)
564 string result = writer(&node);
566 EXPECT_EQ(result,
"123");
579 out <<
"label=\"" << n->get_info() <<
"\"";
583 void operator()(
const Graph&,
Arc* a, ostream&
out) {
584 out <<
"label=\"" << a->get_info() <<
"\"";
591 string result =
out.str();
594 EXPECT_NE(result.find(
"digraph {"), string::npos);
595 EXPECT_NE(result.find(
"->"), string::npos);
605 string operator()(
Arc* a)
const {
return to_string(a->get_info()); }
610 string result =
out.str();
612 EXPECT_NE(result.find(
"graph {"), string::npos);
613 EXPECT_NE(result.find(
"rankdir = TB"), string::npos);
619 const vector<string> rankdirs = {
"TB",
"BT",
"LR",
"RL"};
621 for (
const auto& dir : rankdirs) {
624 string result =
out.str();
625 EXPECT_NE(result.find(
"rankdir = " + dir), string::npos)
626 <<
"Failed for rankdir: " << dir;
631TEST(StringNodeGraphTest, GenerateGraphvizWithStringNodes)
641 string result =
out.str();
643 EXPECT_NE(result.find(
"Alpha"), string::npos);
644 EXPECT_NE(result.find(
"Beta"), string::npos);
648TEST(DoubleArcGraphTest, GenerateGraphvizWithDoubleArcs)
652 auto* a = g.insert_node(1);
653 auto* b = g.insert_node(2);
654 g.insert_arc(a, b, 3.14159);
658 string result =
out.str();
660 EXPECT_NE(result.find(
"3.14"), string::npos);
664TEST(DAGTest, RankGraphvizCreatesSubgraphs)
670 auto* n1 = g.insert_node(1);
671 auto* n2 = g.insert_node(2);
672 auto* n3 = g.insert_node(3);
673 g.insert_arc(n1, n2, 0);
674 g.insert_arc(n2, n3, 0);
677 void operator()(
const DAG&, DAG::Node* n, ostream&
out) {
682 void operator()(
const DAG&, DAG::Arc*, ostream&
out) {
691 string result =
out.str();
694 EXPECT_GE(numRanks, 1u);
696 EXPECT_NE(result.find(
"subgraph"), string::npos);
697 EXPECT_NE(result.find(
"rank"), string::npos);
705TEST(ForestTest, GenerateForestOutputsMultipleTrees)
722 string result =
out.str();
725 EXPECT_NE(result.find(
"100"), string::npos);
726 EXPECT_NE(result.find(
"200"), string::npos);
727 EXPECT_NE(result.find(
"300"), string::npos);
730 size_t firstRoot = result.find(
"Root");
731 size_t secondRoot = result.find(
"Root", firstRoot + 1);
732 size_t thirdRoot = result.find(
"Root", secondRoot + 1);
734 EXPECT_NE(firstRoot, string::npos);
735 EXPECT_NE(secondRoot, string::npos);
736 EXPECT_NE(thirdRoot, string::npos);
745TEST(CustomWriteTest, GenerateTreeWithCustomWriter)
750 ss <<
"0x" << hex << p->
get_key();
759 generate_tree<Tree_Node<int>, HexWriter>(
root,
out);
760 string result =
out.str();
762 EXPECT_NE(result.find(
"0xff"), string::npos);
768TEST(DeepTreeTest, GenerateTreeHandlesDeepTree)
770 const int DEPTH = 10;
775 for (
int i = 1; i < DEPTH; ++i) {
778 current->insert_leftmost_child(child);
784 string result =
out.str();
787 for (
int i = 0; i < DEPTH; ++i) {
788 EXPECT_NE(result.find(
"\"" +
to_string(i) +
"\""), string::npos)
789 <<
"Missing node " << i;
794 EXPECT_NE(result.find(
"Node 0.0.0.0"), string::npos);
800TEST(WideTreeTest, GenerateTreeHandlesWideTree)
802 const int WIDTH = 10;
806 for (
int i = 1; i <=
WIDTH; ++i) {
809 root->insert_rightmost_child(child);
814 string result =
out.str();
817 for (
int i = 1; i <=
WIDTH; ++i) {
818 EXPECT_NE(result.find(
"\"" +
to_string(i) +
"\""), string::npos)
819 <<
"Missing child " << i;
833 graph_to_tree_node<Graph, int, IntConvert, Dft_Show_Arc<Graph>>(tree,
root);
835 ASSERT_NE(treeRoot,
nullptr);
836 EXPECT_EQ(treeRoot->
get_key(), 1);
848 ASSERT_NE(treeRoot,
nullptr);
849 EXPECT_EQ(treeRoot->
get_key(), 2);
853 ASSERT_NE(firstChild,
nullptr);
861 struct DoubleConvert {
864 tnode->
get_key() = gnode->get_info() * 2;
871 EXPECT_EQ(treeRoot->
get_key(), 2);
879 struct StringConvert {
889 EXPECT_EQ(treeRoot->
get_key(),
"Node_1");
899template <
typename GT>
916 out <<
"label=\"" << n->get_info() <<
"\"";
920 void operator()(
const Graph&,
Arc* a, ostream&
out) {
921 out <<
"label=\"" << a->get_info() <<
"\"";
927 (g,
out, NodeAttr(), ArcAttr(),
"LR");
928 string result =
out.str();
931 EXPECT_NE(result.find(
"20"), string::npos);
932 EXPECT_NE(result.find(
"30"), string::npos);
934 EXPECT_EQ(result.find(
"\"10\""), string::npos);
941TEST(LargeGraphTest, GenerateGraphvizHandlesLargeGraph)
946 const int NUM_NODES = 100;
947 vector<LGraph::Node*>
nodes;
950 for (
int i = 0; i < NUM_NODES; ++i) {
951 nodes.push_back(g.insert_node(i));
955 for (
int i = 0; i < NUM_NODES - 1; ++i) {
961 string result =
out.str();
964 EXPECT_NE(result.find(
"graph {"), string::npos);
965 EXPECT_NE(result.find(
"}"), string::npos);
968 EXPECT_NE(result.find(
"\"0\""), string::npos);
969 EXPECT_NE(result.find(
"\"99\""), string::npos);
972TEST(LargeTreeTest, GraphToTreeNodeHandlesLargeTree)
977 const int NUM_NODES = 100;
978 vector<LGraph::Node*>
nodes;
981 for (
int i = 0; i < NUM_NODES; ++i) {
982 nodes.push_back(tree.insert_node(i));
986 for (
int i = 1; i < NUM_NODES; ++i) {
987 int parent = (i - 1) / 2;
993 tn->
get_key() = gn->get_info();
1000 ASSERT_NE(treeRoot,
nullptr);
1001 EXPECT_EQ(treeRoot->
get_key(), 0);
1006 if (node ==
nullptr)
return;
1008 for (
auto* child = node->get_left_child(); child !=
nullptr;
1009 child = child->get_right_sibling())
1012 countNodes(treeRoot);
1014 EXPECT_EQ(
count, NUM_NODES);
WeightedDigraph::Node Node
size_t size_t int32_t value
size_t size_t int32_t * out
Generic directed graph (digraph) wrapper template.
typename BaseGraph::Arc Arc
typename BaseGraph::Node Node
Functor class to convert a tree graph to Tree_Node structure.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Graph_Node< int > Node
The graph type.
Graph_Arc< int > Arc
The node class type.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Graph class implemented with singly-linked adjacency lists.
Arc * insert_arc(Node *src, Node *tgt, void *a)
virtual Node * insert_node(Node *p)
Insertion of a node whose memory has already been allocated.
Forward declaration used by CRTP helpers before the full node definition.
bool traverse(Operation op)
Preorder traversal over all nodes executing op.
Tree_Node * get_left_child() const noexcept
Returns the leftmost child of this.
void insert_right_sibling(Tree_Node *p) noexcept
Inserts p to the right of this node.
T & get_key() noexcept
Returns a modifiable reference to the node contents.
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Simple digraph for testing.
Simple graph for testing.
Tree graph for graph_to_tree conversion testing.
Graph visualization and output generation utilities.
Spanning tree visualization with highlighted tree/non-tree edges.
Tree visualization and output generation.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Convert spanning tree graphs to Tree_Node structures.
TEST_F(SimpleGraphTest, GenerateGraphvizContainsGraphKeyword)
DynArray< Graph::Node * > nodes
#define ARC_COOKIE(p)
Return the arc cookie
#define NODE_COOKIE(p)
Return the node cookie
size_t rank_graphviz(const GT &g, std::ostream &out, Node_Attr node_attr=Node_Attr(), Arc_Attr arc_attr=Arc_Attr(), const std::string &rankdir="LR", SA sa=SA())
Generate Graphviz DOT output with topological ranking.
void generate_forest(Node *root, std::ostream &out)
Generate a forest specification for the ntreepic drawing tool.
void generate_tree(Node *root, std::ostream &out, const int &tree_number=0)
Generate a tree specification for the ntreepic drawing tool.
void destroy_tree(Node *root)
Destroys (frees memory) the tree whose root is root.
Main namespace for Aleph-w library functions.
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Default filter for filtered iterators on arcs.
Default filter for the graph nodes.
Functor for generating Graphviz specifications.
Arc of graph implemented with double-linked adjacency lists.
Functor class for generating Graphviz DOT specifications.
Converter from Graph::Node to int for Tree_Node.
List_Graph< Graph_Node< int >, Graph_Arc< int > >::Node GraphNode
void operator()(GraphNode *gnode, Tree_Node< int > *tnode)
Shading functor for spanning tree arcs.
Shading functor for spanning tree nodes.
bool operator()(typename GT::Arc *a) const
Functor to convert tree node to string for output.
string operator()(Tree_Node< int > *p)
Generic graph and digraph implementations.
Utility algorithms and operations for graphs.
General tree (n-ary tree) node.