This repository has been archived by the owner on Aug 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Thanks to @Kantis
- Loading branch information
Showing
3 changed files
with
155 additions
and
0 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
56 changes: 56 additions & 0 deletions
56
generator/stdlib/src/commonMain/kotlin/com/github/jcornaz/kwik/generator/stdlib/Ranges.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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
@file:Suppress("EmptyRange") | ||
|
||
package com.github.jcornaz.kwik.generator.stdlib | ||
|
||
import com.github.jcornaz.kwik.generator.api.Generator | ||
import com.github.jcornaz.kwik.generator.api.withSamples | ||
|
||
|
||
/** | ||
* Returns a generator of ClosedRange<T>, which uses the provided Generator for creating start and end elements | ||
* | ||
* @param <T> The type of the ClosedRange | ||
* @param elementGen A generator producing T, which will create the start and end elements of the range | ||
*/ | ||
fun <T : Comparable<T>> Generator.Companion.ranges(elementGen: Generator<T>): Generator<ClosedRange<T>> = | ||
Generator { rng -> elementGen.generate(rng)..elementGen.generate(rng) } | ||
|
||
|
||
/** | ||
* Creates a Generator for ClosedRange<Int>, includes samples for empty range and single item range | ||
* | ||
* @param elementGen A generator producing T, which will create the start and end elements of the range | ||
*/ | ||
@Suppress("InvalidRange") | ||
fun Generator.Companion.intRanges(elementGen: Generator<Int> = ints()): Generator<ClosedRange<Int>> = | ||
ranges(elementGen).withSamples( | ||
1..0, | ||
0..0 | ||
) | ||
|
||
|
||
/** | ||
* Creates a Generator for ClosedRange<Char>, includes samples for empty range and single item range | ||
* | ||
* @param elementGen A generator producing T, which will create the start and end elements of the range | ||
*/ | ||
@Suppress("InvalidRange") | ||
fun Generator.Companion.charRanges(elementGen: Generator<Char> = characters()): Generator<ClosedRange<Char>> = | ||
ranges(elementGen).withSamples( | ||
'B'..'A', | ||
'A'..'A' | ||
) | ||
|
||
|
||
/** | ||
* Creates a Generator for ClosedRange<Long>, includes samples for empty range and single item range | ||
* | ||
* @param elementGen A generator producing T, which will create the start and end elements of the range | ||
*/ | ||
@Suppress("InvalidRange") | ||
fun Generator.Companion.longRanges(elementGen: Generator<Long> = longs()): Generator<ClosedRange<Long>> = | ||
ranges(elementGen).withSamples( | ||
1L..0L, | ||
0L..0L | ||
) | ||
|
84 changes: 84 additions & 0 deletions
84
...dlib/src/commonTest/kotlin/com/github/jcornaz/kwik/generator/stdlib/RangeGeneratorTest.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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
@file:Suppress("EmptyRange") | ||
|
||
package com.github.jcornaz.kwik.generator.stdlib | ||
|
||
import com.github.jcornaz.kwik.generator.api.Generator | ||
import com.github.jcornaz.kwik.generator.api.randomSequence | ||
import com.github.jcornaz.kwik.generator.test.AbstractGeneratorTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertTrue | ||
|
||
class RangeGeneratorTest : AbstractGeneratorTest() { | ||
override val generator = Generator.ranges(Generator.booleans()) | ||
|
||
@Test | ||
fun `generates all possible ranges`() { | ||
// False .. True | ||
// False .. False | ||
// True .. True | ||
// Empty | ||
|
||
assertTrue( | ||
generator.randomSequence(0L) | ||
.take(100) | ||
.distinct() | ||
.count() == 4 | ||
) | ||
} | ||
} | ||
|
||
class IntRangeGeneratorTest : AbstractGeneratorTest() { | ||
|
||
override val generator = Generator.intRanges() | ||
|
||
@Test | ||
fun `empty range is sampled`() { | ||
generator.randomSequence(0L) | ||
.take(10) | ||
.contains(1..0) | ||
} | ||
|
||
@Test | ||
fun `single element range is sampled`() { | ||
generator.randomSequence(0L) | ||
.take(10) | ||
.contains(1..1) | ||
} | ||
} | ||
|
||
class CharRangeGeneratorTest : AbstractGeneratorTest() { | ||
override val generator: Generator<ClosedRange<Char>> = Generator.charRanges() | ||
|
||
@Test | ||
fun `empty range is sampled`() { | ||
generator.randomSequence(0L) | ||
.take(10) | ||
.contains('C'..'B') | ||
} | ||
|
||
@Test | ||
fun `single element range is sampled`() { | ||
generator.randomSequence(0L) | ||
.take(10) | ||
.contains('A'..'A') | ||
} | ||
} | ||
|
||
|
||
class LongRangeGeneratorTest : AbstractGeneratorTest() { | ||
override val generator = Generator.longRanges() | ||
|
||
@Test | ||
fun `empty range is sampled`() { | ||
generator.randomSequence(0L) | ||
.take(10) | ||
.contains(1L..0L) | ||
} | ||
|
||
@Test | ||
fun `single element range is sampled`() { | ||
generator.randomSequence(0L) | ||
.take(10) | ||
.contains(1L..1L) | ||
} | ||
} |