1 | //===-- Unittests for endian ----------------------------------------------===// |
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 "src/__support/endian_internal.h" |
10 | #include "src/__support/macros/config.h" |
11 | #include "test/UnitTest/Test.h" |
12 | |
13 | namespace LIBC_NAMESPACE_DECL { |
14 | |
15 | struct LlvmLibcEndian : testing::Test { |
16 | template <typename T> void check(const T original, const T swapped) { |
17 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
18 | EXPECT_EQ(Endian::to_little_endian(original), original); |
19 | EXPECT_EQ(Endian::to_big_endian(original), swapped); |
20 | #endif |
21 | #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
22 | EXPECT_EQ(Endian::to_big_endian(original), original); |
23 | EXPECT_EQ(Endian::to_little_endian(original), swapped); |
24 | #endif |
25 | } |
26 | }; |
27 | |
28 | TEST_F(LlvmLibcEndian, Field) { |
29 | EXPECT_EQ(Endian::IS_LITTLE, __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__); |
30 | EXPECT_EQ(Endian::IS_BIG, __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__); |
31 | } |
32 | |
33 | TEST_F(LlvmLibcEndian, uint8_t) { |
34 | const uint8_t original = uint8_t(0x12); |
35 | check(original, original); |
36 | } |
37 | |
38 | TEST_F(LlvmLibcEndian, uint16_t) { |
39 | const uint16_t original = uint16_t(0x1234); |
40 | const uint16_t swapped = __builtin_bswap16(original); |
41 | check(original, swapped); |
42 | } |
43 | |
44 | TEST_F(LlvmLibcEndian, uint32_t) { |
45 | const uint32_t original = uint32_t(0x12345678); |
46 | const uint32_t swapped = __builtin_bswap32(original); |
47 | check(original, swapped); |
48 | } |
49 | |
50 | TEST_F(LlvmLibcEndian, uint64_t) { |
51 | const uint64_t original = uint64_t(0x123456789ABCDEF0); |
52 | const uint64_t swapped = __builtin_bswap64(original); |
53 | check(original, swapped); |
54 | } |
55 | |
56 | } // namespace LIBC_NAMESPACE_DECL |
57 | |