diff --git a/build.gradle.kts b/build.gradle.kts index 9d9255c..69e4838 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -8,7 +8,7 @@ plugins { } group = "nl.astraeus" -version = "0.3.0" +version = "0.3.2" repositories { mavenCentral() diff --git a/src/commonMain/kotlin/nl/astraeus/tba/SlicedByteArray.kt b/src/commonMain/kotlin/nl/astraeus/tba/SlicedByteArray.kt index 5e0f028..b39f59a 100644 --- a/src/commonMain/kotlin/nl/astraeus/tba/SlicedByteArray.kt +++ b/src/commonMain/kotlin/nl/astraeus/tba/SlicedByteArray.kt @@ -185,6 +185,14 @@ class SlicedByteArray( fun decodeToString(): String = data.decodeToString(offset, offset + length) + fun slice(offset: Int, length: Int): SlicedByteArray { + check(offset >= 0) { "Offset must be non-negative" } + check(length >= 0) { "Length must be non-negative" } + check(offset + length <= this.length) { "Offset + length exceeds array bounds (offset: $offset, length: $length, array length: ${this.length})" } + + return SlicedByteArray(data, this.offset + offset, length) + } + companion object { fun wrap( data: ByteArray, diff --git a/src/commonTest/kotlin/nl/astraeus/tba/SliceTest.kt b/src/commonTest/kotlin/nl/astraeus/tba/SliceTest.kt new file mode 100644 index 0000000..af6259a --- /dev/null +++ b/src/commonTest/kotlin/nl/astraeus/tba/SliceTest.kt @@ -0,0 +1,93 @@ +package nl.astraeus.tba + +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith + +/** + * Test class for the slice method in SlicedByteArray + */ +class SliceTest { + + @Test + fun testSlice() { + // Create a SlicedByteArray with some test data + val array = SlicedByteArray(10) + for (i in 0 until 10) { + array[i] = i.toByte() + } + + // Test basic slicing + val slice1 = array.slice(2, 3) + assertEquals(3, slice1.size) + assertEquals(2.toByte(), slice1[0]) + assertEquals(3.toByte(), slice1[1]) + assertEquals(4.toByte(), slice1[2]) + + // Test slicing with offset 0 + val slice2 = array.slice(0, 5) + assertEquals(5, slice2.size) + for (i in 0 until 5) { + assertEquals(i.toByte(), slice2[i]) + } + + // Test slicing to the end + val slice3 = array.slice(5, 5) + assertEquals(5, slice3.size) + for (i in 0 until 5) { + assertEquals((i + 5).toByte(), slice3[i]) + } + + // Test nested slicing + val nestedSlice = slice1.slice(1, 2) + assertEquals(2, nestedSlice.size) + assertEquals(3.toByte(), nestedSlice[0]) + assertEquals(4.toByte(), nestedSlice[1]) + } + + @Test + fun testSliceEdgeCases() { + val array = SlicedByteArray(10) + for (i in 0 until 10) { + array[i] = i.toByte() + } + + // Test empty slice + val emptySlice = array.slice(5, 0) + assertEquals(0, emptySlice.size) + + // Test slice at the very end + val endSlice = array.slice(9, 1) + assertEquals(1, endSlice.size) + assertEquals(9.toByte(), endSlice[0]) + } + + @Test + fun testSliceErrorCases() { + val array = SlicedByteArray(10) + + // Test negative offset + assertFailsWith { + array.slice(-1, 5) + } + + // Test negative length + assertFailsWith { + array.slice(0, -1) + } + + // Test offset + length > array.length + assertFailsWith { + array.slice(6, 5) + } + + // Test offset = array.length (valid if length = 0) + val emptyEndSlice = array.slice(10, 0) + assertEquals(0, emptyEndSlice.size) + + // Test offset > array.length + assertFailsWith { + array.slice(11, 0) + } + } +} \ No newline at end of file