Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/jdi/ArrayRangeTest.java
38855 views
/*1* Copyright (c) 2001, 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*/2223/**24* @test25* @bug 443963126* @bug 444872127* @bug 444860328* @summary Test access to ranges within ArrayReferences29*30* @author Robert Field31*32* @run build TestScaffold VMConnection TargetListener TargetAdapter33* @run compile -g ArrayRangeTest.java34* @run main ArrayRangeTest35*/36import com.sun.jdi.*;37import com.sun.jdi.event.*;38import com.sun.jdi.request.*;3940import java.util.*;4142/********** target program **********/4344class ArrayRangeTarg {45static int[] emptyArray = {};46static int[] fullArray = {0, 100, 200, 300, 400};4748public static void main(String[] args) {49System.out.println("Goodbye from ArrayRangeTarg!");50}51}5253/********** test program **********/5455public class ArrayRangeTest extends TestScaffold {56ReferenceType targetClass;5758class Sample {59Sample(String name, ArrayReference arrRef, int[] expected) {60this.name = name;61this.arrRef = arrRef;62this.expected = expected;63}64String name;65ArrayReference arrRef;66int[] expected;67}6869ArrayRangeTest (String args[]) {70super(args);71}7273public static void main(String[] args) throws Exception {74new ArrayRangeTest(args).startTests();75}7677/********** test assist **********/7879String arr(int a[]) {80StringBuffer buf = new StringBuffer();81buf.append('[');82if (a.length > 0) {83buf.append(a[0]);84for (int i = 1; i < a.length; ++i) {85buf.append(',');86buf.append(a[i]);87}88}89buf.append(']');90return buf.toString();91}9293void getValueGood(Sample samp, int index) {94try {95Value val = samp.arrRef.getValue(index);96int ival = ((IntegerValue)val).value();97if (ival != samp.expected[index]) {98failure("FAIL - " + samp.name +99".getValue(" + index + ") - wrong value=" + ival);100} else {101println("pass - " + samp.name +102".getValue(" + index + ") - value=" + ival);103}104} catch (Throwable exc) {105failure("FAIL - " + samp.name +106".getValue(" + index + ") - unexpected: " + exc);107}108}109110void getValueBad(Sample samp, int index) {111try {112Value val = samp.arrRef.getValue(index);113failure("FAIL - " + samp.name +114".getValue(" + index + ") - no expected exception");115} catch (IndexOutOfBoundsException exc) {116println("pass - " + samp.name +117".getValue(" + index + ") - got expected: " + exc);118} catch (Throwable exc) {119failure("FAIL - " + samp.name +120".getValue(" + index + ") - unexpected: " + exc);121}122}123124void getValuesGood(Sample samp) {125String desc = samp.name + ".getValues()";126try {127List vals = samp.arrRef.getValues();128if (vals.size() != samp.expected.length) {129failure("FAIL - " + desc +130" - wrong size=" + vals.size() +131" , expected: " + samp.expected.length);132}133for (int index = 0; index < vals.size(); ++index) {134int ival = ((IntegerValue)vals.get(index)).value();135if (ival != samp.expected[index]) {136failure("FAIL - " + desc +137" - wrong value=" + ival);138return;139}140}141println("pass - " + samp.name + ".getValues())");142} catch (Throwable exc) {143failure("FAIL - " + desc +144" - unexpected: " + exc);145}146}147148void getValuesGood(Sample samp, int index, int length) {149try {150List vals = samp.arrRef.getValues(index, length);151if (vals.size() !=152((length==-1)? (samp.expected.length - index) : length)) {153failure("FAIL - " + samp.name + ".getValues(" +154index + ", " + length + ") - wrong size=" +155vals.size());156}157for (int i = 0; i < vals.size(); ++i) {158int ival = ((IntegerValue)vals.get(i)).value();159if (ival != samp.expected[index + i]) {160failure("FAIL - " + samp.name + ".getValues(" +161index + ", " + length + ") - wrong value=" +162ival);163return;164}165}166println("pass - " + samp.name + ".getValues(" +167index + ", " + length + "))");168} catch (Throwable exc) {169failure("FAIL - " + samp.name + ".getValues(" +170index + ", " + length + ") - unexpected: " + exc);171}172}173174void getValuesBad(Sample samp, int index, int length) {175try {176List vals = samp.arrRef.getValues(index, length);177failure("FAIL - " + samp.name + ".getValues(" +178index + ", " + length + ") - no expected exception");179} catch (IndexOutOfBoundsException exc) {180println("pass - " + samp.name + ".getValue(" +181index + ", " + length + ") - got expected: " + exc);182} catch (Throwable exc) {183failure("FAIL - " + samp.name + ".getValues(" +184index + ", " + length + ") - unexpected: " + exc);185}186}187188void setValueGood(Sample samp, int index, int ival) {189try {190Value val = vm().mirrorOf(ival);191samp.arrRef.setValue(index, val);192println("pass - " + samp.name +193".setValue(" + index + ", ..)");194} catch (Throwable exc) {195failure("FAIL - " + samp.name +196".setValue(" + index + ",...) - unexpected: " + exc);197}198}199200void setValueBad(Sample samp, int index, int ival) {201try {202Value val = vm().mirrorOf(ival);203samp.arrRef.setValue(index, val);204failure("FAIL - " + samp.name +205".setValue(" + index + ", ..) - no expected exception");206} catch (IndexOutOfBoundsException exc) {207println("pass - " + samp.name +208".setValue(" + index + ",...) - got expected: " + exc);209} catch (Throwable exc) {210failure("FAIL - " + samp.name +211".setValue(" + index + ",...) - unexpected: " + exc);212}213}214215void setValuesGood(Sample samp, int[] valArray) {216String desc = samp.name + ".setValues(" + arr(valArray) + ")";217try {218List values = new ArrayList();219for (int i = 0; i < valArray.length; ++i) {220Value val = vm().mirrorOf(valArray[i]);221values.add(val);222}223samp.arrRef.setValues(values);224println("pass - " + desc);225} catch (Throwable exc) {226failure("FAIL - " + desc + " - unexpected: " + exc);227}228}229230void setValuesGood(Sample samp, int index, int[] valArray,231int srcInx, int length) {232String desc = samp.name + ".setValues(" + index + ", " +233arr(valArray) + ", " + srcInx + ", " + length + ")";234try {235List values = new ArrayList();236for (int i = 0; i < valArray.length; ++i) {237Value val = vm().mirrorOf(valArray[i]);238values.add(val);239}240samp.arrRef.setValues(index, values, srcInx, length);241println("pass - " + desc);242} catch (Throwable exc) {243failure("FAIL - " + desc + " - unexpected: " + exc);244}245}246247void setValuesBad(Sample samp, int index, int[] valArray,248int srcInx, int length) {249String desc = samp.name + ".setValues(" + index + ", " +250arr(valArray) + ", " + srcInx + ", " + length + ")";251try {252List values = new ArrayList();253for (int i = 0; i < valArray.length; ++i) {254Value val = vm().mirrorOf(valArray[i]);255values.add(val);256}257samp.arrRef.setValues(index, values, srcInx, length);258failure("FAIL - " + desc + " - no expected exception");259} catch (IndexOutOfBoundsException exc) {260println("pass - " + desc + " - got expected: " + exc);261} catch (Throwable exc) {262failure("FAIL - " + desc + " - unexpected: " + exc);263}264}265266void check(Sample samp, int[] expectArray) {267String desc = samp.name + " - check - " + arr(expectArray);268269try {270List vals = samp.arrRef.getValues();271if (vals.size() != expectArray.length) {272failure("FAIL - " + desc +273" - wrong size=" + vals.size() +274" , expected: " + expectArray.length);275}276for (int index = 0; index < vals.size(); ++index) {277int ival = ((IntegerValue)vals.get(index)).value();278if (ival != expectArray[index]) {279failure("FAIL - " + desc +280" - wrong value=" + ival);281return;282}283}284println("pass - " + desc);285} catch (Throwable exc) {286failure("FAIL - " + desc +287" - unexpected: " + exc);288}289}290291/********** test core **********/292293protected void runTests() throws Exception {294/*295* Get to the top of main() to determine targetClass296*/297BreakpointEvent bpe = startToMain("ArrayRangeTarg");298targetClass = bpe.location().declaringType();299Field fullField = targetClass.fieldByName("fullArray");300Field emptyField = targetClass.fieldByName("emptyArray");301ArrayReference emptyAR = (ArrayReference)targetClass.getValue(emptyField);302ArrayReference fullAR = (ArrayReference)targetClass.getValue(fullField);303Sample full = new Sample("full", fullAR, ArrayRangeTarg.fullArray);304Sample empty = new Sample("empty", emptyAR, ArrayRangeTarg.emptyArray);305306getValueGood(full, 0);307getValueGood(full, 4);308309// index < 0310getValueBad(full, -1);311getValueBad(full, -2);312getValueBad(empty, -1);313getValueBad(empty, -2);314315// index >= length316getValueBad(full, 5);317getValueBad(empty, 0);318getValueBad(empty, 5);319320getValuesGood(full);321getValuesGood(empty);322323getValuesGood(full, 0, 5);324getValuesGood(full, 0, 4);325getValuesGood(full, 1, 4);326getValuesGood(full, 5, 0);327getValuesGood(full, 0, 0);328getValuesGood(full, 0, -1);329getValuesGood(full, 1, -1);330getValuesGood(full, 5, -1);331332getValuesGood(empty, 0, 0);333getValuesGood(empty, 0, -1);334335// index < 0336getValuesBad(full, -1, 0);337getValuesBad(full, -1, 3);338getValuesBad(full, -1, -1);339getValuesBad(empty, -1, 0);340getValuesBad(full, -2, 0);341getValuesBad(full, -2, 3);342getValuesBad(full, -2, -1);343getValuesBad(empty, -2, 0);344345// index > length()346getValuesBad(full, 6, 0);347getValuesBad(full, 6, -1);348getValuesBad(empty, 1, 0);349getValuesBad(empty, 1, -1);350351// length < 0352getValuesBad(full, 0, -2);353getValuesBad(empty, 0, -2);354355// index + length > length()356getValuesBad(full, 0, 6);357getValuesBad(full, 1, 5);358getValuesBad(full, 2, 4);359getValuesBad(full, 5, 1);360getValuesBad(empty, 0, 1);361362setValueGood(full, 0, 55);363setValueGood(full, 4, 66);364365// index < 0366setValueBad(full, -1, 77);367setValueBad(full, -2, 77);368369// index > length()370setValueBad(full, 5, 77);371setValueBad(full, 6, 77);372373check(full, new int[] {55, 100, 200, 300, 66});374375// index < 0376setValueBad(empty, -1, 77);377setValueBad(empty, -2, 77);378379// index > length()380setValueBad(empty, 0, 77);381setValueBad(empty, 1, 77);382383setValuesGood(full, new int[] {40, 41, 42});384setValuesGood(full, new int[] {});385386check(full, new int[] {40, 41, 42, 300, 66});387388setValuesGood(full, new int[] {99, 51, 52, 53, 54, 55});389setValuesGood(full, new int[] {50});390391check(full, new int[] {50, 51, 52, 53, 54});392393setValuesGood(empty, new int[] {});394setValuesGood(empty, new int[] {88});395396setValuesGood(full, 2, new int[] {30, 31, 32, 33, 34, 35}, 0, 3);397setValuesGood(full, 0, new int[] {80}, 0, 1);398399check(full, new int[] {80, 51, 30, 31, 32});400401setValuesGood(full, 0, new int[] {90, 91, 92, 93, 94, 95}, 3, 3);402setValuesGood(full, 4, new int[] {81}, 0, 1);403404check(full, new int[] {93, 94, 95, 31, 81});405406setValuesGood(full, 3, new int[] {60, 61, 62, 63}, 0, -1);407setValuesGood(full, 0, new int[] {82}, 0, -1);408409check(full, new int[] {82, 94, 95, 60, 61});410411setValuesGood(full, 3, new int[] {20, 21, 22, 23}, 1, -1);412setValuesGood(full, 1, new int[] {83, 84}, 1, -1);413setValuesGood(full, 1, new int[] {}, 0, -1);414setValuesGood(full, 2, new int[] {}, 0, 0);415setValuesGood(full, 3, new int[] {99}, 0, 0);416setValuesGood(full, 4, new int[] {99, 98}, 1, 0);417418check(full, new int[] {82, 84, 95, 21, 22});419420setValuesGood(empty, 0, new int[] {}, 0, -1);421setValuesGood(empty, 0, new int[] {}, 0, 0);422setValuesGood(empty, 0, new int[] {99}, 0, 0);423setValuesGood(empty, 0, new int[] {99, 98}, 1, 0);424425// index < 0426setValuesBad(full, -1, new int[] {30, 31, 32, 33, 34, 35}, 0, 0);427setValuesBad(full, -1, new int[] {30, 31, 32, 33, 34, 35}, 0, -1);428setValuesBad(full, -2, new int[] {30, 31, 32, 33, 34, 35}, 0, -1);429setValuesBad(empty, -1, new int[] {}, 0, 0);430setValuesBad(empty, -2, new int[] {}, 0, 0);431432// index > length()433setValuesBad(full, 6, new int[] {30, 31, 32, 33, 34, 35}, 0, 1);434setValuesBad(full, 6, new int[] {30, 31, 32, 33, 34, 35}, 0, -1);435setValuesBad(empty, 1, new int[] {4}, 0, 0);436setValuesBad(empty, 1, new int[] {}, 0, 0);437setValuesBad(empty, 1, new int[] {}, 0, -1);438439// srcIndex < 0440setValuesBad(full, 0, new int[] {90, 91, 92, 93, 94, 95}, -1, 3);441setValuesBad(full, 0, new int[] {90, 91, 92, 93, 94, 95}, -1, 0);442setValuesBad(full, 0, new int[] {90, 91, 92, 93, 94, 95}, -1, -1);443setValuesBad(full, 0, new int[] {90, 91, 92, 93, 94, 95}, -2, -1);444setValuesBad(full, 1, new int[] {}, -1, -1);445setValuesBad(full, 2, new int[] {}, -1, 0);446setValuesBad(empty, 0, new int[] {}, -1, 0);447448// srcIndex > values.size()449setValuesBad(full, 0, new int[] {81}, 2, 0);450setValuesBad(full, 0, new int[] {81}, 2, 1);451setValuesBad(full, 0, new int[] {81}, 2, -1);452setValuesBad(full, 4, new int[] {}, 1, 0);453setValuesBad(full, 1, new int[] {}, 1, -1);454setValuesBad(full, 2, new int[] {}, 1, 0);455setValuesBad(empty, 0, new int[] {}, 1, 0);456setValuesBad(empty, 0, new int[] {5}, 2, 0);457458// length < 0 (length != -1)459setValuesBad(full, 3, new int[] {60, 61, 62, 63}, 0, -2);460setValuesBad(full, 3, new int[] {}, 0, -2);461462// index + length > length()463setValuesBad(full, 0, new int[] {20, 21, 22, 23, 24, 25, 26}, 0, 6);464setValuesBad(full, 1, new int[] {20, 21, 22, 23, 24, 25, 26}, 0, 5);465setValuesBad(full, 2, new int[] {20, 21, 22, 23, 24, 25, 26}, 0, 4);466setValuesBad(full, 3, new int[] {20, 21, 22, 23, 24, 25, 26}, 0, 3);467setValuesBad(full, 4, new int[] {20, 21, 22, 23, 24, 25, 26}, 0, 2);468setValuesBad(full, 5, new int[] {20, 21, 22, 23, 24, 25, 26}, 0, 1);469setValuesBad(full, 6, new int[] {20, 21, 22, 23, 24, 25, 26}, 0, 0);470setValuesBad(full, 2, new int[] {20, 21, 22, 23, 24, 25, 26}, 1, 4);471setValuesBad(full, 3, new int[] {20, 21, 22, 23, 24, 25, 26}, 1, 3);472setValuesBad(full, 4, new int[] {20, 21, 22, 23, 24, 25, 26}, 2, 2);473setValuesBad(full, 5, new int[] {20, 21, 22, 23, 24, 25, 26}, 3, 1);474setValuesBad(full, 6, new int[] {20, 21, 22, 23, 24, 25, 26}, 4, 0);475setValuesBad(empty, 0, new int[] {6}, 0, 1);476477// srcIndex + length > values.size()478setValuesBad(full, 0, new int[] {82}, 0, 2);479setValuesBad(full, 0, new int[] {82}, 1, 1);480setValuesBad(full, 0, new int[] {82}, 2, 0);481setValuesBad(full, 0, new int[] {20, 21, 22}, 0, 4);482setValuesBad(full, 0, new int[] {20, 21, 22}, 1, 3);483setValuesBad(full, 0, new int[] {20, 21, 22}, 2, 2);484setValuesBad(full, 0, new int[] {20, 21, 22}, 3, 1);485setValuesBad(full, 0, new int[] {20, 21, 22}, 4, 0);486487check(full, new int[] {82, 84, 95, 21, 22});488489/*490* resume the target until end491*/492listenUntilVMDisconnect();493494/*495* deal with results of test496* if anything has called failure("foo") testFailed will be true497*/498if (!testFailed) {499println("ArrayRangeTest: passed");500} else {501throw new Exception("ArrayRangeTest: failed");502}503}504}505506507