0N/A/*
2362N/A * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 4439631
0N/A * @bug 4448721
0N/A * @bug 4448603
0N/A * @summary Test access to ranges within ArrayReferences
0N/A *
0N/A * @author Robert Field
0N/A *
0N/A * @run build TestScaffold VMConnection TargetListener TargetAdapter
0N/A * @run compile -g ArrayRangeTest.java
0N/A * @run main ArrayRangeTest
0N/A */
0N/Aimport com.sun.jdi.*;
0N/Aimport com.sun.jdi.event.*;
0N/Aimport com.sun.jdi.request.*;
0N/A
0N/Aimport java.util.*;
0N/A
0N/A /********** target program **********/
0N/A
0N/Aclass ArrayRangeTarg {
0N/A static int[] emptyArray = {};
0N/A static int[] fullArray = {0, 100, 200, 300, 400};
0N/A
0N/A public static void main(String[] args) {
0N/A System.out.println("Goodbye from ArrayRangeTarg!");
0N/A }
0N/A}
0N/A
0N/A /********** test program **********/
0N/A
0N/Apublic class ArrayRangeTest extends TestScaffold {
0N/A ReferenceType targetClass;
0N/A
0N/A class Sample {
0N/A Sample(String name, ArrayReference arrRef, int[] expected) {
0N/A this.name = name;
0N/A this.arrRef = arrRef;
0N/A this.expected = expected;
0N/A }
0N/A String name;
0N/A ArrayReference arrRef;
0N/A int[] expected;
0N/A }
0N/A
0N/A ArrayRangeTest (String args[]) {
0N/A super(args);
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A new ArrayRangeTest(args).startTests();
0N/A }
0N/A
0N/A /********** test assist **********/
0N/A
0N/A String arr(int a[]) {
0N/A StringBuffer buf = new StringBuffer();
0N/A buf.append('[');
0N/A if (a.length > 0) {
0N/A buf.append(a[0]);
0N/A for (int i = 1; i < a.length; ++i) {
0N/A buf.append(',');
0N/A buf.append(a[i]);
0N/A }
0N/A }
0N/A buf.append(']');
0N/A return buf.toString();
0N/A }
0N/A
0N/A void getValueGood(Sample samp, int index) {
0N/A try {
0N/A Value val = samp.arrRef.getValue(index);
0N/A int ival = ((IntegerValue)val).value();
0N/A if (ival != samp.expected[index]) {
0N/A failure("FAIL - " + samp.name +
0N/A ".getValue(" + index + ") - wrong value=" + ival);
0N/A } else {
0N/A println("pass - " + samp.name +
0N/A ".getValue(" + index + ") - value=" + ival);
0N/A }
0N/A } catch (Throwable exc) {
0N/A failure("FAIL - " + samp.name +
0N/A ".getValue(" + index + ") - unexpected: " + exc);
0N/A }
0N/A }
0N/A
0N/A void getValueBad(Sample samp, int index) {
0N/A try {
0N/A Value val = samp.arrRef.getValue(index);
0N/A failure("FAIL - " + samp.name +
0N/A ".getValue(" + index + ") - no expected exception");
0N/A } catch (IndexOutOfBoundsException exc) {
0N/A println("pass - " + samp.name +
0N/A ".getValue(" + index + ") - got expected: " + exc);
0N/A } catch (Throwable exc) {
0N/A failure("FAIL - " + samp.name +
0N/A ".getValue(" + index + ") - unexpected: " + exc);
0N/A }
0N/A }
0N/A
0N/A void getValuesGood(Sample samp) {
0N/A String desc = samp.name + ".getValues()";
0N/A try {
0N/A List vals = samp.arrRef.getValues();
0N/A if (vals.size() != samp.expected.length) {
0N/A failure("FAIL - " + desc +
0N/A " - wrong size=" + vals.size() +
0N/A " , expected: " + samp.expected.length);
0N/A }
0N/A for (int index = 0; index < vals.size(); ++index) {
0N/A int ival = ((IntegerValue)vals.get(index)).value();
0N/A if (ival != samp.expected[index]) {
0N/A failure("FAIL - " + desc +
0N/A " - wrong value=" + ival);
0N/A return;
0N/A }
0N/A }
0N/A println("pass - " + samp.name + ".getValues())");
0N/A } catch (Throwable exc) {
0N/A failure("FAIL - " + desc +
0N/A " - unexpected: " + exc);
0N/A }
0N/A }
0N/A
0N/A void getValuesGood(Sample samp, int index, int length) {
0N/A try {
0N/A List vals = samp.arrRef.getValues(index, length);
0N/A if (vals.size() !=
0N/A ((length==-1)? (samp.expected.length - index) : length)) {
0N/A failure("FAIL - " + samp.name + ".getValues(" +
0N/A index + ", " + length + ") - wrong size=" +
0N/A vals.size());
0N/A }
0N/A for (int i = 0; i < vals.size(); ++i) {
0N/A int ival = ((IntegerValue)vals.get(i)).value();
0N/A if (ival != samp.expected[index + i]) {
0N/A failure("FAIL - " + samp.name + ".getValues(" +
0N/A index + ", " + length + ") - wrong value=" +
0N/A ival);
0N/A return;
0N/A }
0N/A }
0N/A println("pass - " + samp.name + ".getValues(" +
0N/A index + ", " + length + "))");
0N/A } catch (Throwable exc) {
0N/A failure("FAIL - " + samp.name + ".getValues(" +
0N/A index + ", " + length + ") - unexpected: " + exc);
0N/A }
0N/A }
0N/A
0N/A void getValuesBad(Sample samp, int index, int length) {
0N/A try {
0N/A List vals = samp.arrRef.getValues(index, length);
0N/A failure("FAIL - " + samp.name + ".getValues(" +
0N/A index + ", " + length + ") - no expected exception");
0N/A } catch (IndexOutOfBoundsException exc) {
0N/A println("pass - " + samp.name + ".getValue(" +
0N/A index + ", " + length + ") - got expected: " + exc);
0N/A } catch (Throwable exc) {
0N/A failure("FAIL - " + samp.name + ".getValues(" +
0N/A index + ", " + length + ") - unexpected: " + exc);
0N/A }
0N/A }
0N/A
0N/A void setValueGood(Sample samp, int index, int ival) {
0N/A try {
0N/A Value val = vm().mirrorOf(ival);
0N/A samp.arrRef.setValue(index, val);
0N/A println("pass - " + samp.name +
0N/A ".setValue(" + index + ", ..)");
0N/A } catch (Throwable exc) {
0N/A failure("FAIL - " + samp.name +
0N/A ".setValue(" + index + ",...) - unexpected: " + exc);
0N/A }
0N/A }
0N/A
0N/A void setValueBad(Sample samp, int index, int ival) {
0N/A try {
0N/A Value val = vm().mirrorOf(ival);
0N/A samp.arrRef.setValue(index, val);
0N/A failure("FAIL - " + samp.name +
0N/A ".setValue(" + index + ", ..) - no expected exception");
0N/A } catch (IndexOutOfBoundsException exc) {
0N/A println("pass - " + samp.name +
0N/A ".setValue(" + index + ",...) - got expected: " + exc);
0N/A } catch (Throwable exc) {
0N/A failure("FAIL - " + samp.name +
0N/A ".setValue(" + index + ",...) - unexpected: " + exc);
0N/A }
0N/A }
0N/A
0N/A void setValuesGood(Sample samp, int[] valArray) {
0N/A String desc = samp.name + ".setValues(" + arr(valArray) + ")";
0N/A try {
0N/A List values = new ArrayList();
0N/A for (int i = 0; i < valArray.length; ++i) {
0N/A Value val = vm().mirrorOf(valArray[i]);
0N/A values.add(val);
0N/A }
0N/A samp.arrRef.setValues(values);
0N/A println("pass - " + desc);
0N/A } catch (Throwable exc) {
0N/A failure("FAIL - " + desc + " - unexpected: " + exc);
0N/A }
0N/A }
0N/A
0N/A void setValuesGood(Sample samp, int index, int[] valArray,
0N/A int srcInx, int length) {
0N/A String desc = samp.name + ".setValues(" + index + ", " +
0N/A arr(valArray) + ", " + srcInx + ", " + length + ")";
0N/A try {
0N/A List values = new ArrayList();
0N/A for (int i = 0; i < valArray.length; ++i) {
0N/A Value val = vm().mirrorOf(valArray[i]);
0N/A values.add(val);
0N/A }
0N/A samp.arrRef.setValues(index, values, srcInx, length);
0N/A println("pass - " + desc);
0N/A } catch (Throwable exc) {
0N/A failure("FAIL - " + desc + " - unexpected: " + exc);
0N/A }
0N/A }
0N/A
0N/A void setValuesBad(Sample samp, int index, int[] valArray,
0N/A int srcInx, int length) {
0N/A String desc = samp.name + ".setValues(" + index + ", " +
0N/A arr(valArray) + ", " + srcInx + ", " + length + ")";
0N/A try {
0N/A List values = new ArrayList();
0N/A for (int i = 0; i < valArray.length; ++i) {
0N/A Value val = vm().mirrorOf(valArray[i]);
0N/A values.add(val);
0N/A }
0N/A samp.arrRef.setValues(index, values, srcInx, length);
0N/A failure("FAIL - " + desc + " - no expected exception");
0N/A } catch (IndexOutOfBoundsException exc) {
0N/A println("pass - " + desc + " - got expected: " + exc);
0N/A } catch (Throwable exc) {
0N/A failure("FAIL - " + desc + " - unexpected: " + exc);
0N/A }
0N/A }
0N/A
0N/A void check(Sample samp, int[] expectArray) {
0N/A String desc = samp.name + " - check - " + arr(expectArray);
0N/A
0N/A try {
0N/A List vals = samp.arrRef.getValues();
0N/A if (vals.size() != expectArray.length) {
0N/A failure("FAIL - " + desc +
0N/A " - wrong size=" + vals.size() +
0N/A " , expected: " + expectArray.length);
0N/A }
0N/A for (int index = 0; index < vals.size(); ++index) {
0N/A int ival = ((IntegerValue)vals.get(index)).value();
0N/A if (ival != expectArray[index]) {
0N/A failure("FAIL - " + desc +
0N/A " - wrong value=" + ival);
0N/A return;
0N/A }
0N/A }
0N/A println("pass - " + desc);
0N/A } catch (Throwable exc) {
0N/A failure("FAIL - " + desc +
0N/A " - unexpected: " + exc);
0N/A }
0N/A }
0N/A
0N/A /********** test core **********/
0N/A
0N/A protected void runTests() throws Exception {
0N/A /*
0N/A * Get to the top of main() to determine targetClass
0N/A */
0N/A BreakpointEvent bpe = startToMain("ArrayRangeTarg");
0N/A targetClass = bpe.location().declaringType();
0N/A Field fullField = targetClass.fieldByName("fullArray");
0N/A Field emptyField = targetClass.fieldByName("emptyArray");
0N/A ArrayReference emptyAR = (ArrayReference)targetClass.getValue(emptyField);
0N/A ArrayReference fullAR = (ArrayReference)targetClass.getValue(fullField);
0N/A Sample full = new Sample("full", fullAR, ArrayRangeTarg.fullArray);
0N/A Sample empty = new Sample("empty", emptyAR, ArrayRangeTarg.emptyArray);
0N/A
0N/A getValueGood(full, 0);
0N/A getValueGood(full, 4);
0N/A
0N/A // index < 0
0N/A getValueBad(full, -1);
0N/A getValueBad(full, -2);
0N/A getValueBad(empty, -1);
0N/A getValueBad(empty, -2);
0N/A
0N/A // index >= length
0N/A getValueBad(full, 5);
0N/A getValueBad(empty, 0);
0N/A getValueBad(empty, 5);
0N/A
0N/A getValuesGood(full);
0N/A getValuesGood(empty);
0N/A
0N/A getValuesGood(full, 0, 5);
0N/A getValuesGood(full, 0, 4);
0N/A getValuesGood(full, 1, 4);
0N/A getValuesGood(full, 5, 0);
0N/A getValuesGood(full, 0, 0);
0N/A getValuesGood(full, 0, -1);
0N/A getValuesGood(full, 1, -1);
0N/A getValuesGood(full, 5, -1);
0N/A
0N/A getValuesGood(empty, 0, 0);
0N/A getValuesGood(empty, 0, -1);
0N/A
0N/A // index < 0
0N/A getValuesBad(full, -1, 0);
0N/A getValuesBad(full, -1, 3);
0N/A getValuesBad(full, -1, -1);
0N/A getValuesBad(empty, -1, 0);
0N/A getValuesBad(full, -2, 0);
0N/A getValuesBad(full, -2, 3);
0N/A getValuesBad(full, -2, -1);
0N/A getValuesBad(empty, -2, 0);
0N/A
0N/A // index > length()
0N/A getValuesBad(full, 6, 0);
0N/A getValuesBad(full, 6, -1);
0N/A getValuesBad(empty, 1, 0);
0N/A getValuesBad(empty, 1, -1);
0N/A
0N/A // length < 0
0N/A getValuesBad(full, 0, -2);
0N/A getValuesBad(empty, 0, -2);
0N/A
0N/A // index + length > length()
0N/A getValuesBad(full, 0, 6);
0N/A getValuesBad(full, 1, 5);
0N/A getValuesBad(full, 2, 4);
0N/A getValuesBad(full, 5, 1);
0N/A getValuesBad(empty, 0, 1);
0N/A
0N/A setValueGood(full, 0, 55);
0N/A setValueGood(full, 4, 66);
0N/A
0N/A // index < 0
0N/A setValueBad(full, -1, 77);
0N/A setValueBad(full, -2, 77);
0N/A
0N/A // index > length()
0N/A setValueBad(full, 5, 77);
0N/A setValueBad(full, 6, 77);
0N/A
0N/A check(full, new int[] {55, 100, 200, 300, 66});
0N/A
0N/A // index < 0
0N/A setValueBad(empty, -1, 77);
0N/A setValueBad(empty, -2, 77);
0N/A
0N/A // index > length()
0N/A setValueBad(empty, 0, 77);
0N/A setValueBad(empty, 1, 77);
0N/A
0N/A setValuesGood(full, new int[] {40, 41, 42});
0N/A setValuesGood(full, new int[] {});
0N/A
0N/A check(full, new int[] {40, 41, 42, 300, 66});
0N/A
0N/A setValuesGood(full, new int[] {99, 51, 52, 53, 54, 55});
0N/A setValuesGood(full, new int[] {50});
0N/A
0N/A check(full, new int[] {50, 51, 52, 53, 54});
0N/A
0N/A setValuesGood(empty, new int[] {});
0N/A setValuesGood(empty, new int[] {88});
0N/A
0N/A setValuesGood(full, 2, new int[] {30, 31, 32, 33, 34, 35}, 0, 3);
0N/A setValuesGood(full, 0, new int[] {80}, 0, 1);
0N/A
0N/A check(full, new int[] {80, 51, 30, 31, 32});
0N/A
0N/A setValuesGood(full, 0, new int[] {90, 91, 92, 93, 94, 95}, 3, 3);
0N/A setValuesGood(full, 4, new int[] {81}, 0, 1);
0N/A
0N/A check(full, new int[] {93, 94, 95, 31, 81});
0N/A
0N/A setValuesGood(full, 3, new int[] {60, 61, 62, 63}, 0, -1);
0N/A setValuesGood(full, 0, new int[] {82}, 0, -1);
0N/A
0N/A check(full, new int[] {82, 94, 95, 60, 61});
0N/A
0N/A setValuesGood(full, 3, new int[] {20, 21, 22, 23}, 1, -1);
0N/A setValuesGood(full, 1, new int[] {83, 84}, 1, -1);
0N/A setValuesGood(full, 1, new int[] {}, 0, -1);
0N/A setValuesGood(full, 2, new int[] {}, 0, 0);
0N/A setValuesGood(full, 3, new int[] {99}, 0, 0);
0N/A setValuesGood(full, 4, new int[] {99, 98}, 1, 0);
0N/A
0N/A check(full, new int[] {82, 84, 95, 21, 22});
0N/A
0N/A setValuesGood(empty, 0, new int[] {}, 0, -1);
0N/A setValuesGood(empty, 0, new int[] {}, 0, 0);
0N/A setValuesGood(empty, 0, new int[] {99}, 0, 0);
0N/A setValuesGood(empty, 0, new int[] {99, 98}, 1, 0);
0N/A
0N/A // index < 0
0N/A setValuesBad(full, -1, new int[] {30, 31, 32, 33, 34, 35}, 0, 0);
0N/A setValuesBad(full, -1, new int[] {30, 31, 32, 33, 34, 35}, 0, -1);
0N/A setValuesBad(full, -2, new int[] {30, 31, 32, 33, 34, 35}, 0, -1);
0N/A setValuesBad(empty, -1, new int[] {}, 0, 0);
0N/A setValuesBad(empty, -2, new int[] {}, 0, 0);
0N/A
0N/A // index > length()
0N/A setValuesBad(full, 6, new int[] {30, 31, 32, 33, 34, 35}, 0, 1);
0N/A setValuesBad(full, 6, new int[] {30, 31, 32, 33, 34, 35}, 0, -1);
0N/A setValuesBad(empty, 1, new int[] {4}, 0, 0);
0N/A setValuesBad(empty, 1, new int[] {}, 0, 0);
0N/A setValuesBad(empty, 1, new int[] {}, 0, -1);
0N/A
0N/A // srcIndex < 0
0N/A setValuesBad(full, 0, new int[] {90, 91, 92, 93, 94, 95}, -1, 3);
0N/A setValuesBad(full, 0, new int[] {90, 91, 92, 93, 94, 95}, -1, 0);
0N/A setValuesBad(full, 0, new int[] {90, 91, 92, 93, 94, 95}, -1, -1);
0N/A setValuesBad(full, 0, new int[] {90, 91, 92, 93, 94, 95}, -2, -1);
0N/A setValuesBad(full, 1, new int[] {}, -1, -1);
0N/A setValuesBad(full, 2, new int[] {}, -1, 0);
0N/A setValuesBad(empty, 0, new int[] {}, -1, 0);
0N/A
0N/A // srcIndex > values.size()
0N/A setValuesBad(full, 0, new int[] {81}, 2, 0);
0N/A setValuesBad(full, 0, new int[] {81}, 2, 1);
0N/A setValuesBad(full, 0, new int[] {81}, 2, -1);
0N/A setValuesBad(full, 4, new int[] {}, 1, 0);
0N/A setValuesBad(full, 1, new int[] {}, 1, -1);
0N/A setValuesBad(full, 2, new int[] {}, 1, 0);
0N/A setValuesBad(empty, 0, new int[] {}, 1, 0);
0N/A setValuesBad(empty, 0, new int[] {5}, 2, 0);
0N/A
0N/A // length < 0 (length != -1)
0N/A setValuesBad(full, 3, new int[] {60, 61, 62, 63}, 0, -2);
0N/A setValuesBad(full, 3, new int[] {}, 0, -2);
0N/A
0N/A // index + length > length()
0N/A setValuesBad(full, 0, new int[] {20, 21, 22, 23, 24, 25, 26}, 0, 6);
0N/A setValuesBad(full, 1, new int[] {20, 21, 22, 23, 24, 25, 26}, 0, 5);
0N/A setValuesBad(full, 2, new int[] {20, 21, 22, 23, 24, 25, 26}, 0, 4);
0N/A setValuesBad(full, 3, new int[] {20, 21, 22, 23, 24, 25, 26}, 0, 3);
0N/A setValuesBad(full, 4, new int[] {20, 21, 22, 23, 24, 25, 26}, 0, 2);
0N/A setValuesBad(full, 5, new int[] {20, 21, 22, 23, 24, 25, 26}, 0, 1);
0N/A setValuesBad(full, 6, new int[] {20, 21, 22, 23, 24, 25, 26}, 0, 0);
0N/A setValuesBad(full, 2, new int[] {20, 21, 22, 23, 24, 25, 26}, 1, 4);
0N/A setValuesBad(full, 3, new int[] {20, 21, 22, 23, 24, 25, 26}, 1, 3);
0N/A setValuesBad(full, 4, new int[] {20, 21, 22, 23, 24, 25, 26}, 2, 2);
0N/A setValuesBad(full, 5, new int[] {20, 21, 22, 23, 24, 25, 26}, 3, 1);
0N/A setValuesBad(full, 6, new int[] {20, 21, 22, 23, 24, 25, 26}, 4, 0);
0N/A setValuesBad(empty, 0, new int[] {6}, 0, 1);
0N/A
0N/A // srcIndex + length > values.size()
0N/A setValuesBad(full, 0, new int[] {82}, 0, 2);
0N/A setValuesBad(full, 0, new int[] {82}, 1, 1);
0N/A setValuesBad(full, 0, new int[] {82}, 2, 0);
0N/A setValuesBad(full, 0, new int[] {20, 21, 22}, 0, 4);
0N/A setValuesBad(full, 0, new int[] {20, 21, 22}, 1, 3);
0N/A setValuesBad(full, 0, new int[] {20, 21, 22}, 2, 2);
0N/A setValuesBad(full, 0, new int[] {20, 21, 22}, 3, 1);
0N/A setValuesBad(full, 0, new int[] {20, 21, 22}, 4, 0);
0N/A
0N/A check(full, new int[] {82, 84, 95, 21, 22});
0N/A
0N/A /*
0N/A * resume the target until end
0N/A */
0N/A listenUntilVMDisconnect();
0N/A
0N/A /*
0N/A * deal with results of test
0N/A * if anything has called failure("foo") testFailed will be true
0N/A */
0N/A if (!testFailed) {
0N/A println("ArrayRangeTest: passed");
0N/A } else {
0N/A throw new Exception("ArrayRangeTest: failed");
0N/A }
0N/A }
0N/A}