1 //===-- Unittests for a freelist --------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
11 #include "src/__support/freelist.h"
12 #include "test/UnitTest/Test.h"
14 using LIBC_NAMESPACE::Block
;
15 using LIBC_NAMESPACE::FreeList
;
16 using LIBC_NAMESPACE::cpp::byte
;
17 using LIBC_NAMESPACE::cpp::optional
;
19 TEST(LlvmLibcFreeList
, FreeList
) {
21 optional
<Block
*> maybeBlock
= Block::init(mem
);
22 ASSERT_TRUE(maybeBlock
.has_value());
23 Block
*block1
= *maybeBlock
;
25 maybeBlock
= block1
->split(128);
26 ASSERT_TRUE(maybeBlock
.has_value());
27 Block
*block2
= *maybeBlock
;
29 maybeBlock
= block2
->split(128);
30 ASSERT_TRUE(maybeBlock
.has_value());
34 ASSERT_FALSE(list
.empty());
35 EXPECT_EQ(list
.front(), block1
);
38 EXPECT_EQ(list
.front(), block1
);
41 ASSERT_FALSE(list
.empty());
42 EXPECT_EQ(list
.front(), block2
);
45 ASSERT_TRUE(list
.empty());
49 list
.remove(reinterpret_cast<FreeList::Node
*>(block2
->usable_space()));
50 EXPECT_EQ(list
.front(), block1
);
52 ASSERT_TRUE(list
.empty());