13

I need to generate strings with the same length. I can't realize how. Many thanks

val s = for {
  x <- Gen.alphaStr
} yield ...
petrn
  • 195
  • 2
  • 8

2 Answers2

20

example code:

import org.scalacheck.Gen
import org.scalacheck.Prop.forAll    

// strGen generates a fixed length random string
val strGen = (n: Int) => Gen.listOfN(n, Gen.alphaChar).map(_.mkString)

val fixedLengthStr = forAll(strGen(10)){ s =>
  s.length == 10
}

fixedLengthStr.check

to inspect a generated string use:

strGen(5).sample
j-keck
  • 1,021
  • 1
  • 7
  • 13
-2

There is a suchThat(f: T => Boolean): Gen[T] = new Gen[T] on Gen, so could be used like

val stringShorterThan = (n: Int) => Gen.alphaStr.suchThat(s => s.length <= n)
val string64 = stringShorterThan(64)