Update bundled GoogleTest to current HEAD
[alexxy/gromacs.git] / src / gromacs / math / tests / paddedvector.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \internal \file
36  * \brief
37  * Tests implementation of gmx::PaddedVector
38  *
39  * In particular we need to check that the padding works, and that
40  * unpadded_end() is maintained correctly.
41  *
42  * \author Mark Abraham <mark.j.abraham@gmail.com>
43  * \ingroup module_math
44  */
45 #include "gmxpre.h"
46
47 #include "gromacs/math/paddedvector.h"
48
49 #include <memory>
50 #include <vector>
51
52 #include <gtest/gtest.h>
53
54 #include "gromacs/math/vec.h"
55 #include "gromacs/math/vectypes.h"
56 #include "gromacs/utility/alignedallocator.h"
57 #include "gromacs/utility/basedefinitions.h"
58
59 #include "gromacs/math/tests/testarrayrefs.h"
60
61 namespace gmx
62 {
63 namespace test
64 {
65
66 //! Typed test fixture
67 template<typename T>
68 class PaddedVectorTest : public ::testing::Test
69 {
70 public:
71 };
72
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);
85
86 TYPED_TEST(PaddedVectorTest, DefaultConstructorWorks)
87 {
88     using VectorType = PaddedVector<typename TypeParam::value_type, TypeParam>;
89
90     VectorType v;
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());
96 }
97
98 TYPED_TEST(PaddedVectorTest, ResizeWorks)
99 {
100     using VectorType = PaddedVector<typename TypeParam::value_type, TypeParam>;
101
102     VectorType v;
103
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());
109 }
110
111 TYPED_TEST(PaddedVectorTest, ReserveWorks)
112 {
113     using VectorType = PaddedVector<typename TypeParam::value_type, TypeParam>;
114
115     VectorType vReserved;
116     vReserved.reserveWithPadding(5);
117     resizeAndFillInput(&vReserved, 3, 1);
118
119     EXPECT_EQ(vReserved.paddedSize(), vReserved.arrayRefWithPadding().size());
120     EXPECT_LE(vReserved.size(), vReserved.arrayRefWithPadding().size());
121 }
122
123 TYPED_TEST(PaddedVectorTest, ReserveWorksTheSameAsNoReserve)
124 {
125     using VectorType = PaddedVector<typename TypeParam::value_type, TypeParam>;
126
127     VectorType v;
128     resizeAndFillInput(&v, 3, 1);
129
130     {
131         SCOPED_TRACE("Test when the reservation is larger than needed");
132         VectorType vReserved;
133         vReserved.reserveWithPadding(5);
134         resizeAndFillInput(&vReserved, 3, 1);
135
136         EXPECT_EQ(v.size(), vReserved.size());
137         EXPECT_LE(v.paddedSize(), vReserved.paddedSize());
138     }
139     {
140         SCOPED_TRACE("Test when the reservation is smaller than needed");
141         VectorType vReserved;
142         vReserved.reserveWithPadding(1);
143         resizeAndFillInput(&vReserved, 3, 1);
144
145         EXPECT_EQ(v.size(), vReserved.size());
146         EXPECT_GE(v.paddedSize(), vReserved.paddedSize());
147     }
148 }
149
150 TYPED_TEST(PaddedVectorTest, MoveConstructorWorks)
151 {
152     using VectorType = PaddedVector<typename TypeParam::value_type, TypeParam>;
153
154     VectorType vOriginal;
155     resizeAndFillInput(&vOriginal, 3, 1);
156
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());
162 }
163
164 TYPED_TEST(PaddedVectorTest, MoveConstructorWithAllocatorWorks)
165 {
166     using VectorType = PaddedVector<typename TypeParam::value_type, TypeParam>;
167
168     VectorType vOriginal;
169     resizeAndFillInput(&vOriginal, 3, 1);
170
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());
177 }
178
179 TYPED_TEST(PaddedVectorTest, MoveAssignmentWorks)
180 {
181     using VectorType = PaddedVector<typename TypeParam::value_type, TypeParam>;
182
183     VectorType vOriginal;
184     resizeAndFillInput(&vOriginal, 3, 1);
185
186     VectorType v;
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());
192 }
193
194 TYPED_TEST(PaddedVectorTest, ArrayRefConversionsAreIdentical)
195 {
196     using VectorType = PaddedVector<typename TypeParam::value_type, TypeParam>;
197
198     VectorType v;
199     resizeAndFillInput(&v, 3, 1);
200
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());
206
207     SCOPED_TRACE("Comparing const to non-const unpadded views");
208     compareViewsIgnoreConst(makeArrayRef(v), makeConstArrayRef(v));
209 }
210
211 TYPED_TEST(PaddedVectorTest, CanCopyAssign)
212 {
213     using VectorType = PaddedVector<typename TypeParam::value_type, TypeParam>;
214
215     VectorType v, w;
216     resizeAndFillInput(&v, 3, 1);
217     resizeAndFillInput(&w, 3, 2);
218
219     w = v;
220     compareViews(v.arrayRefWithPadding().unpaddedArrayRef(), w.arrayRefWithPadding().unpaddedArrayRef());
221     compareViews(makeArrayRef(v), makeArrayRef(w));
222 }
223
224 TYPED_TEST(PaddedVectorTest, CanMoveAssign)
225 {
226     using VectorType = PaddedVector<typename TypeParam::value_type, TypeParam>;
227
228     VectorType v, w, x;
229     resizeAndFillInput(&v, 3, 1);
230     resizeAndFillInput(&w, 3, 2);
231     resizeAndFillInput(&x, 3, 1);
232
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));
237
238     w = std::move(x);
239
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));
244 }
245
246 TYPED_TEST(PaddedVectorTest, CanSwap)
247 {
248     using VectorType = PaddedVector<typename TypeParam::value_type, TypeParam>;
249
250     VectorType v, w, x;
251     resizeAndFillInput(&v, 3, 1);
252     resizeAndFillInput(&w, 3, 2);
253     resizeAndFillInput(&x, 3, 1);
254
255     std::swap(w, x);
256     compareViews(v.arrayRefWithPadding().unpaddedArrayRef(), w.arrayRefWithPadding().unpaddedArrayRef());
257     compareViews(makeArrayRef(v), makeArrayRef(w));
258 }
259
260 } // namespace test
261 } // namespace gmx