Modernize STL usage
[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,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.
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     ArrayRef<real> empty;
61
62     EXPECT_EQ(0U, empty.size());
63     EXPECT_TRUE(empty.empty());
64 }
65
66 TEST(EmptyConstArrayRefTest, IsEmpty)
67 {
68     ArrayRef<const real> empty;
69
70     EXPECT_EQ(0U, empty.size());
71     EXPECT_TRUE(empty.empty());
72 }
73
74 #ifdef GTEST_HAS_TYPED_TEST
75
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>,
79                          ArrayRef<int>,
80                          ArrayRef<unsigned int>,
81                          ArrayRef<long>,
82                          ArrayRef<unsigned long>,
83                          ArrayRef<int64_t>,
84                          ArrayRef<uint64_t>,
85                          ArrayRef<float>,
86                          ArrayRef<double>,
87                          ArrayRef<const char>,
88                          ArrayRef<const unsigned char>,
89                          ArrayRef<const int>,
90                          ArrayRef<const unsigned int>,
91                          ArrayRef<const long>,
92                          ArrayRef<const unsigned long>,
93                          ArrayRef<const int64_t>,
94                          ArrayRef<const uint64_t>,
95                          ArrayRef<const float>,
96                          ArrayRef<const double>>
97         ArrayRefTypes;
98
99 constexpr index aSize = 3;
100
101 /*! \brief Permit all the tests to run on all kinds of ArrayRefs
102  *
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
107 {
108 public:
109     typedef TypeParam                         ArrayRefType;
110     typedef typename ArrayRefType::value_type ValueType;
111     typedef std::remove_const_t<ValueType>    NonConstValueType;
112
113     /*! \brief Run the same tests all the time
114      *
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)
119     {
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)
126         {
127             EXPECT_EQ(a[i], arrayRef[i]);
128         }
129     }
130
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) };
133 };
134
135 TYPED_TEST_CASE(ArrayRefTest, ArrayRefTypes);
136
137
138 TYPED_TEST(ArrayRefTest, MakeWithAssignmentWorks)
139 {
140     typename TestFixture::ArrayRefType arrayRef = this->a;
141     this->runTests(this->a, arrayRef);
142 }
143
144 TYPED_TEST(ArrayRefTest, MakeWithNonConstAssignmentWorks)
145 {
146     typename TestFixture::ArrayRefType arrayRef = this->ma;
147     this->runTests(this->ma, arrayRef);
148 }
149
150 TYPED_TEST(ArrayRefTest, ConstructWithTemplateConstructorWorks)
151 {
152     typename TestFixture::ArrayRefType arrayRef(this->a);
153     this->runTests(this->a, arrayRef);
154 }
155
156 TYPED_TEST(ArrayRefTest, ConstructWithNonConstTemplateConstructorWorks)
157 {
158     typename TestFixture::ArrayRefType arrayRef(this->ma);
159     this->runTests(this->ma, arrayRef);
160 }
161
162 TYPED_TEST(ArrayRefTest, ConstructFromPointersWorks)
163 {
164     typename TestFixture::ArrayRefType arrayRef(this->a, this->a + aSize);
165     this->runTests(this->a, arrayRef);
166 }
167
168 TYPED_TEST(ArrayRefTest, ConstructFromNonConstPointersWorks)
169 {
170     typename TestFixture::ArrayRefType arrayRef(this->ma, this->ma + aSize);
171     this->runTests(this->ma, arrayRef);
172 }
173
174 template<bool c, typename T>
175 using makeConstIf_t = std::conditional_t<c, const T, T>;
176
177 TYPED_TEST(ArrayRefTest, ConstructFromVectorWorks)
178 {
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);
183 }
184
185 TYPED_TEST(ArrayRefTest, ConstructFromNonConstVectorWorks)
186 {
187     std::vector<typename TestFixture::NonConstValueType> v(this->a, this->a + aSize);
188     typename TestFixture::ArrayRefType                   arrayRef(v);
189     this->runTests(v.data(), arrayRef);
190 }
191
192 //! Helper struct for the case actually used in mdrun signalling
193 template<typename T>
194 struct Helper
195 {
196 public:
197     T   a[3];
198     int size;
199 };
200
201 /*! \brief Test of the case actually used in mdrun signalling
202  *
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),
207  * so we test it.
208  */
209
210 TYPED_TEST(ArrayRefTest, ConstructFromStructFieldWithTemplateConstructorWorks)
211 {
212     Helper<typename TestFixture::NonConstValueType> h;
213     h.size = aSize;
214     for (int i = 0; i != h.size; ++i)
215     {
216         h.a[i] = this->a[i];
217     }
218     typename TestFixture::ArrayRefType arrayRef(h.a);
219     this->runTests(h.a, arrayRef);
220 }
221
222 #else // GTEST_HAS_TYPED_TEST
223
224 /* A dummy test that at least signals that something is missing if one runs the
225  * unit test executable itself.
226  */
227 TEST(DISABLED_ArrayRefTest, GenericTests)
228 {
229     ADD_FAILURE() << "Tests for generic ArrayRef functionality require support for "
230                   << "Google Test typed tests, which was not available when the tests "
231                   << "were compiled.";
232 }
233
234 #endif // GTEST_HAS_TYPED_TEST
235
236 } // namespace
237
238 } // namespace gmx