blob: 212169e6212a5e06ec813da89a51cb726ddd0704 (
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
|
/*
* Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.serialization
import kotlinx.serialization.modules.*
import kotlinx.serialization.test.*
import kotlin.test.*
import kotlin.time.*
class CachedSerializersTest {
@Serializable
object Object
@Serializable
sealed class SealedParent {
@Serializable
data class Child(val i: Int) : SealedParent()
}
@Serializable
abstract class Abstract
@Serializable
enum class SerializableEnum {A, B}
@SerialInfo
annotation class MyAnnotation(val x: Int)
@Serializable
enum class SerializableMarkedEnum {
@SerialName("first")
@MyAnnotation(1)
C,
@MyAnnotation(2)
D
}
@Test
fun testObjectSerializersAreSame() {
assertSame(Object.serializer(), Object.serializer())
}
@Test
fun testSerializableEnumSerializersAreSame() {
assertSame(SerializableEnum.serializer(), SerializableEnum.serializer())
}
@Test
fun testSerializableMarkedEnumSerializersAreSame() {
assertSame(SerializableMarkedEnum.serializer(), SerializableMarkedEnum.serializer())
}
@Test
fun testSealedSerializersAreSame() {
assertSame(SealedParent.serializer(), SealedParent.serializer())
}
@Test
fun testAbstractSerializersAreSame() {
assertSame(Abstract.serializer(), Abstract.serializer())
}
@OptIn(ExperimentalTime::class)
@Test
fun testSerializersAreIntrinsified() = jvmOnly {
val m = SerializersModule { }
val direct = measureTime {
Object.serializer()
}
val directMs = direct.inWholeMicroseconds
val indirect = measureTime {
m.serializer<Object>()
}
val indirectMs = indirect.inWholeMicroseconds
if (indirectMs > directMs + (directMs / 4)) error("Direct ($directMs) and indirect ($indirectMs) times are too far apart")
}
}
|