Skip to content
This repository has been archived by the owner on Aug 24, 2021. It is now read-only.

Commit

Permalink
chore: Remove deprecated members (#198)
Browse files Browse the repository at this point in the history
BREAKING CHANGE:  Deprecated fucntions have been removed (`Generator.create` and `simplifier`). The deprecation level has also been raised to "error" for `Generator.flatMap`. Make sure you're not using any deprecated members before updating.
  • Loading branch information
jcornaz authored Aug 16, 2020
1 parent 8ab7b38 commit 393e3eb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,6 @@ package com.github.jcornaz.kwik.fuzzer.api.simplifier

import com.github.jcornaz.kwik.fuzzer.api.ExperimentalKwikFuzzer

/**
* Returns a simplifier that use the given [simplify] function to find simpler values
*/
@Deprecated(
message = "Since Generator is a fun interface, one can create an instance of like for any other fun interface",
replaceWith = ReplaceWith("Simplifier(simplify)")
)
@ExperimentalKwikFuzzer
fun <T> simplifier(simplify: (T) -> Sequence<T>): Simplifier<T> = Simplifier(simplify)

/**
* Create a [Simplifier] that can simplify pairs.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,6 @@ fun interface Generator<out T> {

companion object {

/**
* Create a simple random [Generator].
*
* @param next Function that will be invoked to get a new random parameter.
* The function should use the given [Random] generator to ensure predictability of the values
*/
@Deprecated(
message = "Since Generator is a fun interface, one can instantiate it like for any other fun interface",
replaceWith = ReplaceWith("Generator(next)")
)
fun <T> create(next: (Random) -> T): Generator<T> = Generator(next)

/**
* Create a random [Generator] generating values out of the given [samples]
*/
Expand All @@ -38,7 +26,7 @@ fun interface Generator<out T> {

require(list.isNotEmpty()) { "No given sample" }

return Generator { it: Random -> list.random(it) }
return Generator { list.random(it) }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,23 @@ private const val DEFAULT_SAMPLE_PROBABILITY = 0.2
* the original generator.
*/
fun <T, R> Generator<T>.map(transform: (T) -> R): Generator<R> =
Generator { it: Random -> transform(generate(it)) }
Generator { transform(generate(it)) }

/**
* Returns a generator that generates only elements matching the given predicate.
*
* **Usage of this operator slows down the property tests**
* Use it with caution and always favor customizing or creating generators if possible.
*/
fun <T> Generator<T>.filter(predicate: (T) -> Boolean): Generator<T> =
Generator { random: Random ->
var value = generate(random)

while (!predicate(value))
value = generate(random)

value
}

/**
* Returns a new generator backed by [this] generator and applying the given [transform] function
Expand All @@ -22,30 +38,18 @@ fun <T, R> Generator<T>.map(transform: (T) -> R): Generator<R> =
* ```
*/
fun <T, R> Generator<T>.andThen(transform: (T) -> Generator<R>): Generator<R> =
Generator { it: Random -> transform(generate(it)).generate(it) }
Generator { transform(generate(it)).generate(it) }

/**
* @Deprecated Use `andThen` operator instead
*/
@Deprecated("Use `andThen` operator instead", ReplaceWith("andThen(transform)"))
@Deprecated(
"Use `andThen` operator instead",
ReplaceWith("andThen(transform)"),
DeprecationLevel.ERROR
)
fun <T, R> Generator<T>.flatMap(transform: (T) -> Generator<R>): Generator<R> = andThen(transform)

/**
* Returns a generator that generates only elements matching the given predicate.
*
* **Usage of this operator slows down the property tests**
* Use it with caution and always favor customizing or creating generators if possible.
*/
fun <T> Generator<T>.filter(predicate: (T) -> Boolean): Generator<T> =
Generator { random: Random ->
var value = generate(random)

while (!predicate(value))
value = generate(random)

value
}

/**
* Returns a generator containing all elements except the ones matching the given predicate.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,56 +1,58 @@
package com.github.jcornaz.kwik.generator.api

import com.github.jcornaz.kwik.generator.test.AbstractGeneratorTest
import kotlin.random.Random
import kotlin.test.*
import kotlin.test.Test
import kotlin.test.assertFailsWith
import kotlin.test.assertSame
import kotlin.test.assertTrue

class WithSampleTest : AbstractGeneratorTest() {

override val generator: Generator<Int> =
Generator { it: Random -> it.nextInt(5, Int.MAX_VALUE) }
Generator { it.nextInt(5, Int.MAX_VALUE) }
.withSamples(1, 2, 3, 4)

@Test
fun emptyListOfSamplesReturnOriginalGenerator() {
val gen = Generator { it: Random -> it.nextInt() }
val gen = Generator { it.nextInt() }
assertSame(gen, gen.withSamples())
}

@Test
fun failsIfProbabilityIsBellowZero() {
val source = Generator { it: Random -> it.nextInt(5, Int.MAX_VALUE) }
val source = Generator { it.nextInt(5, Int.MAX_VALUE) }
assertFailsWith<IllegalArgumentException> {
source.withSamples(1, 2, 3, 4, probability = -1.0)
}
}

@Test
fun failsIfProbabilityIsZero() {
val source = Generator { it: Random -> it.nextInt(5, Int.MAX_VALUE) }
val source = Generator { it.nextInt(5, Int.MAX_VALUE) }
assertFailsWith<IllegalArgumentException> {
source.withSamples(1, 2, 3, 4, probability = 0.0)
}
}

@Test
fun failsIfProbabilityOne() {
val source = Generator { it: Random -> it.nextInt(5, Int.MAX_VALUE) }
val source = Generator { it.nextInt(5, Int.MAX_VALUE) }
assertFailsWith<IllegalArgumentException> {
source.withSamples(1, 2, 3, 4, probability = 1.0)
}
}

@Test
fun failsIfProbabilityAboveOne() {
val source = Generator { it: Random -> it.nextInt(5, Int.MAX_VALUE) }
val source = Generator { it.nextInt(5, Int.MAX_VALUE) }
assertFailsWith<IllegalArgumentException> {
source.withSamples(1, 2, 3, 4, probability = 1.01)
}
}

@Test
fun generateSamplesAtGivenProbability() {
val sampleCount = Generator { it: Random -> it.nextInt(5, Int.MAX_VALUE) }
val sampleCount = Generator { it.nextInt(5, Int.MAX_VALUE) }
.withSamples(1, 2, 3, 4, probability = 0.5)
.randomSequence(0)
.take(1000)
Expand All @@ -63,10 +65,10 @@ class WithSampleTest : AbstractGeneratorTest() {
class WithNullTest : AbstractGeneratorTest() {

override val generator: Generator<Int?> =
Generator { it: Random -> it.nextInt(5, Int.MAX_VALUE) }.withNull()
Generator { it.nextInt(5, Int.MAX_VALUE) }.withNull()

@Test
fun generatesNull() {
assertTrue(Generator { it: Random -> Any() }.withNull().randomSequence(0).take(50).any { it == null })
assertTrue(Generator { Any() }.withNull().randomSequence(0).take(50).any { it == null })
}
}

0 comments on commit 393e3eb

Please sign in to comment.