Optimize the implementation of emptyList, emptySet and emptyMap and make them serializable.

Specialized implementations of singleton List, Set and Map are used in JVM.

#KT-6682 Fixed
#KT-7104 Fixed
#KT-4840 Fixed
This commit is contained in:
Ilya Gorbunov
2015-05-14 21:25:43 +03:00
parent b7277cd80c
commit edc471c8ec
6 changed files with 143 additions and 61 deletions
@@ -1,5 +1,9 @@
package test.collections
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
import kotlin.test.*
import java.util.*
@@ -142,4 +146,25 @@ class CollectionJVMTest {
val charValues: List<Char> = src.filterIsInstance<Char>()
assertEquals(0, charValues.size())
}
test fun emptyListIsSerializable() = testSingletonSerialization(emptyList<Any>())
test fun emptySetIsSerializable() = testSingletonSerialization(emptySet<Any>())
private fun testSingletonSerialization(value: Any) {
val outputStream = ByteArrayOutputStream()
val objectOuputStream = ObjectOutputStream(outputStream)
objectOuputStream.writeObject(value)
objectOuputStream.close()
outputStream.close()
val inputStream = ByteArrayInputStream(outputStream.toByteArray())
val inputObjectStream = ObjectInputStream(inputStream)
val result = inputObjectStream.readObject()
assertEquals(value, result)
assertTrue(value === result)
}
}