forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictPropertyMapTest.cpp
More file actions
186 lines (156 loc) · 5.65 KB
/
DictPropertyMapTest.cpp
File metadata and controls
186 lines (156 loc) · 5.65 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
/*
* 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/DictPropertyMap.h"
#include "TestHelpers.h"
#include "gtest/gtest.h"
using namespace hermes::vm;
namespace {
using DictPropertyMapTest = LargeHeapRuntimeTestFixture;
TEST_F(DictPropertyMapTest, SmokeTest) {
auto id1 = SymbolID::unsafeCreate(1);
auto id2 = SymbolID::unsafeCreate(2);
auto id3 = SymbolID::unsafeCreate(3);
auto id4 = SymbolID::unsafeCreate(4);
NamedPropertyDescriptor desc1{};
auto res = DictPropertyMap::create(runtime, 2);
ASSERT_FALSE(isException(res));
MutableHandle<DictPropertyMap> map{runtime, res->get()};
auto saveMap = map.get();
// Try to find a property in the empty map.
ASSERT_FALSE(DictPropertyMap::find(map.get(), id1));
// Add prop1.
DictPropertyMap::add(map, runtime, id1, desc1);
ASSERT_EQ(1u, map->size());
auto found = DictPropertyMap::find(*map, id1);
ASSERT_TRUE(found);
ASSERT_EQ(id1, DictPropertyMap::getDescriptorPair(*map, *found)->first);
// Add prop2.
DictPropertyMap::add(map, runtime, id2, desc1);
ASSERT_EQ(2u, map->size());
// Find prop1, prop2.
found = DictPropertyMap::find(*map, id1);
ASSERT_TRUE(found);
ASSERT_EQ(id1, DictPropertyMap::getDescriptorPair(*map, *found)->first);
found = DictPropertyMap::find(*map, id2);
ASSERT_TRUE(found);
ASSERT_EQ(id2, DictPropertyMap::getDescriptorPair(*map, *found)->first);
// Make sure we haven't reallocated.
ASSERT_EQ(saveMap, map.get());
// Add a prop3, causing a reallocation.
DictPropertyMap::add(map, runtime, id3, desc1);
// Make sure we reallocated.
ASSERT_NE(saveMap, map.get());
saveMap = map.get();
for (unsigned i = 0; i < 3; ++i) {
auto sym = SymbolID::unsafeCreate(id1.unsafeGetIndex() + i);
found = DictPropertyMap::find(*map, sym);
ASSERT_TRUE(found);
ASSERT_EQ(sym, DictPropertyMap::getDescriptorPair(*map, *found)->first);
}
// Add a prop4.
DictPropertyMap::add(map, runtime, id4, desc1);
ASSERT_EQ(saveMap, map.get());
for (unsigned i = 0; i < 4; ++i) {
auto sym = SymbolID::unsafeCreate(id1.unsafeGetIndex() + i);
found = DictPropertyMap::find(*map, sym);
ASSERT_TRUE(found);
ASSERT_EQ(sym, DictPropertyMap::getDescriptorPair(*map, *found)->first);
}
// Verify the enumeration order.
{
unsigned symIndex = 1;
DictPropertyMap::forEachProperty(
map, runtime, [&](SymbolID id, NamedPropertyDescriptor desc) {
ASSERT_EQ(symIndex, id.unsafeGetIndex());
++symIndex;
});
}
// Delete prop2.
found = DictPropertyMap::find(*map, id2);
DictPropertyMap::erase(*map, runtime, *found);
ASSERT_EQ(saveMap, map.get());
ASSERT_EQ(3u, map->size());
{
static const unsigned symbols[] = {1, 3, 4};
for (unsigned i = 0; i < 3; ++i) {
auto sym = SymbolID::unsafeCreate(symbols[i]);
found = DictPropertyMap::find(*map, sym);
ASSERT_TRUE(found);
ASSERT_EQ(sym, DictPropertyMap::getDescriptorPair(*map, *found)->first);
}
unsigned i = 0;
DictPropertyMap::forEachProperty(
map, runtime, [&](SymbolID id, NamedPropertyDescriptor desc) {
ASSERT_EQ(symbols[i], id.unsafeGetIndex());
++i;
});
}
// Add prop2 again.
DictPropertyMap::add(map, runtime, id2, desc1);
ASSERT_EQ(4u, map->size());
{
static const unsigned symbols[] = {1, 3, 4, 2};
for (unsigned i = 0; i < 4; ++i) {
auto sym = SymbolID::unsafeCreate(symbols[i]);
found = DictPropertyMap::find(*map, sym);
ASSERT_TRUE(found);
ASSERT_EQ(sym, DictPropertyMap::getDescriptorPair(*map, *found)->first);
}
unsigned i = 0;
DictPropertyMap::forEachProperty(
map, runtime, [&](SymbolID id, NamedPropertyDescriptor desc) {
ASSERT_EQ(symbols[i], id.unsafeGetIndex());
++i;
});
}
}
TEST_F(DictPropertyMapTest, CreateOverCapacityTest) {
(void)DictPropertyMap::create(runtime);
ASSERT_EQ(
ExecutionStatus::EXCEPTION,
DictPropertyMap::create(runtime, DictPropertyMap::getMaxCapacity() + 1));
}
TEST_F(DictPropertyMapTest, GrowOverCapacityTest) {
// Hades can't handle doing a span of large allocations, because it has
// fragmentation in its heap space.
#if !defined(HERMESVM_GC_HADES) && !defined(HERMESVM_GC_RUNTIME)
// Don't do the test if it requires too many properties. Just cross our
// fingers and hope it works.
auto const maxCapacity = DictPropertyMap::getMaxCapacity();
if (maxCapacity > 500000)
return;
auto res = DictPropertyMap::create(runtime);
ASSERT_RETURNED(res);
MutableHandle<DictPropertyMap> map{runtime, res->get()};
MutableHandle<> value{runtime};
NamedPropertyDescriptor desc(PropertyFlags{}, 0);
auto marker = gcScope.createMarker();
for (unsigned i = 0; i < maxCapacity; ++i) {
value.set(HermesValue::encodeNumberValue(i));
auto symRes = valueToSymbolID(runtime, value);
ASSERT_RETURNED(symRes);
ASSERT_RETURNED(DictPropertyMap::add(map, runtime, **symRes, desc));
gcScope.flushToMarker(marker);
}
value.set(HermesValue::encodeNumberValue(maxCapacity));
auto symRes = valueToSymbolID(runtime, value);
ASSERT_RETURNED(symRes);
ASSERT_EQ(
ExecutionStatus::EXCEPTION,
DictPropertyMap::add(map, runtime, **symRes, desc));
runtime->clearThrownValue();
// Try it again.
value.set(HermesValue::encodeNumberValue(maxCapacity + 1));
symRes = valueToSymbolID(runtime, value);
ASSERT_RETURNED(symRes);
ASSERT_EQ(
ExecutionStatus::EXCEPTION,
DictPropertyMap::add(map, runtime, **symRes, desc));
runtime->clearThrownValue();
#endif
}
} // namespace