SYCL: Avoid using no_init read accessor in rocFFT
[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,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 /*! \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) { return allocator.alignment() - 1; }
76 };
77
78 // NB need to use this->mask() because of GoogleTest quirks
79
80 TYPED_TEST(AllocatorTest, AllocatorAlignAllocatesWithAlignment) //NOLINT(misc-definitions-in-headers)
81 {
82     using pointer = typename TypeParam::value_type*;
83     TypeParam a;
84     pointer   p = a.allocate(1000);
85
86     EXPECT_EQ(0, reinterpret_cast<std::size_t>(p) & this->mask(a));
87     a.deallocate(p, 1000);
88 }
89
90
91 TYPED_TEST(AllocatorTest, VectorAllocatesAndResizesWithAlignment) //NOLINT(misc-definitions-in-headers)
92 {
93     using value_type = typename TypeParam::value_type;
94     std::vector<value_type, TypeParam> v(10);
95     EXPECT_EQ(0, reinterpret_cast<std::size_t>(v.data()) & this->mask(v.get_allocator()));
96
97     // Reserve a few times to check things work ok, making sure we
98     // will trigger several reallocations on common vector
99     // implementations.
100     for (std::size_t i = 1000; i <= 10000; i += 1000)
101     {
102         v.resize(i);
103         EXPECT_EQ(0, reinterpret_cast<std::size_t>(v.data()) & this->mask(v.get_allocator()));
104     }
105 }
106
107 TYPED_TEST(AllocatorTest, VectorAllocatesAndReservesWithAlignment) //NOLINT(misc-definitions-in-headers)
108 {
109     using value_type = typename TypeParam::value_type;
110     std::vector<value_type, TypeParam> v(10);
111     EXPECT_EQ(0, reinterpret_cast<std::size_t>(v.data()) & this->mask(v.get_allocator()));
112
113     // Reserve a few times to check things work ok, making sure we
114     // will trigger several reallocations on common vector
115     // implementations.
116     for (std::size_t i = 1000; i <= 10000; i += 1000)
117     {
118         v.reserve(i);
119         EXPECT_EQ(0, reinterpret_cast<std::size_t>(v.data()) & this->mask(v.get_allocator()));
120     }
121 }
122
123 TYPED_TEST(AllocatorTest, Move) //NOLINT(misc-definitions-in-headers)
124 {
125     using value_type = typename TypeParam::value_type;
126     std::vector<value_type, TypeParam> v1(1);
127     value_type*                        data = v1.data();
128     EXPECT_NE(data, nullptr);
129     std::vector<value_type, TypeParam> v2(std::move(v1));
130     EXPECT_EQ(data, v2.data());
131 }
132
133 } // namespace test
134 } // namespace gmx
135
136 #endif