Remove EmptyArrayRef
[alexxy/gromacs.git] / src / gromacs / math / tests / arrayrefwithpadding.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2018,2019, 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::ArrayRefWithPadding.
37  *
38  * \author Mark Abraham <mark.j.abraham@gmail.com>
39  * \ingroup module_math
40  */
41 #include "gmxpre.h"
42
43 #include "gromacs/math/arrayrefwithpadding.h"
44
45 #include <vector>
46
47 #include <gtest/gtest.h>
48
49 #include "gromacs/math/paddedvector.h"
50 #include "gromacs/utility/arrayref.h"
51 #include "gromacs/utility/basedefinitions.h"
52 #include "gromacs/utility/real.h"
53
54 namespace gmx
55 {
56
57 namespace
58 {
59
60 TEST(EmptyArrayRefWithPaddingTest, IsEmpty)
61 {
62     ArrayRefWithPadding<real> empty;
63
64     EXPECT_EQ(0U, empty.size());
65     EXPECT_TRUE(empty.empty());
66 }
67
68 TEST(EmptyConstArrayRefWithPaddingTest, IsEmpty)
69 {
70     ArrayRefWithPadding<const real> empty;
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 ArrayRefWithPadding
79 typedef ::testing::Types<
80         ArrayRefWithPadding<int32_t>,
81         ArrayRefWithPadding<float>,
82         ArrayRefWithPadding<double>
83         > ArrayRefTypes;
84
85 //! Helper constant used in the text fixture.
86 constexpr index aSize = 3;
87
88 /*! \brief Permit all the tests to run on all kinds of ArrayRefWithPadding types
89  *
90  * The main objective is to verify that all the different kinds of
91  * construction lead to the expected result. */
92 template <typename TypeParam>
93 class ArrayRefWithPaddingTest : public ::testing::Test
94 {
95     public:
96         typedef TypeParam ArrayRefType;
97         typedef typename ArrayRefType::value_type ValueType;
98         typedef PaddedVector<ValueType> PaddedVectorType;
99
100         ValueType        a[aSize] = { ValueType(1.2), ValueType(2.4), ValueType(3.1) };
101         PaddedVectorType v;
102
103         //! Constructor, which prepares a padded vector to take array ref of.
104         ArrayRefWithPaddingTest() : v(aSize)
105         {
106             std::copy(a, a + aSize, v.begin());
107         }
108
109         /*! \brief Run the same tests all the time
110          *
111          * Note that test cases must call this->runTests(), because
112          * that's how the derived-class templates that implement
113          * type-parameterized tests actually work. */
114         void runTests(ArrayRefType  &arrayRefWithPadding)
115         {
116             ASSERT_LE(aSize, arrayRefWithPadding.size());
117             ASSERT_FALSE(arrayRefWithPadding.empty());
118             auto unpaddedArrayRef = arrayRefWithPadding.unpaddedArrayRef();
119             auto paddedArrayRef   = arrayRefWithPadding.paddedArrayRef();
120             EXPECT_LE(unpaddedArrayRef.size(), paddedArrayRef.size());
121             for (index i = 0; i != aSize; ++i)
122             {
123                 EXPECT_EQ(paddedArrayRef[i], unpaddedArrayRef[i]);
124                 EXPECT_EQ(a[i], unpaddedArrayRef[i]);
125             }
126         }
127 };
128
129 TYPED_TEST_CASE(ArrayRefWithPaddingTest, ArrayRefTypes);
130
131
132 TYPED_TEST(ArrayRefWithPaddingTest, AssignFromPaddedVectorWorks)
133 {
134     typename TestFixture::ArrayRefType arrayRef = this->v.arrayRefWithPadding();
135     this->runTests(arrayRef);
136 }
137
138 TYPED_TEST(ArrayRefWithPaddingTest, ConstructFromPointersWorks)
139 {
140     typename TestFixture::ArrayRefType arrayRef(this->v.data(),
141                                                 this->v.data() + this->v.size(),
142                                                 this->v.data() + this->v.paddedSize());
143     this->runTests(arrayRef);
144 }
145
146 template<bool c, typename T>
147 using makeConstIf_t = typename std::conditional<c, const T, T>::type;
148
149 //! Helper struct for the case actually used in mdrun signalling
150 template <typename T>
151 struct Helper
152 {
153     public:
154         T   a[3];
155         int size;
156 };
157
158 #else   // GTEST_HAS_TYPED_TEST
159
160 /* A dummy test that at least signals that something is missing if one runs the
161  * unit test executable itself.
162  */
163 TEST(DISABLED_ArrayRefWithPaddingTest, GenericTests)
164 {
165     ADD_FAILURE()
166     << "Tests for generic ArrayRefWithPadding functionality require support for "
167     << "Google Test typed tests, which was not available when the tests "
168     << "were compiled.";
169 }
170
171 #endif // GTEST_HAS_TYPED_TEST
172
173 }      // namespace
174
175 }      // namespace gmx