fixed bugs in types, added more tests

This commit is contained in:
sergeych committed 2023-03-13 21:49:09 +01:00
1 parent 0e8f3daf99
commit 0b1cd2d9cf
5 files changed
+70 -14

No files matched your search

@@ -13,7 +13,7 @@ interface DataSource {
/**
* 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
@@ -32,6 +32,7 @@ interface DataSource {
fun readI64(): Long = bytesToLong(readBytes(8))
fun readDouble() = Double.fromBits(readI64())
fun readFloat() = Float.fromBits(readI32())
}
@@ -42,11 +43,11 @@ fun ByteArray.toDataSource(): DataSource =
private set
override fun readByte(): Byte =
if( position < size ) this@toDataSource[position++]
else throw DataSource.EndOfData()
if (position < size) this@toDataSource[position++]
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<Float>() -> readFloat() as T
else -> Smartint.decode(this)
@@ -13,7 +13,7 @@ fun longToBytes(value: Long): ByteArray {
fun intToBytes(value: Int): ByteArray {
var l = value
val result = ByteArray(8)
val result = ByteArray(4)
for (i in 3 downTo 0) {
result[i] = (l and 0xFF).toByte()
l = l shr 8
@@ -77,6 +77,7 @@ class BipackDecoder(val input: DataSource, var elementsCount: Int = 0) : Abstrac
super.endStructure(descriptor)
}
override fun decodeSequentially(): Boolean = true
override fun decodeCollectionSize(descriptor: SerialDescriptor): Int =
input.readNumber<UInt>().toInt().also {
elementsCount = it
@@ -96,3 +97,5 @@ class BipackDecoder(val input: DataSource, var elementsCount: Int = 0) : Abstrac
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 {
return super.encodeElement(descriptor, index).also {
println(">> $index: ${descriptor.getElementAnnotations(index)}")
nextIsUnsigned = descriptor.getElementAnnotations(index).any { it is Unsigned }
println("${descriptor.getElementDescriptor(index)} -> $nextIsUnsigned")
}
}
// fun isUnsigned(): Boolean {
@@ -37,7 +35,6 @@ class BipackEncoder(var output: DataSink) : AbstractEncoder() {
else
output.writeNumber(value.toInt())
override fun encodeInt(value: Int) {
println("EncodeInt: $value / $nextIsUnsigned")
if (nextIsUnsigned)
output.writeNumber(value.toUInt())
else