Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / unittests / BinaryFormat / MsgPackTypesTest.cpp
blob84ca48a105c861b093f5ddf1d4a61f0c5f50a599
1 //===- MsgPackTypesTest.cpp -------------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/BinaryFormat/MsgPackTypes.h"
10 #include "gtest/gtest.h"
12 using namespace llvm;
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());
21 ASSERT_TRUE(S);
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());
32 ASSERT_TRUE(A);
33 ASSERT_EQ(A->size(), 2u);
34 auto *SI = dyn_cast<ScalarNode>((*A)[0].get());
35 ASSERT_TRUE(SI);
36 ASSERT_EQ(SI->getScalarKind(), ScalarNode::SK_Int);
37 ASSERT_EQ(SI->getInt(), 1);
38 auto *SN = dyn_cast<ScalarNode>((*A)[1].get());
39 ASSERT_TRUE(SN);
40 ASSERT_EQ(SN->getScalarKind(), ScalarNode::SK_Nil);
43 TEST(MsgPackTypes, TestReadMap) {
44 Reader MPReader(StringRef("\x82\xa3"
45 "foo"
46 "\xd0\x01\xa3"
47 "bar"
48 "\xd0\x02"));
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());
53 ASSERT_TRUE(A);
54 ASSERT_EQ(A->size(), 2u);
55 auto *FooS = dyn_cast<ScalarNode>((*A)["foo"].get());
56 ASSERT_TRUE(FooS);
57 ASSERT_EQ(FooS->getScalarKind(), ScalarNode::SK_Int);
58 ASSERT_EQ(FooS->getInt(), 1);
59 auto *BarS = dyn_cast<ScalarNode>((*A)["bar"].get());
60 ASSERT_TRUE(BarS);
61 ASSERT_EQ(BarS->getScalarKind(), ScalarNode::SK_Int);
62 ASSERT_EQ(BarS->getInt(), 2);
65 TEST(MsgPackTypes, TestWriteInt) {
66 std::string Buffer;
67 raw_string_ostream OStream(Buffer);
68 Writer MPWriter(OStream);
69 ScalarNode I(int64_t(1));
70 I.write(MPWriter);
71 ASSERT_EQ(OStream.str(), "\x01");
74 TEST(MsgPackTypes, TestWriteArray) {
75 std::string Buffer;
76 raw_string_ostream OStream(Buffer);
77 Writer MPWriter(OStream);
78 ArrayNode A;
79 A.push_back(std::make_shared<ScalarNode>(int64_t(1)));
80 A.push_back(std::make_shared<ScalarNode>());
81 A.write(MPWriter);
82 ASSERT_EQ(OStream.str(), "\x92\x01\xc0");
85 TEST(MsgPackTypes, TestWriteMap) {
86 std::string Buffer;
87 raw_string_ostream OStream(Buffer);
88 Writer MPWriter(OStream);
89 MapNode M;
90 M["foo"] = std::make_shared<ScalarNode>(int64_t(1));
91 M["bar"] = std::make_shared<ScalarNode>(int64_t(2));
92 M.write(MPWriter);
93 ASSERT_EQ(OStream.str(), "\x82\xa3"
94 "foo"
95 "\x01\xa3"
96 "bar"
97 "\x02");
100 TEST(MsgPackTypes, TestOutputYAMLArray) {
101 std::string Buffer;
102 raw_string_ostream OStream(Buffer);
103 yaml::Output yout(OStream);
104 ArrayNode A;
105 A.push_back(std::make_shared<ScalarNode>(int64_t(1)));
106 A.push_back(std::make_shared<ScalarNode>(int64_t(2)));
107 yout << A;
108 ASSERT_EQ(OStream.str(), "---\n- !int 1\n- !int 2\n...\n");
111 TEST(MsgPackTypes, TestInputYAMLArray) {
112 NodePtr RootNode;
113 yaml::Input yin("---\n- !int 1\n- !str 2\n...\n");
114 yin >> RootNode;
115 auto *A = dyn_cast<ArrayNode>(RootNode.get());
116 ASSERT_TRUE(A);
117 ASSERT_EQ(A->size(), 2u);
118 auto *SI = dyn_cast<ScalarNode>((*A)[0].get());
119 ASSERT_TRUE(SI);
120 ASSERT_EQ(SI->getScalarKind(), ScalarNode::SK_UInt);
121 ASSERT_EQ(SI->getUInt(), 1u);
122 auto *SS = dyn_cast<ScalarNode>((*A)[1].get());
123 ASSERT_TRUE(SS);
124 ASSERT_EQ(SS->getScalarKind(), ScalarNode::SK_String);
125 ASSERT_EQ(SS->getString(), "2");
128 TEST(MsgPackTypes, TestOutputYAMLMap) {
129 std::string Buffer;
130 raw_string_ostream OStream(Buffer);
131 yaml::Output yout(OStream);
132 MapNode M;
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);
138 yout << M;
139 ASSERT_EQ(OStream.str(), "---\nfoo: !int 1\nbar: "
140 "!int 2\nqux: \n baz: "
141 "!bool true\n...\n");
144 TEST(MsgPackTypes, TestInputYAMLMap) {
145 NodePtr RootNode;
146 yaml::Input yin("---\nfoo: !int 1\nbaz: !str 2\n...\n");
147 yin >> RootNode;
148 auto *M = dyn_cast<MapNode>(RootNode.get());
149 ASSERT_TRUE(M);
150 ASSERT_EQ(M->size(), 2u);
151 auto *SI = dyn_cast<ScalarNode>((*M)["foo"].get());
152 ASSERT_TRUE(SI);
153 ASSERT_EQ(SI->getScalarKind(), ScalarNode::SK_UInt);
154 ASSERT_EQ(SI->getUInt(), 1u);
155 auto *SS = dyn_cast<ScalarNode>((*M)["baz"].get());
156 ASSERT_TRUE(SS);
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");
165 NodePtr InnerMap;
166 NodePtr IntNode;
170 NodePtr RootNode;
171 yin >> RootNode;
172 auto *M = dyn_cast<MapNode>(RootNode.get());
173 ASSERT_TRUE(M);
174 ASSERT_EQ(M->size(), 1u);
175 InnerMap = (*M)["foo"];
177 auto *N = dyn_cast<MapNode>(InnerMap.get());
178 ASSERT_TRUE(N);
179 ASSERT_EQ(N->size(), 1u);
180 IntNode = (*N)["bar"];
182 auto *S = dyn_cast<ScalarNode>(IntNode.get());
183 ASSERT_TRUE(S);
184 ASSERT_EQ(S->getScalarKind(), ScalarNode::SK_UInt);
185 ASSERT_EQ(S->getUInt(), 1u);