Add test for System.arraycopy with different source/target indexes

This commit is contained in:
Alexey Andreev 2024-11-30 18:59:36 +01:00
parent e4c32681b9
commit 4e3cb5628f

View File

@ -15,6 +15,7 @@
*/
package org.teavm.classlib.java.lang;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
@ -44,6 +45,28 @@ public class SystemTest {
assertSame(a, dest[2]);
}
@Test
public void copiesArrayPart() {
var a = new Object();
var b = new Object();
var src = new Object[] { a, b, a };
var dest = new Object[5];
System.arraycopy(src, 0, dest, 1, 3);
assertNull(dest[0]);
assertSame(a, dest[1]);
assertSame(b, dest[2]);
assertSame(a, dest[3]);
assertNull(dest[4]);
dest = new Object[5];
System.arraycopy(src, 1, dest, 0, 2);
assertSame(b, dest[0]);
assertSame(a, dest[1]);
assertNull(dest[2]);
assertNull(dest[3]);
assertNull(dest[4]);
}
@Test
public void copiesPrimitiveArray() {
int[] src = { 23, 24, 25 };
@ -54,6 +77,18 @@ public class SystemTest {
assertEquals(25, dest[2]);
}
@Test
public void copiesPrimitiveArrayPart() {
var src = new int[] { 23, 24, 25 };
var dest = new int[5];
System.arraycopy(src, 0, dest, 1, 3);
assertArrayEquals(new int[] { 0, 23, 24, 25, 0 }, dest);
dest = new int[5];
System.arraycopy(src, 1, dest, 0, 2);
assertArrayEquals(new int[] { 24, 25, 0, 0, 0 }, dest);
}
@Test
public void copiesToSubclassArray() {
String[] src = { "foo", "bar", "baz" };