1 /*
   2  * Copyright (c) 2015, 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 /*
  25  * @test
  26  * @bug 8085832
  27  * @bug 8135069
  28  * @summary x <= 0 || x > 0 wrongly folded as (x-1) >u -1 and x < 0 || x > -1 wrongly folded as x >u -1
  29  * @run main/othervm -XX:-BackgroundCompilation -XX:-UseOnStackReplacement TestBadFoldCompare
  30  */
  31 
  32 public class TestBadFoldCompare {
  33 
  34     static boolean test1_taken;
  35 
  36     static void helper1(int i, int a, int b, boolean flag) {
  37         if (flag) {
  38             if (i <= a || i > b) {
  39                 test1_taken = true;
  40             }
  41         }
  42     }
  43 
  44     static void test1(int i, boolean flag) {
  45         helper1(i, 0, 0, flag);
  46     }
  47 
  48     static boolean test2_taken;
  49 
  50     static void helper2(int i, int a, int b, boolean flag) {
  51         if (flag) {
  52             if (i > b || i <= a) {
  53                 test2_taken = true;
  54             }
  55         }
  56     }
  57 
  58     static void test2(int i, boolean flag) {
  59         helper2(i, 0, 0, flag);
  60     }
  61 
  62     static boolean test3_taken;
  63 
  64     static void helper3(int i, int a, int b, boolean flag) {
  65         if (flag) {
  66             if (i < a || i > b - 1) {
  67                 test3_taken = true;
  68             }
  69         }
  70     }
  71 
  72     static void test3(int i, boolean flag) {
  73         helper3(i, 0, 0, flag);
  74     }
  75 
  76     static boolean test4_taken;
  77 
  78     static void helper4(int i, int a, int b, boolean flag) {
  79         if (flag) {
  80             if (i > b - 1 || i < a) {
  81                 test4_taken = true;
  82             }
  83         }
  84     }
  85 
  86     static void test4(int i, boolean flag) {
  87         helper4(i, 0, 0, flag);
  88     }
  89 
  90     static public void main(String[] args) {
  91         boolean success = true;
  92 
  93         for (int i = 0; i < 20000; i++) {
  94             helper1(5, 0,  10, (i%2)==0);
  95             helper1(-1, 0,  10, (i%2)==0);
  96             helper1(15, 0,  10, (i%2)==0);
  97             test1(0, false);
  98         }
  99         test1_taken = false;
 100         test1(0, true);
 101         if (!test1_taken) {
 102             System.out.println("Test1 failed");
 103             success = false;
 104         }
 105 
 106         for (int i = 0; i < 20000; i++) {
 107             helper2(5, 0,  10, (i%2)==0);
 108             helper2(-1, 0,  10, (i%2)==0);
 109             helper2(15, 0,  10, (i%2)==0);
 110             test2(0, false);
 111         }
 112         test2_taken = false;
 113         test2(0, true);
 114 
 115         if (!test2_taken) {
 116             System.out.println("Test2 failed");
 117             success = false;
 118         }
 119 
 120         for (int i = 0; i < 20000; i++) {
 121             helper3(5, 0,  10, (i%2)==0);
 122             helper3(-1, 0,  10, (i%2)==0);
 123             helper3(15, 0,  10, (i%2)==0);
 124             test3(0, false);
 125         }
 126         test3_taken = false;
 127         test3(0, true);
 128 
 129         if (!test3_taken) {
 130             System.out.println("Test3 failed");
 131             success = false;
 132         }
 133 
 134         for (int i = 0; i < 20000; i++) {
 135             helper4(5, 0,  10, (i%2)==0);
 136             helper4(-1, 0,  10, (i%2)==0);
 137             helper4(15, 0,  10, (i%2)==0);
 138             test4(0, false);
 139         }
 140         test4_taken = false;
 141         test4(0, true);
 142 
 143         if (!test4_taken) {
 144             System.out.println("Test4 failed");
 145             success = false;
 146         }
 147 
 148         if (!success) {
 149             throw new RuntimeException("Some tests failed");
 150         }
 151     }
 152 }