1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
/*
* Copyright 2010-2018 JetBrains s.r.o. 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.ranges
import kotlin.test.*
class CoercionTest {
@Test
fun coercionsInt() {
expect(5) { 5.coerceAtLeast(1) }
expect(5) { 1.coerceAtLeast(5) }
expect(1) { 5.coerceAtMost(1) }
expect(1) { 1.coerceAtMost(5) }
for (value in 0..10) {
expect(value) { value.coerceIn(null, null) }
val min = 2
val max = 5
val range = min..max
expect(value.coerceAtLeast(min)) { value.coerceIn(min, null) }
expect(value.coerceAtMost(max)) { value.coerceIn(null, max) }
expect(value.coerceAtLeast(min).coerceAtMost(max)) { value.coerceIn(min, max) }
expect(value.coerceAtMost(max).coerceAtLeast(min)) { value.coerceIn(range) }
assertTrue((value.coerceIn(range)) in range)
}
assertFails { 1.coerceIn(1, 0) }
assertFails { 1.coerceIn(1..0) }
}
@Test
fun coercionsLong() {
expect(5L) { 5L.coerceAtLeast(1L) }
expect(5L) { 1L.coerceAtLeast(5L) }
expect(1L) { 5L.coerceAtMost(1L) }
expect(1L) { 1L.coerceAtMost(5L) }
for (value in 0L..10L) {
expect(value) { value.coerceIn(null, null) }
val min = 2L
val max = 5L
val range = min..max
expect(value.coerceAtLeast(min)) { value.coerceIn(min, null) }
expect(value.coerceAtMost(max)) { value.coerceIn(null, max) }
expect(value.coerceAtLeast(min).coerceAtMost(max)) { value.coerceIn(min, max) }
expect(value.coerceAtMost(max).coerceAtLeast(min)) { value.coerceIn(range) }
assertTrue((value.coerceIn(range)) in range)
}
assertFails { 1L.coerceIn(1L, 0L) }
assertFails { 1L.coerceIn(1L..0L) }
}
@Test
fun coercionsDouble() {
expect(5.0) { 5.0.coerceAtLeast(1.0) }
expect(5.0) { 1.0.coerceAtLeast(5.0) }
assertTrue { Double.NaN.coerceAtLeast(1.0).isNaN() }
expect(1.0) { 5.0.coerceAtMost(1.0) }
expect(1.0) { 1.0.coerceAtMost(5.0) }
assertTrue { Double.NaN.coerceAtMost(5.0).isNaN() }
for (value in (0..10).map { it.toDouble() }) {
expect(value) { value.coerceIn(null, null) }
val min = 2.0
val max = 5.0
val range = min..max
expect(value.coerceAtLeast(min)) { value.coerceIn(min, null) }
expect(value.coerceAtMost(max)) { value.coerceIn(null, max) }
expect(value.coerceAtLeast(min).coerceAtMost(max)) { value.coerceIn(min, max) }
expect(value.coerceAtMost(max).coerceAtLeast(min)) { value.coerceIn(range) }
assertTrue((value.coerceIn(range)) in range)
}
assertFails { 1.0.coerceIn(1.0, 0.0) }
assertFails { 1.0.coerceIn(1.0..0.0) }
assertTrue(0.0.equals(0.0.coerceIn(0.0, -0.0)))
assertTrue((-0.0).equals((-0.0).coerceIn(0.0..-0.0)))
assertTrue(Double.NaN.coerceIn(0.0, 1.0).isNaN())
assertTrue(Double.NaN.coerceIn(0.0..1.0).isNaN())
}
@Test
fun coercionsComparable() {
val v = (0..10).map { ComparableNumber(it) }
expect(5) { v[5].coerceAtLeast(v[1]).value }
expect(5) { v[1].coerceAtLeast(v[5]).value }
expect(v[5]) { v[5].coerceAtLeast(ComparableNumber(5)) }
expect(1) { v[5].coerceAtMost(v[1]).value }
expect(1) { v[1].coerceAtMost(v[5]).value }
expect(v[1]) { v[1].coerceAtMost(ComparableNumber(1)) }
for (value in v) {
expect(value) { value.coerceIn(null, null) }
val min = v[2]
val max = v[5]
val range = min..max
expect(value.coerceAtLeast(min)) { value.coerceIn(min, null) }
expect(value.coerceAtMost(max)) { value.coerceIn(null, max) }
expect(value.coerceAtLeast(min).coerceAtMost(max)) { value.coerceIn(min, max) }
expect(value.coerceAtMost(max).coerceAtLeast(min)) { value.coerceIn(range) }
assertTrue((value.coerceIn(range)) in range)
}
assertFails { v[1].coerceIn(v[1], v[0]) }
assertFails { v[1].coerceIn(v[1]..v[0]) }
}
}
private class ComparableNumber(val value: Int) : Comparable<ComparableNumber> {
override fun compareTo(other: ComparableNumber): Int = this.value - other.value
override fun toString(): String = "CV$value"
}
|