1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/memory/aligned_memory.h"
6
7 #include <memory>
8
9 #include "build/build_config.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 #define EXPECT_ALIGNED(ptr, align) \
13 EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & (align - 1))
14
15 namespace base {
16
TEST(AlignedMemoryTest,DynamicAllocation)17 TEST(AlignedMemoryTest, DynamicAllocation) {
18 void* p = AlignedAlloc(8, 8);
19 EXPECT_TRUE(p);
20 EXPECT_ALIGNED(p, 8);
21 AlignedFree(p);
22
23 p = AlignedAlloc(8, 16);
24 EXPECT_TRUE(p);
25 EXPECT_ALIGNED(p, 16);
26 AlignedFree(p);
27
28 p = AlignedAlloc(8, 256);
29 EXPECT_TRUE(p);
30 EXPECT_ALIGNED(p, 256);
31 AlignedFree(p);
32
33 p = AlignedAlloc(8, 4096);
34 EXPECT_TRUE(p);
35 EXPECT_ALIGNED(p, 4096);
36 AlignedFree(p);
37 }
38
TEST(AlignedMemoryTest,ScopedDynamicAllocation)39 TEST(AlignedMemoryTest, ScopedDynamicAllocation) {
40 std::unique_ptr<float, AlignedFreeDeleter> p(
41 static_cast<float*>(AlignedAlloc(8, 8)));
42 EXPECT_TRUE(p.get());
43 EXPECT_ALIGNED(p.get(), 8);
44 }
45
46 } // namespace base
47