mirror of https://github.com/grpc/grpc.git
The C based gRPC (C++, Python, Ruby, Objective-C, PHP, C#)
https://grpc.io/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
2.8 KiB
102 lines
2.8 KiB
2 years ago
|
//
|
||
|
//
|
||
|
// Copyright 2015 gRPC authors.
|
||
|
//
|
||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
// you may not use this file except in compliance with the License.
|
||
|
// You may obtain a copy of the License at
|
||
|
//
|
||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||
|
//
|
||
|
// Unless required by applicable law or agreed to in writing, software
|
||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
// See the License for the specific language governing permissions and
|
||
|
// limitations under the License.
|
||
|
//
|
||
|
//
|
||
10 years ago
|
|
||
7 months ago
|
#include "src/core/util/useful.h"
|
||
3 years ago
|
|
||
2 years ago
|
#include <stdint.h>
|
||
|
|
||
|
#include <limits>
|
||
1 year ago
|
#include <memory>
|
||
2 years ago
|
|
||
|
#include "gtest/gtest.h"
|
||
3 years ago
|
|
||
8 months ago
|
#include <grpc/support/port_platform.h>
|
||
|
|
||
3 years ago
|
namespace grpc_core {
|
||
10 years ago
|
|
||
3 years ago
|
TEST(UsefulTest, ClampWorks) {
|
||
3 years ago
|
EXPECT_EQ(Clamp(1, 0, 2), 1);
|
||
|
EXPECT_EQ(Clamp(0, 0, 2), 0);
|
||
|
EXPECT_EQ(Clamp(2, 0, 2), 2);
|
||
|
EXPECT_EQ(Clamp(-1, 0, 2), 0);
|
||
|
EXPECT_EQ(Clamp(3, 0, 2), 2);
|
||
3 years ago
|
}
|
||
|
|
||
|
TEST(UsefulTest, Rotate) {
|
||
3 years ago
|
EXPECT_EQ(RotateLeft(0x80000001u, 1u), 3);
|
||
|
EXPECT_EQ(RotateRight(0x80000001u, 1u), 0xc0000000);
|
||
3 years ago
|
}
|
||
|
|
||
|
TEST(UsefulTest, ArraySize) {
|
||
10 years ago
|
int four[4];
|
||
|
int five[5];
|
||
3 years ago
|
|
||
|
EXPECT_EQ(GPR_ARRAY_SIZE(four), 4);
|
||
|
EXPECT_EQ(GPR_ARRAY_SIZE(five), 5);
|
||
|
}
|
||
|
|
||
|
TEST(UsefulTest, BitOps) {
|
||
9 years ago
|
uint32_t bitset = 0;
|
||
3 years ago
|
|
||
3 years ago
|
EXPECT_EQ(BitCount((1u << 31) - 1), 31);
|
||
|
EXPECT_EQ(BitCount(1u << 3), 1);
|
||
|
EXPECT_EQ(BitCount(0), 0);
|
||
|
EXPECT_EQ(SetBit(&bitset, 3), 8);
|
||
|
EXPECT_EQ(BitCount(bitset), 1);
|
||
|
EXPECT_EQ(GetBit(bitset, 3), 1);
|
||
|
EXPECT_EQ(SetBit(&bitset, 1), 10);
|
||
|
EXPECT_EQ(BitCount(bitset), 2);
|
||
|
EXPECT_EQ(ClearBit(&bitset, 3), 2);
|
||
|
EXPECT_EQ(BitCount(bitset), 1);
|
||
|
EXPECT_EQ(GetBit(bitset, 3), 0);
|
||
|
EXPECT_EQ(BitCount(std::numeric_limits<uint64_t>::max()), 64);
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
TEST(UsefulTest, SaturatingAdd) {
|
||
|
EXPECT_EQ(SaturatingAdd(0, 0), 0);
|
||
|
EXPECT_EQ(SaturatingAdd(0, 1), 1);
|
||
|
EXPECT_EQ(SaturatingAdd(1, 0), 1);
|
||
|
EXPECT_EQ(SaturatingAdd(1, 1), 2);
|
||
|
EXPECT_EQ(SaturatingAdd(std::numeric_limits<int64_t>::max(), 1),
|
||
|
std::numeric_limits<int64_t>::max());
|
||
|
EXPECT_EQ(SaturatingAdd(std::numeric_limits<int64_t>::max(),
|
||
|
std::numeric_limits<int64_t>::max()),
|
||
|
std::numeric_limits<int64_t>::max());
|
||
|
EXPECT_EQ(SaturatingAdd(std::numeric_limits<int64_t>::min(), -1),
|
||
|
std::numeric_limits<int64_t>::min());
|
||
|
}
|
||
|
|
||
2 years ago
|
TEST(UsefulTest, RoundUpToPowerOf2) {
|
||
|
EXPECT_EQ(RoundUpToPowerOf2(0), 0);
|
||
|
EXPECT_EQ(RoundUpToPowerOf2(1), 1);
|
||
|
EXPECT_EQ(RoundUpToPowerOf2(2), 2);
|
||
|
EXPECT_EQ(RoundUpToPowerOf2(3), 4);
|
||
|
EXPECT_EQ(RoundUpToPowerOf2(4), 4);
|
||
|
EXPECT_EQ(RoundUpToPowerOf2(5), 8);
|
||
|
EXPECT_EQ(RoundUpToPowerOf2(6), 8);
|
||
|
EXPECT_EQ(RoundUpToPowerOf2(7), 8);
|
||
|
EXPECT_EQ(RoundUpToPowerOf2(8), 8);
|
||
|
}
|
||
|
|
||
3 years ago
|
} // namespace grpc_core
|
||
|
|
||
|
int main(int argc, char** argv) {
|
||
|
::testing::InitGoogleTest(&argc, argv);
|
||
|
return RUN_ALL_TESTS();
|
||
10 years ago
|
}
|