KT-8283: fix Kotlin.equals method to compare arrays by reference
This commit is contained in:
@@ -69,4 +69,8 @@ public final class EqualsTest extends AbstractExpressionTest {
|
||||
public void testSuperEquals() throws Exception {
|
||||
checkFooBoxIsOk();
|
||||
}
|
||||
|
||||
public void testArrays() throws Exception {
|
||||
checkFooBoxIsOk();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,30 @@ fun <T> assertNotEquals(illegal: T, actual: T, message: String? = null) {
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> assertArrayEquals(expected: Array<out T>, actual: Array<out T>, message: String? = null) {
|
||||
if (!arraysEqual(expected, actual)) {
|
||||
val msg = if (message == null) "" else (" message = '" + message + "',")
|
||||
fail("Unexpected array:$msg expected = '$expected', actual = '$actual'")
|
||||
}
|
||||
}
|
||||
|
||||
private fun <T> arraysEqual(first: Array<out T>, second: Array<out T>): Boolean {
|
||||
if (first === second) return true
|
||||
if (first.size != second.size) return false
|
||||
for (index in first.indices) {
|
||||
if (!equal(first[index], second[index])) return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
private fun equal(first: Any?, second: Any?) =
|
||||
if (first is Array<*> && second is Array<*>) {
|
||||
arraysEqual(first, second)
|
||||
}
|
||||
else {
|
||||
first == second
|
||||
}
|
||||
|
||||
fun assertTrue(actual: Boolean, message: String? = null) = assertEquals(true, actual, message)
|
||||
|
||||
fun assertFalse(actual: Boolean, message: String? = null) = assertEquals(false, actual, message)
|
||||
|
||||
+1
-1
@@ -29,7 +29,7 @@ fun box(): String {
|
||||
|
||||
if (EmptyEnum.values().size != 0) return "EmptyEnum.values().size != 0"
|
||||
|
||||
if (A.values() != arrayOf(A.a, A.b, A.c)) return "Wrong A.values(): " + A.values().toString()
|
||||
if (A.values().asList() != listOf(A.a, A.b, A.c)) return "Wrong A.values(): " + A.values().toString()
|
||||
|
||||
if (A.c.toString() != "c") return "A.c.toString() != c, it: ${A.c.toString()}"
|
||||
if (A.valueOf("b") != A.b) return "A.valueOf('b') != A.b"
|
||||
|
||||
@@ -9,7 +9,8 @@ fun box(): String {
|
||||
success("10 as Any") { assertEquals<Any>(10, 10 as Any) }
|
||||
success("\"abc\" as Any") { assertEquals<Any>("abc", "abc" as Any) }
|
||||
success("\"abc\" as Any") { assertEquals<Any>(true, true as Any) }
|
||||
success("arrayOf(1, 2) as Any") { assertEquals<Any>(arrayOf(1, 2), arrayOf(1, 2) as Any) }
|
||||
val array = arrayOf(1, 2)
|
||||
success("arrayOf(1, 2) as Any") { assertEquals<Any>(array, array as Any) }
|
||||
success("{ 0 } as Any") { { 0 } as Any }
|
||||
success("a as Any") { assertEquals<Any>(a, a as Any) }
|
||||
success("object{} as Any") { object{} as Any }
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
val a = arrayOf(1, 2, 3)
|
||||
val b = arrayOf(1, 2, 3)
|
||||
val c = a
|
||||
|
||||
if (a == b) return "fail1"
|
||||
if (a != c) return "fail2"
|
||||
if (c == b) return "fail3"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -14,7 +14,7 @@ fun box(): String {
|
||||
val expected = test.second
|
||||
val result = testInput.splitWithRegex(regexp)
|
||||
|
||||
if (result != expected) return "Wrong result for '$regexp' -- Expected: $expected | Actual: $result"
|
||||
if (result.asList() != expected.asList()) return "Wrong result for '$regexp' -- Expected: $expected | Actual: $result"
|
||||
}
|
||||
|
||||
for (test in tests) {
|
||||
@@ -23,7 +23,7 @@ fun box(): String {
|
||||
val expected = Array(limit) { test.second[it] }
|
||||
val result = testInput.splitWithRegex(regexp, limit)
|
||||
|
||||
if (result != expected) return "Wrong result for '$regexp' -- Expected: $expected | Actual: $result"
|
||||
if (result.asList() != expected.asList()) return "Wrong result for '$regexp' -- Expected: $expected | Actual: $result"
|
||||
}
|
||||
|
||||
return "OK"
|
||||
|
||||
@@ -21,7 +21,11 @@ inline fun moveTo(source: Array<Int>, sink: Array<Int>): PairArray<Int, Int> {
|
||||
|
||||
fun box(): String {
|
||||
val expected = PairArray<Int, Int>(arrayOf(), arrayOf(1,2,3,4))
|
||||
assertEquals(expected, moveTo(arrayOf(3, 4), arrayOf(1, 2)))
|
||||
assertTrue(expected.deepEquals(moveTo(arrayOf(3, 4), arrayOf(1, 2))))
|
||||
|
||||
return "OK"
|
||||
}
|
||||
|
||||
fun <T, R> PairArray<T, R>.deepEquals(other: PairArray<T, R>): Boolean {
|
||||
return fst.asList() == other.fst.asList() && snd.asList() == other.snd.asList()
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(arrayOf(1, 2), arrayOf(fizz(1), buzz(2)))
|
||||
assertArrayEquals(arrayOf(1, 2), arrayOf(fizz(1), buzz(2)))
|
||||
assertEquals("fizz(1);buzz(2);", pullLog())
|
||||
|
||||
return "OK"
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(arrayOf(1, 2, 3, 4), arrayOf(fizz(1), buzz(2), fizz(3), buzz(4)))
|
||||
assertArrayEquals(arrayOf(1, 2, 3, 4), arrayOf(fizz(1), buzz(2), fizz(3), buzz(4)))
|
||||
assertEquals("fizz(1);buzz(2);fizz(3);buzz(4);", pullLog())
|
||||
|
||||
return "OK"
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(arrayOf(arrayOf(1, 2), arrayOf(3, 4)), arrayOf(arrayOf(fizz(1), buzz(2)), arrayOf(fizz(3), buzz(4))))
|
||||
assertArrayEquals(arrayOf(arrayOf(1, 2), arrayOf(3, 4)), arrayOf(arrayOf(fizz(1), buzz(2)), arrayOf(fizz(3), buzz(4))))
|
||||
assertEquals("fizz(1);buzz(2);fizz(3);buzz(4);", pullLog())
|
||||
|
||||
return "OK"
|
||||
|
||||
+1
-1
@@ -42,7 +42,7 @@ fun box(): Boolean {
|
||||
assertEquals(true, a.isEmpty(), "a.isEmpty()")
|
||||
|
||||
|
||||
assertEquals(arrayOf(1, 500, 2, 3), list.toTypedArray(), "list.toTypedArray()")
|
||||
assertArrayEquals(arrayOf(1, 500, 2, 3), list.toTypedArray(), "list.toTypedArray()")
|
||||
assertEquals("[1,500,2,3]", JSON.stringify(list), "JSON.stringify(list)")
|
||||
assertEquals("[1, 500, 2, 3]", list.toString(), "list.toString()")
|
||||
return true;
|
||||
|
||||
-4
@@ -53,10 +53,6 @@
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Array.isArray(obj1)) {
|
||||
return Kotlin.arrayEquals(obj1, obj2);
|
||||
}
|
||||
|
||||
if (typeof obj1 == "object" && typeof obj1.equals_za3rmp$ === "function") {
|
||||
return obj1.equals_za3rmp$(obj2);
|
||||
}
|
||||
|
||||
@@ -3,8 +3,19 @@ package foo
|
||||
class Fail(message: String) : Exception(message)
|
||||
|
||||
fun test(testName: String, actual: Any, expectedAsString: String) {
|
||||
val expected = eval("[$expectedAsString]")
|
||||
assertEquals(expected, actual)
|
||||
val expected = eval("[$expectedAsString]") as Array<in Any>
|
||||
val expectedJs: dynamic = expected
|
||||
val actualJs: dynamic = actual
|
||||
if (expectedJs.length != actualJs.length) {
|
||||
fail("Lengths do not match: ${expectedJs} vs. ${actualJs}")
|
||||
}
|
||||
for (index in 0..(expectedJs.length)) {
|
||||
val expectedElem = expectedJs[index] as Any?
|
||||
val actualElem = actualJs[index] as Any?
|
||||
if (expectedElem != actualElem) {
|
||||
fail("Content do not match: ${expectedJs} vs. ${actualJs}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
|
||||
Reference in New Issue
Block a user