Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/gtest/utilities/test_resourceHash.cpp
41145 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
};
64
65
class SmallResourceHashtableTest : public CommonResourceHashtableTest {
66
protected:
67
68
template<
69
unsigned (*HASH) (K const&) = primitive_hash<K>,
70
bool (*EQUALS)(K const&, K const&) = primitive_equals<K>,
71
unsigned SIZE = 256,
72
ResourceObj::allocation_type ALLOC_TYPE = ResourceObj::RESOURCE_AREA
73
>
74
class Runner : public AllStatic {
75
public:
76
77
static void test(V step) {
78
EqualityTestIter et;
79
ResourceHashtable<K, V, HASH, EQUALS, SIZE, ALLOC_TYPE, MEM_TYPE> rh;
80
81
ASSERT_FALSE(rh.contains(as_K(step)));
82
83
ASSERT_TRUE(rh.put(as_K(step), step));
84
ASSERT_TRUE(rh.contains(as_K(step)));
85
86
ASSERT_FALSE(rh.put(as_K(step), step));
87
88
ASSERT_TRUE(rh.put(as_K(2 * step), 2 * step));
89
ASSERT_TRUE(rh.put(as_K(3 * step), 3 * step));
90
ASSERT_TRUE(rh.put(as_K(4 * step), 4 * step));
91
ASSERT_TRUE(rh.put(as_K(5 * step), 5 * step));
92
93
ASSERT_FALSE(rh.remove(as_K(0x0)));
94
95
rh.iterate(&et);
96
if (::testing::Test::HasFailure()) {
97
return;
98
}
99
100
ASSERT_TRUE(rh.remove(as_K(step)));
101
ASSERT_FALSE(rh.contains(as_K(step)));
102
rh.iterate(&et);
103
104
105
// Test put_if_absent(key) (creating a default-created value)
106
bool created = false;
107
V* v = rh.put_if_absent(as_K(step), &created);
108
ASSERT_TRUE(rh.contains(as_K(step)));
109
ASSERT_TRUE(created);
110
*v = (V)step;
111
112
// Calling this function a second time should yield the same value pointer
113
V* v2 = rh.put_if_absent(as_K(step), &created);
114
ASSERT_EQ(v, v2);
115
ASSERT_EQ(*v2, *v);
116
ASSERT_FALSE(created);
117
118
ASSERT_TRUE(rh.remove(as_K(step)));
119
ASSERT_FALSE(rh.contains(as_K(step)));
120
rh.iterate(&et);
121
122
// Test put_if_absent(key, value)
123
v = rh.put_if_absent(as_K(step), step, &created);
124
ASSERT_EQ(*v, step);
125
ASSERT_TRUE(rh.contains(as_K(step)));
126
ASSERT_TRUE(created);
127
128
v2 = rh.put_if_absent(as_K(step), step, &created);
129
// Calling this function a second time should yield the same value pointer
130
ASSERT_EQ(v, v2);
131
ASSERT_EQ(*v2, (V)step);
132
ASSERT_FALSE(created);
133
134
ASSERT_TRUE(rh.remove(as_K(step)));
135
ASSERT_FALSE(rh.contains(as_K(step)));
136
rh.iterate(&et);
137
138
139
}
140
};
141
};
142
143
TEST_VM_F(SmallResourceHashtableTest, default) {
144
ResourceMark rm;
145
Runner<>::test(0x1);
146
}
147
148
TEST_VM_F(SmallResourceHashtableTest, default_shifted) {
149
ResourceMark rm;
150
Runner<>::test(0x10);
151
}
152
153
TEST_VM_F(SmallResourceHashtableTest, bad_hash) {
154
ResourceMark rm;
155
Runner<bad_hash>::test(0x1);
156
}
157
158
TEST_VM_F(SmallResourceHashtableTest, bad_hash_shifted) {
159
ResourceMark rm;
160
Runner<bad_hash>::test(0x10);
161
}
162
163
TEST_VM_F(SmallResourceHashtableTest, identity_hash) {
164
ResourceMark rm;
165
Runner<identity_hash>::test(0x1);
166
}
167
168
TEST_VM_F(SmallResourceHashtableTest, identity_hash_shifted) {
169
ResourceMark rm;
170
Runner<identity_hash>::test(0x10);
171
}
172
173
TEST_VM_F(SmallResourceHashtableTest, primitive_hash_no_rm) {
174
Runner<primitive_hash<K>, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test(0x1);
175
}
176
177
TEST_VM_F(SmallResourceHashtableTest, primitive_hash_no_rm_shifted) {
178
Runner<primitive_hash<K>, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test(0x10);
179
}
180
181
TEST_VM_F(SmallResourceHashtableTest, bad_hash_no_rm) {
182
Runner<bad_hash, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test(0x1);
183
}
184
185
TEST_VM_F(SmallResourceHashtableTest, bad_hash_no_rm_shifted) {
186
Runner<bad_hash, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test(0x10);
187
}
188
189
TEST_VM_F(SmallResourceHashtableTest, identity_hash_no_rm) {
190
Runner<identity_hash, primitive_equals<K>, 1, ResourceObj::C_HEAP>::test(0x1);
191
}
192
193
TEST_VM_F(SmallResourceHashtableTest, identity_hash_no_rm_shifted) {
194
Runner<identity_hash, primitive_equals<K>, 1, ResourceObj::C_HEAP>::test(0x10);
195
}
196
197
class GenericResourceHashtableTest : public CommonResourceHashtableTest {
198
protected:
199
200
template<
201
unsigned (*HASH) (K const&) = primitive_hash<K>,
202
bool (*EQUALS)(K const&, K const&) = primitive_equals<K>,
203
unsigned SIZE = 256,
204
ResourceObj::allocation_type ALLOC_TYPE = ResourceObj::RESOURCE_AREA
205
>
206
class Runner : public AllStatic {
207
public:
208
209
static void test(unsigned num_elements = SIZE) {
210
EqualityTestIter et;
211
ResourceHashtable<K, V, HASH, EQUALS, SIZE, ALLOC_TYPE, MEM_TYPE> rh;
212
213
for (uintptr_t i = 0; i < num_elements; ++i) {
214
ASSERT_TRUE(rh.put(as_K(i), i));
215
}
216
217
rh.iterate(&et);
218
if (::testing::Test::HasFailure()) {
219
return;
220
}
221
222
for (uintptr_t i = num_elements; i > 0; --i) {
223
uintptr_t index = i - 1;
224
ASSERT_TRUE((rh.remove(as_K(index))));
225
}
226
227
rh.iterate(&et);
228
if (::testing::Test::HasFailure()) {
229
return;
230
}
231
for (uintptr_t i = num_elements; i > 0; --i) {
232
uintptr_t index = i - 1;
233
ASSERT_FALSE(rh.remove(as_K(index)));
234
}
235
rh.iterate(&et);
236
}
237
};
238
};
239
240
TEST_VM_F(GenericResourceHashtableTest, default) {
241
ResourceMark rm;
242
Runner<>::test();
243
}
244
245
TEST_VM_F(GenericResourceHashtableTest, bad_hash) {
246
ResourceMark rm;
247
Runner<bad_hash>::test();
248
}
249
250
TEST_VM_F(GenericResourceHashtableTest, identity_hash) {
251
ResourceMark rm;
252
Runner<identity_hash>::test();
253
}
254
255
TEST_VM_F(GenericResourceHashtableTest, primitive_hash_no_rm) {
256
Runner<primitive_hash<K>, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test();
257
}
258
259
TEST_VM_F(GenericResourceHashtableTest, bad_hash_no_rm) {
260
Runner<bad_hash, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test();
261
}
262
263
TEST_VM_F(GenericResourceHashtableTest, identity_hash_no_rm) {
264
Runner<identity_hash, primitive_equals<K>, 1, ResourceObj::C_HEAP>::test(512);
265
}
266
267