Page MenuHomePhabricator

No OneTemporary

File Metadata

Created
Jan 24 2020, 4:16 PM
Index: llvm/trunk/unittests/Support/EndianStreamTest.cpp
===================================================================
--- llvm/trunk/unittests/Support/EndianStreamTest.cpp (revision 258534)
+++ llvm/trunk/unittests/Support/EndianStreamTest.cpp (revision 258535)
@@ -1,157 +1,187 @@
//===- unittests/Support/EndianStreamTest.cpp - EndianStream.h tests ------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/EndianStream.h"
#include "llvm/Support/DataTypes.h"
#include "gtest/gtest.h"
using namespace llvm;
using namespace support;
namespace {
TEST(EndianStream, WriteInt32LE) {
SmallString<16> data;
{
raw_svector_ostream OS(data);
endian::Writer<little> LE(OS);
LE.write(static_cast<int32_t>(-1362446643));
}
EXPECT_EQ(static_cast<uint8_t>(data[0]), 0xCD);
EXPECT_EQ(static_cast<uint8_t>(data[1]), 0xB6);
EXPECT_EQ(static_cast<uint8_t>(data[2]), 0xCA);
EXPECT_EQ(static_cast<uint8_t>(data[3]), 0xAE);
}
TEST(EndianStream, WriteInt32BE) {
SmallVector<char, 16> data;
{
raw_svector_ostream OS(data);
endian::Writer<big> BE(OS);
BE.write(static_cast<int32_t>(-1362446643));
}
EXPECT_EQ(static_cast<uint8_t>(data[0]), 0xAE);
EXPECT_EQ(static_cast<uint8_t>(data[1]), 0xCA);
EXPECT_EQ(static_cast<uint8_t>(data[2]), 0xB6);
EXPECT_EQ(static_cast<uint8_t>(data[3]), 0xCD);
}
TEST(EndianStream, WriteFloatLE) {
SmallString<16> data;
{
raw_svector_ostream OS(data);
endian::Writer<little> LE(OS);
LE.write(12345.0f);
}
EXPECT_EQ(static_cast<uint8_t>(data[0]), 0x00);
EXPECT_EQ(static_cast<uint8_t>(data[1]), 0xE4);
EXPECT_EQ(static_cast<uint8_t>(data[2]), 0x40);
EXPECT_EQ(static_cast<uint8_t>(data[3]), 0x46);
}
TEST(EndianStream, WriteFloatBE) {
SmallVector<char, 16> data;
{
raw_svector_ostream OS(data);
endian::Writer<big> BE(OS);
BE.write(12345.0f);
}
EXPECT_EQ(static_cast<uint8_t>(data[0]), 0x46);
EXPECT_EQ(static_cast<uint8_t>(data[1]), 0x40);
EXPECT_EQ(static_cast<uint8_t>(data[2]), 0xE4);
EXPECT_EQ(static_cast<uint8_t>(data[3]), 0x00);
}
TEST(EndianStream, WriteInt64LE) {
SmallString<16> data;
{
raw_svector_ostream OS(data);
endian::Writer<little> LE(OS);
LE.write(static_cast<int64_t>(-136244664332342323));
}
EXPECT_EQ(static_cast<uint8_t>(data[0]), 0xCD);
EXPECT_EQ(static_cast<uint8_t>(data[1]), 0xAB);
EXPECT_EQ(static_cast<uint8_t>(data[2]), 0xED);
EXPECT_EQ(static_cast<uint8_t>(data[3]), 0x1B);
EXPECT_EQ(static_cast<uint8_t>(data[4]), 0x33);
EXPECT_EQ(static_cast<uint8_t>(data[5]), 0xF6);
EXPECT_EQ(static_cast<uint8_t>(data[6]), 0x1B);
EXPECT_EQ(static_cast<uint8_t>(data[7]), 0xFE);
}
TEST(EndianStream, WriteInt64BE) {
SmallVector<char, 16> data;
{
raw_svector_ostream OS(data);
endian::Writer<big> BE(OS);
BE.write(static_cast<int64_t>(-136244664332342323));
}
EXPECT_EQ(static_cast<uint8_t>(data[0]), 0xFE);
EXPECT_EQ(static_cast<uint8_t>(data[1]), 0x1B);
EXPECT_EQ(static_cast<uint8_t>(data[2]), 0xF6);
EXPECT_EQ(static_cast<uint8_t>(data[3]), 0x33);
EXPECT_EQ(static_cast<uint8_t>(data[4]), 0x1B);
EXPECT_EQ(static_cast<uint8_t>(data[5]), 0xED);
EXPECT_EQ(static_cast<uint8_t>(data[6]), 0xAB);
EXPECT_EQ(static_cast<uint8_t>(data[7]), 0xCD);
}
TEST(EndianStream, WriteDoubleLE) {
SmallString<16> data;
{
raw_svector_ostream OS(data);
endian::Writer<little> LE(OS);
LE.write(-2349214918.58107);
}
EXPECT_EQ(static_cast<uint8_t>(data[0]), 0x20);
EXPECT_EQ(static_cast<uint8_t>(data[1]), 0x98);
EXPECT_EQ(static_cast<uint8_t>(data[2]), 0xD2);
EXPECT_EQ(static_cast<uint8_t>(data[3]), 0x98);
EXPECT_EQ(static_cast<uint8_t>(data[4]), 0xC5);
EXPECT_EQ(static_cast<uint8_t>(data[5]), 0x80);
EXPECT_EQ(static_cast<uint8_t>(data[6]), 0xE1);
EXPECT_EQ(static_cast<uint8_t>(data[7]), 0xC1);
}
TEST(EndianStream, WriteDoubleBE) {
SmallVector<char, 16> data;
{
raw_svector_ostream OS(data);
endian::Writer<big> BE(OS);
BE.write(-2349214918.58107);
}
EXPECT_EQ(static_cast<uint8_t>(data[0]), 0xC1);
EXPECT_EQ(static_cast<uint8_t>(data[1]), 0xE1);
EXPECT_EQ(static_cast<uint8_t>(data[2]), 0x80);
EXPECT_EQ(static_cast<uint8_t>(data[3]), 0xC5);
EXPECT_EQ(static_cast<uint8_t>(data[4]), 0x98);
EXPECT_EQ(static_cast<uint8_t>(data[5]), 0xD2);
EXPECT_EQ(static_cast<uint8_t>(data[6]), 0x98);
EXPECT_EQ(static_cast<uint8_t>(data[7]), 0x20);
}
+TEST(EndianStream, WriteArrayLE) {
+ SmallString<16> Data;
+
+ {
+ raw_svector_ostream OS(Data);
+ endian::Writer<little> LE(OS);
+ LE.write<uint16_t>({0x1234, 0x5678});
+ }
+
+ EXPECT_EQ(static_cast<uint8_t>(Data[0]), 0x34);
+ EXPECT_EQ(static_cast<uint8_t>(Data[1]), 0x12);
+ EXPECT_EQ(static_cast<uint8_t>(Data[2]), 0x78);
+ EXPECT_EQ(static_cast<uint8_t>(Data[3]), 0x56);
+}
+
+TEST(EndianStream, WriteVectorLE) {
+ SmallString<16> Data;
+
+ {
+ raw_svector_ostream OS(Data);
+ endian::Writer<little> LE(OS);
+ std::vector<uint16_t> Vec{0x1234, 0x5678};
+ LE.write<uint16_t>(Vec);
+ }
+
+ EXPECT_EQ(static_cast<uint8_t>(Data[0]), 0x34);
+ EXPECT_EQ(static_cast<uint8_t>(Data[1]), 0x12);
+ EXPECT_EQ(static_cast<uint8_t>(Data[2]), 0x78);
+ EXPECT_EQ(static_cast<uint8_t>(Data[3]), 0x56);
+}
} // end anon namespace
Index: llvm/trunk/include/llvm/Support/EndianStream.h
===================================================================
--- llvm/trunk/include/llvm/Support/EndianStream.h (revision 258534)
+++ llvm/trunk/include/llvm/Support/EndianStream.h (revision 258535)
@@ -1,64 +1,71 @@
//===- EndianStream.h - Stream ops with endian specific data ----*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines utilities for operating on streams that have endian
// specific data.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_SUPPORT_ENDIANSTREAM_H
#define LLVM_SUPPORT_ENDIANSTREAM_H
+#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/raw_ostream.h"
namespace llvm {
namespace support {
namespace endian {
/// Adapter to write values to a stream in a particular byte order.
template <endianness endian> struct Writer {
raw_ostream &OS;
Writer(raw_ostream &OS) : OS(OS) {}
+ template <typename value_type> void write(ArrayRef<value_type> Vals) {
+ for (value_type V : Vals) {
+ value_type Swaped = byte_swap<value_type, endian>(V);
+ OS.write((const char *)&Swaped, sizeof(value_type));
+ }
+ }
template <typename value_type> void write(value_type Val) {
Val = byte_swap<value_type, endian>(Val);
OS.write((const char *)&Val, sizeof(value_type));
}
};
template <>
template <>
inline void Writer<little>::write<float>(float Val) {
write(FloatToBits(Val));
}
template <>
template <>
inline void Writer<little>::write<double>(double Val) {
write(DoubleToBits(Val));
}
template <>
template <>
inline void Writer<big>::write<float>(float Val) {
write(FloatToBits(Val));
}
template <>
template <>
inline void Writer<big>::write<double>(double Val) {
write(DoubleToBits(Val));
}
} // end namespace endian
} // end namespace support
} // end namespace llvm
#endif

Event Timeline