1 //===- unittests/Support/SwapByteOrderTest.cpp - swap byte order test -----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Support/SwapByteOrder.h"
10 #include "gtest/gtest.h"
11 #include <cstdlib>
12 #include <ctime>
13 using namespace llvm;
14
15 #undef max
16
17 namespace {
18
TEST(ByteSwap,Swap_32)19 TEST(ByteSwap, Swap_32) {
20 EXPECT_EQ(0x44332211u, ByteSwap_32(0x11223344));
21 EXPECT_EQ(0xDDCCBBAAu, ByteSwap_32(0xAABBCCDD));
22 }
23
TEST(ByteSwap,Swap_64)24 TEST(ByteSwap, Swap_64) {
25 EXPECT_EQ(0x8877665544332211ULL, ByteSwap_64(0x1122334455667788LL));
26 EXPECT_EQ(0x1100FFEEDDCCBBAAULL, ByteSwap_64(0xAABBCCDDEEFF0011LL));
27 }
28
29 // In these first two tests all of the original_uintx values are truncated
30 // except for 64. We could avoid this, but there's really no point.
31
TEST(getSwappedBytes,UnsignedRoundTrip)32 TEST(getSwappedBytes, UnsignedRoundTrip) {
33 // The point of the bit twiddling of magic is to test with and without bits
34 // in every byte.
35 uint64_t value = 1;
36 for (std::size_t i = 0; i <= sizeof(value); ++i) {
37 uint8_t original_uint8 = static_cast<uint8_t>(value);
38 EXPECT_EQ(original_uint8,
39 sys::getSwappedBytes(sys::getSwappedBytes(original_uint8)));
40
41 uint16_t original_uint16 = static_cast<uint16_t>(value);
42 EXPECT_EQ(original_uint16,
43 sys::getSwappedBytes(sys::getSwappedBytes(original_uint16)));
44
45 uint32_t original_uint32 = static_cast<uint32_t>(value);
46 EXPECT_EQ(original_uint32,
47 sys::getSwappedBytes(sys::getSwappedBytes(original_uint32)));
48
49 uint64_t original_uint64 = static_cast<uint64_t>(value);
50 EXPECT_EQ(original_uint64,
51 sys::getSwappedBytes(sys::getSwappedBytes(original_uint64)));
52
53 value = (value << 8) | 0x55; // binary 0101 0101.
54 }
55 }
56
TEST(getSwappedBytes,SignedRoundTrip)57 TEST(getSwappedBytes, SignedRoundTrip) {
58 // The point of the bit twiddling of magic is to test with and without bits
59 // in every byte.
60 uint64_t value = 1;
61 for (std::size_t i = 0; i <= sizeof(value); ++i) {
62 int8_t original_int8 = static_cast<int8_t>(value);
63 EXPECT_EQ(original_int8,
64 sys::getSwappedBytes(sys::getSwappedBytes(original_int8)));
65
66 int16_t original_int16 = static_cast<int16_t>(value);
67 EXPECT_EQ(original_int16,
68 sys::getSwappedBytes(sys::getSwappedBytes(original_int16)));
69
70 int32_t original_int32 = static_cast<int32_t>(value);
71 EXPECT_EQ(original_int32,
72 sys::getSwappedBytes(sys::getSwappedBytes(original_int32)));
73
74 int64_t original_int64 = static_cast<int64_t>(value);
75 EXPECT_EQ(original_int64,
76 sys::getSwappedBytes(sys::getSwappedBytes(original_int64)));
77
78 // Test other sign.
79 value *= -1;
80
81 original_int8 = static_cast<int8_t>(value);
82 EXPECT_EQ(original_int8,
83 sys::getSwappedBytes(sys::getSwappedBytes(original_int8)));
84
85 original_int16 = static_cast<int16_t>(value);
86 EXPECT_EQ(original_int16,
87 sys::getSwappedBytes(sys::getSwappedBytes(original_int16)));
88
89 original_int32 = static_cast<int32_t>(value);
90 EXPECT_EQ(original_int32,
91 sys::getSwappedBytes(sys::getSwappedBytes(original_int32)));
92
93 original_int64 = static_cast<int64_t>(value);
94 EXPECT_EQ(original_int64,
95 sys::getSwappedBytes(sys::getSwappedBytes(original_int64)));
96
97 // Return to normal sign and twiddle.
98 value *= -1;
99 value = (value << 8) | 0x55; // binary 0101 0101.
100 }
101 }
102
TEST(getSwappedBytes,uint8_t)103 TEST(getSwappedBytes, uint8_t) {
104 EXPECT_EQ(uint8_t(0x11), sys::getSwappedBytes(uint8_t(0x11)));
105 }
106
TEST(getSwappedBytes,uint16_t)107 TEST(getSwappedBytes, uint16_t) {
108 EXPECT_EQ(uint16_t(0x1122), sys::getSwappedBytes(uint16_t(0x2211)));
109 }
110
TEST(getSwappedBytes,uint32_t)111 TEST(getSwappedBytes, uint32_t) {
112 EXPECT_EQ(uint32_t(0x11223344), sys::getSwappedBytes(uint32_t(0x44332211)));
113 }
114
TEST(getSwappedBytes,uint64_t)115 TEST(getSwappedBytes, uint64_t) {
116 EXPECT_EQ(uint64_t(0x1122334455667788ULL),
117 sys::getSwappedBytes(uint64_t(0x8877665544332211ULL)));
118 }
119
TEST(getSwappedBytes,int8_t)120 TEST(getSwappedBytes, int8_t) {
121 EXPECT_EQ(int8_t(0x11), sys::getSwappedBytes(int8_t(0x11)));
122 }
123
TEST(getSwappedBytes,int16_t)124 TEST(getSwappedBytes, int16_t) {
125 EXPECT_EQ(int16_t(0x1122), sys::getSwappedBytes(int16_t(0x2211)));
126 }
127
TEST(getSwappedBytes,int32_t)128 TEST(getSwappedBytes, int32_t) {
129 EXPECT_EQ(int32_t(0x11223344), sys::getSwappedBytes(int32_t(0x44332211)));
130 }
131
TEST(getSwappedBytes,int64_t)132 TEST(getSwappedBytes, int64_t) {
133 EXPECT_EQ(int64_t(0x1122334455667788LL),
134 sys::getSwappedBytes(int64_t(0x8877665544332211LL)));
135 }
136
TEST(getSwappedBytes,float)137 TEST(getSwappedBytes, float) {
138 EXPECT_EQ(1.79366203433576585078237386661e-43f, sys::getSwappedBytes(-0.0f));
139 // 0x11223344
140 EXPECT_EQ(7.1653228759765625e2f, sys::getSwappedBytes(1.2795344e-28f));
141 }
142
TEST(getSwappedBytes,double)143 TEST(getSwappedBytes, double) {
144 EXPECT_EQ(6.32404026676795576546008054871e-322, sys::getSwappedBytes(-0.0));
145 // 0x1122334455667788
146 EXPECT_EQ(-7.08687663657301358331704585496e-268,
147 sys::getSwappedBytes(3.84141202447173065923064450234e-226));
148 }
149
TEST(swapByteOrder,uint8_t)150 TEST(swapByteOrder, uint8_t) {
151 uint8_t value = 0x11;
152 sys::swapByteOrder(value);
153 EXPECT_EQ(uint8_t(0x11), value);
154 }
155
TEST(swapByteOrder,uint16_t)156 TEST(swapByteOrder, uint16_t) {
157 uint16_t value = 0x2211;
158 sys::swapByteOrder(value);
159 EXPECT_EQ(uint16_t(0x1122), value);
160 }
161
TEST(swapByteOrder,uint32_t)162 TEST(swapByteOrder, uint32_t) {
163 uint32_t value = 0x44332211;
164 sys::swapByteOrder(value);
165 EXPECT_EQ(uint32_t(0x11223344), value);
166 }
167
TEST(swapByteOrder,uint64_t)168 TEST(swapByteOrder, uint64_t) {
169 uint64_t value = 0x8877665544332211ULL;
170 sys::swapByteOrder(value);
171 EXPECT_EQ(uint64_t(0x1122334455667788ULL), value);
172 }
173
TEST(swapByteOrder,int8_t)174 TEST(swapByteOrder, int8_t) {
175 int8_t value = 0x11;
176 sys::swapByteOrder(value);
177 EXPECT_EQ(int8_t(0x11), value);
178 }
179
TEST(swapByteOrder,int16_t)180 TEST(swapByteOrder, int16_t) {
181 int16_t value = 0x2211;
182 sys::swapByteOrder(value);
183 EXPECT_EQ(int16_t(0x1122), value);
184 }
185
TEST(swapByteOrder,int32_t)186 TEST(swapByteOrder, int32_t) {
187 int32_t value = 0x44332211;
188 sys::swapByteOrder(value);
189 EXPECT_EQ(int32_t(0x11223344), value);
190 }
191
TEST(swapByteOrder,int64_t)192 TEST(swapByteOrder, int64_t) {
193 int64_t value = 0x8877665544332211LL;
194 sys::swapByteOrder(value);
195 EXPECT_EQ(int64_t(0x1122334455667788LL), value);
196 }
197
TEST(swapByteOrder,float)198 TEST(swapByteOrder, float) {
199 float value = 7.1653228759765625e2f; // 0x44332211
200 sys::swapByteOrder(value);
201 EXPECT_EQ(1.2795344e-28f, value);
202 }
203
TEST(swapByteOrder,double)204 TEST(swapByteOrder, double) {
205 double value = -7.08687663657301358331704585496e-268; // 0x8877665544332211
206 sys::swapByteOrder(value);
207 EXPECT_EQ(3.84141202447173065923064450234e-226, value);
208 }
209
210 }
211