Skip to content

Commit 7f68326

Browse files
committed
Add test
1 parent 68cd0fc commit 7f68326

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

absl/strings/charconv_test.cc

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,4 +784,117 @@ TEST(FromChars, DecimalFloatLimits) {
784784
TestOverflowAndUnderflow<float>(input_gen, expected_gen, -45, 38);
785785
}
786786

787+
#if !defined(ABSL_INTERNAL_CPLUSPLUS_LANG) || \
788+
ABSL_INTERNAL_CPLUSPLUS_LANG < 202002L
789+
constexpr bool operator!=(const absl::from_chars_result& l,
790+
const absl::from_chars_result& r) noexcept {
791+
return !(l == r);
792+
}
793+
#endif
794+
// Check that the operator== for from_chars_result works as expected.
795+
//
796+
// EXPECT_TRUE and EXPECT_FALSE used intentionally to check operator== and
797+
// operator!= explicitly.
798+
// Before C++20 we need to define the operator!= explicitly, as it is not
799+
// automatically generated by the compiler.
800+
TEST(FromChars, ResultOperatorEqual) {
801+
{
802+
absl::from_chars_result result1{};
803+
absl::from_chars_result result2{};
804+
EXPECT_TRUE(result1 == result2);
805+
EXPECT_FALSE(result1 != result2);
806+
}
807+
808+
{
809+
absl::from_chars_result result1{};
810+
result1.ec = std::errc::result_out_of_range;
811+
absl::from_chars_result result2{};
812+
result2.ec = std::errc::result_out_of_range;
813+
EXPECT_TRUE(result1 == result2);
814+
EXPECT_FALSE(result1 != result2);
815+
}
816+
{
817+
absl::from_chars_result result1{};
818+
result1.ec = std::errc::result_out_of_range;
819+
absl::from_chars_result result2{};
820+
EXPECT_FALSE(result1 == result2);
821+
EXPECT_TRUE(result1 != result2);
822+
}
823+
{
824+
absl::from_chars_result result1{};
825+
absl::from_chars_result result2{};
826+
result2.ec = std::errc::result_out_of_range;
827+
EXPECT_FALSE(result1 == result2);
828+
EXPECT_TRUE(result1 != result2);
829+
}
830+
831+
{
832+
absl::from_chars_result result1{};
833+
result1.ptr = "";
834+
absl::from_chars_result result2{};
835+
result2.ptr = "";
836+
EXPECT_TRUE(result1 == result2);
837+
EXPECT_FALSE(result1 != result2);
838+
}
839+
{
840+
absl::from_chars_result result1{};
841+
result1.ptr = "";
842+
absl::from_chars_result result2{};
843+
EXPECT_FALSE(result1 == result2);
844+
EXPECT_TRUE(result1 != result2);
845+
}
846+
{
847+
absl::from_chars_result result1{};
848+
absl::from_chars_result result2{};
849+
result2.ptr = "";
850+
EXPECT_FALSE(result1 == result2);
851+
EXPECT_TRUE(result1 != result2);
852+
}
853+
854+
{
855+
absl::from_chars_result result1{"", std::errc::result_out_of_range};
856+
absl::from_chars_result result2{"", std::errc::result_out_of_range};
857+
EXPECT_TRUE(result1 == result2);
858+
EXPECT_FALSE(result1 != result2);
859+
}
860+
{
861+
absl::from_chars_result result1{"", std::errc::result_out_of_range};
862+
absl::from_chars_result result2{};
863+
EXPECT_FALSE(result1 == result2);
864+
EXPECT_TRUE(result1 != result2);
865+
}
866+
{
867+
absl::from_chars_result result1{};
868+
absl::from_chars_result result2{"", std::errc::result_out_of_range};
869+
EXPECT_FALSE(result1 == result2);
870+
EXPECT_TRUE(result1 != result2);
871+
}
872+
}
873+
874+
// Check that the operator bool for from_chars_result works as expected.
875+
TEST(FromChars, ResultOperatorBool) {
876+
{
877+
absl::from_chars_result result{};
878+
EXPECT_TRUE(result);
879+
EXPECT_FALSE(!result);
880+
}
881+
{
882+
absl::from_chars_result result{};
883+
result.ec = std::errc::result_out_of_range;
884+
EXPECT_FALSE(result);
885+
EXPECT_TRUE(!result);
886+
}
887+
{
888+
absl::from_chars_result result{};
889+
result.ptr = "";
890+
EXPECT_TRUE(result);
891+
EXPECT_FALSE(!result);
892+
}
893+
{
894+
absl::from_chars_result result{"", std::errc::result_out_of_range};
895+
EXPECT_FALSE(result);
896+
EXPECT_TRUE(!result);
897+
}
898+
}
899+
787900
} // namespace

0 commit comments

Comments
 (0)