38#include <gtest/gtest.h>
101 n0 =
g.insert_node(0);
102 n1 =
g.insert_node(1);
103 n2 =
g.insert_node(2);
105 g.insert_arc(
n0,
n1, 10);
106 g.insert_arc(
n1,
n2, 20);
107 g.insert_arc(
n2,
n1, 30);
115TEST(MatgraphDetailTest, IndexArray)
124TEST(MatgraphDetailTest, CheckedIndexArrayValid)
130TEST(MatgraphDetailTest, CheckedIndexArrayOutOfRange)
156 EXPECT_NE(mat(0),
nullptr);
157 EXPECT_NE(mat(1),
nullptr);
158 EXPECT_NE(mat(2),
nullptr);
159 EXPECT_NE(mat(3),
nullptr);
182 std::set<long> indices{idx0, idx1, idx2, idx3};
183 EXPECT_EQ(indices.size(), 4u);
196 EXPECT_NE(mat(i0, i1),
nullptr);
197 EXPECT_NE(mat(i1, i0),
nullptr);
200 EXPECT_NE(mat(i1, i2),
nullptr);
203 EXPECT_EQ(mat(i0, i2),
nullptr);
204 EXPECT_EQ(mat(i2, i0),
nullptr);
207 EXPECT_EQ(mat(i0, i3),
nullptr);
214 EXPECT_NE(mat(n0, n1),
nullptr);
215 EXPECT_NE(mat(n1, n0),
nullptr);
216 EXPECT_NE(mat(n1, n2),
nullptr);
217 EXPECT_NE(mat(n1, n3),
nullptr);
218 EXPECT_EQ(mat(n0, n2),
nullptr);
219 EXPECT_EQ(mat(n0, n3),
nullptr);
252 EXPECT_THROW((
void)mat(10), std::out_of_range);
275 std::set<int> values;
276 for (
size_t i = 0; i < 4; ++i)
277 values.insert(mat(
static_cast<long>(i)));
280 EXPECT_TRUE(values.count(0));
281 EXPECT_TRUE(values.count(1));
282 EXPECT_TRUE(values.count(2));
283 EXPECT_TRUE(values.count(3));
291 long idx0 = -1, idx1 = -1;
292 for (
size_t i = 0; i < 4; ++i)
294 if (mat(
static_cast<long>(i)) == 0) idx0 = i;
295 if (mat(
static_cast<long>(i)) == 1) idx1 = i;
302 EXPECT_EQ(mat(idx0, idx1), 10);
303 EXPECT_EQ(mat(idx1, idx0), 10);
310 long idx0 = -1, idx2 = -1;
311 for (
size_t i = 0; i < 4; ++i)
313 if (mat(
static_cast<long>(i)) == 0) idx0 = i;
314 if (mat(
static_cast<long>(i)) == 2) idx2 = i;
317 EXPECT_EQ(mat(idx0, idx2), -1);
325 EXPECT_EQ(mat(0, 3), 999);
362 EXPECT_NE(mat(0),
nullptr);
363 EXPECT_NE(mat(1),
nullptr);
364 EXPECT_NE(mat(2),
nullptr);
390 EXPECT_EQ(mat(0L, 1L), 100);
391 EXPECT_EQ(mat(1L, 2L), 200);
399 EXPECT_EQ(mat(n0, n1), 100);
408 EXPECT_EQ(cmat(0L, 0L), 42);
435 entry = arc->get_info();
445 long i0 = mat(n0), i1 = mat(n1), i2 = mat(n2);
447 EXPECT_EQ(mat(i0, i1), 10);
448 EXPECT_EQ(mat(i1, i2), 20);
449 EXPECT_EQ(mat(i2, i1), 30);
472 EXPECT_EQ(mat(0L, 0L), 0);
473 EXPECT_EQ(mat(0L, 1L), 1);
474 EXPECT_EQ(mat(1L, 0L), 10);
475 EXPECT_EQ(mat(2L, 2L), 22);
484 using Node_Pair = std::pair<IntGraph::Node *, IntGraph::Node *>;
488 std::vector<Node_Pair> touched_entries;
493 void operator()(Mat & mat,
typename Mat::Arc * a,
long i,
long j,
int & e)
496 touched_entries.emplace_back(mat(i), mat(j));
499 void operator()(Mat & mat,
typename Mat::Arc * a,
long i,
long j,
int & e,
502 (*this)(mat, a, i, j, e);
516 std::pair<std::vector<Node_Pair>, std::vector<Node_Pair>> entries_of(Mat & mat)
518 touched_entries.clear();
519 mat.template operate_all_arcs_list_graph<Note_Entry<Mat>>();
520 std::vector<Node_Pair> without_ptr = touched_entries;
521 touched_entries.clear();
522 mat.template operate_all_arcs_list_graph<Note_Entry<Mat>>(
nullptr);
523 std::vector<Node_Pair> with_ptr = touched_entries;
524 std::sort(without_ptr.begin(), without_ptr.end());
525 std::sort(with_ptr.begin(), with_ptr.end());
526 return {without_ptr, with_ptr};
532 std::vector<Node_Pair> both_ways(
GT & g,
int hidden = 0)
534 std::vector<Node_Pair> want;
536 if (
auto * arc = it.get_curr_ne(); arc->get_info() != hidden)
542 std::sort(want.begin(), want.end());
548 void triangle(
GT & g)
561TEST(AdyMatArcs, BothOverloadsTouchTheEntriesOfTheArcs)
566 const auto [without_ptr, with_ptr] = entries_of(mat);
567 EXPECT_EQ(without_ptr, both_ways(g));
568 EXPECT_EQ(with_ptr, both_ways(g));
573 const auto [d_without_ptr, d_with_ptr] = entries_of(dmat);
574 EXPECT_EQ(d_without_ptr, both_ways(dg));
575 EXPECT_EQ(d_with_ptr, both_ways(dg));
580TEST(AdyMatArcs, TheFilterObjectChoosesTheArcs)
585 const auto [without_ptr, with_ptr] = entries_of(mat);
586 EXPECT_EQ(without_ptr, both_ways(g, 20));
587 EXPECT_EQ(with_ptr, both_ways(g, 20));
590 std::vector<Node_Pair> set;
591 for (
long i = 0; i < 3; ++i)
592 for (
long j = 0; j < 3; ++j)
593 if (
static_cast<int>(
bits(i, j)) != 0)
595 std::sort(set.begin(), set.end());
596 EXPECT_EQ(without_ptr, set);
599 EXPECT_EQ(entries_of(without_null).first, both_ways(g, 30));
605TEST(AdyMatArcs, ACopyKeepsTheEntriesTheNullValueAndTheFilter)
613 EXPECT_EQ(std::as_const(
copy)(0L, 1L), 7);
614 EXPECT_EQ(
copy.null_value(), -1);
615 EXPECT_EQ(entries_of(
copy).first, both_ways(g, 20));
618 assigned(0L, 1L) = 3;
620 EXPECT_EQ(std::as_const(assigned)(0L, 1L), std::as_const(mat)(0L, 1L));
621 EXPECT_EQ(assigned.null_value(), -1);
622 EXPECT_EQ(entries_of(assigned).first, both_ways(g, 20));
628 EXPECT_THROW(of_h = mat, std::domain_error);
664 EXPECT_NE(mat(0),
nullptr);
665 EXPECT_NE(mat(1),
nullptr);
686 long i0 = mat(n0), i1 = mat(n1), i2 = mat(n2), i3 = mat(n3);
689 EXPECT_EQ(
static_cast<int>(mat(i0, i1)), 1);
690 EXPECT_EQ(
static_cast<int>(mat(i1, i0)), 1);
693 EXPECT_EQ(
static_cast<int>(mat(i1, i2)), 1);
696 EXPECT_EQ(
static_cast<int>(mat(i1, i3)), 1);
699 EXPECT_EQ(
static_cast<int>(mat(i0, i2)), 0);
706 long i0 = mat(n0), i2 = mat(n2);
708 EXPECT_EQ(
static_cast<int>(mat(i0, i2)), 0);
711 EXPECT_EQ(
static_cast<int>(mat(i0, i2)), 1);
714 EXPECT_EQ(
static_cast<int>(mat(i0, i2)), 0);
721 EXPECT_EQ(
static_cast<int>(mat(n0, n1)), 1);
722 EXPECT_EQ(
static_cast<int>(mat(n0, n2)), 0);
739 for (
int i = 0; i < 4; ++i)
745 for (
long i = 0; i < 4; ++i)
746 for (
long j = 0; j < 4; ++j)
747 count +=
static_cast<int>(mat(i, j));
752 ASSERT_EQ(bits_set(mat), 6);
754 EXPECT_EQ(bits_set(mat), 0);
758 EXPECT_EQ(bits_set(assigned), 0);
771 EXPECT_EQ(
static_cast<int>(mat(n0, n1)), 1);
772 EXPECT_EQ(
static_cast<int>(mat(n1, n2)), 0);
773 EXPECT_EQ(
static_cast<int>(mat(n1, n3)), 1);
775 mat.set_list_graph(g, Hide_Info{10});
776 EXPECT_EQ(
static_cast<int>(mat(n0, n1)), 0);
777 EXPECT_EQ(
static_cast<int>(mat(n1, n2)), 1);
792 ASSERT_EQ(
static_cast<int>(mat(n1, n2)), 0);
794 EXPECT_EQ(
static_cast<int>(mat(n1, n2)), 1);
797 ASSERT_EQ(
static_cast<int>(mat(n2, n3)), 0);
799 EXPECT_EQ(
static_cast<int>(mat(n2, n3)), 1);
800 EXPECT_EQ(
static_cast<int>(mat(n0, n1)), 1);
810 long i0 = mat1(n0), i1 = mat1(n1);
811 EXPECT_EQ(
static_cast<int>(mat2(i0, i1)),
static_cast<int>(mat1(i0, i1)));
827 EXPECT_THROW((
void)mat(0), std::domain_error);
828 EXPECT_THROW((
void)mat(n0), std::domain_error);
843 long i0 = mat(n0), i1 = mat(n1), i2 = mat(n2);
846 EXPECT_NE(mat(i0, i1),
nullptr);
848 EXPECT_EQ(mat(i1, i0),
nullptr);
851 EXPECT_NE(mat(i1, i2),
nullptr);
853 EXPECT_NE(mat(i2, i1),
nullptr);
860 long i0 = mat(n0), i1 = mat(n1), i2 = mat(n2);
862 EXPECT_EQ(
static_cast<int>(mat(i0, i1)), 1);
863 EXPECT_EQ(
static_cast<int>(mat(i1, i0)), 0);
864 EXPECT_EQ(
static_cast<int>(mat(i1, i2)), 1);
865 EXPECT_EQ(
static_cast<int>(mat(i2, i1)), 1);
872TEST(MatGraphEdgeCases, SingleNodeGraph)
879 EXPECT_EQ(mat(0L, 0L),
nullptr);
884 EXPECT_EQ(amat(0L, 0L), 100);
887TEST(MatGraphEdgeCases, DisconnectedGraph)
893 (void)n0; (void)n1; (void)n2;
899 for (
long i = 0; i < 3; ++i)
900 for (
long j = 0; j < 3; ++j)
901 EXPECT_EQ(mat(i, j),
nullptr);
904TEST(MatGraphEdgeCases, CompleteGraph)
908 for (
int i = 0; i < 4; ++i)
912 for (
int i = 0; i < 4; ++i)
913 for (
int j = i + 1; j < 4; ++j)
920 for (
long i = 0; i < 4; ++i)
921 for (
long j = 0; j < 4; ++j)
923 EXPECT_NE(mat(i, j),
nullptr);
926TEST(MatGraphEdgeCases, SelfLoop)
929 auto* n = g.insert_node(0);
930 g.insert_arc(n, n, 99);
934 EXPECT_NE(mat(idx, idx),
nullptr);
935 EXPECT_EQ(mat(idx, idx)->get_info(), 99);
942TEST(MatGraphStress, LargeGraph)
944 constexpr int N = 100;
947 std::vector<IntDigraph::Node*>
nodes(
N);
949 for (
int i = 0; i <
N; ++i)
950 nodes[i] = g.insert_node(i);
953 for (
int i = 0; i <
N - 1; ++i)
963 for (
int i = 0; i <
N - 1; ++i)
965 long si = amat(
nodes[i]);
966 long ti = amat(
nodes[i + 1]);
967 EXPECT_EQ(amat(si, ti), i);
973 ::testing::InitGoogleTest(&argc, argv);
974 return RUN_ALL_TESTS();
Auxiliary adjacency matrix with custom entry type.
void operate_all_arcs_list_graph()
Apply operation to all arcs from the graph.
void set_null_value(const Entry_Type &null) noexcept
Set the null value.
size_t get_num_nodes() const noexcept
Get number of nodes (matrix dimension)
void operate_all_arcs_matrix()
Apply operation to all matrix entries.
const Entry_Type & null_value() const noexcept
Get the null value.
GT & get_list_graph() noexcept
Get reference to underlying graph.
Bit matrix for graph connectivity.
size_t get_num_nodes() const noexcept
Get number of nodes (matrix dimension)
GT * get_list_graph() noexcept
Get pointer to associated graph (nullptr if none)
void set_list_graph(GT &g, SA sa=SA())
Associate with a graph.
Generic directed graph (digraph) wrapper template.
typename BaseGraph::Arc Arc
typename BaseGraph::Node Node
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Adjacency matrix mapped to an adjacency list graph.
GT & get_list_graph() noexcept
Get reference to underlying graph.
size_t get_num_nodes() const noexcept
Get number of nodes (matrix dimension)
Adjacency matrix storing copies of graph attributes.
const Arc_Type & null_value() const noexcept
Get the null value indicating no arc.
size_t get_num_nodes() const noexcept
Get number of nodes (matrix dimension)
Number Theoretic Transform over Z / MOD Z.
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
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_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
DynArray< Graph::Node * > nodes
long index_array(long i, long j, long n) noexcept
Convert 2D matrix indices to 1D array index.
long checked_index_array(long i, long j, long n)
Validate and convert indices to linear index.
Main namespace for Aleph-w library functions.
constexpr Bit_Column bits(const unsigned n)
Column of n bits without sign: the integers of [0, 2^n - 1].
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.
Filtered iterator on all the arcs of a graph.
Arc of graph implemented with double-linked adjacency lists.
void operator()(Ady_Mat< IntDigraph, int > &mat, IntDigraph::Node *src, IntDigraph::Node *tgt, long i, long j, int &entry)
void operator()(Ady_Mat< IntDigraph, int > &mat, IntDigraph::Arc *arc, long i, long j, int &entry)
Generic graph and digraph implementations.
Adjacency matrix representations for graphs.
TEST_F(MapMatrixGraphTest, Constructor)