diff options
author | Guillaume Chatelet <gchatelet@google.com> | 2019-10-14 13:14:34 +0000 |
---|---|---|
committer | Guillaume Chatelet <gchatelet@google.com> | 2019-10-14 13:14:34 +0000 |
commit | ce56e1a1cc5714f4af5675dd963cfebed766d9e1 (patch) | |
tree | 45c6eae172f40a05492dc6566b21e66a836e41ff /llvm/unittests/Support/AlignmentTest.cpp | |
parent | 77748129650271ebd7b3f9c2c6c4f8110cb4a845 (diff) | |
download | bcm5719-llvm-ce56e1a1cc5714f4af5675dd963cfebed766d9e1.tar.gz bcm5719-llvm-ce56e1a1cc5714f4af5675dd963cfebed766d9e1.zip |
[Alignment][NFC] Move and type functions from MathExtras to Alignment
Summary:
This is patch is part of a series to introduce an Alignment type.
See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2019-July/133851.html
See this patch for the introduction of the type: https://reviews.llvm.org/D64790
Reviewers: courbet
Subscribers: hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D68942
llvm-svn: 374773
Diffstat (limited to 'llvm/unittests/Support/AlignmentTest.cpp')
-rw-r--r-- | llvm/unittests/Support/AlignmentTest.cpp | 42 |
1 files changed, 40 insertions, 2 deletions
diff --git a/llvm/unittests/Support/AlignmentTest.cpp b/llvm/unittests/Support/AlignmentTest.cpp index a6c053385cb..35fac5e51a1 100644 --- a/llvm/unittests/Support/AlignmentTest.cpp +++ b/llvm/unittests/Support/AlignmentTest.cpp @@ -90,6 +90,11 @@ TEST(AlignmentTest, AlignTo) { uint64_t alignment; uint64_t offset; uint64_t rounded; + const void *forgedAddr() const { + // A value of any integral or enumeration type can be converted to a + // pointer type. + return reinterpret_cast<const void *>(offset); + } } kTests[] = { // MaybeAlign {0, 0, 0}, @@ -116,6 +121,7 @@ TEST(AlignmentTest, AlignTo) { // Test Align if (A) { EXPECT_EQ(alignTo(T.offset, A.getValue()), T.rounded); + EXPECT_EQ(alignAddr(T.forgedAddr(), A.getValue()), T.rounded); } } } @@ -174,13 +180,17 @@ TEST(AlignmentTest, Encode_Decode) { EXPECT_EQ(Expected, Actual); } -TEST(AlignmentTest, isAligned) { +TEST(AlignmentTest, isAligned_isAddrAligned) { struct { uint64_t alignment; uint64_t offset; bool isAligned; + const void *forgedAddr() const { + // A value of any integral or enumeration type can be converted to a + // pointer type. + return reinterpret_cast<const void *>(offset); + } } kTests[] = { - // MaybeAlign / Align {1, 0, true}, {1, 1, true}, {1, 5, true}, {2, 0, true}, {2, 1, false}, {2, 2, true}, {2, 7, false}, {2, 16, true}, {4, 0, true}, {4, 1, false}, {4, 4, true}, {4, 6, false}, @@ -192,10 +202,32 @@ TEST(AlignmentTest, isAligned) { // Test Align if (A) { EXPECT_EQ(isAligned(A.getValue(), T.offset), T.isAligned); + EXPECT_EQ(isAddrAligned(A.getValue(), T.forgedAddr()), T.isAligned); } } } +TEST(AlignmentTest, offsetToAlignment) { + struct { + uint64_t alignment; + uint64_t offset; + uint64_t alignedOffset; + const void *forgedAddr() const { + // A value of any integral or enumeration type can be converted to a + // pointer type. + return reinterpret_cast<const void *>(offset); + } + } kTests[] = { + {1, 0, 0}, {1, 1, 0}, {1, 5, 0}, {2, 0, 0}, {2, 1, 1}, {2, 2, 0}, + {2, 7, 1}, {2, 16, 0}, {4, 0, 0}, {4, 1, 3}, {4, 4, 0}, {4, 6, 2}, + }; + for (const auto &T : kTests) { + const Align A(T.alignment); + EXPECT_EQ(offsetToAlignment(T.offset, A), T.alignedOffset); + EXPECT_EQ(offsetToAlignedAddr(T.forgedAddr(), A), T.alignedOffset); + } +} + TEST(AlignmentTest, AlignComparisons) { std::vector<uint64_t> ValidAlignments = getValidAlignments(); std::sort(ValidAlignments.begin(), ValidAlignments.end()); @@ -349,6 +381,12 @@ TEST(AlignmentDeathTest, CompareAlignToUndefMaybeAlign) { } } +TEST(AlignmentDeathTest, AlignAddr) { + const void *const unaligned_high_ptr = + reinterpret_cast<const void *>(std::numeric_limits<uintptr_t>::max() - 1); + EXPECT_DEATH(alignAddr(unaligned_high_ptr, Align(16)), "Overflow"); +} + #endif // NDEBUG } // end anonymous namespace |