Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/DoubleValue/compareTo/compareto001.java
41160 views
/*1* Copyright (c) 2002, 2018, 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 nsk.jdi.DoubleValue.compareTo;2425import nsk.share.*;26import nsk.share.jpda.*;27import nsk.share.jdi.*;2829import com.sun.jdi.*;30import java.io.*;31import java.util.*;323334public class compareto001 {35//------------------------------------------------------- immutable common fields3637final static String SIGNAL_READY = "ready";38final static String SIGNAL_GO = "go";39final static String SIGNAL_QUIT = "quit";4041private static int waitTime;42private static int exitStatus;43private static ArgumentHandler argHandler;44private static Log log;45private static IOPipe pipe;46private static Debugee debuggee;47private static ReferenceType debuggeeClass;4849//------------------------------------------------------- mutable common fields5051private final static String prefix = "nsk.jdi.DoubleValue.compareTo";52private final static String className = ".compareto001";53private final static String debuggerName = prefix + className;54private final static String debuggeeName = debuggerName + "a";5556//------------------------------------------------------- test specific fields5758private final static String objectToCheck = "testedObj";59private final static String arrPrimitives = "testedFields";60private static Value objectValue;61private static List fieldList;6263//------------------------------------------------------- immutable common methods6465public static void main(String argv[]) {66System.exit(Consts.JCK_STATUS_BASE + run(argv, System.out));67}6869private static void display(String msg) {70log.display("debugger > " + msg);71}7273private static void complain(String msg) {74log.complain("debugger FAILURE > " + msg);75}7677public static int run(String argv[], PrintStream out) {7879exitStatus = Consts.TEST_FAILED;8081argHandler = new ArgumentHandler(argv);82log = new Log(out, argHandler);83waitTime = argHandler.getWaitTime() * 60000;8485debuggee = Debugee.prepareDebugee(argHandler, log, debuggeeName);8687debuggeeClass = debuggee.classByName(debuggeeName);88if ( debuggeeClass == null ) {89complain("Class '" + debuggeeName + "' not found.");90exitStatus = Consts.TEST_FAILED;91}9293execTest();9495debuggee.quit();9697return exitStatus;98}99100//------------------------------------------------------ mutable common method101102private static void execTest() {103debuggeeClass = debuggee.classByName(debuggeeName);104if ( debuggeeClass == null ) {105complain("Class '" + debuggeeName + "' not found.");106return;107}108109// getting of object to check110Field field = debuggeeClass.fieldByName(objectToCheck);111if ( field == null ) {112complain("Field '" + objectToCheck + "' not found.");113return;114}115116objectValue = debuggeeClass.getValue(field);117if ( objectValue == null ) {118complain("Field '" + objectToCheck + "' not initialized.");119return;120}121122// geting of array of primitive types123field = debuggeeClass.fieldByName(arrPrimitives);124if ( field == null ) {125complain("Field '" + arrPrimitives + "' not found.");126return;127}128Value arrValue = debuggeeClass.getValue(field);129if ( arrValue == null || !(arrValue instanceof ArrayReference) ) {130complain("Field '" + arrValue + "' is wrong.");131return;132}133ArrayReference primitiveValues = (ArrayReference)arrValue;134135fieldList = ((ClassType )objectValue.type()).allFields();136137Value v1, currentValue;138DoubleValue value;139Field fldOtherType;140141exitStatus = Consts.TEST_PASSED;142143// comparing loop144for (int i = 0; i < fieldList.size(); i++ ) {145field = (Field )fieldList.get(i);146v1 = ((ObjectReference )objectValue).getValue(field);147if ( !(v1 instanceof DoubleValue) ) {148exitStatus = Consts.TEST_FAILED;149continue;150}151value = (DoubleValue )v1;152153// comparing with debuggee's fields154display("Checking compateTo(Object object) method for DoubleValue: " + value);155156for (int j = 0; j < primitiveValues.length(); j++) {157arrValue = primitiveValues.getValue(j);158159fldOtherType = debuggeeClass.fieldByName(((StringReference )arrValue).value());160if ( fldOtherType == null ) {161complain("Field '" + arrValue + "' not found.");162exitStatus = Consts.TEST_FAILED;163continue;164}165166currentValue = debuggeeClass.getValue(fldOtherType);167168if ( !PerformComparing(value, currentValue) )169exitStatus = Consts.TEST_FAILED;170}171}172173}174175//--------------------------------------------------------- test specific methods176177178private static boolean PerformComparing(DoubleValue value, Object object ) {179boolean result = true;180181// assertion [ x.compareTo(x) == 0 ]182if (value.compareTo(value) != 0) {183complain("Failed assertion [ x.compareTo(x) == 0 ] for value: " + value.toString());184result = false;185}186187if (object instanceof DoubleValue) {188DoubleValue doubleObject = (DoubleValue)object;189try {190// assertion [ x.compareTo(y) == 0 <==> x.equals(y) ]191if ( ((value.equals(object)) && (value.compareTo(doubleObject) != 0)) ||192(!(value.equals(object)) && (value.compareTo(doubleObject) == 0)) ) {193complain("Failed assertion [ (x.compareTo(y) == 0) is identical to (x.equals(y) == true) ] \n\t" +194"where 'x' is DoubleValue: " + value + " and 'y' is DoubleValue : " + doubleObject + " \n\t" +195"result of (x.compareTo(y)): " + value.compareTo(doubleObject) + "\n\t" +196"result of (x.equals(y)): " + value.equals(object) );197result = false;198}199200// assertion [ x.compareTo(y) == 0 <==> y.compareTo(x) == 0 ]201if ( ((value.compareTo(doubleObject) == 0) && (doubleObject.compareTo(value) != 0)) ||202((value.compareTo(doubleObject) != 0) && (doubleObject.compareTo(value) == 0)) ) {203complain("Failed assertion [ (x.compareTo(y) == 0) is identical to (y.compareTo(x) == 0) ] \n\t" +204"where 'x' is DoubleValue: " + value + " and 'y' is DoubleValue : " + doubleObject + " \n\t" +205"result of (x.compareTo(y)): " + value.compareTo(doubleObject) + "\n\t" +206"result of (y.compareTo(x)): " + doubleObject.compareTo(value) );207result = false;208}209if (value.compareTo(doubleObject) != 0) {210// assertion [ if (x.compareTo(y) == i) then (y.compareTo(x) == -i) ]211if (value.compareTo(doubleObject) != -(doubleObject.compareTo(value))) {212complain("Failed assertion [ if (x.compareTo(y) == i) then (y.compareTo(x) == -i) ] \n\t" +213"where 'x' is DoubleValue: " + value + " and 'y' is DoubleValue : " + doubleObject + " \n\t" +214"result of (x.compareTo(y)): " + value.compareTo(doubleObject) + "\n\t" +215"result of (y.compareTo(x)): " + doubleObject.compareTo(value) );216result = false;217}218}219220// assertion [ if (x.compareTo(y) > 0) and (y.compareTo(z) > 0), then (x.compareTo(z) > 0) ]221if (value.compareTo(doubleObject) > 0) {222DoubleValue lessValue = FindLessDoubleValue(doubleObject);223if (lessValue != null) {224if (value.compareTo(lessValue) <= 0) {225complain("Failed assertion [ if (x.compareTo(y) > 0) and (y.compareTo(z) > 0), then (x.compareTo(z) > 0) ] \n\t" +226"where 'x' is DoubleValue: " + value + " , 'y' is DoubleValue : " + doubleObject + " , 'z' is DoubleValue : " + lessValue + " \n\t" +227"result of (x.compareTo(y)): " + value.compareTo(doubleObject) + "\n\t" +228"result of (y.compareTo(z)): " + doubleObject.compareTo(lessValue) + "\n\t" +229"result of (x.compareTo(z)): " + value.compareTo(lessValue) );230result = false;231}232}233}234235} catch (Exception e) {236complain("Caught unexpected " + e + " when comparing \n\t" +237"DoubleValue: " + value + " and DoubleValue argument: " + object);238result = false;239}240241} else if (object == null) {242try {243value.compareTo(null);244complain("Does not throw expected NullPointerException when comparing \n\t" +245"DoubleValue: " + value + " and null argument");246result = false;247} catch (NullPointerException ne) {248// continue249} catch (Exception e) {250complain("Caught unexpected " + e + " when comparing \n\t" +251"DoubleValue: " + value + " and null argument");252result = false;253}254} else {255try {256value.compareTo((DoubleValue)object);257complain("Does not throw expected ClassCastException when comparing \n\t" +258"DoubleValue: " + value + " and argument: " + object);259result = false;260} catch (ClassCastException ne) {261// continue262} catch (Exception e) {263complain("Caught unexpected " + e + " when comparing \n\t" +264"DoubleValue: " + value + " and argument: " + object);265result = false;266}267}268269return result;270}271272/**273* This function searches the static <i>fieldList</i> - the list of mirrored274* fields of debuggee's <i>compareto001aClassToCheck</i> class. Search is aimed275* to find another DoubleValue field which is less then method's argument via276* <i>compareTo</i> method.277*/278279private static DoubleValue FindLessDoubleValue (DoubleValue currentValue) {280DoubleValue result = null;281282for (int i = 0; i < fieldList.size(); i++ ) {283284Field field = (Field )fieldList.get(i);285DoubleValue newValue = (DoubleValue)((ObjectReference )objectValue).getValue(field);286287if (currentValue.compareTo(newValue) > 0) {288result = newValue;289break;290}291}292return result;293}294}295//--------------------------------------------------------- test specific classes296297298