Skip to content

Commit

Permalink
Fix the size formatter
Browse files Browse the repository at this point in the history
The default size formatter was giving results that were typically
off by orders of magnitude for different size values. This change
fixes the formatter to allow it to give the correct results for
up to thousands of terabytes.

Fix: #30
  • Loading branch information
io7m committed May 1, 2021
1 parent 1ea9115 commit a491354
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 8 deletions.
Expand Up @@ -97,19 +97,20 @@ public static JWFileSizeFormatterType fileSizeFormatter()
return Long.toUnsignedString(size) + "B";
}
if (Long.compareUnsigned(size, 1_000_000L) < 0) {
return String.format(
"%.2fkB", Double.valueOf(real / 1_000.0));
final var resultSize = real / 1_000.0;
return String.format("%.2fkB", Double.valueOf(resultSize));
}
if (Long.compareUnsigned(size, 1_000_000_000L) < 0) {
return String.format(
"%.2fMB", Double.valueOf(real / 1_000_000.0));
final var resultSize = real / 1_000_000.0;
return String.format("%.2fMB", Double.valueOf(resultSize));
}
if (Long.compareUnsigned(size, 1_000_000_000_000L) < 0) {
return String.format(
"%.2fGB", Double.valueOf(real / 1_000_000_000_000.0));
final var resultSize = real / 1_000_000_000.0;
return String.format("%.2fGB", Double.valueOf(resultSize));
}
return String.format(
"%.2fTB", Double.valueOf(real / 1_000_000_000_000.0));

final var resultSize = real / 1_000_000_000_000.0;
return String.format("%.2fTB", Double.valueOf(resultSize));
};
}
}
@@ -0,0 +1,78 @@
/*
* Copyright © 2021 Mark Raynsford <code@io7m.com> http://io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

package com.io7m.jwheatsheaf.tests;

import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;

import java.util.stream.Stream;

import static com.io7m.jwheatsheaf.api.JWFileChooserConfigurationDefaults.fileSizeFormatter;
import static org.junit.jupiter.api.Assertions.assertEquals;

public final class JWFileSizeFormatterTest
{
private static DynamicTest testOf(
final long size,
final String text)
{
return DynamicTest.dynamicTest(
String.format("test_FormatSize_%d_%s", Long.valueOf(size), text),
() -> {
assertEquals(text, fileSizeFormatter().formatSize(size));
}
);
}

/**
* Formatting a negative size yields the empty string.
*/

@Test
public void test_FormatSize_Negative_Empty()
{
assertEquals("", fileSizeFormatter().formatSize(-1L));
}

/**
* Formatting a range of sizes works correctly.
*
* @return A stream of tests
*/

@TestFactory
public Stream<DynamicTest> test_FormatSize_VariousUnits_CorrectString()
{
return Stream.of(
testOf(1L, "1B"),
testOf(1000L, "1.00kB"),
testOf(10_000L, "10.00kB"),
testOf(100_000L, "100.00kB"),
testOf(1_000_000L, "1.00MB"),
testOf(10_000_000L, "10.00MB"),
testOf(100_000_000L, "100.00MB"),
testOf(1_000_000_000L, "1.00GB"),
testOf(10_000_000_000L, "10.00GB"),
testOf(100_000_000_000L, "100.00GB"),
testOf(1000_000_000_000L, "1.00TB"),
testOf(10_000_000_000_000L, "10.00TB"),
testOf(100_000_000_000_000L, "100.00TB"),
testOf(1000_000_000_000_000L, "1000.00TB")
);
}
}

0 comments on commit a491354

Please sign in to comment.