reversed() behavior changed for progressions. Stepped progression behavior changes: 'end' property is now deprecated, use 'last' instead.

This commit is contained in:
Ilya Gorbunov
2015-11-05 20:12:48 +03:00
parent ead333531c
commit 4d2f9b82da
4 changed files with 80 additions and 133 deletions
@@ -8,7 +8,7 @@ fun box(): String {
list1.add(i)
if (list1.size() > 23) break
}
if (list1 != listOf<Int>(3, 5, 7)) {
if (list1 != listOf<Int>(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<Int>(3, 5, 7)) {
if (list2 != listOf<Int>(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<Int>(3, 5, 7)) {
if (list3 != listOf<Int>(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<Long>(3, 5, 7)) {
if (list4 != listOf<Long>(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<Char>('a', 'c')) {
if (list5 != listOf<Char>('b', 'd')) {
return "Wrong elements for ('d' downTo 'a' step 2).reversed(): $list5"
}
val list6 = ArrayList<Double>()
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<Double>(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<Float>()
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<Float>(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"
}
@@ -7,7 +7,7 @@ fun box(): String {
list1.add(i)
if (list1.size() > 23) break
}
if (list1 != listOf<Int>(3, 5, 7)) {
if (list1 != listOf<Int>(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<Int>(3, 5, 7)) {
if (list2 != listOf<Int>(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<Int>(3, 5, 7)) {
if (list3 != listOf<Int>(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<Long>(3, 5, 7)) {
if (list4 != listOf<Long>(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<Char>('a', 'c')) {
if (list5 != listOf<Char>('b', 'd')) {
return "Wrong elements for ('d' downTo 'a' step 2).reversed(): $list5"
}
val list6 = ArrayList<Double>()
for (i in (5.8 downTo 4.0 step 0.5).reversed()) {
list6.add(i)
if (list6.size() > 23) break
}
if (list6 != listOf<Double>(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<Float>()
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<Float>(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"
}
@@ -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 <N : Any> doTest(
sequence: Iterable<N>,
expectedFirst: N,
expectedLast: N,
expectedIncrement: Number,
expectedElements: List<N>
) {
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)))
}
}
@@ -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 <N : Any> doTest(
sequence: Progression<N>,
expectedStart: N,
expectedEnd: N,
public open class RangeIterationTestBase {
public fun <N : Any> doTest(
sequence: Iterable<N>,
expectedFirst: N,
expectedLast: N,
expectedIncrement: Number,
expectedElements: List<N>
) {
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<Long>(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<Long>(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<Long>(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<Long>(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<Float>(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<Long>(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<Long>(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<Float>(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'))
}
}