Update tests and support negative values in formatBytes().
This updates the tests to match the older commit
f723017481a247b4aa842937c3983b7d7d8f75fd and also adds support for
negative values.
Bug: 23191381
Bug: 11237727
Change-Id: Ib9a1f10031b730ee460658949b388c779cefa041
diff --git a/core/java/android/text/format/Formatter.java b/core/java/android/text/format/Formatter.java
index a53da09..9f5dfa6 100644
--- a/core/java/android/text/format/Formatter.java
+++ b/core/java/android/text/format/Formatter.java
@@ -98,7 +98,8 @@
/** {@hide} */
public static BytesResult formatBytes(Resources res, long sizeBytes, int flags) {
- float result = sizeBytes;
+ final boolean isNegative = (sizeBytes < 0);
+ float result = isNegative ? -sizeBytes : sizeBytes;
int suffix = com.android.internal.R.string.byteShort;
long mult = 1;
if (result > 900) {
@@ -154,9 +155,13 @@
roundFormat = "%.2f";
}
}
+
+ if (isNegative) {
+ result = -result;
+ }
final String roundedString = String.format(roundFormat, result);
- // Note this might overflow if result >= Long.MAX_VALUE / 100, but that's like 80PB so
+ // Note this might overflow if abs(result) >= Long.MAX_VALUE / 100, but that's like 80PB so
// it's okay (for now)...
final long roundedBytes =
(flags & FLAG_CALCULATE_ROUNDED) == 0 ? 0
diff --git a/core/tests/coretests/src/android/text/format/FormatterTest.java b/core/tests/coretests/src/android/text/format/FormatterTest.java
index d2e2131..be6e7ea 100644
--- a/core/tests/coretests/src/android/text/format/FormatterTest.java
+++ b/core/tests/coretests/src/android/text/format/FormatterTest.java
@@ -56,14 +56,14 @@
public void testFormatBytes() {
setLocale(Locale.ENGLISH);
- checkFormatBytes(0, true, "0.00", 0);
- checkFormatBytes(0, false, "0.00", 0);
+ checkFormatBytes(0, true, "0", 0);
+ checkFormatBytes(0, false, "0", 0);
- checkFormatBytes(1, true, "1.0", 1);
- checkFormatBytes(1, false, "1.00", 1);
+ checkFormatBytes(1, true, "1", 1);
+ checkFormatBytes(1, false, "1", 1);
checkFormatBytes(12, true, "12", 12);
- checkFormatBytes(12, false, "12.00", 12);
+ checkFormatBytes(12, false, "12", 12);
checkFormatBytes(123, true, "123", 123);
checkFormatBytes(123, false, "123", 123);
@@ -80,13 +80,15 @@
checkFormatBytes(9123000, true, "8.7", 9122611);
checkFormatBytes(9123000, false, "8.70", 9122611);
- // The method doesn't really support negative values, but apparently people pass -1...
- checkFormatBytes(-1, true, "-1.00", -1);
- checkFormatBytes(-1, false, "-1.00", -1);
+ checkFormatBytes(-1, true, "-1", -1);
+ checkFormatBytes(-1, false, "-1", -1);
+
+ checkFormatBytes(-912, true, "-0.89", -911);
+ checkFormatBytes(-912, false, "-0.89", -911);
// Missing FLAG_CALCULATE_ROUNDED case.
BytesResult r = Formatter.formatBytes(getContext().getResources(), 1, 0);
- assertEquals("1.00", r.value);
+ assertEquals("1", r.value);
assertEquals(0, r.roundedBytes); // Didn't pass FLAG_CALCULATE_ROUNDED
// Make sure it works on different locales.