forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializerTest.cpp
More file actions
139 lines (111 loc) · 3.42 KB
/
SerializerTest.cpp
File metadata and controls
139 lines (111 loc) · 3.42 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
/*
* 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.
*/
#ifdef HERMESVM_SERIALIZE
#include "hermes/VM/Serializer.h"
#include "hermes/VM/Deserializer.h"
#include "TestHelpers.h"
#include "gtest/gtest.h"
using namespace hermes::vm;
namespace {
using SerializerTest = LargeHeapRuntimeTestFixture;
class Node {
public:
using TestFunction = uint32_t(uint32_t);
uint32_t value_;
Node *next_; // Field used to test NativePointer relocation.
PinnedHermesValue hvNext_; // Field used to test HermesValue Relocation.
TestFunction *function_;
explicit Node(
uint32_t value = 0,
Node *next = nullptr,
TestFunction *function = nullptr)
: value_(value), next_(next), function_(function) {}
void serialize(Serializer &s) {
s.writeInt<uint32_t>(value_);
s.writeRelocation(next_);
s.writeHermesValue(hvNext_);
s.writeRelocation((void *)function_);
s.endObject(this);
}
static Node *deserialize(Deserializer &d) {
Node *obj = new Node();
obj->value_ = d.readInt<uint32_t>();
d.readRelocation(&obj->next_, RelocationKind::NativePointer);
d.readHermesValue(&obj->hvNext_);
d.readRelocation(&obj->function_, RelocationKind::NativePointer);
d.endObject(obj);
return obj;
}
};
uint32_t testFunction1(uint32_t i) {
return i;
}
uint32_t testFunction2(uint32_t i) {
return i * 2;
}
uint32_t testFunction3(uint32_t i) {
return i * 3;
}
/// Gather function pointers of native functions and put them in \p vec.
static std::vector<void *> testExternalPtrs() {
std::vector<void *> res;
res.push_back((void *)testFunction1);
res.push_back((void *)testFunction2);
res.push_back((void *)testFunction3);
return res;
}
TEST_F(SerializerTest, SerializeDeserializeTest) {
Node n0(0);
Node n1(2);
Node n2(1);
Node n3(3);
n0.next_ = nullptr;
n1.next_ = &n2;
n2.next_ = &n3;
n3.next_ = &n1;
n0.hvNext_ = HermesValue::encodeObjectValue(nullptr);
n1.hvNext_ = HermesValue::encodeObjectValue(&n2);
n2.hvNext_ = HermesValue::encodeObjectValue(&n3);
n3.hvNext_ = HermesValue::encodeObjectValue(&n1);
// Also test external pointers mapping here.
n1.function_ = testFunction1;
n2.function_ = testFunction2;
n3.function_ = testFunction3;
// Now serialize.
std::string str;
llvh::raw_string_ostream os(str);
Serializer s(os, runtime, testExternalPtrs);
n0.serialize(s);
n1.serialize(s);
n2.serialize(s);
n3.serialize(s);
s.writeEpilogue();
Deserializer d(
llvh::MemoryBuffer::getMemBuffer(os.str()), runtime, testExternalPtrs);
Node *n4 = Node::deserialize(d);
Node *n5 = Node::deserialize(d);
Node *n6 = Node::deserialize(d);
Node *n7 = Node::deserialize(d);
d.flushRelocationQueue();
ASSERT_EQ(0u, n4->value_);
ASSERT_EQ(2u, n5->value_);
ASSERT_EQ(1u, n6->value_);
ASSERT_EQ(3u, n7->value_);
ASSERT_EQ(nullptr, n4->next_);
ASSERT_EQ(n6, n5->next_);
ASSERT_EQ(n7, n6->next_);
ASSERT_EQ(n5, n7->next_);
ASSERT_EQ(nullptr, n4->hvNext_.getObject());
ASSERT_EQ(n6, n5->hvNext_.getObject());
ASSERT_EQ(n7, n6->hvNext_.getObject());
ASSERT_EQ(n5, n7->hvNext_.getObject());
ASSERT_EQ(n5->function_(n5->value_), testFunction1(n1.value_));
ASSERT_EQ(n6->function_(n6->value_), testFunction2(n2.value_));
ASSERT_EQ(n7->function_(n7->value_), testFunction3(n3.value_));
}
} // namespace
#endif