Michael J. Spencer | ded95b7 | 2010-10-11 21:56:16 +0000 | [diff] [blame] | 1 | //===- unittests/Support/SwapByteOrderTest.cpp - swap byte order test -----===// |
| 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 "gtest/gtest.h" |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 11 | #include "llvm/Support/SwapByteOrder.h" |
Michael J. Spencer | ded95b7 | 2010-10-11 21:56:16 +0000 | [diff] [blame] | 12 | #include <cstdlib> |
| 13 | #include <ctime> |
| 14 | using namespace llvm; |
| 15 | |
| 16 | #undef max |
| 17 | |
| 18 | namespace { |
| 19 | |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame^] | 20 | // In these first two tests all of the original_uintx values are truncated |
Michael J. Spencer | ded95b7 | 2010-10-11 21:56:16 +0000 | [diff] [blame] | 21 | // except for 64. We could avoid this, but there's really no point. |
| 22 | |
| 23 | TEST(SwapByteOrder, UnsignedRoundTrip) { |
| 24 | // The point of the bit twiddling of magic is to test with and without bits |
| 25 | // in every byte. |
| 26 | uint64_t value = 1; |
| 27 | for (std::size_t i = 0; i <= sizeof(value); ++i) { |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame^] | 28 | uint8_t original_uint8 = static_cast<uint8_t>(value); |
| 29 | EXPECT_EQ(original_uint8, |
| 30 | sys::SwapByteOrder(sys::SwapByteOrder(original_uint8))); |
Michael J. Spencer | ded95b7 | 2010-10-11 21:56:16 +0000 | [diff] [blame] | 31 | |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame^] | 32 | uint16_t original_uint16 = static_cast<uint16_t>(value); |
| 33 | EXPECT_EQ(original_uint16, |
| 34 | sys::SwapByteOrder(sys::SwapByteOrder(original_uint16))); |
Michael J. Spencer | ded95b7 | 2010-10-11 21:56:16 +0000 | [diff] [blame] | 35 | |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame^] | 36 | uint32_t original_uint32 = static_cast<uint32_t>(value); |
| 37 | EXPECT_EQ(original_uint32, |
| 38 | sys::SwapByteOrder(sys::SwapByteOrder(original_uint32))); |
Michael J. Spencer | ded95b7 | 2010-10-11 21:56:16 +0000 | [diff] [blame] | 39 | |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame^] | 40 | uint64_t original_uint64 = static_cast<uint64_t>(value); |
| 41 | EXPECT_EQ(original_uint64, |
| 42 | sys::SwapByteOrder(sys::SwapByteOrder(original_uint64))); |
Michael J. Spencer | ded95b7 | 2010-10-11 21:56:16 +0000 | [diff] [blame] | 43 | |
| 44 | value = (value << 8) | 0x55; // binary 0101 0101. |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | TEST(SwapByteOrder, SignedRoundTrip) { |
| 49 | // The point of the bit twiddling of magic is to test with and without bits |
| 50 | // in every byte. |
| 51 | uint64_t value = 1; |
| 52 | for (std::size_t i = 0; i <= sizeof(value); ++i) { |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame^] | 53 | int8_t original_int8 = static_cast<int8_t>(value); |
| 54 | EXPECT_EQ(original_int8, |
| 55 | sys::SwapByteOrder(sys::SwapByteOrder(original_int8))); |
Michael J. Spencer | ded95b7 | 2010-10-11 21:56:16 +0000 | [diff] [blame] | 56 | |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame^] | 57 | int16_t original_int16 = static_cast<int16_t>(value); |
| 58 | EXPECT_EQ(original_int16, |
| 59 | sys::SwapByteOrder(sys::SwapByteOrder(original_int16))); |
Michael J. Spencer | ded95b7 | 2010-10-11 21:56:16 +0000 | [diff] [blame] | 60 | |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame^] | 61 | int32_t original_int32 = static_cast<int32_t>(value); |
| 62 | EXPECT_EQ(original_int32, |
| 63 | sys::SwapByteOrder(sys::SwapByteOrder(original_int32))); |
Michael J. Spencer | ded95b7 | 2010-10-11 21:56:16 +0000 | [diff] [blame] | 64 | |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame^] | 65 | int64_t original_int64 = static_cast<int64_t>(value); |
| 66 | EXPECT_EQ(original_int64, |
| 67 | sys::SwapByteOrder(sys::SwapByteOrder(original_int64))); |
Michael J. Spencer | ded95b7 | 2010-10-11 21:56:16 +0000 | [diff] [blame] | 68 | |
| 69 | // Test other sign. |
| 70 | value *= -1; |
| 71 | |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame^] | 72 | original_int8 = static_cast<int8_t>(value); |
| 73 | EXPECT_EQ(original_int8, |
| 74 | sys::SwapByteOrder(sys::SwapByteOrder(original_int8))); |
Michael J. Spencer | ded95b7 | 2010-10-11 21:56:16 +0000 | [diff] [blame] | 75 | |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame^] | 76 | original_int16 = static_cast<int16_t>(value); |
| 77 | EXPECT_EQ(original_int16, |
| 78 | sys::SwapByteOrder(sys::SwapByteOrder(original_int16))); |
Michael J. Spencer | ded95b7 | 2010-10-11 21:56:16 +0000 | [diff] [blame] | 79 | |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame^] | 80 | original_int32 = static_cast<int32_t>(value); |
| 81 | EXPECT_EQ(original_int32, |
| 82 | sys::SwapByteOrder(sys::SwapByteOrder(original_int32))); |
Michael J. Spencer | ded95b7 | 2010-10-11 21:56:16 +0000 | [diff] [blame] | 83 | |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame^] | 84 | original_int64 = static_cast<int64_t>(value); |
| 85 | EXPECT_EQ(original_int64, |
| 86 | sys::SwapByteOrder(sys::SwapByteOrder(original_int64))); |
Michael J. Spencer | ded95b7 | 2010-10-11 21:56:16 +0000 | [diff] [blame] | 87 | |
| 88 | // Return to normal sign and twiddle. |
| 89 | value *= -1; |
| 90 | value = (value << 8) | 0x55; // binary 0101 0101. |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | TEST(SwapByteOrder, uint8_t) { |
Chris Lattner | 8bec722 | 2010-11-23 04:04:25 +0000 | [diff] [blame] | 95 | EXPECT_EQ(uint8_t(0x11), sys::SwapByteOrder(uint8_t(0x11))); |
Michael J. Spencer | ded95b7 | 2010-10-11 21:56:16 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | TEST(SwapByteOrder, uint16_t) { |
Chris Lattner | 8bec722 | 2010-11-23 04:04:25 +0000 | [diff] [blame] | 99 | EXPECT_EQ(uint16_t(0x1122), sys::SwapByteOrder(uint16_t(0x2211))); |
Michael J. Spencer | ded95b7 | 2010-10-11 21:56:16 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | TEST(SwapByteOrder, uint32_t) { |
Chris Lattner | 8bec722 | 2010-11-23 04:04:25 +0000 | [diff] [blame] | 103 | EXPECT_EQ(uint32_t(0x11223344), sys::SwapByteOrder(uint32_t(0x44332211))); |
Michael J. Spencer | ded95b7 | 2010-10-11 21:56:16 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | TEST(SwapByteOrder, uint64_t) { |
Michael J. Spencer | b28ce01 | 2010-10-11 22:04:38 +0000 | [diff] [blame] | 107 | EXPECT_EQ(uint64_t(0x1122334455667788ULL), |
Chris Lattner | 8bec722 | 2010-11-23 04:04:25 +0000 | [diff] [blame] | 108 | sys::SwapByteOrder(uint64_t(0x8877665544332211ULL))); |
Michael J. Spencer | ded95b7 | 2010-10-11 21:56:16 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | TEST(SwapByteOrder, int8_t) { |
Chris Lattner | 8bec722 | 2010-11-23 04:04:25 +0000 | [diff] [blame] | 112 | EXPECT_EQ(int8_t(0x11), sys::SwapByteOrder(int8_t(0x11))); |
Michael J. Spencer | ded95b7 | 2010-10-11 21:56:16 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | TEST(SwapByteOrder, int16_t) { |
Chris Lattner | 8bec722 | 2010-11-23 04:04:25 +0000 | [diff] [blame] | 116 | EXPECT_EQ(int16_t(0x1122), sys::SwapByteOrder(int16_t(0x2211))); |
Michael J. Spencer | ded95b7 | 2010-10-11 21:56:16 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | TEST(SwapByteOrder, int32_t) { |
Chris Lattner | 8bec722 | 2010-11-23 04:04:25 +0000 | [diff] [blame] | 120 | EXPECT_EQ(int32_t(0x11223344), sys::SwapByteOrder(int32_t(0x44332211))); |
Michael J. Spencer | ded95b7 | 2010-10-11 21:56:16 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | TEST(SwapByteOrder, int64_t) { |
Michael J. Spencer | b28ce01 | 2010-10-11 22:04:38 +0000 | [diff] [blame] | 124 | EXPECT_EQ(int64_t(0x1122334455667788LL), |
Chris Lattner | 8bec722 | 2010-11-23 04:04:25 +0000 | [diff] [blame] | 125 | sys::SwapByteOrder(int64_t(0x8877665544332211LL))); |
Michael J. Spencer | ded95b7 | 2010-10-11 21:56:16 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | } |