-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_segment.cpp
More file actions
232 lines (187 loc) · 8.17 KB
/
test_segment.cpp
File metadata and controls
232 lines (187 loc) · 8.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include <gtest/gtest.h>
#include "segment_allocator.h"
static constexpr uint32_t error_result = 0;
using Allocator = SegmentAllocator<15, 6 * 64, 2 * 64, 64, error_result>;
using Errors = std::pair<uint64_t, uint64_t>;
using BlockStat = std::tuple<uint32_t, uint32_t, uint32_t>;
BlockStat Block(const Allocator& allocator, uint16_t size)
{
const auto& info = allocator.GetBlocksStat()[size];
return {info.busy_blocks, info.used_blocks, info.used_segments};
}
TEST(SegmentAllocator, Simple)
{
Errors no_errors(0, 0);
Allocator allocator;
// Interval [15, 384), BlockSize = 128.
// 3 intervals [15, 143), [143, 271), [271, 384) - length of the last block = 113 < 128.
// Blocks 0,2 - the blocks will be used for segments of length 23, block 1 - 27.
// Capacity of blocks: 0 => [128/23] = 5, 1 => [128/27] = 4, 2 => [113/23] = 4.
// --------------------------------------------------------------------------------------------
// STEP 1: - Allocate segments, we will get results:
// len=23: Block 0 (15, 38, 61, 84, 107), Block 2 (271, 294, 317, 340)
// len=27: Block 1 (143, 170, 197, 224)
EXPECT_EQ(allocator.GetErrors(), no_errors);
EXPECT_EQ(Block(allocator, 0), BlockStat(0, 3, 0));
EXPECT_EQ(allocator.Allocate(23), 15);
EXPECT_EQ(allocator.GetErrors(), no_errors);
EXPECT_EQ(Block(allocator, 0), BlockStat(0, 2, 0)); // Block 0 for 23
EXPECT_EQ(Block(allocator, 23), BlockStat(0, 1, 1));
EXPECT_EQ(allocator.Allocate(27), 143); // Block 1 for 27
EXPECT_EQ(allocator.GetErrors(), no_errors);
EXPECT_EQ(Block(allocator, 0), BlockStat(0, 1, 0));
EXPECT_EQ(Block(allocator, 27), BlockStat(0, 1, 1));
EXPECT_EQ(allocator.Allocate(23), 38);
EXPECT_EQ(allocator.GetErrors(), no_errors);
EXPECT_EQ(Block(allocator, 0), BlockStat(0, 1, 0));
EXPECT_EQ(Block(allocator, 23), BlockStat(0, 1, 2));
EXPECT_EQ(allocator.Allocate(23), 61);
EXPECT_EQ(allocator.GetErrors(), no_errors);
EXPECT_EQ(Block(allocator, 0), BlockStat(0, 1, 0));
EXPECT_EQ(Block(allocator, 23), BlockStat(0, 1, 3));
EXPECT_EQ(allocator.Allocate(23), 84);
EXPECT_EQ(allocator.GetErrors(), no_errors);
EXPECT_EQ(Block(allocator, 0), BlockStat(0, 1, 0));
EXPECT_EQ(Block(allocator, 23), BlockStat(0, 1, 4));
EXPECT_EQ(allocator.Allocate(23), 107);
EXPECT_EQ(allocator.GetErrors(), no_errors);
EXPECT_EQ(Block(allocator, 0), BlockStat(0, 1, 0));
EXPECT_EQ(Block(allocator, 23), BlockStat(1, 0, 5)); // Block 0 - busy
EXPECT_EQ(allocator.Allocate(27), 170);
EXPECT_EQ(allocator.GetErrors(), no_errors);
EXPECT_EQ(Block(allocator, 0), BlockStat(0, 1, 0));
EXPECT_EQ(Block(allocator, 27), BlockStat(0, 1, 2));
EXPECT_EQ(allocator.Allocate(27), 197);
EXPECT_EQ(allocator.GetErrors(), no_errors);
EXPECT_EQ(Block(allocator, 0), BlockStat(0, 1, 0));
EXPECT_EQ(Block(allocator, 27), BlockStat(0, 1, 3));
EXPECT_EQ(allocator.Allocate(23), 271); // Block 2 for 23
EXPECT_EQ(allocator.GetErrors(), no_errors);
EXPECT_EQ(Block(allocator, 0), BlockStat(0, 0, 0));
EXPECT_EQ(Block(allocator, 23), BlockStat(1, 1, 6));
EXPECT_EQ(allocator.Allocate(23), 294);
EXPECT_EQ(allocator.GetErrors(), no_errors);
EXPECT_EQ(Block(allocator, 23), BlockStat(1, 1, 7));
EXPECT_EQ(allocator.Allocate(27), 224);
EXPECT_EQ(allocator.GetErrors(), no_errors);
EXPECT_EQ(Block(allocator, 27), BlockStat(1, 0, 4)); // Block 1 - busy
EXPECT_EQ(allocator.Allocate(23), 317);
EXPECT_EQ(allocator.GetErrors(), no_errors);
EXPECT_EQ(Block(allocator, 23), BlockStat(1, 1, 8));
EXPECT_EQ(allocator.Allocate(27), 0);
EXPECT_EQ(allocator.GetErrors(), no_errors);
EXPECT_EQ(allocator.Allocate(23), 340);
EXPECT_EQ(allocator.GetErrors(), no_errors);
EXPECT_EQ(Block(allocator, 23), BlockStat(2, 0, 9)); // Block 2 - busy
EXPECT_EQ(allocator.Allocate(23), 0);
EXPECT_EQ(allocator.GetErrors(), no_errors);
// Finish allocate:
// - no free blocks
// - len=23 (2 - busy blocks, 9 - segments)
// - len=27 (1 - busy block, 4 - segments)
EXPECT_EQ(Block(allocator, 0), BlockStat(0, 0, 0));
EXPECT_EQ(Block(allocator, 23), BlockStat(2, 0, 9));
EXPECT_EQ(Block(allocator, 27), BlockStat(1, 0, 4));
// --------------------------------------------------------------------------------------------
// STEP 2: - Release the length 23 segments (271, 294, 317, 340) from block 2 and then place
// the length 27 segments in it
EXPECT_TRUE(allocator.Free(271, 23));
EXPECT_EQ(Block(allocator, 23), BlockStat(1, 1, 8)); // Block 2 - in list 23
EXPECT_TRUE(allocator.Free(294, 23));
EXPECT_TRUE(allocator.Free(317, 23));
EXPECT_EQ(allocator.GetErrors(), no_errors);
EXPECT_EQ(Block(allocator, 23), BlockStat(1, 1, 6));
// release last segment of the length 23
EXPECT_TRUE(allocator.Free(340, 23));
EXPECT_EQ(Block(allocator, 23), BlockStat(1, 0, 5));
EXPECT_EQ(Block(allocator, 0), BlockStat(0, 1, 0)); // Block 2 - free
// start allocate segments len=27
EXPECT_EQ(allocator.Allocate(27), 271);
EXPECT_EQ(allocator.GetErrors(), no_errors);
EXPECT_EQ(Block(allocator, 27), BlockStat(1, 1, 5)); // Block 2 for 27
EXPECT_EQ(Block(allocator, 0), BlockStat(0, 0, 0));
// Allocate other segments
EXPECT_EQ(allocator.Allocate(27), 298);
EXPECT_EQ(allocator.Allocate(27), 325);
EXPECT_EQ(allocator.Allocate(27), 352);
EXPECT_EQ(allocator.Allocate(27), 0);
EXPECT_EQ(allocator.GetErrors(), no_errors);
EXPECT_EQ(Block(allocator, 27), BlockStat(2, 0, 8));
// --------------------------------------------------------------------------------------------
// STEP 3: - Release all segments
// Block 2
EXPECT_TRUE(allocator.Free(298, 27));
EXPECT_TRUE(allocator.Free(352, 27));
EXPECT_TRUE(allocator.Free(325, 27));
EXPECT_TRUE(allocator.Free(271, 27));
EXPECT_EQ(allocator.GetErrors(), no_errors);
EXPECT_EQ(Block(allocator, 27), BlockStat(1, 0, 4));
EXPECT_EQ(Block(allocator, 0), BlockStat(0, 1, 0));
// Block 0
EXPECT_TRUE(allocator.Free(15, 23));
EXPECT_TRUE(allocator.Free(38, 23));
EXPECT_TRUE(allocator.Free(61, 23));
EXPECT_TRUE(allocator.Free(84, 23));
EXPECT_TRUE(allocator.Free(107, 23));
EXPECT_EQ(allocator.GetErrors(), no_errors);
EXPECT_EQ(Block(allocator, 23), BlockStat(0, 0, 0));
EXPECT_EQ(Block(allocator, 0), BlockStat(0, 2, 0));
// Block 1
EXPECT_TRUE(allocator.Free(143, 27));
EXPECT_TRUE(allocator.Free(170, 27));
EXPECT_TRUE(allocator.Free(197, 27));
EXPECT_TRUE(allocator.Free(224, 27));
EXPECT_EQ(allocator.GetErrors(), no_errors);
EXPECT_EQ(Block(allocator, 27), BlockStat(0, 0, 0));
EXPECT_EQ(Block(allocator, 0), BlockStat(0, 3, 0));
EXPECT_EQ(allocator.GetErrors(), no_errors);
}
TEST(SegmentAllocator, BigSize)
{
static constexpr uint32_t index_begin = 128;
static constexpr uint32_t index_end = 8 * 1024 * 1024;
using AllocatorBig = SegmentAllocator<index_begin, index_end, 64 * 64, 64, error_result>;
uint8_t data[sizeof(AllocatorBig)];
memset((void*)data, -1, sizeof(AllocatorBig));
AllocatorBig* allocator = new (data) AllocatorBig();
uint16_t size = 4;
uint32_t full_size = index_end - index_begin;
for (uint32_t index = index_begin; index < index_end; index += size)
{
EXPECT_EQ(allocator->Allocate(size), index);
full_size -= size;
EXPECT_EQ(allocator->Size(), full_size);
}
for (uint32_t index = index_begin; index < index_end; index += size)
{
EXPECT_TRUE(allocator->Free(index, size));
full_size += size;
EXPECT_EQ(allocator->Size(), full_size);
}
Errors no_errors(0, 0);
EXPECT_EQ(allocator->GetErrors(), no_errors);
}
TEST(SegmentAllocator, OnlyErrors)
{
Allocator allocator;
EXPECT_EQ(allocator.Allocate(23), 15);
EXPECT_EQ(allocator.Allocate(0), 0); // size = 0
EXPECT_EQ(allocator.Allocate(65), 0); // size > 64
EXPECT_EQ(allocator.GetErrors(), Errors(2, 0));
// 4 errors for this case:
// ((size == 0) || (size > MaxBufferSize) || (start >= IndexEnd) || (start < IndexBegin))
EXPECT_FALSE(allocator.Free(50, 0));
EXPECT_FALSE(allocator.Free(50, 65));
EXPECT_FALSE(allocator.Free(0, 10));
EXPECT_FALSE(allocator.Free(500, 0));
EXPECT_EQ(allocator.GetErrors(), Errors(6, 0));
EXPECT_FALSE(allocator.Free(15, 27)); // segment size 27 != 23
EXPECT_FALSE(allocator.Free(16, 23)); // segments starts at (15, 15+23, ...)
EXPECT_FALSE(allocator.Free(38, 23)); // segment free
EXPECT_EQ(allocator.GetErrors(), Errors(9, 0));
}
int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}