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 //===----------------------------------------------------------------------===// 8 9 #include <stddef.h> 10 11 #include "src/__support/freelist.h" 12 #include "test/UnitTest/Test.h" 13 14 using LIBC_NAMESPACE::Block; 15 using LIBC_NAMESPACE::FreeList; 16 using LIBC_NAMESPACE::cpp::byte; 17 using LIBC_NAMESPACE::cpp::optional; 18 19 TEST(LlvmLibcFreeList, FreeList) { 20 byte mem[1024]; 21 optional<Block *> maybeBlock = Block::init(mem); 22 ASSERT_TRUE(maybeBlock.has_value()); 23 Block *block1 = *maybeBlock; 24 25 maybeBlock = block1->split(128); 26 ASSERT_TRUE(maybeBlock.has_value()); 27 Block *block2 = *maybeBlock; 28 29 maybeBlock = block2->split(128); 30 ASSERT_TRUE(maybeBlock.has_value()); 31 32 FreeList list; 33 list.push(block1); 34 ASSERT_FALSE(list.empty()); 35 EXPECT_EQ(list.front(), block1); 36 37 list.push(block2); 38 EXPECT_EQ(list.front(), block1); 39 40 list.pop(); 41 ASSERT_FALSE(list.empty()); 42 EXPECT_EQ(list.front(), block2); 43 44 list.pop(); 45 ASSERT_TRUE(list.empty()); 46 47 list.push(block1); 48 list.push(block2); 49 list.remove(reinterpret_cast<FreeList::Node *>(block2->usable_space())); 50 EXPECT_EQ(list.front(), block1); 51 list.pop(); 52 ASSERT_TRUE(list.empty()); 53 } 54