From 17accb568a644c309e2e81bf48dbb73ac3a39a93 Mon Sep 17 00:00:00 2001 From: Volodymyr Kravets Date: Fri, 9 Aug 2024 15:32:52 +0300 Subject: [PATCH] perf: refactor some tests --- .../rsk/core/types/bytes/BytesSliceTest.java | 42 ++++++++++++++++--- .../co/rsk/core/types/bytes/BytesTest.java | 19 +++++++++ 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/rskj-core/src/test/java/co/rsk/core/types/bytes/BytesSliceTest.java b/rskj-core/src/test/java/co/rsk/core/types/bytes/BytesSliceTest.java index 001aee51f35..5abc98a8838 100644 --- a/rskj-core/src/test/java/co/rsk/core/types/bytes/BytesSliceTest.java +++ b/rskj-core/src/test/java/co/rsk/core/types/bytes/BytesSliceTest.java @@ -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()); @@ -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)); @@ -115,6 +119,14 @@ void testEmptySlice() { } private static void checkArraycopy(Functions.Action5 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}; @@ -139,6 +151,26 @@ private static void checkArraycopy(Functions.Action5 void checkCopyOfRange(Functions.Function3 fun, Functions.Function3 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); diff --git a/rskj-core/src/test/java/co/rsk/core/types/bytes/BytesTest.java b/rskj-core/src/test/java/co/rsk/core/types/bytes/BytesTest.java index 7cce5532f17..dd28d5b2e63 100644 --- a/rskj-core/src/test/java/co/rsk/core/types/bytes/BytesTest.java +++ b/rskj-core/src/test/java/co/rsk/core/types/bytes/BytesTest.java @@ -160,6 +160,15 @@ void testEmptyBytesToString() { } private static void checkArraycopy(Functions.Action5 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}; @@ -183,6 +192,16 @@ private static void checkArraycopy(Functions.Action5 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);