Enable clang tidy/warnings for tests
[alexxy/gromacs.git] / src / gromacs / utility / tests / alignedallocator-impl.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2015,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 /*! \libinternal \file
36  * \brief Tests for allocators that offer a minimum alignment.
37  *
38  * This implementation header can be included in multiple modules
39  * tests, which is currently needed because gpu_utils is physically
40  * separate from the utility module.
41  *
42  * \author Erik Lindahl <erik.lindahl@gmail.com>
43  * \author Mark Abraham <mark.j.abraham@gmail.com>
44  * \inlibraryapi
45  * \ingroup module_utility
46  */
47 #ifndef GMX_UTILITY_TESTS_ALIGNEDALLOCATOR_IMPL_H
48 #define GMX_UTILITY_TESTS_ALIGNEDALLOCATOR_IMPL_H
49
50 #include <cstddef>
51
52 #include <vector>
53
54 #include <gtest/gtest.h>
55
56 #include "gromacs/math/vectypes.h"
57 #include "gromacs/utility/real.h"
58
59 namespace gmx
60 {
61 namespace test
62 {
63
64 /*! \libinternal
65  * \brief Templated test fixture. */
66 template <typename T>
67 class AllocatorTest : public ::testing::Test
68 {
69     public:
70         /*! \brief Return a bitmask for testing the alignment.
71          *
72          * e.g. for 128-byte alignment the mask is 128-1 - all of
73          * these bits should be zero in pointers that have the
74          * intended alignment. */
75         std::size_t mask(const T &allocator)
76         {
77             return allocator.getPolicy().alignment() - 1;
78         }
79 };
80
81 // NB need to use this->mask() because of GoogleTest quirks
82
83 TYPED_TEST(AllocatorTest, AllocatorAlignAllocatesWithAlignment) //NOLINT(misc-definitions-in-headers)
84 {
85     using pointer = typename TypeParam::pointer;
86     TypeParam a;
87     pointer   p = a.allocate(1000);
88
89     EXPECT_EQ(0, reinterpret_cast<std::size_t>(p) & this->mask(a));
90     a.deallocate(p, 1000);
91 }
92
93
94 TYPED_TEST(AllocatorTest, VectorAllocatesAndResizesWithAlignment) //NOLINT(misc-definitions-in-headers)
95 {
96     using value_type = typename TypeParam::value_type;
97     std::vector<value_type, TypeParam> v(10);
98     EXPECT_EQ(0, reinterpret_cast<std::size_t>(v.data()) & this->mask(v.get_allocator()));
99
100     // Reserve a few times to check things work ok, making sure we
101     // will trigger several reallocations on common vector
102     // implementations.
103     for (std::size_t i = 1000; i <= 10000; i += 1000)
104     {
105         v.resize(i);
106         EXPECT_EQ(0, reinterpret_cast<std::size_t>(v.data()) & this->mask(v.get_allocator()));
107     }
108 }
109
110 TYPED_TEST(AllocatorTest, VectorAllocatesAndReservesWithAlignment) //NOLINT(misc-definitions-in-headers)
111 {
112     using value_type = typename TypeParam::value_type;
113     std::vector<value_type, TypeParam> v(10);
114     EXPECT_EQ(0, reinterpret_cast<std::size_t>(v.data()) & this->mask(v.get_allocator()));
115
116     // Reserve a few times to check things work ok, making sure we
117     // will trigger several reallocations on common vector
118     // implementations.
119     for (std::size_t i = 1000; i <= 10000; i += 1000)
120     {
121         v.reserve(i);
122         EXPECT_EQ(0, reinterpret_cast<std::size_t>(v.data()) & this->mask(v.get_allocator()));
123     }
124 }
125
126 } // namespace
127 } // namespace
128
129 #endif