Cleanup Serializable test

This commit is contained in:
Ilya Ryzhenkov
2014-09-12 15:28:25 +04:00
committed by Andrey Breslav
parent a6d2624807
commit 7b77229638
@@ -5,46 +5,70 @@ import java.io.ByteArrayOutputStream
import java.io.ByteArrayInputStream import java.io.ByteArrayInputStream
import java.io.ObjectInputStream import java.io.ObjectInputStream
import java.io.Serializable import java.io.Serializable
import java.util.HashMap
import junit.framework.TestCase import junit.framework.TestCase
import junit.framework.Assert import junit.framework.Assert
class Serial(val a : String) : java.lang.Object(), Serializable { private class Serial(val name: String) : Serializable {
override fun toString() = a override fun toString() = name
} }
class SerialTest() : TestCase() { private data class DataType(val name: String, val value: Int, val percent: Double) : Serializable
fun testMe() {
val tuple = Triple("lala", "bbb", Serial("serial"))
val op = { -> tuple.toString() }
val baos = ByteArrayOutputStream() class SerializableTest() : TestCase() {
val oos = ObjectOutputStream(baos) fun testClosure() {
oos.writeObject(op) val tuple = Triple("Ivan", 12, Serial("serial"))
oos.close() val fn = { tuple.toString() }
val bais = ByteArrayInputStream(baos.toByteArray()) val byteOutputStream = ByteArrayOutputStream()
val ins = ObjectInputStream(bais) val bytes = with(byteOutputStream) {
val ops = ins.readObject() as (() -> String) val objectStream = ObjectOutputStream(byteOutputStream)
objectStream.writeObject(fn)
objectStream.close()
toByteArray()
}
Assert.assertEquals(op (), ops()) val byteInputStream = ByteArrayInputStream(bytes)
val objectStream = ObjectInputStream(byteInputStream)
val deserialized = objectStream.readObject() as (() -> String)
Assert.assertEquals(fn(), deserialized())
} }
fun testComplex() { fun testComplexClosure() {
val y = 12 val y = 12
val op = { (x:Int) -> (x + y).toString() } val fn1 = {(x: Int) -> (x + y).toString() }
val fn2: Int.(Int) -> String = { fn1(this + it) }
val op2 : Int.(Int) -> String = { op(this + it) } val byteOutputStream = ByteArrayOutputStream()
val bytes = with(byteOutputStream) {
val objectStream = ObjectOutputStream(byteOutputStream)
objectStream.writeObject(fn2)
objectStream.close()
toByteArray()
}
val baos = ByteArrayOutputStream() val byteInputStream = ByteArrayInputStream(bytes)
val oos = ObjectOutputStream(baos) val objectStream = ObjectInputStream(byteInputStream)
oos.writeObject(op2) val deserialized = objectStream.readObject() as (Int.(Int) -> String)
oos.close()
val bais = ByteArrayInputStream(baos.toByteArray()) Assert.assertEquals(5.fn2(10), 5.deserialized(10))
val ins = ObjectInputStream(bais) }
val ops = ins.readObject() as (Int.(Int) -> String)
Assert.assertEquals("27", 5.ops(10)) fun testDataClass() {
val data = DataType("name", 176, 1.4)
val byteOutputStream = ByteArrayOutputStream()
val bytes = with(byteOutputStream) {
val objectStream = ObjectOutputStream(byteOutputStream)
objectStream.writeObject(data)
objectStream.close()
toByteArray()
}
val byteInputStream = ByteArrayInputStream(bytes)
val objectStream = ObjectInputStream(byteInputStream)
val deserialized = objectStream.readObject() as DataType
Assert.assertEquals(data, deserialized)
} }
} }