fixe #2 effective Instant binary representation (truncates to millis though)
This commit is contained in:
1 parent
d0f29ce06f
commit
ee20bfdee7
7 files changed
+111
-4
No files matched your search
@@ -0,0 +1,28 @@
|
||||
package bintools
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
import net.sergeych.bintools.JsonPacker
|
||||
import net.sergeych.bintools.pack
|
||||
import net.sergeych.bintools.unpack
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNull
|
||||
|
||||
class JsonPackerTest {
|
||||
@Serializable
|
||||
data class FB1(val foo: Int,val bar: String)
|
||||
@Test
|
||||
fun testPackUnpack() {
|
||||
val mp = JsonPacker()
|
||||
println(mp.pack(mapOf("foo" to 42)).decodeToString())
|
||||
assertEquals("""{"foo":42}""", mp.pack(mapOf("foo" to 42)).decodeToString())
|
||||
val x = mp.unpack<FB1>("""{"foo":42, "bar": "foo"}""".encodeToByteArray())
|
||||
println(x)
|
||||
assertEquals(42, x.foo)
|
||||
assertEquals("foo", x.bar)
|
||||
|
||||
var nx: FB1? = null
|
||||
println(mp.pack(nx).decodeToString())
|
||||
assertNull(mp.unpack<FB1?>("null".encodeToByteArray()))
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package bipack
|
||||
|
||||
import kotlinx.datetime.Clock
|
||||
import kotlinx.datetime.Instant
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import net.sergeych.bintools.encodeToHex
|
||||
@@ -341,4 +343,12 @@ class BipackEncoderTest {
|
||||
assertEquals("00 00 00 01 00 00 00 02", BipackEncoder.encode(Foo(0x100000002)).encodeToHex())
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInstant() {
|
||||
val x = Clock.System.now()
|
||||
// println( BipackEncoder.encode(x).toDump() )
|
||||
val y = BipackDecoder.decode<Instant>(BipackEncoder.encode(x))
|
||||
assertEquals(x.toEpochMilliseconds(), y.toEpochMilliseconds())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user