-
Notifications
You must be signed in to change notification settings - Fork 612
/
UnsignedIntegersTest.kt
140 lines (126 loc) · 4.42 KB
/
UnsignedIntegersTest.kt
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Copyright 2017-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.serialization.features.inline
import kotlinx.serialization.*
import kotlinx.serialization.builtins.*
import kotlinx.serialization.json.*
import kotlin.test.*
class UnsignedIntegersTest : JsonTestBase() {
@Serializable
data class AllUnsigned(
val uInt: UInt,
val uLong: ULong,
val uByte: UByte,
val uShort: UShort,
val signedInt: Int,
val signedLong: Long,
val double: Double
)
// TODO uncomment when Kotlin 1.8.0 is released
// @Serializable
// data class UnsignedArrays(
// val uByte: UByteArray,
// val uShort: UShortArray,
// val uInt: UIntArray,
// val uLong: ULongArray
// ) {
// override fun equals(other: Any?): Boolean {
// if (this === other) return true
// if (other == null || this::class != other::class) return false
//
// other as UnsignedArrays
//
// if (!uByte.contentEquals(other.uByte)) return false
// if (!uShort.contentEquals(other.uShort)) return false
// if (!uInt.contentEquals(other.uInt)) return false
// if (!uLong.contentEquals(other.uLong)) return false
//
// return true
// }
//
// override fun hashCode(): Int {
// var result = uByte.contentHashCode()
// result = 31 * result + uShort.contentHashCode()
// result = 31 * result + uInt.contentHashCode()
// result = 31 * result + uLong.contentHashCode()
// return result
// }
// }
@Serializable
data class UnsignedWithoutLong(val uInt: UInt, val uByte: UByte, val uShort: UShort)
@Test
fun testUnsignedIntegersJson() {
val data = AllUnsigned(
Int.MAX_VALUE.toUInt() + 10.toUInt(),
Long.MAX_VALUE.toULong() + 10.toULong(),
239.toUByte(),
65000.toUShort(),
-42,
Long.MIN_VALUE,
1.1
)
assertJsonFormAndRestored(
AllUnsigned.serializer(),
data,
"""{"uInt":2147483657,"uLong":9223372036854775817,"uByte":239,"uShort":65000,"signedInt":-42,"signedLong":-9223372036854775808,"double":1.1}""",
)
}
@Test
fun testUnsignedIntegersWithoutLongJson() {
val data = UnsignedWithoutLong(
Int.MAX_VALUE.toUInt() + 10.toUInt(),
239.toUByte(),
65000.toUShort(),
)
assertJsonFormAndRestored(
UnsignedWithoutLong.serializer(),
data,
"""{"uInt":2147483657,"uByte":239,"uShort":65000}""",
)
}
@Test
fun testRoot() {
assertJsonFormAndRestored(UByte.serializer(), 220U, "220")
assertJsonFormAndRestored(UShort.serializer(), 65000U, "65000")
assertJsonFormAndRestored(UInt.serializer(), 2147483657U, "2147483657")
assertJsonFormAndRestored(ULong.serializer(), 9223372036854775817U, "9223372036854775817")
}
@OptIn(ExperimentalUnsignedTypes::class)
@Test
fun testRootArrays() = parametrizedTest {
assertJsonFormAndRestoredCustom(
UByteArraySerializer(),
ubyteArrayOf(1U, 220U),
"[1,220]"
) { l, r -> l.contentEquals(r) }
assertJsonFormAndRestoredCustom(
UShortArraySerializer(),
ushortArrayOf(1U, 65000U),
"[1,65000]"
) { l, r -> l.contentEquals(r) }
assertJsonFormAndRestoredCustom(
UIntArraySerializer(),
uintArrayOf(1U, 2147483657U),
"[1,2147483657]"
) { l, r -> l.contentEquals(r) }
assertJsonFormAndRestoredCustom(
ULongArraySerializer(),
ulongArrayOf(1U, 9223372036854775817U),
"[1,9223372036854775817]"
) { l, r -> l.contentEquals(r) }
}
// TODO uncomment when Kotlin 1.8.0 is released
// @OptIn(ExperimentalUnsignedTypes::class)
// fun testArrays() {
// val data = UnsignedArrays(
// ubyteArrayOf(1U, 220U),
// ushortArrayOf(1U, 65000U),
// uintArrayOf(1U, 2147483657U),
// ulongArrayOf(1U, 9223372036854775817U)
// )
// val json = """{"uByte":[1,220],uShort:[1,65000],uInt:[1,2147483657],uLong:[1,9223372036854775817]}"""
//
// assertJsonFormAndRestored(UnsignedArrays.serializer(), data, json)
// }
}