1 /**
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "utils/bit_utils.h"
17
18 #include <cstdint>
19
20 #include <gtest/gtest.h>
21
22 namespace panda::test {
23
TEST(BitUtils,ReverseBytesUint32)24 TEST(BitUtils, ReverseBytesUint32)
25 {
26 ASSERT_EQ(ReverseBytes(0x33221100U), 0x00112233U);
27 ASSERT_EQ(ReverseBytes(0U), 0);
28 ASSERT_EQ(ReverseBytes(0xffffffffU), 0xffffffffU);
29 }
30
TEST(BitUtils,ReverseBytesUint64)31 TEST(BitUtils, ReverseBytesUint64)
32 {
33 ASSERT_EQ(ReverseBytes(UINT64_C(0x7766554433221100)), UINT64_C(0x0011223344556677));
34 ASSERT_EQ(ReverseBytes(UINT64_C(0)), 0);
35 ASSERT_EQ(ReverseBytes(UINT64_C(0xffffffffffffffff)), UINT64_C(0xffffffffffffffff));
36 }
37
TEST(BitUtils,ReverseBitsUint32)38 TEST(BitUtils, ReverseBitsUint32)
39 {
40 ASSERT_EQ(ReverseBits(0x33221100U), 0x008844ccU);
41 ASSERT_EQ(ReverseBits(0U), 0);
42 ASSERT_EQ(ReverseBits(0xffffffffU), 0xffffffffU);
43 }
44
TEST(BitUtils,ReverseBitsUint64)45 TEST(BitUtils, ReverseBitsUint64)
46 {
47 ASSERT_EQ(ReverseBits(UINT64_C(0x7766554433221100)), UINT64_C(0x008844cc22aa66ee));
48 ASSERT_EQ(ReverseBits(UINT64_C(0)), 0);
49 ASSERT_EQ(ReverseBits(UINT64_C(0xffffffffffffffff)), UINT64_C(0xffffffffffffffff));
50 }
51
TEST(BitUtils,RoundUp)52 TEST(BitUtils, RoundUp)
53 {
54 ASSERT_EQ(RoundUp(0, 4), 0);
55 ASSERT_EQ(RoundUp(8, 4), 8);
56 ASSERT_EQ(RoundUp(7, 4), 8);
57 ASSERT_EQ(RoundUp(5, 8), 8);
58 }
59
TEST(BitUtils,RoundDown)60 TEST(BitUtils, RoundDown)
61 {
62 ASSERT_EQ(RoundDown(0, 4), 0);
63 ASSERT_EQ(RoundDown(3, 4), 0);
64 ASSERT_EQ(RoundDown(8, 4), 8);
65 }
66
67 } // namespace panda::test
68