Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Cogen Instance For UUID #763

Merged
merged 1 commit into from
Feb 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions jvm/src/test/scala/org/scalacheck/CogenSpecification.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import ScalaVersionSpecific._
import scala.util.Try
import scala.concurrent.duration.{Duration, FiniteDuration}

import java.util.UUID

object CogenSpecification extends Properties("Cogen") {

// We need a customized definition of equality for some of our tests.
Expand Down Expand Up @@ -162,4 +164,5 @@ object CogenSpecification extends Properties("Cogen") {
include(cogenLaws[Seq[Int]], "cogenSeq.")
include(cogenLaws[Duration], "cogenDuration.")
include(cogenLaws[FiniteDuration], "cogenFiniteDuration.")
include(cogenLaws[UUID], "cogenUUID.")
}
6 changes: 6 additions & 0 deletions src/main/scala/org/scalacheck/Cogen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import scala.collection.immutable.BitSet
import scala.util.{Failure, Success, Try}
import scala.concurrent.duration.{Duration, FiniteDuration}
import java.math.BigInteger
import java.util.UUID
import rng.Seed

sealed trait Cogen[T] extends Serializable {
Expand Down Expand Up @@ -158,6 +159,11 @@ object Cogen extends CogenArities with CogenLowPriority with CogenVersionSpecifi
implicit def cogenPartialFunction[A: Arbitrary, B: Cogen]: Cogen[PartialFunction[A, B]] =
Cogen[A => Option[B]].contramap(_.lift)

implicit val cogenUUID: Cogen[UUID] =
Cogen[(Long, Long)].contramap(value =>
(value.getLeastSignificantBits, value.getMostSignificantBits)
)

def perturbPair[A, B](seed: Seed, ab: (A, B))(implicit A: Cogen[A], B: Cogen[B]): Seed =
B.perturb(A.perturb(seed, ab._1), ab._2)

Expand Down