Skip to content

Commit 9158aee

Browse files
committed
Merge branch 'f-lib-hash' into develop
2 parents 0c33c87 + 76ed633 commit 9158aee

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
plugins {
2+
id 'com.adarshr.test-logger' version '1.7.0'
3+
}
4+
15
repositories{
26
mavenCentral()
37
}

src/com/redomar/game/lib/HashGen.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.redomar.game.lib;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
5+
public class HashGen {
6+
7+
private String hexHash;
8+
private boolean prefix;
9+
private int hexLength;
10+
private String previousHash;
11+
12+
/**
13+
* Use for generating a hex Hash. Requires two parameters;
14+
* @param showPrefix to show 0x prefix.
15+
* @param length Length of hash.
16+
*/
17+
public HashGen(boolean showPrefix, int length){
18+
setPrefix(showPrefix);
19+
setHexLength(length);
20+
init();
21+
}
22+
23+
//Remove null char or prepend 0x
24+
private void init(){
25+
if(prefix){
26+
hexHash = "0x";
27+
} else {
28+
hexHash = "";
29+
}
30+
}
31+
32+
/**
33+
* Retrieve hash
34+
* @return String containing hash
35+
*/
36+
public String getHash(){
37+
setHash(hexLength);
38+
return hexHash;
39+
}
40+
41+
private void setHash(int hexLength){
42+
43+
String hex;
44+
45+
for (int i = 0; i < hexLength; i++){
46+
hex = Integer.toHexString(randNumGen());
47+
hex = StringUtils.capitalize(hex);
48+
hexHash = hexHash + hex;
49+
}
50+
previousHash = hexHash;
51+
52+
}
53+
54+
private int randNumGen(){
55+
int rand = (int)(Math.random() * 16);
56+
return rand;
57+
}
58+
59+
//getters and setters
60+
61+
public void setPrefix(boolean prefix) {
62+
this.prefix = prefix;
63+
}
64+
65+
public void setHexLength(int hexLength) {
66+
this.hexLength = hexLength;
67+
}
68+
69+
public String getPreviousHash(){
70+
if(previousHash == null) return null;
71+
return previousHash;
72+
}
73+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.redomar.game.lib;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.*;
7+
8+
public class HashTest {
9+
10+
private HashGen hashGen;
11+
12+
@Before
13+
public void setUp() throws Exception{
14+
hashGen = new HashGen(false,10);
15+
}
16+
17+
@Test
18+
public void hashNotEmpty(){
19+
assertNotNull(hashGen.getHash());
20+
}
21+
22+
@Test
23+
public void hashZeroLengthNotNull(){
24+
HashGen hg = new HashGen(false,0);
25+
assertNotNull(hg.getHash());
26+
}
27+
28+
@Test
29+
public void compareHashAndPrevious(){
30+
hashGen.setHexLength(8);
31+
assertEquals(hashGen.getHash(), hashGen.getPreviousHash());
32+
}
33+
34+
@Test
35+
public void previousShouldNotGenNewHash(){
36+
assertEquals(hashGen.getPreviousHash(), hashGen.getPreviousHash());
37+
}
38+
39+
@Test
40+
public void hashLengthEqualsZero(){
41+
HashGen hg = new HashGen(false, 0);
42+
assertEquals(0,hg.getHash().length());
43+
}
44+
45+
@Test
46+
public void hashLengthEqualsGivenLength(){
47+
HashGen hg = new HashGen(false, 80);
48+
assertEquals(80, hg.getHash().length());
49+
}
50+
51+
@Test
52+
public void hashLengthEqualsSetLength(){
53+
HashGen hg = new HashGen(false, 80);
54+
hg.setHexLength(5);
55+
assertEquals(5,hg.getHash().length());
56+
}
57+
58+
@Test
59+
public void hashPrefix(){
60+
HashGen hg = new HashGen(true,0);
61+
assertEquals("0x", hg.getHash());
62+
}
63+
64+
}

0 commit comments

Comments
 (0)