001/*
002 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.jtt.micro;
024
025import org.junit.*;
026
027import com.oracle.graal.jtt.*;
028
029/*
030 */
031public class Matrix01 extends JTTTest {
032
033    public static class Matrix {
034
035        final int id;
036
037        Matrix(int id) {
038            this.id = id;
039        }
040    }
041
042    public static int test(int arg) {
043        if (arg == 0) {
044            return matrix1(3) + matrix1(5);
045        }
046        if (arg == 1) {
047            return matrix2(3) + matrix2(5);
048        }
049        if (arg == 2) {
050            return matrix3(3) + matrix3(5);
051        }
052        if (arg == 3) {
053            return matrix4(3) + matrix4(5);
054        }
055        if (arg == 4) {
056            return matrix5(3) + matrix5(5);
057        }
058        return 42;
059    }
060
061    static int matrix1(int size) {
062        Matrix[] matrix = new Matrix[size];
063        fillMatrix(matrix, size);
064        int count = 0;
065        for (Matrix m : matrix) {
066            if (m != null) {
067                count++;
068            }
069        }
070        return count;
071    }
072
073    static int matrix2(int size) {
074        Matrix[][] matrix = new Matrix[size][size];
075        fillMatrix(matrix, size * size);
076        int count = 0;
077        for (Matrix[] n : matrix) {
078            for (Matrix m : n) {
079                if (m != null) {
080                    count++;
081                }
082            }
083        }
084        return count;
085    }
086
087    static int matrix3(int size) {
088        Matrix[][][] matrix = new Matrix[size][5][size];
089        fillMatrix(matrix, size * size * size);
090        int count = 0;
091        for (Matrix[][] o : matrix) {
092            for (Matrix[] n : o) {
093                for (Matrix m : n) {
094                    if (m != null) {
095                        count++;
096                    }
097                }
098            }
099        }
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}