Navigation Menu

Skip to content

Commit

Permalink
[JENKINS-31021] HexifiableString should have consistent String format…
Browse files Browse the repository at this point in the history
…ting.

Added toFormattedHexString to HexifiableString
  • Loading branch information
Thierry Lacour committed Oct 21, 2015
1 parent ccf1a65 commit f761bad
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
18 changes: 17 additions & 1 deletion src/main/java/net/praqma/jenkins/memorymap/util/HexUtils.java
Expand Up @@ -26,6 +26,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;

/**
* @author Praqma
Expand All @@ -40,7 +41,7 @@ public class HexUtils {
private static final double MEGA = KILO*1024;
private static final double GIGA = MEGA*1024;

private static final Map<String, Double> scale = new HashMap<String, Double>();
private static final Map<String, Double> scale = new HashMap<>();
private static final Pattern VALID_HEX = Pattern.compile("^[0xX]*[0-9a-fA-F]+$");
private static final Pattern VALID_NUMERICAL = Pattern.compile("^\\d+[mMgGkK]+$");

Expand Down Expand Up @@ -137,5 +138,20 @@ public int compareTo(HexifiableString t) {
return 0;
}
}

/**
* Returns a new HexifiableString with a formatted rawString.
* Format: 0x\d{8}
* @return A HexifiableString with a formatted rawString.
*/
public HexifiableString toFormattedHexString() {
HexifiableString hexString = toValidHexString();
String baseString = hexString.rawString.replace("0x", "");

int targetLength = ((baseString.length() - 1) | 7) + 1; // Next multiple of 8.
String formattedRawString = "0x" + StringUtils.leftPad(baseString, targetLength, "0");

return new HexifiableString(formattedRawString);
}
}
}
28 changes: 27 additions & 1 deletion src/test/java/net/praqma/jenkins/unit/HexUtilsTest.java
Expand Up @@ -23,6 +23,8 @@
*/
package net.praqma.jenkins.unit;

import java.util.HashMap;
import java.util.Map;
import net.praqma.jenkins.memorymap.util.HexUtils;
import org.junit.After;
import org.junit.AfterClass;
Expand Down Expand Up @@ -148,5 +150,29 @@ public void testValidHexStrings() {
HexUtils.HexifiableString metricToHex = new HexUtils.HexifiableString("2m");
HexUtils.HexifiableString hexified = metricToHex.toValidHexString();
}


@Test
public void testFormatting() {
Map<String, String> stringTests = new HashMap<String,String>(){{
put("0x00123456", "0x00123456");
put("7fc3", "0x00007fc3");
put("64M", "0x04000000");
}};
for (Map.Entry<String, String> test : stringTests.entrySet()) {
HexUtils.HexifiableString hexString = new HexUtils.HexifiableString(test.getKey()).toValidHexString();
assertTrue("Failure for test '" + test.getKey() + "': Not a valid hex string.", hexString.isValidHexString());
assertEquals("Failure for test '" + test.getKey() + "': Expected formatted hex string.", test.getValue(), hexString.toFormattedHexString().rawString);
}

Map<Integer, String> intTests = new HashMap<Integer,String>(){{
put(1,"0x00000001");
put(316, "0x0000013c");
put(2000000000, "0x77359400");
}};
for (Map.Entry<Integer, String> test : intTests.entrySet()) {
HexUtils.HexifiableString hexString = new HexUtils.HexifiableString(test.getKey()).toValidHexString();
assertTrue("Failure for test '" + test.getKey() + "': Not a valid hex string.", hexString.isValidHexString());
assertEquals("Failure for test '" + test.getKey() + "': Expected formatted hex string.", test.getValue(), hexString.toFormattedHexString().rawString);
}
}
}

0 comments on commit f761bad

Please sign in to comment.