Generate tests for unsigned progressions using

GenerateSteppedRangesCodegenTestData.
This commit is contained in:
Mark Punzalan
2020-04-04 23:34:18 -07:00
committed by Alexander Udalov
parent 03ef3724f4
commit 0e6af517d7
188 changed files with 9785 additions and 53 deletions
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -10,6 +10,7 @@ import java.io.PrintWriter
object GenerateSteppedRangesCodegenTestData {
private val TEST_DATA_DIR = File("compiler/testData/codegen/box/ranges/stepped")
private val UNSIGNED_TEST_DATA_DIR = File(TEST_DATA_DIR, "unsigned")
private val PREAMBLE_MESSAGE = "Auto-generated by ${GenerateSteppedRangesCodegenTestData::class.java.simpleName}. Do not edit!"
private val KT_34166_HEADER = """
@@ -17,22 +18,28 @@ object GenerateSteppedRangesCodegenTestData {
|// DONT_TARGET_EXACT_BACKEND: JS""".trimMargin()
private val KT_34166_AFFECTED_FILENAMES = setOf("illegalStepZero.kt", "illegalStepNegative.kt", "illegalStepNonConst.kt")
private enum class Type(val type: String) {
INT("Int") {
override fun convertIntValue(i: Int) = i.toString()
},
LONG("Long") {
override fun convertIntValue(i: Int) = i.toString() + 'L'
},
private enum class Type(val type: String, val isLong: Boolean = false, val isUnsigned: Boolean = false) {
INT("Int"),
LONG("Long", isLong = true),
CHAR("Char") {
override fun convertIntValue(i: Int) = "'${'a' - 1 + i}'"
override fun convertIntValue(i: Int, forStep: Boolean) = "'${'a' - 1 + i}'"
},
UINT("UInt", isUnsigned = true) {
// Step is unsigned for UIntProgression.
override fun convertIntValue(i: Int, forStep: Boolean) = if (forStep) INT.convertValue(i) else "${i}u"
},
ULONG("ULong", isUnsigned = true, isLong = true) {
// Step is unsigned for ULongProgression.
override fun convertIntValue(i: Int, forStep: Boolean) = if (forStep) LONG.convertValue(i) else "${i}uL"
};
protected abstract fun convertIntValue(i: Int): String
fun convertValue(i: Any) = when (i) {
is Int -> convertIntValue(i)
protected open fun convertIntValue(i: Int, forStep: Boolean): String =
i.toString() + (if (isUnsigned) "u" else "") + (if (isLong) "L" else "")
fun convertValue(i: Any, forStep: Boolean = false) = when (i) {
is Int -> convertIntValue(i, forStep)
// For convenience, replace the type in value expressions so we can re-use the same builder for different types
else -> i.toString().replace("Int.", "$type.")
else -> i.toString().replace("U?Int\\.".toRegex(), "$type.")
}
}
@@ -80,8 +87,8 @@ object GenerateSteppedRangesCodegenTestData {
when (op) {
is Step -> {
append(" step ").append(op.step)
if (type == Type.LONG) {
append("L")
if (type.isLong) {
append('L')
}
}
is StepExpression -> {
@@ -151,16 +158,10 @@ object GenerateSteppedRangesCodegenTestData {
fileName: String,
typeToBuilderMap: Map<Type, TestBuilder>,
function: Function,
extraCode: String? = null,
subdir: String? = null,
extraCode: String?,
fullSubdir: File,
asLiteral: Boolean
) {
val fullSubdirPath = StringBuilder((if (asLiteral) "literal" else "expression"))
fullSubdirPath.append("/").append(function.subdir)
if (subdir != null) {
fullSubdirPath.append("/").append(subdir)
}
val fullSubdir = File(TEST_DATA_DIR, fullSubdirPath.toString())
fullSubdir.mkdirs()
PrintWriter(File(fullSubdir, fileName)).use {
with(it) {
@@ -187,6 +188,43 @@ object GenerateSteppedRangesCodegenTestData {
}
}
private fun generateTestsForFunction(
fileName: String,
typeToBuilderMap: Map<Type, TestBuilder>,
function: Function,
extraCode: String? = null,
subdir: String? = null,
asLiteral: Boolean
) {
val fullSubdirPath = StringBuilder((if (asLiteral) "literal" else "expression"))
fullSubdirPath.append("/").append(function.subdir)
if (subdir != null) {
fullSubdirPath.append("/").append(subdir)
}
val (unsignedTests, signedTests) = typeToBuilderMap.asSequence().partition { (type, _) -> type.isUnsigned }
if (unsignedTests.isNotEmpty()) {
generateTestsForFunction(
fileName,
unsignedTests.associate { it.toPair() },
function,
extraCode,
File(UNSIGNED_TEST_DATA_DIR, fullSubdirPath.toString()),
asLiteral
)
}
if (signedTests.isNotEmpty()) {
generateTestsForFunction(
fileName,
signedTests.associate { it.toPair() },
function,
extraCode,
File(TEST_DATA_DIR, fullSubdirPath.toString()),
asLiteral
)
}
}
private fun PrintWriter.printTestForFunctionAndType(builder: TestBuilder, function: Function, type: Type, asLiteral: Boolean) {
val shouldFail = (builder.expectedValuesOrFailIfNull == null)
val listVarName = type.type.toLowerCase() + "List"
@@ -231,17 +269,6 @@ object GenerateSteppedRangesCodegenTestData {
generateTests("singleElementStepTwo.kt", TestBuilder(1 to 1).step(2).expectValues(1))
generateTests("stepOne.kt", TestBuilder(1 to 4).step(1).expectValues(1, 2, 3, 4))
generateTests("stepToSameLast.kt", TestBuilder(1 to 7).step(2).expectValues(1, 3, 5, 7))
generateTestsForRangeToAndUntil(
"stepNonConst.kt",
TestBuilder(1 to 8).step("two()").expectValues(1, 3, 5, 7),
extraCode = "fun two() = 2"
)
generateTestsForFunction(
"stepNonConst.kt",
TestBuilder(8 to 1).step("two()").expectValues(8, 6, 4, 2),
Function.DOWN_TO,
extraCode = "fun two() = 2"
)
generateTestsForRangeToAndUntil("stepToOutsideRange.kt", TestBuilder(1 to 7).step(7).expectValues(1))
generateTestsForFunction("stepToOutsideRange.kt", TestBuilder(7 to 1).step(7).expectValues(7), Function.DOWN_TO)
generateTestsForRangeToAndUntil("stepToSmallerLast.kt", TestBuilder(1 to 8).step(2).expectValues(1, 3, 5, 7))
@@ -390,7 +417,9 @@ object GenerateSteppedRangesCodegenTestData {
mapOf(
Type.INT to TestBuilder(1 to "nine()").step(2).expectValues(1, 3, 5, 7),
Type.LONG to TestBuilder(1 to "nine().toLong()").step(2).expectValues(1, 3, 5, 7),
Type.CHAR to TestBuilder("'a'" to "('a' - 1 + nine())").step(2).expectValues("'a'", "'c'", "'e'", "'g'")
Type.CHAR to TestBuilder("'a'" to "('a' - 1 + nine())").step(2).expectValues("'a'", "'c'", "'e'", "'g'"),
Type.UINT to TestBuilder(1 to "nine().toUInt()").step(2).expectValues(1, 3, 5, 7),
Type.ULONG to TestBuilder(1 to "nine().toULong()").step(2).expectValues(1, 3, 5, 7)
),
Function.UNTIL,
extraCode = "fun nine() = 9"
@@ -400,10 +429,12 @@ object GenerateSteppedRangesCodegenTestData {
"illegalStepNonConst.kt",
TestBuilder(1 to 7).step("zero()").shouldFail().let {
mapOf(
// Argument for (U)LongProgression.step() must be a Long
Type.INT to it,
// Argument for LongProgression.step() must be a Long
Type.LONG to TestBuilder(1 to 7).step("zero().toLong()").shouldFail(),
Type.CHAR to it
Type.CHAR to it,
Type.UINT to it,
Type.ULONG to TestBuilder(1 to 7).step("zero().toLong()").shouldFail()
)
},
function,
@@ -414,10 +445,12 @@ object GenerateSteppedRangesCodegenTestData {
"illegalStepNonConst.kt",
TestBuilder(7 to 1).step("zero()").shouldFail().let {
mapOf(
// Argument for (U)LongProgression.step() must be a Long
Type.INT to it,
// Argument for LongProgression.step() must be a Long
Type.LONG to TestBuilder(7 to 1).step("zero().toLong()").shouldFail(),
Type.CHAR to it
Type.CHAR to it,
Type.UINT to it,
Type.ULONG to TestBuilder(7 to 1).step("zero().toLong()").shouldFail(),
)
},
Function.DOWN_TO,
@@ -427,10 +460,12 @@ object GenerateSteppedRangesCodegenTestData {
"stepNonConst.kt",
TestBuilder(1 to 8).step("two()").expectValues(1, 3, 5, 7).let {
mapOf(
// Argument for (U)LongProgression.step() must be a Long
Type.INT to it,
// Argument for LongProgression.step() must be a Long
Type.LONG to TestBuilder(1 to 8).step("two().toLong()").expectValues(1, 3, 5, 7),
Type.CHAR to it
Type.CHAR to it,
Type.UINT to it,
Type.ULONG to TestBuilder(1 to 8).step("two().toLong()").expectValues(1, 3, 5, 7),
)
},
Function.RANGE_TO,
@@ -440,10 +475,12 @@ object GenerateSteppedRangesCodegenTestData {
"stepNonConst.kt",
TestBuilder(1 to 9).step("two()").expectValues(1, 3, 5, 7).let {
mapOf(
// Argument for (U)LongProgression.step() must be a Long
Type.INT to it,
// Argument for LongProgression.step() must be a Long
Type.LONG to TestBuilder(1 to 9).step("two().toLong()").expectValues(1, 3, 5, 7),
Type.CHAR to it
Type.CHAR to it,
Type.UINT to it,
Type.ULONG to TestBuilder(1 to 9).step("two().toLong()").expectValues(1, 3, 5, 7),
)
},
Function.UNTIL,
@@ -453,10 +490,12 @@ object GenerateSteppedRangesCodegenTestData {
"stepNonConst.kt",
TestBuilder(8 to 1).step("two()").expectValues(8, 6, 4, 2).let {
mapOf(
// Argument for (U)LongProgression.step() must be a Long
Type.INT to it,
// Argument for LongProgression.step() must be a Long
Type.LONG to TestBuilder(8 to 1).step("two().toLong()").expectValues(8, 6, 4, 2),
Type.CHAR to it
Type.CHAR to it,
Type.UINT to it,
Type.ULONG to TestBuilder(8 to 1).step("two().toLong()").expectValues(8, 6, 4, 2)
)
},
Function.DOWN_TO,
@@ -466,7 +505,8 @@ object GenerateSteppedRangesCodegenTestData {
"mixedTypeStep.kt",
mapOf(
Type.INT to TestBuilder("1.toShort()" to "7.toByte()").step(2).expectValues(1, 3, 5, 7),
Type.LONG to TestBuilder("1L" to "7").step("2").expectValues(1, 3, 5, 7)
Type.LONG to TestBuilder("1L" to "7").step("2").expectValues(1, 3, 5, 7),
Type.UINT to TestBuilder("1.toUByte()" to "7.toUByte()").step(2).expectValues(1, 3, 5, 7),
),
Function.RANGE_TO
)
@@ -474,7 +514,8 @@ object GenerateSteppedRangesCodegenTestData {
"mixedTypeStep.kt",
mapOf(
Type.INT to TestBuilder("1.toShort()" to "8.toByte()").step(2).expectValues(1, 3, 5, 7),
Type.LONG to TestBuilder("1L" to "8").step("2").expectValues(1, 3, 5, 7)
Type.LONG to TestBuilder("1L" to "8").step("2").expectValues(1, 3, 5, 7),
Type.UINT to TestBuilder("1.toUByte()" to "8.toUByte()").step(2).expectValues(1, 3, 5, 7),
),
Function.UNTIL
)
@@ -482,7 +523,8 @@ object GenerateSteppedRangesCodegenTestData {
"mixedTypeStep.kt",
mapOf(
Type.INT to TestBuilder("7.toByte()" to "1.toShort()").step(2).expectValues(7, 5, 3, 1),
Type.LONG to TestBuilder("7" to "1L").step("2").expectValues(7, 5, 3, 1)
Type.LONG to TestBuilder("7" to "1L").step("2").expectValues(7, 5, 3, 1),
Type.UINT to TestBuilder("7.toUByte()" to "1.toUByte()").step(2).expectValues(7, 5, 3, 1),
),
Function.DOWN_TO
)
@@ -501,7 +543,17 @@ object GenerateSteppedRangesCodegenTestData {
),
Type.CHAR to TestBuilder(
"Char.MIN_VALUE" to "Char.MAX_VALUE"
).step("Char.MAX_VALUE.toInt()").expectValues("Char.MIN_VALUE", "Char.MAX_VALUE")
).step("Char.MAX_VALUE.toInt()").expectValues("Char.MIN_VALUE", "Char.MAX_VALUE"),
Type.UINT to TestBuilder("UInt.MIN_VALUE" to "UInt.MAX_VALUE").step("Int.MAX_VALUE").expectValues(
"UInt.MIN_VALUE",
"2147483647u",
"UInt.MAX_VALUE - 1u"
),
Type.ULONG to TestBuilder("ULong.MIN_VALUE" to "ULong.MAX_VALUE").step("Long.MAX_VALUE").expectValues(
"ULong.MIN_VALUE",
"9223372036854775807uL",
"ULong.MAX_VALUE - 1uL"
)
),
Function.RANGE_TO
)
@@ -517,6 +569,16 @@ object GenerateSteppedRangesCodegenTestData {
"Long.MIN_VALUE",
-1,
"Long.MAX_VALUE - 1"
),
Type.UINT to TestBuilder("UInt.MIN_VALUE" to "UInt.MAX_VALUE").step("Int.MAX_VALUE").expectValues(
"UInt.MIN_VALUE",
"2147483647u",
"UInt.MAX_VALUE - 1u"
),
Type.ULONG to TestBuilder("ULong.MIN_VALUE" to "ULong.MAX_VALUE").step("Long.MAX_VALUE").expectValues(
"ULong.MIN_VALUE",
"9223372036854775807uL",
"ULong.MAX_VALUE - 1uL"
)
),
Function.UNTIL
@@ -526,7 +588,15 @@ object GenerateSteppedRangesCodegenTestData {
mapOf(
Type.INT to TestBuilder(1 to "Int.MAX_VALUE").step("Int.MAX_VALUE").expectValues(1),
Type.LONG to TestBuilder(1 to "Long.MAX_VALUE").step("Long.MAX_VALUE").expectValues(1),
Type.CHAR to TestBuilder("1.toChar()" to "Char.MAX_VALUE").step("Char.MAX_VALUE.toInt()").expectValues("1.toChar()")
Type.CHAR to TestBuilder("1.toChar()" to "Char.MAX_VALUE").step("Char.MAX_VALUE.toInt()").expectValues("1.toChar()"),
Type.UINT to TestBuilder(1 to "UInt.MAX_VALUE").step("Int.MAX_VALUE").expectValues(
1, "2147483648u",
"UInt.MAX_VALUE"
),
Type.ULONG to TestBuilder(1 to "ULong.MAX_VALUE").step("Long.MAX_VALUE").expectValues(
1, "9223372036854775808uL",
"ULong.MAX_VALUE"
),
),
Function.RANGE_TO
)
@@ -538,7 +608,10 @@ object GenerateSteppedRangesCodegenTestData {
Type.CHAR to TestBuilder("0.toChar()" to "Char.MAX_VALUE").step("Char.MAX_VALUE.toInt()").expectValues(
"0.toChar()",
"Char.MAX_VALUE"
)
),
Type.UINT to TestBuilder(0 to "UInt.MAX_VALUE").step("Int.MAX_VALUE").expectValues(0, "2147483647u", "UInt.MAX_VALUE - 1u"),
Type.ULONG to TestBuilder(0 to "ULong.MAX_VALUE").step("Long.MAX_VALUE")
.expectValues(0, "9223372036854775807uL", "ULong.MAX_VALUE - 1uL")
),
Function.RANGE_TO
)
@@ -547,7 +620,10 @@ object GenerateSteppedRangesCodegenTestData {
mapOf(
Type.INT to TestBuilder(0 to "Int.MAX_VALUE").step("Int.MAX_VALUE").expectValues(0),
Type.LONG to TestBuilder(0 to "Long.MAX_VALUE").step("Long.MAX_VALUE").expectValues(0),
Type.CHAR to TestBuilder("0.toChar()" to "Char.MAX_VALUE").step("Char.MAX_VALUE.toInt()").expectValues("0.toChar()")
Type.CHAR to TestBuilder("0.toChar()" to "Char.MAX_VALUE").step("Char.MAX_VALUE.toInt()").expectValues("0.toChar()"),
Type.UINT to TestBuilder(0 to "UInt.MAX_VALUE").step("Int.MAX_VALUE").expectValues(0, "2147483647u", "UInt.MAX_VALUE - 1u"),
Type.ULONG to TestBuilder(0 to "ULong.MAX_VALUE").step("Long.MAX_VALUE")
.expectValues(0, "9223372036854775807uL", "ULong.MAX_VALUE - 1uL")
),
Function.UNTIL
)
@@ -567,6 +643,16 @@ object GenerateSteppedRangesCodegenTestData {
Type.CHAR to TestBuilder("Char.MAX_VALUE" to "Char.MIN_VALUE").step("Char.MAX_VALUE.toInt()").expectValues(
"Char.MAX_VALUE",
"Char.MIN_VALUE"
),
Type.UINT to TestBuilder("UInt.MAX_VALUE" to "UInt.MIN_VALUE").step("Int.MAX_VALUE").expectValues(
"UInt.MAX_VALUE",
"2147483648u",
1
),
Type.ULONG to TestBuilder("ULong.MAX_VALUE" to "ULong.MIN_VALUE").step("Long.MAX_VALUE").expectValues(
"ULong.MAX_VALUE",
"9223372036854775808uL",
1
)
),
Function.DOWN_TO
@@ -576,7 +662,10 @@ object GenerateSteppedRangesCodegenTestData {
mapOf(
Type.INT to TestBuilder("Int.MAX_VALUE" to 1).step("Int.MAX_VALUE").expectValues("Int.MAX_VALUE"),
Type.LONG to TestBuilder("Long.MAX_VALUE" to 1).step("Long.MAX_VALUE").expectValues("Long.MAX_VALUE"),
Type.CHAR to TestBuilder("Char.MAX_VALUE" to "1.toChar()").step("Char.MAX_VALUE.toInt()").expectValues("Char.MAX_VALUE")
Type.CHAR to TestBuilder("Char.MAX_VALUE" to "1.toChar()").step("Char.MAX_VALUE.toInt()").expectValues("Char.MAX_VALUE"),
Type.UINT to TestBuilder("UInt.MAX_VALUE" to 1).step("Int.MAX_VALUE").expectValues("UInt.MAX_VALUE", "2147483648u", 1),
Type.ULONG to TestBuilder("ULong.MAX_VALUE" to 1).step("Long.MAX_VALUE")
.expectValues("ULong.MAX_VALUE", "9223372036854775808uL", 1),
),
Function.DOWN_TO
)
@@ -588,7 +677,11 @@ object GenerateSteppedRangesCodegenTestData {
Type.CHAR to TestBuilder("Char.MAX_VALUE" to "0.toChar()").step("Char.MAX_VALUE.toInt()").expectValues(
"Char.MAX_VALUE",
"0.toChar()"
)
),
Type.UINT to TestBuilder("UInt.MAX_VALUE" to 0).step("Int.MAX_VALUE")
.expectValues("UInt.MAX_VALUE", "2147483648u", 1),
Type.ULONG to TestBuilder("ULong.MAX_VALUE" to 0).step("Long.MAX_VALUE")
.expectValues("ULong.MAX_VALUE", "9223372036854775808uL", 1)
),
Function.DOWN_TO
)