Skip to content

Commit 2b30032

Browse files
committed
add some tests
1 parent 37df2b7 commit 2b30032

11 files changed

+289
-21
lines changed

build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ plugins {
1010
}
1111

1212
group = "io.github.froks"
13-
version = "0.2.1"
13+
version = "0.2.2"
1414

1515
repositories {
1616
mavenLocal()
@@ -19,6 +19,7 @@ repositories {
1919

2020
dependencies {
2121
testImplementation("org.jetbrains.kotlin:kotlin-test")
22+
testImplementation("com.willowtreeapps.assertk:assertk:0.28.1")
2223
}
2324

2425
kotlin {

src/main/kotlin/dltcore/DltExtensions.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package dltcore
22

3-
public fun String.asIntValue(): Int {
3+
public typealias IdInt = Int
4+
public typealias IdString = String
5+
6+
public fun IdString.asIntValue(): Int {
47
if (this.length > 4) {
58
throw IllegalArgumentException("AppId '$this' may not be longer than 4 bytes")
69
}
@@ -13,7 +16,7 @@ public fun String.asIntValue(): Int {
1316
return value
1417
}
1518

16-
public fun Int.asStringValue(): String =
19+
public fun IdInt.asStringValue(): String =
1720
String(
1821
byteArrayOf(
1922
if (this and 0xFF000000.toInt() == 0) ' '.code.toByte() else (this and 0xFF000000.toInt() shr 24).toByte(),

src/main/kotlin/dltcore/DltObjects.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public class DltMessageV1(
9999
public class DltStorageHeaderV1(
100100
public val timestampEpochSeconds: Long, // 4 unsigned byte in V1
101101
public val timestampMicroseconds: Int, // microseconds in V1, Nanoseconds in V2
102-
public val ecuId: Int, // 4 chars
102+
public val ecuId: IdInt, // 4 chars
103103
) : DltStorageHeader {
104104
public override val ecuIdText: String
105105
get() = ecuId.asStringValue()
@@ -267,8 +267,8 @@ public enum class MessageTypeInfo(public val type: MessageType, public val value
267267
public class DltExtendedHeaderV1(
268268
public val msin: Byte, // message info
269269
public val noar: Byte, // number of arguments
270-
public val apid: Int, // application id
271-
public val ctid: Int, // context id
270+
public val apid: IdInt, // application id
271+
public val ctid: IdInt, // context id
272272
) : DltExtendedHeader {
273273
public fun write(bb: BinaryOutputStream) {
274274
bb.order(ByteOrder.BIG_ENDIAN)
@@ -354,7 +354,7 @@ public class DltPayloadV1(
354354
public fun write(bb: BinaryOutputStream) {
355355
bb.order(byteOrder)
356356

357-
bb.put(data)
357+
bb.writeArray(data)
358358
}
359359

360360

@@ -608,7 +608,6 @@ public class DltPayloadArgumentNumber(
608608
}
609609
}
610610

611-
override fun toString(): String {
612-
return number.toString()
613-
}
611+
override fun toString(): String =
612+
number.toString()
614613
}

src/main/kotlin/dltcore/DltParser.kt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package dltcore
33
import library.BinaryInputStream
44
import library.ByteOrder
55
import library.LargeFileByteBufferInputStream
6+
import library.ParseException
67
import java.nio.file.Path
78
import kotlin.io.path.fileSize
89

@@ -18,7 +19,10 @@ public data class DltReadStatus(
1819
val error: Exception?,
1920
)
2021

21-
private class DltMessageIterator(val buffer: BinaryInputStream, val totalSize: Long?) : Iterator<DltReadStatus> {
22+
private class DltMessageIterator(
23+
private val buffer: BinaryInputStream,
24+
private val totalSize: Long?
25+
) : Iterator<DltReadStatus> {
2226
private var index: Long = 0
2327
private var successCount: Long = 0
2428
private var errorCount: Long = 0
@@ -48,16 +52,18 @@ private class DltMessageIterator(val buffer: BinaryInputStream, val totalSize: L
4852
val result = try {
4953
val magic = findFirstValidMagic()
5054
val version = DltStorageVersion.getByMagic(magic)
51-
parseDltMessage(buffer, version)
52-
} catch (e: RuntimeException) {
55+
val msg = parseDltMessage(buffer, version)
56+
successCount++
57+
msg
58+
} catch (e: Exception) {
5359
errorCount++
5460

55-
RuntimeException(
61+
ParseException(
62+
buffer.position(),
5663
"Error while parsing message at file position ${buffer.position()}: ${e.message}",
5764
e
5865
)
5966
}
60-
successCount++
6167
val progress = if (totalSize != null) {
6268
buffer.position().toFloat() / totalSize.toFloat()
6369
} else {
@@ -75,7 +81,7 @@ private class DltMessageIterator(val buffer: BinaryInputStream, val totalSize: L
7581
error = result as? Exception,
7682
)
7783
}
78-
throw RuntimeException("No more data available, but next() was called on iterator")
84+
throw IllegalStateException("No more data available, but next() was called on iterator")
7985
}
8086
}
8187

src/main/kotlin/library/BinaryOutputStream.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package library
22

33
public interface BinaryOutputStream {
44
public fun order(order: ByteOrder)
5-
public fun put(data: ByteArray)
65

76
public fun writeByte(value: Byte)
87
public fun writeShort(value: Short)
98
public fun writeInt(value: Int)
109
public fun writeLong(value: Long)
10+
public fun writeArray(data: ByteArray)
11+
public fun position(): Long
1112
}

src/main/kotlin/library/ByteBufferBinaryOutputStream.kt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,37 @@ package library
33
import java.nio.ByteBuffer
44

55
public class ByteBufferBinaryOutputStream(private val buffer: ByteBuffer) : BinaryOutputStream {
6+
private var position: Long = 0
7+
68
override fun order(order: ByteOrder) {
79
buffer.order(order.asByteBufferByteOrder())
810
}
911

10-
override fun put(data: ByteArray) {
11-
buffer.put(data)
12-
}
13-
1412
override fun writeByte(value: Byte) {
1513
buffer.put(value)
14+
position += 1
1615
}
1716

1817
override fun writeShort(value: Short) {
1918
buffer.putShort(value)
19+
position += 2
2020
}
2121

2222
override fun writeInt(value: Int) {
2323
buffer.putInt(value)
24+
position += 4
2425
}
2526

2627
override fun writeLong(value: Long) {
2728
buffer.putLong(value)
29+
position += 8
2830
}
31+
32+
override fun writeArray(data: ByteArray) {
33+
buffer.put(data)
34+
position += data.size
35+
}
36+
37+
override fun position(): Long =
38+
position
2939
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package library
2+
3+
public class ParseException(public val position: Long, msg: String, e: Exception) : RuntimeException(msg, e)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package dltcore
2+
3+
import assertk.assertThat
4+
import assertk.assertions.isEqualTo
5+
import org.junit.Test
6+
7+
class DltExtensionsTest {
8+
@Test
9+
fun `test int to string`() {
10+
assertThat(0x30313233.asStringValue()).isEqualTo("0123")
11+
assertThat(0x30313200.asStringValue()).isEqualTo("012")
12+
assertThat(0x30310000.asStringValue()).isEqualTo("01")
13+
assertThat(0x30000000.asStringValue()).isEqualTo("0")
14+
assertThat(0x00000000.asStringValue()).isEqualTo("")
15+
}
16+
17+
@Test
18+
fun `test string to int`() {
19+
assertThat("1234".asIntValue()).isEqualTo(0x31323334)
20+
assertThat("123".asIntValue()).isEqualTo(0x31323300)
21+
assertThat("12".asIntValue()).isEqualTo(0x31320000)
22+
assertThat("1".asIntValue()).isEqualTo(0x31000000)
23+
}
24+
}
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
package library
2+
3+
import assertk.assertFailure
4+
import assertk.assertThat
5+
import assertk.assertions.hasClass
6+
import assertk.assertions.isEqualTo
7+
import assertk.assertions.isFalse
8+
import assertk.assertions.isTrue
9+
import org.junit.Test
10+
import java.nio.BufferUnderflowException
11+
12+
class ByteBufferBinaryInputStreamTest {
13+
@Test
14+
fun `test byte`() {
15+
ByteBufferBinaryInputStream(createSequencedByteBuffer(4)).let {
16+
it.order(ByteOrder.BIG_ENDIAN)
17+
assertThat(it.hasRemaining()).isTrue()
18+
assertThat(it.position()).isEqualTo(0)
19+
assertThat(it.readByte()).isEqualTo(0x00)
20+
assertThat(it.readByte()).isEqualTo(0x01)
21+
assertThat(it.readByte()).isEqualTo(0x02)
22+
assertThat(it.readByte()).isEqualTo(0x03)
23+
assertThat(it.hasRemaining()).isFalse()
24+
assertFailure {
25+
it.readByte()
26+
}.hasClass(BufferUnderflowException::class)
27+
}
28+
29+
ByteBufferBinaryInputStream(createSequencedByteBuffer(4)).let {
30+
it.order(ByteOrder.LITTLE_ENDIAN)
31+
assertThat(it.hasRemaining()).isTrue()
32+
assertThat(it.position()).isEqualTo(0)
33+
assertThat(it.readByte()).isEqualTo(0x00)
34+
assertThat(it.readByte()).isEqualTo(0x01)
35+
assertThat(it.readByte()).isEqualTo(0x02)
36+
assertThat(it.readByte()).isEqualTo(0x03)
37+
assertThat(it.hasRemaining()).isFalse()
38+
assertFailure {
39+
it.readByte()
40+
}.hasClass(BufferUnderflowException::class)
41+
}
42+
}
43+
44+
@Test
45+
fun `test short`() {
46+
ByteBufferBinaryInputStream(createSequencedByteBuffer(4)).let {
47+
it.order(ByteOrder.BIG_ENDIAN)
48+
assertThat(it.hasRemaining()).isTrue()
49+
assertThat(it.position()).isEqualTo(0)
50+
assertThat(it.readShort()).isEqualTo(0x0001)
51+
assertThat(it.hasRemaining()).isTrue()
52+
assertThat(it.position()).isEqualTo(2)
53+
assertThat(it.readShort()).isEqualTo(0x0203)
54+
assertThat(it.hasRemaining()).isFalse()
55+
assertFailure {
56+
it.readShort()
57+
}.hasClass(BufferUnderflowException::class)
58+
}
59+
60+
ByteBufferBinaryInputStream(createSequencedByteBuffer(4)).let {
61+
it.order(ByteOrder.LITTLE_ENDIAN)
62+
assertThat(it.hasRemaining()).isTrue()
63+
assertThat(it.position()).isEqualTo(0)
64+
assertThat(it.readShort()).isEqualTo(0x0100)
65+
assertThat(it.hasRemaining()).isTrue()
66+
assertThat(it.position()).isEqualTo(2)
67+
assertThat(it.readShort()).isEqualTo(0x0302)
68+
assertThat(it.hasRemaining()).isFalse()
69+
assertFailure {
70+
it.readShort()
71+
}.hasClass(BufferUnderflowException::class)
72+
}
73+
}
74+
75+
@Test
76+
fun `test int`() {
77+
ByteBufferBinaryInputStream(createSequencedByteBuffer(8)).let {
78+
it.order(ByteOrder.BIG_ENDIAN)
79+
assertThat(it.hasRemaining()).isTrue()
80+
assertThat(it.position()).isEqualTo(0)
81+
assertThat(it.readInt()).isEqualTo(0x00010203)
82+
assertThat(it.hasRemaining()).isTrue()
83+
assertThat(it.position()).isEqualTo(4)
84+
assertThat(it.readInt()).isEqualTo(0x04050607)
85+
assertThat(it.hasRemaining()).isFalse()
86+
assertFailure {
87+
it.readInt()
88+
}.hasClass(BufferUnderflowException::class)
89+
}
90+
91+
ByteBufferBinaryInputStream(createSequencedByteBuffer(8)).let {
92+
it.order(ByteOrder.LITTLE_ENDIAN)
93+
assertThat(it.hasRemaining()).isTrue()
94+
assertThat(it.position()).isEqualTo(0)
95+
assertThat(it.readInt()).isEqualTo(0x03020100)
96+
assertThat(it.hasRemaining()).isTrue()
97+
assertThat(it.position()).isEqualTo(4)
98+
assertThat(it.readInt()).isEqualTo(0x07060504)
99+
assertThat(it.hasRemaining()).isFalse()
100+
assertFailure {
101+
it.readInt()
102+
}.hasClass(BufferUnderflowException::class)
103+
}
104+
}
105+
106+
@Test
107+
fun `test long`() {
108+
ByteBufferBinaryInputStream(createSequencedByteBuffer(16)).let {
109+
it.order(ByteOrder.BIG_ENDIAN)
110+
assertThat(it.hasRemaining()).isTrue()
111+
assertThat(it.position()).isEqualTo(0)
112+
assertThat(it.readLong()).isEqualTo(0x0001020304050607)
113+
assertThat(it.hasRemaining()).isTrue()
114+
assertThat(it.position()).isEqualTo(8)
115+
assertThat(it.readLong()).isEqualTo(0x08090a0b0c0d0e0f)
116+
assertThat(it.hasRemaining()).isFalse()
117+
assertFailure {
118+
it.readLong()
119+
}.hasClass(BufferUnderflowException::class)
120+
}
121+
122+
ByteBufferBinaryInputStream(createSequencedByteBuffer(16)).let {
123+
it.order(ByteOrder.LITTLE_ENDIAN)
124+
assertThat(it.hasRemaining()).isTrue()
125+
assertThat(it.position()).isEqualTo(0)
126+
assertThat(it.readLong()).isEqualTo(0x0706050403020100)
127+
assertThat(it.hasRemaining()).isTrue()
128+
assertThat(it.position()).isEqualTo(8)
129+
assertThat(it.readLong()).isEqualTo(0x0f0e0d0c0b0a0908)
130+
assertThat(it.hasRemaining()).isFalse()
131+
assertFailure {
132+
it.readLong()
133+
}.hasClass(BufferUnderflowException::class)
134+
}
135+
}
136+
137+
@Test
138+
fun `test array`() {
139+
ByteBufferBinaryInputStream(createSequencedByteBuffer(64)).let {
140+
assertThat(it.readArray(32)).isEqualTo(createSequencedByteArray(32))
141+
assertThat(it.position()).isEqualTo(32)
142+
assertThat(it.readArray(16)).isEqualTo(createSequencedByteArray(16, 32))
143+
assertThat(it.position()).isEqualTo(48)
144+
assertThat(it.readArray(16)).isEqualTo(createSequencedByteArray(16, 48))
145+
assertThat(it.position()).isEqualTo(64)
146+
assertFailure {
147+
it.readArray(1)
148+
}.hasClass(BufferUnderflowException::class)
149+
}
150+
}
151+
}

0 commit comments

Comments
 (0)