-
Notifications
You must be signed in to change notification settings - Fork 632
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 (#3523)
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. The empty list check was removed, as an empty list will result in a list of an empty list (as with all permutations of length 0). This commit does break binary compatibility, due to adding the length parameter. Co-authored-by: Sam <sam@sksamuel.com>
- Loading branch information
Showing
3 changed files
with
34 additions
and
14 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
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
26 changes: 24 additions & 2 deletions
26
...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,38 @@ | ||
package com.sksamuel.kotest.property.exhaustive | ||
|
||
import io.kotest.assertions.throwables.shouldThrowWithMessage | ||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder | ||
import io.kotest.matchers.shouldBe | ||
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 generate a single empty list when length is zero") { | ||
Exhaustive.permutations(listOf(1, 2, 3), 0).values shouldBe listOf( listOf() ) | ||
} | ||
|
||
test("Exhaustive.permutations should throw if length is negative") { | ||
shouldThrowWithMessage<IllegalArgumentException>("length must be between 0 and the list size (1), but was -1.") | ||
{ Exhaustive.permutations(listOf(1), -1) } | ||
} | ||
|
||
test("Exhaustive.permutations should throw if length is greater than list length") { | ||
shouldThrowWithMessage<IllegalArgumentException>("length must be between 0 and the list size (3), but was 7.") | ||
{ Exhaustive.permutations(listOf(1,2,3), 7) } | ||
} | ||
} | ||
} |