fixed bugs in types, added more tests
This commit is contained in:
parent
0e8f3daf99
commit
0b1cd2d9cf
@ -13,7 +13,7 @@ interface DataSource {
|
|||||||
/**
|
/**
|
||||||
* Exception that implementations must throw on end of data
|
* Exception that implementations must throw on end of data
|
||||||
*/
|
*/
|
||||||
class EndOfData: Exception("no more data available")
|
class EndOfData : Exception("no more data available")
|
||||||
|
|
||||||
fun readByte(): Byte
|
fun readByte(): Byte
|
||||||
|
|
||||||
@ -32,6 +32,7 @@ interface DataSource {
|
|||||||
fun readI64(): Long = bytesToLong(readBytes(8))
|
fun readI64(): Long = bytesToLong(readBytes(8))
|
||||||
|
|
||||||
fun readDouble() = Double.fromBits(readI64())
|
fun readDouble() = Double.fromBits(readI64())
|
||||||
|
|
||||||
fun readFloat() = Float.fromBits(readI32())
|
fun readFloat() = Float.fromBits(readI32())
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -42,11 +43,11 @@ fun ByteArray.toDataSource(): DataSource =
|
|||||||
private set
|
private set
|
||||||
|
|
||||||
override fun readByte(): Byte =
|
override fun readByte(): Byte =
|
||||||
if( position < size ) this@toDataSource[position++]
|
if (position < size) this@toDataSource[position++]
|
||||||
else throw DataSource.EndOfData()
|
else throw DataSource.EndOfData()
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <reified T : Any> DataSource.readNumber(): T = when(typeOf<T>()) {
|
inline fun <reified T : Any> DataSource.readNumber(): T = when (typeOf<T>()) {
|
||||||
typeOf<Double>() -> readDouble() as T
|
typeOf<Double>() -> readDouble() as T
|
||||||
typeOf<Float>() -> readFloat() as T
|
typeOf<Float>() -> readFloat() as T
|
||||||
else -> Smartint.decode(this)
|
else -> Smartint.decode(this)
|
||||||
|
@ -13,7 +13,7 @@ fun longToBytes(value: Long): ByteArray {
|
|||||||
|
|
||||||
fun intToBytes(value: Int): ByteArray {
|
fun intToBytes(value: Int): ByteArray {
|
||||||
var l = value
|
var l = value
|
||||||
val result = ByteArray(8)
|
val result = ByteArray(4)
|
||||||
for (i in 3 downTo 0) {
|
for (i in 3 downTo 0) {
|
||||||
result[i] = (l and 0xFF).toByte()
|
result[i] = (l and 0xFF).toByte()
|
||||||
l = l shr 8
|
l = l shr 8
|
||||||
|
@ -77,6 +77,7 @@ class BipackDecoder(val input: DataSource, var elementsCount: Int = 0) : Abstrac
|
|||||||
super.endStructure(descriptor)
|
super.endStructure(descriptor)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun decodeSequentially(): Boolean = true
|
||||||
override fun decodeCollectionSize(descriptor: SerialDescriptor): Int =
|
override fun decodeCollectionSize(descriptor: SerialDescriptor): Int =
|
||||||
input.readNumber<UInt>().toInt().also {
|
input.readNumber<UInt>().toInt().also {
|
||||||
elementsCount = it
|
elementsCount = it
|
||||||
@ -96,3 +97,5 @@ class BipackDecoder(val input: DataSource, var elementsCount: Int = 0) : Abstrac
|
|||||||
decode(source.toDataSource(), serializer())
|
decode(source.toDataSource(), serializer())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline fun <reified T>ByteArray.decodeFromBipack() = BipackDecoder.decode<T>(this)
|
||||||
|
@ -18,9 +18,7 @@ class BipackEncoder(var output: DataSink) : AbstractEncoder() {
|
|||||||
|
|
||||||
override fun encodeElement(descriptor: SerialDescriptor, index: Int): Boolean {
|
override fun encodeElement(descriptor: SerialDescriptor, index: Int): Boolean {
|
||||||
return super.encodeElement(descriptor, index).also {
|
return super.encodeElement(descriptor, index).also {
|
||||||
println(">> $index: ${descriptor.getElementAnnotations(index)}")
|
|
||||||
nextIsUnsigned = descriptor.getElementAnnotations(index).any { it is Unsigned }
|
nextIsUnsigned = descriptor.getElementAnnotations(index).any { it is Unsigned }
|
||||||
println("${descriptor.getElementDescriptor(index)} -> $nextIsUnsigned")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// fun isUnsigned(): Boolean {
|
// fun isUnsigned(): Boolean {
|
||||||
@ -37,7 +35,6 @@ class BipackEncoder(var output: DataSink) : AbstractEncoder() {
|
|||||||
else
|
else
|
||||||
output.writeNumber(value.toInt())
|
output.writeNumber(value.toInt())
|
||||||
override fun encodeInt(value: Int) {
|
override fun encodeInt(value: Int) {
|
||||||
println("EncodeInt: $value / $nextIsUnsigned")
|
|
||||||
if (nextIsUnsigned)
|
if (nextIsUnsigned)
|
||||||
output.writeNumber(value.toUInt())
|
output.writeNumber(value.toUInt())
|
||||||
else
|
else
|
||||||
|
@ -16,7 +16,8 @@ data class Foobar1(val bar: Int, val foo: Int = 117)
|
|||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
@ExtendableFormat
|
@ExtendableFormat
|
||||||
data class Foobar2(val bar: Int, val foo: Int,val other: Int = -1)
|
data class Foobar2(val bar: Int, val foo: Int, val other: Int = -1)
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
@Framed
|
@Framed
|
||||||
@ExtendableFormat
|
@ExtendableFormat
|
||||||
@ -26,17 +27,18 @@ data class FoobarF1(val bar: Int, val foo: Int = 117)
|
|||||||
@Framed
|
@Framed
|
||||||
@ExtendableFormat
|
@ExtendableFormat
|
||||||
@SerialName("bipack.FoobarF1")
|
@SerialName("bipack.FoobarF1")
|
||||||
data class FoobarF2(val bar: Int, val foo: Int,val other: Int = -1)
|
data class FoobarF2(val bar: Int, val foo: Int, val other: Int = -1)
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
@Framed
|
@Framed
|
||||||
@ExtendableFormat
|
@ExtendableFormat
|
||||||
data class FoobarF3(val bar: Int, val foo: Int,val other: Int = -1)
|
data class FoobarF3(val bar: Int, val foo: Int, val other: Int = -1)
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
@Framed
|
@Framed
|
||||||
@ExtendableFormat
|
@ExtendableFormat
|
||||||
@CrcProtected()
|
@CrcProtected()
|
||||||
data class FoobarFP1(val bar: Int, val foo: Int,val other: Int = -1)
|
data class FoobarFP1(val bar: Int, val foo: Int, val other: Int = -1)
|
||||||
|
|
||||||
class BipackEncoderTest {
|
class BipackEncoderTest {
|
||||||
|
|
||||||
@ -61,6 +63,7 @@ class BipackEncoderTest {
|
|||||||
assertEquals(-1, c.other)
|
assertEquals(-1, c.other)
|
||||||
assertEquals(a.bar, c.bar)
|
assertEquals(a.bar, c.bar)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun encodeFramed() {
|
fun encodeFramed() {
|
||||||
val a = FoobarF1(42)//, "bum")
|
val a = FoobarF1(42)//, "bum")
|
||||||
@ -103,13 +106,15 @@ class BipackEncoderTest {
|
|||||||
val u: UInt,
|
val u: UInt,
|
||||||
@Unsigned
|
@Unsigned
|
||||||
val i: Int,
|
val i: Int,
|
||||||
val k: UInt = 3u)
|
val k: UInt = 3u,
|
||||||
|
)
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testByteArray() {
|
fun testByteArray() {
|
||||||
// val z = Foobar1(42)//, "bum")
|
// val z = Foobar1(42)//, "bum")
|
||||||
// println(BipackEncoder.encode(z).toDump())
|
// println(BipackEncoder.encode(z).toDump())
|
||||||
|
|
||||||
val x = byteArrayOf(1,2,3)
|
val x = byteArrayOf(1, 2, 3)
|
||||||
var d = BipackEncoder.encode(x)
|
var d = BipackEncoder.encode(x)
|
||||||
println(d.toDump())
|
println(d.toDump())
|
||||||
assertEquals(0x0c, d[0])
|
assertEquals(0x0c, d[0])
|
||||||
@ -128,4 +133,54 @@ class BipackEncoderTest {
|
|||||||
|
|
||||||
assertEquals(f, BipackDecoder.decode(d))
|
assertEquals(f, BipackDecoder.decode(d))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class Types1(
|
||||||
|
val i: Int = 7,
|
||||||
|
val f: Float = 1f/3f,
|
||||||
|
val d: Double = 1.0/3.0,
|
||||||
|
val b: Boolean = true,
|
||||||
|
val s: String = "жёпа",
|
||||||
|
val ba: ByteArray = byteArrayOf(1,2,3),
|
||||||
|
val ch: Char = 'Ы',
|
||||||
|
) {
|
||||||
|
override fun equals(other: Any?): Boolean {
|
||||||
|
if (this === other) return true
|
||||||
|
if (other == null || this::class != other::class) return false
|
||||||
|
|
||||||
|
other as Types1
|
||||||
|
|
||||||
|
if (i != other.i) return false
|
||||||
|
if (f != other.f) return false
|
||||||
|
if (d != other.d) return false
|
||||||
|
if (b != other.b) return false
|
||||||
|
if (s != other.s) return false
|
||||||
|
if (!ba.contentEquals(other.ba)) return false
|
||||||
|
if (ch != other.ch) return false
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun hashCode(): Int {
|
||||||
|
var result = i
|
||||||
|
result = 31 * result + f.hashCode()
|
||||||
|
result = 31 * result + d.hashCode()
|
||||||
|
result = 31 * result + b.hashCode()
|
||||||
|
result = 31 * result + s.hashCode()
|
||||||
|
result = 31 * result + ba.contentHashCode()
|
||||||
|
result = 31 * result + ch.hashCode()
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testTypes() {
|
||||||
|
// val t1 = Types1(10, 1.0f / 10.0f, 1.0 / 10.0, true, "жесть", byteArrayOf(1, 2, 3), 'Ы')
|
||||||
|
val t1 = Types1(f=17f/7f)
|
||||||
|
val d = BipackEncoder.encode(t1)
|
||||||
|
// println(d.toDump())
|
||||||
|
// println(t1)
|
||||||
|
// println(d.decodeFromBipack<Types1>())
|
||||||
|
assertEquals(t1, d.decodeFromBipack())
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user