[libc] Use best-fit binary trie to make malloc logarithmic (#106259)
[llvm-project.git] / libc / test / src / __support / freelist_test.cpp
blobf8bde941df098254ec628ee9928b2bce3533c669
1 //===-- Unittests for a freelist --------------------------------*- 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 //===----------------------------------------------------------------------===//
9 #include <stddef.h>
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) {
20 byte mem1[1024];
21 optional<Block<> *> maybeBlock = Block<>::init(mem1);
22 ASSERT_TRUE(maybeBlock.has_value());
23 Block<> *block1 = *maybeBlock;
25 byte mem2[1024];
26 maybeBlock = Block<>::init(mem2);
27 ASSERT_TRUE(maybeBlock.has_value());
28 Block<> *block2 = *maybeBlock;
30 FreeList list;
31 list.push(block1);
32 ASSERT_FALSE(list.empty());
33 EXPECT_EQ(list.front(), block1);
35 list.push(block2);
36 EXPECT_EQ(list.front(), block1);
38 list.pop();
39 ASSERT_FALSE(list.empty());
40 EXPECT_EQ(list.front(), block2);
42 list.pop();
43 ASSERT_TRUE(list.empty());
45 list.push(block1);
46 list.push(block2);
47 list.remove(reinterpret_cast<FreeList::Node *>(block2->usable_space()));
48 EXPECT_EQ(list.front(), block1);
49 list.pop();
50 ASSERT_TRUE(list.empty());