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"
11 #include "llvm/Support/SwapByteOrder.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
TEST(getSwappedBytes,UnsignedRoundTrip)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
TEST(getSwappedBytes,SignedRoundTrip)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
TEST(getSwappedBytes,uint8_t)94 TEST(getSwappedBytes, uint8_t) {
95 EXPECT_EQ(uint8_t(0x11), sys::getSwappedBytes(uint8_t(0x11)));
96 }
97
TEST(getSwappedBytes,uint16_t)98 TEST(getSwappedBytes, uint16_t) {
99 EXPECT_EQ(uint16_t(0x1122), sys::getSwappedBytes(uint16_t(0x2211)));
100 }
101
TEST(getSwappedBytes,uint32_t)102 TEST(getSwappedBytes, uint32_t) {
103 EXPECT_EQ(uint32_t(0x11223344), sys::getSwappedBytes(uint32_t(0x44332211)));
104 }
105
TEST(getSwappedBytes,uint64_t)106 TEST(getSwappedBytes, uint64_t) {
107 EXPECT_EQ(uint64_t(0x1122334455667788ULL),
108 sys::getSwappedBytes(uint64_t(0x8877665544332211ULL)));
109 }
110
TEST(getSwappedBytes,int8_t)111 TEST(getSwappedBytes, int8_t) {
112 EXPECT_EQ(int8_t(0x11), sys::getSwappedBytes(int8_t(0x11)));
113 }
114
TEST(getSwappedBytes,int16_t)115 TEST(getSwappedBytes, int16_t) {
116 EXPECT_EQ(int16_t(0x1122), sys::getSwappedBytes(int16_t(0x2211)));
117 }
118
TEST(getSwappedBytes,int32_t)119 TEST(getSwappedBytes, int32_t) {
120 EXPECT_EQ(int32_t(0x11223344), sys::getSwappedBytes(int32_t(0x44332211)));
121 }
122
TEST(getSwappedBytes,int64_t)123 TEST(getSwappedBytes, int64_t) {
124 EXPECT_EQ(int64_t(0x1122334455667788LL),
125 sys::getSwappedBytes(int64_t(0x8877665544332211LL)));
126 }
127
TEST(swapByteOrder,uint8_t)128 TEST(swapByteOrder, uint8_t) {
129 uint8_t value = 0x11;
130 sys::swapByteOrder(value);
131 EXPECT_EQ(uint8_t(0x11), value);
132 }
133
TEST(swapByteOrder,uint16_t)134 TEST(swapByteOrder, uint16_t) {
135 uint16_t value = 0x2211;
136 sys::swapByteOrder(value);
137 EXPECT_EQ(uint16_t(0x1122), value);
138 }
139
TEST(swapByteOrder,uint32_t)140 TEST(swapByteOrder, uint32_t) {
141 uint32_t value = 0x44332211;
142 sys::swapByteOrder(value);
143 EXPECT_EQ(uint32_t(0x11223344), value);
144 }
145
TEST(swapByteOrder,uint64_t)146 TEST(swapByteOrder, uint64_t) {
147 uint64_t value = 0x8877665544332211ULL;
148 sys::swapByteOrder(value);
149 EXPECT_EQ(uint64_t(0x1122334455667788ULL), value);
150 }
151
TEST(swapByteOrder,int8_t)152 TEST(swapByteOrder, int8_t) {
153 int8_t value = 0x11;
154 sys::swapByteOrder(value);
155 EXPECT_EQ(int8_t(0x11), value);
156 }
157
TEST(swapByteOrder,int16_t)158 TEST(swapByteOrder, int16_t) {
159 int16_t value = 0x2211;
160 sys::swapByteOrder(value);
161 EXPECT_EQ(int16_t(0x1122), value);
162 }
163
TEST(swapByteOrder,int32_t)164 TEST(swapByteOrder, int32_t) {
165 int32_t value = 0x44332211;
166 sys::swapByteOrder(value);
167 EXPECT_EQ(int32_t(0x11223344), value);
168 }
169
TEST(swapByteOrder,int64_t)170 TEST(swapByteOrder, int64_t) {
171 int64_t value = 0x8877665544332211LL;
172 sys::swapByteOrder(value);
173 EXPECT_EQ(int64_t(0x1122334455667788LL), value);
174 }
175
176 }
177