Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/hotspot/gtest/utilities/test_resourceHash.cpp
64438 views
1
/*
2
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
#include "precompiled.hpp"
25
#include "memory/allocation.hpp"
26
#include "memory/resourceArea.hpp"
27
#include "unittest.hpp"
28
#include "utilities/debug.hpp"
29
#include "utilities/globalDefinitions.hpp"
30
#include "utilities/resourceHash.hpp"
31
32
class CommonResourceHashtableTest : public ::testing::Test {
33
protected:
34
typedef void* K;
35
typedef uintx V;
36
const static MEMFLAGS MEM_TYPE = mtInternal;
37
38
static unsigned identity_hash(const K& k) {
39
return (unsigned) (uintptr_t) k;
40
}
41
42
static unsigned bad_hash(const K& k) {
43
return 1;
44
}
45
46
static void* as_K(uintptr_t val) {
47
return (void*) val;
48
}
49
50
class EqualityTestIter {
51
public:
52
53
bool do_entry(K const& k, V const& v) {
54
if ((uintptr_t) k != (uintptr_t) v) {
55
EXPECT_EQ((uintptr_t) k, (uintptr_t) v);
56
return false;
57
} else {
58
return true; // continue iteration
59
}
60
}
61
};
62
63
class DeleterTestIter {
64
int _val;
65
public:
66
DeleterTestIter(int i) : _val(i) {}
67
68
bool do_entry(K const& k, V const& v) {
69
if ((uintptr_t) k == (uintptr_t) _val) {
70
// Delete me!
71
return true;
72
} else {
73
return false; // continue iteration
74
}
75
}
76
};
77
78
};
79
80
class SmallResourceHashtableTest : public CommonResourceHashtableTest {
81
protected:
82
83
template<
84
unsigned (*HASH) (K const&) = primitive_hash<K>,
85
bool (*EQUALS)(K const&, K const&) = primitive_equals<K>,
86
unsigned SIZE = 256,
87
ResourceObj::allocation_type ALLOC_TYPE = ResourceObj::RESOURCE_AREA
88
>
89
class Runner : public AllStatic {
90
public:
91
92
static void test(V step) {
93
EqualityTestIter et;
94
ResourceHashtable<K, V, HASH, EQUALS, SIZE, ALLOC_TYPE, MEM_TYPE> rh;
95
96
ASSERT_FALSE(rh.contains(as_K(step)));
97
98
ASSERT_TRUE(rh.put(as_K(step), step));
99
ASSERT_TRUE(rh.contains(as_K(step)));
100
101
ASSERT_FALSE(rh.put(as_K(step), step));
102
103
ASSERT_TRUE(rh.put(as_K(2 * step), 2 * step));
104
ASSERT_TRUE(rh.put(as_K(3 * step), 3 * step));
105
ASSERT_TRUE(rh.put(as_K(4 * step), 4 * step));
106
ASSERT_TRUE(rh.put(as_K(5 * step), 5 * step));
107
108
ASSERT_FALSE(rh.remove(as_K(0x0)));
109
110
rh.iterate(&et);
111
if (::testing::Test::HasFailure()) {
112
return;
113
}
114
115
ASSERT_TRUE(rh.remove(as_K(step)));
116
ASSERT_FALSE(rh.contains(as_K(step)));
117
rh.iterate(&et);
118
119
120
// Test put_if_absent(key) (creating a default-created value)
121
bool created = false;
122
V* v = rh.put_if_absent(as_K(step), &created);
123
ASSERT_TRUE(rh.contains(as_K(step)));
124
ASSERT_TRUE(created);
125
*v = (V)step;
126
127
// Calling this function a second time should yield the same value pointer
128
V* v2 = rh.put_if_absent(as_K(step), &created);
129
ASSERT_EQ(v, v2);
130
ASSERT_EQ(*v2, *v);
131
ASSERT_FALSE(created);
132
133
ASSERT_TRUE(rh.remove(as_K(step)));
134
ASSERT_FALSE(rh.contains(as_K(step)));
135
rh.iterate(&et);
136
137
// Test put_if_absent(key, value)
138
v = rh.put_if_absent(as_K(step), step, &created);
139
ASSERT_EQ(*v, step);
140
ASSERT_TRUE(rh.contains(as_K(step)));
141
ASSERT_TRUE(created);
142
143
v2 = rh.put_if_absent(as_K(step), step, &created);
144
// Calling this function a second time should yield the same value pointer
145
ASSERT_EQ(v, v2);
146
ASSERT_EQ(*v2, (V)step);
147
ASSERT_FALSE(created);
148
149
ASSERT_TRUE(rh.remove(as_K(step)));
150
ASSERT_FALSE(rh.contains(as_K(step)));
151
rh.iterate(&et);
152
153
154
}
155
};
156
};
157
158
TEST_VM_F(SmallResourceHashtableTest, default) {
159
ResourceMark rm;
160
Runner<>::test(0x1);
161
}
162
163
TEST_VM_F(SmallResourceHashtableTest, default_shifted) {
164
ResourceMark rm;
165
Runner<>::test(0x10);
166
}
167
168
TEST_VM_F(SmallResourceHashtableTest, bad_hash) {
169
ResourceMark rm;
170
Runner<bad_hash>::test(0x1);
171
}
172
173
TEST_VM_F(SmallResourceHashtableTest, bad_hash_shifted) {
174
ResourceMark rm;
175
Runner<bad_hash>::test(0x10);
176
}
177
178
TEST_VM_F(SmallResourceHashtableTest, identity_hash) {
179
ResourceMark rm;
180
Runner<identity_hash>::test(0x1);
181
}
182
183
TEST_VM_F(SmallResourceHashtableTest, identity_hash_shifted) {
184
ResourceMark rm;
185
Runner<identity_hash>::test(0x10);
186
}
187
188
TEST_VM_F(SmallResourceHashtableTest, primitive_hash_no_rm) {
189
Runner<primitive_hash<K>, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test(0x1);
190
}
191
192
TEST_VM_F(SmallResourceHashtableTest, primitive_hash_no_rm_shifted) {
193
Runner<primitive_hash<K>, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test(0x10);
194
}
195
196
TEST_VM_F(SmallResourceHashtableTest, bad_hash_no_rm) {
197
Runner<bad_hash, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test(0x1);
198
}
199
200
TEST_VM_F(SmallResourceHashtableTest, bad_hash_no_rm_shifted) {
201
Runner<bad_hash, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test(0x10);
202
}
203
204
TEST_VM_F(SmallResourceHashtableTest, identity_hash_no_rm) {
205
Runner<identity_hash, primitive_equals<K>, 1, ResourceObj::C_HEAP>::test(0x1);
206
}
207
208
TEST_VM_F(SmallResourceHashtableTest, identity_hash_no_rm_shifted) {
209
Runner<identity_hash, primitive_equals<K>, 1, ResourceObj::C_HEAP>::test(0x10);
210
}
211
212
class GenericResourceHashtableTest : public CommonResourceHashtableTest {
213
protected:
214
215
template<
216
unsigned (*HASH) (K const&) = primitive_hash<K>,
217
bool (*EQUALS)(K const&, K const&) = primitive_equals<K>,
218
unsigned SIZE = 256,
219
ResourceObj::allocation_type ALLOC_TYPE = ResourceObj::RESOURCE_AREA
220
>
221
class Runner : public AllStatic {
222
public:
223
224
static void test(unsigned num_elements = SIZE) {
225
EqualityTestIter et;
226
ResourceHashtable<K, V, HASH, EQUALS, SIZE, ALLOC_TYPE, MEM_TYPE> rh;
227
228
for (uintptr_t i = 0; i < num_elements; ++i) {
229
ASSERT_TRUE(rh.put(as_K(i), i));
230
}
231
232
rh.iterate(&et);
233
if (::testing::Test::HasFailure()) {
234
return;
235
}
236
237
for (uintptr_t i = num_elements; i > 0; --i) {
238
uintptr_t index = i - 1;
239
ASSERT_TRUE((rh.remove(as_K(index))));
240
}
241
242
rh.iterate(&et);
243
if (::testing::Test::HasFailure()) {
244
return;
245
}
246
for (uintptr_t i = num_elements; i > 0; --i) {
247
uintptr_t index = i - 1;
248
ASSERT_FALSE(rh.remove(as_K(index)));
249
}
250
rh.iterate(&et);
251
252
// Add more entries in and then delete one.
253
for (uintptr_t i = 10; i > 0; --i) {
254
uintptr_t index = i - 1;
255
ASSERT_TRUE(rh.put(as_K(index), index));
256
}
257
DeleterTestIter dt(5);
258
rh.unlink(&dt);
259
ASSERT_FALSE(rh.get(as_K(5)));
260
}
261
};
262
};
263
264
TEST_VM_F(GenericResourceHashtableTest, default) {
265
ResourceMark rm;
266
Runner<>::test();
267
}
268
269
TEST_VM_F(GenericResourceHashtableTest, bad_hash) {
270
ResourceMark rm;
271
Runner<bad_hash>::test();
272
}
273
274
TEST_VM_F(GenericResourceHashtableTest, identity_hash) {
275
ResourceMark rm;
276
Runner<identity_hash>::test();
277
}
278
279
TEST_VM_F(GenericResourceHashtableTest, primitive_hash_no_rm) {
280
Runner<primitive_hash<K>, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test();
281
}
282
283
TEST_VM_F(GenericResourceHashtableTest, bad_hash_no_rm) {
284
Runner<bad_hash, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test();
285
}
286
287
TEST_VM_F(GenericResourceHashtableTest, identity_hash_no_rm) {
288
Runner<identity_hash, primitive_equals<K>, 1, ResourceObj::C_HEAP>::test(512);
289
}
290
291