-
Notifications
You must be signed in to change notification settings - Fork 627
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for partial permutations
Adding length parameter (defaults to list size) to the permutations function, providing the opportunity of creating exhaustives of partial permutations/k-permutations. Also, changed the assertion of the full permutations test from shouldBe to shouldContainExactlyInAnyOrder, as the order in which the permutations come are not (should not be) relevant. This also makes the test less dependent on the underlying implementation. This commit does break binary compatibility, due to adding the length parameter.
- Loading branch information
Showing
2 changed files
with
40 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 30 additions & 3 deletions
33
...t-property/src/jvmTest/kotlin/com/sksamuel/kotest/property/exhaustive/PermutationsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,43 @@ | ||
package com.sksamuel.kotest.property.exhaustive | ||
|
||
import io.kotest.assertions.throwables.shouldThrowWithMessage | ||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.matchers.shouldBe | ||
import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder | ||
import io.kotest.property.Exhaustive | ||
import io.kotest.property.exhaustive.permutations | ||
|
||
class PermutationsTest : FunSpec() { | ||
init { | ||
test("Exhaustive.permutations") { | ||
Exhaustive.permutations(listOf(1, 2, 3)).values shouldBe listOf( | ||
test("Exhaustive.permutations should generate full permutations when length is omitted") { | ||
Exhaustive.permutations(listOf(1, 2, 3)).values shouldContainExactlyInAnyOrder listOf( | ||
listOf(3, 2, 1), listOf(2, 3, 1), listOf(3, 1, 2), listOf(1, 3, 2), listOf(2, 1, 3), listOf(1, 2, 3) | ||
) | ||
} | ||
|
||
test("Exhaustive.permutations should generate partial permutations of given length") { | ||
Exhaustive.permutations(listOf(1, 2, 3), 2).values shouldContainExactlyInAnyOrder listOf( | ||
listOf(1, 2), listOf(1, 3), listOf(2, 1), listOf(2, 3), listOf(3, 1), listOf(3, 2) | ||
) | ||
} | ||
|
||
test("Exhaustive.permutations should throw if list is empty") { | ||
shouldThrowWithMessage<IllegalArgumentException>("Can't build an Exhaustive for an empty list.") | ||
{ Exhaustive.permutations(listOf<Nothing>()) } | ||
} | ||
|
||
test("Exhaustive.permutations should throw if length is zero") { | ||
shouldThrowWithMessage<IllegalArgumentException>("length must be greater than zero.") | ||
{ Exhaustive.permutations(listOf(1), 0) } | ||
} | ||
|
||
test("Exhaustive.permutations should throw if length is negative") { | ||
shouldThrowWithMessage<IllegalArgumentException>("length must be greater than zero.") | ||
{ Exhaustive.permutations(listOf(1), -1) } | ||
} | ||
|
||
test("Exhaustive.permutations should throw if length is greater than list length") { | ||
shouldThrowWithMessage<IllegalArgumentException>("length cannot be greater than the list size.") | ||
{ Exhaustive.permutations(listOf(1), 2) } | ||
} | ||
} | ||
} |