xref: /aosp_15_r20/external/cronet/base/memory/aligned_memory_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
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 <string.h>
8 
9 #include <memory>
10 
11 #include "build/build_config.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 
14 namespace base {
15 
TEST(AlignedMemoryTest,DynamicAllocation)16 TEST(AlignedMemoryTest, DynamicAllocation) {
17   void* p = AlignedAlloc(8, 8);
18   ASSERT_TRUE(p);
19   EXPECT_TRUE(IsAligned(p, 8));
20   memset(p, 0, 8);  // Fill to check allocated size under ASAN.
21   AlignedFree(p);
22 
23   p = AlignedAlloc(8, 16);
24   ASSERT_TRUE(p);
25   EXPECT_TRUE(IsAligned(p, 16));
26   memset(p, 0, 8);  // Fill to check allocated size under ASAN.
27   AlignedFree(p);
28 
29   p = AlignedAlloc(8, 256);
30   ASSERT_TRUE(p);
31   EXPECT_TRUE(IsAligned(p, 256));
32   memset(p, 0, 8);  // Fill to check allocated size under ASAN.
33   AlignedFree(p);
34 
35   p = AlignedAlloc(8, 4096);
36   ASSERT_TRUE(p);
37   EXPECT_TRUE(IsAligned(p, 4096));
38   memset(p, 0, 8);  // Fill to check allocated size under ASAN.
39   AlignedFree(p);
40 }
41 
TEST(AlignedMemoryTest,ScopedDynamicAllocation)42 TEST(AlignedMemoryTest, ScopedDynamicAllocation) {
43   std::unique_ptr<float, AlignedFreeDeleter> p(
44       static_cast<float*>(AlignedAlloc(8, 8)));
45   EXPECT_TRUE(p.get());
46   EXPECT_TRUE(IsAligned(p.get(), 8));
47 
48   // Make sure IsAligned() can check const pointers as well.
49   const float* const_p = p.get();
50   EXPECT_TRUE(IsAligned(const_p, 8));
51 }
52 
TEST(AlignedMemoryTest,IsAligned)53 TEST(AlignedMemoryTest, IsAligned) {
54   // Check alignment around powers of two.
55   for (int i = 0; i < 64; ++i) {
56     const uint64_t n = static_cast<uint64_t>(1) << i;
57 
58     // Walk back down all lower powers of two checking alignment.
59     for (int j = i - 1; j >= 0; --j) {
60       // n is aligned on all powers of two less than or equal to 2^i.
61       EXPECT_TRUE(IsAligned(n, n >> j))
62           << "Expected " << n << " to be " << (n >> j) << " aligned";
63 
64       // Also, n - 1 should not be aligned on ANY lower power of two except 1
65       // (but since we're starting from i - 1 we don't test that case here.
66       EXPECT_FALSE(IsAligned(n - 1, n >> j))
67           << "Expected " << (n - 1) << " to NOT be " << (n >> j) << " aligned";
68     }
69   }
70 
71   // And a few hard coded smoke tests for completeness:
72   EXPECT_TRUE(IsAligned(4, 2));
73   EXPECT_TRUE(IsAligned(8, 4));
74   EXPECT_TRUE(IsAligned(8, 2));
75   EXPECT_TRUE(IsAligned(0x1000, 4 << 10));
76   EXPECT_TRUE(IsAligned(0x2000, 8 << 10));
77   EXPECT_TRUE(IsAligned(1, 1));
78   EXPECT_TRUE(IsAligned(7, 1));
79   EXPECT_TRUE(IsAligned(reinterpret_cast<void*>(0x1000), 4 << 10));
80   EXPECT_TRUE(IsAligned(reinterpret_cast<int*>(0x1000), 4 << 10));
81 
82   EXPECT_FALSE(IsAligned(3, 2));
83   EXPECT_FALSE(IsAligned(7, 4));
84   EXPECT_FALSE(IsAligned(7, 2));
85   EXPECT_FALSE(IsAligned(0x1001, 4 << 10));
86   EXPECT_FALSE(IsAligned(0x999, 8 << 10));
87   EXPECT_FALSE(IsAligned(7, 8));
88 }
89 
90 }  // namespace base
91