gem5
[DEVELOP-FOR-25.0]
Loading...
Searching...
No Matches
base
free_list.test.cc
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2024 The Board of Trustees of the Leland Stanford
3
* Junior University
4
* All rights reserved.
5
*
6
* Redistribution and use in source and binary forms, with or without
7
* modification, are permitted provided that the following conditions are
8
* met: redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer;
10
* redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution;
13
* neither the name of the copyright holders nor the names of its
14
* contributors may be used to endorse or promote products derived from
15
* this software without specific prior written permission.
16
*
17
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
*/
29
30
#include <gtest/gtest.h>
31
32
#include "
base/free_list.hh
"
33
34
using namespace
gem5
;
35
36
TEST
(FreeListTest, Simple)
37
{
38
FreeList<int>
l
;
39
EXPECT_EQ(
l
.size(), 0);
40
EXPECT_EQ(
l
.ranges().size(), 0);
41
l
.insert(0, 16);
42
EXPECT_EQ(
l
.size(), 16);
43
EXPECT_EQ(
l
.ranges().size(), 1);
44
const
std::optional<int>
x
=
l
.allocate(16);
45
EXPECT_TRUE(
x
.has_value());
46
EXPECT_EQ(
l
.size(), 0);
47
EXPECT_EQ(
l
.ranges().size(), 0);
48
}
49
50
TEST
(FreeListTest, FailedAllocation)
51
{
52
FreeList<int>
l
(0, 16);
53
EXPECT_EQ(
l
.size(), 16);
54
const
std::optional<int>
x
=
l
.allocate(17);
55
ASSERT_FALSE(
x
.has_value());
56
ASSERT_EQ(
l
.size(), 16);
57
}
58
59
TEST
(FreeListTest, SucceededAllocation)
60
{
61
FreeList<int>
l
(0, 16);
62
const
std::optional<int>
x
=
l
.allocate(8);
63
ASSERT_TRUE(
x
.has_value());
64
ASSERT_EQ(
l
.size(), 8);
65
}
66
67
TEST
(FreeListTest, MergeLeft)
68
{
69
FreeList<int>
l
(0, 16);
70
l
.insert(16, 8);
71
ASSERT_EQ(
l
.size(), 24);
72
ASSERT_EQ(
l
.ranges().size(), 1);
73
}
74
75
TEST
(FreeListTest, MergeRight)
76
{
77
FreeList<int>
l
(8, 16);
78
l
.insert(0, 8);
79
ASSERT_EQ(
l
.size(), 24);
80
ASSERT_EQ(
l
.ranges().size(), 1);
81
}
82
83
TEST
(FreeListTest, MergeBoth)
84
{
85
FreeList<int>
l
;
86
l
.insert(0, 8);
87
l
.insert(16, 8);
88
ASSERT_EQ(
l
.size(), 16);
89
ASSERT_EQ(
l
.ranges().size(), 2);
90
l
.insert(8, 8);
91
ASSERT_EQ(
l
.size(), 24);
92
ASSERT_EQ(
l
.ranges().size(), 1);
93
}
94
95
TEST
(FreeListTest, DoubleFreeIdenticalDeath)
96
{
97
FreeList<int>
l
;
98
l
.insert(0, 1);
99
ASSERT_ANY_THROW(
l
.insert(0, 1));
100
}
101
102
TEST
(FreeListTest, DoubleFreeSubrangeDeath)
103
{
104
FreeList<int>
l
;
105
l
.insert(0, 2);
106
ASSERT_ANY_THROW(
l
.insert(0, 1));
107
}
108
109
TEST
(FreeListTest, DoubleFreeSuperrangeDeath)
110
{
111
FreeList<int>
l
;
112
l
.insert(1, 2);
113
ASSERT_ANY_THROW(
l
.insert(0, 3));
114
}
115
116
TEST
(FreeListTest, DoubleFreeOverlapLeftDeath)
117
{
118
FreeList<int>
l
;
119
l
.insert(1, 3);
120
ASSERT_ANY_THROW(
l
.insert(0, 2));
121
}
122
123
TEST
(FreeListTest, DoubleFreeOverlapRightDeath)
124
{
125
FreeList<int>
l
;
126
l
.insert(1, 3);
127
ASSERT_ANY_THROW(
l
.insert(2, 4));
128
}
129
130
TEST
(FreeListTest, DoubleFreeMultiDeath)
131
{
132
FreeList<int>
l
;
133
l
.insert(0, 1);
134
l
.insert(2, 3);
135
ASSERT_ANY_THROW(
l
.insert(0, 3));
136
}
free_list.hh
gem5::FreeList
Definition
free_list.hh:45
TEST
TEST(FreeListTest, Simple)
Definition
free_list.test.cc:36
gem5::MipsISA::l
Bitfield< 5 > l
Definition
pra_constants.hh:323
gem5::RiscvISA::x
Bitfield< 3 > x
Definition
pagetable.hh:76
gem5
Copyright (c) 2024 Arm Limited All rights reserved.
Definition
binary32.hh:36
Generated on Mon May 26 2025 09:19:06 for gem5 by
doxygen
1.13.2