Implement "proper numeric comparisons" support in JVM BE

This commit is contained in:
Dmitry Petrov
2018-02-08 09:50:22 +03:00
parent e23a48e8d0
commit a790195808
18 changed files with 652 additions and 138 deletions
@@ -0,0 +1,11 @@
// LANGUAGE_VERSION: 1.3
fun less(x: Comparable<Float>, y: Float) = x is Float && x < y
fun less(x: Comparable<Double>, y: Double) = x is Double && x < y
fun box(): String {
if (less(-0.0F, 0.0F)) return "Fail F"
if (less(-0.0, 0.0)) return "Fail D"
return "OK"
}
@@ -0,0 +1,27 @@
// LANGUAGE_VERSION: 1.3
fun myEquals(a: Double?, b: Double?) = a == b
fun myEquals1(a: Double?, b: Double) = a == b
fun myEquals2(a: Double, b: Double?) = a == b
fun myEquals0(a: Double, b: Double) = a == b
fun box(): String {
if (!myEquals(null, null)) return "fail 1"
if (myEquals(null, 0.0)) return "fail 2"
if (myEquals(0.0, null)) return "fail 3"
if (!myEquals(0.0, 0.0)) return "fail 4"
if (myEquals1(null, 0.0)) return "fail 5"
if (!myEquals1(0.0, 0.0)) return "fail 6"
if (myEquals2(0.0, null)) return "fail 7"
if (!myEquals2(0.0, 0.0)) return "fail 8"
if (!myEquals0(0.0, 0.0)) return "fail 9"
return "OK"
}
@@ -0,0 +1,25 @@
// LANGUAGE_VERSION: 1.3
fun testF(x: Any) =
when (x) {
!is Float -> "!Float"
0.0F -> "0.0"
else -> "other"
}
fun testD(x: Any) =
when (x) {
!is Double -> "!Double"
0.0 -> "0.0"
else -> "other"
}
fun box(): String {
val tf = testF(0.0F)
if (tf != "0.0") return "Fail 1: $tf"
val td = testD(0.0)
if (td != "0.0") return "Fail 2: $td"
return "OK"
}
@@ -0,0 +1,13 @@
// LANGUAGE_VERSION: 1.3
fun ne(x: Any, y: Any) = x is Double && y is Float && x != y
fun lt(x: Any, y: Any) = x is Double && y is Float && x < y
fun gt(x: Any, y: Any) = x is Double && y is Float && x > y
fun box(): String {
if (ne(0.0, -0.0F)) return "fail 1"
if (lt(0.0, -0.0F)) return "fail 2"
if (gt(0.0, -0.0F)) return "fail 3"
return "OK"
}
@@ -0,0 +1,76 @@
// LANGUAGE_VERSION: 1.3
// IGNORE_BACKEND: JS
fun eqDI(x: Any?, y: Any?) = x is Double? && y is Int? && x == y
fun eqDL(x: Any?, y: Any?) = x is Double? && y is Long? && x == y
fun eqID(x: Any?, y: Any?) = x is Int? && y is Double? && x == y
fun eqLD(x: Any?, y: Any?) = x is Long? && y is Double? && x == y
fun eqFI(x: Any?, y: Any?) = x is Float? && y is Int? && x == y
fun eqFL(x: Any?, y: Any?) = x is Float? && y is Long? && x == y
fun eqIF(x: Any?, y: Any?) = x is Int? && y is Float? && x == y
fun eqLF(x: Any?, y: Any?) = x is Long? && y is Float? && x == y
fun testNullNull() {
if (!eqDI(null, null)) throw Exception()
if (!eqDL(null, null)) throw Exception()
if (!eqID(null, null)) throw Exception()
if (!eqLD(null, null)) throw Exception()
if (!eqFI(null, null)) throw Exception()
if (!eqFL(null, null)) throw Exception()
if (!eqIF(null, null)) throw Exception()
if (!eqLF(null, null)) throw Exception()
}
fun testNull0() {
if (eqDI(null, 0)) throw Exception()
if (eqDL(null, 0L)) throw Exception()
if (eqID(null, 0.0)) throw Exception()
if (eqLD(null, 0.0)) throw Exception()
if (eqFI(null, 0)) throw Exception()
if (eqFL(null, 0L)) throw Exception()
if (eqIF(null, 0.0F)) throw Exception()
if (eqLF(null, 0.0F)) throw Exception()
}
fun test0Null() {
if (eqDI(0.0, null)) throw Exception()
if (eqDL(0.0, null)) throw Exception()
if (eqID(0, null)) throw Exception()
if (eqLD(0L, null)) throw Exception()
if (eqFI(0.0F, null)) throw Exception()
if (eqFL(0.0F, null)) throw Exception()
if (eqIF(0, null)) throw Exception()
if (eqLF(0L, null)) throw Exception()
}
fun testPlusMinus0() {
if (!eqDI(-0.0, 0)) throw Exception()
if (!eqDL(-0.0, 0L)) throw Exception()
if (!eqID(0, -0.0)) throw Exception()
if (!eqLD(0L, -0.0)) throw Exception()
if (!eqFI(-0.0F, 0)) throw Exception()
if (!eqFL(-0.0F, 0L)) throw Exception()
if (!eqIF(0, -0.0F)) throw Exception()
if (!eqLF(0L, -0.0F)) throw Exception()
}
fun test01() {
if (eqDI(0.0, 1)) throw Exception()
if (eqDL(0.0, 1L)) throw Exception()
if (eqID(0, 1.0)) throw Exception()
if (eqLD(0L, 1.0)) throw Exception()
if (eqFI(0.0F, 1)) throw Exception()
if (eqFL(0.0F, 1L)) throw Exception()
if (eqIF(0, 1.0F)) throw Exception()
if (eqLF(0L, 1.0F)) throw Exception()
}
fun box(): String {
testNullNull()
testNull0()
test0Null()
testPlusMinus0()
test01()
return "OK"
}