summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse
diff options
context:
space:
mode:
authorThomas Wolf <thomas.wolf@paranor.ch>2021-10-30 19:37:44 +0200
committerThomas Wolf <thomas.wolf@paranor.ch>2021-10-30 23:05:22 +0200
commit3444a3be8c8a567f944fd7b81838e615852d787a (patch)
tree3c5565b2966fff8a8225fbfb3d2c95e9c0dbc254 /org.eclipse.jgit.test/tst/org/eclipse
parentc2204bb6835e4e6dc666bb34eaea910fb1484092 (diff)
downloadjgit-3444a3be8c8a567f944fd7b81838e615852d787a.tar.gz
jgit-3444a3be8c8a567f944fd7b81838e615852d787a.zip
Factor out parsing git-style size numbers to StringUtils
Move the code to parse numbers with an optional 'k', 'm', or 'g' suffix from the config file handling to StringUtils. This enables me to re-use it in EGit, which has duplicate code in StorageSizeFieldEditor. As this is generally useful functionality, providing it in the library makes sense. Change-Id: I86e4f5f62e14f99b35726b198ba3bbf1669418d9 Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
Diffstat (limited to 'org.eclipse.jgit.test/tst/org/eclipse')
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/util/StringUtilsTest.java83
1 files changed, 83 insertions, 0 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/StringUtilsTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/StringUtilsTest.java
index 82c0afec5f..aa7247e105 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/StringUtilsTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/StringUtilsTest.java
@@ -12,6 +12,7 @@ package org.eclipse.jgit.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
@@ -70,4 +71,86 @@ public class StringUtilsTest {
assertEquals("a b c d",
StringUtils.replaceLineBreaksWithSpace("a\r\nb\nc d"));
}
+
+ @Test
+ public void testFormatWithSuffix() {
+ assertEquals("1023", StringUtils.formatWithSuffix(1023));
+ assertEquals("1k", StringUtils.formatWithSuffix(1024));
+ assertEquals("1025", StringUtils.formatWithSuffix(1025));
+ assertEquals("1048575", StringUtils.formatWithSuffix(1024 * 1024 - 1));
+ assertEquals("1m", StringUtils.formatWithSuffix(1024 * 1024));
+ assertEquals("1048577", StringUtils.formatWithSuffix(1024 * 1024 + 1));
+ assertEquals("1073741823",
+ StringUtils.formatWithSuffix(1024 * 1024 * 1024 - 1));
+ assertEquals("1g", StringUtils.formatWithSuffix(1024 * 1024 * 1024));
+ assertEquals("1073741825",
+ StringUtils.formatWithSuffix(1024 * 1024 * 1024 + 1));
+ assertEquals("3k", StringUtils.formatWithSuffix(3 * 1024));
+ assertEquals("3m", StringUtils.formatWithSuffix(3 * 1024 * 1024));
+ assertEquals("2050k",
+ StringUtils.formatWithSuffix(2 * 1024 * 1024 + 2048));
+ assertEquals("3g",
+ StringUtils.formatWithSuffix(3L * 1024 * 1024 * 1024));
+ assertEquals("3000", StringUtils.formatWithSuffix(3000));
+ assertEquals("3000000", StringUtils.formatWithSuffix(3_000_000));
+ assertEquals("1953125k", StringUtils.formatWithSuffix(2_000_000_000));
+ assertEquals("2000000010", StringUtils.formatWithSuffix(2_000_000_010));
+ assertEquals("3000000000",
+ StringUtils.formatWithSuffix(3_000_000_000L));
+ }
+
+ @Test
+ public void testParseWithSuffix() {
+ assertEquals(1024, StringUtils.parseIntWithSuffix("1k", true));
+ assertEquals(1024, StringUtils.parseIntWithSuffix("1 k", true));
+ assertEquals(1024, StringUtils.parseIntWithSuffix("1 k", true));
+ assertEquals(1024, StringUtils.parseIntWithSuffix(" \t1 k \n", true));
+ assertEquals(1024, StringUtils.parseIntWithSuffix("1k", false));
+ assertEquals(1024, StringUtils.parseIntWithSuffix("1K", false));
+ assertEquals(1024 * 1024, StringUtils.parseIntWithSuffix("1m", false));
+ assertEquals(1024 * 1024, StringUtils.parseIntWithSuffix("1M", false));
+ assertEquals(-1024 * 1024,
+ StringUtils.parseIntWithSuffix("-1M", false));
+ assertEquals(1_000_000,
+ StringUtils.parseIntWithSuffix(" 1000000\r\n", false));
+ assertEquals(1024 * 1024 * 1024,
+ StringUtils.parseIntWithSuffix("1g", false));
+ assertEquals(1024 * 1024 * 1024,
+ StringUtils.parseIntWithSuffix("1G", false));
+ assertEquals(3L * 1024 * 1024 * 1024,
+ StringUtils.parseLongWithSuffix("3g", false));
+ assertEquals(3L * 1024 * 1024 * 1024,
+ StringUtils.parseLongWithSuffix("3G", false));
+ assertThrows(NumberFormatException.class,
+ () -> StringUtils.parseIntWithSuffix("2G", false));
+ assertEquals(2L * 1024 * 1024 * 1024,
+ StringUtils.parseLongWithSuffix("2G", false));
+ assertThrows(NumberFormatException.class,
+ () -> StringUtils.parseLongWithSuffix("-1m", true));
+ assertThrows(NumberFormatException.class,
+ () -> StringUtils.parseLongWithSuffix("-1000", true));
+ assertThrows(StringIndexOutOfBoundsException.class,
+ () -> StringUtils.parseLongWithSuffix("", false));
+ assertThrows(StringIndexOutOfBoundsException.class,
+ () -> StringUtils.parseLongWithSuffix(" \t \n", false));
+ assertThrows(StringIndexOutOfBoundsException.class,
+ () -> StringUtils.parseLongWithSuffix("k", false));
+ assertThrows(StringIndexOutOfBoundsException.class,
+ () -> StringUtils.parseLongWithSuffix("m", false));
+ assertThrows(StringIndexOutOfBoundsException.class,
+ () -> StringUtils.parseLongWithSuffix("g", false));
+ assertThrows(NumberFormatException.class,
+ () -> StringUtils.parseLongWithSuffix("1T", false));
+ assertThrows(NumberFormatException.class,
+ () -> StringUtils.parseLongWithSuffix("1t", false));
+ assertThrows(NumberFormatException.class,
+ () -> StringUtils.parseLongWithSuffix("Nonumber", false));
+ assertThrows(NumberFormatException.class,
+ () -> StringUtils.parseLongWithSuffix("0x001f", false));
+ assertThrows(NumberFormatException.class,
+ () -> StringUtils.parseLongWithSuffix("beef", false));
+ assertThrows(NumberFormatException.class,
+ () -> StringUtils.parseLongWithSuffix("8000000000000000000G",
+ false));
+ }
}