我需要生成相同长度的字符串。我不知道怎么做。非常感谢
val s = for {
x <- Gen.alphaStr
} yield ...
示例代码:
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
检查生成的字符串使用:
strGen(5).sample
Gen 上有一个suchThat(f: T => Boolean): Gen[T] = new Gen[T]
,所以可以像这样使用
val stringShorterThan = (n: Int) => Gen.alphaStr.suchThat(s => s.length <= n)
val string64 = stringShorterThan(64)