Removed the exabytes, zettabytes and yottabytes representations from nice_unit_format()
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Fri, 4 Feb 2011 11:25:39 +0000 (12:25 +0100)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Fri, 4 Feb 2011 11:25:39 +0000 (12:25 +0100)
- 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

src/stringfunc.cpp
test/stringfunc.cpp

index 03d362b..48823ec 100644 (file)
@@ -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
index e379bc1..2e85aab 100644 (file)
@@ -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);
 }