commonTest.kotlinx.serialization.protobuf.ByteArraySerializerTest.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlinx-serialization-protobuf-metadata Show documentation
Show all versions of kotlinx-serialization-protobuf-metadata Show documentation
Kotlin multiplatform serialization runtime library
The newest version!
/*
* Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.serialization.protobuf
import kotlinx.serialization.*
import kotlin.random.*
import kotlin.test.*
class ByteArraySerializerTest {
@Serializable
class ByteArrayCarrier(@ProtoNumber(2) val data: ByteArray) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || this::class != other::class) return false
other as ByteArrayCarrier
if (!data.contentEquals(other.data)) return false
return true
}
override fun hashCode(): Int {
return data.contentHashCode()
}
override fun toString(): String {
return "ByteArrayCarrier(data=${data.contentToString()})"
}
}
@Test
fun testByteArrayProtobuf() {
val obj = ByteArrayCarrier(byteArrayOf(42, 100))
val s = ProtoBuf.encodeToHexString(ByteArrayCarrier.serializer(), obj)
assertEquals("""12022a64""", s)
val obj2 = ProtoBuf.decodeFromHexString(ByteArrayCarrier.serializer(), s)
assertEquals(obj, obj2)
}
@Test
fun testWrappedByteArrayProtobuf() {
val arraySize = 301
val arr = Random.nextBytes(ByteArray(arraySize))
val obj = ByteArrayCarrier(arr)
val bytes = ProtoBuf.encodeToByteArray(ByteArrayCarrier.serializer(), obj)
assertEquals(obj, ProtoBuf.decodeFromByteArray(ByteArrayCarrier.serializer(), bytes))
}
}