summaryrefslogtreecommitdiff
path: root/unittests
diff options
context:
space:
mode:
authorMichael J. Spencer <bigcheesegs@gmail.com>2010-10-21 20:28:21 +0000
committerMichael J. Spencer <bigcheesegs@gmail.com>2010-10-21 20:28:21 +0000
commit5e0b2bf657dd8b6b3bb58439e6cb293f3116687f (patch)
tree8d692616f4fb03bd88385566947ffbd9da0ef6c6 /unittests
parentd451f888b85d01caa586b0d45bacb41836fd2c31 (diff)
downloadllvm-5e0b2bf657dd8b6b3bb58439e6cb293f3116687f.tar.gz
llvm-5e0b2bf657dd8b6b3bb58439e6cb293f3116687f.tar.bz2
llvm-5e0b2bf657dd8b6b3bb58439e6cb293f3116687f.tar.xz
Support: Add Endian.h
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@117057 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests')
-rw-r--r--unittests/CMakeLists.txt3
-rw-r--r--unittests/Support/EndianTest.cpp72
2 files changed, 74 insertions, 1 deletions
diff --git a/unittests/CMakeLists.txt b/unittests/CMakeLists.txt
index d37b8906b4..df51bf821f 100644
--- a/unittests/CMakeLists.txt
+++ b/unittests/CMakeLists.txt
@@ -92,12 +92,13 @@ add_llvm_unittest(Support
Support/Casting.cpp
Support/CommandLineTest.cpp
Support/ConstantRangeTest.cpp
+ Support/EndianTest.cpp
Support/LeakDetectorTest.cpp
Support/MathExtrasTest.cpp
Support/raw_ostream_test.cpp
Support/RegexTest.cpp
- Support/System.cpp
Support/SwapByteOrderTest.cpp
+ Support/System.cpp
Support/TypeBuilderTest.cpp
Support/ValueHandleTest.cpp
)
diff --git a/unittests/Support/EndianTest.cpp b/unittests/Support/EndianTest.cpp
new file mode 100644
index 0000000000..d9172935d6
--- /dev/null
+++ b/unittests/Support/EndianTest.cpp
@@ -0,0 +1,72 @@
+//===- unittests/Support/EndianTest.cpp - Endian.h tests ------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "gtest/gtest.h"
+#include "llvm/Support/Endian.h"
+#include "llvm/System/DataTypes.h"
+#include <cstdlib>
+#include <ctime>
+using namespace llvm;
+using namespace support;
+
+#undef max
+
+namespace {
+
+TEST(Endian, Read) {
+ // These are 5 bytes so we can be sure at least one of the reads is unaligned.
+ unsigned char big[] = {0x00, 0x01, 0x02, 0x03, 0x04};
+ unsigned char little[] = {0x00, 0x04, 0x03, 0x02, 0x01};
+ int32_t BigAsHost = 0x00010203;
+ EXPECT_EQ(BigAsHost, (endian::read_be<int32_t, unaligned>(big)));
+ int32_t LittleAsHost = 0x02030400;
+ EXPECT_EQ(LittleAsHost, (endian::read_le<int32_t, unaligned>(little)));
+
+ EXPECT_EQ((endian::read_be<int32_t, unaligned>(big + 1)),
+ (endian::read_le<int32_t, unaligned>(little + 1)));
+}
+
+TEST(Endian, Write) {
+ unsigned char data[5];
+ endian::write_be<int32_t, unaligned>(data, -1362446643);
+ EXPECT_EQ(data[0], 0xAE);
+ EXPECT_EQ(data[1], 0xCA);
+ EXPECT_EQ(data[2], 0xB6);
+ EXPECT_EQ(data[3], 0xCD);
+ endian::write_be<int32_t, unaligned>(data + 1, -1362446643);
+ EXPECT_EQ(data[1], 0xAE);
+ EXPECT_EQ(data[2], 0xCA);
+ EXPECT_EQ(data[3], 0xB6);
+ EXPECT_EQ(data[4], 0xCD);
+
+ endian::write_le<int32_t, unaligned>(data, -1362446643);
+ EXPECT_EQ(data[0], 0xCD);
+ EXPECT_EQ(data[1], 0xB6);
+ EXPECT_EQ(data[2], 0xCA);
+ EXPECT_EQ(data[3], 0xAE);
+ endian::write_le<int32_t, unaligned>(data + 1, -1362446643);
+ EXPECT_EQ(data[1], 0xCD);
+ EXPECT_EQ(data[2], 0xB6);
+ EXPECT_EQ(data[3], 0xCA);
+ EXPECT_EQ(data[4], 0xAE);
+}
+
+TEST(Endian, PackedEndianSpecificIntegral) {
+ // These are 5 bytes so we can be sure at least one of the reads is unaligned.
+ unsigned char big[] = {0x00, 0x01, 0x02, 0x03, 0x04};
+ unsigned char little[] = {0x00, 0x04, 0x03, 0x02, 0x01};
+ big32_t *big_val =
+ reinterpret_cast<big32_t *>(big + 1);
+ little32_t *little_val =
+ reinterpret_cast<little32_t *>(little + 1);
+
+ EXPECT_EQ(*big_val, *little_val);
+}
+
+}