Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
5 changes: 5 additions & 0 deletions core-tests/shared/src/test/scala/zio/RandomSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ object RandomSpec extends ZIOBaseSpec {
} yield assert(n)(isGreaterThanEqualTo(min)) &&
assert(n)(isLessThan(max))
}
},
testM("nextUUID generates universally unique identifiers") {
check(Gen.fromEffect(Live.live(random.nextUUID))) { uuid =>
assert(uuid.variant)(equalTo(2))
}
}
)

Expand Down
18 changes: 18 additions & 0 deletions core/shared/src/main/scala/zio/random/package.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package zio

import java.util.UUID

package object random {
type Random = Has[Random.Service]

Expand All @@ -20,6 +22,7 @@ package object random {
def nextLongBounded(n: Long): UIO[Long]
def nextPrintableChar: UIO[Char]
def nextString(length: Int): UIO[String]
def nextUUID: UIO[UUID] = nextUUIDWith(nextLong)
def setSeed(seed: Long): UIO[Unit]
def shuffle[A, Collection[+Element] <: Iterable[Element]](collection: Collection[A])(implicit
bf: BuildFrom[Collection[A], A, Collection[A]]
Expand Down Expand Up @@ -144,6 +147,15 @@ package object random {
}
}

private[zio] def nextUUIDWith(nextLong: UIO[Long]): UIO[UUID] =
for {
mostSigBits <- nextLong
leastSigBits <- nextLong
} yield new UUID(
(mostSigBits & ~0x0000f000) | 0x00004000,
(leastSigBits & ~(0xc0000000L << 32)) | (0x80000000L << 32)
)

private[zio] def shuffleWith[A, Collection[+Element] <: Iterable[Element]](
nextIntBounded: Int => UIO[Int],
collection: Collection[A]
Expand Down Expand Up @@ -247,6 +259,12 @@ package object random {
def nextLongBounded(n: => Long): URIO[Random, Long] =
ZIO.accessM(_.get.nextLongBounded(n))

/**
* Generates psuedo-random universally unique identifiers.
*/
val nextUUID: URIO[Random, UUID] =
ZIO.accessM(_.get.nextUUID)

/**
* Generates a pseudo-random character from the ASCII range 33-126.
*/
Expand Down
3 changes: 0 additions & 3 deletions test-tests/shared/src/test/scala/zio/test/GenSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,6 @@ object GenSpec extends ZIOBaseSpec {
testM("unit generates the constant unit value") {
checkSample(Gen.unit)(forall(equalTo(())))
},
testM("UUID generates random UUIDs") {
checkSample(Gen.anyUUID)(forall(equalTo(2)), _.map(_.variant))
},
testM("vectorOf generates sizes in range") {
checkSample(Gen.vectorOf(smallInt))(
forall(isGreaterThanEqualTo(0) && isLessThanEqualTo(100)),
Expand Down
8 changes: 1 addition & 7 deletions test/shared/src/main/scala/zio/test/Gen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,7 @@ object Gen extends GenZIO with FunctionVariants with TimeVariants {
* not have any shrinking.
*/
val anyUUID: Gen[Random, UUID] =
for {
mostSigBits <- Gen.anyLong.noShrink
leastSigBits <- Gen.anyLong.noShrink
} yield new UUID(
(mostSigBits & ~0x0000f000) | 0x00004000,
(leastSigBits & ~(0xc0000000L << 32)) | (0x80000000L << 32)
)
Gen.fromEffect(nextUUID)

/**
* A generator of big decimals inside the specified range: [start, end].
Expand Down