useful_test.cc
Go to the documentation of this file.
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 
20 
22 
23 #include <gtest/gtest.h>
24 
25 namespace grpc_core {
26 
27 TEST(UsefulTest, ClampWorks) {
28  EXPECT_EQ(Clamp(1, 0, 2), 1);
29  EXPECT_EQ(Clamp(0, 0, 2), 0);
30  EXPECT_EQ(Clamp(2, 0, 2), 2);
31  EXPECT_EQ(Clamp(-1, 0, 2), 0);
32  EXPECT_EQ(Clamp(3, 0, 2), 2);
33 }
34 
35 TEST(UsefulTest, Rotate) {
36  EXPECT_EQ(RotateLeft(0x80000001u, 1u), 3);
37  EXPECT_EQ(RotateRight(0x80000001u, 1u), 0xc0000000);
38 }
39 
40 TEST(UsefulTest, ArraySize) {
41  int four[4];
42  int five[5];
43 
44  EXPECT_EQ(GPR_ARRAY_SIZE(four), 4);
45  EXPECT_EQ(GPR_ARRAY_SIZE(five), 5);
46 }
47 
48 TEST(UsefulTest, BitOps) {
49  uint32_t bitset = 0;
50 
51  EXPECT_EQ(BitCount((1u << 31) - 1), 31);
52  EXPECT_EQ(BitCount(1u << 3), 1);
53  EXPECT_EQ(BitCount(0), 0);
54  EXPECT_EQ(SetBit(&bitset, 3), 8);
55  EXPECT_EQ(BitCount(bitset), 1);
56  EXPECT_EQ(GetBit(bitset, 3), 1);
57  EXPECT_EQ(SetBit(&bitset, 1), 10);
58  EXPECT_EQ(BitCount(bitset), 2);
59  EXPECT_EQ(ClearBit(&bitset, 3), 2);
60  EXPECT_EQ(BitCount(bitset), 1);
61  EXPECT_EQ(GetBit(bitset, 3), 0);
63 }
64 
65 TEST(UsefulTest, SaturatingAdd) {
66  EXPECT_EQ(SaturatingAdd(0, 0), 0);
67  EXPECT_EQ(SaturatingAdd(0, 1), 1);
68  EXPECT_EQ(SaturatingAdd(1, 0), 1);
69  EXPECT_EQ(SaturatingAdd(1, 1), 2);
77 }
78 
79 } // namespace grpc_core
80 
81 int main(int argc, char** argv) {
82  ::testing::InitGoogleTest(&argc, argv);
83  return RUN_ALL_TESTS();
84 }
main
int main(int argc, char **argv)
Definition: useful_test.cc:81
grpc_core
Definition: call_metric_recorder.h:31
useful.h
u
OPENSSL_EXPORT pem_password_cb void * u
Definition: pem.h:351
grpc_core::TEST
TEST(AvlTest, NoOp)
Definition: avl_test.cc:21
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
grpc_core::GetBit
bool GetBit(T i, size_t n)
Definition: useful.h:61
grpc_core::RotateRight
constexpr T RotateRight(T x, T n)
Definition: useful.h:43
grpc_core::ClearBit
T ClearBit(T *i, size_t n)
Definition: useful.h:55
grpc_core::SaturatingAdd
int64_t SaturatingAdd(int64_t a, int64_t b)
Definition: useful.h:112
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
min
#define min(a, b)
Definition: qsort.h:83
GPR_ARRAY_SIZE
#define GPR_ARRAY_SIZE(array)
Definition: useful.h:129
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
grpc_core::Clamp
T Clamp(T val, T min, T max)
Definition: useful.h:31
port_platform.h
absl::hash_internal::Rotate
static uint64_t Rotate(uint64_t val, int shift)
Definition: abseil-cpp/absl/hash/internal/city.cc:196
grpc_core::BitCount
constexpr uint32_t BitCount(uint32_t i)
Definition: useful.h:72
grpc_core::SetBit
T SetBit(T *i, size_t n)
Definition: useful.h:49
grpc_core::RotateLeft
constexpr T RotateLeft(T x, T n)
Definition: useful.h:39


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:48