Skip to content

Commit

Permalink
perf: refactor some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Vovchyk committed Sep 30, 2024
1 parent 6c718c3 commit 17accb5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
class BytesSliceTest {

@Test
void testBytesLength() {
void testBytesLength() {
assertEquals(0, Bytes.of(new byte[]{}).slice(0, 0).length());
assertEquals(0, Bytes.of(new byte[]{1}).slice(0, 0).length());
assertEquals(1, Bytes.of(new byte[]{1}).slice(0, 1).length());
Expand All @@ -41,16 +41,20 @@ void testBytesLength() {

@Test
void testBytesAt() {
assertThrows(IndexOutOfBoundsException.class, () -> Bytes.of(new byte[]{}).slice(0, 0).byteAt(0));
assertThrows(IndexOutOfBoundsException.class, () -> Bytes.of(new byte[]{1}).slice(0, 1).byteAt(1));
assertThrows(IndexOutOfBoundsException.class, () -> Bytes.of(new byte[]{1}).slice(0, 1).byteAt(-1));
assertThrows(IndexOutOfBoundsException.class, () -> Bytes.of(new byte[]{1,2,3}).slice(1, 2).byteAt(1));
assertEquals(1, Bytes.of(new byte[]{1}).slice(0, 1).byteAt(0));
assertEquals(2, Bytes.of(new byte[]{1,2}).slice(0, 2).byteAt(1));
assertEquals(2, Bytes.of(new byte[]{1,2,3}).slice(1, 2).byteAt(0));
assertEquals(4, Bytes.of(new byte[]{1,2,3,4}).slice(2, 4).byteAt(1));
}

@Test
void testBytesAtIndexOutOfBoundsException() {
assertThrows(IndexOutOfBoundsException.class, () -> Bytes.of(new byte[]{}).slice(0, 0).byteAt(0));
assertThrows(IndexOutOfBoundsException.class, () -> Bytes.of(new byte[]{1}).slice(0, 1).byteAt(1));
assertThrows(IndexOutOfBoundsException.class, () -> Bytes.of(new byte[]{1}).slice(0, 1).byteAt(-1));
assertThrows(IndexOutOfBoundsException.class, () -> Bytes.of(new byte[]{1,2,3}).slice(1, 2).byteAt(1));
}

@Test
void testBytesSliceArraycopy() {
checkArraycopy((src, srcPos, dest, destPos, length) -> Bytes.of((byte[]) src).slice(1, 4).arraycopy(srcPos, (byte[]) dest, destPos, length));
Expand Down Expand Up @@ -115,6 +119,14 @@ void testEmptySlice() {
}

private static void checkArraycopy(Functions.Action5<Object, Integer, Object, Integer, Integer> fun) {
/*
'fun' signature:
@src – the source array.
@srcPos – starting position in the source array.
@dest – the destination array.
@destPos – starting position in the destination data.
@length – the number of array elements to be copied.
*/
byte[] dest = new byte[3];
byte[] origin = new byte[]{1,2,3,4,5};

Expand All @@ -139,6 +151,26 @@ private static void checkArraycopy(Functions.Action5<Object, Integer, Object, In

private static <T> void checkCopyOfRange(Functions.Function3<T, Integer, Integer, byte[]> fun,
Functions.Function3<byte[], Integer, Integer, T> slicer) {
/*
'fun' signature:
@original – the array from which a range is to be copied
@from – the initial index of the range to be copied, inclusive
@to – the final index of the range to be copied, exclusive. (This index may lie outside the array.)
@return a new array containing the specified range from the original array, truncated or padded with zeros
to obtain the required length
*/

/*
'slicer' signature:
@original – the array from which a range is to be copied
@from – the initial index of the range to be copied, inclusive
@to – the final index of the range to be copied, exclusive. (This index may lie outside the array.)
@return a new entity containing the specified range from the original array, truncated or padded with zeros
to obtain the required length
*/

byte[] bArray = new byte[]{1, 2, 3, 4, 5, 6};

assertEquals(bArray.length, fun.apply(slicer.apply(bArray, 0, 6), 0, 6).length);
Expand Down
19 changes: 19 additions & 0 deletions rskj-core/src/test/java/co/rsk/core/types/bytes/BytesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@ void testEmptyBytesToString() {
}

private static void checkArraycopy(Functions.Action5<Object, Integer, Object, Integer, Integer> fun) {
/*
'fun' signature:
@src – the source array.
@srcPos – starting position in the source array.
@dest – the destination array.
@destPos – starting position in the destination data.
@length – the number of array elements to be copied.
*/

byte[] dest = new byte[5];
byte[] origin = new byte[]{1,2,3,4,5};

Expand All @@ -183,6 +192,16 @@ private static void checkArraycopy(Functions.Action5<Object, Integer, Object, In
}

private static void checkCopyOfRange(Functions.Function3<byte[], Integer, Integer, byte[]> fun) {
/*
'fun' signature:
@original – the array from which a range is to be copied
@from – the initial index of the range to be copied, inclusive
@to – the final index of the range to be copied, exclusive. (This index may lie outside the array.)
@return a new array containing the specified range from the original array, truncated or padded with zeros
to obtain the required length
*/

byte[] bArray = new byte[]{1, 2, 3, 4, 5};

assertEquals(bArray.length, fun.apply(bArray, 0, 5).length);
Expand Down

0 comments on commit 17accb5

Please sign in to comment.