Path: blob/master/test/jdk/com/sun/jdi/ArrayRangeTest.java
41152 views
/*1* Copyright (c) 2001, 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*/2223/**24* @test25* @bug 443963126* @bug 444872127* @bug 444860328* @summary Test access to ranges within ArrayReferences29* @author Robert Field30*31* @run build TestScaffold VMConnection TargetListener TargetAdapter32* @run compile -g ArrayRangeTest.java33* @run driver ArrayRangeTest34*/35import com.sun.jdi.*;36import com.sun.jdi.event.*;37import com.sun.jdi.request.*;3839import java.util.*;4041/********** target program **********/4243class ArrayRangeTarg {44static int[] emptyArray = {};45static int[] fullArray = {0, 100, 200, 300, 400};4647public static void main(String[] args) {48System.out.println("Goodbye from ArrayRangeTarg!");49}50}5152/********** test program **********/5354public class ArrayRangeTest extends TestScaffold {55ReferenceType targetClass;5657class Sample {58Sample(String name, ArrayReference arrRef, int[] expected) {59this.name = name;60this.arrRef = arrRef;61this.expected = expected;62}63String name;64ArrayReference arrRef;65int[] expected;66}6768ArrayRangeTest (String args[]) {69super(args);70}7172public static void main(String[] args) throws Exception {73new ArrayRangeTest(args).startTests();74}7576/********** test assist **********/7778String arr(int a[]) {79StringBuffer buf = new StringBuffer();80buf.append('[');81if (a.length > 0) {82buf.append(a[0]);83for (int i = 1; i < a.length; ++i) {84buf.append(',');85buf.append(a[i]);86}87}88buf.append(']');89return buf.toString();90}9192void getValueGood(Sample samp, int index) {93try {94Value val = samp.arrRef.getValue(index);95int ival = ((IntegerValue)val).value();96if (ival != samp.expected[index]) {97failure("FAIL - " + samp.name +98".getValue(" + index + ") - wrong value=" + ival);99} else {100println("pass - " + samp.name +101".getValue(" + index + ") - value=" + ival);102}103} catch (Throwable exc) {104failure("FAIL - " + samp.name +105".getValue(" + index + ") - unexpected: " + exc);106}107}108109void getValueBad(Sample samp, int index) {110try {111Value val = samp.arrRef.getValue(index);112failure("FAIL - " + samp.name +113".getValue(" + index + ") - no expected exception");114} catch (IndexOutOfBoundsException exc) {115println("pass - " + samp.name +116".getValue(" + index + ") - got expected: " + exc);117} catch (Throwable exc) {118failure("FAIL - " + samp.name +119".getValue(" + index + ") - unexpected: " + exc);120}121}122123void getValuesGood(Sample samp) {124String desc = samp.name + ".getValues()";125try {126List vals = samp.arrRef.getValues();127if (vals.size() != samp.expected.length) {128failure("FAIL - " + desc +129" - wrong size=" + vals.size() +130" , expected: " + samp.expected.length);131}132for (int index = 0; index < vals.size(); ++index) {133int ival = ((IntegerValue)vals.get(index)).value();134if (ival != samp.expected[index]) {135failure("FAIL - " + desc +136" - wrong value=" + ival);137return;138}139}140println("pass - " + samp.name + ".getValues())");141} catch (Throwable exc) {142failure("FAIL - " + desc +143" - unexpected: " + exc);144}145}146147void getValuesGood(Sample samp, int index, int length) {148try {149List vals = samp.arrRef.getValues(index, length);150if (vals.size() !=151((length==-1)? (samp.expected.length - index) : length)) {152failure("FAIL - " + samp.name + ".getValues(" +153index + ", " + length + ") - wrong size=" +154vals.size());155}156for (int i = 0; i < vals.size(); ++i) {157int ival = ((IntegerValue)vals.get(i)).value();158if (ival != samp.expected[index + i]) {159failure("FAIL - " + samp.name + ".getValues(" +160index + ", " + length + ") - wrong value=" +161ival);162return;163}164}165println("pass - " + samp.name + ".getValues(" +166index + ", " + length + "))");167} catch (Throwable exc) {168failure("FAIL - " + samp.name + ".getValues(" +169index + ", " + length + ") - unexpected: " + exc);170}171}172173void getValuesBad(Sample samp, int index, int length) {174try {175List vals = samp.arrRef.getValues(index, length);176failure("FAIL - " + samp.name + ".getValues(" +177index + ", " + length + ") - no expected exception");178} catch (IndexOutOfBoundsException exc) {179println("pass - " + samp.name + ".getValue(" +180index + ", " + length + ") - got expected: " + exc);181} catch (Throwable exc) {182failure("FAIL - " + samp.name + ".getValues(" +183index + ", " + length + ") - unexpected: " + exc);184}185}186187void setValueGood(Sample samp, int index, int ival) {188try {189Value val = vm().mirrorOf(ival);190samp.arrRef.setValue(index, val);191println("pass - " + samp.name +192".setValue(" + index + ", ..)");193} catch (Throwable exc) {194failure("FAIL - " + samp.name +195".setValue(" + index + ",...) - unexpected: " + exc);196}197}198199void setValueBad(Sample samp, int index, int ival) {200try {201Value val = vm().mirrorOf(ival);202samp.arrRef.setValue(index, val);203failure("FAIL - " + samp.name +204".setValue(" + index + ", ..) - no expected exception");205} catch (IndexOutOfBoundsException exc) {206println("pass - " + samp.name +207".setValue(" + index + ",...) - got expected: " + exc);208} catch (Throwable exc) {209failure("FAIL - " + samp.name +210".setValue(" + index + ",...) - unexpected: " + exc);211}212}213214void setValuesGood(Sample samp, int[] valArray) {215String desc = samp.name + ".setValues(" + arr(valArray) + ")";216try {217List values = new ArrayList();218for (int i = 0; i < valArray.length; ++i) {219Value val = vm().mirrorOf(valArray[i]);220values.add(val);221}222samp.arrRef.setValues(values);223println("pass - " + desc);224} catch (Throwable exc) {225failure("FAIL - " + desc + " - unexpected: " + exc);226}227}228229void setValuesGood(Sample samp, int index, int[] valArray,230int srcInx, int length) {231String desc = samp.name + ".setValues(" + index + ", " +232arr(valArray) + ", " + srcInx + ", " + length + ")";233try {234List values = new ArrayList();235for (int i = 0; i < valArray.length; ++i) {236Value val = vm().mirrorOf(valArray[i]);237values.add(val);238}239samp.arrRef.setValues(index, values, srcInx, length);240println("pass - " + desc);241} catch (Throwable exc) {242failure("FAIL - " + desc + " - unexpected: " + exc);243}244}245246void setValuesBad(Sample samp, int index, int[] valArray,247int srcInx, int length) {248String desc = samp.name + ".setValues(" + index + ", " +249arr(valArray) + ", " + srcInx + ", " + length + ")";250try {251List values = new ArrayList();252for (int i = 0; i < valArray.length; ++i) {253Value val = vm().mirrorOf(valArray[i]);254values.add(val);255}256samp.arrRef.setValues(index, values, srcInx, length);257failure("FAIL - " + desc + " - no expected exception");258} catch (IndexOutOfBoundsException exc) {259println("pass - " + desc + " - got expected: " + exc);260} catch (Throwable exc) {261failure("FAIL - " + desc + " - unexpected: " + exc);262}263}264265void check(Sample samp, int[] expectArray) {266String desc = samp.name + " - check - " + arr(expectArray);267268try {269List vals = samp.arrRef.getValues();270if (vals.size() != expectArray.length) {271failure("FAIL - " + desc +272" - wrong size=" + vals.size() +273" , expected: " + expectArray.length);274}275for (int index = 0; index < vals.size(); ++index) {276int ival = ((IntegerValue)vals.get(index)).value();277if (ival != expectArray[index]) {278failure("FAIL - " + desc +279" - wrong value=" + ival);280return;281}282}283println("pass - " + desc);284} catch (Throwable exc) {285failure("FAIL - " + desc +286" - unexpected: " + exc);287}288}289290/********** test core **********/291292protected void runTests() throws Exception {293/*294* Get to the top of main() to determine targetClass295*/296BreakpointEvent bpe = startToMain("ArrayRangeTarg");297targetClass = bpe.location().declaringType();298Field fullField = targetClass.fieldByName("fullArray");299Field emptyField = targetClass.fieldByName("emptyArray");300ArrayReference emptyAR = (ArrayReference)targetClass.getValue(emptyField);301ArrayReference fullAR = (ArrayReference)targetClass.getValue(fullField);302Sample full = new Sample("full", fullAR, ArrayRangeTarg.fullArray);303Sample empty = new Sample("empty", emptyAR, ArrayRangeTarg.emptyArray);304305getValueGood(full, 0);306getValueGood(full, 4);307308// index < 0309getValueBad(full, -1);310getValueBad(full, -2);311getValueBad(empty, -1);312getValueBad(empty, -2);313314// index >= length315getValueBad(full, 5);316getValueBad(empty, 0);317getValueBad(empty, 5);318319getValuesGood(full);320getValuesGood(empty);321322getValuesGood(full, 0, 5);323getValuesGood(full, 0, 4);324getValuesGood(full, 1, 4);325getValuesGood(full, 5, 0);326getValuesGood(full, 0, 0);327getValuesGood(full, 0, -1);328getValuesGood(full, 1, -1);329getValuesGood(full, 5, -1);330331getValuesGood(empty, 0, 0);332getValuesGood(empty, 0, -1);333334// index < 0335getValuesBad(full, -1, 0);336getValuesBad(full, -1, 3);337getValuesBad(full, -1, -1);338getValuesBad(empty, -1, 0);339getValuesBad(full, -2, 0);340getValuesBad(full, -2, 3);341getValuesBad(full, -2, -1);342getValuesBad(empty, -2, 0);343344// index > length()345getValuesBad(full, 6, 0);346getValuesBad(full, 6, -1);347getValuesBad(empty, 1, 0);348getValuesBad(empty, 1, -1);349350// length < 0351getValuesBad(full, 0, -2);352getValuesBad(empty, 0, -2);353354// index + length > length()355getValuesBad(full, 0, 6);356getValuesBad(full, 1, 5);357getValuesBad(full, 2, 4);358getValuesBad(full, 5, 1);359getValuesBad(empty, 0, 1);360361setValueGood(full, 0, 55);362setValueGood(full, 4, 66);363364// index < 0365setValueBad(full, -1, 77);366setValueBad(full, -2, 77);367368// index > length()369setValueBad(full, 5, 77);370setValueBad(full, 6, 77);371372check(full, new int[] {55, 100, 200, 300, 66});373374// index < 0375setValueBad(empty, -1, 77);376setValueBad(empty, -2, 77);377378// index > length()379setValueBad(empty, 0, 77);380setValueBad(empty, 1, 77);381382setValuesGood(full, new int[] {40, 41, 42});383setValuesGood(full, new int[] {});384385check(full, new int[] {40, 41, 42, 300, 66});386387setValuesGood(full, new int[] {99, 51, 52, 53, 54, 55});388setValuesGood(full, new int[] {50});389390check(full, new int[] {50, 51, 52, 53, 54});391392setValuesGood(empty, new int[] {});393setValuesGood(empty, new int[] {88});394395setValuesGood(full, 2, new int[] {30, 31, 32, 33, 34, 35}, 0, 3);396setValuesGood(full, 0, new int[] {80}, 0, 1);397398check(full, new int[] {80, 51, 30, 31, 32});399400setValuesGood(full, 0, new int[] {90, 91, 92, 93, 94, 95}, 3, 3);401setValuesGood(full, 4, new int[] {81}, 0, 1);402403check(full, new int[] {93, 94, 95, 31, 81});404405setValuesGood(full, 3, new int[] {60, 61, 62, 63}, 0, -1);406setValuesGood(full, 0, new int[] {82}, 0, -1);407408check(full, new int[] {82, 94, 95, 60, 61});409410setValuesGood(full, 3, new int[] {20, 21, 22, 23}, 1, -1);411setValuesGood(full, 1, new int[] {83, 84}, 1, -1);412setValuesGood(full, 1, new int[] {}, 0, -1);413setValuesGood(full, 2, new int[] {}, 0, 0);414setValuesGood(full, 3, new int[] {99}, 0, 0);415setValuesGood(full, 4, new int[] {99, 98}, 1, 0);416417check(full, new int[] {82, 84, 95, 21, 22});418419setValuesGood(empty, 0, new int[] {}, 0, -1);420setValuesGood(empty, 0, new int[] {}, 0, 0);421setValuesGood(empty, 0, new int[] {99}, 0, 0);422setValuesGood(empty, 0, new int[] {99, 98}, 1, 0);423424// index < 0425setValuesBad(full, -1, new int[] {30, 31, 32, 33, 34, 35}, 0, 0);426setValuesBad(full, -1, new int[] {30, 31, 32, 33, 34, 35}, 0, -1);427setValuesBad(full, -2, new int[] {30, 31, 32, 33, 34, 35}, 0, -1);428setValuesBad(empty, -1, new int[] {}, 0, 0);429setValuesBad(empty, -2, new int[] {}, 0, 0);430431// index > length()432setValuesBad(full, 6, new int[] {30, 31, 32, 33, 34, 35}, 0, 1);433setValuesBad(full, 6, new int[] {30, 31, 32, 33, 34, 35}, 0, -1);434setValuesBad(empty, 1, new int[] {4}, 0, 0);435setValuesBad(empty, 1, new int[] {}, 0, 0);436setValuesBad(empty, 1, new int[] {}, 0, -1);437438// srcIndex < 0439setValuesBad(full, 0, new int[] {90, 91, 92, 93, 94, 95}, -1, 3);440setValuesBad(full, 0, new int[] {90, 91, 92, 93, 94, 95}, -1, 0);441setValuesBad(full, 0, new int[] {90, 91, 92, 93, 94, 95}, -1, -1);442setValuesBad(full, 0, new int[] {90, 91, 92, 93, 94, 95}, -2, -1);443setValuesBad(full, 1, new int[] {}, -1, -1);444setValuesBad(full, 2, new int[] {}, -1, 0);445setValuesBad(empty, 0, new int[] {}, -1, 0);446447// srcIndex > values.size()448setValuesBad(full, 0, new int[] {81}, 2, 0);449setValuesBad(full, 0, new int[] {81}, 2, 1);450setValuesBad(full, 0, new int[] {81}, 2, -1);451setValuesBad(full, 4, new int[] {}, 1, 0);452setValuesBad(full, 1, new int[] {}, 1, -1);453setValuesBad(full, 2, new int[] {}, 1, 0);454setValuesBad(empty, 0, new int[] {}, 1, 0);455setValuesBad(empty, 0, new int[] {5}, 2, 0);456457// length < 0 (length != -1)458setValuesBad(full, 3, new int[] {60, 61, 62, 63}, 0, -2);459setValuesBad(full, 3, new int[] {}, 0, -2);460461// index + length > length()462setValuesBad(full, 0, new int[] {20, 21, 22, 23, 24, 25, 26}, 0, 6);463setValuesBad(full, 1, new int[] {20, 21, 22, 23, 24, 25, 26}, 0, 5);464setValuesBad(full, 2, new int[] {20, 21, 22, 23, 24, 25, 26}, 0, 4);465setValuesBad(full, 3, new int[] {20, 21, 22, 23, 24, 25, 26}, 0, 3);466setValuesBad(full, 4, new int[] {20, 21, 22, 23, 24, 25, 26}, 0, 2);467setValuesBad(full, 5, new int[] {20, 21, 22, 23, 24, 25, 26}, 0, 1);468setValuesBad(full, 6, new int[] {20, 21, 22, 23, 24, 25, 26}, 0, 0);469setValuesBad(full, 2, new int[] {20, 21, 22, 23, 24, 25, 26}, 1, 4);470setValuesBad(full, 3, new int[] {20, 21, 22, 23, 24, 25, 26}, 1, 3);471setValuesBad(full, 4, new int[] {20, 21, 22, 23, 24, 25, 26}, 2, 2);472setValuesBad(full, 5, new int[] {20, 21, 22, 23, 24, 25, 26}, 3, 1);473setValuesBad(full, 6, new int[] {20, 21, 22, 23, 24, 25, 26}, 4, 0);474setValuesBad(empty, 0, new int[] {6}, 0, 1);475476// srcIndex + length > values.size()477setValuesBad(full, 0, new int[] {82}, 0, 2);478setValuesBad(full, 0, new int[] {82}, 1, 1);479setValuesBad(full, 0, new int[] {82}, 2, 0);480setValuesBad(full, 0, new int[] {20, 21, 22}, 0, 4);481setValuesBad(full, 0, new int[] {20, 21, 22}, 1, 3);482setValuesBad(full, 0, new int[] {20, 21, 22}, 2, 2);483setValuesBad(full, 0, new int[] {20, 21, 22}, 3, 1);484setValuesBad(full, 0, new int[] {20, 21, 22}, 4, 0);485486check(full, new int[] {82, 84, 95, 21, 22});487488/*489* resume the target until end490*/491listenUntilVMDisconnect();492493/*494* deal with results of test495* if anything has called failure("foo") testFailed will be true496*/497if (!testFailed) {498println("ArrayRangeTest: passed");499} else {500throw new Exception("ArrayRangeTest: failed");501}502}503}504505506