Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2009 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 import java.util.concurrent.*;
     18 
     19 /**
     20  * Test for Jit regressions.
     21  */
     22 public class Main {
     23     public static void main(String args[]) throws Exception {
     24         b2296099Test();
     25         b2302318Test();
     26         b2487514Test();
     27         b5884080Test();
     28     }
     29 
     30     static void b2296099Test() throws Exception {
     31        int x = -1190771042;
     32        int dist = 360530809;
     33        int xl = -1190771042;
     34        int distl = 360530809;
     35 
     36        for (int i = 0; i < 100000; i++) {
     37            int b = rotateLeft(x, dist);
     38            if (b != 1030884493)
     39                throw new RuntimeException("Unexpected value: " + b
     40                        + " after " + i + " iterations");
     41        }
     42        for (int i = 0; i < 100000; i++) {
     43            long bl = rotateLeft(xl, distl);
     44            if (bl != 1030884493)
     45                throw new RuntimeException("Unexpected value: " + bl
     46                        + " after " + i + " iterations");
     47        }
     48        System.out.println("b2296099 passes");
     49    }
     50 
     51     static int rotateLeft(int i, int distance) {
     52         return ((i << distance) | (i >>> (-distance)));
     53     }
     54 
     55     static void b2302318Test() {
     56         System.gc();
     57 
     58         SpinThread slow = new SpinThread(Thread.MIN_PRIORITY);
     59         SpinThread fast1 = new SpinThread(Thread.NORM_PRIORITY);
     60         SpinThread fast2 = new SpinThread(Thread.MAX_PRIORITY);
     61 
     62         slow.setDaemon(true);
     63         fast1.setDaemon(true);
     64         fast2.setDaemon(true);
     65 
     66         fast2.start();
     67         slow.start();
     68         fast1.start();
     69         try {
     70             Thread.sleep(3000);
     71         } catch (InterruptedException ie) {/*ignore */}
     72         System.gc();
     73 
     74         System.out.println("b2302318 passes");
     75     }
     76 
     77     static void b2487514Test() {
     78         PriorityBlockingQueue q = new PriorityBlockingQueue(10);
     79         int catchCount = 0;
     80 
     81         q.offer(new Integer(0));
     82         /*
     83          * Warm up the code cache to have toArray() compiled. The key here is
     84          * to pass a compatible type so that there are no exceptions when
     85          * executing the method body (ie the APUT_OBJECT bytecode).
     86          */
     87         for (int i = 0; i < 1000; i++) {
     88             Integer[] ints = (Integer[]) q.toArray(new Integer[5]);
     89         }
     90 
     91         /* Now pass an incompatible type which is guaranteed to throw */
     92         for (int i = 0; i < 1000; i++) {
     93             try {
     94                 Object[] obj = q.toArray(new String[5]);
     95             }
     96             catch (ArrayStoreException  success) {
     97                 catchCount++;
     98             }
     99         }
    100 
    101         if (catchCount == 1000) {
    102             System.out.println("b2487514 passes");
    103         }
    104         else {
    105             System.out.println("b2487514 fails: catchCount is " + catchCount +
    106                                " (expecting 1000)");
    107         }
    108     }
    109 
    110     static void b5884080Test() {
    111         int vA = 1;
    112 
    113         int l = 0;
    114         do
    115         {
    116             int k = 0;
    117             do
    118                 vA += 1;
    119             while(++k < 100);
    120         } while(++l < 1000);
    121         if (vA == 100001) {
    122             System.out.println("b5884080 passes");
    123         }
    124         else {
    125             System.out.println("b5884080 fails: vA is " + vA +
    126                                " (expecting 100001)");
    127         }
    128     }
    129 }
    130 
    131 class SpinThread extends Thread {
    132     int mPriority;
    133 
    134     SpinThread(int prio) {
    135         super("Spin prio=" + prio);
    136         mPriority = prio;
    137     }
    138 
    139     public void run() {
    140         setPriority(mPriority);
    141         while (true) {}
    142     }
    143 }
    144