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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
//===- MsgPackDocumentTest.cpp --------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/BinaryFormat/MsgPackDocument.h"
#include "gtest/gtest.h"
using namespace llvm;
using namespace msgpack;
TEST(MsgPackDocument, DocNodeTest) {
Document Doc;
DocNode Int1 = Doc.getNode(1), Int2 = Doc.getNode(2);
DocNode Str1 = Doc.getNode("ab"), Str2 = Doc.getNode("ab");
ASSERT_TRUE(Int1 != Int2);
ASSERT_TRUE(Str1 == Str2);
}
TEST(MsgPackDocument, TestReadInt) {
Document Doc;
bool Ok = Doc.readFromBlob(StringRef("\xd0\x00", 2), /*Multi=*/false);
ASSERT_TRUE(Ok);
ASSERT_EQ(Doc.getRoot().getKind(), Type::Int);
ASSERT_EQ(Doc.getRoot().getInt(), 0);
}
TEST(MsgPackDocument, TestReadMergeArray) {
Document Doc;
bool Ok = Doc.readFromBlob(StringRef("\x92\xd0\x01\xc0"), /*Multi=*/false);
ASSERT_TRUE(Ok);
ASSERT_EQ(Doc.getRoot().getKind(), Type::Array);
auto A = Doc.getRoot().getArray();
ASSERT_EQ(A.size(), 2u);
auto SI = A[0];
ASSERT_EQ(SI.getKind(), Type::Int);
ASSERT_EQ(SI.getInt(), 1);
auto SN = A[1];
ASSERT_EQ(SN.getKind(), Type::Nil);
Ok = Doc.readFromBlob(StringRef("\x91\xd0\x2a"), /*Multi=*/false,
[](DocNode *DestNode, DocNode SrcNode, DocNode MapKey) {
// Allow array, merging into existing elements, ORing
// ints.
if (DestNode->getKind() == Type::Int &&
SrcNode.getKind() == Type::Int) {
*DestNode = DestNode->getDocument()->getNode(
DestNode->getInt() | SrcNode.getInt());
return 0;
}
return DestNode->isArray() && SrcNode.isArray() ? 0
: -1;
});
ASSERT_TRUE(Ok);
A = Doc.getRoot().getArray();
ASSERT_EQ(A.size(), 2u);
SI = A[0];
ASSERT_EQ(SI.getKind(), Type::Int);
ASSERT_EQ(SI.getInt(), 43);
SN = A[1];
ASSERT_EQ(SN.getKind(), Type::Nil);
}
TEST(MsgPackDocument, TestReadAppendArray) {
Document Doc;
bool Ok = Doc.readFromBlob(StringRef("\x92\xd0\x01\xc0"), /*Multi=*/false);
ASSERT_TRUE(Ok);
ASSERT_EQ(Doc.getRoot().getKind(), Type::Array);
auto A = Doc.getRoot().getArray();
ASSERT_EQ(A.size(), 2u);
auto SI = A[0];
ASSERT_EQ(SI.getKind(), Type::Int);
ASSERT_EQ(SI.getInt(), 1);
auto SN = A[1];
ASSERT_EQ(SN.getKind(), Type::Nil);
Ok = Doc.readFromBlob(StringRef("\x91\xd0\x2a"), /*Multi=*/false,
[](DocNode *DestNode, DocNode SrcNode, DocNode MapKey) {
// Allow array, appending after existing elements
return DestNode->isArray() && SrcNode.isArray()
? DestNode->getArray().size()
: -1;
});
ASSERT_TRUE(Ok);
A = Doc.getRoot().getArray();
ASSERT_EQ(A.size(), 3u);
SI = A[0];
ASSERT_EQ(SI.getKind(), Type::Int);
ASSERT_EQ(SI.getInt(), 1);
SN = A[1];
ASSERT_EQ(SN.getKind(), Type::Nil);
SI = A[2];
ASSERT_EQ(SI.getKind(), Type::Int);
ASSERT_EQ(SI.getInt(), 42);
}
TEST(MsgPackDocument, TestReadMergeMap) {
Document Doc;
bool Ok = Doc.readFromBlob(StringRef("\x82\xa3"
"foo"
"\xd0\x01\xa3"
"bar"
"\xd0\x02"),
/*Multi=*/false);
ASSERT_TRUE(Ok);
ASSERT_EQ(Doc.getRoot().getKind(), Type::Map);
auto M = Doc.getRoot().getMap();
ASSERT_EQ(M.size(), 2u);
auto FooS = M["foo"];
ASSERT_EQ(FooS.getKind(), Type::Int);
ASSERT_EQ(FooS.getInt(), 1);
auto BarS = M["bar"];
ASSERT_EQ(BarS.getKind(), Type::Int);
ASSERT_EQ(BarS.getInt(), 2);
Ok = Doc.readFromBlob(StringRef("\x82\xa3"
"foz"
"\xd0\x03\xa3"
"baz"
"\xd0\x04"),
/*Multi=*/false,
[](DocNode *DestNode, DocNode SrcNode, DocNode MapKey) {
return DestNode->isMap() && SrcNode.isMap() ? 0 : -1;
});
ASSERT_TRUE(Ok);
ASSERT_EQ(M.size(), 4u);
FooS = M["foo"];
ASSERT_EQ(FooS.getKind(), Type::Int);
ASSERT_EQ(FooS.getInt(), 1);
BarS = M["bar"];
ASSERT_EQ(BarS.getKind(), Type::Int);
ASSERT_EQ(BarS.getInt(), 2);
auto FozS = M["foz"];
ASSERT_EQ(FozS.getKind(), Type::Int);
ASSERT_EQ(FozS.getInt(), 3);
auto BazS = M["baz"];
ASSERT_EQ(BazS.getKind(), Type::Int);
ASSERT_EQ(BazS.getInt(), 4);
Ok = Doc.readFromBlob(
StringRef("\x82\xa3"
"foz"
"\xd0\x06\xa3"
"bay"
"\xd0\x08"),
/*Multi=*/false, [](DocNode *Dest, DocNode Src, DocNode MapKey) {
// Merger function that merges two ints by ORing their values, as long
// as the map key is "foz".
if (Src.isMap())
return Dest->isMap();
if (Src.isArray())
return Dest->isArray();
if (MapKey.isString() && MapKey.getString() == "foz" &&
Dest->getKind() == Type::Int && Src.getKind() == Type::Int) {
*Dest = Src.getDocument()->getNode(Dest->getInt() | Src.getInt());
return true;
}
return false;
});
ASSERT_TRUE(Ok);
ASSERT_EQ(M.size(), 5u);
FooS = M["foo"];
ASSERT_EQ(FooS.getKind(), Type::Int);
ASSERT_EQ(FooS.getInt(), 1);
BarS = M["bar"];
ASSERT_EQ(BarS.getKind(), Type::Int);
ASSERT_EQ(BarS.getInt(), 2);
FozS = M["foz"];
ASSERT_EQ(FozS.getKind(), Type::Int);
ASSERT_EQ(FozS.getInt(), 7);
BazS = M["baz"];
ASSERT_EQ(BazS.getKind(), Type::Int);
ASSERT_EQ(BazS.getInt(), 4);
auto BayS = M["bay"];
ASSERT_EQ(BayS.getKind(), Type::Int);
ASSERT_EQ(BayS.getInt(), 8);
}
TEST(MsgPackDocument, TestWriteInt) {
Document Doc;
Doc.getRoot() = 1;
std::string Buffer;
Doc.writeToBlob(Buffer);
ASSERT_EQ(Buffer, "\x01");
}
TEST(MsgPackDocument, TestWriteArray) {
Document Doc;
auto A = Doc.getRoot().getArray(/*Convert=*/true);
A.push_back(Doc.getNode(int64_t(1)));
A.push_back(Doc.getNode());
std::string Buffer;
Doc.writeToBlob(Buffer);
ASSERT_EQ(Buffer, "\x92\x01\xc0");
}
TEST(MsgPackDocument, TestWriteMap) {
Document Doc;
auto M = Doc.getRoot().getMap(/*Convert=*/true);
M["foo"] = 1;
M["bar"] = 2;
std::string Buffer;
Doc.writeToBlob(Buffer);
ASSERT_EQ(Buffer, "\x82\xa3"
"bar"
"\x02\xa3"
"foo"
"\x01");
}
TEST(MsgPackDocument, TestOutputYAMLArray) {
Document Doc;
auto A = Doc.getRoot().getArray(/*Convert=*/true);
A.push_back(Doc.getNode(int64_t(1)));
A.push_back(Doc.getNode(int64_t(2)));
std::string Buffer;
raw_string_ostream OStream(Buffer);
Doc.toYAML(OStream);
ASSERT_EQ(OStream.str(), "---\n- 1\n- 2\n...\n");
}
TEST(MsgPackDocument, TestInputYAMLArray) {
Document Doc;
bool Ok = Doc.fromYAML("---\n- !int 0x1\n- !str 2\n...\n");
ASSERT_TRUE(Ok);
ASSERT_EQ(Doc.getRoot().getKind(), Type::Array);
auto A = Doc.getRoot().getArray();
ASSERT_EQ(A.size(), 2u);
auto SI = A[0];
ASSERT_EQ(SI.getKind(), Type::UInt);
ASSERT_EQ(SI.getUInt(), 1u);
auto SS = A[1];
ASSERT_EQ(SS.getKind(), Type::String);
ASSERT_EQ(SS.getString(), "2");
}
TEST(MsgPackDocument, TestOutputYAMLMap) {
Document Doc;
auto M = Doc.getRoot().getMap(/*Convert=*/true);
M["foo"] = 1;
M["bar"] = 2U;
auto N = Doc.getMapNode();
M["qux"] = N;
N["baz"] = true;
std::string Buffer;
raw_string_ostream OStream(Buffer);
Doc.toYAML(OStream);
ASSERT_EQ(OStream.str(), "---\n"
"bar: 2\n"
"foo: 1\n"
"qux:\n"
" baz: true\n"
"...\n");
}
TEST(MsgPackDocument, TestOutputYAMLMapWithErase) {
Document Doc;
auto M = Doc.getRoot().getMap(/*Convert=*/true);
M["foo"] = 1;
M["bar"] = 2U;
auto N = Doc.getMapNode();
M["qux"] = N;
N["baz"] = true;
M.erase(Doc.getNode("bar"));
std::string Buffer;
raw_string_ostream OStream(Buffer);
Doc.toYAML(OStream);
ASSERT_EQ(OStream.str(), "---\n"
"foo: 1\n"
"qux:\n"
" baz: true\n"
"...\n");
}
TEST(MsgPackDocument, TestOutputYAMLMapHex) {
Document Doc;
Doc.setHexMode();
auto M = Doc.getRoot().getMap(/*Convert=*/true);
M["foo"] = 1;
M["bar"] = 2U;
auto N = Doc.getMapNode();
M["qux"] = N;
N["baz"] = true;
std::string Buffer;
raw_string_ostream OStream(Buffer);
Doc.toYAML(OStream);
ASSERT_EQ(OStream.str(), "---\n"
"bar: 0x2\n"
"foo: 1\n"
"qux:\n"
" baz: true\n"
"...\n");
}
TEST(MsgPackDocument, TestInputYAMLMap) {
Document Doc;
bool Ok = Doc.fromYAML("---\nfoo: !int 0x1\nbaz: !str 2\n...\n");
ASSERT_TRUE(Ok);
ASSERT_EQ(Doc.getRoot().getKind(), Type::Map);
auto M = Doc.getRoot().getMap();
ASSERT_EQ(M.size(), 2u);
auto SI = M["foo"];
ASSERT_EQ(SI.getKind(), Type::UInt);
ASSERT_EQ(SI.getUInt(), 1u);
auto SS = M["baz"];
ASSERT_EQ(SS.getKind(), Type::String);
ASSERT_EQ(SS.getString(), "2");
}
|