Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Generators For Hex Char And String Values #470

Merged
merged 1 commit into from
Aug 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
18 changes: 16 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.oneOf("0123456789abcdef".toSeq),
Gen.oneOf("0123456789ABCDEF".toSeq)
)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be:

  def hexChar: Gen[Char] =  Gen.frequency(
    10 -> Gen.oneOf('0' to '9'),
     3 -> Gen.oneOf('a' to 'f'),
     3 -> Gen.oneOf('A' to 'F'))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I was trying to be consistent with the rest of the file.

//// String Generators ////

/** Generates a string that starts with a lower-case alpha character,
Expand Down Expand Up @@ -803,6 +812,11 @@ 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)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This produces a lengthy string by default (up to 100?). That works for your test with BigInteger, but may not for when people want byte, int or long from hex?

I guess you've copied the definition of Gen.numStr. I guess it has these same sort of problems.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, I agree. I did this for consistency. That being said, I'm not too worried about this. If people want to create a String which represents a bounded type, such as i32, then it would be consistent with current idioms to just map on this generator.

//// Number Generators ////

Expand Down