From 009980944b3cfbc768859e7202e4454c98b40c6d Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Mon, 27 Aug 2018 04:07:10 +0300 Subject: [PATCH] Refine the exception type of EmptyRange.random() Make it the same type as in case of an empty collection, so that `emptyRange.random()` throws the same exception as `emptyRange.toList().random()`. #KT-15539 --- .../stdlib/common/src/generated/_Ranges.kt | 18 +++++++++++++++--- .../stdlib/common/src/generated/_URanges.kt | 12 ++++++++++-- libraries/stdlib/test/ranges/RangeTest.kt | 8 ++++++++ .../src/templates/Elements.kt | 8 +++++++- 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/libraries/stdlib/common/src/generated/_Ranges.kt b/libraries/stdlib/common/src/generated/_Ranges.kt index 190de27f7d5..5f1c76fcf7b 100644 --- a/libraries/stdlib/common/src/generated/_Ranges.kt +++ b/libraries/stdlib/common/src/generated/_Ranges.kt @@ -58,7 +58,11 @@ public inline fun CharRange.random(): Char { */ @SinceKotlin("1.3") public fun IntRange.random(random: Random): Int { - return random.nextInt(this) + try { + return random.nextInt(this) + } catch(e: IllegalArgumentException) { + throw NoSuchElementException(e.message) + } } /** @@ -68,7 +72,11 @@ public fun IntRange.random(random: Random): Int { */ @SinceKotlin("1.3") public fun LongRange.random(random: Random): Long { - return random.nextLong(this) + try { + return random.nextLong(this) + } catch(e: IllegalArgumentException) { + throw NoSuchElementException(e.message) + } } /** @@ -78,7 +86,11 @@ public fun LongRange.random(random: Random): Long { */ @SinceKotlin("1.3") public fun CharRange.random(random: Random): Char { - return random.nextInt(first.toInt(), last.toInt() + 1).toChar() + try { + return random.nextInt(first.toInt(), last.toInt() + 1).toChar() + } catch(e: IllegalArgumentException) { + throw NoSuchElementException(e.message) + } } /** diff --git a/libraries/stdlib/common/src/generated/_URanges.kt b/libraries/stdlib/common/src/generated/_URanges.kt index 148c2ce5876..123d1e947fc 100644 --- a/libraries/stdlib/common/src/generated/_URanges.kt +++ b/libraries/stdlib/common/src/generated/_URanges.kt @@ -50,7 +50,11 @@ public inline fun ULongRange.random(): ULong { @SinceKotlin("1.3") @ExperimentalUnsignedTypes public fun UIntRange.random(random: Random): UInt { - return random.nextUInt(this) + try { + return random.nextUInt(this) + } catch(e: IllegalArgumentException) { + throw NoSuchElementException(e.message) + } } /** @@ -61,7 +65,11 @@ public fun UIntRange.random(random: Random): UInt { @SinceKotlin("1.3") @ExperimentalUnsignedTypes public fun ULongRange.random(random: Random): ULong { - return random.nextULong(this) + try { + return random.nextULong(this) + } catch(e: IllegalArgumentException) { + throw NoSuchElementException(e.message) + } } /** diff --git a/libraries/stdlib/test/ranges/RangeTest.kt b/libraries/stdlib/test/ranges/RangeTest.kt index 6779bc2324b..17d67592a34 100644 --- a/libraries/stdlib/test/ranges/RangeTest.kt +++ b/libraries/stdlib/test/ranges/RangeTest.kt @@ -388,4 +388,12 @@ public class RangeTest { assertFailsWithIllegalArgument { UIntProgression.fromClosedRange(0u, 1u, Int.MIN_VALUE) } assertFailsWithIllegalArgument { ULongProgression.fromClosedRange(0u, 1u, Long.MIN_VALUE) } } + + @Test fun randomInEmptyRange() { + assertFailsWith { IntRange.EMPTY.random() } + assertFailsWith { LongRange.EMPTY.random() } + assertFailsWith { CharRange.EMPTY.random() } + assertFailsWith { UIntRange.EMPTY.random() } + assertFailsWith { ULongRange.EMPTY.random() } + } } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt index ec2da831a38..4f56b48ab70 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt @@ -877,7 +877,13 @@ object Elements : TemplateGroupBase() { PrimitiveType.Char -> "nextInt(first.toInt(), last.toInt() + 1).toChar()" else -> "next$primitive(this)" } - """return random.$expr""" + """ + try { + return random.$expr + } catch(e: IllegalArgumentException) { + throw NoSuchElementException(e.message) + } + """ } } }