1 //
2 // Copyright 2015 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // Unit tests for HandleAllocator.
7 //
8
9 #include <unordered_set>
10
11 #include "gmock/gmock.h"
12 #include "gtest/gtest.h"
13
14 #include "libANGLE/HandleAllocator.h"
15
16 namespace
17 {
18
TEST(HandleAllocatorTest,ReservationsWithGaps)19 TEST(HandleAllocatorTest, ReservationsWithGaps)
20 {
21 gl::HandleAllocator allocator;
22
23 std::set<GLuint> allocationList;
24 for (GLuint id = 2; id < 50; id += 2)
25 {
26 allocationList.insert(id);
27 }
28
29 for (GLuint id : allocationList)
30 {
31 allocator.reserve(id);
32 }
33
34 std::set<GLuint> allocatedList;
35 for (size_t allocationNum = 0; allocationNum < allocationList.size() * 2; ++allocationNum)
36 {
37 GLuint handle = allocator.allocate();
38 EXPECT_EQ(0u, allocationList.count(handle));
39 EXPECT_EQ(0u, allocatedList.count(handle));
40 allocatedList.insert(handle);
41 }
42 }
43
TEST(HandleAllocatorTest,Random)44 TEST(HandleAllocatorTest, Random)
45 {
46 gl::HandleAllocator allocator;
47
48 std::set<GLuint> allocationList;
49 for (size_t iterationCount = 0; iterationCount < 40; ++iterationCount)
50 {
51 for (size_t randomCount = 0; randomCount < 40; ++randomCount)
52 {
53 GLuint randomHandle = (rand() % 1000) + 1;
54 if (allocationList.count(randomHandle) == 0)
55 {
56 allocator.reserve(randomHandle);
57 allocationList.insert(randomHandle);
58 }
59 }
60
61 for (size_t normalCount = 0; normalCount < 40; ++normalCount)
62 {
63 GLuint normalHandle = allocator.allocate();
64 EXPECT_EQ(0u, allocationList.count(normalHandle));
65 allocationList.insert(normalHandle);
66 }
67 }
68 }
69
TEST(HandleAllocatorTest,Reallocation)70 TEST(HandleAllocatorTest, Reallocation)
71 {
72 // Note: no current test for overflow
73 gl::HandleAllocator limitedAllocator(10);
74
75 for (GLuint count = 1; count < 10; count++)
76 {
77 GLuint result = limitedAllocator.allocate();
78 EXPECT_EQ(count, result);
79 }
80
81 for (GLuint count = 1; count < 10; count++)
82 {
83 limitedAllocator.release(count);
84 }
85
86 for (GLuint count = 2; count < 10; count++)
87 {
88 limitedAllocator.reserve(count);
89 }
90
91 GLint finalResult = limitedAllocator.allocate();
92 EXPECT_EQ(finalResult, 1);
93 }
94
95 // The following test covers reserving a handle with max uint value. See
96 // http://anglebug.com/42260058
TEST(HandleAllocatorTest,ReserveMaxUintHandle)97 TEST(HandleAllocatorTest, ReserveMaxUintHandle)
98 {
99 gl::HandleAllocator allocator;
100
101 GLuint maxUintHandle = std::numeric_limits<GLuint>::max();
102 allocator.reserve(maxUintHandle);
103
104 GLuint normalHandle = allocator.allocate();
105 EXPECT_EQ(1u, normalHandle);
106 }
107
108 // The following test covers reserving a handle with max uint value minus one then max uint value.
TEST(HandleAllocatorTest,ReserveMaxUintHandle2)109 TEST(HandleAllocatorTest, ReserveMaxUintHandle2)
110 {
111 gl::HandleAllocator allocator;
112
113 GLuint maxUintHandle = std::numeric_limits<GLuint>::max();
114 allocator.reserve(maxUintHandle - 1);
115 allocator.reserve(maxUintHandle);
116
117 GLuint normalHandle = allocator.allocate();
118 EXPECT_EQ(1u, normalHandle);
119 }
120
121 // To test if the allocator keep the handle in a sorted order.
TEST(HandleAllocatorTest,SortedOrderHandle)122 TEST(HandleAllocatorTest, SortedOrderHandle)
123 {
124 gl::HandleAllocator allocator;
125
126 allocator.reserve(3);
127
128 GLuint allocatedList[5];
129 for (GLuint count = 0; count < 5; count++)
130 {
131 allocatedList[count] = allocator.allocate();
132 }
133
134 EXPECT_EQ(1u, allocatedList[0]);
135 EXPECT_EQ(2u, allocatedList[1]);
136 EXPECT_EQ(4u, allocatedList[2]);
137 EXPECT_EQ(5u, allocatedList[3]);
138 EXPECT_EQ(6u, allocatedList[4]);
139 }
140
141 // Tests the reset method.
TEST(HandleAllocatorTest,Reset)142 TEST(HandleAllocatorTest, Reset)
143 {
144 gl::HandleAllocator allocator;
145
146 for (int iteration = 0; iteration < 1; ++iteration)
147 {
148 allocator.reserve(3);
149 EXPECT_EQ(1u, allocator.allocate());
150 EXPECT_EQ(2u, allocator.allocate());
151 EXPECT_EQ(4u, allocator.allocate());
152 allocator.reset();
153 }
154 }
155
156 // Tests the reset method of custom allocator works as expected.
TEST(HandleAllocatorTest,ResetAndReallocate)157 TEST(HandleAllocatorTest, ResetAndReallocate)
158 {
159 // Allocates handles - [1, 3]
160 gl::HandleAllocator allocator(3);
161 const std::unordered_set<GLuint> expectedHandles = {1, 2, 3};
162 std::unordered_set<GLuint> handles;
163
164 EXPECT_EQ(allocator.anyHandleAvailableForAllocation(), true);
165 handles.insert(allocator.allocate());
166 handles.insert(allocator.allocate());
167 handles.insert(allocator.allocate());
168 EXPECT_EQ(expectedHandles, handles);
169 EXPECT_EQ(allocator.anyHandleAvailableForAllocation(), false);
170
171 // Reset the allocator
172 allocator.reset();
173
174 EXPECT_EQ(allocator.anyHandleAvailableForAllocation(), true);
175 handles.insert(allocator.allocate());
176 handles.insert(allocator.allocate());
177 handles.insert(allocator.allocate());
178 EXPECT_EQ(expectedHandles, handles);
179 EXPECT_EQ(allocator.anyHandleAvailableForAllocation(), false);
180 }
181
182 // Covers a particular bug with reserving and allocating sub ranges.
TEST(HandleAllocatorTest,ReserveAndAllocateIterated)183 TEST(HandleAllocatorTest, ReserveAndAllocateIterated)
184 {
185 gl::HandleAllocator allocator;
186
187 for (int iteration = 0; iteration < 3; ++iteration)
188 {
189 allocator.reserve(5);
190 allocator.reserve(6);
191 GLuint a = allocator.allocate();
192 GLuint b = allocator.allocate();
193 GLuint c = allocator.allocate();
194 allocator.release(c);
195 allocator.release(a);
196 allocator.release(b);
197 allocator.release(5);
198 allocator.release(6);
199 }
200 }
201
202 // This test reproduces invalid heap bug when reserve resources after release.
TEST(HandleAllocatorTest,ReserveAfterReleaseBug)203 TEST(HandleAllocatorTest, ReserveAfterReleaseBug)
204 {
205 gl::HandleAllocator allocator;
206
207 for (int iteration = 1; iteration <= 16; ++iteration)
208 {
209 allocator.allocate();
210 }
211
212 allocator.release(15);
213 allocator.release(16);
214
215 for (int iteration = 1; iteration <= 14; ++iteration)
216 {
217 allocator.release(iteration);
218 }
219
220 allocator.reserve(1);
221
222 allocator.allocate();
223 }
224
225 // This test is to verify that we consolidate handle ranges when releasing a handle.
TEST(HandleAllocatorTest,ConsolidateRangeDuringRelease)226 TEST(HandleAllocatorTest, ConsolidateRangeDuringRelease)
227 {
228 gl::HandleAllocator allocator;
229
230 // Reserve GLuint(-1)
231 allocator.reserve(static_cast<GLuint>(-1));
232 // Allocate a few others
233 allocator.allocate();
234 allocator.allocate();
235
236 // Release GLuint(-1)
237 allocator.release(static_cast<GLuint>(-1));
238
239 // Allocate one more handle.
240 // Since we consolidate handle ranges during a release we do not expect to get back a
241 // handle value of GLuint(-1).
242 GLuint handle = allocator.allocate();
243 EXPECT_NE(handle, static_cast<GLuint>(-1));
244 }
245
246 } // anonymous namespace
247