Skip to content

Commit 20cd9f9

Browse files
committed
Add more encoding tests
1 parent 650087c commit 20cd9f9

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

src/main/java/at/favre/lib/bytes/BinaryToTextEncoding.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,16 @@ interface Decoder {
6161
byte[] decode(String encoded);
6262
}
6363

64+
/**
65+
* Unifies both interfaces {@link Encoder} and {@link Decoder}
66+
*/
67+
interface EncoderDecoder extends Encoder, Decoder {
68+
}
69+
6470
/**
6571
* Hex or Base16
6672
*/
67-
class Hex implements Encoder, Decoder {
73+
class Hex implements EncoderDecoder {
6874
private final boolean upperCase;
6975

7076
public Hex() {
@@ -125,7 +131,7 @@ public byte[] decode(String hexString) {
125131
/**
126132
* Simple Base64 encoder
127133
*/
128-
class Base64Encoding implements Encoder, Decoder {
134+
class Base64Encoding implements EncoderDecoder {
129135
@Override
130136
public String encode(byte[] array, ByteOrder byteOrder) {
131137
return Base64.encode((byteOrder == ByteOrder.BIG_ENDIAN) ? array : Bytes.from(array).reverse().array());
@@ -140,7 +146,7 @@ public byte[] decode(String encoded) {
140146
/**
141147
* Simple radix encoder which internally uses {@link BigInteger#toString(int)}
142148
*/
143-
class BaseRadix implements Encoder, Decoder {
149+
class BaseRadix implements EncoderDecoder {
144150
private final int radix;
145151

146152
BaseRadix(int radix) {

src/test/java/at/favre/lib/bytes/BinaryToTextEncodingTest.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
package at.favre.lib.bytes;
2323

24+
import org.junit.Ignore;
2425
import org.junit.Test;
2526

2627
import java.nio.ByteOrder;
@@ -48,8 +49,47 @@ public void encodeBaseRadix() throws Exception {
4849
assertNotEquals(new BinaryToTextEncoding.BaseRadix(2).encode(new byte[]{1, 2, 3}, ByteOrder.LITTLE_ENDIAN), new BinaryToTextEncoding.BaseRadix(2).encode(new byte[]{1, 2, 3}, ByteOrder.BIG_ENDIAN));
4950
}
5051

52+
@Test
53+
@Ignore
54+
public void encodeDecodeRadix() throws Exception {
55+
for (int i = 0; i < 32; i++) {
56+
Bytes rnd = Bytes.random(i);
57+
System.out.println("\n\nNEW TEST: " + i + " bytes\n");
58+
for (int j = 16; j < 36; j++) {
59+
BinaryToTextEncoding.EncoderDecoder encoding = new BinaryToTextEncoding.BaseRadix(j);
60+
String encodedBigEndian = encoding.encode(rnd.array(), ByteOrder.BIG_ENDIAN);
61+
byte[] decoded = encoding.decode(encodedBigEndian);
62+
System.out.println("radix" + j + ":\t" + encodedBigEndian);
63+
System.out.println("orig :\t" + rnd.encodeHex());
64+
System.out.println("enc :\t" + Bytes.wrap(decoded).encodeHex());
65+
}
66+
}
67+
}
68+
69+
@Test
70+
public void encodeDecodeBase64() throws Exception {
71+
for (int i = 4; i < 32; i += 4) {
72+
Bytes rnd = Bytes.random(i);
73+
BinaryToTextEncoding.EncoderDecoder encoding = new BinaryToTextEncoding.Base64Encoding();
74+
String encodedBigEndian = encoding.encode(rnd.array(), ByteOrder.BIG_ENDIAN);
75+
byte[] decoded = encoding.decode(encodedBigEndian);
76+
assertEquals(rnd, Bytes.wrap(decoded));
77+
}
78+
}
79+
80+
@Test
81+
public void encodeDecodeHex() throws Exception {
82+
for (int i = 4; i < 32; i += 4) {
83+
Bytes rnd = Bytes.random(i);
84+
BinaryToTextEncoding.EncoderDecoder encoding = new BinaryToTextEncoding.Hex();
85+
String encodedBigEndian = encoding.encode(rnd.array(), ByteOrder.BIG_ENDIAN);
86+
byte[] decoded = encoding.decode(encodedBigEndian);
87+
assertEquals(rnd, Bytes.wrap(decoded));
88+
}
89+
}
90+
5191
@Test(expected = IllegalArgumentException.class)
52-
public void decodeInvalidRadix116() throws Exception {
92+
public void decodeInvalidRadix16() throws Exception {
5393
new BinaryToTextEncoding.BaseRadix(16).decode("AAI=");
5494
}
5595

0 commit comments

Comments
 (0)