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