1 //===- MsgPackTypesTest.cpp -------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/BinaryFormat/MsgPackTypes.h"
10 #include "gtest/gtest.h"
13 using namespace msgpack
;
15 TEST(MsgPackTypes
, TestReadInt
) {
16 Reader
MPReader(StringRef("\xd0\x00", 2));
17 auto OptNodeOrErr
= Node::read(MPReader
);
18 ASSERT_TRUE(static_cast<bool>(OptNodeOrErr
));
19 ASSERT_TRUE(*OptNodeOrErr
);
20 auto *S
= dyn_cast
<ScalarNode
>((*OptNodeOrErr
)->get());
22 ASSERT_EQ(S
->getScalarKind(), ScalarNode::SK_Int
);
23 ASSERT_EQ(S
->getInt(), 0);
26 TEST(MsgPackTypes
, TestReadArray
) {
27 Reader
MPReader(StringRef("\x92\xd0\x01\xc0"));
28 auto OptNodeOrErr
= Node::read(MPReader
);
29 ASSERT_TRUE(static_cast<bool>(OptNodeOrErr
));
30 ASSERT_TRUE(*OptNodeOrErr
);
31 auto *A
= dyn_cast
<ArrayNode
>((*OptNodeOrErr
)->get());
33 ASSERT_EQ(A
->size(), 2u);
34 auto *SI
= dyn_cast
<ScalarNode
>((*A
)[0].get());
36 ASSERT_EQ(SI
->getScalarKind(), ScalarNode::SK_Int
);
37 ASSERT_EQ(SI
->getInt(), 1);
38 auto *SN
= dyn_cast
<ScalarNode
>((*A
)[1].get());
40 ASSERT_EQ(SN
->getScalarKind(), ScalarNode::SK_Nil
);
43 TEST(MsgPackTypes
, TestReadMap
) {
44 Reader
MPReader(StringRef("\x82\xa3"
49 auto OptNodeOrErr
= Node::read(MPReader
);
50 ASSERT_TRUE(static_cast<bool>(OptNodeOrErr
));
51 ASSERT_TRUE(*OptNodeOrErr
);
52 auto *A
= dyn_cast
<MapNode
>((*OptNodeOrErr
)->get());
54 ASSERT_EQ(A
->size(), 2u);
55 auto *FooS
= dyn_cast
<ScalarNode
>((*A
)["foo"].get());
57 ASSERT_EQ(FooS
->getScalarKind(), ScalarNode::SK_Int
);
58 ASSERT_EQ(FooS
->getInt(), 1);
59 auto *BarS
= dyn_cast
<ScalarNode
>((*A
)["bar"].get());
61 ASSERT_EQ(BarS
->getScalarKind(), ScalarNode::SK_Int
);
62 ASSERT_EQ(BarS
->getInt(), 2);
65 TEST(MsgPackTypes
, TestWriteInt
) {
67 raw_string_ostream
OStream(Buffer
);
68 Writer
MPWriter(OStream
);
69 ScalarNode
I(int64_t(1));
71 ASSERT_EQ(OStream
.str(), "\x01");
74 TEST(MsgPackTypes
, TestWriteArray
) {
76 raw_string_ostream
OStream(Buffer
);
77 Writer
MPWriter(OStream
);
79 A
.push_back(std::make_shared
<ScalarNode
>(int64_t(1)));
80 A
.push_back(std::make_shared
<ScalarNode
>());
82 ASSERT_EQ(OStream
.str(), "\x92\x01\xc0");
85 TEST(MsgPackTypes
, TestWriteMap
) {
87 raw_string_ostream
OStream(Buffer
);
88 Writer
MPWriter(OStream
);
90 M
["foo"] = std::make_shared
<ScalarNode
>(int64_t(1));
91 M
["bar"] = std::make_shared
<ScalarNode
>(int64_t(2));
93 ASSERT_EQ(OStream
.str(), "\x82\xa3"
100 TEST(MsgPackTypes
, TestOutputYAMLArray
) {
102 raw_string_ostream
OStream(Buffer
);
103 yaml::Output
yout(OStream
);
105 A
.push_back(std::make_shared
<ScalarNode
>(int64_t(1)));
106 A
.push_back(std::make_shared
<ScalarNode
>(int64_t(2)));
108 ASSERT_EQ(OStream
.str(), "---\n- !int 1\n- !int 2\n...\n");
111 TEST(MsgPackTypes
, TestInputYAMLArray
) {
113 yaml::Input
yin("---\n- !int 1\n- !str 2\n...\n");
115 auto *A
= dyn_cast
<ArrayNode
>(RootNode
.get());
117 ASSERT_EQ(A
->size(), 2u);
118 auto *SI
= dyn_cast
<ScalarNode
>((*A
)[0].get());
120 ASSERT_EQ(SI
->getScalarKind(), ScalarNode::SK_UInt
);
121 ASSERT_EQ(SI
->getUInt(), 1u);
122 auto *SS
= dyn_cast
<ScalarNode
>((*A
)[1].get());
124 ASSERT_EQ(SS
->getScalarKind(), ScalarNode::SK_String
);
125 ASSERT_EQ(SS
->getString(), "2");
128 TEST(MsgPackTypes
, TestOutputYAMLMap
) {
130 raw_string_ostream
OStream(Buffer
);
131 yaml::Output
yout(OStream
);
133 M
["foo"] = std::make_shared
<ScalarNode
>(int64_t(1));
134 M
["bar"] = std::make_shared
<ScalarNode
>(uint64_t(2));
135 auto N
= std::make_shared
<MapNode
>();
136 (*N
)["baz"] = std::make_shared
<ScalarNode
>(true);
137 M
["qux"] = std::move(N
);
139 ASSERT_EQ(OStream
.str(), "---\nfoo: !int 1\nbar: "
140 "!int 2\nqux: \n baz: "
141 "!bool true\n...\n");
144 TEST(MsgPackTypes
, TestInputYAMLMap
) {
146 yaml::Input
yin("---\nfoo: !int 1\nbaz: !str 2\n...\n");
148 auto *M
= dyn_cast
<MapNode
>(RootNode
.get());
150 ASSERT_EQ(M
->size(), 2u);
151 auto *SI
= dyn_cast
<ScalarNode
>((*M
)["foo"].get());
153 ASSERT_EQ(SI
->getScalarKind(), ScalarNode::SK_UInt
);
154 ASSERT_EQ(SI
->getUInt(), 1u);
155 auto *SS
= dyn_cast
<ScalarNode
>((*M
)["baz"].get());
157 ASSERT_EQ(SS
->getScalarKind(), ScalarNode::SK_String
);
158 ASSERT_EQ(SS
->getString(), "2");
161 // Test that the document is parsed into a tree of shared_ptr where each node
162 // can have multiple owners.
163 TEST(MsgPackTypes
, TestInputShared
) {
164 yaml::Input
yin("---\nfoo:\n bar: !int 1\n...\n");
172 auto *M
= dyn_cast
<MapNode
>(RootNode
.get());
174 ASSERT_EQ(M
->size(), 1u);
175 InnerMap
= (*M
)["foo"];
177 auto *N
= dyn_cast
<MapNode
>(InnerMap
.get());
179 ASSERT_EQ(N
->size(), 1u);
180 IntNode
= (*N
)["bar"];
182 auto *S
= dyn_cast
<ScalarNode
>(IntNode
.get());
184 ASSERT_EQ(S
->getScalarKind(), ScalarNode::SK_UInt
);
185 ASSERT_EQ(S
->getUInt(), 1u);