Fix tiny bug in double 2 string formatting

- I forgot to handle the trailing dots when I added the fmt variant
- UIDouble2Str (the original) lacked the comma check
- Add unit test lol
This commit is contained in:
Marek Roszko 2023-01-21 13:54:07 -05:00
parent 2b128c79f2
commit b2421c7d9f
3 changed files with 45 additions and 3 deletions

View File

@ -152,6 +152,13 @@ std::string EDA_UNIT_UTILS::FormatInternalUnits( const EDA_IU_SCALE& aIuScale, i
{
buf.pop_back();
}
// if the value was really small
// we may have just stripped all the zeros after the decimal
if( buf[buf.size() - 1] == '.' )
{
buf.pop_back();
}
}
else
{

View File

@ -1104,11 +1104,18 @@ std::string FormatDouble2Str( double aValue )
{
buf = fmt::format( "{:.16f}", aValue );
// remove trailing zeros
// remove trailing zeros (and the decimal marker if needed)
while( !buf.empty() && buf[buf.size() - 1] == '0' )
{
buf.pop_back();
}
// if the value was really small
// we may have just stripped all the zeros after the decimal
if( buf[buf.size() - 1] == '.' )
{
buf.pop_back();
}
}
else
{
@ -1133,7 +1140,7 @@ std::string UIDouble2Str( double aValue )
while( --len > 0 && buf[len] == '0' )
buf[len] = '\0';
if( buf[len] == '.' )
if( buf[len] == '.' || buf[len] == ',' )
buf[len] = '\0';
else
++len;

View File

@ -36,6 +36,7 @@
*/
BOOST_AUTO_TEST_SUITE( KicadString )
/**
* Test the #GetTrailingInt method.
*/
@ -58,6 +59,7 @@ BOOST_AUTO_TEST_CASE( TrailingInt )
}
}
/**
* Test the #StrNumCmp method.
*/
@ -113,4 +115,30 @@ BOOST_AUTO_TEST_CASE( NaturalNumberCompare )
}
}
BOOST_AUTO_TEST_SUITE_END()
/**
* Test the #GetTrailingInt method.
*/
BOOST_AUTO_TEST_CASE( Double2Str )
{
using CASE = std::pair<double, std::string>;
// conceptually a little quirky because doubles do have all those pesky additional values
const std::vector<CASE> cases = {
{ 0, "0" },
{ 1.000, "1" },
{ 1.050, "1.05" }, // single trailing zero
{ 0.00001523, "0.00001523" }, // value less than the magic 0.0001 threshold
{ 0.00000000000000001523, "0" }, // really small decimal that gets cut off
{ 623523, "623523" }, // large whole number
};
for( const auto& c : cases )
{
// Test both of these functions that work the same but the innards are different
BOOST_CHECK_EQUAL( FormatDouble2Str( c.first ), c.second );
BOOST_CHECK_EQUAL( UIDouble2Str( c.first ), c.second );
}
}
BOOST_AUTO_TEST_SUITE_END()