1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 public class TestCompare {
18 
19   /// CHECK-START: void TestCompare.$opt$noinline$testReplaceInputWithItself(int) builder (after)
20   /// CHECK-DAG:     <<ArgX:i\d+>>   ParameterValue
21   /// CHECK-DAG:     <<Zero:i\d+>>   IntConstant 0
22   /// CHECK-DAG:     <<Cmp:i\d+>>    Compare [<<ArgX>>,<<Zero>>]
23   /// CHECK-DAG:                     GreaterThanOrEqual [<<Cmp>>,<<Zero>>]
24 
25   /// CHECK-START: void TestCompare.$opt$noinline$testReplaceInputWithItself(int) instruction_simplifier (after)
26   /// CHECK-DAG:     <<ArgX:i\d+>>   ParameterValue
27   /// CHECK-DAG:     <<Zero:i\d+>>   IntConstant 0
28   /// CHECK-DAG:                     GreaterThanOrEqual [<<ArgX>>,<<Zero>>]
29 
$opt$noinline$testReplaceInputWithItself(int x)30   public static void $opt$noinline$testReplaceInputWithItself(int x) {
31     // The instruction builder first replaces Integer.compare(x, 0) with Compare HIR
32     // and then the instruction simplifier merges the Compare into the GreaterThanOrEqual.
33     // This is a regression test to check that it is allowed to replace the second
34     // input of the GreaterThanOrEqual, i.e. <<Zero>>, with the very same instruction.
35     if (Integer.compare(x, 0) < 0) {
36       System.out.println("OOOPS");
37     }
38   }
39 
40   /// CHECK-START: int TestCompare.compareBooleans(boolean, boolean) select_generator (after)
41   /// CHECK-NOT:                     Phi
42 
43   /// CHECK-START: int TestCompare.compareBooleans(boolean, boolean) instruction_simplifier$before_codegen (after)
44   /// CHECK:         <<ArgX:z\d+>>   ParameterValue
45   /// CHECK:         <<ArgY:z\d+>>   ParameterValue
46   /// CHECK-DAG:     <<Result:i\d+>> Compare [<<ArgX>>,<<ArgY>>]
47   /// CHECK-DAG:                     Return [<<Result>>]
48 
49   /// CHECK-START: int TestCompare.compareBooleans(boolean, boolean) instruction_simplifier$before_codegen (after)
50   /// CHECK-NOT:                     Select
51 
compareBooleans(boolean x, boolean y)52   private static int compareBooleans(boolean x, boolean y) {
53     return Integer.compare((x ? 1 : 0), (y ? 1 : 0));
54   }
55 
56   ///  CHECK-START: int TestCompare.compareBooleans2(boolean, boolean) builder (after)
57   ///  CHECK-DAG:     <<Zero:i\d+>>   IntConstant 0
58   ///  CHECK-DAG:     <<One:i\d+>>    IntConstant 1
59   ///  CHECK-DAG:     <<PhiX:i\d+>>   Phi [<<One>>,<<Zero>>]
60   ///  CHECK-DAG:     <<PhiY:i\d+>>   Phi [<<One>>,<<Zero>>]
61   ///  CHECK-DAG:     <<Result:i\d+>> Compare [<<PhiX>>,<<PhiY>>]
62   ///  CHECK-DAG:                     Return [<<Result>>]
63 
64   ///  CHECK-START: int TestCompare.compareBooleans2(boolean, boolean) builder (after)
65   ///  CHECK-NOT:                     InvokeStaticOrDirect
66 
67   ///  CHECK-START: int TestCompare.compareBooleans2(boolean, boolean) select_generator (after)
68   ///  CHECK:         <<ArgX:z\d+>>   ParameterValue
69   ///  CHECK:         <<ArgY:z\d+>>   ParameterValue
70   ///  CHECK-DAG:     <<Zero:i\d+>>   IntConstant 0
71   ///  CHECK-DAG:     <<One:i\d+>>    IntConstant 1
72   ///  CHECK-DAG:     <<SelX:i\d+>>   Select [<<Zero>>,<<One>>,<<ArgX>>]
73   ///  CHECK-DAG:     <<SelY:i\d+>>   Select [<<Zero>>,<<One>>,<<ArgY>>]
74   ///  CHECK-DAG:     <<Result:i\d+>> Compare [<<SelX>>,<<SelY>>]
75   ///  CHECK-DAG:                     Return [<<Result>>]
76 
77   ///  CHECK-START: int TestCompare.compareBooleans2(boolean, boolean) select_generator (after)
78   ///  CHECK-NOT:                     Phi
79 
80   ///  CHECK-START: int TestCompare.compareBooleans2(boolean, boolean) instruction_simplifier$before_codegen (after)
81   ///  CHECK:         <<ArgX:z\d+>>   ParameterValue
82   ///  CHECK:         <<ArgY:z\d+>>   ParameterValue
83   ///  CHECK-DAG:     <<Result:i\d+>> Compare [<<ArgX>>,<<ArgY>>]
84   ///  CHECK-DAG:                     Return [<<Result>>]
85 
86   ///  CHECK-START: int TestCompare.compareBooleans2(boolean, boolean) instruction_simplifier$before_codegen (after)
87   ///  CHECK-NOT:                     Select
88 
compareBooleans2(boolean x, boolean y)89   private static int compareBooleans2(boolean x, boolean y) {
90     // Note: D8 would replace the ternary expression `x ? 1 : 0` with `x`
91     // but explicit `if` is preserved.
92     int src_x;
93     if (x) {
94       src_x = 1;
95     } else {
96       src_x = 0;
97     }
98     int src_y;
99     if (y) {
100       src_y = 1;
101     } else {
102       src_y = 0;
103     }
104     return Integer.compare(src_x, src_y);
105   }
106 
107   /// CHECK-START: int TestCompare.compareBytes(byte, byte) builder (after)
108   /// CHECK-DAG:     <<Result:i\d+>> Compare
109   /// CHECK-DAG:                     Return [<<Result>>]
110 
111   /// CHECK-START: int TestCompare.compareBytes(byte, byte) builder (after)
112   /// CHECK-NOT:                     InvokeStaticOrDirect
113 
compareBytes(byte x, byte y)114   private static int compareBytes(byte x, byte y) {
115     return Integer.compare(x, y);
116   }
117 
118   /// CHECK-START: int TestCompare.compareShorts(short, short) builder (after)
119   /// CHECK-DAG:     <<Result:i\d+>> Compare
120   /// CHECK-DAG:                     Return [<<Result>>]
121 
122   /// CHECK-START: int TestCompare.compareShorts(short, short) builder (after)
123   /// CHECK-NOT:                     InvokeStaticOrDirect
124 
compareShorts(short x, short y)125   private static int compareShorts(short x, short y) {
126     return Integer.compare(x, y);
127   }
128 
129   /// CHECK-START: int TestCompare.compareChars(char, char) builder (after)
130   /// CHECK-DAG:     <<Result:i\d+>> Compare
131   /// CHECK-DAG:                     Return [<<Result>>]
132 
133   /// CHECK-START: int TestCompare.compareChars(char, char) builder (after)
134   /// CHECK-NOT:                     InvokeStaticOrDirect
135 
compareChars(char x, char y)136   private static int compareChars(char x, char y) {
137     return Integer.compare(x, y);
138   }
139 
140   /// CHECK-START: int TestCompare.compareInts(int, int) builder (after)
141   /// CHECK-DAG:     <<Result:i\d+>> Compare
142   /// CHECK-DAG:                     Return [<<Result>>]
143 
144   /// CHECK-START: int TestCompare.compareInts(int, int) builder (after)
145   /// CHECK-NOT:                     InvokeStaticOrDirect
146 
compareInts(int x, int y)147   private static int compareInts(int x, int y) {
148     return Integer.compare(x, y);
149   }
150 
151   /// CHECK-START: int TestCompare.compareLongs(long, long) builder (after)
152   /// CHECK-DAG:     <<Result:i\d+>> Compare
153   /// CHECK-DAG:                     Return [<<Result>>]
154 
155   /// CHECK-START: int TestCompare.compareLongs(long, long) builder (after)
156   /// CHECK-NOT:                     InvokeStaticOrDirect
157 
compareLongs(long x, long y)158   private static int compareLongs(long x, long y) {
159     return Long.compare(x, y);
160   }
161 
162 
163   /// CHECK-START: int TestCompare.compareByteShort(byte, short) builder (after)
164   /// CHECK-DAG:     <<Result:i\d+>> Compare
165   /// CHECK-DAG:                     Return [<<Result>>]
166 
167   /// CHECK-START: int TestCompare.compareByteShort(byte, short) builder (after)
168   /// CHECK-NOT:                     InvokeStaticOrDirect
169 
compareByteShort(byte x, short y)170   public static int compareByteShort(byte x, short y) {
171     return Integer.compare(x, y);
172   }
173 
174   /// CHECK-START: int TestCompare.compareByteChar(byte, char) builder (after)
175   /// CHECK-DAG:     <<Result:i\d+>> Compare
176   /// CHECK-DAG:                     Return [<<Result>>]
177 
178   /// CHECK-START: int TestCompare.compareByteChar(byte, char) builder (after)
179   /// CHECK-NOT:                     InvokeStaticOrDirect
180 
compareByteChar(byte x, char y)181   public static int compareByteChar(byte x, char y) {
182     return Integer.compare(x, y);
183   }
184 
185   /// CHECK-START: int TestCompare.compareByteInt(byte, int) builder (after)
186   /// CHECK-DAG:     <<Result:i\d+>> Compare
187   /// CHECK-DAG:                     Return [<<Result>>]
188 
189   /// CHECK-START: int TestCompare.compareByteInt(byte, int) builder (after)
190   /// CHECK-NOT:                     InvokeStaticOrDirect
191 
compareByteInt(byte x, int y)192   public static int compareByteInt(byte x, int y) {
193     return Integer.compare(x, y);
194   }
195 
196 
197   /// CHECK-START: int TestCompare.compareShortByte(short, byte) builder (after)
198   /// CHECK-DAG:     <<Result:i\d+>> Compare
199   /// CHECK-DAG:                     Return [<<Result>>]
200 
201   /// CHECK-START: int TestCompare.compareShortByte(short, byte) builder (after)
202   /// CHECK-NOT:                     InvokeStaticOrDirect
203 
compareShortByte(short x, byte y)204   public static int compareShortByte(short x, byte y) {
205     return Integer.compare(x, y);
206   }
207 
208   /// CHECK-START: int TestCompare.compareShortChar(short, char) builder (after)
209   /// CHECK-DAG:     <<Result:i\d+>> Compare
210   /// CHECK-DAG:                     Return [<<Result>>]
211 
212   /// CHECK-START: int TestCompare.compareShortChar(short, char) builder (after)
213   /// CHECK-NOT:                     InvokeStaticOrDirect
214 
compareShortChar(short x, char y)215   public static int compareShortChar(short x, char y) {
216     return Integer.compare(x, y);
217   }
218 
219   /// CHECK-START: int TestCompare.compareShortInt(short, int) builder (after)
220   /// CHECK-DAG:     <<Result:i\d+>> Compare
221   /// CHECK-DAG:                     Return [<<Result>>]
222 
223   /// CHECK-START: int TestCompare.compareShortInt(short, int) builder (after)
224   /// CHECK-NOT:                     InvokeStaticOrDirect
225 
compareShortInt(short x, int y)226   public static int compareShortInt(short x, int y) {
227     return Integer.compare(x, y);
228   }
229 
230 
231   /// CHECK-START: int TestCompare.compareCharByte(char, byte) builder (after)
232   /// CHECK-DAG:     <<Result:i\d+>> Compare
233   /// CHECK-DAG:                     Return [<<Result>>]
234 
235   /// CHECK-START: int TestCompare.compareCharByte(char, byte) builder (after)
236   /// CHECK-NOT:                     InvokeStaticOrDirect
237 
compareCharByte(char x, byte y)238   public static int compareCharByte(char x, byte y) {
239     return Integer.compare(x, y);
240   }
241 
242   /// CHECK-START: int TestCompare.compareCharShort(char, short) builder (after)
243   /// CHECK-DAG:     <<Result:i\d+>> Compare
244   /// CHECK-DAG:                     Return [<<Result>>]
245 
246   /// CHECK-START: int TestCompare.compareCharShort(char, short) builder (after)
247   /// CHECK-NOT:                     InvokeStaticOrDirect
248 
compareCharShort(char x, short y)249   public static int compareCharShort(char x, short y) {
250     return Integer.compare(x, y);
251   }
252 
253   /// CHECK-START: int TestCompare.compareCharInt(char, int) builder (after)
254   /// CHECK-DAG:     <<Result:i\d+>> Compare
255   /// CHECK-DAG:                     Return [<<Result>>]
256 
257   /// CHECK-START: int TestCompare.compareCharInt(char, int) builder (after)
258   /// CHECK-NOT:                     InvokeStaticOrDirect
259 
compareCharInt(char x, int y)260   public static int compareCharInt(char x, int y) {
261     return Integer.compare(x, y);
262   }
263 
264 
265   /// CHECK-START: int TestCompare.compareIntByte(int, byte) builder (after)
266   /// CHECK-DAG:     <<Result:i\d+>> Compare
267   /// CHECK-DAG:                     Return [<<Result>>]
268 
269   /// CHECK-START: int TestCompare.compareIntByte(int, byte) builder (after)
270   /// CHECK-NOT:                     InvokeStaticOrDirect
271 
compareIntByte(int x, byte y)272   public static int compareIntByte(int x, byte y) {
273     return Integer.compare(x, y);
274   }
275 
276   /// CHECK-START: int TestCompare.compareIntShort(int, short) builder (after)
277   /// CHECK-DAG:     <<Result:i\d+>> Compare
278   /// CHECK-DAG:                     Return [<<Result>>]
279 
280   /// CHECK-START: int TestCompare.compareIntShort(int, short) builder (after)
281   /// CHECK-NOT:                     InvokeStaticOrDirect
282 
compareIntShort(int x, short y)283   public static int compareIntShort(int x, short y) {
284     return Integer.compare(x, y);
285   }
286 
287   /// CHECK-START: int TestCompare.compareIntChar(int, char) builder (after)
288   /// CHECK-DAG:     <<Result:i\d+>> Compare
289   /// CHECK-DAG:                     Return [<<Result>>]
290 
291   /// CHECK-START: int TestCompare.compareIntChar(int, char) builder (after)
292   /// CHECK-NOT:                     InvokeStaticOrDirect
293 
compareIntChar(int x, char y)294   public static int compareIntChar(int x, char y) {
295     return Integer.compare(x, y);
296   }
297 
298 
testCompareBooleans()299   public static void testCompareBooleans() {
300     expectEquals(-1, compareBooleans(false, true));
301     expectEquals(-1, compareBooleans2(false, true));
302 
303     expectEquals(0, compareBooleans(false, false));
304     expectEquals(0, compareBooleans(true, true));
305     expectEquals(0, compareBooleans2(false, false));
306     expectEquals(0, compareBooleans2(true, true));
307 
308     expectEquals(1, compareBooleans(true, false));
309     expectEquals(1, compareBooleans2(true, false));
310   }
311 
testCompareBytes()312   public static void testCompareBytes() {
313     expectEquals(-1, compareBytes(Byte.MIN_VALUE, (byte)(Byte.MIN_VALUE + 1)));
314     expectEquals(-1, compareBytes(Byte.MIN_VALUE, (byte)-1));
315     expectEquals(-1, compareBytes(Byte.MIN_VALUE, (byte)0));
316     expectEquals(-1, compareBytes(Byte.MIN_VALUE, (byte)1));
317     expectEquals(-1, compareBytes(Byte.MIN_VALUE, Byte.MAX_VALUE));
318     expectEquals(-1, compareBytes((byte)-1, (byte)0));
319     expectEquals(-1, compareBytes((byte)-1, (byte)1));
320     expectEquals(-1, compareBytes((byte)0, (byte)1));
321 
322     expectEquals(0, compareBytes(Byte.MIN_VALUE, Byte.MIN_VALUE));
323     expectEquals(0, compareBytes((byte)-1, (byte)-1));
324     expectEquals(0, compareBytes((byte)0, (byte)0));
325     expectEquals(0, compareBytes((byte)1, (byte)1));
326     expectEquals(0, compareBytes(Byte.MAX_VALUE, Byte.MAX_VALUE));
327 
328     expectEquals(1, compareBytes((byte)0, (byte)-1));
329     expectEquals(1, compareBytes((byte)1, (byte)-1));
330     expectEquals(1, compareBytes((byte)1, (byte)0));
331     expectEquals(1, compareBytes(Byte.MAX_VALUE, Byte.MIN_VALUE));
332     expectEquals(1, compareBytes(Byte.MAX_VALUE, (byte)-1));
333     expectEquals(1, compareBytes(Byte.MAX_VALUE, (byte)0));
334     expectEquals(1, compareBytes(Byte.MAX_VALUE, (byte)1));
335     expectEquals(1, compareBytes(Byte.MAX_VALUE, (byte)(Byte.MAX_VALUE - 1)));
336 
337     for (byte i = -11; i <= 11; i++) {
338       for (byte j = -11; j <= 11; j++) {
339         int expected = 0;
340         if (i < j) expected = -1;
341         else if (i > j) expected = 1;
342         expectEquals(expected, compareBytes(i, j));
343       }
344     }
345   }
346 
testCompareShorts()347   public static void testCompareShorts() {
348     expectEquals(-1, compareShorts(Short.MIN_VALUE, (short)(Short.MIN_VALUE + 1)));
349     expectEquals(-1, compareShorts(Short.MIN_VALUE, (short)-1));
350     expectEquals(-1, compareShorts(Short.MIN_VALUE, (short)0));
351     expectEquals(-1, compareShorts(Short.MIN_VALUE, (short)1));
352     expectEquals(-1, compareShorts(Short.MIN_VALUE, (short)Short.MAX_VALUE));
353     expectEquals(-1, compareShorts((short)-1, (short)0));
354     expectEquals(-1, compareShorts((short)-1, (short)1));
355     expectEquals(-1, compareShorts((short)0, (short)1));
356 
357     expectEquals(0, compareShorts(Short.MIN_VALUE, Short.MIN_VALUE));
358     expectEquals(0, compareShorts((short)-1, (short)-1));
359     expectEquals(0, compareShorts((short)0, (short)0));
360     expectEquals(0, compareShorts((short)1, (short)1));
361     expectEquals(0, compareShorts(Short.MAX_VALUE, Short.MAX_VALUE));
362 
363     expectEquals(1, compareShorts((short)0, (short)-1));
364     expectEquals(1, compareShorts((short)1, (short)-1));
365     expectEquals(1, compareShorts((short)1, (short)0));
366     expectEquals(1, compareShorts(Short.MAX_VALUE, Short.MIN_VALUE));
367     expectEquals(1, compareShorts(Short.MAX_VALUE, (short)-1));
368     expectEquals(1, compareShorts(Short.MAX_VALUE, (short)0));
369     expectEquals(1, compareShorts(Short.MAX_VALUE, (short)1));
370     expectEquals(1, compareShorts(Short.MAX_VALUE, (short)(Short.MAX_VALUE - 1)));
371 
372     for (short i = -11; i <= 11; i++) {
373       for (short j = -11; j <= 11; j++) {
374         int expected = 0;
375         if (i < j) expected = -1;
376         else if (i > j) expected = 1;
377         expectEquals(expected, compareShorts(i, j));
378       }
379     }
380   }
381 
testCompareChars()382   public static void testCompareChars() {
383     expectEquals(-1, compareChars((char)0, Character.MAX_VALUE));
384     expectEquals(-1, compareChars((char)0, (char)1));
385 
386     expectEquals(0, compareChars((char)0, (char)0));
387     expectEquals(0, compareChars((char)1, (char)1));
388     expectEquals(0, compareChars(Character.MAX_VALUE, Character.MAX_VALUE));
389 
390     expectEquals(1, compareChars((char)1, (char)0));
391     expectEquals(1, compareChars(Character.MAX_VALUE, (char)0));
392     expectEquals(1, compareChars(Character.MAX_VALUE, (char)1));
393     expectEquals(1, compareChars(Character.MAX_VALUE, (char)(Character.MAX_VALUE - 1)));
394 
395     for (char i = 0; i <= 11; i++) {
396       for (char j = 0; j <= 11; j++) {
397         int expected = 0;
398         if (i < j) expected = -1;
399         else if (i > j) expected = 1;
400         expectEquals(expected, compareChars(i, j));
401       }
402     }
403   }
404 
testCompareInts()405   public static void testCompareInts() {
406     expectEquals(-1, compareInts(Integer.MIN_VALUE, Integer.MIN_VALUE + 1));
407     expectEquals(-1, compareInts(Integer.MIN_VALUE, -1));
408     expectEquals(-1, compareInts(Integer.MIN_VALUE, 0));
409     expectEquals(-1, compareInts(Integer.MIN_VALUE, 1));
410     expectEquals(-1, compareInts(Integer.MIN_VALUE, Integer.MAX_VALUE));
411     expectEquals(-1, compareInts(-1, 0));
412     expectEquals(-1, compareInts(-1, 1));
413     expectEquals(-1, compareInts(0, 1));
414 
415     expectEquals(0, compareInts(Integer.MIN_VALUE, Integer.MIN_VALUE));
416     expectEquals(0, compareInts(-1, -1));
417     expectEquals(0, compareInts(0, 0));
418     expectEquals(0, compareInts(1, 1));
419     expectEquals(0, compareInts(Integer.MAX_VALUE, Integer.MAX_VALUE));
420 
421     expectEquals(1, compareInts(0, -1));
422     expectEquals(1, compareInts(1, -1));
423     expectEquals(1, compareInts(1, 0));
424     expectEquals(1, compareInts(Integer.MAX_VALUE, Integer.MIN_VALUE));
425     expectEquals(1, compareInts(Integer.MAX_VALUE, -1));
426     expectEquals(1, compareInts(Integer.MAX_VALUE, 0));
427     expectEquals(1, compareInts(Integer.MAX_VALUE, 1));
428     expectEquals(1, compareInts(Integer.MAX_VALUE, Integer.MAX_VALUE - 1));
429 
430     for (int i = -11; i <= 11; i++) {
431       for (int j = -11; j <= 11; j++) {
432         int expected = 0;
433         if (i < j) expected = -1;
434         else if (i > j) expected = 1;
435         expectEquals(expected, compareInts(i, j));
436       }
437     }
438   }
439 
testCompareLongs()440   public static void testCompareLongs() {
441     expectEquals(-1, compareLongs(Long.MIN_VALUE, Long.MIN_VALUE + 1L));
442     expectEquals(-1, compareLongs(Long.MIN_VALUE, -1L));
443     expectEquals(-1, compareLongs(Long.MIN_VALUE, 0L));
444     expectEquals(-1, compareLongs(Long.MIN_VALUE, 1L));
445     expectEquals(-1, compareLongs(Long.MIN_VALUE, Long.MAX_VALUE));
446     expectEquals(-1, compareLongs(-1L, 0L));
447     expectEquals(-1, compareLongs(-1L, 1L));
448     expectEquals(-1, compareLongs(0L, 1L));
449 
450     expectEquals(0, compareLongs(Long.MIN_VALUE, Long.MIN_VALUE));
451     expectEquals(0, compareLongs(-1L, -1L));
452     expectEquals(0, compareLongs(0L, 0L));
453     expectEquals(0, compareLongs(1L, 1L));
454     expectEquals(0, compareLongs(Long.MAX_VALUE, Long.MAX_VALUE));
455 
456     expectEquals(1, compareLongs(0L, -1L));
457     expectEquals(1, compareLongs(1L, -1L));
458     expectEquals(1, compareLongs(1L, 0L));
459     expectEquals(1, compareLongs(Long.MAX_VALUE, Long.MIN_VALUE));
460     expectEquals(1, compareLongs(Long.MAX_VALUE, -1L));
461     expectEquals(1, compareLongs(Long.MAX_VALUE, 0L));
462     expectEquals(1, compareLongs(Long.MAX_VALUE, 1L));
463     expectEquals(1, compareLongs(Long.MAX_VALUE, Long.MAX_VALUE - 1L));
464 
465     expectEquals(-1, compareLongs(0x111111117FFFFFFFL, 0x11111111FFFFFFFFL));
466     expectEquals(0, compareLongs(0x111111117FFFFFFFL, 0x111111117FFFFFFFL));
467     expectEquals(1, compareLongs(0x11111111FFFFFFFFL, 0x111111117FFFFFFFL));
468 
469     for (long i = -11L; i <= 11L; i++) {
470       for (long j = -11L; j <= 11L; j++) {
471         int expected = 0;
472         if (i < j) expected = -1;
473         else if (i > j) expected = 1;
474         expectEquals(expected, compareLongs(i, j));
475       }
476     }
477 
478     for (long i = Long.MIN_VALUE; i <= Long.MIN_VALUE + 11L; i++) {
479       expectEquals(-1, compareLongs(i, 0));
480     }
481 
482     for (long i = Long.MAX_VALUE; i >= Long.MAX_VALUE - 11L; i--) {
483       expectEquals(1, compareLongs(i, 0));
484     }
485   }
486 
487 
testCompareByteShort()488   public static void testCompareByteShort() {
489     expectEquals(-1, compareByteShort(Byte.MIN_VALUE, (short)-1));
490     expectEquals(-1, compareByteShort(Byte.MIN_VALUE, (short)0));
491     expectEquals(-1, compareByteShort(Byte.MIN_VALUE, (short)1));
492     expectEquals(-1, compareByteShort(Byte.MIN_VALUE, Short.MAX_VALUE));
493     expectEquals(-1, compareByteShort((byte)-1, (short)0));
494     expectEquals(-1, compareByteShort((byte)-1, (short)1));
495     expectEquals(-1, compareByteShort((byte)0, (short)1));
496     expectEquals(-1, compareByteShort(Byte.MAX_VALUE, (short)(Short.MAX_VALUE - 1)));
497     expectEquals(-1, compareByteShort(Byte.MAX_VALUE, Short.MAX_VALUE));
498 
499     expectEquals(0, compareByteShort((byte)-1, (short)-1));
500     expectEquals(0, compareByteShort((byte)0, (short)0));
501     expectEquals(0, compareByteShort((byte)1, (short)1));
502 
503     expectEquals(1, compareByteShort(Byte.MIN_VALUE, Short.MIN_VALUE));
504     expectEquals(1, compareByteShort(Byte.MIN_VALUE, (short)(Short.MIN_VALUE + 1)));
505     expectEquals(1, compareByteShort((byte)0, (short)-1));
506     expectEquals(1, compareByteShort((byte)1, (short)-1));
507     expectEquals(1, compareByteShort((byte)1, (short)0));
508     expectEquals(1, compareByteShort(Byte.MAX_VALUE, Short.MIN_VALUE));
509     expectEquals(1, compareByteShort(Byte.MAX_VALUE, (short)-1));
510     expectEquals(1, compareByteShort(Byte.MAX_VALUE, (short)0));
511     expectEquals(1, compareByteShort(Byte.MAX_VALUE, (short)1));
512 
513     for (byte i = -11; i <= 11; i++) {
514       for (short j = -11; j <= 11; j++) {
515         int expected = 0;
516         if (i < j) expected = -1;
517         else if (i > j) expected = 1;
518         expectEquals(expected, compareByteShort(i, j));
519       }
520     }
521   }
522 
testCompareByteChar()523   public static void testCompareByteChar() {
524     expectEquals(-1, compareByteChar(Byte.MIN_VALUE, (char)0));
525     expectEquals(-1, compareByteChar(Byte.MIN_VALUE, (char)1));
526     expectEquals(-1, compareByteChar(Byte.MIN_VALUE, Character.MAX_VALUE));
527     expectEquals(-1, compareByteChar((byte)-1, (char)0));
528     expectEquals(-1, compareByteChar((byte)-1, (char)1));
529     expectEquals(-1, compareByteChar((byte)0, (char)1));
530     expectEquals(-1, compareByteChar(Byte.MAX_VALUE, (char)(Character.MAX_VALUE - 1)));
531     expectEquals(-1, compareByteChar(Byte.MAX_VALUE, Character.MAX_VALUE));
532 
533     expectEquals(0, compareByteChar((byte)0, (char)0));
534     expectEquals(0, compareByteChar((byte)1, (char)1));
535 
536     expectEquals(1, compareByteChar((byte)1, (char)0));
537     expectEquals(1, compareByteChar(Byte.MAX_VALUE, (char)0));
538     expectEquals(1, compareByteChar(Byte.MAX_VALUE, (char)1));
539 
540     for (byte i = -11; i <= 11; i++) {
541       for (char j = 0; j <= 11; j++) {
542         int expected = 0;
543         if (i < j) expected = -1;
544         else if (i > j) expected = 1;
545         expectEquals(expected, compareByteChar(i, j));
546       }
547     }
548   }
549 
testCompareByteInt()550   public static void testCompareByteInt() {
551     expectEquals(-1, compareByteInt(Byte.MIN_VALUE, -1));
552     expectEquals(-1, compareByteInt(Byte.MIN_VALUE, 0));
553     expectEquals(-1, compareByteInt(Byte.MIN_VALUE, 1));
554     expectEquals(-1, compareByteInt(Byte.MIN_VALUE, Integer.MAX_VALUE));
555     expectEquals(-1, compareByteInt((byte)-1, 0));
556     expectEquals(-1, compareByteInt((byte)-1, 1));
557     expectEquals(-1, compareByteInt((byte)0, 1));
558     expectEquals(-1, compareByteInt(Byte.MAX_VALUE, Integer.MAX_VALUE - 1));
559     expectEquals(-1, compareByteInt(Byte.MAX_VALUE, Integer.MAX_VALUE));
560 
561     expectEquals(0, compareByteInt((byte)-1, -1));
562     expectEquals(0, compareByteInt((byte)0, 0));
563     expectEquals(0, compareByteInt((byte)1, 1));
564 
565     expectEquals(1, compareByteInt(Byte.MIN_VALUE, Integer.MIN_VALUE));
566     expectEquals(1, compareByteInt(Byte.MIN_VALUE, Integer.MIN_VALUE + 1));
567     expectEquals(1, compareByteInt((byte)0, -1));
568     expectEquals(1, compareByteInt((byte)1, -1));
569     expectEquals(1, compareByteInt((byte)1, 0));
570     expectEquals(1, compareByteInt(Byte.MAX_VALUE, Integer.MIN_VALUE));
571     expectEquals(1, compareByteInt(Byte.MAX_VALUE, -1));
572     expectEquals(1, compareByteInt(Byte.MAX_VALUE, 0));
573     expectEquals(1, compareByteInt(Byte.MAX_VALUE, 1));
574 
575     for (byte i = -11; i <= 11; i++) {
576       for (int j = -11; j <= 11; j++) {
577         int expected = 0;
578         if (i < j) expected = -1;
579         else if (i > j) expected = 1;
580         expectEquals(expected, compareByteInt(i, j));
581       }
582     }
583   }
584 
585 
testCompareShortByte()586   public static void testCompareShortByte() {
587     expectEquals(-1, compareShortByte(Short.MIN_VALUE, Byte.MIN_VALUE));
588     expectEquals(-1, compareShortByte(Short.MIN_VALUE, (byte)(Byte.MIN_VALUE + 1)));
589     expectEquals(-1, compareShortByte(Short.MIN_VALUE, (byte)-1));
590     expectEquals(-1, compareShortByte(Short.MIN_VALUE, (byte)0));
591     expectEquals(-1, compareShortByte(Short.MIN_VALUE, (byte)1));
592     expectEquals(-1, compareShortByte(Short.MIN_VALUE, Byte.MAX_VALUE));
593     expectEquals(-1, compareShortByte((short)-1, (byte)0));
594     expectEquals(-1, compareShortByte((short)-1, (byte)1));
595     expectEquals(-1, compareShortByte((short)0, (byte)1));
596 
597     expectEquals(0, compareShortByte((short)-1, (byte)-1));
598     expectEquals(0, compareShortByte((short)0, (byte)0));
599     expectEquals(0, compareShortByte((short)1, (byte)1));
600 
601     expectEquals(1, compareShortByte((short)0, (byte)-1));
602     expectEquals(1, compareShortByte((short)1, (byte)-1));
603     expectEquals(1, compareShortByte((short)1, (byte)0));
604     expectEquals(1, compareShortByte(Short.MAX_VALUE, Byte.MIN_VALUE));
605     expectEquals(1, compareShortByte(Short.MAX_VALUE, (byte)-1));
606     expectEquals(1, compareShortByte(Short.MAX_VALUE, (byte)0));
607     expectEquals(1, compareShortByte(Short.MAX_VALUE, (byte)1));
608     expectEquals(1, compareShortByte(Short.MAX_VALUE, (byte)(Byte.MAX_VALUE - 1)));
609     expectEquals(1, compareShortByte(Short.MAX_VALUE, Byte.MAX_VALUE));
610 
611     for (short i = -11; i <= 11; i++) {
612       for (byte j = -11; j <= 11; j++) {
613         int expected = 0;
614         if (i < j) expected = -1;
615         else if (i > j) expected = 1;
616         expectEquals(expected, compareShortByte(i, j));
617       }
618     }
619   }
620 
testCompareShortChar()621   public static void testCompareShortChar() {
622     expectEquals(-1, compareShortChar(Short.MIN_VALUE, (char)0));
623     expectEquals(-1, compareShortChar(Short.MIN_VALUE, (char)1));
624     expectEquals(-1, compareShortChar(Short.MIN_VALUE, Character.MAX_VALUE));
625     expectEquals(-1, compareShortChar((short)-1, (char)0));
626     expectEquals(-1, compareShortChar((short)-1, (char)1));
627     expectEquals(-1, compareShortChar((short)0, (char)1));
628     expectEquals(-1, compareShortChar(Short.MAX_VALUE, (char)(Character.MAX_VALUE - 1)));
629     expectEquals(-1, compareShortChar(Short.MAX_VALUE, Character.MAX_VALUE));
630 
631     expectEquals(0, compareShortChar((short)0, (char)0));
632     expectEquals(0, compareShortChar((short)1, (char)1));
633 
634     expectEquals(1, compareShortChar((short)1, (char)0));
635     expectEquals(1, compareShortChar(Short.MAX_VALUE, (char)0));
636     expectEquals(1, compareShortChar(Short.MAX_VALUE, (char)1));
637 
638     for (short i = -11; i <= 11; i++) {
639       for (char j = 0; j <= 11; j++) {
640         int expected = 0;
641         if (i < j) expected = -1;
642         else if (i > j) expected = 1;
643         expectEquals(expected, compareShortChar(i, j));
644       }
645     }
646   }
647 
testCompareShortInt()648   public static void testCompareShortInt() {
649     expectEquals(-1, compareShortInt(Short.MIN_VALUE, -1));
650     expectEquals(-1, compareShortInt(Short.MIN_VALUE, 0));
651     expectEquals(-1, compareShortInt(Short.MIN_VALUE, 1));
652     expectEquals(-1, compareShortInt(Short.MIN_VALUE, Integer.MAX_VALUE));
653     expectEquals(-1, compareShortInt((short)-1, 0));
654     expectEquals(-1, compareShortInt((short)-1, 1));
655     expectEquals(-1, compareShortInt((short)0, 1));
656     expectEquals(-1, compareShortInt(Short.MAX_VALUE, Integer.MAX_VALUE - 1));
657     expectEquals(-1, compareShortInt(Short.MAX_VALUE, Integer.MAX_VALUE));
658 
659     expectEquals(0, compareShortInt((short)-1, -1));
660     expectEquals(0, compareShortInt((short)0, 0));
661     expectEquals(0, compareShortInt((short)1, 1));
662 
663     expectEquals(1, compareShortInt(Short.MIN_VALUE, Integer.MIN_VALUE));
664     expectEquals(1, compareShortInt(Short.MIN_VALUE, Integer.MIN_VALUE + 1));
665     expectEquals(1, compareShortInt((short)0, -1));
666     expectEquals(1, compareShortInt((short)1, -1));
667     expectEquals(1, compareShortInt((short)1, 0));
668     expectEquals(1, compareShortInt(Short.MAX_VALUE, Integer.MIN_VALUE));
669     expectEquals(1, compareShortInt(Short.MAX_VALUE, -1));
670     expectEquals(1, compareShortInt(Short.MAX_VALUE, 0));
671     expectEquals(1, compareShortInt(Short.MAX_VALUE, 1));
672 
673     for (short i = -11; i <= 11; i++) {
674       for (int j = -11; j <= 11; j++) {
675         int expected = 0;
676         if (i < j) expected = -1;
677         else if (i > j) expected = 1;
678         expectEquals(expected, compareShortInt(i, j));
679       }
680     }
681   }
682 
683 
testCompareCharByte()684   public static void testCompareCharByte() {
685     expectEquals(-1, compareCharByte((char)0, (byte)1));
686     expectEquals(-1, compareCharByte((char)0, Byte.MAX_VALUE));
687 
688     expectEquals(0, compareCharByte((char)0, (byte)0));
689     expectEquals(0, compareCharByte((char)1, (byte)1));
690 
691     expectEquals(1, compareCharByte((char)0, Byte.MIN_VALUE));
692     expectEquals(1, compareCharByte((char)0, (byte)(Byte.MIN_VALUE + 1)));
693     expectEquals(1, compareCharByte((char)0, (byte)-1));
694     expectEquals(1, compareCharByte((char)1, (byte)-1));
695     expectEquals(1, compareCharByte((char)1, (byte)0));
696     expectEquals(1, compareCharByte(Character.MAX_VALUE, Byte.MIN_VALUE));
697     expectEquals(1, compareCharByte(Character.MAX_VALUE, (byte)-1));
698     expectEquals(1, compareCharByte(Character.MAX_VALUE, (byte)0));
699     expectEquals(1, compareCharByte(Character.MAX_VALUE, (byte)1));
700     expectEquals(1, compareCharByte(Character.MAX_VALUE, (byte)(Byte.MAX_VALUE - 1)));
701     expectEquals(1, compareCharByte(Character.MAX_VALUE, Byte.MAX_VALUE));
702 
703     for (char i = 0; i <= 11; i++) {
704       for (byte j = -11; j <= 11; j++) {
705         int expected = 0;
706         if (i < j) expected = -1;
707         else if (i > j) expected = 1;
708         expectEquals(expected, compareCharByte(i, j));
709       }
710     }
711   }
712 
testCompareCharShort()713   public static void testCompareCharShort() {
714     expectEquals(-1, compareCharShort((char)0, (short)1));
715     expectEquals(-1, compareCharShort((char)0, Short.MAX_VALUE));
716 
717     expectEquals(0, compareCharShort((char)0, (short)0));
718     expectEquals(0, compareCharShort((char)1, (short)1));
719 
720     expectEquals(1, compareCharShort((char)0, Short.MIN_VALUE));
721     expectEquals(1, compareCharShort((char)0, (short)(Short.MIN_VALUE + 1)));
722     expectEquals(1, compareCharShort((char)0, (short)-1));
723     expectEquals(1, compareCharShort((char)1, (short)-1));
724     expectEquals(1, compareCharShort((char)1, (short)0));
725     expectEquals(1, compareCharShort(Character.MAX_VALUE, Short.MIN_VALUE));
726     expectEquals(1, compareCharShort(Character.MAX_VALUE, (short)-1));
727     expectEquals(1, compareCharShort(Character.MAX_VALUE, (short)0));
728     expectEquals(1, compareCharShort(Character.MAX_VALUE, (short)1));
729     expectEquals(1, compareCharShort(Character.MAX_VALUE, (short)(Short.MAX_VALUE - 1)));
730     expectEquals(1, compareCharShort(Character.MAX_VALUE, Short.MAX_VALUE));
731 
732     for (char i = 0; i <= 11; i++) {
733       for (short j = -11; j <= 11; j++) {
734         int expected = 0;
735         if (i < j) expected = -1;
736         else if (i > j) expected = 1;
737         expectEquals(expected, compareCharShort(i, j));
738       }
739     }
740   }
741 
testCompareCharInt()742   public static void testCompareCharInt() {
743     expectEquals(-1, compareCharInt((char)0, 1));
744     expectEquals(-1, compareCharInt((char)0, Integer.MAX_VALUE));
745     expectEquals(-1, compareCharInt(Character.MAX_VALUE, Integer.MAX_VALUE - 1));
746     expectEquals(-1, compareCharInt(Character.MAX_VALUE, Integer.MAX_VALUE));
747 
748     expectEquals(0, compareCharInt((char)0, 0));
749     expectEquals(0, compareCharInt((char)1, 1));
750 
751     expectEquals(1, compareCharInt((char)0, Integer.MIN_VALUE));
752     expectEquals(1, compareCharInt((char)0, Integer.MIN_VALUE + 1));
753     expectEquals(1, compareCharInt((char)0, -1));
754     expectEquals(1, compareCharInt((char)1, -1));
755     expectEquals(1, compareCharInt((char)1, 0));
756     expectEquals(1, compareCharInt(Character.MAX_VALUE, Integer.MIN_VALUE));
757     expectEquals(1, compareCharInt(Character.MAX_VALUE, -1));
758     expectEquals(1, compareCharInt(Character.MAX_VALUE, 0));
759     expectEquals(1, compareCharInt(Character.MAX_VALUE, 1));
760 
761     for (char i = 0; i <= 11; i++) {
762       for (int j = -11; j <= 11; j++) {
763         int expected = 0;
764         if (i < j) expected = -1;
765         else if (i > j) expected = 1;
766         expectEquals(expected, compareCharInt(i, j));
767       }
768     }
769   }
770 
771 
testCompareIntByte()772   public static void testCompareIntByte() {
773     expectEquals(-1, compareIntByte(Integer.MIN_VALUE, Byte.MIN_VALUE));
774     expectEquals(-1, compareIntByte(Integer.MIN_VALUE, (byte)(Byte.MIN_VALUE + 1)));
775     expectEquals(-1, compareIntByte(Integer.MIN_VALUE, (byte)-1));
776     expectEquals(-1, compareIntByte(Integer.MIN_VALUE, (byte)0));
777     expectEquals(-1, compareIntByte(Integer.MIN_VALUE, (byte)1));
778     expectEquals(-1, compareIntByte(Integer.MIN_VALUE, Byte.MAX_VALUE));
779     expectEquals(-1, compareIntByte(-1, (byte)0));
780     expectEquals(-1, compareIntByte(-1, (byte)1));
781     expectEquals(-1, compareIntByte(0, (byte)1));
782 
783     expectEquals(0, compareIntByte(-1, (byte)-1));
784     expectEquals(0, compareIntByte(0, (byte)0));
785     expectEquals(0, compareIntByte(1, (byte)1));
786 
787     expectEquals(1, compareIntByte(0, (byte)-1));
788     expectEquals(1, compareIntByte(1, (byte)-1));
789     expectEquals(1, compareIntByte(1, (byte)0));
790     expectEquals(1, compareIntByte(Integer.MAX_VALUE, Byte.MIN_VALUE));
791     expectEquals(1, compareIntByte(Integer.MAX_VALUE, (byte)-1));
792     expectEquals(1, compareIntByte(Integer.MAX_VALUE, (byte)0));
793     expectEquals(1, compareIntByte(Integer.MAX_VALUE, (byte)1));
794     expectEquals(1, compareIntByte(Integer.MAX_VALUE, (byte)(Byte.MAX_VALUE - 1)));
795     expectEquals(1, compareIntByte(Integer.MAX_VALUE, Byte.MAX_VALUE));
796 
797     for (int i = -11; i <= 11; i++) {
798       for (byte j = -11; j <= 11; j++) {
799         int expected = 0;
800         if (i < j) expected = -1;
801         else if (i > j) expected = 1;
802         expectEquals(expected, compareIntByte(i, j));
803       }
804     }
805   }
806 
testCompareIntShort()807   public static void testCompareIntShort() {
808     expectEquals(-1, compareIntShort(Integer.MIN_VALUE, Short.MIN_VALUE));
809     expectEquals(-1, compareIntShort(Integer.MIN_VALUE, (short)(Short.MIN_VALUE + 1)));
810     expectEquals(-1, compareIntShort(Integer.MIN_VALUE, (short)-1));
811     expectEquals(-1, compareIntShort(Integer.MIN_VALUE, (short)0));
812     expectEquals(-1, compareIntShort(Integer.MIN_VALUE, (short)1));
813     expectEquals(-1, compareIntShort(Integer.MIN_VALUE, Short.MAX_VALUE));
814     expectEquals(-1, compareIntShort(-1, (short)0));
815     expectEquals(-1, compareIntShort(-1, (short)1));
816     expectEquals(-1, compareIntShort(0, (short)1));
817 
818     expectEquals(0, compareIntShort(-1, (short)-1));
819     expectEquals(0, compareIntShort(0, (short)0));
820     expectEquals(0, compareIntShort(1, (short)1));
821 
822     expectEquals(1, compareIntShort(0, (short)-1));
823     expectEquals(1, compareIntShort(1, (short)-1));
824     expectEquals(1, compareIntShort(1, (short)0));
825     expectEquals(1, compareIntShort(Integer.MAX_VALUE, Short.MIN_VALUE));
826     expectEquals(1, compareIntShort(Integer.MAX_VALUE, (short)-1));
827     expectEquals(1, compareIntShort(Integer.MAX_VALUE, (short)0));
828     expectEquals(1, compareIntShort(Integer.MAX_VALUE, (short)1));
829     expectEquals(1, compareIntShort(Integer.MAX_VALUE, (short)(Short.MAX_VALUE - 1)));
830     expectEquals(1, compareIntShort(Integer.MAX_VALUE, Short.MAX_VALUE));
831 
832     for (int i = -11; i <= 11; i++) {
833       for (short j = -11; j <= 11; j++) {
834         int expected = 0;
835         if (i < j) expected = -1;
836         else if (i > j) expected = 1;
837         expectEquals(expected, compareIntShort(i, j));
838       }
839     }
840   }
841 
testCompareIntChar()842   public static void testCompareIntChar() {
843     expectEquals(-1, compareIntChar(Integer.MIN_VALUE, (char)0));
844     expectEquals(-1, compareIntChar(Integer.MIN_VALUE, (char)1));
845     expectEquals(-1, compareIntChar(Integer.MIN_VALUE, Character.MAX_VALUE));
846     expectEquals(-1, compareIntChar(-1, (char)0));
847     expectEquals(-1, compareIntChar(-1, (char)1));
848     expectEquals(-1, compareIntChar(0, (char)1));
849 
850     expectEquals(0, compareIntChar(0, (char)0));
851     expectEquals(0, compareIntChar(1, (char)1));
852 
853     expectEquals(1, compareIntChar(1, (char)0));
854     expectEquals(1, compareIntChar(Integer.MAX_VALUE, (char)0));
855     expectEquals(1, compareIntChar(Integer.MAX_VALUE, (char)1));
856     expectEquals(1, compareIntChar(Integer.MAX_VALUE, (char)(Character.MAX_VALUE - 1)));
857     expectEquals(1, compareIntChar(Integer.MAX_VALUE, Character.MAX_VALUE));
858 
859     for (int i = -11; i <= 11; i++) {
860       for (char j = 0; j <= 11; j++) {
861         int expected = 0;
862         if (i < j) expected = -1;
863         else if (i > j) expected = 1;
864         expectEquals(expected, compareIntChar(i, j));
865       }
866     }
867   }
868 
869 
main()870   public static void main() {
871     $opt$noinline$testReplaceInputWithItself(42);
872 
873     testCompareBooleans();
874     testCompareBytes();
875     testCompareShorts();
876     testCompareChars();
877     testCompareInts();
878     testCompareLongs();
879 
880     testCompareByteShort();
881     testCompareByteChar();
882     testCompareByteInt();
883 
884     testCompareShortByte();
885     testCompareShortChar();
886     testCompareShortInt();
887 
888     testCompareCharByte();
889     testCompareCharShort();
890     testCompareCharInt();
891 
892     testCompareIntByte();
893     testCompareIntShort();
894     testCompareIntChar();
895 
896     System.out.println("TestCompare passed");
897   }
898 
expectEquals(int expected, int result)899   private static void expectEquals(int expected, int result) {
900     if (expected != result) {
901       throw new Error("Expected: " + expected + ", found: " + result);
902     }
903   }
904 }
905