Skip to content

Commit

Permalink
Check that quantified and sized work respectively with each other.
Browse files Browse the repository at this point in the history
  • Loading branch information
SimY4 committed Aug 2, 2024
1 parent 74e4dff commit 30a74a3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ class CoregexSuite extends ScalaCheckSuite with CoregexArbitraries {
}

property("empty quantified should give empty") {
forAll { (range: QuantifyRange, `type`: Coregex.Quantified.Type, rng: RNG) =>
Coregex.empty().quantify(range.min, range.max, `type`).generate(rng).isEmpty
forAll { (range: QuantifyRange, rng: RNG) =>
Coregex.empty().quantify(range.min, range.max, range.`type`).generate(rng).isEmpty
}
}

property("quantified length should be in range") {
forAll { (coregex: Coregex, range: QuantifyRange, `type`: Coregex.Quantified.Type, rng: RNG) =>
val quantified = coregex.quantify(range.min, range.max, `type`)
forAll { (coregex: Coregex, range: QuantifyRange, rng: RNG) =>
val quantified = coregex.quantify(range.min, range.max, range.`type`)
val generated = quantified.generate(rng)

val quantifiedMinLengthCheck = (0 < quantified.minLength()) ==>
Expand All @@ -53,11 +53,21 @@ class CoregexSuite extends ScalaCheckSuite with CoregexArbitraries {
}

property("double quantified should multiply quantification") {
forAll {
(coregex: Coregex, first: QuantifyRange, second: QuantifyRange, `type`: Coregex.Quantified.Type, rng: RNG) =>
val quantified = coregex.quantify(first.min * second.min, first.min * second.min, `type`)
val doubleQuantified = coregex.quantify(first.min, first.min, `type`).quantify(second.min, second.min, `type`)
quantified.generate(rng) ?= doubleQuantified.generate(rng)
forAll { (coregex: Coregex, first: QuantifyRange, second: QuantifyRange, rng: RNG) =>
val quantified = coregex.quantify(first.min * second.min, first.min * second.min, first.`type`)
val doubleQuantified =
coregex.quantify(first.min, first.min, first.`type`).quantify(second.min, second.min, second.`type`)
quantified.generate(rng) ?= doubleQuantified.generate(rng)
}
}

property("quantified sized respect both") {
forAll { (coregex: Coregex, range: QuantifyRange, length: Byte, rng: RNG) =>
val quantified = coregex.quantify(range.min, range.max, range.`type`)
val size = quantified.minLength() + length.toInt.abs
val expectedMaxLength = if (quantified.maxLength() < 0) size else quantified.maxLength() min size
val generated = quantified.sized(size).generate(rng)
(generated.length() <= expectedMaxLength) :| s"${generated.length()} <= $expectedMaxLength"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ object QuantifyRange {
implicit def shrinkQuantifyRange(implicit shrinkInt: Shrink[Int]): Shrink[QuantifyRange] =
Shrink { case QuantifyRange(min, max, tpe) =>
shrinkInt.shrink(min).map(QuantifyRange(_, max, tpe)) #:::
shrinkInt.shrink(max).map(QuantifyRange(min, _, tpe))
shrinkInt.shrink(max).filter(min <= _).map(QuantifyRange(min, _, tpe))
}
}

0 comments on commit 30a74a3

Please sign in to comment.