Add tests for open ranges

#KT-52932
This commit is contained in:
Ilya Gorbunov
2022-06-26 03:17:48 +03:00
committed by Space
parent 8584fe2725
commit 4475b15037
2 changed files with 71 additions and 1 deletions
+54 -1
View File
@@ -38,11 +38,17 @@ public class RangeTest {
assertTrue(1 as Int? in range)
assertFalse(10 as Int? in range)
val closedRange = 1..9
val openRange = 1 until 10
assertTrue(9 in openRange)
assertFalse(10 in openRange)
assertEquals(closedRange, openRange)
val openRange2 = 1..<10
assertEquals(closedRange, openRange2)
assertTrue((1 until Int.MIN_VALUE).isEmpty())
assertFailsWith<IllegalStateException> { (1..Int.MAX_VALUE).endExclusive }
}
@Test fun byteRange() {
@@ -141,13 +147,18 @@ public class RangeTest {
assertTrue(1L as Long? in range)
assertFalse(10L as Long? in range)
val closedRange = 1L..9L
val openRange = 1L until 10L
assertTrue(9L in openRange)
assertFalse(10L in openRange)
assertEquals(closedRange, openRange)
val openRange2 = 1L..<10L
assertEquals(closedRange, openRange2)
assertTrue((0 until Long.MIN_VALUE).isEmpty())
assertTrue((0L until Long.MIN_VALUE).isEmpty())
assertFailsWith<IllegalStateException> { (1..Long.MAX_VALUE).endExclusive }
}
@Test fun charRange() {
@@ -174,11 +185,17 @@ public class RangeTest {
assertTrue('p' as Char? in range)
assertFalse('z' as Char? in range)
val closedRange = 'A'..'Y'
val openRange = 'A' until 'Z'
assertTrue('Y' in openRange)
assertFalse('Z' in openRange)
assertEquals(closedRange, openRange)
val openRange2 = 'A'..<'Z'
assertEquals(closedRange, openRange2)
assertTrue(('A' until Char.MIN_VALUE).isEmpty())
assertFailsWith<IllegalStateException> { ('A'..Char.MAX_VALUE).endExclusive }
}
@Test fun doubleRange() {
@@ -221,6 +238,14 @@ public class RangeTest {
assertFalse(Double.NEGATIVE_INFINITY in halfInfRange)
assertFalse(Double.NaN in halfInfRange)
assertTrue(Float.POSITIVE_INFINITY in halfInfRange)
val openRange = 0.0..<Double.POSITIVE_INFINITY
assertTrue(Double.MAX_VALUE in openRange)
assertFalse(Double.POSITIVE_INFINITY in openRange)
assertFalse(Double.NEGATIVE_INFINITY in openRange)
assertFalse(Double.NaN in openRange)
assertFalse(Float.POSITIVE_INFINITY in openRange)
}
@Test fun floatRange() {
@@ -264,6 +289,12 @@ public class RangeTest {
assertFalse(Float.NaN in halfInfRange)
assertTrue(Double.POSITIVE_INFINITY in halfInfRange)
assertTrue(Double.MAX_VALUE in halfInfRange)
val openRange = 0.0F..<Float.POSITIVE_INFINITY
assertTrue(Float.MAX_VALUE in openRange)
assertFalse(Float.POSITIVE_INFINITY in openRange)
assertFalse(Float.NEGATIVE_INFINITY in openRange)
assertFalse(Float.NaN in openRange)
}
@Suppress("EmptyRange")
@@ -334,6 +365,9 @@ public class RangeTest {
@Test fun comparableRange() {
val range = "island".."isle"
assertEquals("island..isle", range.toString())
assertEquals(range, range.start..range.endInclusive)
assertFalse("apple" in range)
assertFalse("icicle" in range)
@@ -345,6 +379,25 @@ public class RangeTest {
assertFalse("trail" in range)
assertFalse(range.isEmpty())
assertFalse(("empty".."empty").isEmpty())
}
@Test fun comparableOpenRange() {
val range = "island"..<"isle"
assertEquals("island..<isle", range.toString())
assertEquals(range, range.start..<range.endExclusive)
assertFalse("apple" in range)
assertFalse("icicle" in range)
assertTrue("island" in range)
assertFalse("isle" in range)
assertTrue("islandic" in range)
assertFalse("item" in range)
assertFalse("trail" in range)
assertFalse(range.isEmpty())
assertTrue(("empty"..<"empty").isEmpty())
}
private fun assertFailsWithIllegalArgument(f: () -> Unit) = assertFailsWith<IllegalArgumentException> { f() }
@@ -40,11 +40,17 @@ public class URangeTest {
assertTrue(12u as UInt? in range)
assertFalse((-3).toUInt() as UInt? in range)
val closedRange = 1u..9u
val openRange = 1u until 10u
assertTrue(9u in openRange)
assertFalse(10u in openRange)
assertEquals(closedRange, openRange)
val openRange2 = 1u..<10u
assertEquals(closedRange, openRange2)
assertTrue((1u until UInt.MIN_VALUE).isEmpty())
assertFailsWith<IllegalStateException> { (1u..UInt.MAX_VALUE).endExclusive }
}
@Test
@@ -77,6 +83,8 @@ public class URangeTest {
val openRange = 1.toUByte() until 10.toUByte()
assertTrue(9.toUByte() in openRange)
assertFalse(10.toUByte() in openRange)
val openRange2 = 1.toUByte()..<10.toUByte()
assertEquals(openRange, openRange2)
assertTrue((UByte.MAX_VALUE until UByte.MIN_VALUE).isEmpty())
}
@@ -111,6 +119,8 @@ public class URangeTest {
val openRange = 1.toUShort() until 10.toUShort()
assertTrue(9.toUShort() in openRange)
assertFalse(10.toUShort() in openRange)
val openRange2 = 1.toUShort()..<10.toUShort()
assertEquals(openRange, openRange2)
assertTrue((0.toUShort() until UShort.MIN_VALUE).isEmpty())
}
@@ -144,11 +154,17 @@ public class URangeTest {
assertTrue(12uL as ULong? in range)
assertFalse((-3).toULong() as ULong? in range)
val closedRange = 1uL..9uL
val openRange = 1uL until 10uL
assertTrue(9uL in openRange)
assertFalse(10uL in openRange)
assertEquals(closedRange, openRange)
val openRange2 = 1uL..<10uL
assertEquals(closedRange, openRange2)
assertTrue((0uL until ULong.MIN_VALUE).isEmpty())
assertFailsWith<IllegalStateException> { (1uL..ULong.MAX_VALUE).endExclusive }
}
@Suppress("EmptyRange")
@@ -183,6 +199,7 @@ public class URangeTest {
assertTrue(0uL downTo (-1).toULong() == (-2).toULong() downTo (-1).toULong())
assertFalse(0u..1u == UIntRange.EMPTY)
assertTrue(0u..<0u == UIntRange.EMPTY)
}
@Suppress("EmptyRange")