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 
156 
157 } // end anon namespace
158