Reputation: 2984
I want to be able to generate an exhaustive permutation of objects. Imagine the following object
data class Person (name: String, age: Int)
For testing purposes, I want to restrict the name
to 3
values. Mohammad
, Nasir
, Rasul
and age
to 4
values. 10
, 20
, 30
, 40
. I want to generate 12 objects, where each name has each of the 4 ages.
I can generate an arbitrary
binding, however that doesn't gurantee 12
iterations will each have a unique object. I have to increase the iteration count, and weed out duplicates.
val list = Arb.bind(
listOf("Nasir", "Rasul", "Mohammad").exhaustive(),
listOf(10, 20, 30, 40).exhaustive()
) { name, age -> Person(name, age) }
"Test person " - {
runBlocking {
list.checkAll(12) {
System.out.println("Testing $it")
assertTrue(it.age < 50)
}
}
}
Looking in the source code, I can't seem to find a way. I am hoping someone in the community has had a need for this.
Thanks.
Note: I am looking for a way with Exhaustive
generator, not the Arb
generator. I could do some post processing and remove duplicates, but I am hoping for something more reliably unique upfront.
Example outout:
Testing Person(name=Mohammad, age=40)
Testing Person(name=Nasir, age=20)
Testing Person(name=Rasul, age=20)
Testing Person(name=Rasul, age=30)
Testing Person(name=Mohammad, age=20)
Testing Person(name=Rasul, age=40)
Testing Person(name=Nasir, age=10)
Testing Person(name=Rasul, age=10)
Testing Person(name=Nasir, age=40)
Testing Person(name=Rasul, age=40)
Testing Person(name=Nasir, age=30)
Testing Person(name=Mohammad, age=30)
Notice the Rasul:40
is duplicated. Mohammad:10
missed out.
1 possible solution based on @Tenfour04's comment is to use times
and map
. Though with my fields, the mapping becomes hairy, as we'll have Pairs
and Pairs
and Pairs
to process.
"Test cross product" - {
val times = Exhaustive.collection(listOf("Nasir", "Rasul"))
.times(Exhaustive.collection(listOf(10, 20)))
.map { Person(it.first as String, it.second) }
runBlocking {
times.checkAll(4) {
println("$it")
}
}
}
Upvotes: 3
Views: 1540
Reputation: 16387
You can do this by just mapping over each of the values in each component and combining them into a new Exhaustive. For example, if you have three components you want to generate all the combinations for:
fun <A, B, C, D> cartesian(
a: Exhaustive<A>,
b: Exhaustive<B>,
c: Exhaustive<C>,
f: (A, B, C) -> D
): Exhaustive<D> {
val ds = a.values.flatMap { _a ->
b.values.flatMap { _b ->
c.values.map { _c ->
f(_a, _b, _c)
}
}
}
return ds.exhaustive()
}
Then that exhaustive can be used in a test (And so on for arity-2 etc).
Here is how you would use it for your persons example.
val persons = cartesian(
Exhaustive.collection(listOf("Nasir", "Rasul")),
Exhaustive.collection(listOf(10, 20))
) { a, b -> Person(a, b) }
checkAll(persons) { person -> .... test here .... }
Note: The above function exists in Kotest 4.5 which at time of writing hasn't been released. https://github.com/kotest/kotest/blob/master/kotest-property/src/commonMain/kotlin/io/kotest/property/exhaustive/cartesian.kt
Upvotes: 5