/* * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree. */ #include "logging/rtc_event_log/encoder/rtc_event_log_encoder_common.h" #include #include #include #include #include "test/gtest.h" namespace webrtc_event_logging { namespace { template class SignednessConversionTest : public ::testing::Test { public: static_assert(std::is_integral::value, ""); static_assert(std::is_signed::value, ""); }; TYPED_TEST_SUITE_P(SignednessConversionTest); TYPED_TEST_P(SignednessConversionTest, CorrectlyConvertsLegalValues) { using T = TypeParam; std::vector legal_values = {std::numeric_limits::min(), std::numeric_limits::min() + 1, -1, 0, 1, std::numeric_limits::max() - 1, std::numeric_limits::max()}; for (T val : legal_values) { const auto unsigned_val = ToUnsigned(val); T signed_val; ASSERT_TRUE(ToSigned(unsigned_val, &signed_val)) << "Failed on " << static_cast(unsigned_val) << "."; EXPECT_EQ(val, signed_val) << "Failed on " << static_cast(unsigned_val) << "."; } } TYPED_TEST_P(SignednessConversionTest, FailsOnConvertingIllegalValues) { using T = TypeParam; // Note that a signed integer whose width is N bits, has N-1 digits. constexpr bool width_is_64 = std::numeric_limits::digits == 63; if (width_is_64) { return; // Test irrelevant; illegal values do not exist. } const uint64_t max_legal_value = ToUnsigned(static_cast(-1)); const std::vector illegal_values = { max_legal_value + 1u, max_legal_value + 2u, std::numeric_limits::max() - 1u, std::numeric_limits::max()}; for (uint64_t unsigned_val : illegal_values) { T signed_val; EXPECT_FALSE(ToSigned(unsigned_val, &signed_val)) << "Failed on " << static_cast(unsigned_val) << "."; } } REGISTER_TYPED_TEST_SUITE_P(SignednessConversionTest, CorrectlyConvertsLegalValues, FailsOnConvertingIllegalValues); using Types = ::testing::Types; INSTANTIATE_TYPED_TEST_SUITE_P(_, SignednessConversionTest, Types); } // namespace } // namespace webrtc_event_logging