1 //===- unittests/Support/EndianStreamTest.cpp - EndianStream.h tests ------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/ADT/SmallString.h"
11 #include "llvm/Support/EndianStream.h"
12 #include "llvm/Support/DataTypes.h"
13 #include "gtest/gtest.h"
14 using namespace llvm;
15 using namespace support;
16
17 namespace {
18
TEST(EndianStream,WriteInt32LE)19 TEST(EndianStream, WriteInt32LE) {
20 SmallString<16> data;
21
22 {
23 raw_svector_ostream OS(data);
24 endian::Writer<little> LE(OS);
25 LE.write(static_cast<int32_t>(-1362446643));
26 }
27
28 EXPECT_EQ(static_cast<uint8_t>(data[0]), 0xCD);
29 EXPECT_EQ(static_cast<uint8_t>(data[1]), 0xB6);
30 EXPECT_EQ(static_cast<uint8_t>(data[2]), 0xCA);
31 EXPECT_EQ(static_cast<uint8_t>(data[3]), 0xAE);
32 }
33
TEST(EndianStream,WriteInt32BE)34 TEST(EndianStream, WriteInt32BE) {
35 SmallVector<char, 16> data;
36
37 {
38 raw_svector_ostream OS(data);
39 endian::Writer<big> BE(OS);
40 BE.write(static_cast<int32_t>(-1362446643));
41 }
42
43 EXPECT_EQ(static_cast<uint8_t>(data[0]), 0xAE);
44 EXPECT_EQ(static_cast<uint8_t>(data[1]), 0xCA);
45 EXPECT_EQ(static_cast<uint8_t>(data[2]), 0xB6);
46 EXPECT_EQ(static_cast<uint8_t>(data[3]), 0xCD);
47 }
48
49
TEST(EndianStream,WriteFloatLE)50 TEST(EndianStream, WriteFloatLE) {
51 SmallString<16> data;
52
53 {
54 raw_svector_ostream OS(data);
55 endian::Writer<little> LE(OS);
56 LE.write(12345.0f);
57 }
58
59 EXPECT_EQ(static_cast<uint8_t>(data[0]), 0x00);
60 EXPECT_EQ(static_cast<uint8_t>(data[1]), 0xE4);
61 EXPECT_EQ(static_cast<uint8_t>(data[2]), 0x40);
62 EXPECT_EQ(static_cast<uint8_t>(data[3]), 0x46);
63 }
64
TEST(EndianStream,WriteFloatBE)65 TEST(EndianStream, WriteFloatBE) {
66 SmallVector<char, 16> data;
67
68 {
69 raw_svector_ostream OS(data);
70 endian::Writer<big> BE(OS);
71 BE.write(12345.0f);
72 }
73
74 EXPECT_EQ(static_cast<uint8_t>(data[0]), 0x46);
75 EXPECT_EQ(static_cast<uint8_t>(data[1]), 0x40);
76 EXPECT_EQ(static_cast<uint8_t>(data[2]), 0xE4);
77 EXPECT_EQ(static_cast<uint8_t>(data[3]), 0x00);
78 }
79
TEST(EndianStream,WriteInt64LE)80 TEST(EndianStream, WriteInt64LE) {
81 SmallString<16> data;
82
83 {
84 raw_svector_ostream OS(data);
85 endian::Writer<little> LE(OS);
86 LE.write(static_cast<int64_t>(-136244664332342323));
87 }
88
89 EXPECT_EQ(static_cast<uint8_t>(data[0]), 0xCD);
90 EXPECT_EQ(static_cast<uint8_t>(data[1]), 0xAB);
91 EXPECT_EQ(static_cast<uint8_t>(data[2]), 0xED);
92 EXPECT_EQ(static_cast<uint8_t>(data[3]), 0x1B);
93 EXPECT_EQ(static_cast<uint8_t>(data[4]), 0x33);
94 EXPECT_EQ(static_cast<uint8_t>(data[5]), 0xF6);
95 EXPECT_EQ(static_cast<uint8_t>(data[6]), 0x1B);
96 EXPECT_EQ(static_cast<uint8_t>(data[7]), 0xFE);
97 }
98
TEST(EndianStream,WriteInt64BE)99 TEST(EndianStream, WriteInt64BE) {
100 SmallVector<char, 16> data;
101
102 {
103 raw_svector_ostream OS(data);
104 endian::Writer<big> BE(OS);
105 BE.write(static_cast<int64_t>(-136244664332342323));
106 }
107
108 EXPECT_EQ(static_cast<uint8_t>(data[0]), 0xFE);
109 EXPECT_EQ(static_cast<uint8_t>(data[1]), 0x1B);
110 EXPECT_EQ(static_cast<uint8_t>(data[2]), 0xF6);
111 EXPECT_EQ(static_cast<uint8_t>(data[3]), 0x33);
112 EXPECT_EQ(static_cast<uint8_t>(data[4]), 0x1B);
113 EXPECT_EQ(static_cast<uint8_t>(data[5]), 0xED);
114 EXPECT_EQ(static_cast<uint8_t>(data[6]), 0xAB);
115 EXPECT_EQ(static_cast<uint8_t>(data[7]), 0xCD);
116 }
117
TEST(EndianStream,WriteDoubleLE)118 TEST(EndianStream, WriteDoubleLE) {
119 SmallString<16> data;
120
121 {
122 raw_svector_ostream OS(data);
123 endian::Writer<little> LE(OS);
124 LE.write(-2349214918.58107);
125 }
126
127 EXPECT_EQ(static_cast<uint8_t>(data[0]), 0x20);
128 EXPECT_EQ(static_cast<uint8_t>(data[1]), 0x98);
129 EXPECT_EQ(static_cast<uint8_t>(data[2]), 0xD2);
130 EXPECT_EQ(static_cast<uint8_t>(data[3]), 0x98);
131 EXPECT_EQ(static_cast<uint8_t>(data[4]), 0xC5);
132 EXPECT_EQ(static_cast<uint8_t>(data[5]), 0x80);
133 EXPECT_EQ(static_cast<uint8_t>(data[6]), 0xE1);
134 EXPECT_EQ(static_cast<uint8_t>(data[7]), 0xC1);
135 }
136
TEST(EndianStream,WriteDoubleBE)137 TEST(EndianStream, WriteDoubleBE) {
138 SmallVector<char, 16> data;
139
140 {
141 raw_svector_ostream OS(data);
142 endian::Writer<big> BE(OS);
143 BE.write(-2349214918.58107);
144 }
145
146 EXPECT_EQ(static_cast<uint8_t>(data[0]), 0xC1);
147 EXPECT_EQ(static_cast<uint8_t>(data[1]), 0xE1);
148 EXPECT_EQ(static_cast<uint8_t>(data[2]), 0x80);
149 EXPECT_EQ(static_cast<uint8_t>(data[3]), 0xC5);
150 EXPECT_EQ(static_cast<uint8_t>(data[4]), 0x98);
151 EXPECT_EQ(static_cast<uint8_t>(data[5]), 0xD2);
152 EXPECT_EQ(static_cast<uint8_t>(data[6]), 0x98);
153 EXPECT_EQ(static_cast<uint8_t>(data[7]), 0x20);
154 }
155
TEST(EndianStream,WriteArrayLE)156 TEST(EndianStream, WriteArrayLE) {
157 SmallString<16> Data;
158
159 {
160 raw_svector_ostream OS(Data);
161 endian::Writer<little> LE(OS);
162 LE.write<uint16_t>({0x1234, 0x5678});
163 }
164
165 EXPECT_EQ(static_cast<uint8_t>(Data[0]), 0x34);
166 EXPECT_EQ(static_cast<uint8_t>(Data[1]), 0x12);
167 EXPECT_EQ(static_cast<uint8_t>(Data[2]), 0x78);
168 EXPECT_EQ(static_cast<uint8_t>(Data[3]), 0x56);
169 }
170
TEST(EndianStream,WriteVectorLE)171 TEST(EndianStream, WriteVectorLE) {
172 SmallString<16> Data;
173
174 {
175 raw_svector_ostream OS(Data);
176 endian::Writer<little> LE(OS);
177 std::vector<uint16_t> Vec{0x1234, 0x5678};
178 LE.write<uint16_t>(Vec);
179 }
180
181 EXPECT_EQ(static_cast<uint8_t>(Data[0]), 0x34);
182 EXPECT_EQ(static_cast<uint8_t>(Data[1]), 0x12);
183 EXPECT_EQ(static_cast<uint8_t>(Data[2]), 0x78);
184 EXPECT_EQ(static_cast<uint8_t>(Data[3]), 0x56);
185 }
186
TEST(EndianStream,WriteFloatArrayLE)187 TEST(EndianStream, WriteFloatArrayLE) {
188 SmallString<16> Data;
189
190 {
191 raw_svector_ostream OS(Data);
192 endian::Writer<little> LE(OS);
193 LE.write<float>({12345.0f, 12346.0f});
194 }
195
196 EXPECT_EQ(static_cast<uint8_t>(Data[0]), 0x00);
197 EXPECT_EQ(static_cast<uint8_t>(Data[1]), 0xE4);
198 EXPECT_EQ(static_cast<uint8_t>(Data[2]), 0x40);
199 EXPECT_EQ(static_cast<uint8_t>(Data[3]), 0x46);
200
201 EXPECT_EQ(static_cast<uint8_t>(Data[4]), 0x00);
202 EXPECT_EQ(static_cast<uint8_t>(Data[5]), 0xE8);
203 EXPECT_EQ(static_cast<uint8_t>(Data[6]), 0x40);
204 EXPECT_EQ(static_cast<uint8_t>(Data[7]), 0x46);
205 }
206
207
208 } // end anon namespace
209