[libc-commits] [libc] [libc][stdlib] Add Block class (PR #94407)
Michael Jones via libc-commits
libc-commits at lists.llvm.org
Fri Jun 7 13:53:26 PDT 2024
================
@@ -0,0 +1,560 @@
+//===-- Unittests for a block of memory -------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+#include <stddef.h>
+
+#include "src/stdlib/block.h"
+
+#include "src/__support/CPP/array.h"
+#include "src/__support/CPP/span.h"
+#include "src/string/memcpy.h"
+#include "test/UnitTest/Test.h"
+
+// Test fixtures.
+using LargeOffsetBlock = LIBC_NAMESPACE::Block<uint64_t>;
+using SmallOffsetBlock = LIBC_NAMESPACE::Block<uint16_t>;
+
+// Macro to provide type-parameterized tests for the various block types above.
+#define TEST_FOR_EACH_BLOCK_TYPE(TestCase) \
+ class LlvmLibcBlockTest##TestCase : public LIBC_NAMESPACE::testing::Test { \
+ public: \
+ template <typename BlockType> void RunTest(); \
+ }; \
+ TEST_F(LlvmLibcBlockTest##TestCase, TestCase) { \
+ RunTest<LargeOffsetBlock>(); \
+ RunTest<SmallOffsetBlock>(); \
+ } \
+ template <typename BlockType> void LlvmLibcBlockTest##TestCase::RunTest()
+
+using LIBC_NAMESPACE::cpp::array;
+using LIBC_NAMESPACE::cpp::byte;
+using LIBC_NAMESPACE::cpp::span;
+
+TEST_FOR_EACH_BLOCK_TYPE(CanCreateSingleAlignedBlock) {
+ constexpr size_t kN = 1024;
+ alignas(BlockType::kAlignment) array<byte, kN> bytes;
+
+ auto result = BlockType::Init(bytes);
+ ASSERT_TRUE(result.has_value());
+ BlockType *block = *result;
+
+ EXPECT_EQ(block->OuterSize(), kN);
+ EXPECT_EQ(block->InnerSize(), kN - BlockType::kBlockOverhead);
+ EXPECT_EQ(block->Prev(), static_cast<BlockType *>(nullptr));
+ EXPECT_EQ(block->Next(), static_cast<BlockType *>(nullptr));
+ EXPECT_FALSE(block->Used());
+ EXPECT_TRUE(block->Last());
+}
+
+TEST_FOR_EACH_BLOCK_TYPE(CanCreateUnalignedSingleBlock) {
+ constexpr size_t kN = 1024;
+
+ // Force alignment, so we can un-force it below
+ alignas(BlockType::kAlignment) array<byte, kN> bytes;
+ span<byte> aligned(bytes);
+
+ auto result = BlockType::Init(aligned.subspan(1));
+ EXPECT_TRUE(result.has_value());
+}
+
+TEST_FOR_EACH_BLOCK_TYPE(CannotCreateTooSmallBlock) {
+ array<byte, 2> bytes;
+ auto result = BlockType::Init(bytes);
+ EXPECT_FALSE(result.has_value());
+}
+
+TEST(LlvmLibcBlockTest, CannotCreateTooLargeBlock) {
+ using BlockType = LIBC_NAMESPACE::Block<uint8_t>;
----------------
michaelrj-google wrote:
I don't see a comment on this test, did it not get pushed?
https://github.com/llvm/llvm-project/pull/94407
More information about the libc-commits
mailing list