diff --git a/compiler/testData/codegen/boxWithStdlib/ranges/expression/reversedInexactSteppedDownTo.kt b/compiler/testData/codegen/boxWithStdlib/ranges/expression/reversedInexactSteppedDownTo.kt index 88c80796961..f15a9d19f9a 100644 --- a/compiler/testData/codegen/boxWithStdlib/ranges/expression/reversedInexactSteppedDownTo.kt +++ b/compiler/testData/codegen/boxWithStdlib/ranges/expression/reversedInexactSteppedDownTo.kt @@ -8,7 +8,7 @@ fun box(): String { list1.add(i) if (list1.size() > 23) break } - if (list1 != listOf(3, 5, 7)) { + if (list1 != listOf(4, 6, 8)) { return "Wrong elements for (8 downTo 3 step 2).reversed(): $list1" } @@ -18,7 +18,7 @@ fun box(): String { list2.add(i) if (list2.size() > 23) break } - if (list2 != listOf(3, 5, 7)) { + if (list2 != listOf(4, 6, 8)) { return "Wrong elements for (8.toByte() downTo 3.toByte() step 2).reversed(): $list2" } @@ -28,7 +28,7 @@ fun box(): String { list3.add(i) if (list3.size() > 23) break } - if (list3 != listOf(3, 5, 7)) { + if (list3 != listOf(4, 6, 8)) { return "Wrong elements for (8.toShort() downTo 3.toShort() step 2).reversed(): $list3" } @@ -38,7 +38,7 @@ fun box(): String { list4.add(i) if (list4.size() > 23) break } - if (list4 != listOf(3, 5, 7)) { + if (list4 != listOf(4, 6, 8)) { return "Wrong elements for (8.toLong() downTo 3.toLong() step 2.toLong()).reversed(): $list4" } @@ -48,29 +48,9 @@ fun box(): String { list5.add(i) if (list5.size() > 23) break } - if (list5 != listOf('a', 'c')) { + if (list5 != listOf('b', 'd')) { return "Wrong elements for ('d' downTo 'a' step 2).reversed(): $list5" } - val list6 = ArrayList() - val range6 = (5.8 downTo 4.0 step 0.5).reversed() - for (i in range6) { - list6.add(i) - if (list6.size() > 23) break - } - if (list6 != listOf(4.0, 4.5, 5.0, 5.5)) { - return "Wrong elements for (5.8 downTo 4.0 step 0.5).reversed(): $list6" - } - - val list7 = ArrayList() - val range7 = (5.8.toFloat() downTo 4.0.toFloat() step 0.5.toFloat()).reversed() - for (i in range7) { - list7.add(i) - if (list7.size() > 23) break - } - if (list7 != listOf(4.0.toFloat(), 4.5.toFloat(), 5.0.toFloat(), 5.5.toFloat())) { - return "Wrong elements for (5.8.toFloat() downTo 4.0.toFloat() step 0.5.toFloat()).reversed(): $list7" - } - return "OK" } diff --git a/compiler/testData/codegen/boxWithStdlib/ranges/literal/reversedInexactSteppedDownTo.kt b/compiler/testData/codegen/boxWithStdlib/ranges/literal/reversedInexactSteppedDownTo.kt index 7ceb7c77eac..4c92150c280 100644 --- a/compiler/testData/codegen/boxWithStdlib/ranges/literal/reversedInexactSteppedDownTo.kt +++ b/compiler/testData/codegen/boxWithStdlib/ranges/literal/reversedInexactSteppedDownTo.kt @@ -7,7 +7,7 @@ fun box(): String { list1.add(i) if (list1.size() > 23) break } - if (list1 != listOf(3, 5, 7)) { + if (list1 != listOf(4, 6, 8)) { return "Wrong elements for (8 downTo 3 step 2).reversed(): $list1" } @@ -16,7 +16,7 @@ fun box(): String { list2.add(i) if (list2.size() > 23) break } - if (list2 != listOf(3, 5, 7)) { + if (list2 != listOf(4, 6, 8)) { return "Wrong elements for (8.toByte() downTo 3.toByte() step 2).reversed(): $list2" } @@ -25,7 +25,7 @@ fun box(): String { list3.add(i) if (list3.size() > 23) break } - if (list3 != listOf(3, 5, 7)) { + if (list3 != listOf(4, 6, 8)) { return "Wrong elements for (8.toShort() downTo 3.toShort() step 2).reversed(): $list3" } @@ -34,7 +34,7 @@ fun box(): String { list4.add(i) if (list4.size() > 23) break } - if (list4 != listOf(3, 5, 7)) { + if (list4 != listOf(4, 6, 8)) { return "Wrong elements for (8.toLong() downTo 3.toLong() step 2.toLong()).reversed(): $list4" } @@ -43,27 +43,9 @@ fun box(): String { list5.add(i) if (list5.size() > 23) break } - if (list5 != listOf('a', 'c')) { + if (list5 != listOf('b', 'd')) { return "Wrong elements for ('d' downTo 'a' step 2).reversed(): $list5" } - val list6 = ArrayList() - for (i in (5.8 downTo 4.0 step 0.5).reversed()) { - list6.add(i) - if (list6.size() > 23) break - } - if (list6 != listOf(4.0, 4.5, 5.0, 5.5)) { - return "Wrong elements for (5.8 downTo 4.0 step 0.5).reversed(): $list6" - } - - val list7 = ArrayList() - for (i in (5.8.toFloat() downTo 4.0.toFloat() step 0.5.toFloat()).reversed()) { - list7.add(i) - if (list7.size() > 23) break - } - if (list7 != listOf(4.0.toFloat(), 4.5.toFloat(), 5.0.toFloat(), 5.5.toFloat())) { - return "Wrong elements for (5.8.toFloat() downTo 4.0.toFloat() step 0.5.toFloat()).reversed(): $list7" - } - return "OK" } diff --git a/libraries/stdlib/test/language/RangeIterationJVMTest.kt b/libraries/stdlib/test/language/RangeIterationJVMTest.kt index b2640a5e1e6..2e023d0f68a 100644 --- a/libraries/stdlib/test/language/RangeIterationJVMTest.kt +++ b/libraries/stdlib/test/language/RangeIterationJVMTest.kt @@ -15,51 +15,7 @@ import org.junit.Test as test import kotlin.test.* // Test data for codegen is generated from this class. If you change it, rerun GenerateTests -public class RangeIterationJVMTest { - private fun doTest( - sequence: Iterable, - expectedFirst: N, - expectedLast: N, - expectedIncrement: Number, - expectedElements: List - ) { - val first: Any - val last: Any - val increment: Number - when (sequence) { - is IntProgression -> { - first = sequence.first - last = sequence.last - increment = sequence.increment - } - is LongProgression -> { - first = sequence.first - last = sequence.last - increment = sequence.increment - } - is CharProgression -> { - first = sequence.first - last = sequence.last - increment = sequence.increment - } - // TODO: Drop this branch - is Progression -> { - first = sequence.start - last = sequence.end - increment = sequence.increment - } - else -> throw IllegalArgumentException("Unsupported sequence type: $sequence") - } - - assertEquals(expectedFirst, first) - assertEquals(expectedLast, last) - assertEquals(expectedIncrement, increment) - - if (expectedElements.isEmpty()) - assertTrue(sequence.none()) - else - assertEquals(expectedElements, sequence.toList()) - } +public class RangeIterationJVMTest : RangeIterationTestBase() { @test fun infiniteSteps() { doTest(0.0..5.0 step java.lang.Double.POSITIVE_INFINITY, 0.0, 5.0, java.lang.Double.POSITIVE_INFINITY, listOf(0.0)) @@ -151,12 +107,12 @@ public class RangeIterationJVMTest { } @test fun inexactToMaxValue() { - doTest((MaxI - 5)..MaxI step 3, MaxI - 5, MaxI, 3, listOf(MaxI - 5, MaxI - 2)) - doTest((MaxB - 5).toByte()..MaxB step 3, (MaxB - 5).toInt(), MaxB.toInt(), 3, listOf((MaxB - 5).toInt(), (MaxB - 2).toInt())) - doTest((MaxS - 5).toShort()..MaxS step 3, (MaxS - 5).toInt(), MaxS.toInt(), 3, listOf((MaxS - 5).toInt(), (MaxS - 2).toInt())) - doTest((MaxL - 5).toLong()..MaxL step 3, (MaxL - 5).toLong(), MaxL, 3.toLong(), listOf((MaxL - 5).toLong(), (MaxL - 2).toLong())) + doTest((MaxI - 5)..MaxI step 3, MaxI - 5, MaxI - 2, 3, listOf(MaxI - 5, MaxI - 2)) + doTest((MaxB - 5).toByte()..MaxB step 3, (MaxB - 5).toInt(), (MaxB - 2).toInt(), 3, listOf((MaxB - 5).toInt(), (MaxB - 2).toInt())) + doTest((MaxS - 5).toShort()..MaxS step 3, (MaxS - 5).toInt(), (MaxS - 2).toInt(), 3, listOf((MaxS - 5).toInt(), (MaxS - 2).toInt())) + doTest((MaxL - 5).toLong()..MaxL step 3, (MaxL - 5).toLong(), (MaxL - 2).toLong(), 3.toLong(), listOf((MaxL - 5).toLong(), (MaxL - 2).toLong())) - doTest((MaxC - 5)..MaxC step 3, (MaxC - 5), MaxC, 3, listOf((MaxC - 5), (MaxC - 2))) + doTest((MaxC - 5)..MaxC step 3, (MaxC - 5), (MaxC - 2), 3, listOf((MaxC - 5), (MaxC - 2))) } @test fun progressionDownToMinValue() { @@ -169,11 +125,11 @@ public class RangeIterationJVMTest { } @test fun inexactDownToMinValue() { - doTest((MinI + 5) downTo MinI step 3, MinI + 5, MinI, -3, listOf(MinI + 5, MinI + 2)) - doTest((MinB + 5).toByte() downTo MinB step 3, (MinB + 5).toInt(), MinB.toInt(), -3, listOf((MinB + 5).toInt(), (MinB + 2).toInt())) - doTest((MinS + 5).toShort() downTo MinS step 3, (MinS + 5).toInt(), MinS.toInt(), -3, listOf((MinS + 5).toInt(), (MinS + 2).toInt())) - doTest((MinL + 5).toLong() downTo MinL step 3, (MinL + 5).toLong(), MinL, -3.toLong(), listOf((MinL + 5).toLong(), (MinL + 2).toLong())) + doTest((MinI + 5) downTo MinI step 3, MinI + 5, MinI + 2, -3, listOf(MinI + 5, MinI + 2)) + doTest((MinB + 5).toByte() downTo MinB step 3, (MinB + 5).toInt(), (MinB + 2).toInt(), -3, listOf((MinB + 5).toInt(), (MinB + 2).toInt())) + doTest((MinS + 5).toShort() downTo MinS step 3, (MinS + 5).toInt(), (MinS + 2).toInt(), -3, listOf((MinS + 5).toInt(), (MinS + 2).toInt())) + doTest((MinL + 5).toLong() downTo MinL step 3, (MinL + 5).toLong(), (MinL + 2).toLong(), -3.toLong(), listOf((MinL + 5).toLong(), (MinL + 2).toLong())) - doTest((MinC + 5) downTo MinC step 3, (MinC + 5), MinC, -3, listOf((MinC + 5), (MinC + 2))) + doTest((MinC + 5) downTo MinC step 3, (MinC + 5), (MinC + 2), -3, listOf((MinC + 5), (MinC + 2))) } } \ No newline at end of file diff --git a/libraries/stdlib/test/language/RangeIterationTest.kt b/libraries/stdlib/test/language/RangeIterationTest.kt index 4c6b64d2c89..cdb90b2a367 100644 --- a/libraries/stdlib/test/language/RangeIterationTest.kt +++ b/libraries/stdlib/test/language/RangeIterationTest.kt @@ -4,25 +4,57 @@ package language import org.junit.Test as test import kotlin.test.* -// Test data for codegen is generated from this class. If you change it, rerun GenerateTests -public class RangeIterationTest { - private fun doTest( - sequence: Progression, - expectedStart: N, - expectedEnd: N, +public open class RangeIterationTestBase { + public fun doTest( + sequence: Iterable, + expectedFirst: N, + expectedLast: N, expectedIncrement: Number, expectedElements: List ) { - assertEquals(expectedStart, sequence.start) - assertEquals(expectedEnd, sequence.end) - assertEquals(expectedIncrement, sequence.increment) + val first: Any + val last: Any + val increment: Number + when (sequence) { + is IntProgression -> { + first = sequence.first + last = sequence.last + increment = sequence.increment + } + is LongProgression -> { + first = sequence.first + last = sequence.last + increment = sequence.increment + } + is CharProgression -> { + first = sequence.first + last = sequence.last + increment = sequence.increment + } + // TODO: Drop this branch + is Progression -> { + first = sequence.start + last = sequence.end + increment = sequence.increment + } + else -> throw IllegalArgumentException("Unsupported sequence type: $sequence") + } - if (expectedElements.none()) + assertEquals(expectedFirst, first) + assertEquals(expectedLast, last) + assertEquals(expectedIncrement, increment) + + if (expectedElements.isEmpty()) assertTrue(sequence.none()) else assertEquals(expectedElements, sequence.toList()) } +} + +// Test data for codegen is generated from this class. If you change it, rerun GenerateTests +public class RangeIterationTest : RangeIterationTestBase() { + @test fun emptyConstant() { doTest(IntRange.EMPTY, 1, 0, 1, listOf()) doTest(ByteRange.EMPTY, 1.toByte(), 0.toByte(), 1, listOf()) @@ -162,12 +194,12 @@ public class RangeIterationTest { // 'inexact' means last element is not equal to sequence end @test fun inexactSteppedRange() { - doTest(3..8 step 2, 3, 8, 2, listOf(3, 5, 7)) - doTest(3.toByte()..8.toByte() step 2, 3, 8, 2, listOf(3, 5, 7)) - doTest(3.toShort()..8.toShort() step 2, 3, 8, 2, listOf(3, 5, 7)) - doTest(3.toLong()..8.toLong() step 2.toLong(), 3.toLong(), 8.toLong(), 2.toLong(), listOf(3, 5, 7)) + doTest(3..8 step 2, 3, 7, 2, listOf(3, 5, 7)) + doTest(3.toByte()..8.toByte() step 2, 3, 7, 2, listOf(3, 5, 7)) + doTest(3.toShort()..8.toShort() step 2, 3, 7, 2, listOf(3, 5, 7)) + doTest(3.toLong()..8.toLong() step 2.toLong(), 3.toLong(), 7.toLong(), 2.toLong(), listOf(3, 5, 7)) - doTest('a'..'d' step 2, 'a', 'd', 2, listOf('a', 'c')) + doTest('a'..'d' step 2, 'a', 'c', 2, listOf('a', 'c')) doTest(4.0..5.8 step 0.5, 4.0, 5.8, 0.5, listOf(4.0, 4.5, 5.0, 5.5)) doTest(4.0.toFloat()..5.8.toFloat() step 0.5.toFloat(), 4.0.toFloat(), 5.8.toFloat(), 0.5.toFloat(), @@ -176,12 +208,12 @@ public class RangeIterationTest { // 'inexact' means last element is not equal to sequence end @test fun inexactSteppedDownTo() { - doTest(8 downTo 3 step 2, 8, 3, -2, listOf(8, 6, 4)) - doTest(8.toByte() downTo 3.toByte() step 2, 8, 3, -2, listOf(8, 6, 4)) - doTest(8.toShort() downTo 3.toShort() step 2, 8, 3, -2, listOf(8, 6, 4)) - doTest(8.toLong() downTo 3.toLong() step 2.toLong(), 8.toLong(), 3.toLong(), -2.toLong(), listOf(8, 6, 4)) + doTest(8 downTo 3 step 2, 8, 4, -2, listOf(8, 6, 4)) + doTest(8.toByte() downTo 3.toByte() step 2, 8, 4, -2, listOf(8, 6, 4)) + doTest(8.toShort() downTo 3.toShort() step 2, 8, 4, -2, listOf(8, 6, 4)) + doTest(8.toLong() downTo 3.toLong() step 2.toLong(), 8.toLong(), 4.toLong(), -2.toLong(), listOf(8, 6, 4)) - doTest('d' downTo 'a' step 2, 'd', 'a', -2, listOf('d', 'b')) + doTest('d' downTo 'a' step 2, 'd', 'b', -2, listOf('d', 'b')) doTest(5.5 downTo 3.7 step 0.5, 5.5, 3.7, -0.5, listOf(5.5, 5.0, 4.5, 4.0)) doTest(5.5.toFloat() downTo 3.7.toFloat() step 0.5.toFloat(), 5.5.toFloat(), 3.7.toFloat(), -0.5.toFloat(), @@ -252,17 +284,14 @@ public class RangeIterationTest { listOf(6.0.toFloat(), 5.5.toFloat(), 5.0.toFloat(), 4.5.toFloat(), 4.0.toFloat())) } - // 'inexact' means last element is not equal to sequence end + // invariant progression.reversed().toList() == progression.toList().reversed() is preserved + // 'inexact' means that start of reversed progression is not the end of original progression, but the last element @test fun reversedInexactSteppedDownTo() { - doTest((8 downTo 3 step 2).reversed(), 3, 8, 2, listOf(3, 5, 7)) - doTest((8.toByte() downTo 3.toByte() step 2).reversed(), 3, 8, 2, listOf(3, 5, 7)) - doTest((8.toShort() downTo 3.toShort() step 2).reversed(), 3, 8, 2, listOf(3, 5, 7)) - doTest((8.toLong() downTo 3.toLong() step 2.toLong()).reversed(), 3.toLong(), 8.toLong(), 2.toLong(), listOf(3, 5, 7)) + doTest((8 downTo 3 step 2).reversed(), 4, 8, 2, listOf(4, 6, 8)) + doTest((8.toByte() downTo 3.toByte() step 2).reversed(), 4, 8, 2, listOf(4, 6, 8)) + doTest((8.toShort() downTo 3.toShort() step 2).reversed(), 4, 8, 2, listOf(4, 6, 8)) + doTest((8.toLong() downTo 3.toLong() step 2.toLong()).reversed(), 4.toLong(), 8.toLong(), 2.toLong(), listOf(4, 6, 8)) - doTest(('d' downTo 'a' step 2).reversed(), 'a', 'd', 2, listOf('a', 'c')) - - doTest((5.8 downTo 4.0 step 0.5).reversed(), 4.0, 5.8, 0.5, listOf(4.0, 4.5, 5.0, 5.5)) - doTest((5.8.toFloat() downTo 4.0.toFloat() step 0.5.toFloat()).reversed(), 4.0.toFloat(), 5.8.toFloat(), 0.5.toFloat(), - listOf(4.0.toFloat(), 4.5.toFloat(), 5.0.toFloat(), 5.5.toFloat())) + doTest(('d' downTo 'a' step 2).reversed(), 'b', 'd', 2, listOf('b', 'd')) } }