2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2018,2019,2020,2021, 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 implementation of gmx::PaddedVector
39 * In particular we need to check that the padding works, and that
40 * unpadded_end() is maintained correctly.
42 * \author Mark Abraham <mark.j.abraham@gmail.com>
43 * \ingroup module_math
47 #include "gromacs/math/paddedvector.h"
52 #include <gtest/gtest.h>
54 #include "gromacs/math/vec.h"
55 #include "gromacs/math/vectypes.h"
56 #include "gromacs/utility/alignedallocator.h"
57 #include "gromacs/utility/basedefinitions.h"
59 #include "gromacs/math/tests/testarrayrefs.h"
66 //! Typed test fixture
68 class PaddedVectorTest : public ::testing::Test
73 //! The types used in testing
74 using Implementations = ::testing::Types<std::allocator<int32_t>,
75 std::allocator<float>,
76 std::allocator<double>,
77 std::allocator<BasicVector<float>>,
78 std::allocator<BasicVector<double>>,
79 AlignedAllocator<int32_t>,
80 AlignedAllocator<float>,
81 AlignedAllocator<double>,
82 AlignedAllocator<BasicVector<float>>,
83 AlignedAllocator<BasicVector<double>>>;
84 TYPED_TEST_SUITE(PaddedVectorTest, Implementations);
86 TYPED_TEST(PaddedVectorTest, DefaultConstructorWorks)
88 using VectorType = PaddedVector<typename TypeParam::value_type, TypeParam>;
91 EXPECT_EQ(v.size(), 0);
92 EXPECT_EQ(v.paddedSize(), 0);
93 EXPECT_TRUE(v.empty());
94 EXPECT_EQ(v.begin(), v.end());
95 EXPECT_EQ(v.cbegin(), v.cend());
98 TYPED_TEST(PaddedVectorTest, ResizeWorks)
100 using VectorType = PaddedVector<typename TypeParam::value_type, TypeParam>;
104 resizeAndFillInput(&v, 3, 1);
105 EXPECT_EQ(v.size(), 3);
106 EXPECT_GE(v.paddedSize(), 3);
107 EXPECT_EQ(v.paddedSize(), v.arrayRefWithPadding().size());
108 EXPECT_LE(v.size(), v.arrayRefWithPadding().size());
111 TYPED_TEST(PaddedVectorTest, ReserveWorks)
113 using VectorType = PaddedVector<typename TypeParam::value_type, TypeParam>;
115 VectorType vReserved;
116 vReserved.reserveWithPadding(5);
117 resizeAndFillInput(&vReserved, 3, 1);
119 EXPECT_EQ(vReserved.paddedSize(), vReserved.arrayRefWithPadding().size());
120 EXPECT_LE(vReserved.size(), vReserved.arrayRefWithPadding().size());
123 TYPED_TEST(PaddedVectorTest, ReserveWorksTheSameAsNoReserve)
125 using VectorType = PaddedVector<typename TypeParam::value_type, TypeParam>;
128 resizeAndFillInput(&v, 3, 1);
131 SCOPED_TRACE("Test when the reservation is larger than needed");
132 VectorType vReserved;
133 vReserved.reserveWithPadding(5);
134 resizeAndFillInput(&vReserved, 3, 1);
136 EXPECT_EQ(v.size(), vReserved.size());
137 EXPECT_LE(v.paddedSize(), vReserved.paddedSize());
140 SCOPED_TRACE("Test when the reservation is smaller than needed");
141 VectorType vReserved;
142 vReserved.reserveWithPadding(1);
143 resizeAndFillInput(&vReserved, 3, 1);
145 EXPECT_EQ(v.size(), vReserved.size());
146 EXPECT_GE(v.paddedSize(), vReserved.paddedSize());
150 TYPED_TEST(PaddedVectorTest, MoveConstructorWorks)
152 using VectorType = PaddedVector<typename TypeParam::value_type, TypeParam>;
154 VectorType vOriginal;
155 resizeAndFillInput(&vOriginal, 3, 1);
157 VectorType v(std::move(vOriginal));
158 EXPECT_EQ(v.size(), 3);
159 EXPECT_GE(v.paddedSize(), 3);
160 EXPECT_EQ(v.paddedSize(), v.arrayRefWithPadding().size());
161 EXPECT_LE(v.size(), v.arrayRefWithPadding().size());
164 TYPED_TEST(PaddedVectorTest, MoveConstructorWithAllocatorWorks)
166 using VectorType = PaddedVector<typename TypeParam::value_type, TypeParam>;
168 VectorType vOriginal;
169 resizeAndFillInput(&vOriginal, 3, 1);
171 TypeParam allocatorToTest;
172 VectorType v(std::move(vOriginal), allocatorToTest);
173 EXPECT_EQ(v.size(), 3);
174 EXPECT_GE(v.paddedSize(), 3);
175 EXPECT_EQ(v.paddedSize(), v.arrayRefWithPadding().size());
176 EXPECT_LE(v.size(), v.arrayRefWithPadding().size());
179 TYPED_TEST(PaddedVectorTest, MoveAssignmentWorks)
181 using VectorType = PaddedVector<typename TypeParam::value_type, TypeParam>;
183 VectorType vOriginal;
184 resizeAndFillInput(&vOriginal, 3, 1);
187 v = std::move(vOriginal);
188 EXPECT_EQ(v.size(), 3);
189 EXPECT_GE(v.paddedSize(), 3);
190 EXPECT_EQ(v.paddedSize(), v.arrayRefWithPadding().size());
191 EXPECT_LE(v.size(), v.arrayRefWithPadding().size());
194 TYPED_TEST(PaddedVectorTest, ArrayRefConversionsAreIdentical)
196 using VectorType = PaddedVector<typename TypeParam::value_type, TypeParam>;
199 resizeAndFillInput(&v, 3, 1);
201 SCOPED_TRACE("Comparing different paths to create identical unpadded views");
202 compareViews(makeArrayRef(v), v.arrayRefWithPadding().unpaddedArrayRef());
203 compareViews(makeConstArrayRef(v), v.constArrayRefWithPadding().unpaddedConstArrayRef());
204 compareViews(makeConstArrayRef(v), v.constArrayRefWithPadding().unpaddedArrayRef());
205 compareViews(makeConstArrayRef(v), v.arrayRefWithPadding().unpaddedConstArrayRef());
207 SCOPED_TRACE("Comparing const to non-const unpadded views");
208 compareViewsIgnoreConst(makeArrayRef(v), makeConstArrayRef(v));
211 TYPED_TEST(PaddedVectorTest, CanCopyAssign)
213 using VectorType = PaddedVector<typename TypeParam::value_type, TypeParam>;
216 resizeAndFillInput(&v, 3, 1);
217 resizeAndFillInput(&w, 3, 2);
220 compareViews(v.arrayRefWithPadding().unpaddedArrayRef(), w.arrayRefWithPadding().unpaddedArrayRef());
221 compareViews(makeArrayRef(v), makeArrayRef(w));
224 TYPED_TEST(PaddedVectorTest, CanMoveAssign)
226 using VectorType = PaddedVector<typename TypeParam::value_type, TypeParam>;
229 resizeAndFillInput(&v, 3, 1);
230 resizeAndFillInput(&w, 3, 2);
231 resizeAndFillInput(&x, 3, 1);
233 SCOPED_TRACE("Comparing padded views before move");
234 compareViews(v.arrayRefWithPadding().unpaddedArrayRef(), x.arrayRefWithPadding().unpaddedArrayRef());
235 SCOPED_TRACE("Comparing unpadded views before move");
236 compareViews(makeArrayRef(v), makeArrayRef(x));
240 SCOPED_TRACE("Comparing padded views");
241 compareViews(v.arrayRefWithPadding().unpaddedArrayRef(), w.arrayRefWithPadding().unpaddedArrayRef());
242 SCOPED_TRACE("Comparing unpadded views");
243 compareViews(makeArrayRef(v), makeArrayRef(w));
246 TYPED_TEST(PaddedVectorTest, CanSwap)
248 using VectorType = PaddedVector<typename TypeParam::value_type, TypeParam>;
251 resizeAndFillInput(&v, 3, 1);
252 resizeAndFillInput(&w, 3, 2);
253 resizeAndFillInput(&x, 3, 1);
256 compareViews(v.arrayRefWithPadding().unpaddedArrayRef(), w.arrayRefWithPadding().unpaddedArrayRef());
257 compareViews(makeArrayRef(v), makeArrayRef(w));