Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
generate_tree.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
33
38# include <gtest/gtest.h>
39
40# include <algorithm>
41
42# include <generate_tree.H>
43# include "tree-node-common.H"
44
45using namespace std;
46using namespace testing;
47using namespace Aleph;
48
49TEST_F(Simple_Tree, Generate_ntreepic)
50{
51 const string result =
52 "Root \"0\" \n" \
53 "Node 0 \"1\" \n" \
54 "Node 0.0 \"6\" \n" \
55 "Node 0.0 \"7\" \n" \
56 "Node 0.0 \"8\" \n" \
57 "Node 0.0 \"9\" \n" \
58 "Node 0.0 \"10\" \n" \
59 "Node 0 \"2\" \n" \
60 "Node 0.1 \"11\" \n" \
61 "Node 0.1 \"12\" \n" \
62 "Node 0.1 \"13\" \n" \
63 "Node 0.1 \"14\" \n" \
64 "Node 0.1 \"15\" \n" \
65 "Node 0 \"3\" \n" \
66 "Node 0.2 \"16\" \n" \
67 "Node 0.2 \"17\" \n" \
68 "Node 0.2 \"18\" \n" \
69 "Node 0.2 \"19\" \n" \
70 "Node 0.2 \"20\" \n" \
71 "Node 0 \"4\" \n" \
72 "Node 0.3 \"21\" \n" \
73 "Node 0.3 \"22\" \n" \
74 "Node 0.3 \"23\" \n" \
75 "Node 0.3 \"24\" \n" \
76 "Node 0.3 \"25\" \n" \
77 "Node 0 \"5\" \n" \
78 "Node 0.4 \"26\" \n" \
79 "Node 0.4 \"27\" \n" \
80 "Node 0.4 \"28\" \n" \
81 "Node 0.4 \"29\" \n" \
82 "Node 0.4 \"30\" \n";
83
84 ostringstream ss;
86
87 ASSERT_EQ(ss.str(), result);
88}
89
90// A chain of n nodes, a tree of n levels
91static Tree_Node<int> * chain(size_t n)
92{
93 auto root = new Tree_Node<int>(0);
94 auto last = root;
95 for (size_t i = 1; i < n; ++i)
96 {
97 auto p = new Tree_Node<int>(int(i));
98 last->insert_rightmost_child(p);
99 last = p;
100 }
101 return root;
102}
103
104// generate_tree() refused the trees of more than Max_Tree_Node_Depth levels
105TEST(Generate_Tree, a_tree_of_any_depth_is_written)
106{
107 for (const size_t n : {Max_Tree_Node_Depth, Max_Tree_Node_Depth + 1, size_t(3000)})
108 {
109 Tree_Node<int> * deep = chain(n);
110 ostringstream out;
111 EXPECT_NO_THROW(generate_tree(deep, out));
112
113 // a line for each node; that of the deepest one has the Dewey number
114 // of its parent, of n - 1 parts
115 const string s = out.str();
116 EXPECT_EQ(size_t(count(s.begin(), s.end(), '\n')), n);
117 const size_t last = s.rfind("Node ");
118 ASSERT_NE(last, string::npos);
119 const string dewey = s.substr(last + 5, s.find(' ', last + 5) - last - 5);
120 EXPECT_EQ(size_t(count(dewey.begin(), dewey.end(), '.')), n - 2);
121
122 destroy_tree(deep);
123 }
124}
125
126// generate_tree_graphviz() recursed as deep as the tree
127TEST(Generate_Tree, graphviz_of_a_deep_tree_needs_no_recursion)
128{
129 constexpr size_t n = 100000;
130 Tree_Node<int> * deep = chain(n);
131 ostringstream out;
133
134 // three lines of header, a label for each node, an arc for each one but
135 // the root, and the closing brace
136 const string s = out.str();
137 EXPECT_EQ(size_t(count(s.begin(), s.end(), '\n')), 3 + n + (n - 1) + 1);
138 EXPECT_EQ(s.substr(s.size() - 2), "}\n");
139
140 destroy_tree(deep);
141}
142
size_t size_t int32_t * out
Definition ca-c-api.h:120
Forward declaration used by CRTP helpers before the full node definition.
#define TEST(name)
Tree visualization and output generation.
TEST_F(Simple_Tree, Generate_ntreepic)
static Tree_Node< int > * chain(size_t n)
__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)
Definition gmpfrxx.h:4138
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.
void generate_tree_graphviz(Node *root, std::ostream &out)
Generate a Graphviz DOT specification for a tree.
Main namespace for Aleph-w library functions.
constexpr size_t Max_Tree_Node_Depth
The depth of the trees that generate_tree() wrote before 7.3.2; it writes trees of any depth now.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
STL namespace.