38#include <gtest/gtest.h>
59TEST(GraphAnode, DefaultConstruction)
67TEST(GraphAnode, ConstructionWithInfo)
75TEST(GraphAnode, CopyConstruction)
83TEST(GraphAnode, MoveConstruction)
94TEST(GraphAarc, DefaultConstruction)
100TEST(GraphAarc, ConstructionWithInfo)
106TEST(GraphAarc, CopyConstruction)
120 EXPECT_EQ(g.
vsize(), 0u);
121 EXPECT_EQ(g.
esize(), 0u);
132 EXPECT_EQ(g.
vsize(), 3u);
133 EXPECT_EQ(n1->get_info(), 1);
134 EXPECT_EQ(n2->get_info(), 2);
135 EXPECT_EQ(n3->get_info(), 3);
146 EXPECT_EQ(g.
esize(), 1u);
147 EXPECT_EQ(arc->get_info(), 10);
161 EXPECT_EQ(n1->num_arcs, 1u);
162 EXPECT_EQ(n2->num_arcs, 1u);
172 EXPECT_EQ(g.
esize(), 1u);
174 EXPECT_EQ(g.
esize(), 0u);
175 EXPECT_EQ(n1->num_arcs, 0u);
176 EXPECT_EQ(n2->num_arcs, 0u);
189 EXPECT_EQ(g.
vsize(), 3u);
190 EXPECT_EQ(g.
esize(), 2u);
194 EXPECT_EQ(g.
vsize(), 2u);
195 EXPECT_EQ(g.
esize(), 0u);
212 EXPECT_EQ(g.
esize(), 5u);
213 EXPECT_EQ(n1->num_arcs, 3u);
223 EXPECT_TRUE(g.is_digraph());
229 auto n1 = g.insert_node(1);
230 auto n2 = g.insert_node(2);
232 g.insert_arc(n1, n2, 10);
235 EXPECT_EQ(n1->num_arcs, 1u);
236 EXPECT_EQ(n2->num_arcs, 0u);
242 auto n1 = g.insert_node(1);
243 auto n2 = g.insert_node(2);
245 g.insert_arc(n1, n2, 12);
246 g.insert_arc(n2, n1, 21);
248 EXPECT_EQ(g.esize(), 2u);
249 EXPECT_EQ(n1->num_arcs, 1u);
250 EXPECT_EQ(n2->num_arcs, 1u);
266 for (IntGraph::Node_Iterator it(g); it.has_curr(); it.next())
268 sum += it.get_curr()->get_info();
288 for (IntGraph::Arc_Iterator it(g); it.has_curr(); it.next())
290 sum += it.get_curr()->get_info();
312 for (IntGraph::Node_Arc_Iterator it(n1); it.has_curr(); it.next())
314 sum += it.get_curr()->get_info();
329 for (
int k = 1;
k <= 5; ++
k)
336 for (
auto p : {hub, leaf, lonely})
338 const long n = long(p->num_arcs);
339 IntGraph::Node_Arc_Iterator it(p);
342 EXPECT_EQ(it.get_base(), p->arc_array);
343 EXPECT_EQ(std::as_const(it).get_base(), p->arc_array);
346 auto curr_of = [](
auto & i) ->
long
352 catch (
const std::underflow_error &)
356 catch (
const std::overflow_error &)
363 auto run = [](
auto op) ->
int
370 catch (
const std::underflow_error &)
374 catch (
const std::overflow_error &)
380 for (
int step = 0; step < 400; ++step)
382 const int what = int(gen() % 9);
383 const long new_pos = long(gen() % (n + 3)) - 1;
387 EXPECT_EQ(run([&] { it.next(); }), run([&] { ref.
next(); }));
390 EXPECT_EQ(run([&] { it.prev(); }), run([&] { ref.
prev(); }));
428 ASSERT_EQ(it.has_curr(), ref.
has_curr()) <<
"step " << step;
429 ASSERT_EQ(it.is_last(), ref.
is_last()) <<
"step " << step;
430 ASSERT_EQ(it.get_pos(), ref.
get_pos()) <<
"step " << step;
431 ASSERT_EQ(curr_of(it), curr_of(ref)) <<
"step " << step;
435 ASSERT_EQ(it.get_current_arc_ne(), ref.
get_curr_ne());
436 ASSERT_EQ(it.get_tgt_node(),
438 ASSERT_EQ(it.get_tgt_node_ne(), it.get_tgt_node());
449 for (
int k = 1;
k <= 6; ++
k)
460 std::vector<int> forward, backward;
462 for (; it.has_curr(); it.
next())
463 forward.push_back(it.get_curr()->get_info());
465 backward.push_back(it.get_curr()->get_info());
467 EXPECT_EQ(forward, (std::vector<int>{20, 40, 60}));
468 EXPECT_EQ(backward, (std::vector<int>{60, 40, 20}));
484 EXPECT_EQ(g2.
vsize(), 2u);
485 EXPECT_EQ(g2.
esize(), 1u);
496 EXPECT_EQ(g2.
vsize(), 2u);
497 EXPECT_EQ(g1.
vsize(), 0u);
510 EXPECT_EQ(g2.
vsize(), 2u);
511 EXPECT_EQ(g2.
esize(), 1u);
523 EXPECT_EQ(g2.
vsize(), 2u);
524 EXPECT_EQ(g1.
vsize(), 0u);
534 size_t orig_nodes = g.
vsize();
535 size_t orig_arcs = g.
esize();
539 EXPECT_EQ(g.
vsize(), orig_nodes);
540 EXPECT_EQ(g.
esize(), orig_arcs);
556 copy.insert_node(999);
569 for (
int i = 0; i < 10; ++i)
572 size_t orig_nodes = g.
vsize();
573 size_t orig_arcs = g.
esize();
578 EXPECT_EQ(target.
vsize(), orig_nodes);
579 EXPECT_EQ(target.
esize(), orig_arcs);
594 return a->get_info() < b->get_info();
597 std::vector<int> values;
598 for (IntGraph::Node_Iterator it(g); it.has_curr(); it.next())
599 values.push_back(it.get_curr()->get_info());
601 EXPECT_EQ(values, (std::vector<int>{1, 2, 3}));
615 return a->get_info() < b->get_info();
618 std::vector<int> values;
619 for (IntGraph::Arc_Iterator it(g); it.has_curr(); it.next())
620 values.push_back(it.get_curr()->get_info());
622 EXPECT_EQ(values, (std::vector<int>{10, 20, 30}));
632 constexpr int N = 100;
635 std::vector<IntGraph::Node*>
nodes;
636 for (
int i = 0; i <
N; ++i)
639 EXPECT_EQ(g.
vsize(),
static_cast<size_t>(
N));
642 for (
int i = 0; i <
N; ++i)
643 for (
int j = i + 1; j <
N; ++j)
646 EXPECT_EQ(g.
esize(),
static_cast<size_t>(
N * (
N - 1) / 2));
652 constexpr int N = 50;
654 std::vector<IntDigraph::Node*>
nodes;
655 for (
int i = 0; i <
N; ++i)
656 nodes.push_back(g.insert_node(i));
659 for (
int i = 0; i <
N; ++i)
660 for (
int j = 0; j <
N; ++j)
664 EXPECT_EQ(g.esize(),
static_cast<size_t>(
N * (
N - 1)));
678 EXPECT_EQ(g.
esize(), 1u);
679 EXPECT_EQ(n1->num_arcs, 1u);
691 EXPECT_EQ(g.
esize(), 1u);
694 EXPECT_EQ(g.
esize(), 0u);
695 EXPECT_EQ(n1->num_arcs, 0u);
698 EXPECT_EQ(g.
esize(), 1u);
699 EXPECT_EQ(n1->num_arcs, 1u);
710 for (IntGraph::Node_Iterator it(g); it.has_curr(); it.next())
712 EXPECT_EQ(node_count, 0);
715 for (IntGraph::Arc_Iterator it(g); it.has_curr(); it.next())
717 EXPECT_EQ(arc_count, 0);
772 auto hub = g.insert_node(0);
773 std::vector<IntDigraph::Arc *>
arcs;
774 for (
int i = 1; i <= 40; ++i)
775 arcs.push_back(g.insert_arc(hub, g.insert_node(i), i));
778 EXPECT_EQ(g.get_num_arcs(hub), 0u);
781 auto n = g.get_first_node();
782 auto a = g.insert_arc(hub, n, 1);
783 EXPECT_EQ(g.get_num_arcs(hub), 1u);
784 EXPECT_EQ(g.get_first_arc(hub), a);
799 EXPECT_EQ(n1->get_info(),
"node1");
800 EXPECT_EQ(n2->get_info(),
"node2");
810TEST(DigraphWrapper, ArrayDigraphInheritsFromBaseGraph)
814 auto n1 = dg.insert_node(1);
815 auto n2 = dg.insert_node(2);
816 auto arc = dg.insert_arc(n1, n2, 100);
818 EXPECT_EQ(dg.vsize(), 2u);
819 EXPECT_EQ(dg.esize(), 1u);
820 EXPECT_TRUE(dg.is_digraph());
821 EXPECT_EQ(dg.get_src_node(arc), n1);
822 EXPECT_EQ(dg.get_tgt_node(arc), n2);
825TEST(DigraphWrapper, ArrayDigraphCopyPreservesDirectedFlag)
832 EXPECT_TRUE(dg2.is_digraph());
833 EXPECT_EQ(dg2.vsize(), 2u);
836TEST(DigraphWrapper, ArrayDigraphMovePreservesDirectedFlag)
843 EXPECT_TRUE(dg2.is_digraph());
844 EXPECT_EQ(dg2.vsize(), 2u);
847TEST(DigraphWrapper, ArrayDigraphAssignmentPreservesDirectedFlag)
856 EXPECT_TRUE(dg2.is_digraph());
857 EXPECT_EQ(dg2.vsize(), 1u);
860TEST(DigraphWrapper, ArrayDigraphMoveAssignment)
866 dg2 = std::move(dg1);
867 EXPECT_TRUE(dg2.is_digraph());
868 EXPECT_EQ(dg2.vsize(), 1u);
877 "Array_Graph::Node_Iterator must satisfy BasicGraphIterator");
879 "Array_Graph::Arc_Iterator must satisfy BasicGraphIterator");
881 "Array_Graph::Node_Iterator must satisfy GraphNodeIterator");
883 "Array_Graph::Arc_Iterator must satisfy GraphArcIterator");
885TEST(ConceptsTest, ArrayGraphIteratorsSatisfyConcepts)
894 EXPECT_TRUE(nit.has_curr());
895 EXPECT_NE(nit.get_curr(),
nullptr);
899 EXPECT_TRUE(ait.has_curr());
900 EXPECT_NE(ait.get_curr(),
nullptr);
Iterator wrapper for C++ raw arrays.
void next_ne() noexcept
Advance to the next item without bounds checking.
void reset_last() noexcept
Reset the iterator to the last item.
void reset() noexcept
Reset the iterator to the first item.
T & get_curr_ne() const noexcept
Get the current item without bounds checking.
void set_pos(const long new_pos) noexcept
Move the iterator to the logical position new_pos.
void end() noexcept
Put the iterator at the end (past the last item).
void prev()
Move to the previous item with bounds checking.
void prev_ne() noexcept
Move to the previous item without bounds checking.
void next()
Advance to the next item with bounds checking.
void reset_first() noexcept
Reset the iterator to the first item (alias for reset()).
bool has_curr() const noexcept
Check if there is a current valid item.
bool is_last() const noexcept
Check if positioned at the last item.
constexpr long get_pos() const noexcept
Get the current position index.
Generic directed graph (digraph) wrapper template.
void reset_last()
Resets the iterator to the last filtered element.
void next()
Advances the iterator to the next filtered element.
void prev()
Moves the iterator backward to the previous filtered element.
Node * get_first_node() const
Return any node in the graph.
Arc * get_first_arc(Node *node) const
Return any arc adjacent to a node.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
virtual Arc * connect_arc(Arc *arc) noexcept
Connect a previously disconnected arc to the graph.
virtual void remove_arc(Arc *arc) noexcept
Remove an arc from the graph and free it.
virtual void remove_node(Node *node)
Remove a node from the graph and free its memory.
virtual void disconnect_arc(Arc *arc) noexcept
Disconnect an arc from graph.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Number Theoretic Transform over Z / MOD Z.
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
size_t num_arcs
data associated to the node. Access it with get_info()
void sort_arcs(Compare &cmp) noexcept(nothrow_cmp_v< Compare, Arc >)
Sort all the arcs of the graph according to a specific criteria.
void sort_nodes(Compare &cmp) noexcept(nothrow_cmp_v< Compare, Node >)
Sort all the nodes of the graph according to a specific criteria.
auto get_arc_it() const noexcept
Obtains an iterator to the arc of graph.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
bool is_digraph() const noexcept
Return true if the graph this is directed.
Node * get_connected_node(Arc *arc, Node *node) const noexcept
Return the adjacent node to node through arc.
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
constexpr size_t vsize() const noexcept
See get_num_nodes().
size_t esize() const noexcept
Return the total of arcs of graph.
auto get_node_it() const noexcept
Obtains an iterator to the nodes of graph.
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Concept for basic graph iterators.
Concept for graph arc iterators.
Concept for graph node iterators.
Concept for node adjacency iterators.
DynArray< Graph::Node * > nodes
DynArray< Graph::Arc * > arcs
Main namespace for Aleph-w library functions.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Arc of graph implemented with double-linked adjacency lists.
Filtered iterator of adjacent arcs of a node.
Array-based graph implementation.