diff options
author | Lukas Rytz <lukas.rytz@gmail.com> | 2016-06-01 23:25:35 +0200 |
---|---|---|
committer | Lukas Rytz <lukas.rytz@gmail.com> | 2016-06-02 08:36:16 +0200 |
commit | 7952cd1651b6ed1fe0cd68198e49cf90423242d8 (patch) | |
tree | 301b93667d810ff28f122f17252f8c4940da2147 /test | |
parent | 128ac65991210fee3d5e273e30c9f31322e1a585 (diff) | |
download | scala-7952cd1651b6ed1fe0cd68198e49cf90423242d8.tar.gz scala-7952cd1651b6ed1fe0cd68198e49cf90423242d8.tar.bz2 scala-7952cd1651b6ed1fe0cd68198e49cf90423242d8.zip |
Fix comparisons involving NaN
Floating point comparisons involving NaN should always return false,
except for !=. Fixes a regression introduced by #4963.
Diffstat (limited to 'test')
-rw-r--r-- | test/junit/scala/lang/primitives/NaNTest.scala | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/test/junit/scala/lang/primitives/NaNTest.scala b/test/junit/scala/lang/primitives/NaNTest.scala new file mode 100644 index 0000000000..f4c4258395 --- /dev/null +++ b/test/junit/scala/lang/primitives/NaNTest.scala @@ -0,0 +1,38 @@ +package scala.lang.primitives + +import org.junit.Assert._ +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +import scala.tools.testing.RunTesting + +@RunWith(classOf[JUnit4]) +class NaNTest extends RunTesting { + + @Test + def compNaNFalse(): Unit = { + def code(tp: String) = + s"""val n = $tp.NaN + |def ne(x: $tp, y: $tp) = x != y + |val fs: List[($tp, $tp) => Boolean] = List(_ < _, _ <= _, _ > _, _ >= _, _ == _, (x, y) => !ne(x, y)) + |val vs = List[$tp](n, 1, -1, 0) + |for (f <- fs; v <- vs; (x, y) <- List((n, v), (v, n))) yield f(x, y) + """.stripMargin + + runner.run[List[Boolean]](code("Double")).foreach(assertFalse) + runner.run[List[Boolean]](code("Float")).foreach(assertFalse) + } + + @Test + def genericEqNe(): Unit = { + def code(tp: String) = + s"""def a[T](x: T, y: T) = x == y + |def b[T](x: T, y: T) = x != y + |val n = $tp.NaN + |a(n, n) :: a(n, 0) :: a (0, n) :: !b(n, n) :: !b(n, 0) :: !b(0, n) :: Nil + """.stripMargin + runner.run[List[Boolean]](code("Double")).foreach(assertFalse) + runner.run[List[Boolean]](code("Float")).foreach(assertFalse) + } +} |