1+ #include < cmath>
12#include < limits>
23
34#include < gmock/gmock.h>
5+ #include < gtest/gtest.h>
46#include < userver/utest/assert_macros.hpp>
57
68#include < userver/utils/numeric_cast.hpp>
@@ -9,6 +11,29 @@ USERVER_NAMESPACE_BEGIN
911
1012namespace {
1113
14+ template <typename TFrom, typename TTo>
15+ struct CastTypes {
16+ using From = TFrom;
17+ using To = TTo;
18+ };
19+
20+ using FloatingPointCastTypes = ::testing::Types<
21+ CastTypes<float , float >,
22+ CastTypes<float , double >,
23+ CastTypes<float , long double >,
24+ CastTypes<double , float >,
25+ CastTypes<double , double >,
26+ CastTypes<double , long double >,
27+ CastTypes<long double , float >,
28+ CastTypes<long double , double >,
29+ CastTypes<long double , long double >>;
30+
31+ template <typename CastTypes>
32+ class NumericCastFloatingPoint : public ::testing::Test {
33+ using From = typename CastTypes::From;
34+ using To = typename CastTypes::To;
35+ };
36+
1237TEST (NumericCast, CompileTime) {
1338 static_assert (utils::numeric_cast<unsigned int >(1 ) == 1u );
1439 static_assert (utils::numeric_cast<float >(1.0 ) == 1 .0f );
@@ -33,6 +58,29 @@ TEST(NumericCast, Smoke) {
3358 EXPECT_EQ (utils::numeric_cast<long double >(1 .0L ), 1 .0L );
3459}
3560
61+ TYPED_TEST_SUITE (NumericCastFloatingPoint, FloatingPointCastTypes);
62+
63+ TYPED_TEST (NumericCastFloatingPoint, Nan) {
64+ using Limits = std::numeric_limits<typename TypeParam::From>;
65+
66+ if constexpr (Limits::has_quiet_NaN) {
67+ EXPECT_TRUE (std::isnan (utils::numeric_cast<typename TypeParam::To>(Limits::quiet_NaN ())));
68+ }
69+ }
70+
71+ TYPED_TEST (NumericCastFloatingPoint, Infinity) {
72+ using To = typename TypeParam::To;
73+ using FromLimits = std::numeric_limits<typename TypeParam::From>;
74+ using ToLimits = std::numeric_limits<To>;
75+
76+ if constexpr (FromLimits::has_infinity) {
77+ // +inf
78+ EXPECT_EQ (utils::numeric_cast<To>(FromLimits::infinity ()), ToLimits::infinity ());
79+ // -inf
80+ EXPECT_EQ (utils::numeric_cast<To>(-FromLimits::infinity ()), -ToLimits::infinity ());
81+ }
82+ }
83+
3684TEST (NumericCast, SignedToUnsignedOverflow) {
3785 // / [Sample utils::numeric_cast usage]
3886 EXPECT_EQ (utils::numeric_cast<std::uint16_t >(0xffff ), 0xffffu );
0 commit comments