Files
mp_bintools/src/commonTest/kotlin/bintools/SmartintTest.kt
T

78 lines
2.2 KiB
Kotlin

package bintools
import net.sergeych.bintools.*
import kotlin.test.Test
import kotlin.test.assertEquals
class SmartintTest {
inline fun <reified T : Number> testValue(x: T) {
assertEquals(x, Smartint.decode(Smartint.encode(x)))
println("+ ${x}: ${Smartint.encode(x).encodeToHex()}")
}
fun testAround(bits: Int) {
println("----- $bits -----")
val window = 5
var median: Long = (1.toULong() shl bits).toLong()
for (x in (median - 2)..(median + 2)) {
testValue(x)
testValue(-x)
}
median = median * 3 / 2
for (x in (median - 5)..(median + 5)) {
testValue(x)
testValue(-x)
}
}
@Test
fun encode() {
for (i in 0..70) testValue(i.toLong())
testAround(6)
testAround(14)
testAround(22)
testAround(29)
testAround(36)
testValue(-1)
// testAround(28)
}
// @Test
fun compareTest() {
// for( x in listOf<ULong>(0uL, 1uL, 66uL, 129uL, 219uL, 0x1122uL, 0xFFEEuL, 0xAAbbCCdduL,
// 0x1111222233334444uL, (1UL shl 63))) {
var lastDelta = 0
for (x in 0..3000000000) {
val n1 = Varint.encode(x).size
val n2 = Smartint.encode(x).size
val delta = n1 - n2
if (delta != lastDelta) {
when {
delta < 0 -> println("LOSS: $delta from $x")
delta > 0 -> println("GAIN: $delta from $x")
delta == 0 -> println("SAME: from $x")
}
lastDelta = delta
}
// for( x in listOf<ULong>(0uL, 1uL, (1UL shl 17)+11UL)) {
// 0x1111222233334444uL, (1UL shl 63))) {
// 1--12--23--34--4
// println("--- $x / 0x${x.encodeToHex(8)}")
// println("V: ${Varint.encode(x).encodeToHex()}")
// println("S: ${Smartint.encode(x).encodeToHex()}")
// }
}
}
@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)
}
}