SizeFormat { public static String format(long bytesCount) { Preconditions.checkArgument(bytesCount >= 0, "Formatting of a negative size is forbidden"); return Unit.locateUnit(bytesCount) .format(bytesCount); }  static String format(long bytesCount); static long parseAsByteCount(String bytesWithUnit);  }
@Test void formatShouldThrowWhenNegative() { assertThatThrownBy(() -> SizeFormat.format(-1)) .isInstanceOf(IllegalArgumentException.class); }
@Test void formatShouldAcceptZero() { assertThat(SizeFormat.format(0)) .isEqualTo("0 bytes"); }
@Test void formatShouldUseByteWhenAlmostKiB() { assertThat(SizeFormat.format(1023)) .isEqualTo("1023 bytes"); }
@Test void formatShouldUseKiBWhenExactlyOneKiB() { assertThat(SizeFormat.format(1024)) .isEqualTo("1 KiB"); }
@Test void formatShouldHaveTwoDigitPrecision() { assertThat(SizeFormat.format(1024 + 100)) .isEqualTo("1.09 KiB"); }
@Test void formatShouldBeExpressedInKiBWhenAlmostMiB() { assertThat(SizeFormat.format(1024 * 1024 - 1)) .isEqualTo("1023.99 KiB"); }
@Test void formatShouldKeepTwoDigitPrecisionWhenRoundingDown() { assertThat(SizeFormat.format(2 * 1024 * 1024 - 1)) .isEqualTo("1.99 MiB"); }
@Test void formatShouldUseMiBWhenExactlyOneMiB() { assertThat(SizeFormat.format(1024 * 1024)) .isEqualTo("1 MiB"); }
@Test void formatShouldUseGiBWhenExactlyOneGiB() { assertThat(SizeFormat.format(1024 * 1024 * 1024)) .isEqualTo("1 GiB"); }
@Test void formatShouldUseTiBWhenExactlyOneTiB() { assertThat(SizeFormat.format(1024L * 1024L * 1024L * 1024L)) .isEqualTo("1 TiB"); }