2 * This file is part of the GROMACS molecular simulation package.
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.
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.
35 /*! \libinternal \file
36 * \brief Tests for allocators that offer a minimum alignment.
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.
42 * \author Erik Lindahl <erik.lindahl@gmail.com>
43 * \author Mark Abraham <mark.j.abraham@gmail.com>
45 * \ingroup module_utility
47 #ifndef GMX_UTILITY_TESTS_ALIGNEDALLOCATOR_IMPL_H
48 #define GMX_UTILITY_TESTS_ALIGNEDALLOCATOR_IMPL_H
54 #include <gtest/gtest.h>
56 #include "gromacs/math/vectypes.h"
57 #include "gromacs/utility/real.h"
65 * \brief Templated test fixture. */
67 class AllocatorTest : public ::testing::Test
70 /*! \brief Return a bitmask for testing the alignment.
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)
77 return allocator.getPolicy().alignment() - 1;
81 // NB need to use this->mask() because of GoogleTest quirks
83 TYPED_TEST(AllocatorTest, AllocatorAlignAllocatesWithAlignment) //NOLINT(misc-definitions-in-headers)
85 using pointer = typename TypeParam::pointer;
87 pointer p = a.allocate(1000);
89 EXPECT_EQ(0, reinterpret_cast<std::size_t>(p) & this->mask(a));
90 a.deallocate(p, 1000);
94 TYPED_TEST(AllocatorTest, VectorAllocatesAndResizesWithAlignment) //NOLINT(misc-definitions-in-headers)
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()));
100 // Reserve a few times to check things work ok, making sure we
101 // will trigger several reallocations on common vector
103 for (std::size_t i = 1000; i <= 10000; i += 1000)
106 EXPECT_EQ(0, reinterpret_cast<std::size_t>(v.data()) & this->mask(v.get_allocator()));
110 TYPED_TEST(AllocatorTest, VectorAllocatesAndReservesWithAlignment) //NOLINT(misc-definitions-in-headers)
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()));
116 // Reserve a few times to check things work ok, making sure we
117 // will trigger several reallocations on common vector
119 for (std::size_t i = 1000; i <= 10000; i += 1000)
122 EXPECT_EQ(0, reinterpret_cast<std::size_t>(v.data()) & this->mask(v.get_allocator()));
126 TYPED_TEST(AllocatorTest, Move) //NOLINT(misc-definitions-in-headers)
128 using value_type = typename TypeParam::value_type;
129 std::vector<value_type, TypeParam> v1(1);
130 value_type* data = v1.data();
131 EXPECT_NE(data, nullptr);
132 std::vector<value_type, TypeParam> v2(std::move(v1));
133 EXPECT_EQ(data, v2.data());