Random extensions for unsigned: fixes after review

#KT-25570
This commit is contained in:
Ilya Gorbunov
2018-08-23 17:18:19 +03:00
parent f9dec5e794
commit d035c2690a
2 changed files with 54 additions and 38 deletions
+40 -25
View File
@@ -324,29 +324,32 @@ public fun Random(seed: Long): Random = XorWowRandom(seed.toInt(), seed.shr(32).
/** /**
* Gets the next random [UInt] from the random number generator. * Gets the next random [UInt] from the random number generator.
* *
* Generates a Unsigned Int random value uniformly distributed between [UInt.MIN_VALUE] and [UInt.MAX_VALUE] (inclusive) * Generates a [UInt] random value uniformly distributed between [UInt.MIN_VALUE] and [UInt.MAX_VALUE] (inclusive).
*/ */
@SinceKotlin("1.3") @SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun Random.nextUInt(): UInt = nextInt().toUInt() public fun Random.nextUInt(): UInt = nextInt().toUInt()
/** /**
* Gets the next random [UInt] from the random number generator not greater than the specified [bound]. * Gets the next random [UInt] from the random number generator not greater than the specified [bound].
* *
* Generated an Unsigned Int random value uniformly distributed between `0`(inclusive) and the specified [bound] (exclusive). * Generates a [UInt] random value uniformly distributed between `0` (inclusive) and the specified [bound] (exclusive).
* *
* @throws IllegalArgumentException if [bound] is zero * @throws IllegalArgumentException if [bound] is zero.
*/ */
@SinceKotlin("1.3") @SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun Random.nextUInt(bound: UInt): UInt = nextUInt(0u, bound) public fun Random.nextUInt(bound: UInt): UInt = nextUInt(0u, bound)
/** /**
* Gets the next random [UInt] from the random number generator in the specified range. * Gets the next random [UInt] from the random number generator in the specified range.
* *
* Generated an Unsigned Integer random value uniformly distributed between the specified [origin] (inclusive) and the specified [bound] (exclusive). * Generates a [UInt] random value uniformly distributed between the specified [origin] (inclusive) and the specified [bound] (exclusive).
* *
* @throws IllegalArgumentException if [origin] is greater than or equal to [bound] * @throws IllegalArgumentException if [origin] is greater than or equal to [bound].
*/ */
@SinceKotlin("1.3") @SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun Random.nextUInt(origin: UInt, bound: UInt): UInt { public fun Random.nextUInt(origin: UInt, bound: UInt): UInt {
checkUIntRangeBounds(origin, bound) checkUIntRangeBounds(origin, bound)
@@ -361,45 +364,49 @@ public fun Random.nextUInt(origin: UInt, bound: UInt): UInt {
/** /**
* Gets the next random [UInt] from the random number generator in the specified [range]. * Gets the next random [UInt] from the random number generator in the specified [range].
* *
* Generates an Unsigned Integer random value uniformly distributed in the specified [range]: * Generates a [UInt] random value uniformly distributed in the specified [range]:
* from `range.start` inclusive to `range.endInclusive` inclusive. * from `range.start` inclusive to `range.endInclusive` inclusive.
* *
* @throws IllegalArgumentException if [range] is empty. * @throws IllegalArgumentException if [range] is empty.
*/ */
@SinceKotlin("1.3") @SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun Random.nextUInt(range: UIntRange): UInt = when { public fun Random.nextUInt(range: UIntRange): UInt = when {
range.isEmpty() -> throw IllegalArgumentException("Cannot get random in empty range: $range") range.isEmpty() -> throw IllegalArgumentException("Cannot get random in empty range: $range")
range.last < UInt.MAX_VALUE -> nextUInt(range.first, range.last + 1.toUInt()) range.last < UInt.MAX_VALUE -> nextUInt(range.first, range.last + 1u)
range.first > UInt.MIN_VALUE -> nextUInt(range.first, range.last + 1.toUInt()) range.first > UInt.MIN_VALUE -> nextUInt(range.first, range.last + 1u)
else -> nextUInt() else -> nextUInt()
} }
/** /**
* Gets the next random [ULong] from the random number generator. * Gets the next random [ULong] from the random number generator.
* *
* Generated a Unsigned Long random value uniformly distributed between [ULong.MIN_VALUE] and [ULong.MAX_VALUE] (inclusive) * Generates a [ULong] random value uniformly distributed between [ULong.MIN_VALUE] and [ULong.MAX_VALUE] (inclusive).
*/ */
@SinceKotlin("1.3") @SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun Random.nextULong(): ULong = nextLong().toULong() public fun Random.nextULong(): ULong = nextLong().toULong()
/** /**
* Gets the next random [ULong] from the random number generator not greater than the specified [bound]. * Gets the next random [ULong] from the random number generator not greater than the specified [bound].
* *
* Generated an Unsigned Long random value uniformly distributed between `0`(inclusive) and the specified [bound] (exclusive). * Generates a [ULong] random value uniformly distributed between `0` (inclusive) and the specified [bound] (exclusive).
* *
* @throws IllegalArgumentException if [bound] is zero * @throws IllegalArgumentException if [bound] is zero.
*/ */
@SinceKotlin("1.3") @SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun Random.nextULong(bound: ULong): ULong = nextULong(0uL, bound) public fun Random.nextULong(bound: ULong): ULong = nextULong(0uL, bound)
/** /**
* Gets the next random [ULong] from the random number generator in the specified range * Gets the next random [ULong] from the random number generator in the specified range.
* *
* Generated an Unsigned Long random value uniformly distributed between the specified [origin] (inclusive) and the specified [bound] (exclusive). * Generates a [ULong] random value uniformly distributed between the specified [origin] (inclusive) and the specified [bound] (exclusive).
* *
* @throws IllegalArgumentException if [origin] is greater than or equal to [bound] * @throws IllegalArgumentException if [origin] is greater than or equal to [bound].
*/ */
@SinceKotlin("1.3") @SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun Random.nextULong(origin: ULong, bound: ULong): ULong { public fun Random.nextULong(origin: ULong, bound: ULong): ULong {
checkULongRangeBounds(origin, bound) checkULongRangeBounds(origin, bound)
@@ -413,41 +420,49 @@ public fun Random.nextULong(origin: ULong, bound: ULong): ULong {
/** /**
* Gets the next random [ULong] from the random number generator in the specified [range]. * Gets the next random [ULong] from the random number generator in the specified [range].
* *
* Generates an Unsigned Long random value uniformly distributed in the specified [range]: * Generates a [ULong] random value uniformly distributed in the specified [range]:
* from `range.start` inclusive to `range.endInclusive` inclusive. * from `range.start` inclusive to `range.endInclusive` inclusive.
* *
* @throws IllegalArgumentException if [range] is empty * @throws IllegalArgumentException if [range] is empty.
*/ */
@SinceKotlin("1.3") @SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun Random.nextULong(range: ULongRange): ULong = when { public fun Random.nextULong(range: ULongRange): ULong = when {
range.isEmpty() -> throw IllegalArgumentException("Cannot get random in empty range: $range") range.isEmpty() -> throw IllegalArgumentException("Cannot get random in empty range: $range")
range.last < ULong.MAX_VALUE -> nextULong(range.first, range.last + 1.toULong()) range.last < ULong.MAX_VALUE -> nextULong(range.first, range.last + 1u)
range.first > ULong.MIN_VALUE -> nextULong(range.first, range.last + 1.toULong()) range.first > ULong.MIN_VALUE -> nextULong(range.first, range.last + 1u)
else -> nextULong() else -> nextULong()
} }
/** /**
* Fills the specified byte [array] with random bytes and returns it. * Fills the specified unsigned byte [array] with random bytes and returns it.
* *
* @return [array] filled with random bytes. * @return [array] filled with random bytes.
*/ */
@SinceKotlin("1.3") @SinceKotlin("1.3")
public fun Random.nextUBytes(array: UByteArray) = nextUBytes(array, 0, array.size) @ExperimentalUnsignedTypes
public fun Random.nextUBytes(array: UByteArray): UByteArray {
nextBytes(array.asByteArray())
return array
}
/** /**
* Creates a byte array of the specified [size], filled with random bytes. * Creates an unsigned byte array of the specified [size], filled with random bytes.
*/ */
@SinceKotlin("1.3") @SinceKotlin("1.3")
public fun Random.nextUBytes(size: Int): UByteArray = UByteArray(nextBytes(size)) @ExperimentalUnsignedTypes
public fun Random.nextUBytes(size: Int): UByteArray = nextBytes(size).asUByteArray()
/** /**
* Fills a subrange of the specified UByte [array] starting from [fromIndex] inclusive and ending [toIndex] exclusive with random UBytes * Fills a subrange of the specified `UByte` [array] starting from [fromIndex] inclusive and ending [toIndex] exclusive with random UBytes.
* *
* @returns [array] with the subrange filled with random bytes. * @return [array] with the subrange filled with random bytes.
*/ */
@SinceKotlin("1.3") @SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun Random.nextUBytes(array: UByteArray, fromIndex: Int = 0, toIndex: Int = array.size): UByteArray { public fun Random.nextUBytes(array: UByteArray, fromIndex: Int = 0, toIndex: Int = array.size): UByteArray {
return UByteArray(nextBytes(array.storage, fromIndex, toIndex)) nextBytes(array.asByteArray(), fromIndex, toIndex)
return array
} }
internal expect fun defaultPlatformRandom(): Random internal expect fun defaultPlatformRandom(): Random
+14 -13
View File
@@ -51,7 +51,7 @@ abstract class RandomSmokeTest {
@Test @Test
fun nextUInt() { fun nextUInt() {
var result1 = UInt.MIN_VALUE var result1 = 0u
var result2 = UInt.MAX_VALUE var result2 = UInt.MAX_VALUE
repeat(1000) { repeat(1000) {
val r = subject.nextUInt() val r = subject.nextUInt()
@@ -59,7 +59,7 @@ abstract class RandomSmokeTest {
result2 = result2 and r result2 = result2 and r
} }
assertEquals(UInt.MAX_VALUE, result1, "All one bits should be present") assertEquals(UInt.MAX_VALUE, result1, "All one bits should be present")
assertEquals(UInt.MIN_VALUE, result2, "All zero bits should present") assertEquals(0u, result2, "All zero bits should present")
} }
@@ -93,7 +93,7 @@ abstract class RandomSmokeTest {
for (bound in listOf(2u, 3u, 7u, 16u, 32u, 0x4000_0000u, UInt.MAX_VALUE)) { for (bound in listOf(2u, 3u, 7u, 16u, 32u, 0x4000_0000u, UInt.MAX_VALUE)) {
repeat(1000) { repeat(1000) {
val x = subject.nextUInt(bound) val x = subject.nextUInt(bound)
if (x !in 0u..(bound - 1u)) if (x !in 0u until bound)
fail("Value $x must be in range [0, $bound)") fail("Value $x must be in range [0, $bound)")
} }
} }
@@ -137,7 +137,7 @@ abstract class RandomSmokeTest {
for ((origin, bound) in listOf((0u to 2u), (1u to 6u), (0u to 32u), (0u to UInt.MAX_VALUE), (1u to (Int.MAX_VALUE.toUInt() + 1u)), (UInt.MIN_VALUE to UInt.MAX_VALUE))) { for ((origin, bound) in listOf((0u to 2u), (1u to 6u), (0u to 32u), (0u to UInt.MAX_VALUE), (1u to (Int.MAX_VALUE.toUInt() + 1u)), (UInt.MIN_VALUE to UInt.MAX_VALUE))) {
repeat(1000) { repeat(1000) {
val x = subject.nextUInt(origin, bound) val x = subject.nextUInt(origin, bound)
if (x !in origin..bound - 1u) if (x !in origin until bound)
fail("Value $x must be in range [$origin, $bound)") fail("Value $x must be in range [$origin, $bound)")
} }
} }
@@ -167,7 +167,7 @@ abstract class RandomSmokeTest {
@Test @Test
fun nextUIntInUIntRange() { fun nextUIntInUIntRange() {
assertFailsWith<IllegalArgumentException> { subject.nextUInt(1u..0u) } assertFailsWith<IllegalArgumentException> { subject.nextUInt(1u..0u) }
assertFailsWith<IllegalArgumentException> { subject.nextUInt((-1).toUInt()..UInt.MIN_VALUE) } assertFailsWith<IllegalArgumentException> { subject.nextUInt(UInt.MAX_VALUE..UInt.MIN_VALUE) }
assertFailsWith<IllegalArgumentException> { subject.nextUInt(UInt.MAX_VALUE..(UInt.MAX_VALUE - 1u)) } assertFailsWith<IllegalArgumentException> { subject.nextUInt(UInt.MAX_VALUE..(UInt.MAX_VALUE - 1u)) }
repeat(1000) { it -> repeat(1000) { it ->
@@ -205,7 +205,7 @@ abstract class RandomSmokeTest {
@Test @Test
fun nextULong() { fun nextULong() {
var result1 = ULong.MIN_VALUE var result1 = 0uL
var result2 = ULong.MAX_VALUE var result2 = ULong.MAX_VALUE
repeat(1000) { repeat(1000) {
val r = subject.nextULong() val r = subject.nextULong()
@@ -214,7 +214,7 @@ abstract class RandomSmokeTest {
} }
assertEquals(ULong.MAX_VALUE, result1, "All one bits should be present") assertEquals(ULong.MAX_VALUE, result1, "All one bits should be present")
assertEquals(ULong.MIN_VALUE, result2, "All zero bits should be present") assertEquals(0uL, result2, "All zero bits should be present")
} }
@Test @Test
@@ -244,10 +244,10 @@ abstract class RandomSmokeTest {
assertEquals(0uL, subject.nextULong(1uL)) assertEquals(0uL, subject.nextULong(1uL))
} }
for (bound in listOf(2uL, 23uL, 32uL, (0x1_0000_0000).toULong(), 0x4000_0000_0000_000.toULong(), ULong.MAX_VALUE)) { for (bound in listOf(2uL, 23uL, 32uL, 0x1_0000_0000uL, 0x8000_0000_0000_000uL, ULong.MAX_VALUE)) {
repeat(1000) { repeat(1000) {
val x = subject.nextULong(bound) val x = subject.nextULong(bound)
if (x !in 0uL..(bound - 1uL)) { if (x !in 0uL until bound) {
fail("Value $x must be in range [0, $bound)") fail("Value $x must be in range [0, $bound)")
} }
} }
@@ -280,8 +280,8 @@ abstract class RandomSmokeTest {
assertFailsWith<IllegalArgumentException> { subject.nextULong(0uL, 0uL) } assertFailsWith<IllegalArgumentException> { subject.nextULong(0uL, 0uL) }
assertFailsWith<IllegalArgumentException> { subject.nextULong((-1).toULong(), (-2).toULong()) } assertFailsWith<IllegalArgumentException> { subject.nextULong((-1).toULong(), (-2).toULong()) }
for (i in 500uL..1500uL) { for (i in 0u..1000u) {
val n = (0x1_0000_0000).toULong() + i val n = 0x1_0000_0000uL - 500u + i
assertEquals(n, subject.nextULong(n, n + 1uL)) assertEquals(n, subject.nextULong(n, n + 1uL))
} }
@@ -293,7 +293,7 @@ abstract class RandomSmokeTest {
)) { )) {
repeat(1000) { repeat(1000) {
val x = subject.nextULong(origin, bound) val x = subject.nextULong(origin, bound)
if (x !in origin..(bound - 1uL)) { if (x !in origin until bound) {
fail("Value $x must be in range [$origin, $bound)") fail("Value $x must be in range [$origin, $bound)")
} }
} }
@@ -331,7 +331,6 @@ abstract class RandomSmokeTest {
repeat(1000) { i -> repeat(1000) { i ->
val n = (0x1_0000_0000).toULong() - 500uL + i.toULong() val n = (0x1_0000_0000).toULong() - 500uL + i.toULong()
assertEquals(n, subject.nextULong(n..n)) assertEquals(n, subject.nextULong(n..n))
} }
for (range in listOf( for (range in listOf(
@@ -466,6 +465,8 @@ abstract class RandomSmokeTest {
val ubytes2 = subject.nextUBytes(UByteArray(size)) val ubytes2 = subject.nextUBytes(UByteArray(size))
assertEquals(size, ubytes2.size) assertEquals(size, ubytes2.size)
assertTrue(ubytes2.any { it != 0.toUByte() }) assertTrue(ubytes2.any { it != 0.toUByte() })
assertFalse(ubytes1 contentEquals ubytes2)
} }
@Test @Test