summaryrefslogtreecommitdiff
path: root/runtime/common/src/testWithNative/kotlinx/serialization/cbor/CborReaderTest.kt
blob: e6776bf7a7b653d5341131a36da6fdecf558157e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
 * Copyright 2017 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package kotlinx.serialization.cbor

import kotlinx.io.ByteArrayInputStream
import kotlinx.serialization.internal.HexConverter
import kotlinx.serialization.loads
import kotlinx.serialization.test.shouldBe
import kotlin.test.Test
import kotlin.test.assertEquals

class CborReaderTest {
    private fun withDecoder(input: String, block: Cbor.CborDecoder.() -> Unit) {
        val bytes = HexConverter.parseHexBinary(input.toUpperCase())
        Cbor.CborDecoder(ByteArrayInputStream(bytes)).block()
    }

    @Test
    fun testDecodeIntegers() {
        withDecoder("0C1903E8") {
            nextNumber() shouldBe 12L
            nextNumber() shouldBe 1000L
        }
        withDecoder("203903e7") {
            nextNumber() shouldBe -1L
            nextNumber() shouldBe -1000L
        }
    }

    @Test
    fun testDecodeStrings() {
        withDecoder("6568656C6C6F") {
            nextString() shouldBe "hello"
        }
        withDecoder("7828737472696E672074686174206973206C6F6E676572207468616E2032332063686172616374657273") {
            nextString() shouldBe "string that is longer than 23 characters"
        }
    }

    @Test
    fun testDecodeDoubles() {
        withDecoder("fb7e37e43c8800759c") {
            nextDouble() shouldBe 1e+300
        }
        withDecoder("fa47c35000") {
            nextFloat() shouldBe 100000.0f
        }
    }

    @Test
    fun testDecodeSimpleObject() {
        Cbor.loads(Simple.serializer(), "bf616163737472ff") shouldBe Simple("str")
    }

    @Test
    fun testDecodeComplicatedObject() {
        val test = SmallZoo(
            "Hello, world!",
            42,
            null,
            listOf("a", "b"),
            mapOf(1 to true, 2 to false),
            Simple("lol"),
            listOf(Simple("kek"))
        )

        Cbor.loads(SmallZoo.serializer(),
            "bf637374726d48656c6c6f2c20776f726c64216169182a686e756c6c61626c65f6646c6973749f61616162ff636d6170bf01f502f4ff65696e6e6572bf6161636c6f6cff6a696e6e6572734c6973749fbf6161636b656bffffff"
        ) shouldBe test
    }
}