Avoid primitive boxing for 'boxed == primitive' if possible
This makes sense for non-floating-point primitive type (boolean, char, byte, short, int, long): floating-point types use specialized versions of 'areEqual'.
This commit is contained in:
+100
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.generators.tests
|
||||
|
||||
import com.intellij.openapi.util.io.FileUtil
|
||||
import java.io.File
|
||||
import java.io.PrintWriter
|
||||
|
||||
object GeneratePrimitiveEqualityWithNullableTestData {
|
||||
private val TEST_DATA_DIR = File("compiler/testData/codegen/box/primitiveTypes/equalityWithNullable")
|
||||
private val GENERATED_DIR = File(TEST_DATA_DIR, "generated")
|
||||
|
||||
private val PREAMBLE_MESSAGE = "Auto-generated by ${this::class.java.simpleName}. Do not edit!"
|
||||
|
||||
private fun generateBoxedVsPrimitiveTest(type: String, x: String, y: String) {
|
||||
PrintWriter(File(GENERATED_DIR, "boxedEqPrimitive$type.kt")).use {
|
||||
it.generateBoxedVsPrimitiveTestBody(type, x, y)
|
||||
}
|
||||
}
|
||||
|
||||
private fun PrintWriter.generateBoxedVsPrimitiveTestBody(type: String, x: String, y: String) {
|
||||
println("// $PREAMBLE_MESSAGE")
|
||||
println()
|
||||
println("val nx: $type? = $x")
|
||||
println("val nn: $type? = null")
|
||||
println("val x: $type = $x")
|
||||
println("val y: $type = $y")
|
||||
println()
|
||||
println("fun box(): String {")
|
||||
println(" val ax: $type? = $x")
|
||||
println(" val an: $type? = null")
|
||||
println(" val bx: $type = $x")
|
||||
println(" val by: $type = $y")
|
||||
println()
|
||||
|
||||
println(" return when {")
|
||||
|
||||
listOf(
|
||||
"nx != $x",
|
||||
"nx == $y",
|
||||
"!(nx == $x)",
|
||||
"!(nx != $y)",
|
||||
"nx != x",
|
||||
"nx == y",
|
||||
"!(nx == x)",
|
||||
"!(nx != y)",
|
||||
"nn == $x",
|
||||
"!(nn != $x)",
|
||||
"nn == x",
|
||||
"!(nn != x)",
|
||||
"ax != $x",
|
||||
"ax == $y",
|
||||
"!(ax == $x)",
|
||||
"!(ax != $y)",
|
||||
"ax != bx",
|
||||
"ax == by",
|
||||
"!(ax == bx)",
|
||||
"!(ax != by)",
|
||||
"an == $x",
|
||||
"!(an != $x)",
|
||||
"an == bx",
|
||||
"!(an != bx)"
|
||||
).forEachIndexed { i, condition ->
|
||||
println(" $condition -> \"Fail $i\"")
|
||||
}
|
||||
|
||||
println(" else -> \"OK\"")
|
||||
println(" }")
|
||||
println("}")
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun main(args: Array<String>) {
|
||||
if (!TEST_DATA_DIR.exists()) throw AssertionError("${TEST_DATA_DIR.path} doesn't exist")
|
||||
|
||||
FileUtil.delete(GENERATED_DIR)
|
||||
GENERATED_DIR.mkdirs()
|
||||
|
||||
generateBoxedVsPrimitiveTest("Boolean", "true", "false")
|
||||
generateBoxedVsPrimitiveTest("Char", "'0'", "'1'")
|
||||
generateBoxedVsPrimitiveTest("Byte", "0.toByte()", "1.toByte()")
|
||||
generateBoxedVsPrimitiveTest("Short", "0.toShort()", "1.toShort()")
|
||||
generateBoxedVsPrimitiveTest("Int", "0", "1")
|
||||
generateBoxedVsPrimitiveTest("Long", "0L", "1L")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user