From: Guilherme Maciel Ferreira Date: Fri, 4 Feb 2011 11:25:39 +0000 (+0100) Subject: Removed the exabytes, zettabytes and yottabytes representations from nice_unit_format() X-Git-Tag: v2.6~86^2~3 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=83809f5e58c942899e7c63c54365c353123d4f5e;p=libi2ncommon Removed the exabytes, zettabytes and yottabytes representations from nice_unit_format() - Makes no sense keep those representations once int64 cannot handle them - Added a safe guard to rollback values that cannot be represented (ie 2000.00 PB instead of 2.00 EB) - Added a unit test to check how is represented the max int64 number --- diff --git a/src/stringfunc.cpp b/src/stringfunc.cpp index 03d362b..48823ec 100644 --- a/src/stringfunc.cpp +++ b/src/stringfunc.cpp @@ -892,7 +892,7 @@ string to_upper(const string &src) } #endif -const int MAX_UNIT_FORMAT_SYMBOLS = 9; +const int MAX_UNIT_FORMAT_SYMBOLS = 6; const string shortUnitFormatSymbols[MAX_UNIT_FORMAT_SYMBOLS] = { " B", @@ -900,10 +900,7 @@ const string shortUnitFormatSymbols[MAX_UNIT_FORMAT_SYMBOLS] = { " MB", " GB", " TB", - " PB", - " EB", - " ZB", - " YB" + " PB" }; const string longUnitFormatSymbols[MAX_UNIT_FORMAT_SYMBOLS] = { @@ -912,10 +909,7 @@ const string longUnitFormatSymbols[MAX_UNIT_FORMAT_SYMBOLS] = { i18n_noop(" MBytes"), i18n_noop(" GBytes"), i18n_noop(" TBytes"), - i18n_noop(" PBytes"), - i18n_noop(" EBytes"), - i18n_noop(" ZBytes"), - i18n_noop(" YBytes") + i18n_noop(" PBytes") }; @@ -959,6 +953,15 @@ string nice_unit_format( { size = size / multiple; sizecount++; + + // rollback to the previous values and stop the loop when cannot + // represent the number length. + if (sizecount >= MAX_UNIT_FORMAT_SYMBOLS) + { + size = size * multiple; + sizecount--; + break; + } } // round the input number "half up" to multiples of 10 diff --git a/test/stringfunc.cpp b/test/stringfunc.cpp index e379bc1..2e85aab 100644 --- a/test/stringfunc.cpp +++ b/test/stringfunc.cpp @@ -219,18 +219,29 @@ BOOST_AUTO_TEST_CASE(nice_unit_format7) const int64_t two_exabytes = 2000000000000000000LL; string output = nice_unit_format(two_exabytes); - BOOST_CHECK_EQUAL(string("2.00 EBytes"), output); + BOOST_CHECK_EQUAL(string("2000.00 PBytes"), output); output = nice_unit_format(two_exabytes, UnitBase1024, ShortUnitFormat); - BOOST_CHECK_EQUAL(string("1.7 EB"), output); + BOOST_CHECK_EQUAL(string("1776.4 PB"), output); const int64_t two_and_half_exabytes = 2500000000000000000LL; output = nice_unit_format(two_and_half_exabytes); - BOOST_CHECK_EQUAL(string("2.50 EBytes"), output); + BOOST_CHECK_EQUAL(string("2500.00 PBytes"), output); output = nice_unit_format(two_and_half_exabytes, UnitBase1024, ShortUnitFormat); - BOOST_CHECK_EQUAL(string("2.2 EB"), output); + BOOST_CHECK_EQUAL(string("2220.4 PB"), output); +} + +BOOST_AUTO_TEST_CASE(nice_unit_format8) +{ + const int64_t max_representable_64bits_number = 9223372036854775807LL; + + string output = nice_unit_format(max_representable_64bits_number); + BOOST_CHECK_EQUAL(string("9223.40 PBytes"), output); + + output = nice_unit_format(max_representable_64bits_number, UnitBase1024, ShortUnitFormat); + BOOST_CHECK_EQUAL(string("8192.0 PB"), output); }