From db531ee3da0fa8d55a65b596b818abcd350c15f9 Mon Sep 17 00:00:00 2001
From: Stephane Del Pino <stephane.delpino44@gmail.com>
Date: Fri, 7 Sep 2018 10:24:19 +0200
Subject: [PATCH] Add unit test for Array

---
 tests/CMakeLists.txt |  1 +
 tests/test_Array.cpp | 87 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 88 insertions(+)
 create mode 100644 tests/test_Array.cpp

diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 03480e1d6..b6c12e701 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -4,6 +4,7 @@ set(EXECUTABLE_OUTPUT_PATH ${PASTIS_BINARY_DIR})
 
 add_executable (unit_tests
   test_main.cpp
+  test_Array.cpp
   test_ItemType.cpp
   test_PastisAssert.cpp
   test_RevisionInfo.cpp
diff --git a/tests/test_Array.cpp b/tests/test_Array.cpp
new file mode 100644
index 000000000..6cd02146c
--- /dev/null
+++ b/tests/test_Array.cpp
@@ -0,0 +1,87 @@
+#include <catch.hpp>
+
+#include <PastisAssert.hpp>
+#include <Array.hpp>
+#include <Types.hpp>
+
+// Instantiate to ensure full coverage is performed
+template class Array<int>;
+
+TEST_CASE("Array", "[utils]") {
+
+  Array<int> a(10);
+  REQUIRE(a.size() == 10);
+
+  for (size_t i=0; i<a.size(); ++i) {
+    a[i] = 2*i;
+  }
+
+  REQUIRE(((a[0] == 0) and (a[1] == 2) and
+           (a[2] == 4) and (a[3] == 6) and
+           (a[4] == 8) and (a[5] ==10) and
+           (a[6] ==12) and (a[7] ==14) and
+           (a[8] ==16) and (a[9] ==18)));
+
+  SECTION("checking for copies") {
+    Array<const int> b{a};
+
+    REQUIRE(((b[0] == 0) and (b[1] == 2) and
+             (b[2] == 4) and (b[3] == 6) and
+             (b[4] == 8) and (b[5] ==10) and
+             (b[6] ==12) and (b[7] ==14) and
+             (b[8] ==16) and (b[9] ==18)));
+
+    Array<int> c{a};
+
+    REQUIRE(((c[0] == 0) and (c[1] == 2) and
+             (c[2] == 4) and (c[3] == 6) and
+             (c[4] == 8) and (c[5] ==10) and
+             (c[6] ==12) and (c[7] ==14) and
+             (c[8] ==16) and (c[9] ==18)));
+
+
+    Array<int> d = std::move(c);
+
+    REQUIRE(((d[0] == 0) and (d[1] == 2) and
+             (d[2] == 4) and (d[3] == 6) and
+             (d[4] == 8) and (d[5] ==10) and
+             (d[6] ==12) and (d[7] ==14) and
+             (d[8] ==16) and (d[9] ==18)));
+
+  }
+
+  SECTION("checking for affectations") {
+    Array<const int> b;
+    b = a;
+
+    REQUIRE(((b[0] == 0) and (b[1] == 2) and
+             (b[2] == 4) and (b[3] == 6) and
+             (b[4] == 8) and (b[5] ==10) and
+             (b[6] ==12) and (b[7] ==14) and
+             (b[8] ==16) and (b[9] ==18)));
+
+
+    Array<int> c;
+    c = a;
+
+    REQUIRE(((c[0] == 0) and (c[1] == 2) and
+             (c[2] == 4) and (c[3] == 6) and
+             (c[4] == 8) and (c[5] ==10) and
+             (c[6] ==12) and (c[7] ==14) and
+             (c[8] ==16) and (c[9] ==18)));
+
+    Array<int> d;
+    d = std::move(c);
+
+    REQUIRE(((d[0] == 0) and (d[1] == 2) and
+             (d[2] == 4) and (d[3] == 6) and
+             (d[4] == 8) and (d[5] ==10) and
+             (d[6] ==12) and (d[7] ==14) and
+             (d[8] ==16) and (d[9] ==18)));
+  }
+#ifndef NDEBUG
+  SECTION("checking for bounds violation") {
+    REQUIRE_THROWS_AS(a[10], AssertError);
+  }
+#endif // NDEBUG
+}
-- 
GitLab