Remove hacks for outdated BlueGene support
[gromacs/AngularHB.git] / src / gromacs / utility / tests / arrayref.cpp
blob1face9567dcf0b300c4875a7155f8250d3b501e9
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2015,2016, 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.
35 /*! \internal \file
36 * \brief Tests for gmx::ArrayRef and gmx::ConstArrayRef.
38 * \author Mark Abraham <mark.j.abraham@gmail.com>
39 * \ingroup module_utility
41 #include "gmxpre.h"
43 #include "gromacs/utility/arrayref.h"
45 #include <vector>
47 #include <gtest/gtest.h>
49 #include "gromacs/utility/basedefinitions.h"
50 #include "gromacs/utility/real.h"
52 namespace gmx
55 namespace
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 ConstArrayRef<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<
80 ArrayRef<char>,
81 ArrayRef<unsigned char>,
82 ArrayRef<int>,
83 ArrayRef<unsigned int>,
84 ArrayRef<long>,
85 ArrayRef<unsigned long>,
86 ArrayRef<gmx_int64_t>,
87 ArrayRef<gmx_uint64_t>,
88 ArrayRef<float>,
89 ArrayRef<double>,
90 ConstArrayRef<char>,
91 ConstArrayRef<unsigned char>,
92 ConstArrayRef<int>,
93 ConstArrayRef<unsigned int>,
94 ConstArrayRef<long>,
95 ConstArrayRef<unsigned long>,
96 ConstArrayRef<gmx_int64_t>,
97 ConstArrayRef<gmx_uint64_t>,
98 ConstArrayRef<float>,
99 ConstArrayRef<double>
100 > ArrayRefTypes;
102 /*! \brief Permit all the tests to run on all kinds of ArrayRefs
104 * The main objective is to verify that all the different kinds of
105 * construction lead to the expected result. */
106 template <typename TypeParam>
107 class ArrayRefTest : public ::testing::Test
109 public:
110 typedef TypeParam ArrayRefType;
111 typedef typename ArrayRefType::value_type ValueType;
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 *a,
119 size_t aSize,
120 ValueType *aData,
121 ArrayRefType &arrayRef)
123 ASSERT_EQ(aSize, arrayRef.size());
124 ASSERT_FALSE(arrayRef.empty());
125 EXPECT_EQ(aData, arrayRef.data());
126 EXPECT_EQ(a[0], arrayRef.front());
127 EXPECT_EQ(a[aSize-1], arrayRef.back());
128 for (size_t i = 0; i != aSize; ++i)
130 EXPECT_EQ(a[i], arrayRef[i]);
135 TYPED_TEST_CASE(ArrayRefTest, ArrayRefTypes);
137 /* Welcome back to the past. While you can declare a static array[] of
138 templated type in a class, in C++98, you have to define it outside
139 the class, and when you do, the compiler knows the declaration is
140 incomplete and can't match the types to actual functions. So,
141 declaring locals is the only choice available, so we need macros to
142 avoid duplication. Lovely. */
143 #define DEFINE_ARRAY(a, aSize) \
144 typename TestFixture::ValueType (a)[] = { \
145 static_cast<typename TestFixture::ValueType>(1.2), \
146 static_cast<typename TestFixture::ValueType>(2.4), \
147 static_cast<typename TestFixture::ValueType>(3.1) \
148 }; \
149 size_t (aSize) = sizeof((a)) / sizeof(typename TestFixture::ValueType);
151 TYPED_TEST(ArrayRefTest, MakeWithAssignmentWorks)
153 DEFINE_ARRAY(a, aSize);
154 typename TestFixture::ArrayRefType arrayRef = a;
155 this->runTests(a, aSize, a, arrayRef);
158 TYPED_TEST(ArrayRefTest, ConstructWithTemplateConstructorWorks)
160 DEFINE_ARRAY(a, aSize);
161 typename TestFixture::ArrayRefType arrayRef(a);
162 this->runTests(a, aSize, a, arrayRef);
165 TYPED_TEST(ArrayRefTest, ConstructFromPointersWorks)
167 DEFINE_ARRAY(a, aSize);
168 typename TestFixture::ArrayRefType arrayRef(a, a + aSize);
169 this->runTests(a, aSize, a, arrayRef);
172 TYPED_TEST(ArrayRefTest, MakeFromPointersWorks)
174 DEFINE_ARRAY(a, aSize);
175 typename TestFixture::ArrayRefType arrayRef
176 = TestFixture::ArrayRefType::fromPointers(a, a + aSize);
177 this->runTests(a, aSize, a, arrayRef);
180 TYPED_TEST(ArrayRefTest, MakeFromArrayWorks)
182 DEFINE_ARRAY(a, aSize);
183 typename TestFixture::ArrayRefType arrayRef
184 = TestFixture::ArrayRefType::fromArray(a, aSize);
185 this->runTests(a, aSize, a, arrayRef);
188 TYPED_TEST(ArrayRefTest, ConstructFromVectorWorks)
190 DEFINE_ARRAY(a, aSize);
191 std::vector<typename TestFixture::ValueType> v(a, a + aSize);
192 typename TestFixture::ArrayRefType arrayRef(v);
193 this->runTests(a, v.size(), v.data(), arrayRef);
196 TYPED_TEST(ArrayRefTest, MakeFromVectorWorks)
198 DEFINE_ARRAY(a, aSize);
199 std::vector<typename TestFixture::ValueType> v(a, a + aSize);
200 typename TestFixture::ArrayRefType arrayRef
201 = TestFixture::ArrayRefType::fromVector(v.begin(), v.end());
202 this->runTests(a, v.size(), v.data(), arrayRef);
205 //! Helper struct for the case actually used in mdrun signalling
206 template <typename T>
207 struct Helper
209 public:
210 T a[3];
211 int size;
214 /*! \brief Test of the case actually used in mdrun signalling
216 * There, we take a non-const struct-field array of static length and
217 * make an ArrayRef to it using the template constructor that is
218 * supposed to infer the length from the static size. This has
219 * been a problem (for a compiler that we no longer support),
220 * so we test it.
223 TYPED_TEST(ArrayRefTest, ConstructFromStructFieldWithTemplateConstructorWorks)
225 DEFINE_ARRAY(a, aSize);
226 Helper<typename TestFixture::ValueType> h;
227 h.size = aSize;
228 for (int i = 0; i != h.size; ++i)
230 h.a[i] = a[i];
232 typename TestFixture::ArrayRefType arrayRef(h.a);
233 this->runTests(h.a, h.size, h.a, arrayRef);
236 #else // GTEST_HAS_TYPED_TEST
238 /* A dummy test that at least signals that something is missing if one runs the
239 * unit test executable itself.
241 TEST(DISABLED_ArrayRefTest, GenericTests)
243 ADD_FAILURE()
244 << "Tests for generic ArrayRef functionality require support for "
245 << "Google Test typed tests, which was not available when the tests "
246 << "were compiled.";
249 #endif // GTEST_HAS_TYPED_TEST
251 } // namespace
253 } // namespace