diff --git a/general/array.hpp b/general/array.hpp index 46c7f6684f..1224a265bf 100644 --- a/general/array.hpp +++ b/general/array.hpp @@ -78,6 +78,10 @@ public: template inline Array(const Array &src); + /// Deep copy from a braced init-list of convertible type + template + explicit inline Array(const CT (&values)[N]); + /// Destructor inline ~Array() { data.Delete(); } @@ -631,6 +635,12 @@ inline Array::Array(const Array &src) for (int i = 0; i < size; i++) { (*this)[i] = T(src[i]); } } +template template +inline Array::Array(const CT (&values)[N]) : Array(N) +{ + for (int i = 0; i < size; i++) { (*this)[i] = T(values[i]); } +} + template inline void Array::GrowSize(int minsize) { diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index e01db4f431..c36066c467 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -18,6 +18,7 @@ include_directories(BEFORE ${PROJECT_BINARY_DIR}) include_directories(BEFORE ${CMAKE_CURRENT_SOURCE_DIR}) set(UNIT_TESTS_SRCS + general/test_array.cpp general/test_mem.cpp general/test_text.cpp general/test_zlib.cpp diff --git a/tests/unit/general/test_array.cpp b/tests/unit/general/test_array.cpp new file mode 100644 index 0000000000..0c4d117883 --- /dev/null +++ b/tests/unit/general/test_array.cpp @@ -0,0 +1,27 @@ +// Copyright (c) 2010-2021, Lawrence Livermore National Security, LLC. Produced +// at the Lawrence Livermore National Laboratory. All Rights reserved. See files +// LICENSE and NOTICE for details. LLNL-CODE-806117. +// +// This file is part of the MFEM library. For more information and source code +// availability visit https://mfem.org. +// +// MFEM is free software; you can redistribute it and/or modify it under the +// terms of the BSD-3 license. We welcome feedback and contributions, see file +// CONTRIBUTING.md for details. + +#include "mfem.hpp" +#include "unit_tests.hpp" + +using namespace mfem; + +TEST_CASE("Array init-list construction", "[Array]") +{ + int ContigData[6] = {6, 5, 4, 3, 2, 1}; + Array a(ContigData, 6); + Array b({6.0, 5.0, 4.0, 3.0, 2.0, 1.0}); + + for (int i = 0; i < a.Size(); i++) + { + REQUIRE(a[i] == b[i]); + } +} diff --git a/tests/unit/linalg/test_vector.cpp b/tests/unit/linalg/test_vector.cpp index c59fe2d94d..8904a17541 100644 --- a/tests/unit/linalg/test_vector.cpp +++ b/tests/unit/linalg/test_vector.cpp @@ -16,10 +16,7 @@ using namespace mfem; TEST_CASE("Vector init-list construction", "[Vector]") { - double ContigData[6] = {6.0, 5.0, - 4.0, 3.0, - 2.0, 1.0 - }; + double ContigData[6] = {6.0, 5.0, 4.0, 3.0, 2.0, 1.0}; Vector a(ContigData, 6); Vector b({6.0, 5.0, 4.0, 3.0, 2.0, 1.0});