Skip to content

Commit 845184e

Browse files
committed
test: adding tests for memalignment
1 parent 000e4e2 commit 845184e

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

libc/test/src/stdlib/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,4 +457,16 @@ if(LLVM_LIBC_FULL_BUILD)
457457
libc.src.stdlib.free
458458
)
459459
endif()
460+
add_libc_test(
461+
memalignment_test
462+
# The EXPECT_EXITS test is only availible for unit tests.
463+
UNIT_TEST_ONLY
464+
SUITE
465+
libc-stdlib-tests
466+
SRCS
467+
memalignment_test.cpp
468+
DEPENDS
469+
libc.include.stdlib
470+
libc.src.stdlib.memalignment
471+
)
460472
endif()
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//===-- Unittests for memalignment ---------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/stdlib/memalignment.h"
10+
#include "src/stdlib/malloc.h"
11+
#include "src/stdlib/free.h"
12+
#include "test/UnitTest/Test.h"
13+
14+
#include <stdint.h>
15+
16+
TEST(LlvmLibcMemAlignmentTest, NullPointer) {
17+
void *ptr = nullptr;
18+
EXPECT_EQ(LIBC_NAMESPACE::memalignment(ptr), static_cast<size_t>(0));
19+
}
20+
21+
TEST(LlvmLibcMemAlignmentTest, MallocedPointers) {
22+
int *int_ptr = reinterpret_cast<int *>(LIBC_NAMESPACE::malloc(sizeof(int)));
23+
EXPECT_NE(reinterpret_cast<void *>(int_ptr), static_cast<void *>(nullptr));
24+
25+
size_t int_alignment = LIBC_NAMESPACE::memalignment(int_ptr);
26+
EXPECT_GE(int_alignment, alignof(int));
27+
28+
29+
LIBC_NAMESPACE::free(int_ptr);
30+
31+
// Allocate a double (typically 8-byte aligned)
32+
double *double_ptr = reinterpret_cast<double *>(LIBC_NAMESPACE::malloc(sizeof(double)));
33+
EXPECT_NE(reinterpret_cast<void *>(double_ptr), static_cast<void *>(nullptr));
34+
35+
size_t double_alignment = LIBC_NAMESPACE::memalignment(double_ptr);
36+
EXPECT_GE(double_alignment, alignof(double));
37+
38+
EXPECT_EQ(double_alignment & (double_alignment - 1), static_cast<size_t>(0));
39+
40+
LIBC_NAMESPACE::free(double_ptr);
41+
}
42+
43+
TEST(LlvmLibcMemAlignmentTest, SpecificAlignment) {
44+
45+
// These addresses have known alignment patterns - if we can construct them
46+
uintptr_t addr_align2 = 0x2; // 2-byte aligned
47+
uintptr_t addr_align4 = 0x4; // 4-byte aligned
48+
uintptr_t addr_align8 = 0x8; // 8-byte aligned
49+
uintptr_t addr_align16 = 0x10; // 16-byte aligned
50+
uintptr_t addr_align32 = 0x20; // 32-byte aligned
51+
52+
void *ptr_align2 = reinterpret_cast<void *>(addr_align2);
53+
void *ptr_align4 = reinterpret_cast<void *>(addr_align4);
54+
void *ptr_align8 = reinterpret_cast<void *>(addr_align8);
55+
void *ptr_align16 = reinterpret_cast<void *>(addr_align16);
56+
void *ptr_align32 = reinterpret_cast<void *>(addr_align32);
57+
58+
EXPECT_EQ(LIBC_NAMESPACE::memalignment(ptr_align2), static_cast<size_t>(2));
59+
EXPECT_EQ(LIBC_NAMESPACE::memalignment(ptr_align4), static_cast<size_t>(4));
60+
EXPECT_EQ(LIBC_NAMESPACE::memalignment(ptr_align8), static_cast<size_t>(8));
61+
EXPECT_EQ(LIBC_NAMESPACE::memalignment(ptr_align16), static_cast<size_t>(16));
62+
EXPECT_EQ(LIBC_NAMESPACE::memalignment(ptr_align32), static_cast<size_t>(32));
63+
64+
uintptr_t addr_complex = 0x1234560; // 16-byte aligned (ends in 0)
65+
void *ptr_complex = reinterpret_cast<void *>(addr_complex);
66+
EXPECT_EQ(LIBC_NAMESPACE::memalignment(ptr_complex), static_cast<size_t>(16));
67+
}

0 commit comments

Comments
 (0)