1 //===-- Unittests for endian ----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "src/__support/endian_internal.h"
10 #include "src/__support/macros/config.h"
11 #include "test/UnitTest/Test.h"
13 namespace LIBC_NAMESPACE_DECL
{
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
);
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
);
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__
);
33 TEST_F(LlvmLibcEndian
, uint8_t) {
34 const uint8_t original
= uint8_t(0x12);
35 check(original
, original
);
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
);
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
);
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
);
56 } // namespace LIBC_NAMESPACE_DECL