Skip to content

Commit 0b627cd

Browse files
committed
Add transformer tests
1 parent f476b11 commit 0b627cd

File tree

6 files changed

+313
-24
lines changed

6 files changed

+313
-24
lines changed

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,16 @@ public Bytes append(byte singleByte) {
429429
return append(new byte[]{singleByte});
430430
}
431431

432+
/**
433+
* Creates a new instance with the current array appended with the provided data (ie. append at the end)
434+
*
435+
* @param char2Bytes to append
436+
* @return appended instance
437+
*/
438+
public Bytes append(char char2Bytes) {
439+
return append(Bytes.from(char2Bytes));
440+
}
441+
432442
/**
433443
* Creates a new instance with the current array appended with the provided data (ie. append at the end)
434444
*
@@ -557,7 +567,7 @@ public Bytes negate() {
557567
* <p>
558568
* See the considerations about possible in-place operation in {@link #transform(BytesTransformer)}.
559569
*
560-
* @param shiftCount how many bits
570+
* @param shiftCount how many bits (not bytes)
561571
* @return shifted instance
562572
* @see <a href="https://en.wikipedia.org/wiki/Bitwise_operation#Bit_shifts">Bit shifts</a>
563573
*/
@@ -570,7 +580,7 @@ public Bytes leftShift(int shiftCount) {
570580
* <p>
571581
* See the considerations about possible in-place operation in {@link #transform(BytesTransformer)}.
572582
*
573-
* @param shiftCount how many bits
583+
* @param shiftCount how many bits (not bytes)
574584
* @return shifted instance
575585
* @see <a href="https://en.wikipedia.org/wiki/Bitwise_operation#Bit_shifts">Bit shifts</a>
576586
*/
@@ -690,8 +700,21 @@ public Bytes resize(int newByteLength) {
690700
if (length() == newByteLength) {
691701
return this;
692702
}
703+
704+
if (newByteLength < 0) {
705+
throw new IllegalArgumentException("cannot resize to smaller than 0");
706+
}
707+
708+
if (newByteLength == 0) {
709+
return wrap(new byte[0]);
710+
}
711+
693712
byte[] newSize = new byte[newByteLength];
694-
System.arraycopy(internalArray(), 0, newSize, Math.max(0, Math.abs(newByteLength - length())), Math.min(newByteLength, length()));
713+
if (newByteLength > length()) {
714+
System.arraycopy(internalArray(), 0, newSize, Math.max(0, Math.abs(newByteLength - length())), Math.min(newByteLength, length()));
715+
} else {
716+
System.arraycopy(internalArray(), Math.max(0, Math.abs(newByteLength - length())), newSize, Math.min(0, Math.abs(newByteLength - length())), Math.min(newByteLength, length()));
717+
}
695718
return wrap(newSize);
696719
}
697720

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

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,18 @@ public Bytes transform(Bytes victim, boolean inPlace) {
6464
throw new IllegalArgumentException("all byte array must be of same length doing bit wise operation");
6565
}
6666

67-
byte[] out = inPlace ? victim.array() : new byte[victim.length()];
67+
byte[] out = inPlace ? victim.internalArray() : new byte[victim.length()];
6868

6969
for (int i = 0; i < victim.length(); i++) {
7070
switch (mode) {
7171
case OR:
72-
out[i] = (byte) (victim.array()[i] | secondArray[i]);
72+
out[i] = (byte) (victim.internalArray()[i] | secondArray[i]);
7373
break;
7474
case AND:
75-
out[i] = (byte) (victim.array()[i] & secondArray[i]);
75+
out[i] = (byte) (victim.internalArray()[i] & secondArray[i]);
7676
break;
7777
case XOR:
78-
out[i] = (byte) (victim.array()[i] ^ secondArray[i]);
78+
out[i] = (byte) (victim.internalArray()[i] ^ secondArray[i]);
7979
break;
8080
default:
8181
throw new IllegalArgumentException("unknown bitwise transform mode " + mode);
@@ -94,10 +94,10 @@ public Bytes transform(Bytes victim, boolean inPlace) {
9494
final class NegateTransformer implements BytesTransformer {
9595
@Override
9696
public Bytes transform(Bytes victim, boolean inPlace) {
97-
byte[] out = inPlace ? victim.array() : new byte[victim.length()];
97+
byte[] out = inPlace ? victim.internalArray() : victim.copy().internalArray();
9898

9999
for (int i = 0; i < victim.length(); i++) {
100-
out[i] = (byte) ~victim.array()[i];
100+
out[i] = (byte) ~out[i];
101101
}
102102

103103
return inPlace ? victim : Bytes.wrap(out);
@@ -129,9 +129,9 @@ public Bytes transform(Bytes victim, boolean inPlace) {
129129
BigInteger bigInt;
130130

131131
if (inPlace) {
132-
bigInt = new BigInteger(victim.array());
132+
bigInt = new BigInteger(victim.internalArray());
133133
} else {
134-
bigInt = new BigInteger(victim.copy().array());
134+
bigInt = new BigInteger(victim.copy().internalArray());
135135
}
136136

137137
switch (type) {
@@ -160,7 +160,7 @@ public ConcatTransformer(byte[] secondArrays) {
160160

161161
@Override
162162
public Bytes transform(Bytes victim, boolean inPlace) {
163-
return Bytes.wrap(Util.concat(victim.array(), secondArray));
163+
return Bytes.wrap(Util.concat(victim.internalArray(), secondArray));
164164
}
165165
}
166166

@@ -170,14 +170,13 @@ public Bytes transform(Bytes victim, boolean inPlace) {
170170
final class ReverseTransformer implements BytesTransformer {
171171
@Override
172172
public Bytes transform(Bytes victim, boolean inPlace) {
173-
byte[] out = inPlace ? victim.array() : new byte[victim.length()];
173+
byte[] out = inPlace ? victim.internalArray() : victim.copy().internalArray();
174174

175-
for (int k = 0; k < out.length / 2; k++) {
176-
byte temp = out[k];
177-
out[k] = out[out.length - (1 + k)];
178-
out[out.length - (1 + k)] = temp;
175+
for (int i = 0; i < out.length / 2; i++) {
176+
byte temp = out[i];
177+
out[i] = out[out.length - i - 1];
178+
out[out.length - i - 1] = temp;
179179
}
180-
181180
return inPlace ? victim : Bytes.wrap(out);
182181
}
183182
}
@@ -200,13 +199,13 @@ public SortTransformer(Comparator<Byte> comparator) {
200199
public Bytes transform(Bytes victim, boolean inPlace) {
201200

202201
if (comparator == null) {
203-
byte[] out = inPlace ? victim.array() : new byte[victim.length()];
202+
byte[] out = inPlace ? victim.internalArray() : victim.copy().internalArray();
204203
Arrays.sort(out);
205204
return inPlace ? victim : Bytes.wrap(out);
206205
} else {
207206
//no in-place implementation with comparator
208207
List<Byte> list = victim.toList();
209-
Collections.sort(list);
208+
Collections.sort(list, comparator);
210209
return Bytes.from(list);
211210
}
212211
}
@@ -225,7 +224,7 @@ public ShuffleTransformer(Random random) {
225224

226225
@Override
227226
public Bytes transform(Bytes victim, boolean inPlace) {
228-
byte[] out = inPlace ? victim.array() : new byte[victim.length()];
227+
byte[] out = inPlace ? victim.internalArray() : victim.copy().internalArray();
229228
Util.shuffle(out, random);
230229
return inPlace ? victim : Bytes.wrap(out);
231230
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public abstract class ABytesTest {
5151
byte[] example_bytes_sixteen;
5252
String example_hex_sixteen;
5353

54+
byte[] example_bytes_twentyfour;
55+
String example_hex_twentyfour;
5456
@Before
5557
public void setUp() throws Exception {
5658
example_bytes_empty = new byte[0];
@@ -68,6 +70,8 @@ public void setUp() throws Exception {
6870
example_hex_eight = "eed1fdaa12af7809";
6971
example_bytes_sixteen = new byte[]{0x7E, (byte) 0xD1, (byte) 0xFD, (byte) 0xAA, 0x12, (byte) 0xAF, (byte) 0x78, 0x09, 0x1E, (byte) 0xD1, (byte) 0xFD, (byte) 0xAA, 0x12, (byte) 0xAF, (byte) 0x00, 0x0A};
7072
example_hex_sixteen = "7ed1fdaa12af78091ed1fdaa12af000a";
73+
example_bytes_twentyfour = new byte[]{(byte) 0x8E, (byte) 0xD1, (byte) 0xFD, (byte) 0xAA, 0x12, (byte) 0xAF, (byte) 0x78, 0x09, 0x1E, (byte) 0xD1, (byte) 0xFD, (byte) 0xAA, 0x12, (byte) 0xAF, (byte) 0x00, 0x0A, (byte) 0xEE, (byte) 0xD1, (byte) 0xFD, (byte) 0xAA, 0x12, (byte) 0xAF, (byte) 0x78, 0x11};
74+
example_hex_twentyfour = "8ed1fdaa12af78091ed1fdaa12af000aeed1fdaa12af7811";
7175
}
7276

7377
Bytes fromAndTest(byte[] bytes) {

src/test/java/at/favre/lib/bytes/BytesPrimitivesTest.java renamed to src/test/java/at/favre/lib/bytes/BytesByteOrderTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@
3030
import static org.junit.Assert.assertArrayEquals;
3131
import static org.junit.Assert.assertEquals;
3232

33-
public class BytesPrimitivesTest {
34-
private final static byte[] example2 = new byte[]{0x4A, (byte) 0x94, (byte) 0xFD, (byte) 0xFF, 0x1E, (byte) 0xAF, (byte) 0xED};
35-
33+
public class BytesByteOrderTest {
3634
@Before
3735
public void setUp() throws Exception {
3836
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,10 @@ public void fromVariousBytes() throws Exception {
217217
assertArrayEquals(new byte[2], Bytes.from((byte) 0, (byte) 0).array());
218218
assertArrayNotEquals(new byte[2], Bytes.from((byte) 1, (byte) 0).array());
219219
}
220+
221+
@Test
222+
public void fromPartByte() throws Exception {
223+
assertArrayEquals(new byte[]{example_bytes_four[1]}, Bytes.from(example_bytes_four, 1, 1).array());
224+
assertArrayEquals(new byte[]{example_bytes_eight[4], example_bytes_eight[5], example_bytes_eight[6]}, Bytes.from(example_bytes_eight, 4, 3).array());
225+
}
220226
}

0 commit comments

Comments
 (0)