1 /*
2  * Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 package nsk.jdi.IntegerValue.compareTo;
25 
26 import nsk.share.*;
27 import nsk.share.jpda.*;
28 import nsk.share.jdi.*;
29 
30 import com.sun.jdi.*;
31 import java.io.*;
32 import java.util.*;
33 
34 
35 public class compareto001 {
36     //------------------------------------------------------- immutable common fields
37 
38     final static String SIGNAL_READY = "ready";
39     final static String SIGNAL_GO    = "go";
40     final static String SIGNAL_QUIT  = "quit";
41 
42     private static int waitTime;
43     private static int exitStatus;
44     private static ArgumentHandler     argHandler;
45     private static Log                 log;
46     private static IOPipe pipe;
47     private static Debugee             debuggee;
48     private static ReferenceType       debuggeeClass;
49 
50     //------------------------------------------------------- mutable common fields
51 
52     private final static String prefix = "nsk.jdi.IntegerValue.compareTo";
53     private final static String className = ".compareto001";
54     private final static String debuggerName = prefix + className;
55     private final static String debuggeeName = debuggerName + "a";
56 
57     //------------------------------------------------------- test specific fields
58 
59     private final static String objectToCheck = "testedObj";
60     private final static String arrPrimitives = "testedFields";
61     private static Value objectValue;
62     private static List fieldList;
63 
64     //------------------------------------------------------- immutable common methods
65 
main(String argv[])66     public static void main(String argv[]) {
67         System.exit(Consts.JCK_STATUS_BASE + run(argv, System.out));
68     }
69 
display(String msg)70     private static void display(String msg) {
71         log.display("debugger > " + msg);
72     }
73 
complain(String msg)74     private static void complain(String msg) {
75         log.complain("debugger FAILURE > " + msg);
76     }
77 
run(String argv[], PrintStream out)78     public static int run(String argv[], PrintStream out) {
79 
80         exitStatus = Consts.TEST_FAILED;
81 
82         argHandler = new ArgumentHandler(argv);
83         log = new Log(out, argHandler);
84         waitTime = argHandler.getWaitTime() * 60000;
85 
86         debuggee = Debugee.prepareDebugee(argHandler, log, debuggeeName);
87 
88         debuggeeClass = debuggee.classByName(debuggeeName);
89         if ( debuggeeClass == null ) {
90             complain("Class '" + debuggeeName + "' not found.");
91             exitStatus = Consts.TEST_FAILED;
92         }
93 
94         execTest();
95 
96         debuggee.quit();
97 
98         return exitStatus;
99     }
100 
101     //------------------------------------------------------ mutable common method
102 
execTest()103     private static void execTest() {
104         debuggeeClass = debuggee.classByName(debuggeeName);
105         if ( debuggeeClass == null ) {
106             complain("Class '" + debuggeeName + "' not found.");
107             return;
108         }
109 
110         // getting of object to check
111         Field field = debuggeeClass.fieldByName(objectToCheck);
112         if ( field == null ) {
113             complain("Field '" + objectToCheck + "' not found.");
114             return;
115         }
116 
117         objectValue = debuggeeClass.getValue(field);
118         if ( objectValue == null ) {
119             complain("Field '" + objectToCheck + "' not initialized.");
120             return;
121         }
122 
123         // geting of array of primitive types
124         field = debuggeeClass.fieldByName(arrPrimitives);
125         if ( field == null ) {
126             complain("Field '" + arrPrimitives + "' not found.");
127             return;
128         }
129         Value arrValue = debuggeeClass.getValue(field);
130         if ( arrValue == null || !(arrValue instanceof ArrayReference) ) {
131             complain("Field '" + arrValue + "' is wrong.");
132             return;
133         }
134         ArrayReference primitiveValues = (ArrayReference)arrValue;
135 
136         fieldList = ((ClassType )objectValue.type()).allFields();
137 
138         Value v1, currentValue;
139         IntegerValue value;
140         Field fldOtherType;
141 
142         exitStatus = Consts.TEST_PASSED;
143 
144         // comparing loop
145         for (int i = 0; i < fieldList.size(); i++ ) {
146             field = (Field )fieldList.get(i);
147             v1 = ((ObjectReference )objectValue).getValue(field);
148             if ( !(v1 instanceof IntegerValue) ) {
149                 exitStatus = Consts.TEST_FAILED;
150                 continue;
151             }
152             value = (IntegerValue )v1;
153 
154             // comparing with debuggee's fields
155             display("Checking compateTo(Object object) method for IntegerValue: " + value);
156 
157             for (int j = 0; j < primitiveValues.length(); j++) {
158                 arrValue = primitiveValues.getValue(j);
159 
160                 fldOtherType = debuggeeClass.fieldByName(((StringReference )arrValue).value());
161                 if ( fldOtherType == null ) {
162                     complain("Field '" + arrValue + "' not found.");
163                     exitStatus = Consts.TEST_FAILED;
164                     continue;
165                 }
166 
167                 currentValue = debuggeeClass.getValue(fldOtherType);
168 
169                 if ( !PerformComparing(value, currentValue) )
170                     exitStatus = Consts.TEST_FAILED;
171             }
172         }
173 
174     }
175 
176     //--------------------------------------------------------- test specific methods
177 
178 
PerformComparing(IntegerValue value, Object object )179     private static boolean PerformComparing(IntegerValue value, Object object ) {
180         boolean result = true;
181 
182         // assertion [ x.compareTo(x) == 0 ]
183         if (value.compareTo(value) != 0) {
184             complain("Failed assertion [ x.compareTo(x) == 0 ] for value: " + value.toString());
185             result = false;
186         }
187 
188         if (object instanceof IntegerValue) {
189             IntegerValue intObject = (IntegerValue)object;
190             try {
191                 // assertion [ x.compareTo(y) == 0 <==> x.equals(y) ]
192                 if ( ((value.equals(object)) && (value.compareTo(intObject) != 0)) ||
193                      (!(value.equals(object)) && (value.compareTo(intObject) == 0)) ) {
194                     complain("Failed assertion [ (x.compareTo(y) == 0) is identical to (x.equals(y) == true) ] \n\t" +
195                              "where 'x' is IntegerValue: " + value + " and 'y' is IntegerValue : " + intObject + " \n\t" +
196                              "result of (x.compareTo(y)): " + value.compareTo(intObject) + "\n\t" +
197                              "result of (x.equals(y)): " + value.equals(object) );
198                     result = false;
199                 }
200 
201                 // assertion [ x.compareTo(y) == 0 <==> y.compareTo(x) == 0 ]
202                 if ( ((value.compareTo(intObject) == 0) && (intObject.compareTo(value) != 0)) ||
203                      ((value.compareTo(intObject) != 0) && (intObject.compareTo(value) == 0)) ) {
204                     complain("Failed assertion [ (x.compareTo(y) == 0) is identical to (y.compareTo(x) == 0) ] \n\t" +
205                              "where 'x' is IntegerValue: " + value + " and 'y' is IntegerValue : " + intObject + " \n\t" +
206                              "result of (x.compareTo(y)): " + value.compareTo(intObject) + "\n\t" +
207                              "result of (y.compareTo(x)): " + intObject.compareTo(value) );
208                     result = false;
209                 }
210                 if (value.compareTo(intObject) != 0) {
211                     // assertion [ if (x.compareTo(y) == i) then (y.compareTo(x) == -i) ]
212                     if (value.compareTo(intObject) != -(intObject.compareTo(value))) {
213                         complain("Failed assertion [ if (x.compareTo(y) == i) then (y.compareTo(x) == -i) ] \n\t" +
214                              "where 'x' is IntegerValue: " + value + " and 'y' is IntegerValue : " + intObject + " \n\t" +
215                              "result of (x.compareTo(y)): " + value.compareTo(intObject) + "\n\t" +
216                              "result of (y.compareTo(x)): " + intObject.compareTo(value) );
217                         result = false;
218                     }
219                 }
220 
221                 // assertion [ if (x.compareTo(y) > 0) and (y.compareTo(z) > 0), then (x.compareTo(z) > 0) ]
222                 if (value.compareTo(intObject) > 0) {
223                      IntegerValue lessValue = FindLessIntegerValue(intObject);
224                      if (lessValue != null) {
225                          if (value.compareTo(lessValue) <= 0) {
226                               complain("Failed assertion [ if (x.compareTo(y) > 0) and (y.compareTo(z) > 0), then (x.compareTo(z) > 0) ] \n\t" +
227                              "where 'x' is IntegerValue: " + value + " , 'y' is IntegerValue : " + intObject + " , 'z' is IntegerValue : " + lessValue + " \n\t" +
228                              "result of (x.compareTo(y)): " + value.compareTo(intObject) + "\n\t" +
229                              "result of (y.compareTo(z)): " + intObject.compareTo(lessValue)  + "\n\t" +
230                              "result of (x.compareTo(z)): " + value.compareTo(lessValue) );
231                               result = false;
232                          }
233                      }
234                 }
235 
236             } catch (Exception e) {
237                 complain("Caught unexpected " + e + " when comparing \n\t" +
238                          "IntegerValue: " + value + " and IntegerValue argument: " + object);
239                 result = false;
240             }
241 
242         } else if (object == null) {
243             try {
244                 value.compareTo(null);
245                 complain("Does not throw expected NullPointerException when comparing \n\t" +
246                          "IntegerValue: " + value + " and null argument");
247                 result = false;
248             } catch (NullPointerException ne) {
249                 // continue
250             } catch (Exception e) {
251                 complain("Caught unexpected " + e + " when comparing \n\t" +
252                          "IntegerValue: " + value + " and null argument");
253                 result = false;
254             }
255         } else {
256             try {
257                 value.compareTo((IntegerValue)object);
258                 complain("Does not throw expected ClassCastException when comparing \n\t" +
259                          "IntegerValue: " + value + " and argument: " + object);
260                 result = false;
261             } catch (ClassCastException ne) {
262                 // continue
263             } catch (Exception e) {
264                 complain("Caught unexpected " + e + " when comparing \n\t" +
265                          "IntegerValue: " + value + " and argument: " + object);
266                 result = false;
267             }
268         }
269 
270         return result;
271     }
272 
273     /**
274      *  This function searches the static <i>fieldList</i> - the list of mirrored
275      *  fields of debuggee's <i>compareto001aClassToCheck</i> class. Search is aimed
276      *  to find another IntegerValue field which is less then method's argument via
277      *  <i>compareTo</i> method.
278      */
279 
FindLessIntegerValue(IntegerValue currentValue)280     private static IntegerValue FindLessIntegerValue (IntegerValue currentValue) {
281         IntegerValue result = null;
282 
283         for (int i = 0; i < fieldList.size(); i++ ) {
284 
285             Field field = (Field )fieldList.get(i);
286             IntegerValue newValue = (IntegerValue)((ObjectReference )objectValue).getValue(field);
287 
288             if (currentValue.compareTo(newValue) > 0) {
289                 result = newValue;
290                 break;
291             }
292         }
293         return result;
294     }
295 }
296 //--------------------------------------------------------- test specific classes
297