2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2019, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Tests multidimensional arrays
39 * \author Christian Blau <cblau@gwdg.de>
40 * \ingroup module_math
44 #include "gromacs/math/multidimarray.h"
49 #include <gmock/gmock.h>
50 #include <gtest/gtest.h>
52 #include "gromacs/mdspan/extensions.h"
54 #include "testutils/testasserts.h"
65 class MultiDimArrayTest
: public ::testing::Test
70 std::fill(begin(staticArray_
), end(staticArray_
), testNumber_
- 1);
71 std::fill(begin(dynamicArray_
), end(dynamicArray_
), testNumber_
- 1);
75 using static_container_type
= std::array
<float, 3*3>;
76 using static_extents_type
= extents
<3, 3>;
77 using static_array_type
= MultiDimArray
<static_container_type
, static_extents_type
>;
79 using dynamic_container_type
= std::vector
<float>;
80 using dynamic_extents_type
= extents
<dynamic_extent
, dynamic_extent
>;
81 using dynamic_array_type
= MultiDimArray
<dynamic_container_type
, dynamic_extents_type
>;
83 static_array_type staticArray_
;
84 dynamic_array_type dynamicArray_
{2, 2};
86 float testNumber_
= 42;
90 TEST_F(MultiDimArrayTest
, canConstructAndFillStatic
)
92 for (const auto &x
: staticArray_
)
94 EXPECT_EQ(testNumber_
- 1, x
);
98 TEST_F(MultiDimArrayTest
, canConstructAndFillDynamic
)
100 for (const auto &x
: dynamicArray_
)
102 EXPECT_EQ(testNumber_
- 1, x
);
106 TEST_F(MultiDimArrayTest
, canSetValuesInStatic
)
108 staticArray_(1, 1) = testNumber_
;
109 EXPECT_EQ(testNumber_
, staticArray_(1, 1) );
112 TEST_F(MultiDimArrayTest
, canSetValuesInDynamic
)
114 dynamicArray_(1, 1) = testNumber_
;
115 EXPECT_EQ(testNumber_
, dynamicArray_(1, 1));
118 TEST_F(MultiDimArrayTest
, canMoveConstructStatic
)
120 auto other(std::move(staticArray_
));
121 for (const auto &x
: other
)
123 EXPECT_EQ(testNumber_
- 1, x
);
127 TEST_F(MultiDimArrayTest
, canMoveConstructDynamic
)
129 auto other(std::move(dynamicArray_
));
130 for (const auto &x
: other
)
132 EXPECT_EQ(testNumber_
- 1, x
);
136 TEST_F(MultiDimArrayTest
, canMoveAssignStatic
)
138 static_array_type other
;
139 other
= std::move(staticArray_
);
140 for (const auto &x
: other
)
142 EXPECT_EQ(testNumber_
- 1, x
);
146 TEST_F(MultiDimArrayTest
, canMoveAssignDynamic
)
148 dynamic_array_type other
;
149 other
= std::move(dynamicArray_
);
150 for (const auto &x
: other
)
152 EXPECT_EQ(testNumber_
- 1, x
);
156 TEST_F(MultiDimArrayTest
, canCopyConstructStatic
)
158 auto other
= staticArray_
;
159 auto twoDArrayIt
= begin(staticArray_
);
160 for (const auto &x
: other
)
162 EXPECT_EQ(*twoDArrayIt
, x
);
167 TEST_F(MultiDimArrayTest
, canCopyConstructDynamic
)
169 auto other
= dynamicArray_
;
170 auto twoDArrayIt
= begin(dynamicArray_
);
171 for (const auto &x
: other
)
173 EXPECT_EQ(*twoDArrayIt
, x
);
178 TEST_F(MultiDimArrayTest
, canCopyAssignStatic
)
180 static_array_type other
;
181 other
= staticArray_
;
182 auto twoDArrayIt
= begin(staticArray_
);
183 for (const auto &x
: other
)
185 EXPECT_EQ(*twoDArrayIt
, x
);
190 TEST_F(MultiDimArrayTest
, canCopyAssignDynamic
)
192 dynamic_array_type other
;
193 other
= dynamicArray_
;
194 auto twoDArrayIt
= begin(dynamicArray_
);
195 for (const auto &x
: other
)
197 EXPECT_EQ(*twoDArrayIt
, x
);
202 TEST_F(MultiDimArrayTest
, canSwapStatic
)
204 static_array_type other
;
205 staticArray_(0, 0) = testNumber_
;
206 other
.swap(staticArray_
);
207 EXPECT_EQ(testNumber_
, other(0, 0));
208 for (auto x
= begin(other
) + 1; x
!= end(other
); ++x
)
210 EXPECT_EQ(testNumber_
- 1, *x
);
214 TEST_F(MultiDimArrayTest
, canSwapDynamic
)
216 dynamic_array_type other
;
217 dynamicArray_(0, 0) = testNumber_
;
218 other
.swap(dynamicArray_
);
219 EXPECT_EQ(testNumber_
, other(0, 0));
220 for (auto x
= begin(other
) + 1; x
!= end(other
); ++x
)
222 EXPECT_EQ(testNumber_
- 1, *x
);
226 TEST_F(MultiDimArrayTest
, staticMultiDimArrayExtent
)
228 EXPECT_EQ(staticArray_
.extent(0), 3);
229 EXPECT_EQ(staticArray_
.extent(1), 3);
232 TEST_F(MultiDimArrayTest
, dynamicMultiDimArrayExtent
)
234 EXPECT_EQ(dynamicArray_
.extent(0), 2);
235 EXPECT_EQ(dynamicArray_
.extent(1), 2);
238 TEST_F(MultiDimArrayTest
, dynamicMultiDimArrayResizesToCorrectExtent
)
240 dynamicArray_
.resize(5, 4);
241 EXPECT_EQ(dynamicArray_
.extent(0), 5);
242 EXPECT_EQ(dynamicArray_
.extent(1), 4);
245 TEST_F(MultiDimArrayTest
, dynamicMultiDimArrayResizeAndSetValue
)
247 dynamicArray_
.resize(5, 4);
248 dynamicArray_(4, 3) = testNumber_
;
249 EXPECT_EQ(dynamicArray_(4, 3), testNumber_
);
252 TEST_F(MultiDimArrayTest
, staticMultiDimArrayFromArray
)
254 static_array_type arr
= {{1, 2, 3, 4, 5, 6, 7, 8, 9}};
255 EXPECT_EQ(arr(0, 0), 1);
256 EXPECT_EQ(arr(0, 1), 2);
257 EXPECT_EQ(arr(0, 2), 3);
258 EXPECT_EQ(arr(1, 0), 4);
259 EXPECT_EQ(arr(1, 1), 5);
260 EXPECT_EQ(arr(1, 2), 6);
261 EXPECT_EQ(arr(2, 0), 7);
262 EXPECT_EQ(arr(2, 1), 8);
263 EXPECT_EQ(arr(2, 2), 9);
266 TEST_F(MultiDimArrayTest
, conversionToView
)
268 static_array_type::view_type view
= staticArray_
.asView();
269 view(2, 2) = testNumber_
;
270 EXPECT_EQ(testNumber_
, view(2, 2));
273 TEST_F(MultiDimArrayTest
, conversionToConstView
)
275 static_array_type::const_view_type view
= staticArray_
.asConstView();
276 // the following must not compile:
277 // view(2, 2) = testNumber_;
278 for (const auto &x
: view
)
280 EXPECT_EQ(testNumber_
- 1, x
);
284 TEST_F(MultiDimArrayTest
, viewBegin
)
286 static_array_type::view_type view
= staticArray_
.asView();
287 *begin(view
) = testNumber_
;
288 EXPECT_EQ(*begin(view
), testNumber_
);
291 TEST_F(MultiDimArrayTest
, viewEnd
)
293 static_array_type::view_type view
= staticArray_
.asView();
296 view(2, 2) = testNumber_
;
297 EXPECT_EQ(*x
, testNumber_
);
300 TEST_F(MultiDimArrayTest
, constViewConstBegin
)
302 staticArray_(0, 0) = testNumber_
;
303 const auto view
= staticArray_
.asConstView();
304 // must not compile: *begin(view) = testNumber_;
305 EXPECT_EQ(*begin(view
), testNumber_
);
308 TEST_F(MultiDimArrayTest
, constViewConstEnd
)
310 staticArray_(2, 2) = testNumber_
;
311 const auto view
= staticArray_
.asConstView();
314 EXPECT_EQ(*x
, testNumber_
);
317 TEST(MultiDimArrayToMdSpanTest
, convertsToMdSpan
)
319 MultiDimArray
< std::array
<int, 4>, extents
< 2, 2>> arr
= {{0, 1, 2, 3}};
320 basic_mdspan
< int, extents
< 2, 2>> span(arr
);
322 // test copy correctness
323 EXPECT_EQ(span(1, 1), 3);
325 // test that span operates on same data as array
327 EXPECT_EQ(arr(0, 1), -5);
330 TEST(MultiDimArrayToMdSpanTest
, constArrayToMdSpan
)
332 const MultiDimArray
< std::array
<int, 4>, extents
< 2, 2>> constArr
= {{0, 1, 2, 3}};
333 basic_mdspan
< const int, extents
< 2, 2>> span(constArr
);
334 EXPECT_EQ(span(0, 1), 1);
337 TEST(MultiDimArrayToMdSpanTest
, nonConstArrayToConstMdSpan
)
339 MultiDimArray
< std::array
<int, 4>, extents
< 2, 2>> arr
= {{0, 1, 2, 3}};
340 basic_mdspan
< const int, extents
< 2, 2>> span(arr
);
341 EXPECT_EQ(span(0, 1), 1);
344 TEST(MultiDimArrayToMdSpanTest
, implicitConversionToMdSpan
)
346 auto testFunc
= [](basic_mdspan
< const int, extents
< 2, 2>> a
){
349 MultiDimArray
< std::array
<int, 4>, extents
< 2, 2>> arr
= {{0, 1, 2, 3}};
350 EXPECT_EQ(testFunc(arr
), 0);