/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * 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. */ #include #include #include #include using namespace ::testing; namespace apache::thrift::util { namespace { template void basicOpsTest() { EXPECT_EQ(43, add_saturating(42, 1)); EXPECT_EQ(41, add_saturating(42, -1)); EXPECT_EQ(-43, add_saturating(-42, -1)); EXPECT_EQ(-41, add_saturating(-42, 1)); } template void boundaryOpsTest() { EXPECT_EQ( std::numeric_limits::max(), add_saturating(std::numeric_limits::max(), 1)); EXPECT_EQ( std::numeric_limits::max() - 1, add_saturating(std::numeric_limits::max(), -1)); EXPECT_EQ( 0, add_saturating( std::numeric_limits::max(), -std::numeric_limits::max())); EXPECT_EQ( std::numeric_limits::lowest(), add_saturating(std::numeric_limits::lowest(), -1)); EXPECT_EQ( std::numeric_limits::lowest() + 1, add_saturating(std::numeric_limits::lowest(), 1)); if constexpr (std::is_floating_point_v) { EXPECT_TRUE(std::isnan(add_saturating(NAN, 1))); EXPECT_TRUE(std::isnan(add_saturating(1, NAN))); EXPECT_TRUE(std::isinf(add_saturating(INFINITY, 1))); EXPECT_EQ(add_saturating(INFINITY, 1), INFINITY); EXPECT_TRUE(std::isinf(add_saturating(1, INFINITY))); EXPECT_EQ(add_saturating(1, INFINITY), INFINITY); EXPECT_TRUE(std::isnan(add_saturating(INFINITY, -INFINITY))); EXPECT_TRUE(std::isinf(add_saturating(1, -INFINITY))); EXPECT_EQ(add_saturating(1, -INFINITY), -INFINITY); EXPECT_TRUE(std::isinf(add_saturating(-INFINITY, 1))); EXPECT_EQ(add_saturating(-INFINITY, 1), -INFINITY); } } template class SaturatingMatchTest : public testing::Test {}; } // namespace using Types = ::testing::Types; TYPED_TEST_SUITE(SaturatingMatchTest, Types); TYPED_TEST(SaturatingMatchTest, Basic) { basicOpsTest(); } TYPED_TEST(SaturatingMatchTest, Boundary) { boundaryOpsTest(); } } // namespace apache::thrift::util