0.0.4-rc: fixed a bug in variable-size short field (rare situation and most often masked).
This commit is contained in:
parent
b7784fec18
commit
e1c78e2bbb
@ -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<UInt>().toShort() else input.readNumber()
|
||||
else if (nextIsUnsigned)
|
||||
input.readNumber<UInt>().toShort()
|
||||
else
|
||||
input.readNumber()
|
||||
override fun decodeInt(): Int =
|
||||
if (fixedNumber) input.readI32()
|
||||
else if (nextIsUnsigned) input.readNumber<UInt>().toInt() else input.readNumber()
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
@ -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<Instant>(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<UInts>(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<VarUInts>(pv)
|
||||
assertEquals(xv, yv)
|
||||
println(xv)
|
||||
println(yv)
|
||||
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user