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

source code of libc/test/src/__support/endian_test.cpp