Fix Double and Float equality/hashCode in data classes

#KT-5818 Fixed
This commit is contained in:
Alexander Udalov
2014-10-16 19:09:43 +04:00
parent f632a34959
commit 1edaf43051
6 changed files with 100 additions and 21 deletions
@@ -0,0 +1,30 @@
import java.util.HashSet
val NAN = java.lang.Double.NaN
data class A(val x: Double)
fun box(): String {
if (A(+0.0) == A(-0.0)) return "Fail: +0.0 == -0.0"
if (A(+0.0).hashCode() == A(-0.0).hashCode()) return "Fail: hash(+0.0) == hash(-0.0)"
if (A(NAN) != A(NAN)) return "Fail: NaN != NaN"
if (A(NAN).hashCode() != A(NAN).hashCode()) return "Fail: hash(NaN) != hash(NaN)"
val s = HashSet<A>()
for (times in 1..5) {
s.add(A(3.14))
s.add(A(+0.0))
s.add(A(-0.0))
s.add(A(-2.72))
s.add(A(NAN))
}
if (A(3.14) !in s) return "Fail: 3.14 not found"
if (A(+0.0) !in s) return "Fail: +0.0 not found"
if (A(-0.0) !in s) return "Fail: -0.0 not found"
if (A(-2.72) !in s) return "Fail: -2.72 not found"
if (A(NAN) !in s) return "Fail: NaN not found"
return if (s.size == 5) "OK" else "Fail $s"
}
@@ -0,0 +1,30 @@
import java.util.HashSet
val NAN = java.lang.Float.NaN
data class A(val x: Float)
fun box(): String {
if (A(+0f) == A(-0f)) return "Fail: +0 == -0"
if (A(+0f).hashCode() == A(-0f).hashCode()) return "Fail: hash(+0) == hash(-0)"
if (A(NAN) != A(NAN)) return "Fail: NaN != NaN"
if (A(NAN).hashCode() != A(NAN).hashCode()) return "Fail: hash(NaN) != hash(NaN)"
val s = HashSet<A>()
for (times in 1..5) {
s.add(A(3.14f))
s.add(A(+0f))
s.add(A(-0f))
s.add(A(-2.72f))
s.add(A(NAN))
}
if (A(3.14f) !in s) return "Fail: 3.14 not found"
if (A(+0f) !in s) return "Fail: +0 not found"
if (A(-0f) !in s) return "Fail: -0 not found"
if (A(-2.72f) !in s) return "Fail: -2.72 not found"
if (A(NAN) !in s) return "Fail: NaN not found"
return if (s.size == 5) "OK" else "Fail $s"
}