Minor code improvements, add more test cases #KT-11208

Compare new readLine with the reference implementation in BufferedReader.
This commit is contained in:
Ilya Gorbunov
2018-03-28 18:57:53 +03:00
parent 12e427b4b5
commit d2a235b752
2 changed files with 33 additions and 19 deletions
+2 -3
View File
@@ -179,7 +179,6 @@ internal fun readLine(inputStream: InputStream, decoder: CharsetDecoder): String
val length = position()
val first = get(0)
val second = get(1)
flip()
when (length) {
2 -> {
if (!(first == '\r' && second == '\n')) stringBuilder.append(first)
@@ -199,7 +198,7 @@ private fun CharsetDecoder.tryDecode(byteBuffer: ByteBuffer, charBuffer: CharBuf
if (isError) throwException()
}
return (charBuffer.position() > positionBefore).also { isDecoded ->
byteBuffer.apply { if (isDecoded) clear() else flipBack() }
if (isDecoded) byteBuffer.clear() else byteBuffer.flipBack()
}
}
@@ -207,7 +206,7 @@ private fun CharBuffer.containsLineSeparator(): Boolean {
return get(1) == '\n' || get(0) == '\n'
}
private fun Buffer.flipBack(): Buffer = apply {
private fun Buffer.flipBack() {
position(limit())
limit(capacity())
}
+31 -16
View File
@@ -1,20 +1,11 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package io
@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
@file:kotlin.jvm.JvmVersion
package test.io
import org.junit.Test
import java.nio.charset.Charset
@@ -30,8 +21,19 @@ class ConsoleTest {
}
@Test
fun shouldReadOneLetter() {
testReadLine("a", listOf("a"))
fun shouldReadSingleLine() {
for (length in 1..3) {
val line = buildString { repeat(length) { append('a' + it) } }
testReadLine(line, listOf(line))
}
}
@Test
fun trailingEmptyLineIsIgnored() {
testReadLine(linuxLineSeparator, listOf(""))
testReadLine(windowsLineSeparator, listOf(""))
testReadLine("a$linuxLineSeparator", listOf("a"))
testReadLine("a$windowsLineSeparator", listOf("a"))
}
@Test
@@ -47,6 +49,9 @@ class ConsoleTest {
@Test
fun shouldReadConsecutiveEmptyLines() {
testReadLine("$linuxLineSeparator$linuxLineSeparator", listOf("", ""))
testReadLine("$linuxLineSeparator$windowsLineSeparator", listOf("", ""))
testReadLine("$windowsLineSeparator$linuxLineSeparator", listOf("", ""))
testReadLine("$windowsLineSeparator$windowsLineSeparator", listOf("", ""))
}
@Test
@@ -62,14 +67,24 @@ class ConsoleTest {
private fun testReadLine(text: String, expected: List<String>, charset: Charset = Charsets.UTF_8) {
val actual = readLines(text, charset)
assertEquals(expected, actual)
val referenceExpected = readLinesReference(text, charset)
assertEquals(referenceExpected, actual, "Comparing to reference readLine")
}
private fun readLines(text: String, charset: Charset): List<String> {
text.byteInputStream(charset).use { stream ->
val decoder = charset.newDecoder()
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
return generateSequence { readLine(stream, decoder) }.toList().also {
assertTrue("All bytes should be read") { stream.read() == -1 }
}
}
}
private fun readLinesReference(text: String, charset: Charset): List<String> {
text.byteInputStream(charset).bufferedReader(charset).use { reader ->
return generateSequence { reader.readLine() }.toList()
}
}
}