aboutsummaryrefslogtreecommitdiff
path: root/libc/test/src/stdlib/memalignment_test.cpp
blob: f1640e996e261c6f0212ef8b6b311c0fe78ec437 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//===-- Unittests for memalignment ----------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "hdr/stdint_proxy.h"
#include "src/stdlib/memalignment.h"
#include "test/UnitTest/Test.h"

TEST(LlvmLibcMemAlignmentTest, NullPointer) {
  void *ptr = nullptr;
  EXPECT_EQ(LIBC_NAMESPACE::memalignment(ptr), static_cast<size_t>(0));
}

TEST(LlvmLibcMemAlignmentTest, SpecificAlignment) {

  // These addresses have known alignment patterns - if we can construct them
  uintptr_t addr_align2 = 0x2;   // 2-byte aligned
  uintptr_t addr_align4 = 0x4;   // 4-byte aligned
  uintptr_t addr_align8 = 0x8;   // 8-byte aligned
  uintptr_t addr_align16 = 0x10; // 16-byte aligned
  uintptr_t addr_align32 = 0x20; // 32-byte aligned

  void *ptr_align2 = reinterpret_cast<void *>(addr_align2);
  void *ptr_align4 = reinterpret_cast<void *>(addr_align4);
  void *ptr_align8 = reinterpret_cast<void *>(addr_align8);
  void *ptr_align16 = reinterpret_cast<void *>(addr_align16);
  void *ptr_align32 = reinterpret_cast<void *>(addr_align32);

  EXPECT_EQ(LIBC_NAMESPACE::memalignment(ptr_align2), static_cast<size_t>(2));
  EXPECT_EQ(LIBC_NAMESPACE::memalignment(ptr_align4), static_cast<size_t>(4));
  EXPECT_EQ(LIBC_NAMESPACE::memalignment(ptr_align8), static_cast<size_t>(8));
  EXPECT_EQ(LIBC_NAMESPACE::memalignment(ptr_align16), static_cast<size_t>(16));
  EXPECT_EQ(LIBC_NAMESPACE::memalignment(ptr_align32), static_cast<size_t>(32));

  uintptr_t addr_complex = 0x1234560; // 16-byte aligned (ends in 0)
  void *ptr_complex = reinterpret_cast<void *>(addr_complex);
  EXPECT_EQ(LIBC_NAMESPACE::memalignment(ptr_complex), static_cast<size_t>(32));
}

TEST(LlvmLibcMemAlignmentTest, AlignasSpecifiedAlignment) {
  alignas(16) static int aligned_16;
  alignas(32) static int aligned_32;
  alignas(64) static int aligned_64;
  alignas(128) static int aligned_128;
  alignas(256) static int aligned_256;

  EXPECT_GE(LIBC_NAMESPACE::memalignment(&aligned_16), static_cast<size_t>(16));
  EXPECT_GE(LIBC_NAMESPACE::memalignment(&aligned_32), static_cast<size_t>(32));
  EXPECT_GE(LIBC_NAMESPACE::memalignment(&aligned_64), static_cast<size_t>(64));
  EXPECT_GE(LIBC_NAMESPACE::memalignment(&aligned_128),
            static_cast<size_t>(128));
  EXPECT_GE(LIBC_NAMESPACE::memalignment(&aligned_256),
            static_cast<size_t>(256));
}