Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4059d7db authored by Roozbeh Pournader's avatar Roozbeh Pournader
Browse files

Update tests and support negative values in formatBytes().

This updates the tests to match the older commit
f7230174 and also adds support for
negative values.

Bug: 23191381
Bug: 11237727
Change-Id: Ib9a1f10031b730ee460658949b388c779cefa041
parent 2fce18d9
Loading
Loading
Loading
Loading
+7 −2
Original line number Diff line number Diff line
@@ -98,7 +98,8 @@ public final class Formatter {

    /** {@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 @@ public final class Formatter {
                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
+11 −9
Original line number Diff line number Diff line
@@ -56,14 +56,14 @@ public class FormatterTest extends AndroidTestCase {
    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 @@ public class FormatterTest extends AndroidTestCase {
        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.