我需要生成长度相同的字符串。我怎么也想不到。非常感谢
val s = for {
x <- Gen.alphaStr
} yield ...
答案 0 :(得分:12)
示例代码:
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
答案 1 :(得分:0)
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)