1 /*
   2  * Copyright (c) 2009, 2012, 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 package org.graalvm.compiler.jtt.micro;
  24 
  25 import org.junit.Test;
  26 
  27 import org.graalvm.compiler.jtt.JTTTest;
  28 
  29 /*
  30  */
  31 public class Matrix01 extends JTTTest {
  32 
  33     public static class Matrix {
  34 
  35         final int id;
  36 
  37         Matrix(int id) {
  38             this.id = id;
  39         }
  40     }
  41 
  42     public static int test(int arg) {
  43         if (arg == 0) {
  44             return matrix1(3) + matrix1(5);
  45         }
  46         if (arg == 1) {
  47             return matrix2(3) + matrix2(5);
  48         }
  49         if (arg == 2) {
  50             return matrix3(3) + matrix3(5);
  51         }
  52         if (arg == 3) {
  53             return matrix4(3) + matrix4(5);
  54         }
  55         if (arg == 4) {
  56             return matrix5(3) + matrix5(5);
  57         }
  58         return 42;
  59     }
  60 
  61     static int matrix1(int size) {
  62         Matrix[] matrix = new Matrix[size];
  63         fillMatrix(matrix, size);
  64         int count = 0;
  65         for (Matrix m : matrix) {
  66             if (m != null) {
  67                 count++;
  68             }
  69         }
  70         return count;
  71     }
  72 
  73     static int matrix2(int size) {
  74         Matrix[][] matrix = new Matrix[size][size];
  75         fillMatrix(matrix, size * size);
  76         int count = 0;
  77         for (Matrix[] n : matrix) {
  78             for (Matrix m : n) {
  79                 if (m != null) {
  80                     count++;
  81                 }
  82             }
  83         }
  84         return count;
  85     }
  86 
  87     static int matrix3(int size) {
  88         Matrix[][][] matrix = new Matrix[size][5][size];
  89         fillMatrix(matrix, size * size * size);
  90         int count = 0;
  91         for (Matrix[][] o : matrix) {
  92             for (Matrix[] n : o) {
  93                 for (Matrix m : n) {
  94                     if (m != null) {
  95                         count++;
  96                     }
  97                 }
  98             }
  99         }
 100         return count;
 101     }
 102 
 103     static int matrix4(int size) {
 104         Matrix[][][][] matrix = new Matrix[size][2][size][3];
 105         fillMatrix(matrix, size * size * size * size);
 106         int count = 0;
 107         for (Matrix[][][] p : matrix) {
 108             for (Matrix[][] o : p) {
 109                 for (Matrix[] n : o) {
 110                     for (Matrix m : n) {
 111                         if (m != null) {
 112                             count++;
 113                         }
 114                     }
 115                 }
 116             }
 117         }
 118         return count;
 119     }
 120 
 121     static int matrix5(int size) {
 122         Matrix[][][][][] matrix = new Matrix[size][size][3][4][size];
 123         fillMatrix(matrix, size * size * size * size * size);
 124         int count = 0;
 125         for (Matrix[][][][] q : matrix) {
 126             for (Matrix[][][] p : q) {
 127                 for (Matrix[][] o : p) {
 128                     for (Matrix[] n : o) {
 129                         for (Matrix m : n) {
 130                             if (m != null) {
 131                                 count++;
 132                             }
 133                         }
 134                     }
 135                 }
 136             }
 137         }
 138         return count;
 139     }
 140 
 141     static void fillMatrix(Object[] matrix, int total) {
 142         for (int i = 0; i < 10000; i += 7) {
 143             int number = i % total;
 144             set(matrix, number);
 145         }
 146     }
 147 
 148     static void set(Object[] matrix, int number) {
 149         int val = number;
 150         Object[] array = matrix;
 151         while (!(array instanceof Matrix[])) {
 152             int index = val % array.length;
 153             val = val / array.length;
 154             array = (Object[]) array[index];
 155         }
 156         ((Matrix[]) array)[val % array.length] = new Matrix(number);
 157     }
 158 
 159     @Test
 160     public void run0() throws Throwable {
 161         runTest("test", 0);
 162     }
 163 
 164     @Test
 165     public void run1() throws Throwable {
 166         runTest("test", 1);
 167     }
 168 
 169     @Test
 170     public void run2() throws Throwable {
 171         runTest("test", 2);
 172     }
 173 
 174     @Test
 175     public void run3() throws Throwable {
 176         runTest("test", 3);
 177     }
 178 
 179     @Test
 180     public void run4() throws Throwable {
 181         runTest("test", 4);
 182     }
 183 
 184     @Test
 185     public void run5() throws Throwable {
 186         runTest("test", 5);
 187     }
 188 
 189 }