Debian Codegraph @

test/core/gpr/useful_test.cc

raw
1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <grpc/impl/codegen/port_platform.h>
20
21 #include "src/core/lib/gpr/useful.h"
22
23 #include <stdint.h>
24
25 #include <limits>
26
27 #include "gtest/gtest.h"
28
29 namespace grpc_core {
30
31 TEST(UsefulTest, ClampWorks) {
32 EXPECT_EQ(Clamp(1, 0, 2), 1);
33 EXPECT_EQ(Clamp(0, 0, 2), 0);
34 EXPECT_EQ(Clamp(2, 0, 2), 2);
35 EXPECT_EQ(Clamp(-1, 0, 2), 0);
36 EXPECT_EQ(Clamp(3, 0, 2), 2);
37 }
38
39 TEST(UsefulTest, Rotate) {
40 EXPECT_EQ(RotateLeft(0x80000001u, 1u), 3);
41 EXPECT_EQ(RotateRight(0x80000001u, 1u), 0xc0000000);
42 }
43
44 TEST(UsefulTest, ArraySize) {
45 int four[4];
46 int five[5];
47
48 EXPECT_EQ(GPR_ARRAY_SIZE(four), 4);
49 EXPECT_EQ(GPR_ARRAY_SIZE(five), 5);
50 }
51
52 TEST(UsefulTest, BitOps) {
53 uint32_t bitset = 0;
54
55 EXPECT_EQ(BitCount((1u << 31) - 1), 31);
56 EXPECT_EQ(BitCount(1u << 3), 1);
57 EXPECT_EQ(BitCount(0), 0);
58 EXPECT_EQ(SetBit(&bitset, 3), 8);
59 EXPECT_EQ(BitCount(bitset), 1);
60 EXPECT_EQ(GetBit(bitset, 3), 1);
61 EXPECT_EQ(SetBit(&bitset, 1), 10);
62 EXPECT_EQ(BitCount(bitset), 2);
63 EXPECT_EQ(ClearBit(&bitset, 3), 2);
64 EXPECT_EQ(BitCount(bitset), 1);
65 EXPECT_EQ(GetBit(bitset, 3), 0);
66 EXPECT_EQ(BitCount(std::numeric_limits<uint64_t>::max()), 64);
67 }
68
69 TEST(UsefulTest, SaturatingAdd) {
70 EXPECT_EQ(SaturatingAdd(0, 0), 0);
71 EXPECT_EQ(SaturatingAdd(0, 1), 1);
72 EXPECT_EQ(SaturatingAdd(1, 0), 1);
73 EXPECT_EQ(SaturatingAdd(1, 1), 2);
74 EXPECT_EQ(SaturatingAdd(std::numeric_limits<int64_t>::max(), 1),
75 std::numeric_limits<int64_t>::max());
76 EXPECT_EQ(SaturatingAdd(std::numeric_limits<int64_t>::max(),
77 std::numeric_limits<int64_t>::max()),
78 std::numeric_limits<int64_t>::max());
79 EXPECT_EQ(SaturatingAdd(std::numeric_limits<int64_t>::min(), -1),
80 std::numeric_limits<int64_t>::min());
81 }
82
83 TEST(UsefulTest, RoundUpToPowerOf2) {
84 EXPECT_EQ(RoundUpToPowerOf2(0), 0);
85 EXPECT_EQ(RoundUpToPowerOf2(1), 1);
86 EXPECT_EQ(RoundUpToPowerOf2(2), 2);
87 EXPECT_EQ(RoundUpToPowerOf2(3), 4);
88 EXPECT_EQ(RoundUpToPowerOf2(4), 4);
89 EXPECT_EQ(RoundUpToPowerOf2(5), 8);
90 EXPECT_EQ(RoundUpToPowerOf2(6), 8);
91 EXPECT_EQ(RoundUpToPowerOf2(7), 8);
92 EXPECT_EQ(RoundUpToPowerOf2(8), 8);
93 }
94
95 } // namespace grpc_core
96
97 int main(int argc, char** argv) {
98 ::testing::InitGoogleTest(&argc, argv);
99 return RUN_ALL_TESTS();
100 }