forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeakValueMapTest.cpp
More file actions
124 lines (97 loc) · 3.65 KB
/
Copy pathWeakValueMapTest.cpp
File metadata and controls
124 lines (97 loc) · 3.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
/*
* 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/WeakValueMap.h"
#include "hermes/VM/DummyObject.h"
#include "hermes/VM/PrimitiveBox.h"
#include "hermes/VM/Runtime.h"
#include "TestHelpers.h"
#include "gtest/gtest.h"
// This test relies on getHeapInfo, which is only enabled in debug builds.
#ifndef NDEBUG
using namespace hermes::vm;
namespace {
using WeakValueMapTest = LargeHeapRuntimeTestFixture;
using testhelpers::DummyObject;
TEST_F(WeakValueMapTest, SmokeTest) {
runtime->collect("test");
WeakValueMap<int, JSNumber> wvp{};
auto dummyObj = runtime->makeHandle(DummyObject::create(&runtime->getHeap()));
dummyObj->markWeakCallback = std::make_unique<DummyObject::MarkWeakCallback>(
[&wvp](GCCell *, WeakRefAcceptor &acceptor) {
wvp.markWeakRefs(acceptor);
});
auto makeNumber = [&](int n) -> JSNumber * {
return JSNumber::create(
runtime, (double)n, Runtime::makeNullHandle<JSObject>())
.get();
};
MutableHandle<JSNumber> h1{runtime};
MutableHandle<JSNumber> h2{runtime};
MutableHandle<JSNumber> h3{runtime};
auto marker = gcScope.createMarker();
h1 = makeNumber(1);
h2 = makeNumber(2);
h3 = makeNumber(3);
gcScope.flushToMarker(marker);
EXPECT_TRUE(wvp.insertNew(&runtime->getHeap(), 1, h1));
EXPECT_TRUE(wvp.insertNew(&runtime->getHeap(), 2, h2));
EXPECT_TRUE(wvp.insertNew(&runtime->getHeap(), 3, h3));
EXPECT_FALSE(wvp.insertNew(&runtime->getHeap(), 2, h2));
EXPECT_FALSE(wvp.insertNew(&runtime->getHeap(), 3, h3));
// Make sure enumaration covers all cases.
{
std::set<int> intset{};
wvp.forEachEntry(
[&intset](int key, const WeakRef<JSNumber> &) { intset.insert(key); });
ASSERT_EQ(intset.size(), 3);
}
// Validate erase. Erase 1 and 2.
ASSERT_TRUE(wvp.containsKey(1));
ASSERT_TRUE(wvp.containsKey(2));
wvp.erase(1, &runtime->getHeap());
ASSERT_FALSE(wvp.containsKey(1));
ASSERT_FALSE(wvp.erase(1, &runtime->getHeap()));
ASSERT_TRUE(wvp.erase(2, &runtime->getHeap()));
ASSERT_FALSE(wvp.containsKey(2));
// Add 1 and 2 again.
EXPECT_TRUE(wvp.insertNew(&runtime->getHeap(), 1, h1));
EXPECT_TRUE(wvp.insertNew(&runtime->getHeap(), 2, h2));
// Now make sure 1 gets garbage collected.
ASSERT_TRUE(wvp.containsKey(1));
h1.clear();
// Make sure no temporary handles exist.
gcScope.flushToMarker(marker);
runtime->collect("test");
#if !defined(HERMESVM_GC_HADES) && !defined(HERMESVM_GC_RUNTIME)
// Hades doesn't support DebugHeapInfo yet.
GCBase::DebugHeapInfo debugInfo;
runtime->getHeap().getDebugHeapInfo(debugInfo);
// We can't be sure how many cells precisely this will collect.
ASSERT_TRUE(
debugInfo.numCollectedObjects > 0 && debugInfo.numCollectedObjects <= 5);
#endif
// Make sure we can't find the collected value.
ASSERT_FALSE(wvp.containsKey(1));
// Now make sure 2 gets garbage collected, but check differently.
h2.clear();
// Make sure no temporary handles exist.
gcScope.flushToMarker(marker);
runtime->collect("test");
#if !defined(HERMESVM_GC_HADES) && !defined(HERMESVM_GC_RUNTIME)
// Hades doesn't support debugInfo yet.
runtime->getHeap().getDebugHeapInfo(debugInfo);
// We can't be sure how many cells precisely this will collect.
ASSERT_TRUE(
debugInfo.numCollectedObjects > 0 && debugInfo.numCollectedObjects <= 5);
#endif
ASSERT_TRUE(wvp.containsKey(3));
// Test lookup.
ASSERT_TRUE(wvp.lookup(runtime, &runtime->getHeap(), 3));
ASSERT_FALSE(wvp.lookup(runtime, &runtime->getHeap(), 300));
}
} // namespace
#endif // NDEBUG