forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIdentifierTableTest.cpp
More file actions
200 lines (163 loc) · 5.96 KB
/
IdentifierTableTest.cpp
File metadata and controls
200 lines (163 loc) · 5.96 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
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "hermes/VM/IdentifierTable.h"
#include "hermes/Support/UTF8.h"
#include "hermes/VM/StringPrimitive.h"
#include "hermes/VM/StringRefUtils.h"
#include "hermes/VM/StringView.h"
#include "TestHelpers.h"
#include "gtest/gtest.h"
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
using namespace hermes;
using namespace hermes::vm;
namespace {
using IdentifierTableLargeHeapTest = LargeHeapRuntimeTestFixture;
TEST_F(IdentifierTableLargeHeapTest, LookupTest) {
IdentifierTable &table = runtime->getIdentifierTable();
uint32_t predefinedCount = table.getSymbolsEnd();
UTF16Ref a{u"foo", 3};
UTF16Ref b{u"ab", 2};
UTF16Ref c{u"foo", 3};
SymbolID sa = table.getSymbolHandle(runtime, a).getValue().get();
ASSERT_GE(sa.unsafeGetIndex(), predefinedCount);
SymbolID sb = table.getSymbolHandle(runtime, b).getValue().get();
ASSERT_GE(sb.unsafeGetIndex(), predefinedCount);
EXPECT_EQ(0u, sa.unsafeGetIndex() - predefinedCount);
EXPECT_EQ(1u, sb.unsafeGetIndex() - predefinedCount);
EXPECT_EQ(
0u,
table.getSymbolHandle(runtime, a).getValue().get().unsafeGetIndex() -
predefinedCount);
EXPECT_EQ(
0u,
table.getSymbolHandle(runtime, c).getValue().get().unsafeGetIndex() -
predefinedCount);
auto d = StringPrimitive::createNoThrow(runtime, llvh::StringRef("foo"));
EXPECT_EQ(
0u,
table.getSymbolHandleFromPrimitive(runtime, d)
.getValue()
.get()
.unsafeGetIndex() -
predefinedCount);
auto e = StringPrimitive::createNoThrow(runtime, llvh::StringRef("ab"));
EXPECT_EQ(
1u,
table.getSymbolHandleFromPrimitive(runtime, e)
.getValue()
.get()
.unsafeGetIndex() -
predefinedCount);
EXPECT_TRUE(table.getStringView(runtime, sa).equals(a));
EXPECT_TRUE(table.getStringView(runtime, sb).equals(b));
SmallU16String<8> tmp;
table.getStringView(runtime, sa).appendUTF16String(tmp);
EXPECT_EQ(a, tmp.arrayRef());
tmp.clear();
table.getStringView(runtime, sb).appendUTF16String(tmp);
EXPECT_EQ(b, tmp.arrayRef());
// Ensure allocations are aligned.
EXPECT_EQ(
0u,
(uint64_t)runtime->getStringPrimFromSymbolID(sa) % (uint64_t)HeapAlign);
EXPECT_EQ(
0u,
(uint64_t)runtime->getStringPrimFromSymbolID(sb) % (uint64_t)HeapAlign);
}
using IdentifierTableTest = RuntimeTestFixture;
TEST_F(IdentifierTableTest, NotUniquedSymbol) {
auto &idTable = runtime->getIdentifierTable();
{
ASCIIRef asdf{"asdf", 4};
Handle<StringPrimitive> id1 = runtime->makeHandle<StringPrimitive>(
*StringPrimitive::create(runtime, asdf));
Handle<SymbolID> sym =
runtime->makeHandle(*idTable.createNotUniquedSymbol(runtime, id1));
EXPECT_TRUE((*sym).isNotUniqued());
EXPECT_FALSE((*sym).isUniqued());
EXPECT_TRUE(idTable.getStringView(runtime, *sym).equals(asdf));
}
}
TEST_F(IdentifierTableTest, LazyExternalSymbolTooBig) {
GCScope gcScope{runtime};
auto &idTable = runtime->getIdentifierTable();
const auto extSize = (1 << 24) +
std::max(kTestGCConfig.getMaxHeapSize(),
toRValue(StringPrimitive::EXTERNAL_STRING_THRESHOLD));
// A string of this size is definitely too big to be allocated.
ASSERT_FALSE(runtime->getHeap().canAllocExternalMemory(extSize));
std::string buf(extSize, '\0');
ASCIIRef ref{buf.data(), extSize};
SymbolID symbol = idTable.registerLazyIdentifier(ref);
EXPECT_DEATH_IF_SUPPORTED(
{ idTable.getStringPrim(runtime, symbol); },
"Unhandled out of memory exception");
}
// Verifies that SymbolIDs are allocated consecutively, increasing from zero, as
// long as none have been freed.
TEST_F(IdentifierTableTest, ConsecutiveIncreasingSymbolIDAlloc) {
IdentifierTable idTable;
// Backing store for StringRefs
std::vector<std::string> ascii;
std::vector<std::u16string> utf16;
for (size_t i = 0; i < 100; ++i) {
std::stringstream ssa;
ssa << "ascii-" << i;
ascii.emplace_back(ssa.str());
std::stringstream ssu;
ssu << "utf16-" << i;
auto abuf = ssu.str();
std::u16string buf;
convertUTF8WithSurrogatesToUTF16(
std::back_inserter(buf), abuf.data(), abuf.data() + abuf.size());
utf16.emplace_back(std::move(buf));
}
{ // Add refs to the ID Table. First time round, allocate all new IDs.
size_t idx = 0;
for (auto &s : ascii) {
auto r = createASCIIRef(s.c_str());
EXPECT_EQ(idTable.registerLazyIdentifier(r).unsafeGetIndex(), idx++)
<< "Uniqued ASCII First Round";
}
for (auto &s : utf16) {
auto r = createUTF16Ref(s.c_str());
EXPECT_EQ(idTable.registerLazyIdentifier(r).unsafeGetIndex(), idx++)
<< "Uniqued UTF16 First Round";
}
for (auto &s : ascii) {
auto r = createASCIIRef(s.c_str());
EXPECT_EQ(idTable.createNotUniquedLazySymbol(r).unsafeGetIndex(), idx++)
<< "Not Uniqued ASCII First Round";
}
}
{ // Next time around: The IDs should be the same as before for the uniqued
// SymbolIDs.
size_t idx = 0;
for (auto &s : ascii) {
auto r = createASCIIRef(s.c_str());
EXPECT_EQ(idTable.registerLazyIdentifier(r).unsafeGetIndex(), idx++)
<< "Uniqued ASCII Second Round";
}
for (auto &s : utf16) {
auto r = createUTF16Ref(s.c_str());
EXPECT_EQ(idTable.registerLazyIdentifier(r).unsafeGetIndex(), idx++)
<< "Uniqued UTF16 Second Round";
}
// These symbols are not uniqued so they are going to get re-allocated every
// time, although still linearly.
idx = idTable.getSymbolsEnd();
for (auto &s : ascii) {
auto r = createASCIIRef(s.c_str());
EXPECT_EQ(idTable.createNotUniquedLazySymbol(r).unsafeGetIndex(), idx++)
<< "Not Uniqued ASCII Second Round";
}
}
}
} // namespace