1 | //===- endian_test.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 | // This file is a part of the ORC runtime. |
10 | // |
11 | // Adapted from the llvm/unittests/Support/SwapByteOrderTest.cpp LLVM unit test. |
12 | // |
13 | //===----------------------------------------------------------------------===// |
14 | |
15 | #include "endianness.h" |
16 | #include "gtest/gtest.h" |
17 | |
18 | using namespace __orc_rt; |
19 | |
20 | TEST(Endian, ByteSwap_32) { |
21 | EXPECT_EQ(0x44332211u, ByteSwap_32(0x11223344)); |
22 | EXPECT_EQ(0xDDCCBBAAu, ByteSwap_32(0xAABBCCDD)); |
23 | } |
24 | |
25 | TEST(Endian, ByteSwap_64) { |
26 | EXPECT_EQ(0x8877665544332211ULL, ByteSwap_64(0x1122334455667788LL)); |
27 | EXPECT_EQ(0x1100FFEEDDCCBBAAULL, ByteSwap_64(0xAABBCCDDEEFF0011LL)); |
28 | } |
29 | |
30 | // In these first two tests all of the original_uintx values are truncated |
31 | // except for 64. We could avoid this, but there's really no point. |
32 | TEST(Endian, 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, getSwappedBytes(getSwappedBytes(original_uint8))); |
39 | |
40 | uint16_t original_uint16 = static_cast<uint16_t>(value); |
41 | EXPECT_EQ(original_uint16, |
42 | getSwappedBytes(getSwappedBytes(original_uint16))); |
43 | |
44 | uint32_t original_uint32 = static_cast<uint32_t>(value); |
45 | EXPECT_EQ(original_uint32, |
46 | getSwappedBytes(getSwappedBytes(original_uint32))); |
47 | |
48 | uint64_t original_uint64 = static_cast<uint64_t>(value); |
49 | EXPECT_EQ(original_uint64, |
50 | getSwappedBytes(getSwappedBytes(original_uint64))); |
51 | |
52 | value = (value << 8) | 0x55; // binary 0101 0101. |
53 | } |
54 | } |
55 | |
56 | TEST(Endian, getSwappedBytes_SignedRoundTrip) { |
57 | // The point of the bit twiddling of magic is to test with and without bits |
58 | // in every byte. |
59 | uint64_t value = 1; |
60 | for (std::size_t i = 0; i <= sizeof(value); ++i) { |
61 | int8_t original_int8 = static_cast<int8_t>(value); |
62 | EXPECT_EQ(original_int8, getSwappedBytes(getSwappedBytes(original_int8))); |
63 | |
64 | int16_t original_int16 = static_cast<int16_t>(value); |
65 | EXPECT_EQ(original_int16, getSwappedBytes(getSwappedBytes(original_int16))); |
66 | |
67 | int32_t original_int32 = static_cast<int32_t>(value); |
68 | EXPECT_EQ(original_int32, getSwappedBytes(getSwappedBytes(original_int32))); |
69 | |
70 | int64_t original_int64 = static_cast<int64_t>(value); |
71 | EXPECT_EQ(original_int64, getSwappedBytes(getSwappedBytes(original_int64))); |
72 | |
73 | // Test other sign. |
74 | value *= -1; |
75 | |
76 | original_int8 = static_cast<int8_t>(value); |
77 | EXPECT_EQ(original_int8, getSwappedBytes(getSwappedBytes(original_int8))); |
78 | |
79 | original_int16 = static_cast<int16_t>(value); |
80 | EXPECT_EQ(original_int16, getSwappedBytes(getSwappedBytes(original_int16))); |
81 | |
82 | original_int32 = static_cast<int32_t>(value); |
83 | EXPECT_EQ(original_int32, getSwappedBytes(getSwappedBytes(original_int32))); |
84 | |
85 | original_int64 = static_cast<int64_t>(value); |
86 | EXPECT_EQ(original_int64, getSwappedBytes(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(Endian, getSwappedBytes_uint8_t) { |
95 | EXPECT_EQ(uint8_t(0x11), getSwappedBytes(uint8_t(0x11))); |
96 | } |
97 | |
98 | TEST(Endian, getSwappedBytes_uint16_t) { |
99 | EXPECT_EQ(uint16_t(0x1122), getSwappedBytes(uint16_t(0x2211))); |
100 | } |
101 | |
102 | TEST(Endian, getSwappedBytes_uint32_t) { |
103 | EXPECT_EQ(uint32_t(0x11223344), getSwappedBytes(uint32_t(0x44332211))); |
104 | } |
105 | |
106 | TEST(Endian, getSwappedBytes_uint64_t) { |
107 | EXPECT_EQ(uint64_t(0x1122334455667788ULL), |
108 | getSwappedBytes(uint64_t(0x8877665544332211ULL))); |
109 | } |
110 | |
111 | TEST(Endian, getSwappedBytes_int8_t) { |
112 | EXPECT_EQ(int8_t(0x11), getSwappedBytes(int8_t(0x11))); |
113 | } |
114 | |
115 | TEST(Endian, getSwappedBytes_int16_t) { |
116 | EXPECT_EQ(int16_t(0x1122), getSwappedBytes(int16_t(0x2211))); |
117 | } |
118 | |
119 | TEST(Endian, getSwappedBytes_int32_t) { |
120 | EXPECT_EQ(int32_t(0x11223344), getSwappedBytes(int32_t(0x44332211))); |
121 | } |
122 | |
123 | TEST(Endian, getSwappedBytes_int64_t) { |
124 | EXPECT_EQ(int64_t(0x1122334455667788LL), |
125 | getSwappedBytes(int64_t(0x8877665544332211LL))); |
126 | } |
127 | |
128 | TEST(Endian, swapByteOrder_uint8_t) { |
129 | uint8_t value = 0x11; |
130 | swapByteOrder(value); |
131 | EXPECT_EQ(uint8_t(0x11), value); |
132 | } |
133 | |
134 | TEST(Endian, swapByteOrder_uint16_t) { |
135 | uint16_t value = 0x2211; |
136 | swapByteOrder(value); |
137 | EXPECT_EQ(uint16_t(0x1122), value); |
138 | } |
139 | |
140 | TEST(Endian, swapByteOrder_uint32_t) { |
141 | uint32_t value = 0x44332211; |
142 | swapByteOrder(value); |
143 | EXPECT_EQ(uint32_t(0x11223344), value); |
144 | } |
145 | |
146 | TEST(Endian, swapByteOrder_uint64_t) { |
147 | uint64_t value = 0x8877665544332211ULL; |
148 | swapByteOrder(value); |
149 | EXPECT_EQ(uint64_t(0x1122334455667788ULL), value); |
150 | } |
151 | |
152 | TEST(Endian, swapByteOrder_int8_t) { |
153 | int8_t value = 0x11; |
154 | swapByteOrder(value); |
155 | EXPECT_EQ(int8_t(0x11), value); |
156 | } |
157 | |
158 | TEST(Endian, swapByteOrder_int16_t) { |
159 | int16_t value = 0x2211; |
160 | swapByteOrder(value); |
161 | EXPECT_EQ(int16_t(0x1122), value); |
162 | } |
163 | |
164 | TEST(Endian, swapByteOrder_int32_t) { |
165 | int32_t value = 0x44332211; |
166 | swapByteOrder(value); |
167 | EXPECT_EQ(int32_t(0x11223344), value); |
168 | } |
169 | |
170 | TEST(Endian, swapByteOrder_int64_t) { |
171 | int64_t value = 0x8877665544332211LL; |
172 | swapByteOrder(value); |
173 | EXPECT_EQ(int64_t(0x1122334455667788LL), value); |
174 | } |
175 | |