From e1c78e2bbbb025d3aa6d853144bd1b4446edc733 Mon Sep 17 00:00:00 2001 From: sergeych Date: Sat, 7 Oct 2023 01:50:26 +0100 Subject: [PATCH] 0.0.4-rc: fixed a bug in variable-size short field (rare situation and most often masked). --- .../net.sergeych.bipack/BipackDecoder.kt | 7 ++- .../net.sergeych.bipack/BipackEncoder.kt | 6 +-- .../kotlin/bintools/SmartintTest.kt | 10 +++++ .../kotlin/bipack/BipackEncoderTest.kt | 45 +++++++++++++++++++ 4 files changed, 63 insertions(+), 5 deletions(-) diff --git a/src/commonMain/kotlin/net.sergeych.bipack/BipackDecoder.kt b/src/commonMain/kotlin/net.sergeych.bipack/BipackDecoder.kt index 305394e..f22a068 100644 --- a/src/commonMain/kotlin/net.sergeych.bipack/BipackDecoder.kt +++ b/src/commonMain/kotlin/net.sergeych.bipack/BipackDecoder.kt @@ -27,12 +27,15 @@ class BipackDecoder( private var fixedSize = -1 private var fixedNumber = false - override val serializersModule: SerializersModule = EmptySerializersModule + override val serializersModule: SerializersModule = EmptySerializersModule() override fun decodeBoolean(): Boolean = input.readByte().toInt() != 0 override fun decodeByte(): Byte = input.readByte() override fun decodeShort(): Short = if( fixedNumber ) input.readI16() - else if (nextIsUnsigned) input.readNumber().toShort() else input.readNumber() + else if (nextIsUnsigned) + input.readNumber().toShort() + else + input.readNumber() override fun decodeInt(): Int = if (fixedNumber) input.readI32() else if (nextIsUnsigned) input.readNumber().toInt() else input.readNumber() diff --git a/src/commonMain/kotlin/net.sergeych.bipack/BipackEncoder.kt b/src/commonMain/kotlin/net.sergeych.bipack/BipackEncoder.kt index 5e0f8eb..6d80e8c 100644 --- a/src/commonMain/kotlin/net.sergeych.bipack/BipackEncoder.kt +++ b/src/commonMain/kotlin/net.sergeych.bipack/BipackEncoder.kt @@ -28,15 +28,15 @@ class BipackEncoder(val output: DataSink) : AbstractEncoder() { } } - override val serializersModule: SerializersModule = EmptySerializersModule + override val serializersModule: SerializersModule = EmptySerializersModule() override fun encodeBoolean(value: Boolean) = output.writeByte(if (value) 1 else 0) override fun encodeByte(value: Byte) = output.writeByte(value.toInt()) override fun encodeShort(value: Short) = if (fixedNumber) output.writeI16(value) else if (nextIsUnsigned) - output.writeNumber(value.toUInt()) + output.writeNumber(value.toUShort()) else - output.writeNumber(value.toInt()) + output.writeNumber(value) override fun encodeInt(value: Int) = if (fixedNumber) diff --git a/src/commonTest/kotlin/bintools/SmartintTest.kt b/src/commonTest/kotlin/bintools/SmartintTest.kt index 33a52d2..478ff04 100644 --- a/src/commonTest/kotlin/bintools/SmartintTest.kt +++ b/src/commonTest/kotlin/bintools/SmartintTest.kt @@ -65,4 +65,14 @@ class SmartintTest { } } + @Test + fun testCustom() { + val x = 64000.toULong() + val p = Smartint.encodeUnsigned(x) + println(p.toDump()) + val y = Smartint.decodeUnsigned(p) + println(y) + assertEquals(y, x) + } + } \ No newline at end of file diff --git a/src/commonTest/kotlin/bipack/BipackEncoderTest.kt b/src/commonTest/kotlin/bipack/BipackEncoderTest.kt index a01f625..7cb24aa 100644 --- a/src/commonTest/kotlin/bipack/BipackEncoderTest.kt +++ b/src/commonTest/kotlin/bipack/BipackEncoderTest.kt @@ -7,6 +7,7 @@ import kotlinx.serialization.Serializable import net.sergeych.bintools.encodeToHex import net.sergeych.bintools.toDump import net.sergeych.bipack.* +import net.sergeych.mp_tools.encodeToBase64Compact import kotlin.experimental.xor import kotlin.test.Test import kotlin.test.assertContentEquals @@ -314,6 +315,7 @@ class BipackEncoderTest { @Serializable data class FU16(@Fixed val i: UShort) + @Serializable class Foo( @Fixed @@ -351,4 +353,47 @@ class BipackEncoderTest { val y = BipackDecoder.decode(BipackEncoder.encode(x)) assertEquals(x.toEpochMilliseconds(), y.toEpochMilliseconds()) } + + @Serializable + data class UInts( + val b: UByte, + @Fixed + val si: UShort, + @Fixed + val i: UInt, + @Fixed + val li: ULong + ) + + @Serializable + data class VarUInts( + val b: UByte, + @Unsigned + val si: UShort, + @Unsigned + val i: UInt, + @Unsigned + val li: ULong + ) + + @Test + fun vectors() { + val x = UInts(7u, 64000.toUShort(), 66000u, 931127140399u); + val p = BipackEncoder.encode(x); + println(p.toDump()) + println(p.encodeToBase64Compact()) + val y = BipackDecoder.decode(p) + assertEquals(x, y) + + val xv = VarUInts(7u, 64000.toUShort(), 66000u, 931127140399u); + val pv = BipackEncoder.encode(xv); + println(pv.toDump()) + println(pv.encodeToBase64Compact()) + val yv = BipackDecoder.decode(pv) + assertEquals(xv, yv) + println(xv) + println(yv) + + + } } \ No newline at end of file