From 784790bd16fcc005796c487998502d5ddc64c979 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Del=20Pino?= <stephane.delpino44@gmail.com> Date: Fri, 11 Feb 2022 14:21:22 +0100 Subject: [PATCH] Allow friend access of non-const versions to const versions --- src/mesh/ItemArray.hpp | 6 ++++ src/mesh/ItemValue.hpp | 6 ++++ tests/test_ItemArray.cpp | 33 ++++++++++++++++++++-- tests/test_ItemValue.cpp | 45 ++++++++++++++++++++++++------ tests/test_SubItemArrayPerItem.cpp | 4 ++- tests/test_SubItemValuePerItem.cpp | 3 +- 6 files changed, 85 insertions(+), 12 deletions(-) diff --git a/src/mesh/ItemArray.hpp b/src/mesh/ItemArray.hpp index 5a16861a3..3bd7de633 100644 --- a/src/mesh/ItemArray.hpp +++ b/src/mesh/ItemArray.hpp @@ -36,6 +36,12 @@ class ItemArray // Allow const std:weak_ptr version to access our data friend ItemArray<std::add_const_t<DataType>, item_type, ConnectivityWeakPtr>; + // Allow non-const std:shared_ptr version to access our data + friend ItemArray<std::remove_const_t<DataType>, item_type, ConnectivitySharedPtr>; + + // Allow non-const std:weak_ptr version to access our data + friend ItemArray<std::remove_const_t<DataType>, item_type, ConnectivityWeakPtr>; + public: friend PUGS_INLINE ItemArray<std::remove_const_t<DataType>, item_type, ConnectivityPtr> copy(const ItemArray<DataType, item_type, ConnectivityPtr>& source) diff --git a/src/mesh/ItemValue.hpp b/src/mesh/ItemValue.hpp index d48468f39..981ebcc19 100644 --- a/src/mesh/ItemValue.hpp +++ b/src/mesh/ItemValue.hpp @@ -36,6 +36,12 @@ class ItemValue // Allow const std:weak_ptr version to access our data friend ItemValue<std::add_const_t<DataType>, item_type, ConnectivityWeakPtr>; + // Allow non-const std:shared_ptr version to access our data + friend ItemValue<std::remove_const_t<DataType>, item_type, ConnectivitySharedPtr>; + + // Allow non-const std:weak_ptr version to access our data + friend ItemValue<std::remove_const_t<DataType>, item_type, ConnectivityWeakPtr>; + public: friend PUGS_INLINE ItemValue<std::remove_const_t<DataType>, item_type, ConnectivityPtr> copy(const ItemValue<DataType, item_type, ConnectivityPtr>& source) diff --git a/tests/test_ItemArray.cpp b/tests/test_ItemArray.cpp index 3422d1da9..9b3b95fde 100644 --- a/tests/test_ItemArray.cpp +++ b/tests/test_ItemArray.cpp @@ -232,12 +232,41 @@ TEST_CASE("ItemArray", "[mesh]") const Connectivity<2>& connectivity = mesh_2d->connectivity(); WeakFaceArray<int> weak_face_array{connectivity, 5}; - - weak_face_array.fill(parallel::rank()); + for (FaceId face_id = 0; face_id < mesh_2d->numberOfFaces(); ++face_id) { + for (size_t i = 0; i < weak_face_array.sizeOfArrays(); ++i) { + weak_face_array[face_id][i] = 2 * face_id + 3 * i; + } + } FaceArray<const int> face_array{weak_face_array}; REQUIRE(face_array.connectivity_ptr() == weak_face_array.connectivity_ptr()); + + FaceArray<int> copied_face_array = copy(weak_face_array); + REQUIRE(copied_face_array.connectivity_ptr() == weak_face_array.connectivity_ptr()); + REQUIRE(weak_face_array.sizeOfArrays() == copied_face_array.sizeOfArrays()); + + weak_face_array.fill(0); + + { + bool is_same = true; + for (FaceId face_id = 0; face_id < mesh_2d->numberOfFaces(); ++face_id) { + for (size_t i = 0; i < face_array.sizeOfArrays(); ++i) { + is_same &= (face_array[face_id][i] == 0); + } + } + REQUIRE(is_same); + } + + { + bool is_same = true; + for (FaceId face_id = 0; face_id < mesh_2d->numberOfFaces(); ++face_id) { + for (size_t i = 0; i < copied_face_array.sizeOfArrays(); ++i) { + is_same &= (copied_face_array[face_id][i] == static_cast<int>(2 * face_id + 3 * i)); + } + } + REQUIRE(is_same); + } } } } diff --git a/tests/test_ItemValue.cpp b/tests/test_ItemValue.cpp index 84778227a..5ec3fdb89 100644 --- a/tests/test_ItemValue.cpp +++ b/tests/test_ItemValue.cpp @@ -130,8 +130,12 @@ TEST_CASE("ItemValue", "[mesh]") cell_value = values; - for (CellId i_cell = 0; i_cell < mesh_3d->numberOfCells(); ++i_cell) { - REQUIRE(cell_value[i_cell] == i_cell); + { + bool is_same = true; + for (CellId i_cell = 0; i_cell < mesh_3d->numberOfCells(); ++i_cell) { + is_same &= (cell_value[i_cell] == i_cell); + } + REQUIRE(is_same); } } } @@ -156,12 +160,20 @@ TEST_CASE("ItemValue", "[mesh]") cell_value.fill(0); - for (CellId i_cell = 0; i_cell < mesh_3d->numberOfCells(); ++i_cell) { - REQUIRE(cell_value[i_cell] == 0); + { + bool is_same = true; + for (CellId i_cell = 0; i_cell < mesh_3d->numberOfCells(); ++i_cell) { + is_same &= (cell_value[i_cell] == 0); + } + REQUIRE(is_same); } - for (CellId i_cell = 0; i_cell < mesh_3d->numberOfCells(); ++i_cell) { - REQUIRE(const_cell_value[i_cell] == static_cast<std::int64_t>(parallel::rank())); + { + bool is_same = true; + for (CellId i_cell = 0; i_cell < mesh_3d->numberOfCells(); ++i_cell) { + is_same &= (const_cell_value[i_cell] == static_cast<std::int64_t>(parallel::rank())); + } + REQUIRE(is_same); } } } @@ -179,12 +191,29 @@ TEST_CASE("ItemValue", "[mesh]") const Connectivity<2>& connectivity = mesh_2d->connectivity(); WeakFaceValue<int> weak_face_value{connectivity}; - - weak_face_value.fill(parallel::rank()); + for (FaceId face_id = 0; face_id < mesh_2d->numberOfFaces(); ++face_id) { + weak_face_value[face_id] = 2 * face_id + 1; + } FaceValue<const int> face_value{weak_face_value}; REQUIRE(face_value.connectivity_ptr() == weak_face_value.connectivity_ptr()); + + FaceValue<int> copied_face_value = copy(weak_face_value); + REQUIRE(copied_face_value.connectivity_ptr() == weak_face_value.connectivity_ptr()); + + weak_face_value.fill(0); + for (FaceId face_id = 0; face_id < mesh_2d->numberOfFaces(); ++face_id) { + REQUIRE(face_value[face_id] == static_cast<int>(0)); + } + + { + bool is_same = true; + for (FaceId face_id = 0; face_id < mesh_2d->numberOfFaces(); ++face_id) { + is_same &= (copied_face_value[face_id] == static_cast<int>(2 * face_id + 1)); + } + REQUIRE(is_same); + } } } } diff --git a/tests/test_SubItemArrayPerItem.cpp b/tests/test_SubItemArrayPerItem.cpp index 23be4a345..71a226f01 100644 --- a/tests/test_SubItemArrayPerItem.cpp +++ b/tests/test_SubItemArrayPerItem.cpp @@ -835,7 +835,9 @@ TEST_CASE("SubItemArrayPerItem", "[mesh]") } } - NodeArrayPerCell<size_t> copy_node_array_per_cell = copy(node_array_per_cell); + WeakNodeArrayPerCell<const size_t> node_const_array_per_cell = node_array_per_cell; + + NodeArrayPerCell<size_t> copy_node_array_per_cell = copy(node_const_array_per_cell); { bool is_same = true; for (size_t i = 0; i < copy_node_array_per_cell.numberOfArrays(); ++i) { diff --git a/tests/test_SubItemValuePerItem.cpp b/tests/test_SubItemValuePerItem.cpp index 5eaadd94d..4063133a3 100644 --- a/tests/test_SubItemValuePerItem.cpp +++ b/tests/test_SubItemValuePerItem.cpp @@ -720,7 +720,8 @@ TEST_CASE("SubItemValuePerItem", "[mesh]") } } - NodeValuePerCell<size_t> copy_node_value_per_cell = copy(node_value_per_cell); + WeakNodeValuePerCell<const size_t> node_const_value_per_cell = node_value_per_cell; + NodeValuePerCell<size_t> copy_node_value_per_cell = copy(node_const_value_per_cell); { bool is_same = true; -- GitLab