Skip to content

Commit ef46e64

Browse files
test: Add tests for handling out-of-range floating-point inputs in primitive types
Signed-off-by: FrozenlemonTee <1115306170@qq.com>
1 parent e30cc4c commit ef46e64

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

tests/basic/test_operations.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,22 @@ TEST(OperationsTest, PrimitiveStoreAndCasAllowTransparentCrossCategory) {
445445
EXPECT_EQ(expected, 99.0);
446446
}
447447

448+
TEST(OperationsTest, PrimitiveTransparentStoreClampsOutOfRangeFloatingInput) {
449+
using transparent_t =
450+
primitive<int, policy::type::transparent, policy::error::expected>;
451+
452+
auto value = transparent_t{0};
453+
454+
value.store(std::numeric_limits<double>::infinity());
455+
EXPECT_EQ(value.load(), std::numeric_limits<int>::max());
456+
457+
value.store(-std::numeric_limits<double>::infinity());
458+
EXPECT_EQ(value.load(), std::numeric_limits<int>::lowest());
459+
460+
value.store(std::numeric_limits<double>::quiet_NaN());
461+
EXPECT_EQ(value.load(), 0);
462+
}
463+
448464
TEST(OperationsTest, PrimitiveSpecialMembersSupportCrossUnderlyingWithCompatibleType) {
449465
using dst_t =
450466
primitive<int, policy::type::compatible, policy::error::expected>;
@@ -858,6 +874,29 @@ TEST(OperationsTest, CompoundAssignmentKeepsLhsWhenOperationFails) {
858874
EXPECT_EQ(value.load(), 100);
859875
}
860876

877+
TEST(OperationsTest,
878+
CompoundAssignmentCheckedRejectsFloatingToIntegralOutOfRange) {
879+
using lhs_t = primitive<int, policy::value::checked, policy::type::transparent,
880+
policy::error::expected>;
881+
using rhs_t =
882+
primitive<double, policy::value::checked, policy::type::transparent,
883+
policy::error::expected>;
884+
885+
auto value = lhs_t{7};
886+
887+
auto const overflow_result =
888+
operations::add_assign(value, rhs_t{std::numeric_limits<double>::max()});
889+
ASSERT_FALSE(overflow_result.has_value());
890+
EXPECT_EQ(overflow_result.error(), policy::error::kind::overflow);
891+
EXPECT_EQ(value.load(), 7);
892+
893+
auto const domain_result = operations::add_assign(
894+
value, rhs_t{std::numeric_limits<double>::quiet_NaN()});
895+
ASSERT_FALSE(domain_result.has_value());
896+
EXPECT_EQ(domain_result.error(), policy::error::kind::domain_error);
897+
EXPECT_EQ(value.load(), 7);
898+
}
899+
861900
TEST(OperationsTest, CompoundAssignmentSupportsMixedTypesWithCompatibleTypePolicy) {
862901
using namespace mcpplibs::primitives::operators;
863902

0 commit comments

Comments
 (0)