2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2015,2016,2017,2018, 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)
60 EmptyArrayRef emptyArray = {};
61 ArrayRef<real> empty(emptyArray);
63 EXPECT_EQ(0U, empty.size());
64 EXPECT_TRUE(empty.empty());
67 TEST(EmptyConstArrayRefTest, IsEmpty)
69 EmptyArrayRef emptyArray = {};
70 ArrayRef<const real> empty(emptyArray);
72 EXPECT_EQ(0U, empty.size());
73 EXPECT_TRUE(empty.empty());
76 #ifdef GTEST_HAS_TYPED_TEST
78 //! Define the types that end up being available as TypeParam in the test cases for both kinds of ArrayRef
79 typedef ::testing::Types<
81 ArrayRef<unsigned char>,
83 ArrayRef<unsigned int>,
85 ArrayRef<unsigned long>,
91 ArrayRef<const unsigned char>,
93 ArrayRef<const unsigned int>,
95 ArrayRef<const unsigned long>,
96 ArrayRef<const int64_t>,
97 ArrayRef<const uint64_t>,
98 ArrayRef<const float>,
99 ArrayRef<const double>
102 constexpr index aSize = 3;
104 /*! \brief Permit all the tests to run on all kinds of ArrayRefs
106 * The main objective is to verify that all the different kinds of
107 * construction lead to the expected result. */
108 template <typename TypeParam>
109 class ArrayRefTest : public ::testing::Test
112 typedef TypeParam ArrayRefType;
113 typedef typename ArrayRefType::value_type ValueType;
114 typedef typename std::remove_const<ValueType>::type NonConstValueType;
116 /*! \brief Run the same tests all the time
118 * Note that test cases must call this->runTests(), because
119 * that's how the derived-class templates that implement
120 * type-parameterized tests actually work. */
121 void runTests(ValueType *aData,
122 ArrayRefType &arrayRef)
124 ASSERT_EQ(aSize, arrayRef.size());
125 ASSERT_FALSE(arrayRef.empty());
126 EXPECT_EQ(aData, arrayRef.data());
127 EXPECT_EQ(a[0], arrayRef.front());
128 EXPECT_EQ(a[aSize-1], arrayRef.back());
129 for (index i = 0; i != aSize; ++i)
131 EXPECT_EQ(a[i], arrayRef[i]);
135 ValueType a[aSize] = { ValueType(1.2), ValueType(2.4), ValueType(3.1) };
136 NonConstValueType ma[aSize] = { ValueType(1.2), ValueType(2.4), ValueType(3.1) };
139 TYPED_TEST_CASE(ArrayRefTest, ArrayRefTypes);
142 TYPED_TEST(ArrayRefTest, MakeWithAssignmentWorks)
144 typename TestFixture::ArrayRefType arrayRef = this->a;
145 this->runTests(this->a, arrayRef);
148 TYPED_TEST(ArrayRefTest, MakeWithNonConstAssignmentWorks)
150 typename TestFixture::ArrayRefType arrayRef = this->ma;
151 this->runTests(this->ma, arrayRef);
154 TYPED_TEST(ArrayRefTest, ConstructWithTemplateConstructorWorks)
156 typename TestFixture::ArrayRefType arrayRef(this->a);
157 this->runTests(this->a, arrayRef);
160 TYPED_TEST(ArrayRefTest, ConstructWithNonConstTemplateConstructorWorks)
162 typename TestFixture::ArrayRefType arrayRef(this->ma);
163 this->runTests(this->ma, arrayRef);
166 TYPED_TEST(ArrayRefTest, ConstructFromPointersWorks)
168 typename TestFixture::ArrayRefType arrayRef(this->a, this->a + aSize);
169 this->runTests(this->a, arrayRef);
172 TYPED_TEST(ArrayRefTest, ConstructFromNonConstPointersWorks)
174 typename TestFixture::ArrayRefType arrayRef(this->ma, this->ma + aSize);
175 this->runTests(this->ma, arrayRef);
178 template<bool c, typename T>
179 using makeConstIf_t = typename std::conditional<c, const T, T>::type;
181 TYPED_TEST(ArrayRefTest, ConstructFromVectorWorks)
183 makeConstIf_t<std::is_const<typename TestFixture::ValueType>::value,
184 std::vector<typename TestFixture::NonConstValueType> > v(this->a, this->a + aSize);
185 typename TestFixture::ArrayRefType arrayRef(v);
186 this->runTests(v.data(), arrayRef);
189 TYPED_TEST(ArrayRefTest, ConstructFromNonConstVectorWorks)
191 std::vector<typename TestFixture::NonConstValueType> v(this->a, this->a + aSize);
192 typename TestFixture::ArrayRefType arrayRef(v);
193 this->runTests(v.data(), arrayRef);
196 //! Helper struct for the case actually used in mdrun signalling
197 template <typename T>
205 /*! \brief Test of the case actually used in mdrun signalling
207 * There, we take a non-const struct-field array of static length and
208 * make an ArrayRef to it using the template constructor that is
209 * supposed to infer the length from the static size. This has
210 * been a problem (for a compiler that we no longer support),
214 TYPED_TEST(ArrayRefTest, ConstructFromStructFieldWithTemplateConstructorWorks)
216 Helper<typename TestFixture::NonConstValueType> h;
218 for (int i = 0; i != h.size; ++i)
222 typename TestFixture::ArrayRefType arrayRef(h.a);
223 this->runTests(h.a, arrayRef);
226 #else // GTEST_HAS_TYPED_TEST
228 /* A dummy test that at least signals that something is missing if one runs the
229 * unit test executable itself.
231 TEST(DISABLED_ArrayRefTest, GenericTests)
234 << "Tests for generic ArrayRef functionality require support for "
235 << "Google Test typed tests, which was not available when the tests "
239 #endif // GTEST_HAS_TYPED_TEST