Add sample for coerceIn with floating point range
#KT-20357
This commit is contained in:
@@ -889,6 +889,7 @@ public fun Double.coerceIn(minimumValue: Double, maximumValue: Double): Double {
|
|||||||
* Ensures that this value lies in the specified [range].
|
* Ensures that this value lies in the specified [range].
|
||||||
*
|
*
|
||||||
* @return this value if it's in the [range], or `range.start` if this value is less than `range.start`, or `range.endInclusive` if this value is greater than `range.endInclusive`.
|
* @return this value if it's in the [range], or `range.start` if this value is less than `range.start`, or `range.endInclusive` if this value is greater than `range.endInclusive`.
|
||||||
|
* @sample samples.comparisons.ComparableOps.coerceInFloatingPointRange
|
||||||
*/
|
*/
|
||||||
@SinceKotlin("1.1")
|
@SinceKotlin("1.1")
|
||||||
public fun <T: Comparable<T>> T.coerceIn(range: ClosedFloatingPointRange<T>): T {
|
public fun <T: Comparable<T>> T.coerceIn(range: ClosedFloatingPointRange<T>): T {
|
||||||
|
|||||||
@@ -640,6 +640,7 @@ public expect fun Double.coerceIn(minimumValue: Double, maximumValue: Double): D
|
|||||||
* Ensures that this value lies in the specified [range].
|
* Ensures that this value lies in the specified [range].
|
||||||
*
|
*
|
||||||
* @return this value if it's in the [range], or `range.start` if this value is less than `range.start`, or `range.endInclusive` if this value is greater than `range.endInclusive`.
|
* @return this value if it's in the [range], or `range.start` if this value is less than `range.start`, or `range.endInclusive` if this value is greater than `range.endInclusive`.
|
||||||
|
* @sample samples.comparisons.ComparableOps.coerceInFloatingPointRange
|
||||||
*/
|
*/
|
||||||
@SinceKotlin("1.1")
|
@SinceKotlin("1.1")
|
||||||
public expect fun <T: Comparable<T>> T.coerceIn(range: ClosedFloatingPointRange<T>): T
|
public expect fun <T: Comparable<T>> T.coerceIn(range: ClosedFloatingPointRange<T>): T
|
||||||
|
|||||||
@@ -66,4 +66,12 @@ class ComparableOps {
|
|||||||
|
|
||||||
assertPrints(DayOfWeek.FRIDAY.coerceIn(DayOfWeek.SATURDAY, DayOfWeek.SUNDAY), "SATURDAY")
|
assertPrints(DayOfWeek.FRIDAY.coerceIn(DayOfWeek.SATURDAY, DayOfWeek.SUNDAY), "SATURDAY")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Sample
|
||||||
|
fun coerceInFloatingPointRange() {
|
||||||
|
assertPrints(10.1.coerceIn(1.0..10.0), "10.0")
|
||||||
|
assertPrints(9.9.coerceIn(1.0..10.0), "9.9")
|
||||||
|
|
||||||
|
assertFailsWith<IllegalArgumentException> { 9.9.coerceIn(1.0..Double.NaN) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -889,6 +889,7 @@ public fun Double.coerceIn(minimumValue: Double, maximumValue: Double): Double {
|
|||||||
* Ensures that this value lies in the specified [range].
|
* Ensures that this value lies in the specified [range].
|
||||||
*
|
*
|
||||||
* @return this value if it's in the [range], or `range.start` if this value is less than `range.start`, or `range.endInclusive` if this value is greater than `range.endInclusive`.
|
* @return this value if it's in the [range], or `range.start` if this value is less than `range.start`, or `range.endInclusive` if this value is greater than `range.endInclusive`.
|
||||||
|
* @sample samples.comparisons.ComparableOps.coerceInFloatingPointRange
|
||||||
*/
|
*/
|
||||||
@SinceKotlin("1.1")
|
@SinceKotlin("1.1")
|
||||||
public fun <T: Comparable<T>> T.coerceIn(range: ClosedFloatingPointRange<T>): T {
|
public fun <T: Comparable<T>> T.coerceIn(range: ClosedFloatingPointRange<T>): T {
|
||||||
|
|||||||
@@ -123,6 +123,7 @@ object ComparableOps : TemplateGroupBase() {
|
|||||||
Ensures that this value lies in the specified [range].
|
Ensures that this value lies in the specified [range].
|
||||||
|
|
||||||
@return this value if it's in the [range], or `range.start` if this value is less than `range.start`, or `range.endInclusive` if this value is greater than `range.endInclusive`.
|
@return this value if it's in the [range], or `range.start` if this value is less than `range.start`, or `range.endInclusive` if this value is greater than `range.endInclusive`.
|
||||||
|
@sample samples.comparisons.ComparableOps.coerceInFloatingPointRange
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
body(Generic) {
|
body(Generic) {
|
||||||
|
|||||||
Reference in New Issue
Block a user