Extension random() to select random element from a collection
Fixes #KT-15539
This commit is contained in:
@@ -11,6 +11,7 @@ import test.collections.behaviors.*
|
||||
import test.comparisons.STRING_CASE_INSENSITIVE_ORDER
|
||||
import kotlin.test.*
|
||||
import kotlin.comparisons.*
|
||||
import kotlin.random.Random
|
||||
|
||||
fun <T> assertArrayNotSameButEquals(expected: Array<out T>, actual: Array<out T>, message: String = "") { assertTrue(expected !== actual && expected contentEquals actual, message) }
|
||||
fun assertArrayNotSameButEquals(expected: IntArray, actual: IntArray, message: String = "") { assertTrue(expected !== actual && expected contentEquals actual, message) }
|
||||
@@ -592,6 +593,31 @@ class ArraysTest {
|
||||
expect(2) { arrayOf(1, 2, 3).last { it % 2 == 0 } }
|
||||
}
|
||||
|
||||
|
||||
@Test fun random() {
|
||||
Array(100) { it }.let { array ->
|
||||
val tosses = List(10) { array.random() }
|
||||
assertTrue(tosses.distinct().size > 1, "Should be some distinct elements in $tosses")
|
||||
|
||||
val seed = Random.nextInt()
|
||||
val random1 = Random(seed)
|
||||
val random2 = Random(seed)
|
||||
|
||||
val tosses1 = List(10) { array.random(random1) }
|
||||
val tosses2 = List(10) { array.random(random2) }
|
||||
|
||||
assertEquals(tosses1, tosses2)
|
||||
}
|
||||
|
||||
arrayOf("x").let { singletonArray ->
|
||||
val tosses = List(10) { singletonArray.random() }
|
||||
assertEquals(singletonArray.toList(), tosses.distinct())
|
||||
}
|
||||
|
||||
assertFailsWith<NoSuchElementException> { emptyArray<Any>().random() }
|
||||
}
|
||||
|
||||
|
||||
@Test fun contains() {
|
||||
assertTrue(arrayOf("1", "2", "3", "4").contains("2"))
|
||||
assertTrue("3" in arrayOf("1", "2", "3", "4"))
|
||||
|
||||
@@ -10,6 +10,8 @@ import kotlin.test.*
|
||||
import test.collections.behaviors.*
|
||||
import test.comparisons.STRING_CASE_INSENSITIVE_ORDER
|
||||
import kotlin.comparisons.*
|
||||
import kotlin.math.sin
|
||||
import kotlin.random.Random
|
||||
|
||||
class CollectionTest {
|
||||
|
||||
@@ -620,6 +622,31 @@ class CollectionTest {
|
||||
assertFails { arrayListOf<Int>().last() }
|
||||
}
|
||||
|
||||
@Test fun random() {
|
||||
val list = List(100) { it }
|
||||
val set = list.toSet()
|
||||
listOf(list, set).forEach { collection: Collection<Int> ->
|
||||
val tosses = List(10) { collection.random() }
|
||||
assertTrue(tosses.distinct().size > 1, "Should be some distinct elements in $tosses")
|
||||
|
||||
val seed = Random.nextInt()
|
||||
val random1 = Random(seed)
|
||||
val random2 = Random(seed)
|
||||
|
||||
val tosses1 = List(10) { collection.random(random1) }
|
||||
val tosses2 = List(10) { collection.random(random2) }
|
||||
|
||||
assertEquals(tosses1, tosses2)
|
||||
}
|
||||
|
||||
listOf("x").let { singletonList ->
|
||||
val tosses = List(10) { singletonList.random() }
|
||||
assertEquals(singletonList, tosses.distinct())
|
||||
}
|
||||
|
||||
assertFailsWith<NoSuchElementException> { emptyList<Any>().random() }
|
||||
}
|
||||
|
||||
@Test fun subscript() {
|
||||
val list = arrayListOf("foo", "bar")
|
||||
assertEquals("foo", list[0])
|
||||
|
||||
@@ -10,6 +10,7 @@ import test.*
|
||||
import test.collections.behaviors.iteratorBehavior
|
||||
import test.collections.compare
|
||||
import kotlin.math.sign
|
||||
import kotlin.random.Random
|
||||
|
||||
|
||||
fun createString(content: String): CharSequence = content
|
||||
@@ -936,6 +937,29 @@ class StringTest {
|
||||
assertNull(data.filterNot { it.isAsciiLetter() || it.isAsciiDigit() }.firstOrNull())
|
||||
}
|
||||
|
||||
@Test fun random() = withOneCharSequenceArg { data ->
|
||||
data("abcdefg").let { charSeq ->
|
||||
val tosses = List(10) { charSeq.random() }
|
||||
assertTrue(tosses.distinct().size > 1, "Should be some distinct elements in $tosses")
|
||||
|
||||
val seed = Random.nextInt()
|
||||
val random1 = Random(seed)
|
||||
val random2 = Random(seed)
|
||||
|
||||
val tosses1 = List(10) { charSeq.random(random1) }
|
||||
val tosses2 = List(10) { charSeq.random(random2) }
|
||||
|
||||
assertEquals(tosses1, tosses2)
|
||||
}
|
||||
|
||||
data("x").let { singletonCharSeq ->
|
||||
val tosses = List(10) { singletonCharSeq.random() }
|
||||
assertEquals(singletonCharSeq.toList(), tosses.distinct())
|
||||
}
|
||||
|
||||
assertFailsWith<NoSuchElementException> { data("").random() }
|
||||
}
|
||||
|
||||
@Test fun partition() {
|
||||
val data = "a1b2c3"
|
||||
val pair = data.partition { it.isAsciiDigit() }
|
||||
|
||||
Reference in New Issue
Block a user