AJWCW2MWRGZXHVNGX5M6S4RMBNG6AGMYK32XJHPF44EYGDRGAZKQC 1, AQ==127, fw==128, gAE=255, /wE=300, rAI=16384, gIAB
package multiformatsimport java.io.Fileimport java.math.BigIntegerimport java.util.Base64import kotlin.ExperimentalUnsignedTypesimport kotlin.math.minimport kotlin.random.Randomimport kotlin.test.*import kotlin.test.Testimport kotlin.ubyteArrayOfval decoder = Base64.getDecoder()@kotlin.ExperimentalUnsignedTypesclass UnsignedVarIntTest {@Testfun exampleValuesFromSpec() {val testVectors: List<Pair<BigInteger, ByteArray>> =File("../../../spec/multiformats/unsigned-varint-examples.csv").readLines().map { it.split(',') }.map { Pair(it.component1().trim(), it.component2().trim()) }.map { (int, bytes) -> Pair(int.toBigInteger(), decoder.decode(bytes)) }testVectors.forEach { (int, bytes) ->assertContentEquals(bytes, UnsignedVarInt(int).bytes)assertEquals(int, UnsignedVarInt(bytes).int)}}@Testfun randomValues() {for (i in 1..1000) {val cieling = min(Long.MAX_VALUE, UnsignedVarInt.PRACTICAL_MAX.toLong())val random = Random.nextLong(cieling)assertEquals(random,UnsignedVarInt(UnsignedVarInt(random.toBigInteger()).bytes).int.toLong())}}@Testfun throwsErrors() {// Negatives are not validassertFailsWith(IllegalArgumentException::class) {UnsignedVarInt.digits(-1.toBigInteger())}assertFailsWith(IllegalArgumentException::class) { UnsignedVarInt(-1.toBigInteger()) }assertFailsWith(IllegalArgumentException::class) {UnsignedVarInt.encode(-1.toBigInteger())}// PRACTICAL_MAX is tipping pointUnsignedVarInt(UnsignedVarInt.PRACTICAL_MAX)UnsignedVarInt(UnsignedVarInt(UnsignedVarInt.PRACTICAL_MAX).bytes)assertFailsWith(IllegalArgumentException::class) {UnsignedVarInt(UnsignedVarInt.PRACTICAL_MAX + 1.toBigInteger())}assertFailsWith(IllegalArgumentException::class) {UnsignedVarInt.digits(UnsignedVarInt.PRACTICAL_MAX + 1.toBigInteger())}assertFailsWith(IllegalArgumentException::class) {UnsignedVarInt.encode(UnsignedVarInt.PRACTICAL_MAX + 1.toBigInteger())}// Empty nor several nulls are validassertFailsWith(IllegalArgumentException::class) { UnsignedVarInt(byteArrayOf()) }for (i in 2..UnsignedVarInt.PRACTICAL_MAX_BYTES) {assertFailsWith(IllegalArgumentException::class) { UnsignedVarInt(ByteArray(i) { 0 }) }}// Last should be less than 128for (i in 2..UnsignedVarInt.PRACTICAL_MAX_BYTES) {assertFailsWith(IllegalArgumentException::class) {UnsignedVarInt(ByteArray(i) { 128.toUByte().toByte() })}}// PRACTICAL_MAX_BYTES is tipping pointUnsignedVarInt((UByteArray(UnsignedVarInt.PRACTICAL_MAX_BYTES - 1) { 255.toUByte() } +ubyteArrayOf(127.toUByte())).toByteArray())assertFailsWith(IllegalArgumentException::class) {UnsignedVarInt((UByteArray(UnsignedVarInt.PRACTICAL_MAX_BYTES) { 255.toUByte() } +ubyteArrayOf(127.toUByte())).toByteArray())}}}
package multiformatsimport java.io.Fileimport java.math.BigIntegerimport java.util.Base64import kotlin.ExperimentalUnsignedTypesimport kotlin.math.minimport kotlin.random.Randomimport kotlin.test.*import kotlin.test.Testimport kotlin.ubyteArrayOfval decoder = Base64.getDecoder()@kotlin.ExperimentalUnsignedTypesclass UnsignedVarIntTest {@Testfun exampleValuesFromSpec() {val testVectors: List<Pair<BigInteger, ByteArray>> =File("../../../spec/multiformats/unsigned-varint-examples.csv").readLines().map { it.split(',') }.map { Pair(it.component1().trim(), it.component2().trim()) }.map { (int, bytes) -> Pair(int.toBigInteger(), decoder.decode(bytes)) }testVectors.forEach { (int, bytes) ->assertContentEquals(bytes, UnsignedVarInt(int).bytes)assertEquals(int, UnsignedVarInt(bytes).int)}}@Testfun randomValues() {for (i in 1..1000) {val cieling = min(Long.MAX_VALUE, UnsignedVarInt.PRACTICAL_MAX.toLong())val random = Random.nextLong(cieling)assertEquals(random,UnsignedVarInt(UnsignedVarInt(random.toBigInteger()).bytes).int.toLong())}}@Testfun throwsErrors() {// Negatives are not validassertFailsWith(IllegalArgumentException::class) {UnsignedVarInt.digits(-1.toBigInteger())}assertFailsWith(IllegalArgumentException::class) { UnsignedVarInt(-1.toBigInteger()) }assertFailsWith(IllegalArgumentException::class) {UnsignedVarInt.encode(-1.toBigInteger())}// PRACTICAL_MAX is tipping pointUnsignedVarInt(UnsignedVarInt.PRACTICAL_MAX)UnsignedVarInt(UnsignedVarInt(UnsignedVarInt.PRACTICAL_MAX).bytes)assertFailsWith(IllegalArgumentException::class) {UnsignedVarInt(UnsignedVarInt.PRACTICAL_MAX + 1.toBigInteger())}assertFailsWith(IllegalArgumentException::class) {UnsignedVarInt.digits(UnsignedVarInt.PRACTICAL_MAX + 1.toBigInteger())}assertFailsWith(IllegalArgumentException::class) {UnsignedVarInt.encode(UnsignedVarInt.PRACTICAL_MAX + 1.toBigInteger())}// Empty nor several nulls are validassertFailsWith(IllegalArgumentException::class) { UnsignedVarInt(byteArrayOf()) }for (i in 2..UnsignedVarInt.PRACTICAL_MAX_BYTES) {assertFailsWith(IllegalArgumentException::class) { UnsignedVarInt(ByteArray(i) { 0 }) }}// Last should be less than 128for (i in 2..UnsignedVarInt.PRACTICAL_MAX_BYTES) {assertFailsWith(IllegalArgumentException::class) {UnsignedVarInt(ByteArray(i) { 128.toUByte().toByte() })}}// PRACTICAL_MAX_BYTES is tipping pointUnsignedVarInt((UByteArray(UnsignedVarInt.PRACTICAL_MAX_BYTES - 1) { 255.toUByte() } +ubyteArrayOf(127.toUByte())).toByteArray())assertFailsWith(IllegalArgumentException::class) {UnsignedVarInt((UByteArray(UnsignedVarInt.PRACTICAL_MAX_BYTES) { 255.toUByte() } +ubyteArrayOf(127.toUByte())).toByteArray())}}}