From 4e3cb5628f010b37f8cea94e54b80d3f702299e1 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Sat, 30 Nov 2024 18:59:36 +0100 Subject: [PATCH] Add test for System.arraycopy with different source/target indexes --- .../teavm/classlib/java/lang/SystemTest.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/src/test/java/org/teavm/classlib/java/lang/SystemTest.java b/tests/src/test/java/org/teavm/classlib/java/lang/SystemTest.java index a76b46b25..8e434c9a2 100644 --- a/tests/src/test/java/org/teavm/classlib/java/lang/SystemTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/lang/SystemTest.java @@ -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 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 }; @@ -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" };