Floor division and remainder for numeric types KT-26234
- floorDiv/mod for unsigned types - floorDiv/mod for signed types - mod for floating point types - mod return type: same as divisor for integer types - Update JS API dump - Avoid triggering division overflow in tests due to K/N - Workaround failing test in JS-legacy
This commit is contained in:
@@ -1,15 +1,12 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Copyright 2010-2021 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.
|
||||
*/
|
||||
|
||||
package test.unsigned
|
||||
|
||||
import kotlin.math.nextDown
|
||||
import kotlin.math.nextUp
|
||||
import kotlin.math.pow
|
||||
import kotlin.math.sign
|
||||
import kotlin.random.Random
|
||||
import kotlin.math.*
|
||||
import kotlin.random.*
|
||||
import kotlin.test.*
|
||||
|
||||
class UIntTest {
|
||||
@@ -69,6 +66,42 @@ class UIntTest {
|
||||
testToString("4294967295", UInt.MAX_VALUE)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun operations() {
|
||||
assertEquals(2_147_483_648u, Int.MAX_VALUE.toUInt() + identity(1u))
|
||||
assertEquals(11u, UInt.MAX_VALUE + identity(12u))
|
||||
assertEquals(UInt.MAX_VALUE - 99u, 45u - identity(145u))
|
||||
|
||||
assertEquals(UInt.MAX_VALUE - 1u, Int.MAX_VALUE.toUInt() * identity(2u))
|
||||
assertEquals(2_147_483_645u, Int.MAX_VALUE.toUInt() * identity(3u))
|
||||
|
||||
testMulDivRem(125u, 3u, 41u, 2u)
|
||||
testMulDivRem(210u, 5u, 42u, 0u)
|
||||
testMulDivRem(UInt.MAX_VALUE, 256u, 16777215u, 255u)
|
||||
testMulDivRem(UInt.MAX_VALUE - 1u, UInt.MAX_VALUE, 0u, UInt.MAX_VALUE - 1u)
|
||||
testMulDivRem(UInt.MAX_VALUE, UInt.MAX_VALUE - 1u, 1u, 1u)
|
||||
testMulDivRem(UInt.MAX_VALUE, Int.MAX_VALUE.toUInt(), 2u, 1u)
|
||||
}
|
||||
|
||||
|
||||
private fun testMulDivRem(number: UInt, divisor: UInt, div: UInt, rem: UInt) {
|
||||
assertEquals(div, number / divisor)
|
||||
assertEquals(rem, number % divisor)
|
||||
assertEquals(div, number.floorDiv(divisor))
|
||||
assertEquals(rem, number.mod(divisor))
|
||||
|
||||
assertEquals(number, div * divisor + rem)
|
||||
assertTrue(rem < divisor)
|
||||
assertTrue(div < number)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun divRem() = repeat(1000) {
|
||||
val number = Random.nextUInt()
|
||||
val divisor = Random.nextUInt(until = UInt.MAX_VALUE) + 1u
|
||||
testMulDivRem(number, divisor, number / divisor, number % divisor)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun comparisons() {
|
||||
fun <T> compare(op1: Comparable<T>, op2: T) = op1.compareTo(op2)
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Copyright 2010-2021 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.
|
||||
*/
|
||||
|
||||
package test.unsigned
|
||||
|
||||
import kotlin.math.*
|
||||
import kotlin.random.Random
|
||||
import kotlin.random.*
|
||||
import kotlin.test.*
|
||||
|
||||
class ULongTest {
|
||||
@@ -66,6 +66,42 @@ class ULongTest {
|
||||
testToString("18446744073709551615", ULong.MAX_VALUE)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun operations() {
|
||||
assertEquals(9223372036854775808u, Long.MAX_VALUE.toULong() + identity(1u))
|
||||
assertEquals(11u, ULong.MAX_VALUE + identity(12u))
|
||||
assertEquals(ULong.MAX_VALUE - 99u, 45u - identity(145u))
|
||||
|
||||
assertEquals(ULong.MAX_VALUE - 1u, Long.MAX_VALUE.toULong() * identity(2u))
|
||||
assertEquals(9223372036854775805u, Long.MAX_VALUE.toULong() * identity(3u))
|
||||
|
||||
testMulDivRem(125u, 3u, 41u, 2u)
|
||||
testMulDivRem(210u, 5u, 42u, 0u)
|
||||
testMulDivRem(ULong.MAX_VALUE, 65536uL * 65536u, 4294967295u, 4294967295u)
|
||||
testMulDivRem(ULong.MAX_VALUE - 1u, ULong.MAX_VALUE, 0u, ULong.MAX_VALUE - 1u)
|
||||
testMulDivRem(ULong.MAX_VALUE, ULong.MAX_VALUE - 1u, 1u, 1u)
|
||||
testMulDivRem(ULong.MAX_VALUE, Long.MAX_VALUE.toULong(), 2u, 1u)
|
||||
}
|
||||
|
||||
|
||||
private fun testMulDivRem(number: ULong, divisor: ULong, div: ULong, rem: ULong) {
|
||||
assertEquals(div, number / divisor)
|
||||
assertEquals(rem, number % divisor)
|
||||
assertEquals(div, number.floorDiv(divisor))
|
||||
assertEquals(rem, number.mod(divisor))
|
||||
|
||||
assertEquals(number, div * divisor + rem)
|
||||
assertTrue(rem < divisor)
|
||||
assertTrue(div < number)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun divRem() = repeat(1000) {
|
||||
val number = Random.nextULong()
|
||||
val divisor = Random.nextULong(until = ULong.MAX_VALUE) + 1u
|
||||
testMulDivRem(number, divisor, number / divisor, number % divisor)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun comparisons() {
|
||||
fun <T> compare(op1: Comparable<T>, op2: T) = op1.compareTo(op2)
|
||||
@@ -150,7 +186,7 @@ class ULongTest {
|
||||
assertTrue(diff <= actual.ulp, "$actual should be within one ulp (${actual.ulp}) from the expected $expected")
|
||||
}
|
||||
|
||||
fun testRounding(from: ULong, count: UInt) {
|
||||
fun testRounding(from: ULong, count: ULong) {
|
||||
for (x in from..(from + count)) {
|
||||
val double = x.toDouble()
|
||||
val v = double.toULong()
|
||||
|
||||
Reference in New Issue
Block a user