Skip to content

Commit 97d5d48

Browse files
perlfuxazhangAMD
andauthored
[MsgPack] Add code for floating point assignment and writes (#153544)
Allow assignment of float to DocType and support output of float in writeToBlob method. Expand tests coverage to various missing basic I/O operations. Co-authored-by: Xavi Zhang <[email protected]>
1 parent d42a1d4 commit 97d5d48

File tree

3 files changed

+100
-4
lines changed

3 files changed

+100
-4
lines changed

llvm/include/llvm/BinaryFormat/MsgPackDocument.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ class DocNode {
213213
LLVM_ABI DocNode &operator=(unsigned Val);
214214
LLVM_ABI DocNode &operator=(int64_t Val);
215215
LLVM_ABI DocNode &operator=(uint64_t Val);
216+
LLVM_ABI DocNode &operator=(double Val);
216217

217218
private:
218219
// Private constructor setting KindAndDoc, used by methods in Document.

llvm/lib/BinaryFormat/MsgPackDocument.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ DocNode &DocNode::operator=(uint64_t Val) {
104104
*this = getDocument()->getNode(Val);
105105
return *this;
106106
}
107+
DocNode &DocNode::operator=(double Val) {
108+
*this = getDocument()->getNode(Val);
109+
return *this;
110+
}
107111

108112
// A level in the document reading stack.
109113
struct StackLevel {
@@ -293,6 +297,9 @@ void Document::writeToBlob(std::string &Blob) {
293297
case Type::Binary:
294298
MPWriter.write(Node.getBinary());
295299
break;
300+
case Type::Float:
301+
MPWriter.write(Node.getFloat());
302+
break;
296303
case Type::Empty:
297304
llvm_unreachable("unhandled empty msgpack node");
298305
default:

llvm/unittests/BinaryFormat/MsgPackDocumentTest.cpp

Lines changed: 92 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,58 @@ TEST(MsgPackDocument, DocNodeTest) {
2222
ASSERT_TRUE(Str1 == Str2);
2323
}
2424

25+
TEST(MsgPackDocument, TestReadBoolean) {
26+
Document Doc1;
27+
bool Ok = Doc1.readFromBlob(StringRef("\xC2", 1), /*Multi=*/false);
28+
ASSERT_TRUE(Ok);
29+
ASSERT_EQ(Doc1.getRoot().getKind(), Type::Boolean);
30+
ASSERT_EQ(Doc1.getRoot().getBool(), false);
31+
Document Doc2;
32+
Ok = Doc2.readFromBlob(StringRef("\xC3", 1), /*Multi=*/false);
33+
ASSERT_TRUE(Ok);
34+
ASSERT_EQ(Doc2.getRoot().getKind(), Type::Boolean);
35+
ASSERT_EQ(Doc2.getRoot().getBool(), true);
36+
}
37+
2538
TEST(MsgPackDocument, TestReadInt) {
26-
Document Doc;
27-
bool Ok = Doc.readFromBlob(StringRef("\xd0\x00", 2), /*Multi=*/false);
39+
Document Doc1;
40+
bool Ok = Doc1.readFromBlob(StringRef("\xD0\x00", 2), /*Multi=*/false);
41+
ASSERT_TRUE(Ok);
42+
ASSERT_EQ(Doc1.getRoot().getKind(), Type::Int);
43+
ASSERT_EQ(Doc1.getRoot().getInt(), 0);
44+
Document Doc2;
45+
Ok = Doc2.readFromBlob(StringRef("\xFF", 1), /*Multi=*/false);
46+
ASSERT_TRUE(Ok);
47+
ASSERT_EQ(Doc2.getRoot().getKind(), Type::Int);
48+
ASSERT_EQ(Doc2.getRoot().getInt(), -1);
49+
}
50+
51+
TEST(MsgPackDocument, TestReadUInt) {
52+
Document Doc1;
53+
bool Ok = Doc1.readFromBlob(StringRef("\xCC\x00", 2), /*Multi=*/false);
54+
ASSERT_TRUE(Ok);
55+
ASSERT_EQ(Doc1.getRoot().getKind(), Type::UInt);
56+
ASSERT_EQ(Doc1.getRoot().getUInt(), 0U);
57+
Document Doc2;
58+
Ok = Doc2.readFromBlob(StringRef("\x01", 1), /*Multi=*/false);
2859
ASSERT_TRUE(Ok);
29-
ASSERT_EQ(Doc.getRoot().getKind(), Type::Int);
30-
ASSERT_EQ(Doc.getRoot().getInt(), 0);
60+
ASSERT_EQ(Doc2.getRoot().getKind(), Type::UInt);
61+
ASSERT_EQ(Doc2.getRoot().getUInt(), 1U);
62+
}
63+
64+
TEST(MsgPackDocument, TestReadFloat) {
65+
Document Doc1;
66+
bool Ok =
67+
Doc1.readFromBlob(StringRef("\xCA\x3F\x80\x00\x00", 5), /*Multi=*/false);
68+
ASSERT_TRUE(Ok);
69+
ASSERT_EQ(Doc1.getRoot().getKind(), Type::Float);
70+
ASSERT_EQ(Doc1.getRoot().getFloat(), 1.0);
71+
Document Doc2;
72+
Ok = Doc2.readFromBlob(StringRef("\xCB\x48\x3D\x63\x29\xF1\xC3\x5C\xA5", 9),
73+
/*Multi=*/false);
74+
ASSERT_TRUE(Ok);
75+
ASSERT_EQ(Doc2.getRoot().getKind(), Type::Float);
76+
ASSERT_EQ(Doc2.getRoot().getFloat(), 1e40);
3177
}
3278

3379
TEST(MsgPackDocument, TestReadBinary) {
@@ -192,12 +238,54 @@ TEST(MsgPackDocument, TestReadMergeMap) {
192238
ASSERT_EQ(BayS.getInt(), 8);
193239
}
194240

241+
TEST(MsgPackDocument, TestWriteBoolean) {
242+
Document Doc;
243+
Doc.getRoot() = true;
244+
std::string Buffer;
245+
Doc.writeToBlob(Buffer);
246+
ASSERT_EQ(Buffer, "\xc3");
247+
Doc.getRoot() = false;
248+
Doc.writeToBlob(Buffer);
249+
ASSERT_EQ(Buffer, "\xc2");
250+
}
251+
195252
TEST(MsgPackDocument, TestWriteInt) {
196253
Document Doc;
197254
Doc.getRoot() = 1;
198255
std::string Buffer;
199256
Doc.writeToBlob(Buffer);
200257
ASSERT_EQ(Buffer, "\x01");
258+
Doc.getRoot() = -1;
259+
Doc.writeToBlob(Buffer);
260+
ASSERT_EQ(Buffer, "\xFF");
261+
Doc.getRoot() = -4096;
262+
Doc.writeToBlob(Buffer);
263+
ASSERT_EQ(Buffer, StringRef("\xD1\xF0\x00", 3));
264+
}
265+
266+
TEST(MsgPackDocument, TestWriteUInt) {
267+
Document Doc;
268+
Doc.getRoot() = 1U;
269+
std::string Buffer;
270+
Doc.writeToBlob(Buffer);
271+
ASSERT_EQ(Buffer, "\x01");
272+
Doc.getRoot() = 4096U;
273+
Doc.writeToBlob(Buffer);
274+
ASSERT_EQ(Buffer, StringRef("\xCD\x10\x00", 3));
275+
}
276+
277+
TEST(MsgPackDocument, TestWriteFloat) {
278+
Document Doc;
279+
Doc.getRoot() = 1.0;
280+
std::string Buffer;
281+
Doc.writeToBlob(Buffer);
282+
ASSERT_EQ(Buffer, StringRef("\xCA\x3F\x80\x00\x00", 5));
283+
Doc.getRoot() = 1.0f;
284+
Doc.writeToBlob(Buffer);
285+
ASSERT_EQ(Buffer, StringRef("\xCA\x3F\x80\x00\x00", 5));
286+
Doc.getRoot() = 1e40;
287+
Doc.writeToBlob(Buffer);
288+
ASSERT_EQ(Buffer, "\xCB\x48\x3D\x63\x29\xF1\xC3\x5C\xA5");
201289
}
202290

203291
TEST(MsgPackDocument, TestWriteBinary) {

0 commit comments

Comments
 (0)