679b4d135312263d3c195ef1c2f6683f5d074d9f
[alexxy/gromacs.git] / src / gromacs / utility / tests / arrayref.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \internal \file
36  * \brief Tests for gmx::ArrayRef.
37  *
38  * \author Mark Abraham <mark.j.abraham@gmail.com>
39  * \ingroup module_utility
40  */
41 #include "gmxpre.h"
42
43 #include "gromacs/utility/arrayref.h"
44
45 #include <vector>
46
47 #include <gtest/gtest.h>
48
49 #include "gromacs/utility/basedefinitions.h"
50 #include "gromacs/utility/real.h"
51
52 namespace gmx
53 {
54
55 namespace
56 {
57
58 TEST(EmptyArrayRefTest, IsEmpty)
59 {
60     EmptyArrayRef  emptyArray = {};
61     ArrayRef<real> empty(emptyArray);
62
63     EXPECT_EQ(0U, empty.size());
64     EXPECT_TRUE(empty.empty());
65 }
66
67 TEST(EmptyConstArrayRefTest, IsEmpty)
68 {
69     EmptyArrayRef        emptyArray = {};
70     ArrayRef<const real> empty(emptyArray);
71
72     EXPECT_EQ(0U, empty.size());
73     EXPECT_TRUE(empty.empty());
74 }
75
76 #ifdef GTEST_HAS_TYPED_TEST
77
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<int64_t>,
87         ArrayRef<uint64_t>,
88         ArrayRef<float>,
89         ArrayRef<double>,
90         ArrayRef<const char>,
91         ArrayRef<const unsigned char>,
92         ArrayRef<const int>,
93         ArrayRef<const unsigned int>,
94         ArrayRef<const long>,
95         ArrayRef<const unsigned long>,
96         ArrayRef<const int64_t>,
97         ArrayRef<const uint64_t>,
98         ArrayRef<const float>,
99         ArrayRef<const double>
100         > ArrayRefTypes;
101
102 constexpr index aSize = 3;
103
104 /*! \brief Permit all the tests to run on all kinds of ArrayRefs
105  *
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
110 {
111     public:
112         typedef TypeParam ArrayRefType;
113         typedef typename ArrayRefType::value_type ValueType;
114         typedef typename std::remove_const<ValueType>::type NonConstValueType;
115
116         /*! \brief Run the same tests all the time
117          *
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)
123         {
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)
130             {
131                 EXPECT_EQ(a[i], arrayRef[i]);
132             }
133         }
134
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) };
137 };
138
139 TYPED_TEST_CASE(ArrayRefTest, ArrayRefTypes);
140
141
142 TYPED_TEST(ArrayRefTest, MakeWithAssignmentWorks)
143 {
144     typename TestFixture::ArrayRefType arrayRef = this->a;
145     this->runTests(this->a, arrayRef);
146 }
147
148 TYPED_TEST(ArrayRefTest, MakeWithNonConstAssignmentWorks)
149 {
150     typename TestFixture::ArrayRefType arrayRef = this->ma;
151     this->runTests(this->ma, arrayRef);
152 }
153
154 TYPED_TEST(ArrayRefTest, ConstructWithTemplateConstructorWorks)
155 {
156     typename TestFixture::ArrayRefType arrayRef(this->a);
157     this->runTests(this->a, arrayRef);
158 }
159
160 TYPED_TEST(ArrayRefTest, ConstructWithNonConstTemplateConstructorWorks)
161 {
162     typename TestFixture::ArrayRefType arrayRef(this->ma);
163     this->runTests(this->ma, arrayRef);
164 }
165
166 TYPED_TEST(ArrayRefTest, ConstructFromPointersWorks)
167 {
168     typename TestFixture::ArrayRefType arrayRef(this->a, this->a + aSize);
169     this->runTests(this->a, arrayRef);
170 }
171
172 TYPED_TEST(ArrayRefTest, ConstructFromNonConstPointersWorks)
173 {
174     typename TestFixture::ArrayRefType arrayRef(this->ma, this->ma + aSize);
175     this->runTests(this->ma, arrayRef);
176 }
177
178 template<bool c, typename T>
179 using makeConstIf_t = typename std::conditional<c, const T, T>::type;
180
181 TYPED_TEST(ArrayRefTest, ConstructFromVectorWorks)
182 {
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);
187 }
188
189 TYPED_TEST(ArrayRefTest, ConstructFromNonConstVectorWorks)
190 {
191     std::vector<typename TestFixture::NonConstValueType> v(this->a, this->a + aSize);
192     typename TestFixture::ArrayRefType                   arrayRef(v);
193     this->runTests(v.data(), arrayRef);
194 }
195
196 //! Helper struct for the case actually used in mdrun signalling
197 template <typename T>
198 struct Helper
199 {
200     public:
201         T   a[3];
202         int size;
203 };
204
205 /*! \brief Test of the case actually used in mdrun signalling
206  *
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),
211  * so we test it.
212  */
213
214 TYPED_TEST(ArrayRefTest, ConstructFromStructFieldWithTemplateConstructorWorks)
215 {
216     Helper<typename TestFixture::NonConstValueType> h;
217     h.size = aSize;
218     for (int i = 0; i != h.size; ++i)
219     {
220         h.a[i] = this->a[i];
221     }
222     typename TestFixture::ArrayRefType arrayRef(h.a);
223     this->runTests(h.a, arrayRef);
224 }
225
226 #else   // GTEST_HAS_TYPED_TEST
227
228 /* A dummy test that at least signals that something is missing if one runs the
229  * unit test executable itself.
230  */
231 TEST(DISABLED_ArrayRefTest, GenericTests)
232 {
233     ADD_FAILURE()
234     << "Tests for generic ArrayRef functionality require support for "
235     << "Google Test typed tests, which was not available when the tests "
236     << "were compiled.";
237 }
238
239 #endif // GTEST_HAS_TYPED_TEST
240
241 }      // namespace
242
243 }      // namespace gmx