2 * This file is part of the GROMACS molecular simulation package.
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.
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::ArrayRefWithPadding.
38 * \author Mark Abraham <mark.j.abraham@gmail.com>
39 * \ingroup module_math
43 #include "gromacs/math/arrayrefwithpadding.h"
47 #include <gtest/gtest.h>
49 #include "gromacs/math/paddedvector.h"
50 #include "gromacs/utility/arrayref.h"
51 #include "gromacs/utility/basedefinitions.h"
52 #include "gromacs/utility/real.h"
60 TEST(EmptyArrayRefWithPaddingTest
, IsEmpty
)
62 ArrayRefWithPadding
<real
> empty
;
64 EXPECT_EQ(0U, empty
.size());
65 EXPECT_TRUE(empty
.empty());
68 TEST(EmptyConstArrayRefWithPaddingTest
, IsEmpty
)
70 ArrayRefWithPadding
<const real
> empty
;
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 ArrayRefWithPadding
79 typedef ::testing::Types
<
80 ArrayRefWithPadding
<int32_t>,
81 ArrayRefWithPadding
<float>,
82 ArrayRefWithPadding
<double>
85 //! Helper constant used in the text fixture.
86 constexpr index aSize
= 3;
88 /*! \brief Permit all the tests to run on all kinds of ArrayRefWithPadding types
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
96 typedef TypeParam ArrayRefType
;
97 typedef typename
ArrayRefType::value_type ValueType
;
98 typedef PaddedVector
<ValueType
> PaddedVectorType
;
100 ValueType a
[aSize
] = { ValueType(1.2), ValueType(2.4), ValueType(3.1) };
103 //! Constructor, which prepares a padded vector to take array ref of.
104 ArrayRefWithPaddingTest() : v(aSize
)
106 std::copy(a
, a
+ aSize
, v
.begin());
109 /*! \brief Run the same tests all the time
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
)
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
)
123 EXPECT_EQ(paddedArrayRef
[i
], unpaddedArrayRef
[i
]);
124 EXPECT_EQ(a
[i
], unpaddedArrayRef
[i
]);
127 using ConstArrayRefWithPaddingType
= ArrayRefWithPadding
<const typename
ArrayRefType::value_type
>;
129 // Check that we can make a padded view that refers to const elements
130 ConstArrayRefWithPaddingType constArrayRefWithPadding
= arrayRefWithPadding
.constArrayRefWithPadding();
131 for (index i
= 0; i
!= aSize
; ++i
)
133 EXPECT_EQ(a
[i
], constArrayRefWithPadding
.paddedArrayRef()[i
]);
138 // Check that we can implicitly make a padded view that refers to const elements
139 ConstArrayRefWithPaddingType constArrayRefWithPadding
= arrayRefWithPadding
;
140 for (index i
= 0; i
!= aSize
; ++i
)
142 EXPECT_EQ(a
[i
], constArrayRefWithPadding
.paddedArrayRef()[i
]);
147 // Check that a swap works, by making an empty padded
148 // vector, and a view of it, and observing what
149 // happens when we swap with the one constructed for
152 ArrayRefType view
= w
.arrayRefWithPadding();
153 EXPECT_TRUE(view
.empty());
155 view
.swap(arrayRefWithPadding
);
156 EXPECT_TRUE(arrayRefWithPadding
.empty());
157 EXPECT_LE(aSize
, view
.size());
158 for (index i
= 0; i
!= v
.size(); ++i
)
160 EXPECT_EQ(v
[i
], view
.paddedArrayRef()[i
]);
162 // Restore arrayRefWithPadding for future test code
163 view
.swap(arrayRefWithPadding
);
168 TYPED_TEST_CASE(ArrayRefWithPaddingTest
, ArrayRefTypes
);
171 TYPED_TEST(ArrayRefWithPaddingTest
, AssignFromPaddedVectorWorks
)
173 typename
TestFixture::ArrayRefType arrayRef
= this->v
.arrayRefWithPadding();
174 this->runTests(arrayRef
);
177 TYPED_TEST(ArrayRefWithPaddingTest
, ConstructFromPointersWorks
)
179 typename
TestFixture::ArrayRefType
arrayRef(this->v
.data(),
180 this->v
.data() + this->v
.size(),
181 this->v
.data() + this->v
.paddedSize());
182 this->runTests(arrayRef
);
185 template<bool c
, typename T
>
186 using makeConstIf_t
= std::conditional_t
<c
, const T
, T
>;
188 //! Helper struct for the case actually used in mdrun signalling
189 template <typename T
>
197 #else // GTEST_HAS_TYPED_TEST
199 /* A dummy test that at least signals that something is missing if one runs the
200 * unit test executable itself.
202 TEST(DISABLED_ArrayRefWithPaddingTest
, GenericTests
)
205 << "Tests for generic ArrayRefWithPadding functionality require support for "
206 << "Google Test typed tests, which was not available when the tests "
210 #endif // GTEST_HAS_TYPED_TEST