1 //===-- alignment.cpp -------------------------------------------*- C++ -*-===// 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 "gwp_asan/tests/harness.h" 10 #include "gwp_asan/utilities.h" 11 12 #include <vector> 13 14 TEST(AlignmentTest, PowerOfTwo) { 15 std::vector<std::pair<size_t, size_t>> AskedSizeToAlignedSize = { 16 {1, 1}, {2, 2}, {3, 4}, {4, 4}, {5, 8}, {7, 8}, 17 {8, 8}, {9, 16}, {15, 16}, {16, 16}, {17, 32}, {31, 32}, 18 {32, 32}, {33, 48}, {4095, 4096}, {4096, 4096}, 19 }; 20 21 for (const auto &KV : AskedSizeToAlignedSize) { 22 EXPECT_EQ(KV.second, 23 gwp_asan::rightAlignedAllocationSize( 24 KV.first, gwp_asan::AlignmentStrategy::POWER_OF_TWO)); 25 } 26 } 27 28 TEST(AlignmentTest, AlignBionic) { 29 std::vector<std::pair<size_t, size_t>> AskedSizeToAlignedSize = { 30 {1, 8}, {2, 8}, {3, 8}, {4, 8}, {5, 8}, {7, 8}, 31 {8, 8}, {9, 16}, {15, 16}, {16, 16}, {17, 24}, {31, 32}, 32 {32, 32}, {33, 40}, {4095, 4096}, {4096, 4096}, 33 }; 34 35 for (const auto &KV : AskedSizeToAlignedSize) { 36 EXPECT_EQ(KV.second, gwp_asan::rightAlignedAllocationSize( 37 KV.first, gwp_asan::AlignmentStrategy::BIONIC)); 38 } 39 } 40 41 TEST(AlignmentTest, PerfectAlignment) { 42 for (size_t i = 1; i <= 4096; ++i) { 43 EXPECT_EQ(i, gwp_asan::rightAlignedAllocationSize( 44 i, gwp_asan::AlignmentStrategy::PERFECT)); 45 } 46 } 47