use SizeRange instead
Use PropertyCheckConfiguration instead
Use PropertyCheckConfiguration directly instead.
On the basis of the above we can create a generator for the following case class:
On the basis of the above we can create a generator for the following case class:
case class Foo(intValue: Int, charValue: Char)
Conditional generators can be defined using Gen.suchThat
.
Conditional generators can be defined using Gen.suchThat
.
Conditional generators works just like conditional properties, in the sense that if the condition is too hard,
ScalaCheck might not be able to generate enough values, and it might report a property test as undecided.
The smallEvenInteger
definition is probably OK, since it will only throw away half of the generated numbers,
but one has to be careful when using the suchThat
operator.
Let's see how to create a new generator.
Let's see how to create a new generator. The best way to do it is to use the generator combinators that exist
in the org.scalacheck.Gen
module. These can be combined using a for-comprehension. Suppose you need a generator
which generates a tuple that contains two random integer values, one of them being at least twice as big as the
other. The following definition does this:
The distribution is uniform, but if you want to control it you can use the frequency combinator:
The distribution is uniform, but if you want to control it you can use the frequency combinator:
val vowel = Gen.frequency( (3, 'A'), (4, 'E'), (2, 'I'), (3, 'O'), (1, 'U') )
Now, the vowel generator will generate E:s more often than U:s. Roughly, 4/14 of the values generated will be E:s, and 1/14 of them will be U:s.
Other methods in the Gen
API:
def alphaChar: Gen[Char] def alphaStr: Gen[String] def posNum[T](implicit n: Numeric[T]): Gen[T] def listOf[T](g: Gen[T]): Gen[List[T]] def listOfN[T](n: Int, g: Gen[T]): Gen[List[T]]
You can create generators that pick one value out of a selection of values.
You can create generators that pick one value out of a selection of values.
The oneOf
method creates a generator that randomly picks one of its parameters each time it generates a value.
Notice that plain values are implicitly converted to generators (which always generate that value) if needed.
The following generator generates a vowel:
There is a special generator, Gen.containerOf
, that generates containers such as lists and arrays.
There is a special generator, Gen.containerOf
, that generates containers such as lists and arrays.
It takes another generator as argument which is responsible for generating the individual items.
You can use it in the following way:
val genIntList = Gen.containerOf[List,Int](Gen.oneOf(1, 3, 5)) val genStringStream = Gen.containerOf[Stream,String](Gen.alphaStr) val genBoolArray = Gen.containerOf[Array,Boolean](true)
By default, ScalaCheck supports generation of List
, Stream
, Set
, Array
, and ArrayList
(from java.util
). You can add support for additional containers by adding implicit Buildable
instances.
There is also Gen.nonEmptyContainerOf
for generating non-empty containers, and Gen.containerOfN
for
generating containers of a given size.
When ScalaCheck uses a generator to generate a value, it feeds it with some parameters.
When ScalaCheck uses a generator to generate a value, it feeds it with some parameters. One of the parameters the generator is given is a size value, which some generators use to generate their values.
If you want to use the size parameter in your own generator, you can use the Gen.sized
method:
def sized[T](f: Int => Gen[T])
In this example we're creating a generator that produces two lists of numbers where 1/3 are positive and 2/3 are negative. Note: we're also returning the original size to verify the behaviour.
use maxDiscardedFactor instead
use SizeRange instead
The trap method is no longer needed for demos in the REPL, which now abreviates stack traces, and will be removed in a future version of ScalaTest
Generators are responsible for generating test data in ScalaCheck, and are represented by the
org.scalacheck.Gen
class. In theGen
object, there are several methods for creating new and modifying existing generators. We will show how to use some of them in this section. For a more complete reference of what is available, please see the API scaladoc.A generator can be seen simply as a function that takes some generation parameters, and (maybe) returns a generated value. That is, the type
Gen[T]
may be thought of as a function of typeGen.Params => Option[T]
. However, theGen
class contains additional methods to make it possible to map generators, use them in for-comprehensions and so on. Conceptually, though, you should think of generators simply as functions, and the combinators in theGen
object can be used to create or modify the behaviour of such generator functions.