diff options
author | Roland <rk@rkuhn.info> | 2012-09-05 18:50:58 +0200 |
---|---|---|
committer | Roland <rk@rkuhn.info> | 2012-09-05 18:50:58 +0200 |
commit | 9da135880d0b78b440f4ce62dd8b7dc156571d71 (patch) | |
tree | 061ff73d180c7a69ea3e7251f741fbd5329071d5 /test/files/jvm/duration-tck.scala | |
parent | adf2d3632b07eef4fc2303aef994e66584a73f49 (diff) | |
download | scala-9da135880d0b78b440f4ce62dd8b7dc156571d71.tar.gz scala-9da135880d0b78b440f4ce62dd8b7dc156571d71.tar.bz2 scala-9da135880d0b78b440f4ce62dd8b7dc156571d71.zip |
several fixes to scala.concurrent.util.Duration
- add test cases (migrated from Akka sources)
- add overflow checking (will throw IllegalArgumentException instead of
giving wrong results)
- make string parsing more precise when giving >100days in nanoseconds
- make method signatures more precise in retaining FiniteDuration
throughout calculations
- fix mul/div of infinities by negative number
- add Ordering for Deadline (was accidentally left out earlier)
Diffstat (limited to 'test/files/jvm/duration-tck.scala')
-rw-r--r-- | test/files/jvm/duration-tck.scala | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/test/files/jvm/duration-tck.scala b/test/files/jvm/duration-tck.scala new file mode 100644 index 0000000000..679712aa59 --- /dev/null +++ b/test/files/jvm/duration-tck.scala @@ -0,0 +1,107 @@ +/** + * Copyright (C) 2012 Typesafe Inc. <http://www.typesafe.com> + */ + +import scala.concurrent.util._ +import duration._ +import scala.reflect._ +import java.util.concurrent.TimeUnit._ + +object Test extends App { + + implicit class Assert(val left: Any) extends AnyVal { + def =!=(right: Any) = assert(left == right, s"$left was not equal to $right") + } + + def intercept[T <: Exception : ClassTag](code: => Unit) = + try { code; assert(false, "did not throw expected exception " + classTag[T]) } + catch { + case ex: Exception => if (classTag[T].runtimeClass isAssignableFrom ex.getClass) () else throw ex + } + + { // test field ops + val zero = 0 seconds + val one = 1 second + val two = one + one + val three = 3 * one + (0 * one) =!= (zero) + (2 * one) =!= (two) + (three - two) =!= (one) + (three / 3) =!= (one) + (two / one) =!= (2) + (one + zero) =!= (one) + (one / 1000000) =!= (1.micro) + } + + { // test infinities + val one = 1.second + val inf = Duration.Inf + val minf = Duration.MinusInf + (-inf) =!= (minf) + intercept[IllegalArgumentException] { minf + inf } + intercept[IllegalArgumentException] { inf - inf } + intercept[IllegalArgumentException] { inf + minf } + intercept[IllegalArgumentException] { minf - minf } + (inf + inf) =!= (inf) + (inf - minf) =!= (inf) + (minf - inf) =!= (minf) + (minf + minf) =!= (minf) + assert(inf == inf) + assert(minf == minf) + inf.compareTo(inf) =!= (0) + inf.compareTo(one) =!= (1) + minf.compareTo(minf) =!= (0) + minf.compareTo(one) =!= (-1) + assert(inf != minf) + assert(minf != inf) + assert(one != inf) + assert(minf != one) + inf =!= (minf * -1d) + inf =!= (minf / -1d) + } + + { // test overflow protection + for (unit ← Seq(DAYS, HOURS, MINUTES, SECONDS, MILLISECONDS, MICROSECONDS, NANOSECONDS)) { + val x = unit.convert(Long.MaxValue, NANOSECONDS) + val dur = Duration(x, unit) + val mdur = Duration(-x, unit) + -mdur =!= (dur) + intercept[IllegalArgumentException] { Duration(x + 10000000d, unit) } + intercept[IllegalArgumentException] { Duration(-x - 10000000d, unit) } + if (unit != NANOSECONDS) { + intercept[IllegalArgumentException] { Duration(x + 1, unit) } + intercept[IllegalArgumentException] { Duration(-x - 1, unit) } + } + intercept[IllegalArgumentException] { dur + 1.day } + intercept[IllegalArgumentException] { mdur - 1.day } + intercept[IllegalArgumentException] { dur * 1.1 } + intercept[IllegalArgumentException] { mdur * 1.1 } + intercept[IllegalArgumentException] { dur * 2.1 } + intercept[IllegalArgumentException] { mdur * 2.1 } + intercept[IllegalArgumentException] { dur / 0.9 } + intercept[IllegalArgumentException] { mdur / 0.9 } + intercept[IllegalArgumentException] { dur / 0.4 } + intercept[IllegalArgumentException] { mdur / 0.4 } + Duration(x + unit.toString.toLowerCase) + Duration("-" + x + unit.toString.toLowerCase) + intercept[IllegalArgumentException] { Duration("%.0f".format(x + 10000000d) + unit.toString.toLowerCase) } + intercept[IllegalArgumentException] { Duration("-%.0f".format(x + 10000000d) + unit.toString.toLowerCase) } + } + } + + { // test Deadline + val dead = 2.seconds.fromNow + val dead2 = 2 seconds fromNow + // view bounds vs. very local type inference vs. operator precedence: sigh + assert(dead.timeLeft > (1 second: Duration)) + assert(dead2.timeLeft > (1 second: Duration)) + Thread.sleep(1.second.toMillis) + assert(dead.timeLeft < (1 second: Duration)) + assert(dead2.timeLeft < (1 second: Duration)) + } + + { // check statically retaining finite-ness + val d: FiniteDuration = 1.second * 2 / 1.4 mul 1.1 div 2.1 plus 3.seconds minus 1.millisecond min 1.second max 1.second + } + +} |