1 //===- unittests/Support/EndianStreamTest.cpp - EndianStream.h tests ------===//
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/Support/EndianStream.h"
10 #include "llvm/ADT/SmallString.h"
11 #include "llvm/Support/DataTypes.h"
12 #include "gtest/gtest.h"
14 using namespace support
;
18 TEST(EndianStream
, WriteInt32LE
) {
22 raw_svector_ostream
OS(data
);
23 endian::Writer
LE(OS
, llvm::endianness::little
);
24 LE
.write(static_cast<int32_t>(-1362446643));
27 EXPECT_EQ(static_cast<uint8_t>(data
[0]), 0xCD);
28 EXPECT_EQ(static_cast<uint8_t>(data
[1]), 0xB6);
29 EXPECT_EQ(static_cast<uint8_t>(data
[2]), 0xCA);
30 EXPECT_EQ(static_cast<uint8_t>(data
[3]), 0xAE);
33 TEST(EndianStream
, WriteInt32BE
) {
34 SmallVector
<char, 16> data
;
37 raw_svector_ostream
OS(data
);
38 endian::Writer
BE(OS
, llvm::endianness::big
);
39 BE
.write(static_cast<int32_t>(-1362446643));
42 EXPECT_EQ(static_cast<uint8_t>(data
[0]), 0xAE);
43 EXPECT_EQ(static_cast<uint8_t>(data
[1]), 0xCA);
44 EXPECT_EQ(static_cast<uint8_t>(data
[2]), 0xB6);
45 EXPECT_EQ(static_cast<uint8_t>(data
[3]), 0xCD);
49 TEST(EndianStream
, WriteFloatLE
) {
53 raw_svector_ostream
OS(data
);
54 endian::Writer
LE(OS
, llvm::endianness::little
);
58 EXPECT_EQ(static_cast<uint8_t>(data
[0]), 0x00);
59 EXPECT_EQ(static_cast<uint8_t>(data
[1]), 0xE4);
60 EXPECT_EQ(static_cast<uint8_t>(data
[2]), 0x40);
61 EXPECT_EQ(static_cast<uint8_t>(data
[3]), 0x46);
64 TEST(EndianStream
, WriteFloatBE
) {
65 SmallVector
<char, 16> data
;
68 raw_svector_ostream
OS(data
);
69 endian::Writer
BE(OS
, llvm::endianness::big
);
73 EXPECT_EQ(static_cast<uint8_t>(data
[0]), 0x46);
74 EXPECT_EQ(static_cast<uint8_t>(data
[1]), 0x40);
75 EXPECT_EQ(static_cast<uint8_t>(data
[2]), 0xE4);
76 EXPECT_EQ(static_cast<uint8_t>(data
[3]), 0x00);
79 TEST(EndianStream
, WriteInt64LE
) {
83 raw_svector_ostream
OS(data
);
84 endian::Writer
LE(OS
, llvm::endianness::little
);
85 LE
.write(static_cast<int64_t>(-136244664332342323));
88 EXPECT_EQ(static_cast<uint8_t>(data
[0]), 0xCD);
89 EXPECT_EQ(static_cast<uint8_t>(data
[1]), 0xAB);
90 EXPECT_EQ(static_cast<uint8_t>(data
[2]), 0xED);
91 EXPECT_EQ(static_cast<uint8_t>(data
[3]), 0x1B);
92 EXPECT_EQ(static_cast<uint8_t>(data
[4]), 0x33);
93 EXPECT_EQ(static_cast<uint8_t>(data
[5]), 0xF6);
94 EXPECT_EQ(static_cast<uint8_t>(data
[6]), 0x1B);
95 EXPECT_EQ(static_cast<uint8_t>(data
[7]), 0xFE);
98 TEST(EndianStream
, WriteInt64BE
) {
99 SmallVector
<char, 16> data
;
102 raw_svector_ostream
OS(data
);
103 endian::Writer
BE(OS
, llvm::endianness::big
);
104 BE
.write(static_cast<int64_t>(-136244664332342323));
107 EXPECT_EQ(static_cast<uint8_t>(data
[0]), 0xFE);
108 EXPECT_EQ(static_cast<uint8_t>(data
[1]), 0x1B);
109 EXPECT_EQ(static_cast<uint8_t>(data
[2]), 0xF6);
110 EXPECT_EQ(static_cast<uint8_t>(data
[3]), 0x33);
111 EXPECT_EQ(static_cast<uint8_t>(data
[4]), 0x1B);
112 EXPECT_EQ(static_cast<uint8_t>(data
[5]), 0xED);
113 EXPECT_EQ(static_cast<uint8_t>(data
[6]), 0xAB);
114 EXPECT_EQ(static_cast<uint8_t>(data
[7]), 0xCD);
117 TEST(EndianStream
, WriteDoubleLE
) {
118 SmallString
<16> data
;
121 raw_svector_ostream
OS(data
);
122 endian::Writer
LE(OS
, llvm::endianness::little
);
123 LE
.write(-2349214918.58107);
126 EXPECT_EQ(static_cast<uint8_t>(data
[0]), 0x20);
127 EXPECT_EQ(static_cast<uint8_t>(data
[1]), 0x98);
128 EXPECT_EQ(static_cast<uint8_t>(data
[2]), 0xD2);
129 EXPECT_EQ(static_cast<uint8_t>(data
[3]), 0x98);
130 EXPECT_EQ(static_cast<uint8_t>(data
[4]), 0xC5);
131 EXPECT_EQ(static_cast<uint8_t>(data
[5]), 0x80);
132 EXPECT_EQ(static_cast<uint8_t>(data
[6]), 0xE1);
133 EXPECT_EQ(static_cast<uint8_t>(data
[7]), 0xC1);
136 TEST(EndianStream
, WriteDoubleBE
) {
137 SmallVector
<char, 16> data
;
140 raw_svector_ostream
OS(data
);
141 endian::Writer
BE(OS
, llvm::endianness::big
);
142 BE
.write(-2349214918.58107);
145 EXPECT_EQ(static_cast<uint8_t>(data
[0]), 0xC1);
146 EXPECT_EQ(static_cast<uint8_t>(data
[1]), 0xE1);
147 EXPECT_EQ(static_cast<uint8_t>(data
[2]), 0x80);
148 EXPECT_EQ(static_cast<uint8_t>(data
[3]), 0xC5);
149 EXPECT_EQ(static_cast<uint8_t>(data
[4]), 0x98);
150 EXPECT_EQ(static_cast<uint8_t>(data
[5]), 0xD2);
151 EXPECT_EQ(static_cast<uint8_t>(data
[6]), 0x98);
152 EXPECT_EQ(static_cast<uint8_t>(data
[7]), 0x20);
155 TEST(EndianStream
, WriteArrayLE
) {
156 SmallString
<16> Data
;
159 raw_svector_ostream
OS(Data
);
160 endian::Writer
LE(OS
, llvm::endianness::little
);
161 LE
.write
<uint16_t>({0x1234, 0x5678});
164 EXPECT_EQ(static_cast<uint8_t>(Data
[0]), 0x34);
165 EXPECT_EQ(static_cast<uint8_t>(Data
[1]), 0x12);
166 EXPECT_EQ(static_cast<uint8_t>(Data
[2]), 0x78);
167 EXPECT_EQ(static_cast<uint8_t>(Data
[3]), 0x56);
170 TEST(EndianStream
, WriteVectorLE
) {
171 SmallString
<16> Data
;
174 raw_svector_ostream
OS(Data
);
175 endian::Writer
LE(OS
, llvm::endianness::little
);
176 std::vector
<uint16_t> Vec
{0x1234, 0x5678};
177 LE
.write
<uint16_t>(Vec
);
180 EXPECT_EQ(static_cast<uint8_t>(Data
[0]), 0x34);
181 EXPECT_EQ(static_cast<uint8_t>(Data
[1]), 0x12);
182 EXPECT_EQ(static_cast<uint8_t>(Data
[2]), 0x78);
183 EXPECT_EQ(static_cast<uint8_t>(Data
[3]), 0x56);
186 TEST(EndianStream
, WriteFloatArrayLE
) {
187 SmallString
<16> Data
;
190 raw_svector_ostream
OS(Data
);
191 endian::Writer
LE(OS
, llvm::endianness::little
);
192 LE
.write
<float>({12345.0f
, 12346.0f
});
195 EXPECT_EQ(static_cast<uint8_t>(Data
[0]), 0x00);
196 EXPECT_EQ(static_cast<uint8_t>(Data
[1]), 0xE4);
197 EXPECT_EQ(static_cast<uint8_t>(Data
[2]), 0x40);
198 EXPECT_EQ(static_cast<uint8_t>(Data
[3]), 0x46);
200 EXPECT_EQ(static_cast<uint8_t>(Data
[4]), 0x00);
201 EXPECT_EQ(static_cast<uint8_t>(Data
[5]), 0xE8);
202 EXPECT_EQ(static_cast<uint8_t>(Data
[6]), 0x40);
203 EXPECT_EQ(static_cast<uint8_t>(Data
[7]), 0x46);
207 } // end anon namespace