Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
Created October 24, 2014 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xuwei-k/16d2ae62e9e04e9fda94 to your computer and use it in GitHub Desktop.
Save xuwei-k/16d2ae62e9e04e9fda94 to your computer and use it in GitHub Desktop.
libraryDependencies += "com.typesafe" % "config" % "1.2.1"
libraryDependencies += "org.scalacheck" %% "scalacheck" % "1.11.6"
scalaVersion := "2.11.2"
import com.typesafe.config.ConfigFactory
import java.util.concurrent.TimeUnit
import org.scalacheck._
import scala.concurrent.duration.Duration
object nano extends Test(TimeUnit.NANOSECONDS, 0)
object micro extends Test(TimeUnit.MICROSECONDS, 1)
object millis extends Test(TimeUnit.MILLISECONDS, 2)
object seconds extends Test(TimeUnit.SECONDS, 3)
object minutes extends Test(TimeUnit.MINUTES, 4)
object hours extends Test(TimeUnit.HOURS, 5)
object days extends Test(TimeUnit.DAYS, 6)
abstract class Test(val unit: TimeUnit, n: Int) extends Properties(unit.toString){
implicit final def durationArbitrary: Arbitrary[Duration] = {
val x = math.pow(1000, n).toLong
val z = 1000000L
Arbitrary(
Gen.choose[Long](
(Int.MinValue.toLong * z) / x,
(Int.MaxValue.toLong * z) / x
).map(Duration(_, unit))
)
}
property("test") = Prop.forAll{ a: Duration =>
val key = "a"
val str = key + " : " + a.toString
val d = ConfigFactory.parseString(str).getDuration(key, unit)
val x = Duration(d, unit)
if(x != a){
println((d, x, a))
}
a == x
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment