refs #35 minimal serialization of simple types in bit-effective format

This commit is contained in:
sergeych committed 2025-07-12 00:46:51 +03:00
1 parent d969993997
commit f26ee7cd7c
44 files changed
+537 -49

No files matched your search

+1 -1
View File
@@ -4,8 +4,8 @@ import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import net.sergeych.lyng.ObjVoid
import net.sergeych.lyng.Scope
import net.sergeych.lyng.obj.ObjVoid
import java.nio.file.Files
import java.nio.file.Files.readAllLines
import java.nio.file.Paths
+139 -9
View File
@@ -1,8 +1,8 @@
import junit.framework.TestCase.assertEquals
import net.sergeych.bintools.toDump
import net.sergeych.lynon.MemoryBitInput
import net.sergeych.lynon.MemoryBitOutput
import net.sergeych.lynon.sizeInTetrades
import junit.framework.TestCase.*
import kotlinx.coroutines.test.runTest
import net.sergeych.lyng.Scope
import net.sergeych.lyng.obj.*
import net.sergeych.lynon.*
import kotlin.test.Test
class LynonTests {
@@ -19,14 +19,23 @@ class LynonTests {
assertEquals(3, sizeInTetrades(257u))
}
@Test
fun testSizeInBits() {
assertEquals(1, sizeInBits(0u))
assertEquals(1, sizeInBits(1u))
assertEquals(2, sizeInBits(2u))
assertEquals(2, sizeInBits(3u))
assertEquals(4, sizeInBits(15u))
}
@Test
fun testBitStreams() {
val bout = MemoryBitOutput()
bout.putBits(2, 3)
bout.putBits(1, 7)
bout.putBits( 197, 8)
bout.putBits( 3, 4)
bout.putBits(197, 8)
bout.putBits(3, 4)
bout.close()
val bin = MemoryBitInput(bout.toUByteArray())
@@ -41,11 +50,28 @@ class LynonTests {
val bout = MemoryBitOutput()
bout.packUnsigned(1471792UL)
bout.close()
println(bout.toUByteArray().toDump())
val bin = MemoryBitInput(bout.toUByteArray())
assertEquals(1471792UL, bin.unpackUnsigned())
}
@Test
fun testUnsignedPackLongInteger() {
val bout = MemoryBitOutput()
bout.packUnsigned(ULong.MAX_VALUE)
bout.close()
val bin = MemoryBitInput(bout.toUByteArray())
assertEquals(ULong.MAX_VALUE, bin.unpackUnsigned())
}
@Test
fun testUnsignedPackLongSmallInteger() {
val bout = MemoryBitOutput()
bout.packUnsigned(7UL)
bout.close()
val bin = MemoryBitInput(bout.toUByteArray())
assertEquals(7UL, bin.unpackUnsigned())
}
@Test
fun testSignedPackInteger() {
val bout = MemoryBitOutput()
@@ -53,10 +79,114 @@ class LynonTests {
bout.packSigned(1471792L)
// bout.packSigned(147179L)
bout.close()
println(bout.toUByteArray().toDump())
val bin = MemoryBitInput(bout.toUByteArray())
assertEquals(-1471792L, bin.unpackSigned())
assertEquals(1471792L, bin.unpackSigned())
}
@Test
fun testCache1() = runTest {
val bout = MemoryBitOutput()
val encoder = LynonEncoder(bout)
val s = "Hello, World!".toObj()
val scope = Scope()
encoder.packObject(scope, s) // 1
encoder.packObject(scope, s)
encoder.packObject(scope, s)
encoder.packObject(scope, s)
encoder.packObject(scope, s)
encoder.packObject(scope, s)
encoder.packObject(scope, s)
encoder.packObject(scope, s) // 8
val decoder = LynonDecoder(MemoryBitInput(bout))
val s1 = decoder.unpackObject(scope, ObjString.type) // 1
assertEquals(s, s1)
assertNotSame(s, s1)
val s2 = decoder.unpackObject(scope, ObjString.type)
assertEquals(s, s2)
assertSame(s1, s2)
assertSame(s1, decoder.unpackObject(scope, ObjString.type))
assertSame(s1, decoder.unpackObject(scope, ObjString.type))
assertSame(s1, decoder.unpackObject(scope, ObjString.type))
assertSame(s1, decoder.unpackObject(scope, ObjString.type))
assertSame(s1, decoder.unpackObject(scope, ObjString.type))
assertSame(s1, decoder.unpackObject(scope, ObjString.type)) // 8
}
@Test
fun testCache2() = runTest {
val variants = (100..500).map { "Sample $it".toObj() }.shuffled()
var source = variants.shuffled()
for (i in 0..300) source += variants.shuffled()
val encoder = LynonPacker()
val scope = Scope()
for (s in source) {
encoder.packObject(scope, s)
}
val decoder = LynonUnpacker(encoder.toUByteArray())
val restored = mutableListOf<Obj>()
for (i in source.indices) {
restored.add(decoder.unpackObject(scope, ObjString.type))
}
assertEquals(restored, source)
}
@Test
fun testUnpackBoolean() = runTest {
val scope = Scope()
val decoder = LynonUnpacker(LynonPacker().apply {
packObject(scope, ObjBool(true))
packObject(scope, ObjBool(false))
packObject(scope, ObjBool(true))
packObject(scope, ObjBool(true))
}.toUByteArray())
assertEquals(ObjTrue, decoder.unpackObject(scope, ObjBool.type))
assertEquals(ObjFalse, decoder.unpackObject(scope, ObjBool.type))
assertEquals(ObjTrue, decoder.unpackObject(scope, ObjBool.type))
assertEquals(ObjTrue, decoder.unpackObject(scope, ObjBool.type))
}
@Test
fun testUnpackReal() = runTest {
val scope = Scope()
val decoder = LynonUnpacker(LynonPacker().apply {
packObject(scope, ObjReal(-Math.PI))
packObject(scope, ObjReal(Math.PI))
packObject(scope, ObjReal(-Math.PI))
packObject(scope, ObjReal(Math.PI))
packObject(scope, ObjReal(Double.NaN))
packObject(scope, ObjReal(Double.NEGATIVE_INFINITY))
packObject(scope, ObjReal(Double.POSITIVE_INFINITY))
packObject(scope, ObjReal(Double.MIN_VALUE))
packObject(scope, ObjReal(Double.MAX_VALUE))
}.toUByteArray())
assertEquals(ObjReal(-Math.PI), decoder.unpackObject(scope, ObjReal.type))
assertEquals(ObjReal(Math.PI), decoder.unpackObject(scope, ObjReal.type))
assertEquals(ObjReal(-Math.PI), decoder.unpackObject(scope, ObjReal.type))
assertEquals(ObjReal(Math.PI), decoder.unpackObject(scope, ObjReal.type))
assert((decoder.unpackObject(scope, ObjReal.type)).toDouble().isNaN())
assertEquals(ObjReal(Double.NEGATIVE_INFINITY), decoder.unpackObject(scope, ObjReal.type))
assertEquals(ObjReal(Double.POSITIVE_INFINITY), decoder.unpackObject(scope, ObjReal.type))
assertEquals(ObjReal(Double.MIN_VALUE), decoder.unpackObject(scope, ObjReal.type))
assertEquals(ObjReal(Double.MAX_VALUE), decoder.unpackObject(scope, ObjReal.type))
}
@Test
fun testUnpackInt() = runTest {
val scope = Scope()
val decoder = LynonUnpacker(LynonPacker().apply {
packObject(scope, ObjInt(0))
packObject(scope, ObjInt(-1))
packObject(scope, ObjInt(23))
packObject(scope, ObjInt(Long.MIN_VALUE))
packObject(scope, ObjInt(Long.MAX_VALUE))
packObject(scope, ObjInt(Long.MAX_VALUE))
}.toUByteArray())
assertEquals(ObjInt(0), decoder.unpackObject(scope, ObjInt.type))
assertEquals(ObjInt(-1), decoder.unpackObject(scope, ObjInt.type))
assertEquals(ObjInt(23), decoder.unpackObject(scope, ObjInt.type))
assertEquals(ObjInt(Long.MIN_VALUE), decoder.unpackObject(scope, ObjInt.type))
assertEquals(ObjInt(Long.MAX_VALUE), decoder.unpackObject(scope, ObjInt.type))
assertEquals(ObjInt(Long.MAX_VALUE), decoder.unpackObject(scope, ObjInt.type))
}
}