1//===-- Unittests for MemorySize ------------------------------------------===//
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/__support/memory_size.h"
10#include "test/UnitTest/Test.h"
11
12namespace LIBC_NAMESPACE {
13namespace internal {
14static inline constexpr size_t SAFE_MEM_SIZE_TEST_LIMIT =
15 static_cast<size_t>(cpp::numeric_limits<cpp::make_signed_t<size_t>>::max());
16
17TEST(LlvmLibcMemSizeTest, Constuction) {
18 ASSERT_FALSE(SafeMemSize{static_cast<size_t>(-1)}.valid());
19 ASSERT_FALSE(SafeMemSize{static_cast<size_t>(-2)}.valid());
20 ASSERT_FALSE(SafeMemSize{static_cast<size_t>(-1024 + 33)}.valid());
21 ASSERT_FALSE(SafeMemSize{static_cast<size_t>(-1024 + 66)}.valid());
22 ASSERT_FALSE(SafeMemSize{SAFE_MEM_SIZE_TEST_LIMIT + 1}.valid());
23 ASSERT_FALSE(SafeMemSize{SAFE_MEM_SIZE_TEST_LIMIT + 13}.valid());
24
25 ASSERT_TRUE(SafeMemSize{static_cast<size_t>(1)}.valid());
26 ASSERT_TRUE(SafeMemSize{static_cast<size_t>(1024 + 13)}.valid());
27 ASSERT_TRUE(SafeMemSize{static_cast<size_t>(2048 - 13)}.valid());
28 ASSERT_TRUE(SafeMemSize{static_cast<size_t>(4096 + 1)}.valid());
29 ASSERT_TRUE(SafeMemSize{static_cast<size_t>(8192 - 1)}.valid());
30 ASSERT_TRUE(SafeMemSize{static_cast<size_t>(16384 + 15)}.valid());
31 ASSERT_TRUE(SafeMemSize{static_cast<size_t>(32768 * 3)}.valid());
32 ASSERT_TRUE(SafeMemSize{static_cast<size_t>(65536 * 13)}.valid());
33 ASSERT_TRUE(SafeMemSize{SAFE_MEM_SIZE_TEST_LIMIT}.valid());
34 ASSERT_TRUE(SafeMemSize{SAFE_MEM_SIZE_TEST_LIMIT - 1}.valid());
35 ASSERT_TRUE(SafeMemSize{SAFE_MEM_SIZE_TEST_LIMIT - 13}.valid());
36}
37
38TEST(LlvmLibcMemSizeTest, Addition) {
39 auto max = SafeMemSize{SAFE_MEM_SIZE_TEST_LIMIT};
40 auto half = SafeMemSize{SAFE_MEM_SIZE_TEST_LIMIT / 2};
41 auto third = SafeMemSize{SAFE_MEM_SIZE_TEST_LIMIT / 3};
42
43 ASSERT_TRUE(half.valid());
44 ASSERT_TRUE(third.valid());
45 ASSERT_TRUE((half + half).valid());
46 ASSERT_TRUE((third + third + third).valid());
47 ASSERT_TRUE((half + third).valid());
48
49 ASSERT_FALSE((max + SafeMemSize{static_cast<size_t>(1)}).valid());
50 ASSERT_FALSE((third + third + third + third).valid());
51 ASSERT_FALSE((half + half + half).valid());
52
53 ASSERT_FALSE((SafeMemSize{static_cast<size_t>(-1)} +
54 SafeMemSize{static_cast<size_t>(2)})
55 .valid());
56 ASSERT_FALSE((SafeMemSize{static_cast<size_t>(2)} +
57 SafeMemSize{static_cast<size_t>(-1)})
58 .valid());
59}
60
61TEST(LlvmLibcMemSizeTest, Multiplication) {
62 auto max = SafeMemSize{SAFE_MEM_SIZE_TEST_LIMIT};
63 auto half = SafeMemSize{SAFE_MEM_SIZE_TEST_LIMIT / 2};
64 auto third = SafeMemSize{SAFE_MEM_SIZE_TEST_LIMIT / 3};
65
66 ASSERT_TRUE((max * SafeMemSize{static_cast<size_t>(1)}).valid());
67 ASSERT_TRUE((max * SafeMemSize{static_cast<size_t>(0)}).valid());
68
69 ASSERT_FALSE((max * SafeMemSize{static_cast<size_t>(2)}).valid());
70 ASSERT_FALSE((half * half).valid());
71 ASSERT_FALSE((half * SafeMemSize{static_cast<size_t>(3)}).valid());
72 ASSERT_FALSE((third * SafeMemSize{static_cast<size_t>(4)}).valid());
73}
74
75TEST(LlvmLibcMemSizeTest, AlignUp) {
76 size_t sizes[] = {
77 0, 1, 8, 13, 60, 97, 128, 1024, 5124, 5120,
78 };
79 for (size_t i = 2; i <= 16; ++i) {
80 size_t alignment = 1 << i;
81 for (size_t size : sizes) {
82 auto safe_size = SafeMemSize{size};
83 auto safe_aligned_size = safe_size.align_up(alignment);
84 ASSERT_TRUE(safe_aligned_size.valid());
85 ASSERT_EQ(static_cast<size_t>(safe_aligned_size) % alignment, size_t(0));
86 }
87 }
88 auto max = SafeMemSize{SAFE_MEM_SIZE_TEST_LIMIT};
89 ASSERT_FALSE(max.align_up(8).valid());
90}
91
92TEST(LlvmLibcBlockBitTest, OffsetTo) {
93 ASSERT_EQ(SafeMemSize::offset_to(0, 512), size_t(0));
94 ASSERT_EQ(SafeMemSize::offset_to(1, 512), size_t(511));
95 ASSERT_EQ(SafeMemSize::offset_to(2, 512), size_t(510));
96 ASSERT_EQ(SafeMemSize::offset_to(13, 1), size_t(0));
97 ASSERT_EQ(SafeMemSize::offset_to(13, 4), size_t(3));
98 for (unsigned int i = 0; i < 31; ++i) {
99 ASSERT_EQ((SafeMemSize::offset_to(i, 1u << i) + i) % (1u << i), size_t(0));
100 }
101}
102} // namespace internal
103} // namespace LIBC_NAMESPACE
104

source code of libc/test/src/__support/memory_size_test.cpp