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
74 changes: 74 additions & 0 deletions core-tests/shared/src/test/scala/zio/stm/random/TRandomSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package zio.stm.random

import zio.ZIOBaseSpec
import zio.random.Random
import zio.test.Assertion._
import zio.test._

object TRandomSpec extends ZIOBaseSpec {

implicit val DoubleOrdering: Ordering[Double] =
(l, r) => java.lang.Double.compare(l, r)

implicit val FloatOrdering: Ordering[Float] =
(l, r) => java.lang.Float.compare(l, r)

def spec: ZSpec[Environment, Failure] = suite("TRandomSpec")(
testM("nextDoubleBetween generates doubles in specified range") {
checkM(genDoubles) { case (min, max) =>
for {
n <- nextDoubleBetween(min, max).commit
} yield assert(n)(isGreaterThanEqualTo(min)) &&
assert(n)(isLessThan(max))
}
},
testM("nextFloatBetween generates floats in specified range") {
checkM(genFloats) { case (min, max) =>
for {
n <- nextFloatBetween(min, max).commit
} yield assert(n)(isGreaterThanEqualTo(min)) &&
assert(n)(isLessThan(max))
}
},
testM("nextIntBetween generates integers in specified range") {
checkM(genInts) { case (min, max) =>
for {
n <- nextIntBetween(min, max).commit
} yield assert(n)(isGreaterThanEqualTo(min)) &&
assert(n)(isLessThan(max))
}
},
testM("nextLongBetween generates longs in specified range") {
checkM(genLongs) { case (min, max) =>
for {
n <- nextLongBetween(min, max).commit
} yield assert(n)(isGreaterThanEqualTo(min)) &&
assert(n)(isLessThan(max))
}
}
).provideCustomLayer(TRandom.live)

val genDoubles: Gen[Random, (Double, Double)] =
for {
a <- Gen.anyDouble
b <- Gen.anyDouble if a != b
} yield if (b > a) (a, b) else (b, a)

val genFloats: Gen[Random, (Float, Float)] =
for {
a <- Gen.anyFloat
b <- Gen.anyFloat if a != b
} yield if (b > a) (a, b) else (b, a)

val genInts: Gen[Random, (Int, Int)] =
for {
a <- Gen.anyInt
b <- Gen.anyInt if a != b
} yield if (b > a) (a, b) else (b, a)

val genLongs: Gen[Random, (Long, Long)] =
for {
a <- Gen.anyLong
b <- Gen.anyLong if a != b
} yield if (b > a) (a, b) else (b, a)
}
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/zio/random/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ package object random {
}

/**
* Generates a pseudo-random boolean.
* generates a pseudo-random boolean.
*/
val nextBoolean: URIO[Random, Boolean] =
ZIO.accessM(_.get.nextBoolean)
Expand Down
1 change: 1 addition & 0 deletions core/shared/src/main/scala/zio/stm/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ package object stm extends EitherCompat {
type TaskSTM[+A] = ZSTM[Any, Throwable, A]
type TRef[A] = ZTRef[Nothing, Nothing, A, A]
type ETRef[+E, A] = ZTRef[E, E, A, A]

}
Loading