BLI: fix fixed-width-int to string conversion

This commit is contained in:
Jacques Lucke
2024-03-26 14:25:33 +01:00
parent d3504a6bab
commit 4cdc62044e
2 changed files with 25 additions and 1 deletions

View File

@@ -63,7 +63,7 @@ template<typename T, int S> inline std::string UIntF<T, S>::to_string(const int
template<typename T, int S> inline std::string IntF<T, S>::to_string(const int base) const
{
if (is_negative(*this)) {
std::string str = UIntF<T, S>(-*this);
std::string str = UIntF<T, S>(-*this).to_string(base);
str.insert(str.begin(), '-');
return str;
}

View File

@@ -27,6 +27,30 @@ TEST(fixed_width_int, IsZero)
EXPECT_FALSE(is_zero(Int256(-10)));
}
TEST(fixed_width_int, ToString)
{
{
const std::string str = "4875677549274093345634534";
EXPECT_EQ(UInt256(str).to_string(), str);
}
{
const std::string str = "0";
EXPECT_EQ(UInt256(str).to_string(), str);
}
{
const std::string str = "4875677549274093345634534";
EXPECT_EQ(Int256(str).to_string(), str);
}
{
const std::string str = "-4875677549274093345634534";
EXPECT_EQ(Int256(str).to_string(), str);
}
{
const std::string str = "0";
EXPECT_EQ(Int256(str).to_string(), str);
}
}
TEST(fixed_width_int, Add256)
{
EXPECT_EQ(UInt256("290213998554153310989149424513459608072") +