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