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         zeroTest();
     29     }
     30 
     31     static void b2296099Test() throws Exception {
     32        int x = -1190771042;
     33        int dist = 360530809;
     34        int xl = -1190771042;
     35        int distl = 360530809;
     36 
     37        for (int i = 0; i < 100000; i++) {
     38            int b = rotateLeft(x, dist);
     39            if (b != 1030884493)
     40                throw new RuntimeException("Unexpected value: " + b
     41                        + " after " + i + " iterations");
     42        }
     43        for (int i = 0; i < 100000; i++) {
     44            long bl = rotateLeft(xl, distl);
     45            if (bl != 1030884493)
     46                throw new RuntimeException("Unexpected value: " + bl
     47                        + " after " + i + " iterations");
     48        }
     49        System.out.println("b2296099 passes");
     50    }
     51 
     52     static int rotateLeft(int i, int distance) {
     53         return ((i << distance) | (i >>> (-distance)));
     54     }
     55 
     56     static void b2302318Test() {
     57         System.gc();
     58 
     59         SpinThread slow = new SpinThread(Thread.MIN_PRIORITY);
     60         SpinThread fast1 = new SpinThread(Thread.NORM_PRIORITY);
     61         SpinThread fast2 = new SpinThread(Thread.MAX_PRIORITY);
     62 
     63         slow.setDaemon(true);
     64         fast1.setDaemon(true);
     65         fast2.setDaemon(true);
     66 
     67         fast2.start();
     68         slow.start();
     69         fast1.start();
     70         try {
     71             Thread.sleep(3000);
     72         } catch (InterruptedException ie) {/*ignore */}
     73         System.gc();
     74 
     75         System.out.println("b2302318 passes");
     76     }
     77 
     78     static void b2487514Test() {
     79         PriorityBlockingQueue q = new PriorityBlockingQueue(10);
     80         int catchCount = 0;
     81 
     82         q.offer(new Integer(0));
     83         /*
     84          * Warm up the code cache to have toArray() compiled. The key here is
     85          * to pass a compatible type so that there are no exceptions when
     86          * executing the method body (ie the APUT_OBJECT bytecode).
     87          */
     88         for (int i = 0; i < 1000; i++) {
     89             Integer[] ints = (Integer[]) q.toArray(new Integer[5]);
     90         }
     91 
     92         /* Now pass an incompatible type which is guaranteed to throw */
     93         for (int i = 0; i < 1000; i++) {
     94             try {
     95                 Object[] obj = q.toArray(new String[5]);
     96             }
     97             catch (ArrayStoreException  success) {
     98                 catchCount++;
     99             }
    100         }
    101 
    102         if (catchCount == 1000) {
    103             System.out.println("b2487514 passes");
    104         }
    105         else {
    106             System.out.println("b2487514 fails: catchCount is " + catchCount +
    107                                " (expecting 1000)");
    108         }
    109     }
    110 
    111     static void b5884080Test() {
    112         int vA = 1;
    113 
    114         int l = 0;
    115         do {
    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     static void zeroTest() throws Exception {
    131         ZeroTests zt = new ZeroTests();
    132         try {
    133             zt.longDivTest();
    134         } catch (Throwable th) {
    135             th.printStackTrace();
    136         }
    137         try {
    138             zt.longModTest();
    139         } catch (Throwable th) {
    140             th.printStackTrace();
    141         }
    142     }
    143 }
    144 
    145 class SpinThread extends Thread {
    146     int mPriority;
    147 
    148     SpinThread(int prio) {
    149         super("Spin prio=" + prio);
    150         mPriority = prio;
    151     }
    152 
    153     public void run() {
    154         setPriority(mPriority);
    155         while (true) {}
    156     }
    157 }
    158