Skip to content

Commit b128f41

Browse files
committed
Overload randomSublist and randomSubset with sizeRange parameter
1 parent 20baafb commit b128f41

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

core/src/integration/kotlin/io/github/serpro69/kfaker/docs/Extras.kt

+2
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,15 @@ class Extras : DescribeSpec({
213213
// START extras_random_everything_six
214214
val list = List(100) { it }
215215
faker.random.randomSublist(list, size = 10, shuffled = true)
216+
faker.random.randomSublist(list, sizeRange = 6..42, shuffled = true)
216217
// END extras_random_everything_six
217218
}
218219

219220
it("should generate random subset") {
220221
// START extras_random_everything_seven
221222
val set = setOf(*List(100) { it }.toTypedArray())
222223
faker.random.randomSubset(set, size = 10, shuffled = true)
224+
faker.random.randomSubset(set, sizeRange = 66..99, shuffled = true)
223225
// END extras_random_everything_seven
224226
}
225227

core/src/main/kotlin/io/github/serpro69/kfaker/RandomService.kt

+30-1
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,26 @@ class RandomService internal constructor(private val config: FakerConfig) {
285285
.subList(from, to)
286286
}
287287

288+
/**
289+
* Returns a view of the portion of the [list]
290+
* with pseudo-randomly generated `fromIndex` and (possibly) `toIndex` values.
291+
*
292+
* @param sizeRange the desired size range of the resulting list.
293+
* The `size` of the returned list is the result of calling [nextInt] with the [sizeRange].
294+
* IF `size <= 0` then `toIndex` will also be randomly-generated.
295+
* @param shuffled if `true` the [list] will be shuffled before extracting the sublist
296+
*/
297+
@JvmOverloads
298+
fun <T> randomSublist(list: List<T>, sizeRange: IntRange, shuffled: Boolean = false): List<T> {
299+
return randomSublist(list, nextInt(sizeRange), shuffled)
300+
}
301+
288302
/**
289303
* Returns a portion of the [set]
290-
* with pseudo-randomly generated `fromIndex` and `toIndex` values.
304+
* with pseudo-randomly generated `fromIndex` and (possibly) `toIndex` values.
291305
*
306+
* @param size the desired size of the resulting set.
307+
* If `size <= 0` then `toIndex` will also be randomly-generated.
292308
* @param shuffled if `true` the [set] will be shuffled before extracting the subset
293309
*/
294310
@JvmOverloads
@@ -300,6 +316,19 @@ class RandomService internal constructor(private val config: FakerConfig) {
300316
.toSet()
301317
}
302318

319+
/**
320+
* Returns a portion of the [set]
321+
* with pseudo-randomly generated `fromIndex` and (possibly) `toIndex` values.
322+
*
323+
* @param sizeRange the desired size range of the resulting list.
324+
* The `size` of the returned list is the result of calling [nextInt] with the [sizeRange].
325+
* IF `size <= 0` then `toIndex` will also be randomly-generated.
326+
* @param shuffled if `true` the [set] will be shuffled before extracting the subset
327+
*/
328+
fun <T> randomSubset(set: Set<T>, sizeRange: IntRange, shuffled: Boolean = false): Set<T> {
329+
return randomSubset(set, nextInt(sizeRange), shuffled)
330+
}
331+
303332
private fun <T> Collection<T>.randomFromToIndices(s: Int): Pair<Int, Int> {
304333
val fromIndex = if (s > 0) {
305334
(0..size - s).random(random.asKotlinRandom())

core/src/test/kotlin/io/github/serpro69/kfaker/RandomServiceTest.kt

+19-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import io.kotest.matchers.collections.shouldContain
99
import io.kotest.matchers.collections.shouldContainAll
1010
import io.kotest.matchers.collections.shouldHaveSize
1111
import io.kotest.matchers.collections.shouldNotBeSortedWith
12+
import io.kotest.matchers.ints.shouldBeInRange
1213
import io.kotest.matchers.should
1314
import io.kotest.matchers.shouldBe
1415
import io.kotest.matchers.shouldNotBe
@@ -246,10 +247,19 @@ internal class RandomServiceTest : DescribeSpec({
246247
if (sublist.isNotEmpty()) sublist shouldBeSortedWith Comparator { o1, o2 -> o1.compareTo(o2) }
247248
}
248249
}
250+
251+
it("should return a random sublist of a size withing a given range run#$it") {
252+
val sublist = randomService.randomSublist(list, sizeRange = 12..42)
253+
assertSoftly {
254+
list shouldContainAll sublist
255+
sublist.size shouldBeInRange 12..42
256+
if (sublist.isNotEmpty()) sublist shouldBeSortedWith Comparator { o1, o2 -> o1.compareTo(o2) }
257+
}
258+
}
249259
}
250260

251261
it("should return a random shuffled sublist") {
252-
val sublist = randomService.randomSublist(list, shuffled = true)
262+
val sublist = randomService.randomSublist(list, size = 10, shuffled = true)
253263
assertSoftly {
254264
list shouldContainAll sublist
255265
if (sublist.isNotEmpty()) sublist shouldNotBeSortedWith Comparator { o1, o2 -> o1.compareTo(o2) }
@@ -277,6 +287,14 @@ internal class RandomServiceTest : DescribeSpec({
277287
if (subset.isNotEmpty()) subset shouldBeSortedWith Comparator { o1, o2 -> o1.compareTo(o2) }
278288
}
279289
}
290+
it("should return a random subset of a size witing a given range run#$it") {
291+
val subset = randomService.randomSubset(set, sizeRange = 10..20)
292+
assertSoftly {
293+
set shouldContainAll subset
294+
subset.size shouldBeInRange 10..20
295+
if (subset.isNotEmpty()) subset shouldBeSortedWith Comparator { o1, o2 -> o1.compareTo(o2) }
296+
}
297+
}
280298
}
281299

282300
it("should return a random shuffled subset") {

0 commit comments

Comments
 (0)