1 //===- MsgPackDocumentTest.cpp --------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/BinaryFormat/MsgPackDocument.h"
11 #include "gtest/gtest.h"
14 using namespace msgpack
;
16 TEST(MsgPackDocument
, DocNodeTest
) {
19 DocNode Int1
= Doc
.getNode(1), Int2
= Doc
.getNode(2);
20 DocNode Str1
= Doc
.getNode("ab"), Str2
= Doc
.getNode("ab");
22 ASSERT_TRUE(Int1
!= Int2
);
23 ASSERT_TRUE(Str1
== Str2
);
26 TEST(MsgPackDocument
, TestReadInt
) {
28 bool Ok
= Doc
.readFromBlob(StringRef("\xd0\x00", 2), /*Multi=*/false);
30 ASSERT_EQ(Doc
.getRoot().getKind(), Type::Int
);
31 ASSERT_EQ(Doc
.getRoot().getInt(), 0);
34 TEST(MsgPackDocument
, TestReadMergeArray
) {
36 bool Ok
= Doc
.readFromBlob(StringRef("\x92\xd0\x01\xc0"), /*Multi=*/false);
38 ASSERT_EQ(Doc
.getRoot().getKind(), Type::Array
);
39 auto A
= Doc
.getRoot().getArray();
40 ASSERT_EQ(A
.size(), 2u);
42 ASSERT_EQ(SI
.getKind(), Type::Int
);
43 ASSERT_EQ(SI
.getInt(), 1);
45 ASSERT_EQ(SN
.getKind(), Type::Nil
);
47 Ok
= Doc
.readFromBlob(StringRef("\x91\xd0\x2a"), /*Multi=*/false,
48 [](DocNode
*DestNode
, DocNode SrcNode
, DocNode MapKey
) {
49 // Allow array, merging into existing elements, ORing
51 if (DestNode
->getKind() == Type::Int
&&
52 SrcNode
.getKind() == Type::Int
) {
53 *DestNode
= DestNode
->getDocument()->getNode(
54 DestNode
->getInt() | SrcNode
.getInt());
57 return DestNode
->isArray() && SrcNode
.isArray() ? 0
61 A
= Doc
.getRoot().getArray();
62 ASSERT_EQ(A
.size(), 2u);
64 ASSERT_EQ(SI
.getKind(), Type::Int
);
65 ASSERT_EQ(SI
.getInt(), 43);
67 ASSERT_EQ(SN
.getKind(), Type::Nil
);
70 TEST(MsgPackDocument
, TestReadAppendArray
) {
72 bool Ok
= Doc
.readFromBlob(StringRef("\x92\xd0\x01\xc0"), /*Multi=*/false);
74 ASSERT_EQ(Doc
.getRoot().getKind(), Type::Array
);
75 auto A
= Doc
.getRoot().getArray();
76 ASSERT_EQ(A
.size(), 2u);
78 ASSERT_EQ(SI
.getKind(), Type::Int
);
79 ASSERT_EQ(SI
.getInt(), 1);
81 ASSERT_EQ(SN
.getKind(), Type::Nil
);
83 Ok
= Doc
.readFromBlob(StringRef("\x91\xd0\x2a"), /*Multi=*/false,
84 [](DocNode
*DestNode
, DocNode SrcNode
, DocNode MapKey
) {
85 // Allow array, appending after existing elements
86 return DestNode
->isArray() && SrcNode
.isArray()
87 ? DestNode
->getArray().size()
91 A
= Doc
.getRoot().getArray();
92 ASSERT_EQ(A
.size(), 3u);
94 ASSERT_EQ(SI
.getKind(), Type::Int
);
95 ASSERT_EQ(SI
.getInt(), 1);
97 ASSERT_EQ(SN
.getKind(), Type::Nil
);
99 ASSERT_EQ(SI
.getKind(), Type::Int
);
100 ASSERT_EQ(SI
.getInt(), 42);
103 TEST(MsgPackDocument
, TestReadMergeMap
) {
105 bool Ok
= Doc
.readFromBlob(StringRef("\x82\xa3"
112 ASSERT_EQ(Doc
.getRoot().getKind(), Type::Map
);
113 auto M
= Doc
.getRoot().getMap();
114 ASSERT_EQ(M
.size(), 2u);
115 auto FooS
= M
["foo"];
116 ASSERT_EQ(FooS
.getKind(), Type::Int
);
117 ASSERT_EQ(FooS
.getInt(), 1);
118 auto BarS
= M
["bar"];
119 ASSERT_EQ(BarS
.getKind(), Type::Int
);
120 ASSERT_EQ(BarS
.getInt(), 2);
122 Ok
= Doc
.readFromBlob(StringRef("\x82\xa3"
128 [](DocNode
*DestNode
, DocNode SrcNode
, DocNode MapKey
) {
129 return DestNode
->isMap() && SrcNode
.isMap() ? 0 : -1;
132 ASSERT_EQ(M
.size(), 4u);
134 ASSERT_EQ(FooS
.getKind(), Type::Int
);
135 ASSERT_EQ(FooS
.getInt(), 1);
137 ASSERT_EQ(BarS
.getKind(), Type::Int
);
138 ASSERT_EQ(BarS
.getInt(), 2);
139 auto FozS
= M
["foz"];
140 ASSERT_EQ(FozS
.getKind(), Type::Int
);
141 ASSERT_EQ(FozS
.getInt(), 3);
142 auto BazS
= M
["baz"];
143 ASSERT_EQ(BazS
.getKind(), Type::Int
);
144 ASSERT_EQ(BazS
.getInt(), 4);
146 Ok
= Doc
.readFromBlob(
152 /*Multi=*/false, [](DocNode
*Dest
, DocNode Src
, DocNode MapKey
) {
153 // Merger function that merges two ints by ORing their values, as long
154 // as the map key is "foz".
156 return Dest
->isMap();
158 return Dest
->isArray();
159 if (MapKey
.isString() && MapKey
.getString() == "foz" &&
160 Dest
->getKind() == Type::Int
&& Src
.getKind() == Type::Int
) {
161 *Dest
= Src
.getDocument()->getNode(Dest
->getInt() | Src
.getInt());
167 ASSERT_EQ(M
.size(), 5u);
169 ASSERT_EQ(FooS
.getKind(), Type::Int
);
170 ASSERT_EQ(FooS
.getInt(), 1);
172 ASSERT_EQ(BarS
.getKind(), Type::Int
);
173 ASSERT_EQ(BarS
.getInt(), 2);
175 ASSERT_EQ(FozS
.getKind(), Type::Int
);
176 ASSERT_EQ(FozS
.getInt(), 7);
178 ASSERT_EQ(BazS
.getKind(), Type::Int
);
179 ASSERT_EQ(BazS
.getInt(), 4);
180 auto BayS
= M
["bay"];
181 ASSERT_EQ(BayS
.getKind(), Type::Int
);
182 ASSERT_EQ(BayS
.getInt(), 8);
185 TEST(MsgPackDocument
, TestWriteInt
) {
189 Doc
.writeToBlob(Buffer
);
190 ASSERT_EQ(Buffer
, "\x01");
193 TEST(MsgPackDocument
, TestWriteArray
) {
195 auto A
= Doc
.getRoot().getArray(/*Convert=*/true);
196 A
.push_back(Doc
.getNode(int64_t(1)));
197 A
.push_back(Doc
.getNode());
199 Doc
.writeToBlob(Buffer
);
200 ASSERT_EQ(Buffer
, "\x92\x01\xc0");
203 TEST(MsgPackDocument
, TestWriteMap
) {
205 auto M
= Doc
.getRoot().getMap(/*Convert=*/true);
209 Doc
.writeToBlob(Buffer
);
210 ASSERT_EQ(Buffer
, "\x82\xa3"
217 TEST(MsgPackDocument
, TestOutputYAMLArray
) {
219 auto A
= Doc
.getRoot().getArray(/*Convert=*/true);
220 A
.push_back(Doc
.getNode(int64_t(1)));
221 A
.push_back(Doc
.getNode(int64_t(2)));
223 raw_string_ostream
OStream(Buffer
);
225 ASSERT_EQ(OStream
.str(), "---\n- 1\n- 2\n...\n");
228 TEST(MsgPackDocument
, TestInputYAMLArray
) {
230 bool Ok
= Doc
.fromYAML("---\n- !int 0x1\n- !str 2\n...\n");
232 ASSERT_EQ(Doc
.getRoot().getKind(), Type::Array
);
233 auto A
= Doc
.getRoot().getArray();
234 ASSERT_EQ(A
.size(), 2u);
236 ASSERT_EQ(SI
.getKind(), Type::UInt
);
237 ASSERT_EQ(SI
.getUInt(), 1u);
239 ASSERT_EQ(SS
.getKind(), Type::String
);
240 ASSERT_EQ(SS
.getString(), "2");
243 TEST(MsgPackDocument
, TestOutputYAMLMap
) {
245 auto M
= Doc
.getRoot().getMap(/*Convert=*/true);
248 auto N
= Doc
.getMapNode();
252 raw_string_ostream
OStream(Buffer
);
254 ASSERT_EQ(OStream
.str(), "---\n"
262 TEST(MsgPackDocument
, TestOutputYAMLMapWithErase
) {
264 auto M
= Doc
.getRoot().getMap(/*Convert=*/true);
267 auto N
= Doc
.getMapNode();
270 M
.erase(Doc
.getNode("bar"));
272 raw_string_ostream
OStream(Buffer
);
274 ASSERT_EQ(OStream
.str(), "---\n"
281 TEST(MsgPackDocument
, TestOutputYAMLMapHex
) {
284 auto M
= Doc
.getRoot().getMap(/*Convert=*/true);
287 auto N
= Doc
.getMapNode();
291 raw_string_ostream
OStream(Buffer
);
293 ASSERT_EQ(OStream
.str(), "---\n"
301 TEST(MsgPackDocument
, TestInputYAMLMap
) {
303 bool Ok
= Doc
.fromYAML("---\nfoo: !int 0x1\nbaz: !str 2\n...\n");
305 ASSERT_EQ(Doc
.getRoot().getKind(), Type::Map
);
306 auto M
= Doc
.getRoot().getMap();
307 ASSERT_EQ(M
.size(), 2u);
309 ASSERT_EQ(SI
.getKind(), Type::UInt
);
310 ASSERT_EQ(SI
.getUInt(), 1u);
312 ASSERT_EQ(SS
.getKind(), Type::String
);
313 ASSERT_EQ(SS
.getString(), "2");