Skip to content

Commit 4c2429b

Browse files
committed
UnitUtilsTest: ensure test string formats match
Split the test strings to numeric and label components, and used the same formatting as used in UnitUtils. This should mitigate locale differences.
1 parent 1a58716 commit 4c2429b

File tree

1 file changed

+41
-34
lines changed

1 file changed

+41
-34
lines changed

src/test/java/org/scijava/util/UnitUtilsTest.java

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import static org.junit.Assert.assertEquals;
3535

3636
import org.junit.Test;
37-
import org.scijava.util.UnitUtils;
3837

3938
/**
4039
* Tests {@link UnitUtils}.
@@ -45,40 +44,48 @@ public class UnitUtilsTest {
4544

4645
@Test
4746
public void testGetAbbreviatedByteLabel() {
48-
assertEquals("0B", UnitUtils.getAbbreviatedByteLabel(0));
49-
assertEquals("1B", UnitUtils.getAbbreviatedByteLabel(1));
50-
assertEquals("123B", UnitUtils.getAbbreviatedByteLabel(123));
47+
assertEquals(local(0, "B"), UnitUtils.getAbbreviatedByteLabel(0));
48+
assertEquals(local(1, "B"), UnitUtils.getAbbreviatedByteLabel(1));
49+
assertEquals(local(123, "B"), UnitUtils.getAbbreviatedByteLabel(123));
5150

52-
assertEquals("10B", UnitUtils.getAbbreviatedByteLabel(1e1));
53-
assertEquals("100B", UnitUtils.getAbbreviatedByteLabel(1e2));
54-
assertEquals("1000B", UnitUtils.getAbbreviatedByteLabel(1e3));
55-
assertEquals("9.8KiB", UnitUtils.getAbbreviatedByteLabel(1e4));
56-
assertEquals("97.7KiB", UnitUtils.getAbbreviatedByteLabel(1e5));
57-
assertEquals("976.6KiB", UnitUtils.getAbbreviatedByteLabel(1e6));
58-
assertEquals("9.5MiB", UnitUtils.getAbbreviatedByteLabel(1e7));
59-
assertEquals("95.4MiB", UnitUtils.getAbbreviatedByteLabel(1e8));
60-
assertEquals("953.7MiB", UnitUtils.getAbbreviatedByteLabel(1e9));
61-
assertEquals("9.3GiB", UnitUtils.getAbbreviatedByteLabel(1e10));
62-
assertEquals("93.1GiB", UnitUtils.getAbbreviatedByteLabel(1e11));
63-
assertEquals("931.3GiB", UnitUtils.getAbbreviatedByteLabel(1e12));
64-
assertEquals("9.1TiB", UnitUtils.getAbbreviatedByteLabel(1e13));
65-
assertEquals("90.9TiB", UnitUtils.getAbbreviatedByteLabel(1e14));
66-
assertEquals("909.5TiB", UnitUtils.getAbbreviatedByteLabel(1e15));
67-
assertEquals("8.9PiB", UnitUtils.getAbbreviatedByteLabel(1e16));
68-
assertEquals("88.8PiB", UnitUtils.getAbbreviatedByteLabel(1e17));
69-
assertEquals("888.2PiB", UnitUtils.getAbbreviatedByteLabel(1e18));
70-
assertEquals("8.7EiB", UnitUtils.getAbbreviatedByteLabel(1e19));
71-
assertEquals("86.7EiB", UnitUtils.getAbbreviatedByteLabel(1e20));
72-
assertEquals("867.4EiB", UnitUtils.getAbbreviatedByteLabel(1e21));
73-
assertEquals("8.5ZiB", UnitUtils.getAbbreviatedByteLabel(1e22));
74-
assertEquals("84.7ZiB", UnitUtils.getAbbreviatedByteLabel(1e23));
75-
assertEquals("847.0ZiB", UnitUtils.getAbbreviatedByteLabel(1e24));
76-
assertEquals("8.3YiB", UnitUtils.getAbbreviatedByteLabel(1e25));
77-
assertEquals("82.7YiB", UnitUtils.getAbbreviatedByteLabel(1e26));
78-
assertEquals("827.2YiB", UnitUtils.getAbbreviatedByteLabel(1e27));
79-
assertEquals("8271.8YiB", UnitUtils.getAbbreviatedByteLabel(1e28));
80-
assertEquals("82718.1YiB", UnitUtils.getAbbreviatedByteLabel(1e29));
81-
assertEquals("827180.6YiB", UnitUtils.getAbbreviatedByteLabel(1e30));
51+
assertEquals(local(10, "B"), UnitUtils.getAbbreviatedByteLabel(1e1));
52+
assertEquals(local(100, "B"), UnitUtils.getAbbreviatedByteLabel(1e2));
53+
assertEquals(local(1000, "B"), UnitUtils.getAbbreviatedByteLabel(1e3));
54+
assertEquals(local(9.8, "KiB"), UnitUtils.getAbbreviatedByteLabel(1e4));
55+
assertEquals(local(97.7, "KiB"), UnitUtils.getAbbreviatedByteLabel(1e5));
56+
assertEquals(local(976.6, "KiB"), UnitUtils.getAbbreviatedByteLabel(1e6));
57+
assertEquals(local(9.5, "MiB"), UnitUtils.getAbbreviatedByteLabel(1e7));
58+
assertEquals(local(95.4, "MiB"), UnitUtils.getAbbreviatedByteLabel(1e8));
59+
assertEquals(local(953.7, "MiB"), UnitUtils.getAbbreviatedByteLabel(1e9));
60+
assertEquals(local(9.3, "GiB"), UnitUtils.getAbbreviatedByteLabel(1e10));
61+
assertEquals(local(93.1, "GiB"), UnitUtils.getAbbreviatedByteLabel(1e11));
62+
assertEquals(local(931.3, "GiB"), UnitUtils.getAbbreviatedByteLabel(1e12));
63+
assertEquals(local(9.1, "TiB"), UnitUtils.getAbbreviatedByteLabel(1e13));
64+
assertEquals(local(90.9, "TiB"), UnitUtils.getAbbreviatedByteLabel(1e14));
65+
assertEquals(local(909.5, "TiB"), UnitUtils.getAbbreviatedByteLabel(1e15));
66+
assertEquals(local(8.9, "PiB"), UnitUtils.getAbbreviatedByteLabel(1e16));
67+
assertEquals(local(88.8, "PiB"), UnitUtils.getAbbreviatedByteLabel(1e17));
68+
assertEquals(local(888.2, "PiB"), UnitUtils.getAbbreviatedByteLabel(1e18));
69+
assertEquals(local(8.7, "EiB"), UnitUtils.getAbbreviatedByteLabel(1e19));
70+
assertEquals(local(86.7, "EiB"), UnitUtils.getAbbreviatedByteLabel(1e20));
71+
assertEquals(local(867.4, "EiB"), UnitUtils.getAbbreviatedByteLabel(1e21));
72+
assertEquals(local(8.5, "ZiB"), UnitUtils.getAbbreviatedByteLabel(1e22));
73+
assertEquals(local(84.7, "ZiB"), UnitUtils.getAbbreviatedByteLabel(1e23));
74+
assertEquals(local(847.0, "ZiB"), UnitUtils.getAbbreviatedByteLabel(1e24));
75+
assertEquals(local(8.3, "YiB"), UnitUtils.getAbbreviatedByteLabel(1e25));
76+
assertEquals(local(82.7, "YiB"), UnitUtils.getAbbreviatedByteLabel(1e26));
77+
assertEquals(local(827.2, "YiB"), UnitUtils.getAbbreviatedByteLabel(1e27));
78+
assertEquals(local(8271.8, "YiB"), UnitUtils.getAbbreviatedByteLabel(1e28));
79+
assertEquals(local(82718.1, "YiB"), UnitUtils.getAbbreviatedByteLabel(1e29));
80+
assertEquals(local(827180.6, "YiB"), UnitUtils.getAbbreviatedByteLabel(1e30));
8281
}
8382

83+
/**
84+
* Helper method to ensure the strings tested here match the
85+
* {@link java.util.Locale} of the current JVM.
86+
*/
87+
private String local(final double value, final String unit) {
88+
final String format = UnitUtils.format(unit.equals("B") ? 0 : 1);
89+
return String.format(format, value, unit);
90+
}
8491
}

0 commit comments

Comments
 (0)