Bug 779736 - FormatFileSize fails for sizes >999.5 GB; r=standard8
authorJim Porter <jporter@mozilla.com>
Sat, 18 Aug 2012 03:01:55 -0500
changeset 13289 0ce4d789f7ec7f8b324d8dd9866b23145b84a661
parent 13288 8261f4f34dcad8863abeb698fc7863b335e8a830
child 13290 5cd70ca479d1a05f2836b5f3da43b9a829429395
push id701
push userbugzilla@standard8.plus.com
push dateMon, 08 Oct 2012 19:06:59 +0000
treeherdercomm-beta@c9696e33af8e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersstandard8
bugs779736
Bug 779736 - FormatFileSize fails for sizes >999.5 GB; r=standard8
mailnews/base/test/unit/test_formatFileSize.js
mailnews/base/test/unit/xpcshell.ini
mailnews/base/util/nsMsgUtils.cpp
new file mode 100644
--- /dev/null
+++ b/mailnews/base/test/unit/test_formatFileSize.js
@@ -0,0 +1,63 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/*
+ * Tests for the formatFileSize method.
+ */
+
+load("../../../../mailnews/resources/logHelper.js");
+load("../../../../mailnews/resources/asyncTestUtils.js");
+
+const gStringBundle = Services.strings.createBundle(
+  "chrome://messenger/locale/messenger.properties");
+
+var gMessenger = Cc["@mozilla.org/messenger;1"]
+                   .createInstance(Ci.nsIMessenger);
+
+function test_formatFileSize(aArgs) {
+  const strings = {  b: "byteAbbreviation2",
+                    kb: "kiloByteAbbreviation2",
+                    mb: "megaByteAbbreviation2",
+                    gb: "gigaByteAbbreviation2" };
+
+  let actual = gMessenger.formatFileSize(aArgs.bytes, aArgs.useKB);
+  let expected = gStringBundle.GetStringFromName(strings[aArgs.units])
+                              .replace("%.*f", aArgs.mantissa);
+  do_check_eq(actual, expected);
+}
+
+/* ===== Driver ===== */
+
+var test_data = [
+  { bytes:                   0, useKB: false, mantissa:    "0", units:  "b" },
+  { bytes:                   1, useKB: false, mantissa:    "1", units:  "b" },
+  { bytes:                  10, useKB: false, mantissa:   "10", units:  "b" },
+  { bytes:                 999, useKB: false, mantissa:  "999", units:  "b" },
+  { bytes:                1000, useKB: false, mantissa:  "1.0", units: "kb" },
+  { bytes:                1024, useKB: false, mantissa:  "1.0", units: "kb" },
+  { bytes:             10*1024, useKB: false, mantissa: "10.0", units: "kb" },
+  { bytes:            999*1024, useKB: false, mantissa:  "999", units: "kb" },
+  { bytes:           1000*1024, useKB: false, mantissa:  "1.0", units: "mb" },
+  { bytes:           1024*1024, useKB: false, mantissa:  "1.0", units: "mb" },
+  { bytes:        10*1024*1024, useKB: false, mantissa: "10.0", units: "mb" },
+  { bytes:       999*1024*1024, useKB: false, mantissa:  "999", units: "mb" },
+  { bytes:      1000*1024*1024, useKB: false, mantissa:  "1.0", units: "gb" },
+  { bytes:      1024*1024*1024, useKB: false, mantissa:  "1.0", units: "gb" },
+  { bytes:   10*1024*1024*1024, useKB: false, mantissa: "10.0", units: "gb" },
+  { bytes:  999*1024*1024*1024, useKB: false, mantissa:  "999", units: "gb" },
+  { bytes: 1000*1024*1024*1024, useKB: false, mantissa: "1000", units: "gb" },
+
+  { bytes:                   0, useKB: true,  mantissa:    "0", units: "kb" },
+  { bytes:                   1, useKB: true,  mantissa:  "0.1", units: "kb" },
+  { bytes:                 500, useKB: true,  mantissa:  "0.5", units: "kb" },
+  { bytes:                 999, useKB: true,  mantissa:  "1.0", units: "kb" },
+];
+
+var tests = [
+  parameterizeTest(test_formatFileSize, test_data)
+];
+
+function run_test() {
+  async_run_tests(tests);
+}
--- a/mailnews/base/test/unit/xpcshell.ini
+++ b/mailnews/base/test/unit/xpcshell.ini
@@ -57,8 +57,9 @@ skip-if = os != 'mac'
 [test_searchCustomTerm.js]
 [test_searchJunk.js]
 [test_searchLocalizationStrings.js]
 [test_searchTag.js]
 [test_searchUint32HdrProperty.js]
 [test_testsuite_base64.js]
 [test_testsuite_fakeserverAuth.js]
 [test_viewSortByAddresses.js]
+[test_formatFileSize.js]
--- a/mailnews/base/util/nsMsgUtils.cpp
+++ b/mailnews/base/util/nsMsgUtils.cpp
@@ -492,41 +492,40 @@ nsresult FormatFileSize(PRUint64 size, b
   NS_ENSURE_SUCCESS(rv, rv);
 
   float unitSize = size;
   PRUint32 unitIndex = 0;
 
   if (useKB) {
     // Start by formatting in kilobytes
     unitSize /= 1024;
-    if (unitSize < 0.1)
+    if (unitSize < 0.1 && unitSize != 0)
       unitSize = 0.1;
     unitIndex++;
   }
 
   // Convert to next unit if it needs 4 digits (after rounding), but only if
   // we know the name of the next unit
-  while ((unitSize >= 999.5) && (unitIndex < ArrayLength(sizeAbbrNames)))
+  while ((unitSize >= 999.5) && (unitIndex < ArrayLength(sizeAbbrNames) - 1))
   {
       unitSize /= 1024;
       unitIndex++;
   }
 
   // Grab the string for the appropriate unit
   nsString sizeAbbr;
   rv = bundle->GetStringFromName(sizeAbbrNames[unitIndex],
                                  getter_Copies(sizeAbbr));
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Get rid of insignificant bits by truncating to 1 or 0 decimal points
   // 0.1 -> 0.1; 1.2 -> 1.2; 12.3 -> 12.3; 123.4 -> 123; 234.5 -> 235
-  nsTextFormatter::ssprintf(formattedSize,
-                            sizeAbbr.get(),
-                            (unitIndex != 0) && (unitSize < 99.95) ? 1 : 0,
-                            unitSize);
+  nsTextFormatter::ssprintf(
+    formattedSize, sizeAbbr.get(),
+    (unitIndex != 0) && (unitSize < 99.95 && unitSize != 0) ? 1 : 0, unitSize);
   return NS_OK;
 }
 
 
 nsresult NS_MsgCreatePathStringFromFolderURI(const char *aFolderURI,
                                              nsCString& aPathCString,
                                              const nsCString &aScheme,
                                              bool aIsNewsFolder)