Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scalacheck generate Gen.alphastr with the same length

Tags:

scalacheck

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

val s = for {
  x <- Gen.alphaStr
} yield ...
like image 420
petrn Avatar asked Nov 07 '14 19:11

petrn


1 Answers

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
like image 101
j-keck Avatar answered Oct 10 '22 17:10

j-keck