llvm.org GIT mirror llvm / 7575430
Add support for double / float to EndianStream Also add new unit tests for endian::Writer git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@228269 91177308-0d34-0410-b5e6-96231b3b80d8 Matt Arsenault 5 years ago
3 changed file(s) with 183 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
3030 OS.write((const char *)&Val, sizeof(value_type));
3131 }
3232 };
33
34 template <>
35 template <>
36 inline void Writer::write(float Val) {
37 write(FloatToBits(Val));
38 }
39
40 template <>
41 template <>
42 inline void Writer::write(double Val) {
43 write(DoubleToBits(Val));
44 }
45
46 template <>
47 template <>
48 inline void Writer::write(float Val) {
49 write(FloatToBits(Val));
50 }
51
52 template <>
53 template <>
54 inline void Writer::write(double Val) {
55 write(DoubleToBits(Val));
56 }
57
3358 } // end namespace endian
3459
3560 } // end namespace support
1313 ConvertUTFTest.cpp
1414 DataExtractorTest.cpp
1515 DwarfTest.cpp
16 EndianStreamTest.cpp
1617 EndianTest.cpp
1718 ErrorOrTest.cpp
1819 FileOutputBufferTest.cpp
0 //===- unittests/Support/EndianStreamTest.cpp - EndianStream.h tests ------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/ADT/SmallString.h"
10 #include "llvm/Support/EndianStream.h"
11 #include "llvm/Support/DataTypes.h"
12 #include "gtest/gtest.h"
13 using namespace llvm;
14 using namespace support;
15
16 namespace {
17
18 TEST(EndianStream, WriteInt32LE) {
19 SmallString<16> data;
20
21 {
22 raw_svector_ostream OS(data);
23 endian::Writer LE(OS);
24 LE.write(static_cast(-1362446643));
25 }
26
27 EXPECT_EQ(static_cast(data[0]), 0xCD);
28 EXPECT_EQ(static_cast(data[1]), 0xB6);
29 EXPECT_EQ(static_cast(data[2]), 0xCA);
30 EXPECT_EQ(static_cast(data[3]), 0xAE);
31 }
32
33 TEST(EndianStream, WriteInt32BE) {
34 SmallVector data;
35
36 {
37 raw_svector_ostream OS(data);
38 endian::Writer BE(OS);
39 BE.write(static_cast(-1362446643));
40 }
41
42 EXPECT_EQ(static_cast(data[0]), 0xAE);
43 EXPECT_EQ(static_cast(data[1]), 0xCA);
44 EXPECT_EQ(static_cast(data[2]), 0xB6);
45 EXPECT_EQ(static_cast(data[3]), 0xCD);
46 }
47
48
49 TEST(EndianStream, WriteFloatLE) {
50 SmallString<16> data;
51
52 {
53 raw_svector_ostream OS(data);
54 endian::Writer LE(OS);
55 LE.write(12345.0f);
56 }
57
58 EXPECT_EQ(static_cast(data[0]), 0x00);
59 EXPECT_EQ(static_cast(data[1]), 0xE4);
60 EXPECT_EQ(static_cast(data[2]), 0x40);
61 EXPECT_EQ(static_cast(data[3]), 0x46);
62 }
63
64 TEST(EndianStream, WriteFloatBE) {
65 SmallVector data;
66
67 {
68 raw_svector_ostream OS(data);
69 endian::Writer BE(OS);
70 BE.write(12345.0f);
71 }
72
73 EXPECT_EQ(static_cast(data[0]), 0x46);
74 EXPECT_EQ(static_cast(data[1]), 0x40);
75 EXPECT_EQ(static_cast(data[2]), 0xE4);
76 EXPECT_EQ(static_cast(data[3]), 0x00);
77 }
78
79 TEST(EndianStream, WriteInt64LE) {
80 SmallString<16> data;
81
82 {
83 raw_svector_ostream OS(data);
84 endian::Writer LE(OS);
85 LE.write(static_cast(-136244664332342323));
86 }
87
88 EXPECT_EQ(static_cast(data[0]), 0xCD);
89 EXPECT_EQ(static_cast(data[1]), 0xAB);
90 EXPECT_EQ(static_cast(data[2]), 0xED);
91 EXPECT_EQ(static_cast(data[3]), 0x1B);
92 EXPECT_EQ(static_cast(data[4]), 0x33);
93 EXPECT_EQ(static_cast(data[5]), 0xF6);
94 EXPECT_EQ(static_cast(data[6]), 0x1B);
95 EXPECT_EQ(static_cast(data[7]), 0xFE);
96 }
97
98 TEST(EndianStream, WriteInt64BE) {
99 SmallVector data;
100
101 {
102 raw_svector_ostream OS(data);
103 endian::Writer BE(OS);
104 BE.write(static_cast(-136244664332342323));
105 }
106
107 EXPECT_EQ(static_cast(data[0]), 0xFE);
108 EXPECT_EQ(static_cast(data[1]), 0x1B);
109 EXPECT_EQ(static_cast(data[2]), 0xF6);
110 EXPECT_EQ(static_cast(data[3]), 0x33);
111 EXPECT_EQ(static_cast(data[4]), 0x1B);
112 EXPECT_EQ(static_cast(data[5]), 0xED);
113 EXPECT_EQ(static_cast(data[6]), 0xAB);
114 EXPECT_EQ(static_cast(data[7]), 0xCD);
115 }
116
117 TEST(EndianStream, WriteDoubleLE) {
118 SmallString<16> data;
119
120 {
121 raw_svector_ostream OS(data);
122 endian::Writer LE(OS);
123 LE.write(-2349214918.58107);
124 }
125
126 EXPECT_EQ(static_cast(data[0]), 0x20);
127 EXPECT_EQ(static_cast(data[1]), 0x98);
128 EXPECT_EQ(static_cast(data[2]), 0xD2);
129 EXPECT_EQ(static_cast(data[3]), 0x98);
130 EXPECT_EQ(static_cast(data[4]), 0xC5);
131 EXPECT_EQ(static_cast(data[5]), 0x80);
132 EXPECT_EQ(static_cast(data[6]), 0xE1);
133 EXPECT_EQ(static_cast(data[7]), 0xC1);
134 }
135
136 TEST(EndianStream, WriteDoubleBE) {
137 SmallVector data;
138
139 {
140 raw_svector_ostream OS(data);
141 endian::Writer BE(OS);
142 BE.write(-2349214918.58107);
143 }
144
145 EXPECT_EQ(static_cast(data[0]), 0xC1);
146 EXPECT_EQ(static_cast(data[1]), 0xE1);
147 EXPECT_EQ(static_cast(data[2]), 0x80);
148 EXPECT_EQ(static_cast(data[3]), 0xC5);
149 EXPECT_EQ(static_cast(data[4]), 0x98);
150 EXPECT_EQ(static_cast(data[5]), 0xD2);
151 EXPECT_EQ(static_cast(data[6]), 0x98);
152 EXPECT_EQ(static_cast(data[7]), 0x20);
153 }
154
155
156 } // end anon namespace