Skip to content

Commit afe3280

Browse files
committed
New tests for packed unsigned encoding (failing for now)
1 parent bf381a2 commit afe3280

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

formats/protobuf/commonTest/src/kotlinx/serialization/protobuf/PackedArraySerializerTest.kt

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5+
@file:OptIn(ExperimentalUnsignedTypes::class)
6+
57
package kotlinx.serialization.protobuf
68

79
import kotlinx.serialization.*
@@ -57,6 +59,31 @@ class PackedArraySerializerTest {
5759
val i: List<Int>
5860
)
5961

62+
@Serializable
63+
data class PackedUByteCarrier(
64+
@ProtoPacked
65+
val b: UByteArray
66+
) {
67+
override fun equals(other: Any?): Boolean {
68+
if (this === other) return true
69+
if (other == null || this::class != other::class) return false
70+
71+
other as PackedUByteCarrier
72+
73+
return b.contentEquals(other.b)
74+
}
75+
76+
override fun hashCode(): Int {
77+
return b.contentHashCode()
78+
}
79+
}
80+
81+
@Serializable
82+
data class PackedUintCarrier(
83+
@ProtoPacked
84+
val i: List<UInt>
85+
)
86+
6087
/**
6188
* Test that when packing is specified the array is encoded as packed
6289
*/
@@ -164,4 +191,33 @@ class PackedArraySerializerTest {
164191
assertEquals(obj, decodedAbsentField)
165192
}
166193

194+
@Test
195+
fun testDecodePackedUnsigned() {
196+
val expectedByteCarrier = PackedUByteCarrier(ubyteArrayOf(1.toUByte(), 2.toUByte(), 128.toUByte()))
197+
val expectedIntCarrier = PackedUintCarrier(listOf(1u, 2u, 3u, 128u))
198+
199+
val byteHex = """0a03010280"""
200+
val intHex = """0a050102038001"""
201+
202+
val decodedBytes = ProtoBuf.decodeFromHexString<PackedUByteCarrier>(byteHex)
203+
val decodedInts = ProtoBuf.decodeFromHexString<PackedUintCarrier>(intHex)
204+
205+
assertEquals(expectedByteCarrier, decodedBytes)
206+
assertEquals(expectedIntCarrier, decodedInts)
207+
}
208+
@Test
209+
fun testEncodePackedUnsigned() {
210+
val byteCarrier = PackedUByteCarrier(ubyteArrayOf(1.toUByte(), 2.toUByte(), 128.toUByte()))
211+
val intCarrier = PackedUintCarrier(listOf(1u, 2u, 3u, 128u))
212+
213+
val expectedByteHex = """0a03010280"""
214+
val expectedIntHex = """0a050102038001"""
215+
216+
val byteHex = ProtoBuf.encodeToHexString(byteCarrier)
217+
val intHex = ProtoBuf.encodeToHexString(intCarrier)
218+
219+
assertEquals(expectedByteHex, byteHex)
220+
assertEquals(expectedIntHex, intHex)
221+
}
222+
167223
}

0 commit comments

Comments
 (0)