summaryrefslogtreecommitdiff
path: root/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonExponentTest.kt
blob: 0f31ac507421cc24bd7a280e655d2e6869bc6cb5 (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
package kotlinx.serialization.json

import kotlinx.serialization.Serializable
import kotlinx.serialization.test.*
import kotlin.test.Test
import kotlin.test.assertEquals

class JsonExponentTest : JsonTestBase() {
    @Serializable
    data class SomeData(val count: Long)
    @Serializable
    data class SomeDataDouble(val count: Double)

    @Test
    fun testExponentDecodingPositive() = parametrizedTest {
        val decoded =  Json.decodeFromString<SomeData>("""{ "count": 23e11 }""", it)
        assertEquals(2300000000000, decoded.count)
    }

    @Test
    fun testExponentDecodingNegative() = parametrizedTest {
        val decoded = Json.decodeFromString<SomeData>("""{ "count": -10E1 }""", it)
        assertEquals(-100, decoded.count)
    }

    @Test
    fun testExponentDecodingPositiveDouble() = parametrizedTest {
        val decoded = Json.decodeFromString<SomeDataDouble>("""{ "count": 1.5E1 }""", it)
        assertEquals(15.0, decoded.count)
    }

    @Test
    fun testExponentDecodingNegativeDouble() = parametrizedTest {
        val decoded = Json.decodeFromString<SomeDataDouble>("""{ "count": -1e-1 }""", it)
        assertEquals(-0.1, decoded.count)
    }

    @Test
    fun testExponentDecodingErrorTruncatedDecimal() = parametrizedTest {
        assertFailsWithSerial("JsonDecodingException")
        { Json.decodeFromString<SomeData>("""{ "count": -1E-1 }""", it) }
    }

    @Test
    fun testExponentDecodingErrorExponent() = parametrizedTest {
        assertFailsWithSerial("JsonDecodingException")
        { Json.decodeFromString<SomeData>("""{ "count": 1e-1e-1 }""", it) }
    }

    @Test
    fun testExponentDecodingErrorExponentDouble() = parametrizedTest {
        assertFailsWithSerial("JsonDecodingException")
        { Json.decodeFromString<SomeDataDouble>("""{ "count": 1e-1e-1 }""", it) }
    }

    @Test
    fun testExponentOverflowDouble() = parametrizedTest {
        assertFailsWithSerial("JsonDecodingException")
        { Json.decodeFromString<SomeDataDouble>("""{ "count": 10000e10000 }""", it) }
    }

    @Test
    fun testExponentUnderflowDouble() = parametrizedTest {
        assertFailsWithSerial("JsonDecodingException")
        { Json.decodeFromString<SomeDataDouble>("""{ "count": -100e2222 }""", it) }
    }

    @Test
    fun testExponentOverflow() = parametrizedTest {
        assertFailsWithSerial("JsonDecodingException")
        { Json.decodeFromString<SomeData>("""{ "count": 10000e10000 }""", it) }
    }

    @Test
    fun testExponentUnderflow() = parametrizedTest {
        assertFailsWithSerial("JsonDecodingException")
        { Json.decodeFromString<SomeData>("""{ "count": -10000e10000 }""", it) }
    }
}