[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / llvm / unittests / Support / SwapByteOrderTest.cpp
blob85392fad67e614aced3e869c99b396334e1d26de
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 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/SwapByteOrder.h"
10 #include "gtest/gtest.h"
11 #include <cstdlib>
12 #include <ctime>
13 using namespace llvm;
15 #undef max
17 namespace {
19 TEST(ByteSwap, Swap_32) {
20 EXPECT_EQ(0x44332211u, ByteSwap_32(0x11223344));
21 EXPECT_EQ(0xDDCCBBAAu, ByteSwap_32(0xAABBCCDD));
24 TEST(ByteSwap, Swap_64) {
25 EXPECT_EQ(0x8877665544332211ULL, ByteSwap_64(0x1122334455667788LL));
26 EXPECT_EQ(0x1100FFEEDDCCBBAAULL, ByteSwap_64(0xAABBCCDDEEFF0011LL));
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.
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)));
41 uint16_t original_uint16 = static_cast<uint16_t>(value);
42 EXPECT_EQ(original_uint16,
43 sys::getSwappedBytes(sys::getSwappedBytes(original_uint16)));
45 uint32_t original_uint32 = static_cast<uint32_t>(value);
46 EXPECT_EQ(original_uint32,
47 sys::getSwappedBytes(sys::getSwappedBytes(original_uint32)));
49 uint64_t original_uint64 = static_cast<uint64_t>(value);
50 EXPECT_EQ(original_uint64,
51 sys::getSwappedBytes(sys::getSwappedBytes(original_uint64)));
53 value = (value << 8) | 0x55; // binary 0101 0101.
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)));
66 int16_t original_int16 = static_cast<int16_t>(value);
67 EXPECT_EQ(original_int16,
68 sys::getSwappedBytes(sys::getSwappedBytes(original_int16)));
70 int32_t original_int32 = static_cast<int32_t>(value);
71 EXPECT_EQ(original_int32,
72 sys::getSwappedBytes(sys::getSwappedBytes(original_int32)));
74 int64_t original_int64 = static_cast<int64_t>(value);
75 EXPECT_EQ(original_int64,
76 sys::getSwappedBytes(sys::getSwappedBytes(original_int64)));
78 // Test other sign.
79 value *= -1;
81 original_int8 = static_cast<int8_t>(value);
82 EXPECT_EQ(original_int8,
83 sys::getSwappedBytes(sys::getSwappedBytes(original_int8)));
85 original_int16 = static_cast<int16_t>(value);
86 EXPECT_EQ(original_int16,
87 sys::getSwappedBytes(sys::getSwappedBytes(original_int16)));
89 original_int32 = static_cast<int32_t>(value);
90 EXPECT_EQ(original_int32,
91 sys::getSwappedBytes(sys::getSwappedBytes(original_int32)));
93 original_int64 = static_cast<int64_t>(value);
94 EXPECT_EQ(original_int64,
95 sys::getSwappedBytes(sys::getSwappedBytes(original_int64)));
97 // Return to normal sign and twiddle.
98 value *= -1;
99 value = (value << 8) | 0x55; // binary 0101 0101.
103 TEST(getSwappedBytes, uint8_t) {
104 EXPECT_EQ(uint8_t(0x11), sys::getSwappedBytes(uint8_t(0x11)));
107 TEST(getSwappedBytes, uint16_t) {
108 EXPECT_EQ(uint16_t(0x1122), sys::getSwappedBytes(uint16_t(0x2211)));
111 TEST(getSwappedBytes, uint32_t) {
112 EXPECT_EQ(uint32_t(0x11223344), sys::getSwappedBytes(uint32_t(0x44332211)));
115 TEST(getSwappedBytes, uint64_t) {
116 EXPECT_EQ(uint64_t(0x1122334455667788ULL),
117 sys::getSwappedBytes(uint64_t(0x8877665544332211ULL)));
120 TEST(getSwappedBytes, int8_t) {
121 EXPECT_EQ(int8_t(0x11), sys::getSwappedBytes(int8_t(0x11)));
124 TEST(getSwappedBytes, int16_t) {
125 EXPECT_EQ(int16_t(0x1122), sys::getSwappedBytes(int16_t(0x2211)));
128 TEST(getSwappedBytes, int32_t) {
129 EXPECT_EQ(int32_t(0x11223344), sys::getSwappedBytes(int32_t(0x44332211)));
132 TEST(getSwappedBytes, int64_t) {
133 EXPECT_EQ(int64_t(0x1122334455667788LL),
134 sys::getSwappedBytes(int64_t(0x8877665544332211LL)));
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));
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));
150 TEST(swapByteOrder, uint8_t) {
151 uint8_t value = 0x11;
152 sys::swapByteOrder(value);
153 EXPECT_EQ(uint8_t(0x11), value);
156 TEST(swapByteOrder, uint16_t) {
157 uint16_t value = 0x2211;
158 sys::swapByteOrder(value);
159 EXPECT_EQ(uint16_t(0x1122), value);
162 TEST(swapByteOrder, uint32_t) {
163 uint32_t value = 0x44332211;
164 sys::swapByteOrder(value);
165 EXPECT_EQ(uint32_t(0x11223344), value);
168 TEST(swapByteOrder, uint64_t) {
169 uint64_t value = 0x8877665544332211ULL;
170 sys::swapByteOrder(value);
171 EXPECT_EQ(uint64_t(0x1122334455667788ULL), value);
174 TEST(swapByteOrder, int8_t) {
175 int8_t value = 0x11;
176 sys::swapByteOrder(value);
177 EXPECT_EQ(int8_t(0x11), value);
180 TEST(swapByteOrder, int16_t) {
181 int16_t value = 0x2211;
182 sys::swapByteOrder(value);
183 EXPECT_EQ(int16_t(0x1122), value);
186 TEST(swapByteOrder, int32_t) {
187 int32_t value = 0x44332211;
188 sys::swapByteOrder(value);
189 EXPECT_EQ(int32_t(0x11223344), value);
192 TEST(swapByteOrder, int64_t) {
193 int64_t value = 0x8877665544332211LL;
194 sys::swapByteOrder(value);
195 EXPECT_EQ(int64_t(0x1122334455667788LL), value);
198 TEST(swapByteOrder, float) {
199 float value = 7.1653228759765625e2f; // 0x44332211
200 sys::swapByteOrder(value);
201 EXPECT_EQ(1.2795344e-28f, value);
204 TEST(swapByteOrder, double) {
205 double value = -7.08687663657301358331704585496e-268; // 0x8877665544332211
206 sys::swapByteOrder(value);
207 EXPECT_EQ(3.84141202447173065923064450234e-226, value);