Path: blob/master/test/hotspot/jtreg/compiler/arraycopy/TestArrayCopyUtils.java
41152 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package compiler.arraycopy;2425import java.lang.annotation.Retention;26import java.lang.annotation.RetentionPolicy;27import java.lang.reflect.Method;28import java.lang.reflect.Modifier;29import java.util.HashMap;3031abstract class TestArrayCopyUtils {32public enum ArraySrc {33SMALL,34LARGE,35ZERO36}3738public enum ArrayDst {39NONE,40NEW,41SRC42}4344static class A {45}4647static class B extends A {48}4950static final A[] small_a_src = new A[5];51static final A[] large_a_src = new A[10];52static final A[] zero_a_src = new A[0];53static final int[] small_int_src = new int[5];54static final int[] large_int_src = new int[10];55static final int[] zero_int_src = new int[0];56static final Object[] small_object_src = new Object[5];57static Object src;5859@Retention(RetentionPolicy.RUNTIME)60@interface Args {61ArraySrc src();62ArrayDst dst() default ArrayDst.NONE;63int[] extra_args() default {};64}6566final HashMap<String,Method> tests = new HashMap<>();67{68for (Method m : this.getClass().getDeclaredMethods()) {69if (m.getName().matches("m[0-9]+(_check)?")) {70assert(Modifier.isStatic(m.getModifiers())) : m;71tests.put(m.getName(), m);72}73}74}7576boolean success = true;7778void doTest(String name) throws Exception {79Method m = tests.get(name);80Method m_check = tests.get(name + "_check");81Class[] paramTypes = m.getParameterTypes();82Object[] params = new Object[paramTypes.length];83Class retType = m.getReturnType();84boolean isIntArray = (retType.isPrimitive() && !retType.equals(Void.TYPE)) ||85(retType.equals(Void.TYPE) && paramTypes[0].getComponentType().isPrimitive()) ||86(retType.isArray() && retType.getComponentType().isPrimitive());8788Args args = m.getAnnotation(Args.class);8990Object src = null;91switch(args.src()) {92case SMALL: {93if (isIntArray) {94src = small_int_src;95} else {96src = small_a_src;97}98break;99}100case LARGE: {101if (isIntArray) {102src = large_int_src;103} else {104src = large_a_src;105}106break;107}108case ZERO: {109if (isIntArray) {110src = zero_int_src;111} else {112src = zero_a_src;113}114break;115}116}117118for (int i = 0; i < 20000; i++) {119boolean failure = false;120121int p = 0;122123if (params.length > 0) {124if (isIntArray) {125params[0] = ((int[])src).clone();126} else {127params[0] = ((A[])src).clone();128}129p++;130}131132if (params.length > 1) {133switch(args.dst()) {134case NEW: {135if (isIntArray) {136params[1] = new int[((int[])params[0]).length];137} else {138params[1] = new A[((A[])params[0]).length];139}140p++;141break;142}143case SRC: {144params[1] = params[0];145p++;146break;147}148case NONE: break;149}150}151152for (int j = 0; j < args.extra_args().length; j++) {153params[p+j] = args.extra_args()[j];154}155156Object res = m.invoke(null, params);157158if (retType.isPrimitive() && !retType.equals(Void.TYPE)) {159int s = (int)res;160int sum = 0;161int[] int_res = (int[])src;162for (int j = 0; j < int_res.length; j++) {163sum += int_res[j];164}165failure = (s != sum);166if (failure) {167System.out.println("Test " + name + " failed: result = " + s + " != " + sum);168}169} else {170Object dest = null;171if (!retType.equals(Void.TYPE)) {172dest = res;173} else {174dest = params[1];175}176177if (m_check != null) {178failure = (boolean)m_check.invoke(null, new Object[] { src, dest });179} else {180if (isIntArray) {181int[] int_res = (int[])src;182int[] int_dest = (int[])dest;183for (int j = 0; j < int_res.length; j++) {184if (int_res[j] != int_dest[j]) {185System.out.println("Test " + name + " failed for " + j + " src[" + j +"]=" + int_res[j] + ", dest[" + j + "]=" + int_dest[j]);186failure = true;187}188}189} else {190Object[] object_res = (Object[])src;191Object[] object_dest = (Object[])dest;192for (int j = 0; j < object_res.length; j++) {193if (object_res[j] != object_dest[j]) {194System.out.println("Test " + name + " failed for " + j + " src[" + j +"]=" + object_res[j] + ", dest[" + j + "]=" + object_dest[j]);195failure = true;196}197}198}199}200}201202if (failure) {203success = false;204break;205}206}207}208209TestArrayCopyUtils() {210for (int i = 0; i < small_a_src.length; i++) {211small_a_src[i] = new A();212}213214for (int i = 0; i < small_int_src.length; i++) {215small_int_src[i] = i;216}217218for (int i = 0; i < large_int_src.length; i++) {219large_int_src[i] = i;220}221222for (int i = 0; i < 5; i++) {223small_object_src[i] = new Object();224}225}226}227228229