Support pinning in HostAllocator
[alexxy/gromacs.git] / src / gromacs / utility / tests / alignedallocator.cpp
index 09dc6101574d3e5ccd170c78ccb12a88074b2e98..11a695336f61262feb7242c12c7c085e360ed0d4 100644 (file)
@@ -33,7 +33,7 @@
  * the research papers on the package. Check out http://www.gromacs.org.
  */
 /*! \internal \file
- * \brief Tests for gmx::AlignedAllocator
+ * \brief Tests for gmx::AlignedAllocator and gmx::PageAlignedAllocator.
  *
  * \author Erik Lindahl <erik.lindahl@gmail.com>
  * \author Mark Abraham <mark.j.abraham@gmail.com>
 #include <gtest/gtest.h>
 
 #include "gromacs/math/vectypes.h"
-#include "gromacs/utility/real.h"
 
 namespace gmx
 {
-
-//! Templated test fixture.
-template <typename T>
-class AllocatorTest : public ::testing::Test
+namespace test
 {
-    public:
-        /*! \brief Bitmask for testing the alignment.
-         *
-         * e.g. for 128-byte alignment the mask is 128-1 - all of
-         * these bits should be zero in pointers that have the
-         * intended alignment. */
-        std::size_t mask_ = T::allocation_policy::alignment()-1;
-};
 
 //! Declare allocator types to test.
 using AllocatorTypesToTest = ::testing::Types<AlignedAllocator<real>,
@@ -75,52 +63,19 @@ using AllocatorTypesToTest = ::testing::Types<AlignedAllocator<real>,
                                               AlignedAllocator<RVec>,
                                               PageAlignedAllocator<RVec>
                                               >;
-TYPED_TEST_CASE(AllocatorTest, AllocatorTypesToTest);
 
-// NB need to use this->mask_ because of GoogleTest quirks
-
-TYPED_TEST(AllocatorTest, AllocatorAlignAllocatesWithAlignment)
-{
-    using pointer = typename TypeParam::pointer;
-    TypeParam a;
-    pointer   p = a.allocate(1000);
+TYPED_TEST_CASE(AllocatorTest, AllocatorTypesToTest);
 
-    EXPECT_EQ(0, reinterpret_cast<std::size_t>(p) & this->mask_);
-    a.deallocate(p, 1000);
-}
+} // namespace
+} // namespace
 
+// Includes tests common to all allocation policies.
+#include "gromacs/utility/tests/alignedallocator-impl.h"
 
-TYPED_TEST(AllocatorTest, VectorAllocatesAndResizesWithAlignment)
+namespace gmx
 {
-    using value_type = typename TypeParam::value_type;
-    std::vector<value_type, TypeParam> v(10);
-    EXPECT_EQ(0, reinterpret_cast<std::size_t>(v.data()) & this->mask_);
-
-    // Reserve a few times to check things work ok, making sure we
-    // will trigger several reallocations on common vector
-    // implementations.
-    for (std::size_t i = 1000; i <= 10000; i += 1000)
-    {
-        v.resize(i);
-        EXPECT_EQ(0, reinterpret_cast<std::size_t>(v.data()) & this->mask_);
-    }
-}
-
-TYPED_TEST(AllocatorTest, VectorAllocatesAndReservesWithAlignment)
+namespace test
 {
-    using value_type = typename TypeParam::value_type;
-    std::vector<value_type, TypeParam> v(10);
-    EXPECT_EQ(0, reinterpret_cast<std::size_t>(v.data()) & this->mask_);
-
-    // Reserve a few times to check things work ok, making sure we
-    // will trigger several reallocations on common vector
-    // implementations.
-    for (std::size_t i = 1000; i <= 10000; i += 1000)
-    {
-        v.reserve(i);
-        EXPECT_EQ(0, reinterpret_cast<std::size_t>(v.data()) & this->mask_);
-    }
-}
 
 TYPED_TEST(AllocatorTest, StatelessAllocatorUsesNoMemory)
 {
@@ -129,4 +84,5 @@ TYPED_TEST(AllocatorTest, StatelessAllocatorUsesNoMemory)
               sizeof(std::vector<value_type, TypeParam>));
 }
 
-}
+} // namespace
+} // namespace