1/*
2 * Copyright 2011-present Facebook, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <folly/lang/Bits.h>
18
19#include <folly/portability/GTest.h>
20
21using namespace folly;
22
23TEST(Endian, Basic) {
24 uint8_t v8 = 0x12;
25 uint8_t v8s = 0x12;
26 uint16_t v16 = 0x1234;
27 uint16_t v16s = 0x3412;
28 uint32_t v32 = 0x12345678;
29 uint32_t v32s = 0x78563412;
30 uint64_t v64 = 0x123456789abcdef0ULL;
31 uint64_t v64s = 0xf0debc9a78563412ULL;
32
33#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
34
35#define GEN1(sz) \
36 EXPECT_EQ(v##sz, Endian::little(v##sz)); \
37 EXPECT_EQ(v##sz, Endian::little##sz(v##sz)); \
38 EXPECT_EQ(v##sz##s, Endian::big(v##sz)); \
39 EXPECT_EQ(v##sz##s, Endian::big##sz(v##sz));
40
41#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
42
43#define GEN1(sz) \
44 EXPECT_EQ(v##sz##s, Endian::little(v##sz)); \
45 EXPECT_EQ(v##sz##s, Endian::little##sz(v##sz)); \
46 EXPECT_EQ(v##sz, Endian::big(v##sz)); \
47 EXPECT_EQ(v##sz, Endian::big##sz(v##sz));
48
49#else
50#error Your machine uses a weird endianness!
51#endif /* __BYTE_ORDER__ */
52
53#define GEN(sz) \
54 EXPECT_EQ(v##sz##s, Endian::swap(v##sz)); \
55 EXPECT_EQ(v##sz##s, Endian::swap##sz(v##sz)); \
56 GEN1(sz);
57
58 GEN(8);
59 GEN(16)
60 GEN(32)
61 GEN(64)
62
63#undef GEN
64#undef GEN1
65}
66