diff --git a/llvm/include/llvm/Support/EndianStream.h b/llvm/include/llvm/Support/EndianStream.h index 94f372f020b4..d44a9b3b7ce8 100644 --- a/llvm/include/llvm/Support/EndianStream.h +++ b/llvm/include/llvm/Support/EndianStream.h @@ -31,6 +31,31 @@ template struct Writer { OS.write((const char *)&Val, sizeof(value_type)); } }; + +template <> +template <> +inline void Writer::write(float Val) { + write(FloatToBits(Val)); +} + +template <> +template <> +inline void Writer::write(double Val) { + write(DoubleToBits(Val)); +} + +template <> +template <> +inline void Writer::write(float Val) { + write(FloatToBits(Val)); +} + +template <> +template <> +inline void Writer::write(double Val) { + write(DoubleToBits(Val)); +} + } // end namespace endian } // end namespace support diff --git a/llvm/unittests/Support/CMakeLists.txt b/llvm/unittests/Support/CMakeLists.txt index 214972ef8a91..f3b55c3a4172 100644 --- a/llvm/unittests/Support/CMakeLists.txt +++ b/llvm/unittests/Support/CMakeLists.txt @@ -14,6 +14,7 @@ add_llvm_unittest(SupportTests ConvertUTFTest.cpp DataExtractorTest.cpp DwarfTest.cpp + EndianStreamTest.cpp EndianTest.cpp ErrorOrTest.cpp FileOutputBufferTest.cpp diff --git a/llvm/unittests/Support/EndianStreamTest.cpp b/llvm/unittests/Support/EndianStreamTest.cpp new file mode 100644 index 000000000000..6a69be55f926 --- /dev/null +++ b/llvm/unittests/Support/EndianStreamTest.cpp @@ -0,0 +1,157 @@ +//===- 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 LE(OS); + LE.write(static_cast(-1362446643)); + } + + EXPECT_EQ(static_cast(data[0]), 0xCD); + EXPECT_EQ(static_cast(data[1]), 0xB6); + EXPECT_EQ(static_cast(data[2]), 0xCA); + EXPECT_EQ(static_cast(data[3]), 0xAE); +} + +TEST(EndianStream, WriteInt32BE) { + SmallVector data; + + { + raw_svector_ostream OS(data); + endian::Writer BE(OS); + BE.write(static_cast(-1362446643)); + } + + EXPECT_EQ(static_cast(data[0]), 0xAE); + EXPECT_EQ(static_cast(data[1]), 0xCA); + EXPECT_EQ(static_cast(data[2]), 0xB6); + EXPECT_EQ(static_cast(data[3]), 0xCD); +} + + +TEST(EndianStream, WriteFloatLE) { + SmallString<16> data; + + { + raw_svector_ostream OS(data); + endian::Writer LE(OS); + LE.write(12345.0f); + } + + EXPECT_EQ(static_cast(data[0]), 0x00); + EXPECT_EQ(static_cast(data[1]), 0xE4); + EXPECT_EQ(static_cast(data[2]), 0x40); + EXPECT_EQ(static_cast(data[3]), 0x46); +} + +TEST(EndianStream, WriteFloatBE) { + SmallVector data; + + { + raw_svector_ostream OS(data); + endian::Writer BE(OS); + BE.write(12345.0f); + } + + EXPECT_EQ(static_cast(data[0]), 0x46); + EXPECT_EQ(static_cast(data[1]), 0x40); + EXPECT_EQ(static_cast(data[2]), 0xE4); + EXPECT_EQ(static_cast(data[3]), 0x00); +} + +TEST(EndianStream, WriteInt64LE) { + SmallString<16> data; + + { + raw_svector_ostream OS(data); + endian::Writer LE(OS); + LE.write(static_cast(-136244664332342323)); + } + + EXPECT_EQ(static_cast(data[0]), 0xCD); + EXPECT_EQ(static_cast(data[1]), 0xAB); + EXPECT_EQ(static_cast(data[2]), 0xED); + EXPECT_EQ(static_cast(data[3]), 0x1B); + EXPECT_EQ(static_cast(data[4]), 0x33); + EXPECT_EQ(static_cast(data[5]), 0xF6); + EXPECT_EQ(static_cast(data[6]), 0x1B); + EXPECT_EQ(static_cast(data[7]), 0xFE); +} + +TEST(EndianStream, WriteInt64BE) { + SmallVector data; + + { + raw_svector_ostream OS(data); + endian::Writer BE(OS); + BE.write(static_cast(-136244664332342323)); + } + + EXPECT_EQ(static_cast(data[0]), 0xFE); + EXPECT_EQ(static_cast(data[1]), 0x1B); + EXPECT_EQ(static_cast(data[2]), 0xF6); + EXPECT_EQ(static_cast(data[3]), 0x33); + EXPECT_EQ(static_cast(data[4]), 0x1B); + EXPECT_EQ(static_cast(data[5]), 0xED); + EXPECT_EQ(static_cast(data[6]), 0xAB); + EXPECT_EQ(static_cast(data[7]), 0xCD); +} + +TEST(EndianStream, WriteDoubleLE) { + SmallString<16> data; + + { + raw_svector_ostream OS(data); + endian::Writer LE(OS); + LE.write(-2349214918.58107); + } + + EXPECT_EQ(static_cast(data[0]), 0x20); + EXPECT_EQ(static_cast(data[1]), 0x98); + EXPECT_EQ(static_cast(data[2]), 0xD2); + EXPECT_EQ(static_cast(data[3]), 0x98); + EXPECT_EQ(static_cast(data[4]), 0xC5); + EXPECT_EQ(static_cast(data[5]), 0x80); + EXPECT_EQ(static_cast(data[6]), 0xE1); + EXPECT_EQ(static_cast(data[7]), 0xC1); +} + +TEST(EndianStream, WriteDoubleBE) { + SmallVector data; + + { + raw_svector_ostream OS(data); + endian::Writer BE(OS); + BE.write(-2349214918.58107); + } + + EXPECT_EQ(static_cast(data[0]), 0xC1); + EXPECT_EQ(static_cast(data[1]), 0xE1); + EXPECT_EQ(static_cast(data[2]), 0x80); + EXPECT_EQ(static_cast(data[3]), 0xC5); + EXPECT_EQ(static_cast(data[4]), 0x98); + EXPECT_EQ(static_cast(data[5]), 0xD2); + EXPECT_EQ(static_cast(data[6]), 0x98); + EXPECT_EQ(static_cast(data[7]), 0x20); +} + + +} // end anon namespace