Skip to content

Commit

Permalink
Added more seeding methods
Browse files Browse the repository at this point in the history
  • Loading branch information
japgolly committed Sep 19, 2016
1 parent 5f3689e commit 25b9f98
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
5 changes: 4 additions & 1 deletion doc/changelog/0.7.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
12 changes: 12 additions & 0 deletions nyaya-gen/src/main/scala/nyaya/gen/Gen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

Expand Down Expand Up @@ -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.
*
Expand Down
14 changes: 14 additions & 0 deletions nyaya-test/src/test/scala/nyaya/gen/GenTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}
}

0 comments on commit 25b9f98

Please sign in to comment.