1/*
2 * Copyright (c) 2007, 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// Checkstyle: stop
24package org.graalvm.compiler.jtt.hotpath;
25
26import java.util.Random;
27
28import org.junit.Test;
29
30import org.graalvm.compiler.jtt.JTTTest;
31
32/*
33 */
34public class HP_life extends JTTTest {
35
36    public static int test(int generations) {
37        reset();
38        for (int i = 0; i < generations; ++i) {
39            step();
40        }
41        int sum = 0;
42        for (int row = 0; row < rows; ++row) {
43            for (int col = 0; col < cols; ++col) {
44                boolean value = cell(row, col);
45                sum += (row * 15223242 + col * 21623234) ^ ((value ? 1 : 0) * 15323142);
46            }
47        }
48        return sum;
49    }
50
51    private static final int rows = 20;
52    private static final int cols = 20;
53    private static boolean cells[] = new boolean[rows * cols];
54
55    private static boolean cell(int row, int col) {
56        return ((row >= 0) && (row < rows) && (col >= 0) && (col < cols) && cells[row * cols + col]);
57    }
58
59    private static boolean step() {
60        boolean next[] = new boolean[rows * cols];
61        boolean changed = false;
62        for (int row = rows - 1; row >= 0; --row) {
63            int row_offset = row * cols;
64            for (int col = cols - 1; col >= 0; --col) {
65                int count = 0;
66                if (cell(row - 1, col - 1)) {
67                    count++;
68                }
69                if (cell(row - 1, col)) {
70                    count++;
71                }
72                if (cell(row - 1, col + 1)) {
73                    count++;
74                }
75                if (cell(row, col - 1)) {
76                    count++;
77                }
78                if (cell(row, col + 1)) {
79                    count++;
80                }
81                if (cell(row + 1, col - 1)) {
82                    count++;
83                }
84                if (cell(row + 1, col)) {
85                    count++;
86                }
87                if (cell(row + 1, col + 1)) {
88                    count++;
89                }
90                boolean old_state = cells[row_offset + col];
91                boolean new_state = (!old_state && count == 3) || (old_state && (count == 2 || count == 3));
92                if (!changed && new_state != old_state) {
93                    changed = true;
94                }
95                next[row_offset + col] = new_state;
96            }
97        }
98        cells = next;
99        return changed;
100    }
101
102    private static void reset() {
103        Random random = new Random(0);
104        boolean cells2[] = HP_life.cells;
105        for (int offset = 0; offset < cells2.length; ++offset) {
106            cells2[offset] = random.nextDouble() > 0.5;
107        }
108    }
109
110    @Test
111    public void run0() throws Throwable {
112        runTest("test", 5);
113    }
114
115}
116