diff options
author | Jason Zaugg <jzaugg@gmail.com> | 2015-01-16 14:27:46 +1000 |
---|---|---|
committer | Jason Zaugg <jzaugg@gmail.com> | 2015-01-20 18:40:56 +1000 |
commit | c8393fdf44862cf09fca6ef4bc7899e7c0386f79 (patch) | |
tree | 8d9e0caf096699c96f0ab6027b413827c1d0e520 /test/junit | |
parent | 3d76836bc81c3ec183e83ee186e447ff212507d0 (diff) | |
download | scala-c8393fdf44862cf09fca6ef4bc7899e7c0386f79.tar.gz scala-c8393fdf44862cf09fca6ef4bc7899e7c0386f79.tar.bz2 scala-c8393fdf44862cf09fca6ef4bc7899e7c0386f79.zip |
SI-9087 Fix min/max of reversed Double/Float orderings
As diagnosed by the reporter, we needed additional overrides
due to the way these orderings are implemented.
I've added tests to show other methods and other orderings
are working correctly.
After writing that, I found a scalacheck test related to
NaN handling that also covers `Ordering`. I had to correct
the assertion in the tests of `reverse.{min,max}`.
Diffstat (limited to 'test/junit')
-rw-r--r-- | test/junit/scala/math/OrderingTest.scala | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/test/junit/scala/math/OrderingTest.scala b/test/junit/scala/math/OrderingTest.scala new file mode 100644 index 0000000000..218622b8b4 --- /dev/null +++ b/test/junit/scala/math/OrderingTest.scala @@ -0,0 +1,61 @@ +package scala.math + +import org.junit.Assert._ +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +@RunWith(classOf[JUnit4]) +class OrderingTest { + + /* Test for SI-9077 */ + @Test + def testReverseOrdering { + def check[T: Ordering](t1: T, t2: T): Unit = { + val O = Ordering[T] + val R = O.reverse + assertEquals(O.min(t1, t2), R.max(t1, t2)) + assertEquals(O.max(t1, t2), R.min(t1, t2)) + + assertEquals(O.lteq(t1, t2), R.lteq(t2, t1)) + assertEquals(O.lt(t1, t2), R.lt(t2, t1)) + assertEquals(O.gteq(t1, t2), R.gteq(t2, t1)) + assertEquals(O.gt(t1, t2), R.gt(t2, t1)) + assertEquals(O.compare(t1, t2), R.compare(t2, t1)) + + assertEquals(O.equiv(t1, t2), R.equiv(t1, t2)) + + assertEquals(O.on((x: T) => x).min(t1, t2), R.on((x: T) => x).max(t1, t2)) + + assertEquals(O.tryCompare(t1, t2), R.tryCompare(t2, t1)) + + assertEquals(O.mkOrderingOps(t1).<(t2), R.mkOrderingOps(t2).<(t1)) + assertEquals(O.mkOrderingOps(t1).<=(t2), R.mkOrderingOps(t2).<=(t1)) + assertEquals(O.mkOrderingOps(t1).>(t2), R.mkOrderingOps(t2).>(t1)) + assertEquals(O.mkOrderingOps(t1).>=(t2), R.mkOrderingOps(t2).>=(t1)) + + assertEquals(O.mkOrderingOps(t1).min(t2), R.mkOrderingOps(t1).max(t2)) + assertEquals(O.mkOrderingOps(t1).max(t2), R.mkOrderingOps(t1).min(t2)) + } + def checkAll[T: Ordering](ts: T*): Unit = { + for (t1 <- ts; t2 <- ts) check(t1, t2) + } + checkAll[Unit](()) + checkAll[Boolean](true, false) + checkAll[Byte](Byte.MinValue, -1.toByte, 0.toByte, 1.toByte, Byte.MaxValue) + checkAll[Char](Char.MinValue, -1.toChar, 0.toChar, 1.toChar, Char.MaxValue) + checkAll[Short](Short.MinValue, -1, 0, 1, Short.MaxValue) + checkAll[Int](Int.MinValue, -1, 0, 1, Int.MaxValue) + checkAll[Double](Double.MinValue, -1, -0, 0, 1, Double.MaxValue) + checkAll[Float](Float.MinValue, -1, -0, 0, 1, Float.MaxValue) + + checkAll[BigInt](Int.MinValue, -1, 0, 1, Int.MaxValue) + checkAll[BigDecimal](Int.MinValue, -1, -0, 1, Int.MaxValue) + checkAll[String]("", "a", "b", "bb") + checkAll[String]("", "a", "b", "bb") + checkAll[Option[Int]](None, Some(1), Some(2)) + checkAll[Iterable[Int]](Nil, List(1), List(1, 2)) + checkAll[(Int, Int)]((1, 2), (1, 3), (4, 5)) + } +} + |