diff --git a/jvm/src/test/scala/org/scalacheck/GenSpecification.scala b/jvm/src/test/scala/org/scalacheck/GenSpecification.scala index 714e9f487..0f7a48c3d 100644 --- a/jvm/src/test/scala/org/scalacheck/GenSpecification.scala +++ b/jvm/src/test/scala/org/scalacheck/GenSpecification.scala @@ -58,6 +58,11 @@ object GenSpecification extends Properties("Gen") with GenSpecificationVersionSp forAll(frequency(List.fill(n)((1,const(0))): _*)) { _ == 0 } } + property("frequency 4") = + Prop.throws(classOf[IllegalArgumentException]) { + frequency() + } + property("lzy") = forAll((g: Gen[Int]) => lzy(g) == g) property("wrap") = forAll((g: Gen[Int]) => delay(g) == g) @@ -141,6 +146,13 @@ object GenSpecification extends Properties("Gen") with GenSpecificationVersionSp } } + property("oneOf n in set") = forAll { (s: Set[Int]) => + Try(oneOf(s)) match { + case Success(g) => forAll(g)(s.contains) + case Failure(_) => Prop(s.isEmpty) + } + } + property("oneOf 2") = forAll { (n1:Int, n2:Int) => forAll(oneOf(n1, n2)) { n => n == n1 || n == n2 } } diff --git a/src/main/scala/org/scalacheck/Gen.scala b/src/main/scala/org/scalacheck/Gen.scala index b7c75f1f1..e0feea8ac 100644 --- a/src/main/scala/org/scalacheck/Gen.scala +++ b/src/main/scala/org/scalacheck/Gen.scala @@ -521,8 +521,8 @@ object Gen extends GenArities with GenVersionSpecific { /** Creates a resized version of a generator */ def resize[T](s: Int, g: Gen[T]) = gen((p, seed) => g.doApply(p.withSize(s), seed)) - /** Picks a random value from a list */ - def oneOf[T](xs: Seq[T]): Gen[T] = + /** Picks a random value from a list. */ + def oneOf[T](xs: Iterable[T]): Gen[T] = if (xs.isEmpty) { throw new IllegalArgumentException("oneOf called on empty collection") } else { @@ -530,6 +530,12 @@ object Gen extends GenArities with GenVersionSpecific { choose(0, vector.size - 1).map(vector(_)) } + /** Picks a random value from a list. + * @todo Remove this overloaded method in the next major release. See #438. + */ + def oneOf[T](xs: Seq[T]): Gen[T] = + oneOf(xs: Iterable[T]) + /** Picks a random value from a list */ def oneOf[T](t0: T, t1: T, tn: T*): Gen[T] = oneOf(t0 +: t1 +: tn)