From 52860a57867b120b39ee7502231f4a1d885c8cfe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Del=20Pino?= <stephane.delpino44@gmail.com>
Date: Mon, 2 Nov 2020 12:46:05 +0100
Subject: [PATCH] Add CRSGraph tests

Also made a few cosmetic cleanups
---
 src/utils/CRSGraph.hpp    | 19 +++++++++--------
 src/utils/Partitioner.cpp |  9 +++++---
 tests/CMakeLists.txt      |  1 +
 tests/test_CRSGraph.cpp   | 44 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 61 insertions(+), 12 deletions(-)
 create mode 100644 tests/test_CRSGraph.cpp

diff --git a/src/utils/CRSGraph.hpp b/src/utils/CRSGraph.hpp
index 18300f47a..59b74ab57 100644
--- a/src/utils/CRSGraph.hpp
+++ b/src/utils/CRSGraph.hpp
@@ -6,8 +6,8 @@
 class CRSGraph
 {
  private:
-  Array<int> m_entries;
-  Array<int> m_neighbors;
+  Array<const int> m_entries;
+  Array<const int> m_neighbors;
 
  public:
   size_t
@@ -17,29 +17,30 @@ class CRSGraph
     return m_entries.size() - 1;
   }
 
-  const Array<int>&
+  const Array<const int>&
   entries() const
   {
     return m_entries;
   }
 
-  const Array<int>&
+  const Array<const int>&
   neighbors() const
   {
     return m_neighbors;
   }
 
-  CRSGraph& operator=(CRSGraph&&) = default;
-  CRSGraph& operator=(const CRSGraph&) = default;
+  CRSGraph& operator=(CRSGraph&&) = delete;
+  CRSGraph& operator=(const CRSGraph&) = delete;
 
   CRSGraph(const Array<int>& entries, const Array<int>& neighbors) : m_entries(entries), m_neighbors(neighbors)
   {
     Assert(m_entries.size() > 0);
+    Assert(static_cast<size_t>(m_entries[m_entries.size() - 1]) == m_neighbors.size());
   }
 
-  CRSGraph()                = default;
-  CRSGraph(CRSGraph&&)      = default;
-  CRSGraph(const CRSGraph&) = default;
+  CRSGraph()                = delete;
+  CRSGraph(CRSGraph&&)      = delete;
+  CRSGraph(const CRSGraph&) = delete;
   ~CRSGraph()               = default;
 };
 
diff --git a/src/utils/Partitioner.cpp b/src/utils/Partitioner.cpp
index 4e6a45e1c..7a7eb2d90 100644
--- a/src/utils/Partitioner.cpp
+++ b/src/utils/Partitioner.cpp
@@ -57,11 +57,14 @@ Partitioner::partition(const CRSGraph& graph)
     part = Array<int>(local_number_of_nodes);
     std::vector<int> vtxdist{0, local_number_of_nodes};
 
-    const Array<int>& entries   = graph.entries();
-    const Array<int>& neighbors = graph.neighbors();
+    const Array<const int>& entries   = graph.entries();
+    const Array<const int>& neighbors = graph.neighbors();
+
+    int* entries_ptr   = const_cast<int*>(&(entries[0]));
+    int* neighbors_ptr = const_cast<int*>(&(neighbors[0]));
 
     int result =
-      ParMETIS_V3_PartKway(&(vtxdist[0]), &(entries[0]), &(neighbors[0]), NULL, NULL, &wgtflag, &numflag, &ncon, &npart,
+      ParMETIS_V3_PartKway(&(vtxdist[0]), entries_ptr, neighbors_ptr, NULL, NULL, &wgtflag, &numflag, &ncon, &npart,
                            &(tpwgts[0]), &(ubvec[0]), &(options[0]), &edgecut, &(part[0]), &parmetis_comm);
     if (result == METIS_ERROR) {
       throw UnexpectedError("Metis Error");
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 81d7553d5..ff217a345 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -52,6 +52,7 @@ add_executable (unit_tests
   test_CG.cpp
   test_ContinueProcessor.cpp
   test_ConcatExpressionProcessor.cpp
+  test_CRSGraph.cpp
   test_CRSMatrix.cpp
   test_DataVariant.cpp
   test_DoWhileProcessor.cpp
diff --git a/tests/test_CRSGraph.cpp b/tests/test_CRSGraph.cpp
new file mode 100644
index 000000000..1759dd213
--- /dev/null
+++ b/tests/test_CRSGraph.cpp
@@ -0,0 +1,44 @@
+#ifndef TEST_CRS_GRAPH_HPP
+#define TEST_CRS_GRAPH_HPP
+
+#include <catch2/catch.hpp>
+
+#include <utils/CRSGraph.hpp>
+
+// clazy:excludeall=non-pod-global-static
+
+TEST_CASE("CRSGraph", "[utils]")
+{
+  Array<int> entries{5};
+  Array<int> neighbors{9};
+
+  entries[0]   = 0;
+  neighbors[0] = 0;
+  neighbors[1] = 1;
+
+  entries[1]   = 2;
+  neighbors[2] = 1;
+  neighbors[3] = 3;
+
+  entries[2]   = 4;
+  neighbors[4] = 2;
+  neighbors[5] = 1;
+  neighbors[6] = 3;
+
+  entries[3]   = 7;
+  neighbors[7] = 0;
+  neighbors[8] = 1;
+
+  entries[4] = 9;
+
+  CRSGraph graph(entries, neighbors);
+
+  REQUIRE(graph.numberOfNodes() == 4);
+
+  REQUIRE(entries.size() == graph.entries().size());
+  REQUIRE(&entries[0] == &graph.entries()[0]);
+  REQUIRE(neighbors.size() == graph.neighbors().size());
+  REQUIRE(&neighbors[0] == &graph.neighbors()[0]);
+}
+
+#endif   // TEST_CRS_GRAPH_HPP
-- 
GitLab