Skip to content

Commit 0160c98

Browse files
committed
Add byte order tests
1 parent e8a99ec commit 0160c98

File tree

3 files changed

+105
-24
lines changed

3 files changed

+105
-24
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,12 +1241,19 @@ public boolean equals(Object o) {
12411241

12421242
Bytes bytes = (Bytes) o;
12431243

1244-
return Arrays.equals(internalArray(), bytes.internalArray());
1244+
if (mutable != bytes.mutable) return false;
1245+
if (readonly != bytes.readonly) return false;
1246+
if (!Arrays.equals(byteArray, bytes.byteArray)) return false;
1247+
return byteOrder != null ? byteOrder.equals(bytes.byteOrder) : bytes.byteOrder == null;
12451248
}
12461249

12471250
@Override
12481251
public int hashCode() {
1249-
return Arrays.hashCode(internalArray());
1252+
int result = Arrays.hashCode(byteArray);
1253+
result = 31 * result + (byteOrder != null ? byteOrder.hashCode() : 0);
1254+
result = 31 * result + (mutable ? 1 : 0);
1255+
result = 31 * result + (readonly ? 1 : 0);
1256+
return result;
12501257
}
12511258

12521259
/**

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public Bytes transform(Bytes victim, boolean inPlace) {
8282
}
8383
}
8484

85-
return inPlace ? victim : Bytes.wrap(out);
85+
return inPlace ? victim : new Bytes(out, victim);
8686
}
8787
}
8888

@@ -100,7 +100,7 @@ public Bytes transform(Bytes victim, boolean inPlace) {
100100
out[i] = (byte) ~out[i];
101101
}
102102

103-
return inPlace ? victim : Bytes.wrap(out);
103+
return inPlace ? victim : new Bytes(out, victim);
104104
}
105105
}
106106

@@ -177,7 +177,7 @@ public Bytes transform(Bytes victim, boolean inPlace) {
177177
out[i] = out[out.length - i - 1];
178178
out[out.length - i - 1] = temp;
179179
}
180-
return inPlace ? victim : Bytes.wrap(out);
180+
return inPlace ? victim : new Bytes(out, victim);
181181
}
182182
}
183183

@@ -201,7 +201,7 @@ public Bytes transform(Bytes victim, boolean inPlace) {
201201
if (comparator == null) {
202202
byte[] out = inPlace ? victim.internalArray() : victim.copy().internalArray();
203203
Arrays.sort(out);
204-
return inPlace ? victim : Bytes.wrap(out);
204+
return inPlace ? victim : new Bytes(out, victim);
205205
} else {
206206
//no in-place implementation with comparator
207207
List<Byte> list = victim.toList();
@@ -226,7 +226,7 @@ public ShuffleTransformer(Random random) {
226226
public Bytes transform(Bytes victim, boolean inPlace) {
227227
byte[] out = inPlace ? victim.internalArray() : victim.copy().internalArray();
228228
Util.shuffle(out, random);
229-
return inPlace ? victim : Bytes.wrap(out);
229+
return inPlace ? victim : new Bytes(out, victim);
230230
}
231231
}
232232
}

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

Lines changed: 91 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,107 @@
2121

2222
package at.favre.lib.bytes;
2323

24-
import org.junit.Before;
2524
import org.junit.Test;
2625

27-
import java.nio.ByteBuffer;
2826
import java.nio.ByteOrder;
2927

30-
import static org.junit.Assert.assertArrayEquals;
3128
import static org.junit.Assert.assertEquals;
29+
import static org.junit.Assert.assertNotEquals;
3230

33-
public class BytesByteOrderTest {
34-
@Before
35-
public void setUp() throws Exception {
31+
public class BytesByteOrderTest extends ABytesTest {
32+
33+
@Test
34+
public void miscInput() throws Exception {
35+
testOrder(Bytes.from(350));
36+
testOrder(Bytes.from(172863182736L));
37+
testOrder(Bytes.from(example_bytes_one));
38+
testOrder(Bytes.from(example_bytes_four));
39+
testOrder(Bytes.from(example_bytes_sixteen));
40+
testOrder(Bytes.from(example_bytes_twentyfour));
41+
}
42+
43+
private void testOrder(Bytes bytes) {
44+
Bytes bigEndian = bytes.copy().byteOrder(ByteOrder.BIG_ENDIAN);
45+
Bytes littleEndian = bytes.copy().byteOrder(ByteOrder.LITTLE_ENDIAN);
46+
assertEquals(ByteOrder.BIG_ENDIAN, bigEndian.byteOrder());
47+
assertEquals(ByteOrder.LITTLE_ENDIAN, littleEndian.byteOrder());
48+
assertNotEquals(bigEndian, littleEndian);
49+
50+
System.out.println("big endian: " + bigEndian);
51+
System.out.println("little endian: " + littleEndian);
52+
}
53+
54+
@Test
55+
public void encodeBinary() throws Exception {
56+
Bytes b = Bytes.from(example_bytes_four);
57+
assertNotEquals(b.byteOrder(ByteOrder.BIG_ENDIAN).encodeBinary(), b.byteOrder(ByteOrder.LITTLE_ENDIAN).encodeBinary());
58+
assertEquals(b.byteOrder(ByteOrder.BIG_ENDIAN).encodeBinary(), b.byteOrder(ByteOrder.LITTLE_ENDIAN).reverse().encodeBinary());
59+
}
60+
61+
@Test
62+
public void encodeOct() throws Exception {
63+
Bytes b = Bytes.from(example_bytes_four);
64+
assertNotEquals(b.byteOrder(ByteOrder.BIG_ENDIAN).encodeOctal(), b.byteOrder(ByteOrder.LITTLE_ENDIAN).encodeOctal());
65+
assertEquals(b.byteOrder(ByteOrder.BIG_ENDIAN).encodeOctal(), b.byteOrder(ByteOrder.LITTLE_ENDIAN).reverse().encodeOctal());
3666
}
3767

3868
@Test
39-
public void fromLong() throws Exception {
40-
long number = 172863182736L;
41-
System.out.println(Bytes.from(number).encodeHex());
42-
System.out.println(Bytes.from(number).toLong());
43-
assertArrayEquals(Bytes.parseHex("000000283f72d790").array(), Bytes.from(number).array());
44-
assertEquals(number, Bytes.from(number).toLong());
69+
public void encodeDec() throws Exception {
70+
Bytes b = Bytes.from(example_bytes_four);
71+
assertNotEquals(b.byteOrder(ByteOrder.BIG_ENDIAN).encodeDec(), b.byteOrder(ByteOrder.LITTLE_ENDIAN).encodeDec());
72+
assertEquals(b.byteOrder(ByteOrder.BIG_ENDIAN).encodeDec(), b.byteOrder(ByteOrder.LITTLE_ENDIAN).reverse().encodeDec());
73+
}
4574

46-
byte[] defaultArray = new byte[]{(byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04};
75+
@Test
76+
public void encodeHex() throws Exception {
77+
Bytes b = Bytes.from(example_bytes_two);
78+
assertNotEquals(b.byteOrder(ByteOrder.BIG_ENDIAN).encodeHex(), b.byteOrder(ByteOrder.LITTLE_ENDIAN).encodeHex());
79+
assertEquals(b.byteOrder(ByteOrder.BIG_ENDIAN).encodeHex(), b.byteOrder(ByteOrder.LITTLE_ENDIAN).reverse().encodeHex());
80+
}
4781

48-
System.out.println(Bytes.from(ByteBuffer.wrap(defaultArray).order(ByteOrder.BIG_ENDIAN)).encodeHex());
49-
System.out.println(Bytes.from(ByteBuffer.wrap(defaultArray).order(ByteOrder.BIG_ENDIAN).getInt()).encodeHex());
50-
System.out.println(Bytes.from(ByteBuffer.wrap(defaultArray).order(ByteOrder.LITTLE_ENDIAN)).encodeHex());
51-
System.out.println(Bytes.from(ByteBuffer.wrap(defaultArray).order(ByteOrder.LITTLE_ENDIAN).getInt()).encodeHex());
82+
@Test
83+
public void encodeBase36() throws Exception {
84+
Bytes b = Bytes.from(example_bytes_four);
85+
assertNotEquals(b.byteOrder(ByteOrder.BIG_ENDIAN).encodeBase36(), b.byteOrder(ByteOrder.LITTLE_ENDIAN).encodeBase36());
86+
assertEquals(b.byteOrder(ByteOrder.BIG_ENDIAN).encodeBase36(), b.byteOrder(ByteOrder.LITTLE_ENDIAN).reverse().encodeBase36());
87+
}
88+
89+
@Test
90+
public void encodeBase64() throws Exception {
91+
Bytes b = Bytes.from(example_bytes_four);
92+
assertNotEquals(b.byteOrder(ByteOrder.BIG_ENDIAN).encodeBase64(), b.byteOrder(ByteOrder.LITTLE_ENDIAN).encodeBase64());
93+
assertEquals(b.byteOrder(ByteOrder.BIG_ENDIAN).encodeBase64(), b.byteOrder(ByteOrder.LITTLE_ENDIAN).reverse().encodeBase64());
5294
}
95+
96+
@Test
97+
public void toByte() throws Exception {
98+
Bytes b = Bytes.from(example_bytes_one);
99+
assertEquals(b.byteOrder(ByteOrder.BIG_ENDIAN).toByte(), b.byteOrder(ByteOrder.LITTLE_ENDIAN).toByte());
100+
}
101+
102+
@Test
103+
public void toChar() throws Exception {
104+
Bytes b = Bytes.from(example_bytes_two);
105+
assertNotEquals(b.byteOrder(ByteOrder.BIG_ENDIAN).toChar(), b.byteOrder(ByteOrder.LITTLE_ENDIAN).toChar());
106+
}
107+
108+
@Test
109+
public void toShort() throws Exception {
110+
Bytes b = Bytes.from(example_bytes_two);
111+
assertNotEquals(b.byteOrder(ByteOrder.BIG_ENDIAN).toShort(), b.byteOrder(ByteOrder.LITTLE_ENDIAN).toShort());
112+
}
113+
114+
@Test
115+
public void toInt() throws Exception {
116+
Bytes b = Bytes.from(example_bytes_four);
117+
assertNotEquals(b.byteOrder(ByteOrder.BIG_ENDIAN).toInt(), b.byteOrder(ByteOrder.LITTLE_ENDIAN).toInt());
118+
}
119+
120+
@Test
121+
public void toLong() throws Exception {
122+
Bytes b = Bytes.from(example_bytes_eight);
123+
assertNotEquals(b.byteOrder(ByteOrder.BIG_ENDIAN).toLong(), b.byteOrder(ByteOrder.LITTLE_ENDIAN).toLong());
124+
}
125+
126+
53127
}

0 commit comments

Comments
 (0)