diff --git a/doc/changelog/0.7.3.md b/doc/changelog/0.7.3.md index a409a5f..cc8e459 100644 --- a/doc/changelog/0.7.3.md +++ b/doc/changelog/0.7.3.md @@ -3,7 +3,10 @@ * Added to `object Gen`: * `uuid` for UUIDs. * `randomSeed` to generate and apply a random (non-determinstic) seed. - * + * `setOptionalSeed`. * Added to `Gen` instances: * `>>` to discard the result and `flatMap` to something else. + * `withSeed`. + * `withOptionalSeed`. + * `withRandomSeed`. diff --git a/nyaya-gen/src/main/scala/nyaya/gen/Gen.scala b/nyaya-gen/src/main/scala/nyaya/gen/Gen.scala index d05aebb..3af79f8 100644 --- a/nyaya-gen/src/main/scala/nyaya/gen/Gen.scala +++ b/nyaya-gen/src/main/scala/nyaya/gen/Gen.scala @@ -63,6 +63,15 @@ final case class Gen[+A](run: Gen.Run[A]) extends AnyVal { // This is what scala.Future does throw new NoSuchElementException("Gen.withFilter predicate is not satisfied")) + def withSeed(seed: Long): Gen[A] = + Gen.setSeed(seed) >> this + + def withOptionalSeed(s: Option[Long]): Gen[A] = + Gen.setOptionalSeed(s) >> this + + def withRandomSeed: Gen[A] = + Gen.randomSeed >> this + def option: Gen[Option[A]] = Gen(c => if (c.nextBit()) None else Some(run(c))) @@ -392,6 +401,9 @@ object Gen { def setSeed(seed: Long): Gen[Unit] = Gen(_ setSeed seed) + def setOptionalSeed(s: Option[Long]): Gen[Unit] = + s.fold(unit)(setSeed) + /** * Apply a new deterministic seed to the RNG. * diff --git a/nyaya-test/src/test/scala/nyaya/gen/GenTest.scala b/nyaya-test/src/test/scala/nyaya/gen/GenTest.scala index 1e661db..d4971fc 100644 --- a/nyaya-test/src/test/scala/nyaya/gen/GenTest.scala +++ b/nyaya-test/src/test/scala/nyaya/gen/GenTest.scala @@ -200,5 +200,19 @@ object GenTest extends TestSuite { assert(a != b) } + 'withSeed { + val (a,b,c,d,e) = Gen.tuple5( + Gen.long, + Gen.long, + Gen.long withSeed 0, + Gen.long withSeed 1, + Gen.long).sample() + val s0 = -4962768465676381896L + val s1 = -4964420948893066024L + val n1 = 7564655870752979346L + assert(c == s0, d == s1, e == n1) + assert(Set(a,b,c,d,e).size == 5) + } + } }