2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2015,2016,2017,2018,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.
38 * \author Mark Abraham <mark.j.abraham@gmail.com>
39 * \ingroup module_utility
43 #include "gromacs/utility/arrayref.h"
47 #include <gtest/gtest.h>
49 #include "gromacs/utility/basedefinitions.h"
50 #include "gromacs/utility/real.h"
58 TEST(EmptyArrayRefTest, IsEmpty)
62 EXPECT_EQ(0U, empty.size());
63 EXPECT_TRUE(empty.empty());
66 TEST(EmptyConstArrayRefTest, IsEmpty)
68 ArrayRef<const real> empty;
70 EXPECT_EQ(0U, empty.size());
71 EXPECT_TRUE(empty.empty());
74 #ifdef GTEST_HAS_TYPED_TEST
76 //! Define the types that end up being available as TypeParam in the test cases for both kinds of ArrayRef
77 typedef ::testing::Types<ArrayRef<char>,
78 ArrayRef<unsigned char>,
80 ArrayRef<unsigned int>,
82 ArrayRef<unsigned long>,
88 ArrayRef<const unsigned char>,
90 ArrayRef<const unsigned int>,
92 ArrayRef<const unsigned long>,
93 ArrayRef<const int64_t>,
94 ArrayRef<const uint64_t>,
95 ArrayRef<const float>,
96 ArrayRef<const double>>
99 constexpr index aSize = 3;
101 /*! \brief Permit all the tests to run on all kinds of ArrayRefs
103 * The main objective is to verify that all the different kinds of
104 * construction lead to the expected result. */
105 template<typename TypeParam>
106 class ArrayRefTest : public ::testing::Test
109 typedef TypeParam ArrayRefType;
110 typedef typename ArrayRefType::value_type ValueType;
111 typedef std::remove_const_t<ValueType> NonConstValueType;
113 /*! \brief Run the same tests all the time
115 * Note that test cases must call this->runTests(), because
116 * that's how the derived-class templates that implement
117 * type-parameterized tests actually work. */
118 void runTests(ValueType* aData, ArrayRefType& arrayRef)
120 ASSERT_EQ(aSize, arrayRef.size());
121 ASSERT_FALSE(arrayRef.empty());
122 EXPECT_EQ(aData, arrayRef.data());
123 EXPECT_EQ(a[0], arrayRef.front());
124 EXPECT_EQ(a[aSize - 1], arrayRef.back());
125 for (index i = 0; i != aSize; ++i)
127 EXPECT_EQ(a[i], arrayRef[i]);
131 ValueType a[aSize] = { ValueType(1.2), ValueType(2.4), ValueType(3.1) };
132 NonConstValueType ma[aSize] = { ValueType(1.2), ValueType(2.4), ValueType(3.1) };
135 TYPED_TEST_CASE(ArrayRefTest, ArrayRefTypes);
138 TYPED_TEST(ArrayRefTest, MakeWithAssignmentWorks)
140 typename TestFixture::ArrayRefType arrayRef = this->a;
141 this->runTests(this->a, arrayRef);
144 TYPED_TEST(ArrayRefTest, MakeWithNonConstAssignmentWorks)
146 typename TestFixture::ArrayRefType arrayRef = this->ma;
147 this->runTests(this->ma, arrayRef);
150 TYPED_TEST(ArrayRefTest, ConstructWithTemplateConstructorWorks)
152 typename TestFixture::ArrayRefType arrayRef(this->a);
153 this->runTests(this->a, arrayRef);
156 TYPED_TEST(ArrayRefTest, ConstructWithNonConstTemplateConstructorWorks)
158 typename TestFixture::ArrayRefType arrayRef(this->ma);
159 this->runTests(this->ma, arrayRef);
162 TYPED_TEST(ArrayRefTest, ConstructFromPointersWorks)
164 typename TestFixture::ArrayRefType arrayRef(this->a, this->a + aSize);
165 this->runTests(this->a, arrayRef);
168 TYPED_TEST(ArrayRefTest, ConstructFromNonConstPointersWorks)
170 typename TestFixture::ArrayRefType arrayRef(this->ma, this->ma + aSize);
171 this->runTests(this->ma, arrayRef);
174 template<bool c, typename T>
175 using makeConstIf_t = std::conditional_t<c, const T, T>;
177 TYPED_TEST(ArrayRefTest, ConstructFromVectorWorks)
179 makeConstIf_t<std::is_const_v<typename TestFixture::ValueType>, std::vector<typename TestFixture::NonConstValueType>> v(
180 this->a, this->a + aSize);
181 typename TestFixture::ArrayRefType arrayRef(v);
182 this->runTests(v.data(), arrayRef);
185 TYPED_TEST(ArrayRefTest, ConstructFromNonConstVectorWorks)
187 std::vector<typename TestFixture::NonConstValueType> v(this->a, this->a + aSize);
188 typename TestFixture::ArrayRefType arrayRef(v);
189 this->runTests(v.data(), arrayRef);
192 //! Helper struct for the case actually used in mdrun signalling
201 /*! \brief Test of the case actually used in mdrun signalling
203 * There, we take a non-const struct-field array of static length and
204 * make an ArrayRef to it using the template constructor that is
205 * supposed to infer the length from the static size. This has
206 * been a problem (for a compiler that we no longer support),
210 TYPED_TEST(ArrayRefTest, ConstructFromStructFieldWithTemplateConstructorWorks)
212 Helper<typename TestFixture::NonConstValueType> h;
214 for (int i = 0; i != h.size; ++i)
218 typename TestFixture::ArrayRefType arrayRef(h.a);
219 this->runTests(h.a, arrayRef);
222 #else // GTEST_HAS_TYPED_TEST
224 /* A dummy test that at least signals that something is missing if one runs the
225 * unit test executable itself.
227 TEST(DISABLED_ArrayRefTest, GenericTests)
229 ADD_FAILURE() << "Tests for generic ArrayRef functionality require support for "
230 << "Google Test typed tests, which was not available when the tests "
234 #endif // GTEST_HAS_TYPED_TEST