38#include <gtest/gtest.h>
48std::string make_nodes_csv()
51 "id|plazo|class|f3|nes|power|label\n"
52 "A|cp|it|x|12|5|Alpha\n"
53 "B|mp|ef|x|34|7|Beta\n"
54 "C|lp|pp|x|56|9|Gamma\n";
57std::string make_arcs_csv()
93TEST(LoadDigraph, LoadsNodesAndArcsAndSkipsShortRows)
96 std::stringstream
nodes(make_nodes_csv());
98 std::stringstream
arcs(make_arcs_csv());
103 EXPECT_EQ(g.get_num_nodes(), 3U);
104 EXPECT_EQ(g.get_num_arcs(), 3U);
107 ASSERT_NE(a,
nullptr);
108 EXPECT_EQ(a->get_info().first,
"A");
109 EXPECT_EQ(a->get_info().second[6],
"Alpha");
112TEST(LoadDigraph, CreatesMissingNodesWhenParsingArcs)
115 std::stringstream
nodes;
116 std::stringstream
arcs;
122 EXPECT_EQ(g.get_num_nodes(), 2U);
123 EXPECT_EQ(g.get_num_arcs(), 1U);
127 ASSERT_NE(x,
nullptr);
128 EXPECT_TRUE(x->get_info().second.is_empty());
131TEST(LoadDigraph, EmptyInputsProduceEmptyGraph)
134 std::stringstream
nodes;
135 std::stringstream
arcs;
140 EXPECT_EQ(g.get_num_nodes(), 0U);
141 EXPECT_EQ(g.get_num_arcs(), 0U);
144TEST(LoadDigraph, LinesWithExtraSeparatorsAreIgnored)
147 std::stringstream
nodes;
148 nodes <<
"id|plazo|class|f3|nes|power|label\n";
150 nodes <<
"E|cp|it|x|44|11|Echo\n";
152 std::stringstream
arcs;
158 EXPECT_EQ(g.get_num_nodes(), 2U);
159 EXPECT_EQ(g.get_num_arcs(), 1U);
162 ASSERT_NE(d,
nullptr);
163 EXPECT_TRUE(d->get_info().second.is_empty());
166 ASSERT_NE(e,
nullptr);
167 EXPECT_EQ(e->get_info().second.size(), 7U);
170TEST(GenerateDotFile, ProducesWarningWhenCycleExists)
173 std::stringstream
nodes(make_nodes_csv());
174 std::stringstream
arcs(make_arcs_csv());
185 std::stringstream
out;
187 const auto dot =
out.str();
189 EXPECT_NE(dot.find(
"WARNING: Cycle detected"), std::string::npos);
190 EXPECT_NE(dot.find(
"A [color=Green"), std::string::npos);
191 EXPECT_NE(dot.find(
"\\nP=5"), std::string::npos);
192 EXPECT_NE(dot.find(
"\\n12%"), std::string::npos);
193 EXPECT_NE(dot.find(
"shape = box"), std::string::npos);
196TEST(GenerateDotFile, EmitsTopologicalRanksWhenAcyclic)
199 std::stringstream
nodes(make_nodes_csv());
200 std::stringstream
arcs;
212 std::stringstream
out;
214 const auto dot =
out.str();
216 EXPECT_EQ(dot.find(
"WARNING: Cycle detected"), std::string::npos);
217 EXPECT_NE(dot.find(
"{ rank = same;"), std::string::npos);
218 EXPECT_NE(dot.find(
"A -> B"), std::string::npos);
219 EXPECT_NE(dot.find(
"A -> C"), std::string::npos);
222TEST(GenerateDotFile, RespectsOnlyNumFlagWhenTrue)
225 std::stringstream
nodes(make_nodes_csv());
226 std::stringstream
arcs;
237 std::stringstream
out;
239 const auto dot =
out.str();
242 EXPECT_EQ(dot.find(
"Alpha"), std::string::npos);
243 EXPECT_EQ(dot.find(
"P="), std::string::npos);
244 EXPECT_EQ(dot.find(
"%"), std::string::npos);
245 EXPECT_NE(dot.find(
"fontsize = 10"), std::string::npos);
250TEST(Split, BasicSplitWithSingleSeparator)
253 split(
"a|b|c",
"|", words);
254 ASSERT_EQ(words.
size(), 3U);
255 EXPECT_EQ(words[0],
"a");
256 EXPECT_EQ(words[1],
"b");
257 EXPECT_EQ(words[2],
"c");
260TEST(Split, SplitWithMultipleSeparators)
263 split(
"a,b c d",
" ,", words);
264 ASSERT_EQ(words.
size(), 4U);
265 EXPECT_EQ(words[0],
"a");
266 EXPECT_EQ(words[1],
"b");
267 EXPECT_EQ(words[2],
"c");
268 EXPECT_EQ(words[3],
"d");
274 split(
"",
"|", words);
275 EXPECT_EQ(words.
size(), 0U);
281 split(
"|||",
"|", words);
282 EXPECT_EQ(words.
size(), 0U);
288 split(
"hello",
"|", words);
289 ASSERT_EQ(words.
size(), 1U);
290 EXPECT_EQ(words[0],
"hello");
293TEST(Split, LeadingAndTrailingSeparators)
296 split(
"|a|b|",
"|", words);
297 ASSERT_EQ(words.
size(), 2U);
298 EXPECT_EQ(words[0],
"a");
299 EXPECT_EQ(words[1],
"b");
313 EXPECT_TRUE(
eq(n1, n2));
314 EXPECT_FALSE(
eq(n1, n3));
317TEST(LoadDigraph, ArcsWithCommaSeparator)
320 std::stringstream
nodes;
321 std::stringstream
arcs;
328 EXPECT_EQ(g.get_num_nodes(), 3U);
329 EXPECT_EQ(g.get_num_arcs(), 2U);
332TEST(LoadDigraph, DuplicateNodeIdsInArcs)
335 std::stringstream
nodes(make_nodes_csv());
336 std::stringstream
arcs;
342 EXPECT_EQ(g.get_num_nodes(), 3U);
343 EXPECT_EQ(g.get_num_arcs(), 1U);
346 bool found_self_loop =
false;
349 auto *a = it.get_curr();
350 if (g.get_src_node(a) == g.get_tgt_node(a))
351 found_self_loop =
true;
353 EXPECT_TRUE(found_self_loop);
356TEST(LoadDigraph, MultipleArcsWithSameEndpoints)
359 std::stringstream
nodes;
360 std::stringstream
arcs;
367 EXPECT_EQ(g.get_num_nodes(), 2U);
368 EXPECT_EQ(g.get_num_arcs(), 2U);
371TEST(GenerateDotFile, AllColorsAndShapes)
374 std::stringstream
nodes;
375 nodes <<
"id|plazo|class|f3|nes|power|label\n"
376 <<
"A|cp|it|x|10|1|Node1\n"
377 <<
"B|mp|ef|x|20|2|Node2\n"
378 <<
"C|lp|pp|x|30|3|Node3\n"
379 <<
"D|unknown|unknown|x|40|4|Node4\n";
381 std::stringstream
arcs;
395 std::stringstream
out;
397 const auto dot =
out.str();
400 EXPECT_NE(dot.find(
"A [color=Green"), std::string::npos);
401 EXPECT_NE(dot.find(
"B [color=Yellow"), std::string::npos);
402 EXPECT_NE(dot.find(
"C [color=Red"), std::string::npos);
405 EXPECT_NE(dot.find(
"shape = box"), std::string::npos);
406 EXPECT_NE(dot.find(
"shape = ellipse"), std::string::npos);
407 EXPECT_NE(dot.find(
"shape = hexagon"), std::string::npos);
410 EXPECT_NE(dot.find(
"P=1"), std::string::npos);
411 EXPECT_NE(dot.find(
"P=2"), std::string::npos);
412 EXPECT_NE(dot.find(
"10%"), std::string::npos);
413 EXPECT_NE(dot.find(
"20%"), std::string::npos);
416TEST(GenerateDotFile, SingleNodeGraph)
419 std::stringstream
nodes;
420 nodes <<
"id|plazo|class|f3|nes|power|label\n"
421 <<
"X|cp|it|x|50|5|Single\n";
422 std::stringstream
arcs;
429 std::stringstream
out;
431 const auto dot =
out.str();
433 EXPECT_NE(dot.find(
"X [color=Green"), std::string::npos);
434 EXPECT_NE(dot.find(
"Single"), std::string::npos);
435 EXPECT_EQ(dot.find(
"->"), std::string::npos);
438TEST(GenerateDotFile, DisconnectedComponents)
441 std::stringstream
nodes;
442 nodes <<
"id|plazo|class|f3|nes|power|label\n"
443 <<
"A|cp|it|x|10|1|A\n"
444 <<
"B|mp|ef|x|20|2|B\n"
445 <<
"C|lp|pp|x|30|3|C\n"
446 <<
"D|cp|it|x|40|4|D\n";
447 std::stringstream
arcs;
456 std::stringstream
out;
458 const auto dot =
out.str();
460 EXPECT_EQ(dot.find(
"WARNING: Cycle"), std::string::npos);
461 EXPECT_NE(dot.find(
"A -> B"), std::string::npos);
462 EXPECT_NE(dot.find(
"C -> D"), std::string::npos);
465TEST(SearchNode, FindsExistingNode)
471 ASSERT_NE(found,
nullptr);
472 EXPECT_EQ(found->get_info().first,
"existing");
475 EXPECT_EQ(g.get_num_nodes(), 1U);
478TEST(SearchNode, CreatesNewNodeIfNotFound)
483 ASSERT_NE(created,
nullptr);
484 EXPECT_EQ(created->get_info().first,
"new_node");
485 EXPECT_TRUE(created->get_info().second.is_empty());
486 EXPECT_EQ(g.get_num_nodes(), 1U);
489TEST(MinNodeFields, HasCorrectValue)
497 std::stringstream
nodes;
498 nodes <<
"id|plazo|class|f3|nes|power|label\n";
500 constexpr size_t NUM_NODES = 100;
501 for (
size_t i = 0; i < NUM_NODES; ++i)
503 nodes <<
"N" << i <<
"|cp|it|x|10|5|Node" << i <<
"\n";
506 std::stringstream
arcs;
507 for (
size_t i = 0; i < NUM_NODES - 1; ++i)
509 arcs <<
"N" << i <<
" N" << (i + 1) <<
"\n";
515 EXPECT_EQ(g.get_num_nodes(), NUM_NODES);
516 EXPECT_EQ(g.get_num_arcs(), NUM_NODES - 1);
519 std::stringstream
out;
521 const auto dot =
out.str();
522 EXPECT_EQ(dot.find(
"WARNING: Cycle"), std::string::npos);
size_t size_t int32_t * out
Generic directed graph (digraph) wrapper template.
Dynamic doubly linked list with O(1) size and bidirectional access.
const size_t & size() const noexcept
Return the number of elements (constant time)
T & append(const T &item)
Append a copied item at the end of the list.
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
DynArray< Graph::Node * > nodes
DynArray< Graph::Arc * > arcs
bool with_power
Configuration options for DOT file generation.
bool with_class
If true, set node shapes based on class field.
Digrafo::Node * search_node(Digrafo &g, const std::string &s)
Find or create a node by ID.
void load_digraph(Digrafo &g, std::istream &nodes_input, std::istream &arcs_input)
Load nodes and arcs from streams into the directed graph.
bool only_num
If true, show only the node number, not the full label.
size_t font_size
Font size for the DOT output.
void generate_dot_file(Digrafo &g, std::ostream &output)
Generate a DOT representation of the digraph.
bool with_nes
If true, include NES percentage in node labels.
Utilities for loading directed graphs from pipe-separated files.
Main namespace for Aleph-w library functions.
bool eq(const C1 &c1, const C2 &c2, Eq e=Eq())
Check equality of two containers using a predicate.
constexpr size_t MIN_NODE_FIELDS
Minimum expected columns for a node record.
std::pair< std::string, DynDlist< std::string > > Info_Nodo
Node payload: (id, fields) where fields come from the input row.
std::vector< std::string > & split(const std::string &s, const char delim, std::vector< std::string > &elems)
Split a std::string by a single delimiter character.
Filtered iterator on all the arcs of a graph.
Functor to compare nodes by their ID.