2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2015,2016,2017,2019,2020, 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<gmx::internal::SimdTraitsT<ValueType>>;
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, size_t aSize, ArrayRefType& arrayRef)
110 ASSERT_EQ(aSize, arrayRef.size());
111 ASSERT_FALSE(arrayRef.empty());
113 GMX_EXPECT_SIMD_EQ(load<ValueType>(a), arrayRef.front());
114 GMX_EXPECT_SIMD_EQ(load<ValueType>(a + (aSize - 1) * width), arrayRef.back());
116 auto it = arrayRef.begin();
117 for (size_t i = 0; i != aSize; ++i, ++it)
119 GMX_EXPECT_SIMD_EQ(load<ValueType>(a + i * width), arrayRef[i]);
120 GMX_EXPECT_SIMD_EQ(load<ValueType>(a + i * width), *it);
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());
130 it = arrayRef.begin();
132 ASSERT_EQ(arrayRef.begin() + 1, it);
134 ASSERT_EQ(arrayRef.begin(), it);
136 ASSERT_EQ(arrayRef.begin() + 1, it);
138 ASSERT_EQ(arrayRef.begin(), it);
140 ASSERT_EQ(arrayRef.begin() + 1, it);
142 ASSERT_EQ(arrayRef.begin(), it);
146 using ArrayRefTypes =
147 ::testing::Types<ArrayRef<SimdReal>, ArrayRef<const SimdReal>, ArrayRef<SimdInt32>, ArrayRef<const SimdInt32>>;
148 TYPED_TEST_CASE(ArrayRefTest, ArrayRefTypes);
150 TYPED_TEST(ArrayRefTest, ConstructFromPointersWorks)
152 alignas(TestFixture::width * sizeof(typename TestFixture::ElementType))
153 std::array<typename TestFixture::ElementType, TestFixture::width * 3>
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);
161 TYPED_TEST(ArrayRefTest, ConstructFromArrayRefWorks)
163 alignas(TestFixture::width * sizeof(typename TestFixture::ElementType))
164 std::array<typename TestFixture::ElementType, TestFixture::width * 3>
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);
173 TYPED_TEST(ArrayRefTest, ConstructFromArrayWorks)
175 alignas(TestFixture::width * sizeof(typename TestFixture::ElementType))
176 std::array<typename TestFixture::ElementType, TestFixture::width * 3>
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)) std::array<typename TestFixture::ElementType, width * 4> a;
196 typename TestFixture::ArrayRefType arrayRef(a.data(), a.data() + a.size());
198 arrayRef.front() = 1;
199 EXPECT_EQ(1, a[0 * width]);
200 (arrayRef.front() = 1) = 2;
201 EXPECT_EQ(2, a[0 * width]);
204 EXPECT_EQ(2, a[1 * width]);
205 *(arrayRef.begin() + 2) = 3;
206 EXPECT_EQ(3, a[2 * width]);
208 EXPECT_EQ(4, a[3 * width]);
211 template<typename TypeParam>
212 using ArrayRefArithmeticTest = ArrayRefTest<TypeParam>;
214 using ArrayRefArithmeticTypes = ::testing::Types<ArrayRef<SimdReal>
215 # if GMX_SIMD_HAVE_INT32_ARITHMETICS
220 TYPED_TEST_CASE(ArrayRefArithmeticTest, ArrayRefArithmeticTypes);
222 TYPED_TEST(ArrayRefArithmeticTest, Basic)
224 constexpr int width = TestFixture::width;
226 alignas(width * sizeof(typename TestFixture::ElementType)) std::array<typename TestFixture::ElementType, width> a;
228 typename TestFixture::ArrayRefType arrayRef(a.data(), a.data() + a.size());
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]);
240 # endif // GTEST_HAS_TYPED_TEST
244 #endif // GMX_HAVE_SIMD_REAL