Skip to content

Commit

Permalink
Add Generators For Hex Char And String Values
Browse files Browse the repository at this point in the history
  • Loading branch information
isomarcte committed May 8, 2019
1 parent ec5ed7a commit 55d504c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
17 changes: 17 additions & 0 deletions jvm/src/test/scala/org/scalacheck/GenSpecification.scala
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ object GenSpecification extends Properties("Gen") with GenSpecificationVersionSp
charType == Character.MODIFIER_SYMBOL || charType == Character.OTHER_SYMBOL
}

property("hexChar") = forAll(hexChar){ ch =>
val l: Long = java.lang.Long.parseLong(ch.toString, 16)
l < 16 && l >= 0
}

property("identifier") = forAll(identifier) { s =>
s.length > 0 && s(0).isLetter && s(0).isLower &&
s.forall(_.isLetterOrDigit)
Expand Down Expand Up @@ -312,6 +317,18 @@ object GenSpecification extends Properties("Gen") with GenSpecificationVersionSp
}
}

property("hexStr") = forAll(hexStr) { s =>
if (s.size > 0) {
Try(BigInt(new java.math.BigInteger(s, 16))) match {
case Success(bi) => bi >= BigInt(0L)
case _ => false
}
}
else {
true
}
}

// BigDecimal generation is tricky; just ensure that the generator gives
// its constructor valid values.
property("BigDecimal") = forAll { _: BigDecimal => true }
Expand Down
19 changes: 17 additions & 2 deletions src/main/scala/org/scalacheck/Gen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ object Gen extends GenArities with GenVersionSpecific {
choose(1, gs.length+2).flatMap(pick(_, g1, g2, gs: _*))

/** A generator that randomly picks a given number of elements from a list
*
*
* The elements are not guaranteed to be permuted in random order.
*/
def pick[T](n: Int, l: Iterable[T]): Gen[collection.Seq[T]] = {
Expand Down Expand Up @@ -722,7 +722,7 @@ object Gen extends GenArities with GenVersionSpecific {
}

/** A generator that randomly picks a given number of elements from a list
*
*
* The elements are not guaranteed to be permuted in random order.
*/
def pick[T](n: Int, g1: Gen[T], g2: Gen[T], gn: Gen[T]*): Gen[Seq[T]] = {
Expand Down Expand Up @@ -766,6 +766,15 @@ object Gen extends GenArities with GenVersionSpecific {
/** Generates a ASCII printable character */
def asciiPrintableChar: Gen[Char] = choose(32.toChar, 126.toChar)

/** Generates a character that can represent a valid hexadecimal digit. This
* includes both upper and lower case values.
*/
def hexChar: Gen[Char] = Gen.oneOf(
Gen.choose(48.toChar, 57.toChar), // 0-9
Gen.choose(97.toChar, 102.toChar), // a-f
Gen.choose(65.toChar, 70.toChar) // A-F
)

//// String Generators ////

/** Generates a string that starts with a lower-case alpha character,
Expand Down Expand Up @@ -803,6 +812,12 @@ object Gen extends GenArities with GenVersionSpecific {
def asciiPrintableStr: Gen[String] =
listOf(asciiPrintableChar).map(_.mkString)

/** Generates a string that can represent a valid hexadecimal digit. This
* includes both upper and lower case values.
*/
def hexStr: Gen[String] =
listOf(hexChar).map(_.mkString)


//// Number Generators ////

Expand Down

0 comments on commit 55d504c

Please sign in to comment.