2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2015,2016,2017,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.
36 * \brief Tests for gmx::ArrayRef for SIMD types.
38 * \author Roland Schulz<roland.schulz@intel.com>
39 * \ingroup module_simd
46 #include <gtest/gtest.h>
48 #include "gromacs/simd/simd.h"
55 #if GMX_SIMD_HAVE_REAL
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
64 class ArrayRef<SimdInt32> : public internal::SimdArrayRef<SimdInt32>
66 using Base = internal::SimdArrayRef<SimdInt32>;
70 class ArrayRef<const SimdInt32> : public internal::SimdArrayRef<const SimdInt32>
72 using Base = internal::SimdArrayRef<const SimdInt32>;
79 TEST(EmptyArrayRefTest, IsEmpty)
81 ArrayRef<SimdReal> empty = ArrayRef<real>();
83 EXPECT_EQ(0U, empty.size());
84 EXPECT_TRUE(empty.empty());
87 #ifdef GTEST_HAS_TYPED_TEST
89 /*! \brief Permit all the tests to run on all kinds of ArrayRefs
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
97 using ArrayRefType = TypeParam;
98 using PointerType = typename ArrayRefType::pointer;
99 using ValueType = typename ArrayRefType::value_type;
100 using ElementType = std::remove_const_t < typename gmx::internal::SimdTraits < ValueType>::type>;
101 static constexpr int width = gmx::internal::SimdTraits<ValueType>::width;
103 /*! \brief Run the same tests all the time
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,
110 ArrayRefType &arrayRef)
112 ASSERT_EQ(aSize, arrayRef.size());
113 ASSERT_FALSE(arrayRef.empty());
115 GMX_EXPECT_SIMD_EQ(load<ValueType>(a), arrayRef.front());
116 GMX_EXPECT_SIMD_EQ(load<ValueType>(a+(aSize-1)*width), arrayRef.back());
118 auto it = arrayRef.begin();
119 for (size_t i = 0; i != aSize; ++i, ++it)
121 GMX_EXPECT_SIMD_EQ(load<ValueType>(a+i*width), arrayRef[i]);
122 GMX_EXPECT_SIMD_EQ(load<ValueType>(a+i*width), *it);
125 EXPECT_EQ(aSize, arrayRef.end()-arrayRef.begin());
126 EXPECT_EQ(arrayRef.begin()+1, arrayRef.end()-(aSize-1));
127 EXPECT_LT(arrayRef.end()-1, arrayRef.end());
128 EXPECT_GT(arrayRef.begin()+1, arrayRef.begin());
129 EXPECT_LE(arrayRef.end()-1, arrayRef.end());
130 EXPECT_GE(arrayRef.begin()+1, arrayRef.begin());
132 it = arrayRef.begin();
134 ASSERT_EQ(arrayRef.begin()+1, it);
136 ASSERT_EQ(arrayRef.begin(), it);
138 ASSERT_EQ(arrayRef.begin()+1, it);
140 ASSERT_EQ(arrayRef.begin(), it);
142 ASSERT_EQ(arrayRef.begin()+1, it);
144 ASSERT_EQ(arrayRef.begin(), it);
148 using ArrayRefTypes = ::testing::Types<ArrayRef<SimdReal>, ArrayRef<const SimdReal>,
149 ArrayRef<SimdInt32>, ArrayRef<const SimdInt32> >;
150 TYPED_TEST_CASE(ArrayRefTest, ArrayRefTypes);
152 TYPED_TEST(ArrayRefTest, ConstructFromPointersWorks)
154 alignas(TestFixture::width*sizeof(typename TestFixture::ElementType))
155 std::array<typename TestFixture::ElementType, TestFixture::width*3> a;
157 std::iota(a.begin(), a.end(), 0);
158 typename TestFixture::ArrayRefType arrayRef(a.data(), a.data()+a.size());
159 this->runReadOnlyTests(a.data(), 3, arrayRef);
162 TYPED_TEST(ArrayRefTest, ConstructFromArrayRefWorks)
164 alignas(TestFixture::width*sizeof(typename TestFixture::ElementType))
165 std::array<typename TestFixture::ElementType, TestFixture::width*3> a;
167 std::iota(a.begin(), a.end(), 0);
168 ArrayRef < std::remove_const_t < typename TestFixture::ValueType>>
169 ref(a.data(), a.data()+a.size());
170 typename TestFixture::ArrayRefType arrayRef(ref);
171 this->runReadOnlyTests(a.data(), 3, arrayRef);
174 TYPED_TEST(ArrayRefTest, ConstructFromArrayWorks)
176 alignas(TestFixture::width*sizeof(typename TestFixture::ElementType))
177 std::array<typename TestFixture::ElementType, TestFixture::width*3> a;
179 std::iota(a.begin(), a.end(), 0);
180 typename TestFixture::ArrayRefType arrayRef(a);
181 this->runReadOnlyTests(a.data(), 3, arrayRef);
184 template <typename TypeParam>
185 using ArrayRefReadWriteTest = ArrayRefTest<TypeParam>;
187 using ArrayRefReadWriteTypes = ::testing::Types< ArrayRef<SimdReal>, ArrayRef<SimdInt32> >;
188 TYPED_TEST_CASE(ArrayRefReadWriteTest, ArrayRefReadWriteTypes);
190 TYPED_TEST(ArrayRefReadWriteTest, Assignment)
192 constexpr int width = TestFixture::width;
194 alignas(width*sizeof(typename TestFixture::ElementType))
195 std::array<typename TestFixture::ElementType, width*4> a;
197 typename TestFixture::ArrayRefType arrayRef(a.data(), a.data()+a.size());
199 arrayRef.front() = 1;
200 EXPECT_EQ(1, a[0*width]);
201 (arrayRef.front() = 1) = 2;
202 EXPECT_EQ(2, a[0*width]);
205 EXPECT_EQ(2, a[1*width]);
206 *(arrayRef.begin()+2) = 3;
207 EXPECT_EQ(3, a[2*width]);
209 EXPECT_EQ(4, a[3*width]);
212 template <typename TypeParam>
213 using ArrayRefArithmeticTest = ArrayRefTest<TypeParam>;
215 using ArrayRefArithmeticTypes = ::testing::Types< ArrayRef<SimdReal>
216 #if GMX_SIMD_HAVE_INT32_ARITHMETICS
217 , ArrayRef<SimdInt32>
220 TYPED_TEST_CASE(ArrayRefArithmeticTest, ArrayRefArithmeticTypes);
222 TYPED_TEST(ArrayRefArithmeticTest, Basic)
224 constexpr int width = TestFixture::width;
226 alignas(width*sizeof(typename TestFixture::ElementType))
227 std::array<typename TestFixture::ElementType, width> a;
229 typename TestFixture::ArrayRefType arrayRef(a.data(), a.data()+a.size());
231 arrayRef.front() = 1;
232 ASSERT_EQ(1, a[0*width]);
233 arrayRef.front() += 1;
234 ASSERT_EQ(2, a[0*width]);
235 arrayRef.front() *= 2;
236 ASSERT_EQ(4, a[0*width]);
237 arrayRef.front() -= 3;
238 ASSERT_EQ(1, a[0*width]);
241 #endif // GTEST_HAS_TYPED_TEST
245 #endif // GMX_HAVE_SIMD_REAL