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
, TestReadInt
) {
18 bool Ok
= Doc
.readFromBlob(StringRef("\xd0\x00", 2), /*Multi=*/false);
20 ASSERT_EQ(Doc
.getRoot().getKind(), Type::Int
);
21 ASSERT_EQ(Doc
.getRoot().getInt(), 0);
24 TEST(MsgPackDocument
, TestReadArray
) {
26 bool Ok
= Doc
.readFromBlob(StringRef("\x92\xd0\x01\xc0"), /*Multi=*/false);
28 ASSERT_EQ(Doc
.getRoot().getKind(), Type::Array
);
29 auto A
= Doc
.getRoot().getArray();
30 ASSERT_EQ(A
.size(), 2u);
32 ASSERT_EQ(SI
.getKind(), Type::Int
);
33 ASSERT_EQ(SI
.getInt(), 1);
35 ASSERT_EQ(SN
.getKind(), Type::Nil
);
38 TEST(MsgPackDocument
, TestReadMap
) {
40 bool Ok
= Doc
.readFromBlob(StringRef("\x82\xa3"
47 ASSERT_EQ(Doc
.getRoot().getKind(), Type::Map
);
48 auto M
= Doc
.getRoot().getMap();
49 ASSERT_EQ(M
.size(), 2u);
51 ASSERT_EQ(FooS
.getKind(), Type::Int
);
52 ASSERT_EQ(FooS
.getInt(), 1);
54 ASSERT_EQ(BarS
.getKind(), Type::Int
);
55 ASSERT_EQ(BarS
.getInt(), 2);
58 TEST(MsgPackDocument
, TestWriteInt
) {
60 Doc
.getRoot() = Doc
.getNode(int64_t(1));
62 Doc
.writeToBlob(Buffer
);
63 ASSERT_EQ(Buffer
, "\x01");
66 TEST(MsgPackDocument
, TestWriteArray
) {
68 auto A
= Doc
.getRoot().getArray(/*Convert=*/true);
69 A
.push_back(Doc
.getNode(int64_t(1)));
70 A
.push_back(Doc
.getNode());
72 Doc
.writeToBlob(Buffer
);
73 ASSERT_EQ(Buffer
, "\x92\x01\xc0");
76 TEST(MsgPackDocument
, TestWriteMap
) {
78 auto M
= Doc
.getRoot().getMap(/*Convert=*/true);
79 M
["foo"] = Doc
.getNode(int64_t(1));
80 M
["bar"] = Doc
.getNode(int64_t(2));
82 Doc
.writeToBlob(Buffer
);
83 ASSERT_EQ(Buffer
, "\x82\xa3"
90 TEST(MsgPackDocument
, TestOutputYAMLArray
) {
92 auto A
= Doc
.getRoot().getArray(/*Convert=*/true);
93 A
.push_back(Doc
.getNode(int64_t(1)));
94 A
.push_back(Doc
.getNode(int64_t(2)));
96 raw_string_ostream
OStream(Buffer
);
98 ASSERT_EQ(OStream
.str(), "---\n- 1\n- 2\n...\n");
101 TEST(MsgPackDocument
, TestInputYAMLArray
) {
103 bool Ok
= Doc
.fromYAML("---\n- !int 0x1\n- !str 2\n...\n");
105 ASSERT_EQ(Doc
.getRoot().getKind(), Type::Array
);
106 auto A
= Doc
.getRoot().getArray();
107 ASSERT_EQ(A
.size(), 2u);
109 ASSERT_EQ(SI
.getKind(), Type::UInt
);
110 ASSERT_EQ(SI
.getUInt(), 1u);
112 ASSERT_EQ(SS
.getKind(), Type::String
);
113 ASSERT_EQ(SS
.getString(), "2");
116 TEST(MsgPackDocument
, TestOutputYAMLMap
) {
118 auto M
= Doc
.getRoot().getMap(/*Convert=*/true);
119 M
["foo"] = Doc
.getNode(int64_t(1));
120 M
["bar"] = Doc
.getNode(uint64_t(2));
121 auto N
= Doc
.getMapNode();
123 N
["baz"] = Doc
.getNode(true);
125 raw_string_ostream
OStream(Buffer
);
127 ASSERT_EQ(OStream
.str(), "---\n"
135 TEST(MsgPackDocument
, TestOutputYAMLMapHex
) {
138 auto M
= Doc
.getRoot().getMap(/*Convert=*/true);
139 M
["foo"] = Doc
.getNode(int64_t(1));
140 M
["bar"] = Doc
.getNode(uint64_t(2));
141 auto N
= Doc
.getMapNode();
143 N
["baz"] = Doc
.getNode(true);
145 raw_string_ostream
OStream(Buffer
);
147 ASSERT_EQ(OStream
.str(), "---\n"
155 TEST(MsgPackDocument
, TestInputYAMLMap
) {
157 bool Ok
= Doc
.fromYAML("---\nfoo: !int 0x1\nbaz: !str 2\n...\n");
159 ASSERT_EQ(Doc
.getRoot().getKind(), Type::Map
);
160 auto M
= Doc
.getRoot().getMap();
161 ASSERT_EQ(M
.size(), 2u);
163 ASSERT_EQ(SI
.getKind(), Type::UInt
);
164 ASSERT_EQ(SI
.getUInt(), 1u);
166 ASSERT_EQ(SS
.getKind(), Type::String
);
167 ASSERT_EQ(SS
.getString(), "2");