Bump version to 19.1.0-rc3
[llvm-project.git] / llvm / unittests / Support / SwapByteOrderTest.cpp
blob9581f070475e14d9cca07cce4d3b2af13eda86ff
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 // In these first two tests all of the original_uintx values are truncated
20 // except for 64. We could avoid this, but there's really no point.
22 TEST(getSwappedBytes, UnsignedRoundTrip) {
23 // The point of the bit twiddling of magic is to test with and without bits
24 // in every byte.
25 uint64_t value = 1;
26 for (std::size_t i = 0; i <= sizeof(value); ++i) {
27 uint8_t original_uint8 = static_cast<uint8_t>(value);
28 EXPECT_EQ(original_uint8,
29 sys::getSwappedBytes(sys::getSwappedBytes(original_uint8)));
31 uint16_t original_uint16 = static_cast<uint16_t>(value);
32 EXPECT_EQ(original_uint16,
33 sys::getSwappedBytes(sys::getSwappedBytes(original_uint16)));
35 uint32_t original_uint32 = static_cast<uint32_t>(value);
36 EXPECT_EQ(original_uint32,
37 sys::getSwappedBytes(sys::getSwappedBytes(original_uint32)));
39 uint64_t original_uint64 = static_cast<uint64_t>(value);
40 EXPECT_EQ(original_uint64,
41 sys::getSwappedBytes(sys::getSwappedBytes(original_uint64)));
43 value = (value << 8) | 0x55; // binary 0101 0101.
47 TEST(getSwappedBytes, SignedRoundTrip) {
48 // The point of the bit twiddling of magic is to test with and without bits
49 // in every byte.
50 uint64_t value = 1;
51 for (std::size_t i = 0; i <= sizeof(value); ++i) {
52 int8_t original_int8 = static_cast<int8_t>(value);
53 EXPECT_EQ(original_int8,
54 sys::getSwappedBytes(sys::getSwappedBytes(original_int8)));
56 int16_t original_int16 = static_cast<int16_t>(value);
57 EXPECT_EQ(original_int16,
58 sys::getSwappedBytes(sys::getSwappedBytes(original_int16)));
60 int32_t original_int32 = static_cast<int32_t>(value);
61 EXPECT_EQ(original_int32,
62 sys::getSwappedBytes(sys::getSwappedBytes(original_int32)));
64 int64_t original_int64 = static_cast<int64_t>(value);
65 EXPECT_EQ(original_int64,
66 sys::getSwappedBytes(sys::getSwappedBytes(original_int64)));
68 // Test other sign.
69 value *= -1;
71 original_int8 = static_cast<int8_t>(value);
72 EXPECT_EQ(original_int8,
73 sys::getSwappedBytes(sys::getSwappedBytes(original_int8)));
75 original_int16 = static_cast<int16_t>(value);
76 EXPECT_EQ(original_int16,
77 sys::getSwappedBytes(sys::getSwappedBytes(original_int16)));
79 original_int32 = static_cast<int32_t>(value);
80 EXPECT_EQ(original_int32,
81 sys::getSwappedBytes(sys::getSwappedBytes(original_int32)));
83 original_int64 = static_cast<int64_t>(value);
84 EXPECT_EQ(original_int64,
85 sys::getSwappedBytes(sys::getSwappedBytes(original_int64)));
87 // Return to normal sign and twiddle.
88 value *= -1;
89 value = (value << 8) | 0x55; // binary 0101 0101.
93 TEST(getSwappedBytes, uint8_t) {
94 EXPECT_EQ(uint8_t(0x11), sys::getSwappedBytes(uint8_t(0x11)));
97 TEST(getSwappedBytes, uint16_t) {
98 EXPECT_EQ(uint16_t(0x1122), sys::getSwappedBytes(uint16_t(0x2211)));
101 TEST(getSwappedBytes, uint32_t) {
102 EXPECT_EQ(uint32_t(0x11223344), sys::getSwappedBytes(uint32_t(0x44332211)));
105 TEST(getSwappedBytes, uint64_t) {
106 EXPECT_EQ(uint64_t(0x1122334455667788ULL),
107 sys::getSwappedBytes(uint64_t(0x8877665544332211ULL)));
110 TEST(getSwappedBytes, int8_t) {
111 EXPECT_EQ(int8_t(0x11), sys::getSwappedBytes(int8_t(0x11)));
114 TEST(getSwappedBytes, int16_t) {
115 EXPECT_EQ(int16_t(0x1122), sys::getSwappedBytes(int16_t(0x2211)));
118 TEST(getSwappedBytes, int32_t) {
119 EXPECT_EQ(int32_t(0x11223344), sys::getSwappedBytes(int32_t(0x44332211)));
122 TEST(getSwappedBytes, int64_t) {
123 EXPECT_EQ(int64_t(0x1122334455667788LL),
124 sys::getSwappedBytes(int64_t(0x8877665544332211LL)));
127 TEST(getSwappedBytes, float) {
128 EXPECT_EQ(1.79366203433576585078237386661e-43f, sys::getSwappedBytes(-0.0f));
129 // 0x11223344
130 EXPECT_EQ(7.1653228759765625e2f, sys::getSwappedBytes(1.2795344e-28f));
133 TEST(getSwappedBytes, double) {
134 EXPECT_EQ(6.32404026676795576546008054871e-322, sys::getSwappedBytes(-0.0));
135 // 0x1122334455667788
136 EXPECT_EQ(-7.08687663657301358331704585496e-268,
137 sys::getSwappedBytes(3.84141202447173065923064450234e-226));
140 TEST(swapByteOrder, uint8_t) {
141 uint8_t value = 0x11;
142 sys::swapByteOrder(value);
143 EXPECT_EQ(uint8_t(0x11), value);
146 TEST(swapByteOrder, uint16_t) {
147 uint16_t value = 0x2211;
148 sys::swapByteOrder(value);
149 EXPECT_EQ(uint16_t(0x1122), value);
152 TEST(swapByteOrder, uint32_t) {
153 uint32_t value = 0x44332211;
154 sys::swapByteOrder(value);
155 EXPECT_EQ(uint32_t(0x11223344), value);
158 TEST(swapByteOrder, uint64_t) {
159 uint64_t value = 0x8877665544332211ULL;
160 sys::swapByteOrder(value);
161 EXPECT_EQ(uint64_t(0x1122334455667788ULL), value);
164 TEST(swapByteOrder, int8_t) {
165 int8_t value = 0x11;
166 sys::swapByteOrder(value);
167 EXPECT_EQ(int8_t(0x11), value);
170 TEST(swapByteOrder, int16_t) {
171 int16_t value = 0x2211;
172 sys::swapByteOrder(value);
173 EXPECT_EQ(int16_t(0x1122), value);
176 TEST(swapByteOrder, int32_t) {
177 int32_t value = 0x44332211;
178 sys::swapByteOrder(value);
179 EXPECT_EQ(int32_t(0x11223344), value);
182 TEST(swapByteOrder, int64_t) {
183 int64_t value = 0x8877665544332211LL;
184 sys::swapByteOrder(value);
185 EXPECT_EQ(int64_t(0x1122334455667788LL), value);
188 TEST(swapByteOrder, float) {
189 float value = 7.1653228759765625e2f; // 0x44332211
190 sys::swapByteOrder(value);
191 EXPECT_EQ(1.2795344e-28f, value);
194 TEST(swapByteOrder, double) {
195 double value = -7.08687663657301358331704585496e-268; // 0x8877665544332211
196 sys::swapByteOrder(value);
197 EXPECT_EQ(3.84141202447173065923064450234e-226, value);