Skip to content

Commit

Permalink
Add test for System.arraycopy with different source/target indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
konsoletyper committed Nov 30, 2024
1 parent e4c3268 commit 4e3cb56
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions tests/src/test/java/org/teavm/classlib/java/lang/SystemTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,6 +45,28 @@ public void copiesArray() {
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 };
Expand All @@ -54,6 +77,18 @@ public void copiesPrimitiveArray() {
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" };
Expand Down

0 comments on commit 4e3cb56

Please sign in to comment.