1 package kotlinx.serialization.json 2 3 import kotlinx.serialization.Serializable 4 import kotlinx.serialization.test.* 5 import kotlin.test.Test 6 import kotlin.test.assertEquals 7 8 class JsonExponentTest : JsonTestBase() { 9 @Serializable 10 data class SomeData(val count: Long) 11 @Serializable 12 data class SomeDataDouble(val count: Double) 13 14 @Test <lambda>null15 fun testExponentDecodingPositive() = parametrizedTest { 16 val decoded = Json.decodeFromString<SomeData>("""{ "count": 23e11 }""", it) 17 assertEquals(2300000000000, decoded.count) 18 } 19 20 @Test <lambda>null21 fun testExponentDecodingNegative() = parametrizedTest { 22 val decoded = Json.decodeFromString<SomeData>("""{ "count": -10E1 }""", it) 23 assertEquals(-100, decoded.count) 24 } 25 26 @Test testExponentDecodingPositiveDoublenull27 fun testExponentDecodingPositiveDouble() = parametrizedTest { 28 val decoded = Json.decodeFromString<SomeDataDouble>("""{ "count": 1.5E1 }""", it) 29 assertEquals(15.0, decoded.count) 30 } 31 32 @Test testExponentDecodingNegativeDoublenull33 fun testExponentDecodingNegativeDouble() = parametrizedTest { 34 val decoded = Json.decodeFromString<SomeDataDouble>("""{ "count": -1e-1 }""", it) 35 assertEquals(-0.1, decoded.count) 36 } 37 38 @Test <lambda>null39 fun testExponentDecodingErrorTruncatedDecimal() = parametrizedTest { 40 assertFailsWithSerial("JsonDecodingException") 41 { Json.decodeFromString<SomeData>("""{ "count": -1E-1 }""", it) } 42 } 43 44 @Test <lambda>null45 fun testExponentDecodingErrorExponent() = parametrizedTest { 46 assertFailsWithSerial("JsonDecodingException") 47 { Json.decodeFromString<SomeData>("""{ "count": 1e-1e-1 }""", it) } 48 } 49 50 @Test <lambda>null51 fun testExponentDecodingErrorExponentDouble() = parametrizedTest { 52 assertFailsWithSerial("JsonDecodingException") 53 { Json.decodeFromString<SomeDataDouble>("""{ "count": 1e-1e-1 }""", it) } 54 } 55 56 @Test <lambda>null57 fun testExponentOverflowDouble() = parametrizedTest { 58 assertFailsWithSerial("JsonDecodingException") 59 { Json.decodeFromString<SomeDataDouble>("""{ "count": 10000e10000 }""", it) } 60 } 61 62 @Test <lambda>null63 fun testExponentUnderflowDouble() = parametrizedTest { 64 assertFailsWithSerial("JsonDecodingException") 65 { Json.decodeFromString<SomeDataDouble>("""{ "count": -100e2222 }""", it) } 66 } 67 68 @Test <lambda>null69 fun testExponentOverflow() = parametrizedTest { 70 assertFailsWithSerial("JsonDecodingException") 71 { Json.decodeFromString<SomeData>("""{ "count": 10000e10000 }""", it) } 72 } 73 74 @Test <lambda>null75 fun testExponentUnderflow() = parametrizedTest { 76 assertFailsWithSerial("JsonDecodingException") 77 { Json.decodeFromString<SomeData>("""{ "count": -10000e10000 }""", it) } 78 } 79 }