Document and test NaN propagation of maxOf/minOf

Also simplify minOf/maxOf implementations

#KT-38708
This commit is contained in:
Ilya Gorbunov
2020-05-21 23:42:57 +03:00
parent 7b68de38e1
commit b4ba00ca36
10 changed files with 514 additions and 1232 deletions
File diff suppressed because it is too large Load Diff
@@ -1797,6 +1797,8 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.maxBy(selector: (T) -> R):
* Returns the largest value among all values produced by [selector] function * Returns the largest value among all values produced by [selector] function
* applied to each element in the collection. * applied to each element in the collection.
* *
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*
* @throws NoSuchElementException if the collection is empty. * @throws NoSuchElementException if the collection is empty.
*/ */
@SinceKotlin("1.4") @SinceKotlin("1.4")
@@ -1807,13 +1809,9 @@ public inline fun <T> Iterable<T>.maxOf(selector: (T) -> Double): Double {
val iterator = iterator() val iterator = iterator()
if (!iterator.hasNext()) throw NoSuchElementException() if (!iterator.hasNext()) throw NoSuchElementException()
var maxValue = selector(iterator.next()) var maxValue = selector(iterator.next())
if (maxValue.isNaN()) return maxValue
while (iterator.hasNext()) { while (iterator.hasNext()) {
val v = selector(iterator.next()) val v = selector(iterator.next())
if (v.isNaN()) return v maxValue = maxOf(maxValue, v)
if (maxValue < v) {
maxValue = v
}
} }
return maxValue return maxValue
} }
@@ -1822,6 +1820,8 @@ public inline fun <T> Iterable<T>.maxOf(selector: (T) -> Double): Double {
* Returns the largest value among all values produced by [selector] function * Returns the largest value among all values produced by [selector] function
* applied to each element in the collection. * applied to each element in the collection.
* *
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*
* @throws NoSuchElementException if the collection is empty. * @throws NoSuchElementException if the collection is empty.
*/ */
@SinceKotlin("1.4") @SinceKotlin("1.4")
@@ -1832,13 +1832,9 @@ public inline fun <T> Iterable<T>.maxOf(selector: (T) -> Float): Float {
val iterator = iterator() val iterator = iterator()
if (!iterator.hasNext()) throw NoSuchElementException() if (!iterator.hasNext()) throw NoSuchElementException()
var maxValue = selector(iterator.next()) var maxValue = selector(iterator.next())
if (maxValue.isNaN()) return maxValue
while (iterator.hasNext()) { while (iterator.hasNext()) {
val v = selector(iterator.next()) val v = selector(iterator.next())
if (v.isNaN()) return v maxValue = maxOf(maxValue, v)
if (maxValue < v) {
maxValue = v
}
} }
return maxValue return maxValue
} }
@@ -1859,7 +1855,6 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.maxOf(selector: (T) -> R):
var maxValue = selector(iterator.next()) var maxValue = selector(iterator.next())
while (iterator.hasNext()) { while (iterator.hasNext()) {
val v = selector(iterator.next()) val v = selector(iterator.next())
if (maxValue < v) { if (maxValue < v) {
maxValue = v maxValue = v
} }
@@ -1870,6 +1865,8 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.maxOf(selector: (T) -> R):
/** /**
* Returns the largest value among all values produced by [selector] function * Returns the largest value among all values produced by [selector] function
* applied to each element in the collection or `null` if there are no elements. * applied to each element in the collection or `null` if there are no elements.
*
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*/ */
@SinceKotlin("1.4") @SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class) @OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@@ -1879,13 +1876,9 @@ public inline fun <T> Iterable<T>.maxOfOrNull(selector: (T) -> Double): Double?
val iterator = iterator() val iterator = iterator()
if (!iterator.hasNext()) return null if (!iterator.hasNext()) return null
var maxValue = selector(iterator.next()) var maxValue = selector(iterator.next())
if (maxValue.isNaN()) return maxValue
while (iterator.hasNext()) { while (iterator.hasNext()) {
val v = selector(iterator.next()) val v = selector(iterator.next())
if (v.isNaN()) return v maxValue = maxOf(maxValue, v)
if (maxValue < v) {
maxValue = v
}
} }
return maxValue return maxValue
} }
@@ -1893,6 +1886,8 @@ public inline fun <T> Iterable<T>.maxOfOrNull(selector: (T) -> Double): Double?
/** /**
* Returns the largest value among all values produced by [selector] function * Returns the largest value among all values produced by [selector] function
* applied to each element in the collection or `null` if there are no elements. * applied to each element in the collection or `null` if there are no elements.
*
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*/ */
@SinceKotlin("1.4") @SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class) @OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@@ -1902,13 +1897,9 @@ public inline fun <T> Iterable<T>.maxOfOrNull(selector: (T) -> Float): Float? {
val iterator = iterator() val iterator = iterator()
if (!iterator.hasNext()) return null if (!iterator.hasNext()) return null
var maxValue = selector(iterator.next()) var maxValue = selector(iterator.next())
if (maxValue.isNaN()) return maxValue
while (iterator.hasNext()) { while (iterator.hasNext()) {
val v = selector(iterator.next()) val v = selector(iterator.next())
if (v.isNaN()) return v maxValue = maxOf(maxValue, v)
if (maxValue < v) {
maxValue = v
}
} }
return maxValue return maxValue
} }
@@ -1927,7 +1918,6 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.maxOfOrNull(selector: (T) -
var maxValue = selector(iterator.next()) var maxValue = selector(iterator.next())
while (iterator.hasNext()) { while (iterator.hasNext()) {
val v = selector(iterator.next()) val v = selector(iterator.next())
if (maxValue < v) { if (maxValue < v) {
maxValue = v maxValue = v
} }
@@ -2071,6 +2061,8 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.minBy(selector: (T) -> R):
* Returns the smallest value among all values produced by [selector] function * Returns the smallest value among all values produced by [selector] function
* applied to each element in the collection. * applied to each element in the collection.
* *
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*
* @throws NoSuchElementException if the collection is empty. * @throws NoSuchElementException if the collection is empty.
*/ */
@SinceKotlin("1.4") @SinceKotlin("1.4")
@@ -2081,13 +2073,9 @@ public inline fun <T> Iterable<T>.minOf(selector: (T) -> Double): Double {
val iterator = iterator() val iterator = iterator()
if (!iterator.hasNext()) throw NoSuchElementException() if (!iterator.hasNext()) throw NoSuchElementException()
var minValue = selector(iterator.next()) var minValue = selector(iterator.next())
if (minValue.isNaN()) return minValue
while (iterator.hasNext()) { while (iterator.hasNext()) {
val v = selector(iterator.next()) val v = selector(iterator.next())
if (v.isNaN()) return v minValue = minOf(minValue, v)
if (minValue > v) {
minValue = v
}
} }
return minValue return minValue
} }
@@ -2096,6 +2084,8 @@ public inline fun <T> Iterable<T>.minOf(selector: (T) -> Double): Double {
* Returns the smallest value among all values produced by [selector] function * Returns the smallest value among all values produced by [selector] function
* applied to each element in the collection. * applied to each element in the collection.
* *
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*
* @throws NoSuchElementException if the collection is empty. * @throws NoSuchElementException if the collection is empty.
*/ */
@SinceKotlin("1.4") @SinceKotlin("1.4")
@@ -2106,13 +2096,9 @@ public inline fun <T> Iterable<T>.minOf(selector: (T) -> Float): Float {
val iterator = iterator() val iterator = iterator()
if (!iterator.hasNext()) throw NoSuchElementException() if (!iterator.hasNext()) throw NoSuchElementException()
var minValue = selector(iterator.next()) var minValue = selector(iterator.next())
if (minValue.isNaN()) return minValue
while (iterator.hasNext()) { while (iterator.hasNext()) {
val v = selector(iterator.next()) val v = selector(iterator.next())
if (v.isNaN()) return v minValue = minOf(minValue, v)
if (minValue > v) {
minValue = v
}
} }
return minValue return minValue
} }
@@ -2133,7 +2119,6 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.minOf(selector: (T) -> R):
var minValue = selector(iterator.next()) var minValue = selector(iterator.next())
while (iterator.hasNext()) { while (iterator.hasNext()) {
val v = selector(iterator.next()) val v = selector(iterator.next())
if (minValue > v) { if (minValue > v) {
minValue = v minValue = v
} }
@@ -2144,6 +2129,8 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.minOf(selector: (T) -> R):
/** /**
* Returns the smallest value among all values produced by [selector] function * Returns the smallest value among all values produced by [selector] function
* applied to each element in the collection or `null` if there are no elements. * applied to each element in the collection or `null` if there are no elements.
*
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*/ */
@SinceKotlin("1.4") @SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class) @OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@@ -2153,13 +2140,9 @@ public inline fun <T> Iterable<T>.minOfOrNull(selector: (T) -> Double): Double?
val iterator = iterator() val iterator = iterator()
if (!iterator.hasNext()) return null if (!iterator.hasNext()) return null
var minValue = selector(iterator.next()) var minValue = selector(iterator.next())
if (minValue.isNaN()) return minValue
while (iterator.hasNext()) { while (iterator.hasNext()) {
val v = selector(iterator.next()) val v = selector(iterator.next())
if (v.isNaN()) return v minValue = minOf(minValue, v)
if (minValue > v) {
minValue = v
}
} }
return minValue return minValue
} }
@@ -2167,6 +2150,8 @@ public inline fun <T> Iterable<T>.minOfOrNull(selector: (T) -> Double): Double?
/** /**
* Returns the smallest value among all values produced by [selector] function * Returns the smallest value among all values produced by [selector] function
* applied to each element in the collection or `null` if there are no elements. * applied to each element in the collection or `null` if there are no elements.
*
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*/ */
@SinceKotlin("1.4") @SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class) @OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@@ -2176,13 +2161,9 @@ public inline fun <T> Iterable<T>.minOfOrNull(selector: (T) -> Float): Float? {
val iterator = iterator() val iterator = iterator()
if (!iterator.hasNext()) return null if (!iterator.hasNext()) return null
var minValue = selector(iterator.next()) var minValue = selector(iterator.next())
if (minValue.isNaN()) return minValue
while (iterator.hasNext()) { while (iterator.hasNext()) {
val v = selector(iterator.next()) val v = selector(iterator.next())
if (v.isNaN()) return v minValue = minOf(minValue, v)
if (minValue > v) {
minValue = v
}
} }
return minValue return minValue
} }
@@ -2201,7 +2182,6 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.minOfOrNull(selector: (T) -
var minValue = selector(iterator.next()) var minValue = selector(iterator.next())
while (iterator.hasNext()) { while (iterator.hasNext()) {
val v = selector(iterator.next()) val v = selector(iterator.next())
if (minValue > v) { if (minValue > v) {
minValue = v minValue = v
} }
@@ -193,6 +193,8 @@ public inline fun <K, V, R : Comparable<R>> Map<out K, V>.maxBy(selector: (Map.E
* Returns the largest value among all values produced by [selector] function * Returns the largest value among all values produced by [selector] function
* applied to each entry in the map. * applied to each entry in the map.
* *
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*
* @throws NoSuchElementException if the map is empty. * @throws NoSuchElementException if the map is empty.
*/ */
@SinceKotlin("1.4") @SinceKotlin("1.4")
@@ -207,6 +209,8 @@ public inline fun <K, V> Map<out K, V>.maxOf(selector: (Map.Entry<K, V>) -> Doub
* Returns the largest value among all values produced by [selector] function * Returns the largest value among all values produced by [selector] function
* applied to each entry in the map. * applied to each entry in the map.
* *
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*
* @throws NoSuchElementException if the map is empty. * @throws NoSuchElementException if the map is empty.
*/ */
@SinceKotlin("1.4") @SinceKotlin("1.4")
@@ -234,6 +238,8 @@ public inline fun <K, V, R : Comparable<R>> Map<out K, V>.maxOf(selector: (Map.E
/** /**
* Returns the largest value among all values produced by [selector] function * Returns the largest value among all values produced by [selector] function
* applied to each entry in the map or `null` if there are no entries. * applied to each entry in the map or `null` if there are no entries.
*
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*/ */
@SinceKotlin("1.4") @SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class) @OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@@ -246,6 +252,8 @@ public inline fun <K, V> Map<out K, V>.maxOfOrNull(selector: (Map.Entry<K, V>) -
/** /**
* Returns the largest value among all values produced by [selector] function * Returns the largest value among all values produced by [selector] function
* applied to each entry in the map or `null` if there are no entries. * applied to each entry in the map or `null` if there are no entries.
*
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*/ */
@SinceKotlin("1.4") @SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class) @OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@@ -314,6 +322,8 @@ public inline fun <K, V, R : Comparable<R>> Map<out K, V>.minBy(selector: (Map.E
* Returns the smallest value among all values produced by [selector] function * Returns the smallest value among all values produced by [selector] function
* applied to each entry in the map. * applied to each entry in the map.
* *
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*
* @throws NoSuchElementException if the map is empty. * @throws NoSuchElementException if the map is empty.
*/ */
@SinceKotlin("1.4") @SinceKotlin("1.4")
@@ -328,6 +338,8 @@ public inline fun <K, V> Map<out K, V>.minOf(selector: (Map.Entry<K, V>) -> Doub
* Returns the smallest value among all values produced by [selector] function * Returns the smallest value among all values produced by [selector] function
* applied to each entry in the map. * applied to each entry in the map.
* *
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*
* @throws NoSuchElementException if the map is empty. * @throws NoSuchElementException if the map is empty.
*/ */
@SinceKotlin("1.4") @SinceKotlin("1.4")
@@ -355,6 +367,8 @@ public inline fun <K, V, R : Comparable<R>> Map<out K, V>.minOf(selector: (Map.E
/** /**
* Returns the smallest value among all values produced by [selector] function * Returns the smallest value among all values produced by [selector] function
* applied to each entry in the map or `null` if there are no entries. * applied to each entry in the map or `null` if there are no entries.
*
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*/ */
@SinceKotlin("1.4") @SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class) @OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@@ -367,6 +381,8 @@ public inline fun <K, V> Map<out K, V>.minOfOrNull(selector: (Map.Entry<K, V>) -
/** /**
* Returns the smallest value among all values produced by [selector] function * Returns the smallest value among all values produced by [selector] function
* applied to each entry in the map or `null` if there are no entries. * applied to each entry in the map or `null` if there are no entries.
*
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*/ */
@SinceKotlin("1.4") @SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class) @OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@@ -1252,6 +1252,8 @@ public inline fun <T, R : Comparable<R>> Sequence<T>.maxBy(selector: (T) -> R):
* Returns the largest value among all values produced by [selector] function * Returns the largest value among all values produced by [selector] function
* applied to each element in the sequence. * applied to each element in the sequence.
* *
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*
* @throws NoSuchElementException if the sequence is empty. * @throws NoSuchElementException if the sequence is empty.
* *
* The operation is _terminal_. * The operation is _terminal_.
@@ -1264,13 +1266,9 @@ public inline fun <T> Sequence<T>.maxOf(selector: (T) -> Double): Double {
val iterator = iterator() val iterator = iterator()
if (!iterator.hasNext()) throw NoSuchElementException() if (!iterator.hasNext()) throw NoSuchElementException()
var maxValue = selector(iterator.next()) var maxValue = selector(iterator.next())
if (maxValue.isNaN()) return maxValue
while (iterator.hasNext()) { while (iterator.hasNext()) {
val v = selector(iterator.next()) val v = selector(iterator.next())
if (v.isNaN()) return v maxValue = maxOf(maxValue, v)
if (maxValue < v) {
maxValue = v
}
} }
return maxValue return maxValue
} }
@@ -1279,6 +1277,8 @@ public inline fun <T> Sequence<T>.maxOf(selector: (T) -> Double): Double {
* Returns the largest value among all values produced by [selector] function * Returns the largest value among all values produced by [selector] function
* applied to each element in the sequence. * applied to each element in the sequence.
* *
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*
* @throws NoSuchElementException if the sequence is empty. * @throws NoSuchElementException if the sequence is empty.
* *
* The operation is _terminal_. * The operation is _terminal_.
@@ -1291,13 +1291,9 @@ public inline fun <T> Sequence<T>.maxOf(selector: (T) -> Float): Float {
val iterator = iterator() val iterator = iterator()
if (!iterator.hasNext()) throw NoSuchElementException() if (!iterator.hasNext()) throw NoSuchElementException()
var maxValue = selector(iterator.next()) var maxValue = selector(iterator.next())
if (maxValue.isNaN()) return maxValue
while (iterator.hasNext()) { while (iterator.hasNext()) {
val v = selector(iterator.next()) val v = selector(iterator.next())
if (v.isNaN()) return v maxValue = maxOf(maxValue, v)
if (maxValue < v) {
maxValue = v
}
} }
return maxValue return maxValue
} }
@@ -1320,7 +1316,6 @@ public inline fun <T, R : Comparable<R>> Sequence<T>.maxOf(selector: (T) -> R):
var maxValue = selector(iterator.next()) var maxValue = selector(iterator.next())
while (iterator.hasNext()) { while (iterator.hasNext()) {
val v = selector(iterator.next()) val v = selector(iterator.next())
if (maxValue < v) { if (maxValue < v) {
maxValue = v maxValue = v
} }
@@ -1331,6 +1326,8 @@ public inline fun <T, R : Comparable<R>> Sequence<T>.maxOf(selector: (T) -> R):
/** /**
* Returns the largest value among all values produced by [selector] function * Returns the largest value among all values produced by [selector] function
* applied to each element in the sequence or `null` if there are no elements. * applied to each element in the sequence or `null` if there are no elements.
*
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
* *
* The operation is _terminal_. * The operation is _terminal_.
*/ */
@@ -1342,13 +1339,9 @@ public inline fun <T> Sequence<T>.maxOfOrNull(selector: (T) -> Double): Double?
val iterator = iterator() val iterator = iterator()
if (!iterator.hasNext()) return null if (!iterator.hasNext()) return null
var maxValue = selector(iterator.next()) var maxValue = selector(iterator.next())
if (maxValue.isNaN()) return maxValue
while (iterator.hasNext()) { while (iterator.hasNext()) {
val v = selector(iterator.next()) val v = selector(iterator.next())
if (v.isNaN()) return v maxValue = maxOf(maxValue, v)
if (maxValue < v) {
maxValue = v
}
} }
return maxValue return maxValue
} }
@@ -1356,6 +1349,8 @@ public inline fun <T> Sequence<T>.maxOfOrNull(selector: (T) -> Double): Double?
/** /**
* Returns the largest value among all values produced by [selector] function * Returns the largest value among all values produced by [selector] function
* applied to each element in the sequence or `null` if there are no elements. * applied to each element in the sequence or `null` if there are no elements.
*
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
* *
* The operation is _terminal_. * The operation is _terminal_.
*/ */
@@ -1367,13 +1362,9 @@ public inline fun <T> Sequence<T>.maxOfOrNull(selector: (T) -> Float): Float? {
val iterator = iterator() val iterator = iterator()
if (!iterator.hasNext()) return null if (!iterator.hasNext()) return null
var maxValue = selector(iterator.next()) var maxValue = selector(iterator.next())
if (maxValue.isNaN()) return maxValue
while (iterator.hasNext()) { while (iterator.hasNext()) {
val v = selector(iterator.next()) val v = selector(iterator.next())
if (v.isNaN()) return v maxValue = maxOf(maxValue, v)
if (maxValue < v) {
maxValue = v
}
} }
return maxValue return maxValue
} }
@@ -1394,7 +1385,6 @@ public inline fun <T, R : Comparable<R>> Sequence<T>.maxOfOrNull(selector: (T) -
var maxValue = selector(iterator.next()) var maxValue = selector(iterator.next())
while (iterator.hasNext()) { while (iterator.hasNext()) {
val v = selector(iterator.next()) val v = selector(iterator.next())
if (maxValue < v) { if (maxValue < v) {
maxValue = v maxValue = v
} }
@@ -1552,6 +1542,8 @@ public inline fun <T, R : Comparable<R>> Sequence<T>.minBy(selector: (T) -> R):
* Returns the smallest value among all values produced by [selector] function * Returns the smallest value among all values produced by [selector] function
* applied to each element in the sequence. * applied to each element in the sequence.
* *
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*
* @throws NoSuchElementException if the sequence is empty. * @throws NoSuchElementException if the sequence is empty.
* *
* The operation is _terminal_. * The operation is _terminal_.
@@ -1564,13 +1556,9 @@ public inline fun <T> Sequence<T>.minOf(selector: (T) -> Double): Double {
val iterator = iterator() val iterator = iterator()
if (!iterator.hasNext()) throw NoSuchElementException() if (!iterator.hasNext()) throw NoSuchElementException()
var minValue = selector(iterator.next()) var minValue = selector(iterator.next())
if (minValue.isNaN()) return minValue
while (iterator.hasNext()) { while (iterator.hasNext()) {
val v = selector(iterator.next()) val v = selector(iterator.next())
if (v.isNaN()) return v minValue = minOf(minValue, v)
if (minValue > v) {
minValue = v
}
} }
return minValue return minValue
} }
@@ -1579,6 +1567,8 @@ public inline fun <T> Sequence<T>.minOf(selector: (T) -> Double): Double {
* Returns the smallest value among all values produced by [selector] function * Returns the smallest value among all values produced by [selector] function
* applied to each element in the sequence. * applied to each element in the sequence.
* *
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*
* @throws NoSuchElementException if the sequence is empty. * @throws NoSuchElementException if the sequence is empty.
* *
* The operation is _terminal_. * The operation is _terminal_.
@@ -1591,13 +1581,9 @@ public inline fun <T> Sequence<T>.minOf(selector: (T) -> Float): Float {
val iterator = iterator() val iterator = iterator()
if (!iterator.hasNext()) throw NoSuchElementException() if (!iterator.hasNext()) throw NoSuchElementException()
var minValue = selector(iterator.next()) var minValue = selector(iterator.next())
if (minValue.isNaN()) return minValue
while (iterator.hasNext()) { while (iterator.hasNext()) {
val v = selector(iterator.next()) val v = selector(iterator.next())
if (v.isNaN()) return v minValue = minOf(minValue, v)
if (minValue > v) {
minValue = v
}
} }
return minValue return minValue
} }
@@ -1620,7 +1606,6 @@ public inline fun <T, R : Comparable<R>> Sequence<T>.minOf(selector: (T) -> R):
var minValue = selector(iterator.next()) var minValue = selector(iterator.next())
while (iterator.hasNext()) { while (iterator.hasNext()) {
val v = selector(iterator.next()) val v = selector(iterator.next())
if (minValue > v) { if (minValue > v) {
minValue = v minValue = v
} }
@@ -1631,6 +1616,8 @@ public inline fun <T, R : Comparable<R>> Sequence<T>.minOf(selector: (T) -> R):
/** /**
* Returns the smallest value among all values produced by [selector] function * Returns the smallest value among all values produced by [selector] function
* applied to each element in the sequence or `null` if there are no elements. * applied to each element in the sequence or `null` if there are no elements.
*
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
* *
* The operation is _terminal_. * The operation is _terminal_.
*/ */
@@ -1642,13 +1629,9 @@ public inline fun <T> Sequence<T>.minOfOrNull(selector: (T) -> Double): Double?
val iterator = iterator() val iterator = iterator()
if (!iterator.hasNext()) return null if (!iterator.hasNext()) return null
var minValue = selector(iterator.next()) var minValue = selector(iterator.next())
if (minValue.isNaN()) return minValue
while (iterator.hasNext()) { while (iterator.hasNext()) {
val v = selector(iterator.next()) val v = selector(iterator.next())
if (v.isNaN()) return v minValue = minOf(minValue, v)
if (minValue > v) {
minValue = v
}
} }
return minValue return minValue
} }
@@ -1656,6 +1639,8 @@ public inline fun <T> Sequence<T>.minOfOrNull(selector: (T) -> Double): Double?
/** /**
* Returns the smallest value among all values produced by [selector] function * Returns the smallest value among all values produced by [selector] function
* applied to each element in the sequence or `null` if there are no elements. * applied to each element in the sequence or `null` if there are no elements.
*
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
* *
* The operation is _terminal_. * The operation is _terminal_.
*/ */
@@ -1667,13 +1652,9 @@ public inline fun <T> Sequence<T>.minOfOrNull(selector: (T) -> Float): Float? {
val iterator = iterator() val iterator = iterator()
if (!iterator.hasNext()) return null if (!iterator.hasNext()) return null
var minValue = selector(iterator.next()) var minValue = selector(iterator.next())
if (minValue.isNaN()) return minValue
while (iterator.hasNext()) { while (iterator.hasNext()) {
val v = selector(iterator.next()) val v = selector(iterator.next())
if (v.isNaN()) return v minValue = minOf(minValue, v)
if (minValue > v) {
minValue = v
}
} }
return minValue return minValue
} }
@@ -1694,7 +1675,6 @@ public inline fun <T, R : Comparable<R>> Sequence<T>.minOfOrNull(selector: (T) -
var minValue = selector(iterator.next()) var minValue = selector(iterator.next())
while (iterator.hasNext()) { while (iterator.hasNext()) {
val v = selector(iterator.next()) val v = selector(iterator.next())
if (minValue > v) { if (minValue > v) {
minValue = v minValue = v
} }
@@ -1127,6 +1127,8 @@ public inline fun <R : Comparable<R>> CharSequence.maxBy(selector: (Char) -> R):
* Returns the largest value among all values produced by [selector] function * Returns the largest value among all values produced by [selector] function
* applied to each character in the char sequence. * applied to each character in the char sequence.
* *
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*
* @throws NoSuchElementException if the char sequence is empty. * @throws NoSuchElementException if the char sequence is empty.
*/ */
@SinceKotlin("1.4") @SinceKotlin("1.4")
@@ -1136,15 +1138,9 @@ public inline fun <R : Comparable<R>> CharSequence.maxBy(selector: (Char) -> R):
public inline fun CharSequence.maxOf(selector: (Char) -> Double): Double { public inline fun CharSequence.maxOf(selector: (Char) -> Double): Double {
if (isEmpty()) throw NoSuchElementException() if (isEmpty()) throw NoSuchElementException()
var maxValue = selector(this[0]) var maxValue = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxValue
if (maxValue.isNaN()) return maxValue
for (i in 1..lastIndex) { for (i in 1..lastIndex) {
val v = selector(this[i]) val v = selector(this[i])
if (v.isNaN()) return v maxValue = maxOf(maxValue, v)
if (maxValue < v) {
maxValue = v
}
} }
return maxValue return maxValue
} }
@@ -1153,6 +1149,8 @@ public inline fun CharSequence.maxOf(selector: (Char) -> Double): Double {
* Returns the largest value among all values produced by [selector] function * Returns the largest value among all values produced by [selector] function
* applied to each character in the char sequence. * applied to each character in the char sequence.
* *
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*
* @throws NoSuchElementException if the char sequence is empty. * @throws NoSuchElementException if the char sequence is empty.
*/ */
@SinceKotlin("1.4") @SinceKotlin("1.4")
@@ -1162,15 +1160,9 @@ public inline fun CharSequence.maxOf(selector: (Char) -> Double): Double {
public inline fun CharSequence.maxOf(selector: (Char) -> Float): Float { public inline fun CharSequence.maxOf(selector: (Char) -> Float): Float {
if (isEmpty()) throw NoSuchElementException() if (isEmpty()) throw NoSuchElementException()
var maxValue = selector(this[0]) var maxValue = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxValue
if (maxValue.isNaN()) return maxValue
for (i in 1..lastIndex) { for (i in 1..lastIndex) {
val v = selector(this[i]) val v = selector(this[i])
if (v.isNaN()) return v maxValue = maxOf(maxValue, v)
if (maxValue < v) {
maxValue = v
}
} }
return maxValue return maxValue
} }
@@ -1188,12 +1180,8 @@ public inline fun CharSequence.maxOf(selector: (Char) -> Float): Float {
public inline fun <R : Comparable<R>> CharSequence.maxOf(selector: (Char) -> R): R { public inline fun <R : Comparable<R>> CharSequence.maxOf(selector: (Char) -> R): R {
if (isEmpty()) throw NoSuchElementException() if (isEmpty()) throw NoSuchElementException()
var maxValue = selector(this[0]) var maxValue = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxValue
for (i in 1..lastIndex) { for (i in 1..lastIndex) {
val v = selector(this[i]) val v = selector(this[i])
if (maxValue < v) { if (maxValue < v) {
maxValue = v maxValue = v
} }
@@ -1204,6 +1192,8 @@ public inline fun <R : Comparable<R>> CharSequence.maxOf(selector: (Char) -> R):
/** /**
* Returns the largest value among all values produced by [selector] function * Returns the largest value among all values produced by [selector] function
* applied to each character in the char sequence or `null` if there are no characters. * applied to each character in the char sequence or `null` if there are no characters.
*
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*/ */
@SinceKotlin("1.4") @SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class) @OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@@ -1212,15 +1202,9 @@ public inline fun <R : Comparable<R>> CharSequence.maxOf(selector: (Char) -> R):
public inline fun CharSequence.maxOfOrNull(selector: (Char) -> Double): Double? { public inline fun CharSequence.maxOfOrNull(selector: (Char) -> Double): Double? {
if (isEmpty()) return null if (isEmpty()) return null
var maxValue = selector(this[0]) var maxValue = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxValue
if (maxValue.isNaN()) return maxValue
for (i in 1..lastIndex) { for (i in 1..lastIndex) {
val v = selector(this[i]) val v = selector(this[i])
if (v.isNaN()) return v maxValue = maxOf(maxValue, v)
if (maxValue < v) {
maxValue = v
}
} }
return maxValue return maxValue
} }
@@ -1228,6 +1212,8 @@ public inline fun CharSequence.maxOfOrNull(selector: (Char) -> Double): Double?
/** /**
* Returns the largest value among all values produced by [selector] function * Returns the largest value among all values produced by [selector] function
* applied to each character in the char sequence or `null` if there are no characters. * applied to each character in the char sequence or `null` if there are no characters.
*
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*/ */
@SinceKotlin("1.4") @SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class) @OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@@ -1236,15 +1222,9 @@ public inline fun CharSequence.maxOfOrNull(selector: (Char) -> Double): Double?
public inline fun CharSequence.maxOfOrNull(selector: (Char) -> Float): Float? { public inline fun CharSequence.maxOfOrNull(selector: (Char) -> Float): Float? {
if (isEmpty()) return null if (isEmpty()) return null
var maxValue = selector(this[0]) var maxValue = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxValue
if (maxValue.isNaN()) return maxValue
for (i in 1..lastIndex) { for (i in 1..lastIndex) {
val v = selector(this[i]) val v = selector(this[i])
if (v.isNaN()) return v maxValue = maxOf(maxValue, v)
if (maxValue < v) {
maxValue = v
}
} }
return maxValue return maxValue
} }
@@ -1260,12 +1240,8 @@ public inline fun CharSequence.maxOfOrNull(selector: (Char) -> Float): Float? {
public inline fun <R : Comparable<R>> CharSequence.maxOfOrNull(selector: (Char) -> R): R? { public inline fun <R : Comparable<R>> CharSequence.maxOfOrNull(selector: (Char) -> R): R? {
if (isEmpty()) return null if (isEmpty()) return null
var maxValue = selector(this[0]) var maxValue = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxValue
for (i in 1..lastIndex) { for (i in 1..lastIndex) {
val v = selector(this[i]) val v = selector(this[i])
if (maxValue < v) { if (maxValue < v) {
maxValue = v maxValue = v
} }
@@ -1286,8 +1262,6 @@ public inline fun <R : Comparable<R>> CharSequence.maxOfOrNull(selector: (Char)
public inline fun <R> CharSequence.maxOfWith(comparator: Comparator<in R>, selector: (Char) -> R): R { public inline fun <R> CharSequence.maxOfWith(comparator: Comparator<in R>, selector: (Char) -> R): R {
if (isEmpty()) throw NoSuchElementException() if (isEmpty()) throw NoSuchElementException()
var maxValue = selector(this[0]) var maxValue = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxValue
for (i in 1..lastIndex) { for (i in 1..lastIndex) {
val v = selector(this[i]) val v = selector(this[i])
if (comparator.compare(maxValue, v) < 0) { if (comparator.compare(maxValue, v) < 0) {
@@ -1308,8 +1282,6 @@ public inline fun <R> CharSequence.maxOfWith(comparator: Comparator<in R>, selec
public inline fun <R> CharSequence.maxOfWithOrNull(comparator: Comparator<in R>, selector: (Char) -> R): R? { public inline fun <R> CharSequence.maxOfWithOrNull(comparator: Comparator<in R>, selector: (Char) -> R): R? {
if (isEmpty()) return null if (isEmpty()) return null
var maxValue = selector(this[0]) var maxValue = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return maxValue
for (i in 1..lastIndex) { for (i in 1..lastIndex) {
val v = selector(this[i]) val v = selector(this[i])
if (comparator.compare(maxValue, v) < 0) { if (comparator.compare(maxValue, v) < 0) {
@@ -1371,6 +1343,8 @@ public inline fun <R : Comparable<R>> CharSequence.minBy(selector: (Char) -> R):
* Returns the smallest value among all values produced by [selector] function * Returns the smallest value among all values produced by [selector] function
* applied to each character in the char sequence. * applied to each character in the char sequence.
* *
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*
* @throws NoSuchElementException if the char sequence is empty. * @throws NoSuchElementException if the char sequence is empty.
*/ */
@SinceKotlin("1.4") @SinceKotlin("1.4")
@@ -1380,15 +1354,9 @@ public inline fun <R : Comparable<R>> CharSequence.minBy(selector: (Char) -> R):
public inline fun CharSequence.minOf(selector: (Char) -> Double): Double { public inline fun CharSequence.minOf(selector: (Char) -> Double): Double {
if (isEmpty()) throw NoSuchElementException() if (isEmpty()) throw NoSuchElementException()
var minValue = selector(this[0]) var minValue = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return minValue
if (minValue.isNaN()) return minValue
for (i in 1..lastIndex) { for (i in 1..lastIndex) {
val v = selector(this[i]) val v = selector(this[i])
if (v.isNaN()) return v minValue = minOf(minValue, v)
if (minValue > v) {
minValue = v
}
} }
return minValue return minValue
} }
@@ -1397,6 +1365,8 @@ public inline fun CharSequence.minOf(selector: (Char) -> Double): Double {
* Returns the smallest value among all values produced by [selector] function * Returns the smallest value among all values produced by [selector] function
* applied to each character in the char sequence. * applied to each character in the char sequence.
* *
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*
* @throws NoSuchElementException if the char sequence is empty. * @throws NoSuchElementException if the char sequence is empty.
*/ */
@SinceKotlin("1.4") @SinceKotlin("1.4")
@@ -1406,15 +1376,9 @@ public inline fun CharSequence.minOf(selector: (Char) -> Double): Double {
public inline fun CharSequence.minOf(selector: (Char) -> Float): Float { public inline fun CharSequence.minOf(selector: (Char) -> Float): Float {
if (isEmpty()) throw NoSuchElementException() if (isEmpty()) throw NoSuchElementException()
var minValue = selector(this[0]) var minValue = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return minValue
if (minValue.isNaN()) return minValue
for (i in 1..lastIndex) { for (i in 1..lastIndex) {
val v = selector(this[i]) val v = selector(this[i])
if (v.isNaN()) return v minValue = minOf(minValue, v)
if (minValue > v) {
minValue = v
}
} }
return minValue return minValue
} }
@@ -1432,12 +1396,8 @@ public inline fun CharSequence.minOf(selector: (Char) -> Float): Float {
public inline fun <R : Comparable<R>> CharSequence.minOf(selector: (Char) -> R): R { public inline fun <R : Comparable<R>> CharSequence.minOf(selector: (Char) -> R): R {
if (isEmpty()) throw NoSuchElementException() if (isEmpty()) throw NoSuchElementException()
var minValue = selector(this[0]) var minValue = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return minValue
for (i in 1..lastIndex) { for (i in 1..lastIndex) {
val v = selector(this[i]) val v = selector(this[i])
if (minValue > v) { if (minValue > v) {
minValue = v minValue = v
} }
@@ -1448,6 +1408,8 @@ public inline fun <R : Comparable<R>> CharSequence.minOf(selector: (Char) -> R):
/** /**
* Returns the smallest value among all values produced by [selector] function * Returns the smallest value among all values produced by [selector] function
* applied to each character in the char sequence or `null` if there are no characters. * applied to each character in the char sequence or `null` if there are no characters.
*
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*/ */
@SinceKotlin("1.4") @SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class) @OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@@ -1456,15 +1418,9 @@ public inline fun <R : Comparable<R>> CharSequence.minOf(selector: (Char) -> R):
public inline fun CharSequence.minOfOrNull(selector: (Char) -> Double): Double? { public inline fun CharSequence.minOfOrNull(selector: (Char) -> Double): Double? {
if (isEmpty()) return null if (isEmpty()) return null
var minValue = selector(this[0]) var minValue = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return minValue
if (minValue.isNaN()) return minValue
for (i in 1..lastIndex) { for (i in 1..lastIndex) {
val v = selector(this[i]) val v = selector(this[i])
if (v.isNaN()) return v minValue = minOf(minValue, v)
if (minValue > v) {
minValue = v
}
} }
return minValue return minValue
} }
@@ -1472,6 +1428,8 @@ public inline fun CharSequence.minOfOrNull(selector: (Char) -> Double): Double?
/** /**
* Returns the smallest value among all values produced by [selector] function * Returns the smallest value among all values produced by [selector] function
* applied to each character in the char sequence or `null` if there are no characters. * applied to each character in the char sequence or `null` if there are no characters.
*
* If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
*/ */
@SinceKotlin("1.4") @SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class) @OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@@ -1480,15 +1438,9 @@ public inline fun CharSequence.minOfOrNull(selector: (Char) -> Double): Double?
public inline fun CharSequence.minOfOrNull(selector: (Char) -> Float): Float? { public inline fun CharSequence.minOfOrNull(selector: (Char) -> Float): Float? {
if (isEmpty()) return null if (isEmpty()) return null
var minValue = selector(this[0]) var minValue = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return minValue
if (minValue.isNaN()) return minValue
for (i in 1..lastIndex) { for (i in 1..lastIndex) {
val v = selector(this[i]) val v = selector(this[i])
if (v.isNaN()) return v minValue = minOf(minValue, v)
if (minValue > v) {
minValue = v
}
} }
return minValue return minValue
} }
@@ -1504,12 +1456,8 @@ public inline fun CharSequence.minOfOrNull(selector: (Char) -> Float): Float? {
public inline fun <R : Comparable<R>> CharSequence.minOfOrNull(selector: (Char) -> R): R? { public inline fun <R : Comparable<R>> CharSequence.minOfOrNull(selector: (Char) -> R): R? {
if (isEmpty()) return null if (isEmpty()) return null
var minValue = selector(this[0]) var minValue = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return minValue
for (i in 1..lastIndex) { for (i in 1..lastIndex) {
val v = selector(this[i]) val v = selector(this[i])
if (minValue > v) { if (minValue > v) {
minValue = v minValue = v
} }
@@ -1530,8 +1478,6 @@ public inline fun <R : Comparable<R>> CharSequence.minOfOrNull(selector: (Char)
public inline fun <R> CharSequence.minOfWith(comparator: Comparator<in R>, selector: (Char) -> R): R { public inline fun <R> CharSequence.minOfWith(comparator: Comparator<in R>, selector: (Char) -> R): R {
if (isEmpty()) throw NoSuchElementException() if (isEmpty()) throw NoSuchElementException()
var minValue = selector(this[0]) var minValue = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return minValue
for (i in 1..lastIndex) { for (i in 1..lastIndex) {
val v = selector(this[i]) val v = selector(this[i])
if (comparator.compare(minValue, v) > 0) { if (comparator.compare(minValue, v) > 0) {
@@ -1552,8 +1498,6 @@ public inline fun <R> CharSequence.minOfWith(comparator: Comparator<in R>, selec
public inline fun <R> CharSequence.minOfWithOrNull(comparator: Comparator<in R>, selector: (Char) -> R): R? { public inline fun <R> CharSequence.minOfWithOrNull(comparator: Comparator<in R>, selector: (Char) -> R): R? {
if (isEmpty()) return null if (isEmpty()) return null
var minValue = selector(this[0]) var minValue = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return minValue
for (i in 1..lastIndex) { for (i in 1..lastIndex) {
val v = selector(this[i]) val v = selector(this[i])
if (comparator.compare(minValue, v) > 0) { if (comparator.compare(minValue, v) > 0) {
File diff suppressed because it is too large Load Diff
@@ -5,6 +5,8 @@
package test.collections package test.collections
import test.assertIsNegativeZero
import test.assertIsPositiveZero
import test.assertStaticAndRuntimeTypeIs import test.assertStaticAndRuntimeTypeIs
import kotlin.test.* import kotlin.test.*
import test.collections.behaviors.* import test.collections.behaviors.*
@@ -909,6 +911,9 @@ class CollectionTest {
assertEquals(Double.NaN, listOf(1, -1, 0).minOf { it.toDouble().pow(0.5) }) assertEquals(Double.NaN, listOf(1, -1, 0).minOf { it.toDouble().pow(0.5) })
assertEquals(Float.NaN, listOf(1, -1, 0).minOf { it.toFloat().pow(0.5F) }) assertEquals(Float.NaN, listOf(1, -1, 0).minOf { it.toFloat().pow(0.5F) })
assertIsNegativeZero(listOf(1.0, -1.0).shuffled().minOf { it * 0.0 })
assertIsNegativeZero(listOf(1.0F, -1.0F).shuffled().minOf { it * 0.0F }.toDouble())
} }
@Test fun minOfWith() { @Test fun minOfWith() {
@@ -934,6 +939,9 @@ class CollectionTest {
assertEquals(16.0, listOf(1, 2, 3, 4, 5).maxOf { (-2.0).pow(it) }) assertEquals(16.0, listOf(1, 2, 3, 4, 5).maxOf { (-2.0).pow(it) })
assertEquals(16.0F, listOf(1, 2, 3, 4, 5).maxOf { (-2.0F).pow(it) }) assertEquals(16.0F, listOf(1, 2, 3, 4, 5).maxOf { (-2.0F).pow(it) })
assertIsPositiveZero(listOf(1.0, -1.0).shuffled().maxOf { it * 0.0 })
assertIsPositiveZero(listOf(1.0F, -1.0F).shuffled().maxOf { it * 0.0F }.toDouble())
} }
@Test fun maxOfWith() { @Test fun maxOfWith() {
@@ -946,9 +954,6 @@ class CollectionTest {
assertEquals(null, emptyList<Int>().maxOfWithOrNull(naturalOrder()) { it }) assertEquals(null, emptyList<Int>().maxOfWithOrNull(naturalOrder()) { it })
// TODO: investigate why no unit-coercion happens here and an explicit 'Unit' is required // TODO: investigate why no unit-coercion happens here and an explicit 'Unit' is required
assertFailsWith<NoSuchElementException> { emptyList<Int>().maxOfWith(naturalOrder()) { it }; Unit } assertFailsWith<NoSuchElementException> { emptyList<Int>().maxOfWith(naturalOrder()) { it }; Unit }
assertEquals(Double.NaN, listOf(1, -1, 0).maxOf { it.toDouble().pow(0.5) })
assertEquals(Float.NaN, listOf(1, -1, 0).maxOf { it.toFloat().pow(0.5F) })
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/ */
@@ -100,6 +100,24 @@ class NaNPropagationTest {
) )
} }
@Test
fun arrayMinOf() {
propagateOf3(
{ a, b, c -> arrayOf(a, b, c).minOf { it } },
{ a, b, c -> arrayOf(a, b, c).minOf { it } },
"arrayOf().minOf()"
)
}
@Test
fun arrayMaxOf() {
propagateOf3(
{ a, b, c -> arrayOf(a, b, c).maxOf { it } },
{ a, b, c -> arrayOf(a, b, c).maxOf { it } },
"arrayOf().maxOf()"
)
}
@Test @Test
fun primitiveArrayMin() { fun primitiveArrayMin() {
propagateOf2( propagateOf2(
@@ -118,6 +136,34 @@ class NaNPropagationTest {
) )
} }
@Test
fun primitiveArrayMinOf() {
propagateOf3(
{ a, b, c -> doubleArrayOf(a, b, c).minOf { it } },
{ a, b, c -> floatArrayOf(a, b, c).minOf { it } },
"primitiveArrayOf().minOf()"
)
propagateOf3(
{ a, b, c -> val arr = doubleArrayOf(a, b, c); intArrayOf(0, 1, 2).minOf { arr[it] } },
{ a, b, c -> val arr = floatArrayOf(a, b, c); intArrayOf(0, 1, 2).minOf { arr[it] } },
"intArrayOf().minOf()"
)
}
@Test
fun primitiveArrayMaxOf() {
propagateOf3(
{ a, b, c -> doubleArrayOf(a, b, c).maxOf { it } },
{ a, b, c -> floatArrayOf(a, b, c).maxOf { it } },
"primitiveArrayOf().maxOf()"
)
propagateOf3(
{ a, b, c -> val arr = doubleArrayOf(a, b, c); intArrayOf(0, 1, 2).maxOf { arr[it] } },
{ a, b, c -> val arr = floatArrayOf(a, b, c); intArrayOf(0, 1, 2).maxOf { arr[it] } },
"intArrayOf().maxOf()"
)
}
@Test @Test
fun listMin() { fun listMin() {
propagateOf2( propagateOf2(
@@ -136,6 +182,24 @@ class NaNPropagationTest {
) )
} }
@Test
fun listMinOf() {
propagateOf3(
{ a, b, c -> listOf(a, b, c).minOf { it } },
{ a, b, c -> listOf(a, b, c).minOf { it } },
"listOf().minOf()"
)
}
@Test
fun listMaxOf() {
propagateOf3(
{ a, b, c -> listOf(a, b, c).maxOf { it } },
{ a, b, c -> listOf(a, b, c).maxOf { it } },
"listOf().maxOf()"
)
}
@Test @Test
fun sequenceMin() { fun sequenceMin() {
propagateOf2( propagateOf2(
@@ -154,6 +218,23 @@ class NaNPropagationTest {
) )
} }
@Test
fun sequenceMinOf() {
propagateOf3(
{ a, b, c -> sequenceOf(a, b, c).minOf { it } },
{ a, b, c -> sequenceOf(a, b, c).minOf { it } },
"sequenceOf().minOf()"
)
}
@Test
fun sequenceMaxOf() {
propagateOf3(
{ a, b, c -> sequenceOf(a, b, c).maxOf { it } },
{ a, b, c -> sequenceOf(a, b, c).maxOf { it } },
"sequenceOf().maxOf()"
)
}
} }
class NaNTotalOrderTest { class NaNTotalOrderTest {
+13 -2
View File
@@ -1,10 +1,12 @@
/* /*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/ */
package test package test
import kotlin.math.withSign
import kotlin.test.assertEquals
import kotlin.test.assertTrue import kotlin.test.assertTrue
import kotlin.test.fail import kotlin.test.fail
@@ -27,4 +29,13 @@ fun assertArrayContentEquals(expected: CharArray, actual: CharArray, message: St
fun assertArrayContentEquals(expected: UIntArray, actual: UIntArray, message: String? = null) = assertTrue(expected contentEquals actual, message) fun assertArrayContentEquals(expected: UIntArray, actual: UIntArray, message: String? = null) = assertTrue(expected contentEquals actual, message)
fun assertArrayContentEquals(expected: ULongArray, actual: ULongArray, message: String? = null) = assertTrue(expected contentEquals actual, message) fun assertArrayContentEquals(expected: ULongArray, actual: ULongArray, message: String? = null) = assertTrue(expected contentEquals actual, message)
fun assertArrayContentEquals(expected: UShortArray, actual: UShortArray, message: String? = null) = assertTrue(expected contentEquals actual, message) fun assertArrayContentEquals(expected: UShortArray, actual: UShortArray, message: String? = null) = assertTrue(expected contentEquals actual, message)
fun assertArrayContentEquals(expected: UByteArray, actual: UByteArray, message: String? = null) = assertTrue(expected contentEquals actual, message) fun assertArrayContentEquals(expected: UByteArray, actual: UByteArray, message: String? = null) = assertTrue(expected contentEquals actual, message)
fun assertIsNegativeZero(value: Double) {
assertEquals(-0.0, value)
assertEquals(-1.0, 1.0.withSign(value))
}
fun assertIsPositiveZero(value: Double) {
assertEquals(0.0, value)
assertEquals(1.0, 1.0.withSign(value))
}
@@ -562,9 +562,9 @@ object Aggregates : TemplateGroupBase() {
Returns the ${if (op == "max") "largest" else "smallest"} value among all values produced by [selector] function Returns the ${if (op == "max") "largest" else "smallest"} value among all values produced by [selector] function
applied to each ${f.element} in the ${f.collection}${" or `null` if there are no ${f.element.pluralize()}".ifOrEmpty(nullable)}. applied to each ${f.element} in the ${f.collection}${" or `null` if there are no ${f.element.pluralize()}".ifOrEmpty(nullable)}.
""" + """ +
// """ """
// If any of values produced by [selector] function is `NaN`, the returned result is `NaN`. If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
// """.ifOrEmpty(isFloat) + """.ifOrEmpty(isFloat) +
""" """
@throws NoSuchElementException if the ${f.collection} is empty. @throws NoSuchElementException if the ${f.collection} is empty.
""".ifOrEmpty(!nullable) """.ifOrEmpty(!nullable)
@@ -574,19 +574,20 @@ object Aggregates : TemplateGroupBase() {
returns(selectorType + "?".ifOrEmpty(nullable)) returns(selectorType + "?".ifOrEmpty(nullable))
val doOnEmpty = if (nullable) "return null" else "throw NoSuchElementException()" val doOnEmpty = if (nullable) "return null" else "throw NoSuchElementException()"
val acc = op + "Value" val acc = op + "Value"
val cmp = if (op == "max") "<" else ">" val cmpBlock = if (isFloat)
"""$acc = ${op}Of($acc, v)"""
else
"""if ($acc ${if (op == "max") "<" else ">"} v) {
$acc = v
}"""
body { body {
""" """
val iterator = iterator() val iterator = iterator()
if (!iterator.hasNext()) $doOnEmpty if (!iterator.hasNext()) $doOnEmpty
var $acc = selector(iterator.next()) var $acc = selector(iterator.next())
${"if ($acc.isNaN()) return $acc".ifOrEmpty(isFloat)}
while (iterator.hasNext()) { while (iterator.hasNext()) {
val v = selector(iterator.next()) val v = selector(iterator.next())
${"if (v.isNaN()) return v".ifOrEmpty(isFloat)} $cmpBlock
if ($acc $cmp v) {
$acc = v
}
} }
return $acc return $acc
""" """
@@ -596,15 +597,9 @@ object Aggregates : TemplateGroupBase() {
if (isEmpty()) $doOnEmpty if (isEmpty()) $doOnEmpty
var $acc = selector(this[0]) var $acc = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return $acc
${"if ($acc.isNaN()) return $acc".ifOrEmpty(isFloat)}
for (i in 1..lastIndex) { for (i in 1..lastIndex) {
val v = selector(this[i]) val v = selector(this[i])
${"if (v.isNaN()) return v".ifOrEmpty(isFloat)} $cmpBlock
if ($acc $cmp v) {
$acc = v
}
} }
return $acc return $acc
""" """
@@ -668,8 +663,6 @@ object Aggregates : TemplateGroupBase() {
if (isEmpty()) $doOnEmpty if (isEmpty()) $doOnEmpty
var $acc = selector(this[0]) var $acc = selector(this[0])
val lastIndex = this.lastIndex
if (lastIndex == 0) return $acc
for (i in 1..lastIndex) { for (i in 1..lastIndex) {
val v = selector(this[i]) val v = selector(this[i])
if (comparator.compare($acc, v) $cmp 0) { if (comparator.compare($acc, v) $cmp 0) {