Update bundled GoogleTest to current HEAD
[alexxy/gromacs.git] / src / gromacs / simd / tests / simd_memory.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2015,2016,2017,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 Tests for gmx::ArrayRef for SIMD types.
37  *
38  * \author Roland Schulz<roland.schulz@intel.com>
39  * \ingroup module_simd
40  */
41 #include "gmxpre.h"
42
43 #include <numeric>
44 #include <vector>
45
46 #include <gtest/gtest.h>
47
48 #include "gromacs/simd/simd.h"
49
50 #include "simd.h"
51
52 namespace gmx
53 {
54
55 #if GMX_SIMD_HAVE_REAL
56
57 /* SimdInt32 is a strange type which would never belong in an interface,
58  * because its properties are peculiar to int-to-float conversions within
59  * SIMD types, so there is no need to support it as a specialization of
60  * SimdArrayRef. But it is useful here for better test coverage of
61  * SimdArrayRef. */
62
63 template<>
64 class ArrayRef<SimdInt32> : public internal::SimdArrayRef<SimdInt32>
65 {
66     using Base = internal::SimdArrayRef<SimdInt32>;
67     using Base::Base;
68 };
69 template<>
70 class ArrayRef<const SimdInt32> : public internal::SimdArrayRef<const SimdInt32>
71 {
72     using Base = internal::SimdArrayRef<const SimdInt32>;
73     using Base::Base;
74 };
75
76 namespace
77 {
78
79 TEST(EmptyArrayRefTest, IsEmpty)
80 {
81     ArrayRef<SimdReal> empty = ArrayRef<real>();
82
83     EXPECT_EQ(0U, empty.size());
84     EXPECT_TRUE(empty.empty());
85 }
86
87 #    ifdef GTEST_HAS_TYPED_TEST
88
89 /*! \brief Permit all the tests to run on all kinds of ArrayRefs
90  *
91  * The main objective is to verify that all the different kinds of
92  * construction lead to the expected result. */
93 template<typename TypeParam>
94 class ArrayRefTest : public test::SimdTest
95 {
96 public:
97     using ArrayRefType         = TypeParam;
98     using PointerType          = typename ArrayRefType::pointer;
99     using ValueType            = typename ArrayRefType::value_type;
100     using ElementType          = std::remove_const_t<gmx::internal::SimdTraitsT<ValueType>>;
101     static constexpr int width = gmx::internal::SimdTraits<ValueType>::width;
102
103     /*! \brief Run the same tests all the time
104      *
105      * Note that test cases must call this->runTests(), because
106      * that's how the derived-class templates that implement
107      * type-parameterized tests actually work. */
108     void runReadOnlyTests(PointerType a, size_t aSize, ArrayRefType& arrayRef)
109     {
110         ASSERT_EQ(aSize, arrayRef.size());
111         ASSERT_FALSE(arrayRef.empty());
112
113         GMX_EXPECT_SIMD_EQ(load<ValueType>(a), arrayRef.front());
114         GMX_EXPECT_SIMD_EQ(load<ValueType>(a + (aSize - 1) * width), arrayRef.back());
115
116         auto it = arrayRef.begin();
117         for (size_t i = 0; i != aSize; ++i, ++it)
118         {
119             GMX_EXPECT_SIMD_EQ(load<ValueType>(a + i * width), arrayRef[i]);
120             GMX_EXPECT_SIMD_EQ(load<ValueType>(a + i * width), *it);
121         }
122
123         EXPECT_EQ(aSize, arrayRef.end() - arrayRef.begin());
124         EXPECT_EQ(arrayRef.begin() + 1, arrayRef.end() - (aSize - 1));
125         EXPECT_LT(arrayRef.end() - 1, arrayRef.end());
126         EXPECT_GT(arrayRef.begin() + 1, arrayRef.begin());
127         EXPECT_LE(arrayRef.end() - 1, arrayRef.end());
128         EXPECT_GE(arrayRef.begin() + 1, arrayRef.begin());
129
130         it = arrayRef.begin();
131         it++;
132         ASSERT_EQ(arrayRef.begin() + 1, it);
133         it--;
134         ASSERT_EQ(arrayRef.begin(), it);
135         it += 1;
136         ASSERT_EQ(arrayRef.begin() + 1, it);
137         it -= 1;
138         ASSERT_EQ(arrayRef.begin(), it);
139         ++it;
140         ASSERT_EQ(arrayRef.begin() + 1, it);
141         --it;
142         ASSERT_EQ(arrayRef.begin(), it);
143     }
144 };
145
146 using ArrayRefTypes =
147         ::testing::Types<ArrayRef<SimdReal>, ArrayRef<const SimdReal>, ArrayRef<SimdInt32>, ArrayRef<const SimdInt32>>;
148 TYPED_TEST_SUITE(ArrayRefTest, ArrayRefTypes);
149
150 TYPED_TEST(ArrayRefTest, ConstructFromPointersWorks)
151 {
152     alignas(TestFixture::width * sizeof(typename TestFixture::ElementType))
153             std::array<typename TestFixture::ElementType, TestFixture::width * 3>
154                     a;
155
156     std::iota(a.begin(), a.end(), 0);
157     typename TestFixture::ArrayRefType arrayRef(a.data(), a.data() + a.size());
158     this->runReadOnlyTests(a.data(), 3, arrayRef);
159 }
160
161 TYPED_TEST(ArrayRefTest, ConstructFromArrayRefWorks)
162 {
163     alignas(TestFixture::width * sizeof(typename TestFixture::ElementType))
164             std::array<typename TestFixture::ElementType, TestFixture::width * 3>
165                     a;
166
167     std::iota(a.begin(), a.end(), 0);
168     ArrayRef<std::remove_const_t<typename TestFixture::ValueType>> ref(a.data(), a.data() + a.size());
169     typename TestFixture::ArrayRefType                             arrayRef(ref);
170     this->runReadOnlyTests(a.data(), 3, arrayRef);
171 }
172
173 TYPED_TEST(ArrayRefTest, ConstructFromArrayWorks)
174 {
175     alignas(TestFixture::width * sizeof(typename TestFixture::ElementType))
176             std::array<typename TestFixture::ElementType, TestFixture::width * 3>
177                     a;
178
179     std::iota(a.begin(), a.end(), 0);
180     typename TestFixture::ArrayRefType arrayRef(a);
181     this->runReadOnlyTests(a.data(), 3, arrayRef);
182 }
183
184 template<typename TypeParam>
185 using ArrayRefReadWriteTest = ArrayRefTest<TypeParam>;
186
187 using ArrayRefReadWriteTypes = ::testing::Types<ArrayRef<SimdReal>, ArrayRef<SimdInt32>>;
188 TYPED_TEST_SUITE(ArrayRefReadWriteTest, ArrayRefReadWriteTypes);
189
190 TYPED_TEST(ArrayRefReadWriteTest, Assignment)
191 {
192     constexpr int width = TestFixture::width;
193
194     alignas(width * sizeof(typename TestFixture::ElementType)) std::array<typename TestFixture::ElementType, width * 4> a;
195
196     typename TestFixture::ArrayRefType arrayRef(a.data(), a.data() + a.size());
197
198     arrayRef.front() = 1;
199     EXPECT_EQ(1, a[0 * width]);
200     (arrayRef.front() = 1) = 2;
201     EXPECT_EQ(2, a[0 * width]);
202
203     arrayRef[1] = 2;
204     EXPECT_EQ(2, a[1 * width]);
205     *(arrayRef.begin() + 2) = 3;
206     EXPECT_EQ(3, a[2 * width]);
207     arrayRef.back() = 4;
208     EXPECT_EQ(4, a[3 * width]);
209 }
210
211 template<typename TypeParam>
212 using ArrayRefArithmeticTest = ArrayRefTest<TypeParam>;
213
214 using ArrayRefArithmeticTypes = ::testing::Types<ArrayRef<SimdReal>
215 #        if GMX_SIMD_HAVE_INT32_ARITHMETICS
216                                                  ,
217                                                  ArrayRef<SimdInt32>
218 #        endif
219                                                  >;
220 TYPED_TEST_SUITE(ArrayRefArithmeticTest, ArrayRefArithmeticTypes);
221
222 TYPED_TEST(ArrayRefArithmeticTest, Basic)
223 {
224     constexpr int width = TestFixture::width;
225
226     alignas(width * sizeof(typename TestFixture::ElementType)) std::array<typename TestFixture::ElementType, width> a;
227
228     typename TestFixture::ArrayRefType arrayRef(a.data(), a.data() + a.size());
229
230     arrayRef.front() = 1;
231     ASSERT_EQ(1, a[0 * width]);
232     arrayRef.front() += 1;
233     ASSERT_EQ(2, a[0 * width]);
234     arrayRef.front() *= 2;
235     ASSERT_EQ(4, a[0 * width]);
236     arrayRef.front() -= 3;
237     ASSERT_EQ(1, a[0 * width]);
238 }
239
240 #    endif // GTEST_HAS_TYPED_TEST
241
242 } // namespace
243
244 #endif // GMX_HAVE_SIMD_REAL
245
246 } // namespace gmx