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 "llvm/Support/SwapByteOrder.h" 11 #include "gtest/gtest.h" 12 #include <cstdlib> 13 #include <ctime> 14 using namespace llvm; 15 16 #undef max 17 18 namespace { 19 20 // In these first two tests all of the original_uintx values are truncated 21 // except for 64. We could avoid this, but there's really no point. 22 23 TEST(getSwappedBytes, 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) { 28 uint8_t original_uint8 = static_cast<uint8_t>(value); 29 EXPECT_EQ(original_uint8, 30 sys::getSwappedBytes(sys::getSwappedBytes(original_uint8))); 31 32 uint16_t original_uint16 = static_cast<uint16_t>(value); 33 EXPECT_EQ(original_uint16, 34 sys::getSwappedBytes(sys::getSwappedBytes(original_uint16))); 35 36 uint32_t original_uint32 = static_cast<uint32_t>(value); 37 EXPECT_EQ(original_uint32, 38 sys::getSwappedBytes(sys::getSwappedBytes(original_uint32))); 39 40 uint64_t original_uint64 = static_cast<uint64_t>(value); 41 EXPECT_EQ(original_uint64, 42 sys::getSwappedBytes(sys::getSwappedBytes(original_uint64))); 43 44 value = (value << 8) | 0x55; // binary 0101 0101. 45 } 46 } 47 48 TEST(getSwappedBytes, 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) { 53 int8_t original_int8 = static_cast<int8_t>(value); 54 EXPECT_EQ(original_int8, 55 sys::getSwappedBytes(sys::getSwappedBytes(original_int8))); 56 57 int16_t original_int16 = static_cast<int16_t>(value); 58 EXPECT_EQ(original_int16, 59 sys::getSwappedBytes(sys::getSwappedBytes(original_int16))); 60 61 int32_t original_int32 = static_cast<int32_t>(value); 62 EXPECT_EQ(original_int32, 63 sys::getSwappedBytes(sys::getSwappedBytes(original_int32))); 64 65 int64_t original_int64 = static_cast<int64_t>(value); 66 EXPECT_EQ(original_int64, 67 sys::getSwappedBytes(sys::getSwappedBytes(original_int64))); 68 69 // Test other sign. 70 value *= -1; 71 72 original_int8 = static_cast<int8_t>(value); 73 EXPECT_EQ(original_int8, 74 sys::getSwappedBytes(sys::getSwappedBytes(original_int8))); 75 76 original_int16 = static_cast<int16_t>(value); 77 EXPECT_EQ(original_int16, 78 sys::getSwappedBytes(sys::getSwappedBytes(original_int16))); 79 80 original_int32 = static_cast<int32_t>(value); 81 EXPECT_EQ(original_int32, 82 sys::getSwappedBytes(sys::getSwappedBytes(original_int32))); 83 84 original_int64 = static_cast<int64_t>(value); 85 EXPECT_EQ(original_int64, 86 sys::getSwappedBytes(sys::getSwappedBytes(original_int64))); 87 88 // Return to normal sign and twiddle. 89 value *= -1; 90 value = (value << 8) | 0x55; // binary 0101 0101. 91 } 92 } 93 94 TEST(getSwappedBytes, uint8_t) { 95 EXPECT_EQ(uint8_t(0x11), sys::getSwappedBytes(uint8_t(0x11))); 96 } 97 98 TEST(getSwappedBytes, uint16_t) { 99 EXPECT_EQ(uint16_t(0x1122), sys::getSwappedBytes(uint16_t(0x2211))); 100 } 101 102 TEST(getSwappedBytes, uint32_t) { 103 EXPECT_EQ(uint32_t(0x11223344), sys::getSwappedBytes(uint32_t(0x44332211))); 104 } 105 106 TEST(getSwappedBytes, uint64_t) { 107 EXPECT_EQ(uint64_t(0x1122334455667788ULL), 108 sys::getSwappedBytes(uint64_t(0x8877665544332211ULL))); 109 } 110 111 TEST(getSwappedBytes, int8_t) { 112 EXPECT_EQ(int8_t(0x11), sys::getSwappedBytes(int8_t(0x11))); 113 } 114 115 TEST(getSwappedBytes, int16_t) { 116 EXPECT_EQ(int16_t(0x1122), sys::getSwappedBytes(int16_t(0x2211))); 117 } 118 119 TEST(getSwappedBytes, int32_t) { 120 EXPECT_EQ(int32_t(0x11223344), sys::getSwappedBytes(int32_t(0x44332211))); 121 } 122 123 TEST(getSwappedBytes, int64_t) { 124 EXPECT_EQ(int64_t(0x1122334455667788LL), 125 sys::getSwappedBytes(int64_t(0x8877665544332211LL))); 126 } 127 128 TEST(getSwappedBytes, float) { 129 EXPECT_EQ(1.79366203433576585078237386661e-43f, sys::getSwappedBytes(-0.0f)); 130 // 0x11223344 131 EXPECT_EQ(7.1653228759765625e2f, sys::getSwappedBytes(1.2795344e-28f)); 132 } 133 134 TEST(getSwappedBytes, double) { 135 EXPECT_EQ(6.32404026676795576546008054871e-322, sys::getSwappedBytes(-0.0)); 136 // 0x1122334455667788 137 EXPECT_EQ(-7.08687663657301358331704585496e-268, 138 sys::getSwappedBytes(3.84141202447173065923064450234e-226)); 139 } 140 141 TEST(swapByteOrder, uint8_t) { 142 uint8_t value = 0x11; 143 sys::swapByteOrder(value); 144 EXPECT_EQ(uint8_t(0x11), value); 145 } 146 147 TEST(swapByteOrder, uint16_t) { 148 uint16_t value = 0x2211; 149 sys::swapByteOrder(value); 150 EXPECT_EQ(uint16_t(0x1122), value); 151 } 152 153 TEST(swapByteOrder, uint32_t) { 154 uint32_t value = 0x44332211; 155 sys::swapByteOrder(value); 156 EXPECT_EQ(uint32_t(0x11223344), value); 157 } 158 159 TEST(swapByteOrder, uint64_t) { 160 uint64_t value = 0x8877665544332211ULL; 161 sys::swapByteOrder(value); 162 EXPECT_EQ(uint64_t(0x1122334455667788ULL), value); 163 } 164 165 TEST(swapByteOrder, int8_t) { 166 int8_t value = 0x11; 167 sys::swapByteOrder(value); 168 EXPECT_EQ(int8_t(0x11), value); 169 } 170 171 TEST(swapByteOrder, int16_t) { 172 int16_t value = 0x2211; 173 sys::swapByteOrder(value); 174 EXPECT_EQ(int16_t(0x1122), value); 175 } 176 177 TEST(swapByteOrder, int32_t) { 178 int32_t value = 0x44332211; 179 sys::swapByteOrder(value); 180 EXPECT_EQ(int32_t(0x11223344), value); 181 } 182 183 TEST(swapByteOrder, int64_t) { 184 int64_t value = 0x8877665544332211LL; 185 sys::swapByteOrder(value); 186 EXPECT_EQ(int64_t(0x1122334455667788LL), value); 187 } 188 189 TEST(swapByteOrder, float) { 190 float value = 7.1653228759765625e2f; // 0x44332211 191 sys::swapByteOrder(value); 192 EXPECT_EQ(1.2795344e-28f, value); 193 } 194 195 TEST(swapByteOrder, double) { 196 double value = -7.08687663657301358331704585496e-268; // 0x8877665544332211 197 sys::swapByteOrder(value); 198 EXPECT_EQ(3.84141202447173065923064450234e-226, value); 199 } 200 201 } 202