001/*
002 * Copyright (c) 2007, 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 */
023// Checkstyle: stop
024package com.oracle.graal.jtt.hotpath;
025
026import java.util.*;
027
028import org.junit.*;
029
030import com.oracle.graal.jtt.*;
031
032/*
033 */
034public class HP_life extends JTTTest {
035
036    public static int test(int generations) {
037        reset();
038        for (int i = 0; i < generations; ++i) {
039            step();
040        }
041        int sum = 0;
042        for (int row = 0; row < rows; ++row) {
043            for (int col = 0; col < cols; ++col) {
044                boolean value = cell(row, col);
045                sum += (row * 15223242 + col * 21623234) ^ ((value ? 1 : 0) * 15323142);
046            }
047        }
048        return sum;
049    }
050
051    private static final int rows = 20;
052    private static final int cols = 20;
053    private static boolean cells[] = new boolean[rows * cols];
054
055    private static boolean cell(int row, int col) {
056        return ((row >= 0) && (row < rows) && (col >= 0) && (col < cols) && cells[row * cols + col]);
057    }
058
059    private static boolean step() {
060        boolean next[] = new boolean[rows * cols];
061        boolean changed = false;
062        for (int row = rows - 1; row >= 0; --row) {
063            int row_offset = row * cols;
064            for (int col = cols - 1; col >= 0; --col) {
065                int count = 0;
066                if (cell(row - 1, col - 1)) {
067                    count++;
068                }
069                if (cell(row - 1, col)) {
070                    count++;
071                }
072                if (cell(row - 1, col + 1)) {
073                    count++;
074                }
075                if (cell(row, col - 1)) {
076                    count++;
077                }
078                if (cell(row, col + 1)) {
079                    count++;
080                }
081                if (cell(row + 1, col - 1)) {
082                    count++;
083                }
084                if (cell(row + 1, col)) {
085                    count++;
086                }
087                if (cell(row + 1, col + 1)) {
088                    count++;
089                }
090                boolean old_state = cells[row_offset + col];
091                boolean new_state = (!old_state && count == 3) || (old_state && (count == 2 || count == 3));
092                if (!changed && new_state != old_state) {
093                    changed = true;
094                }
095                next[row_offset + col] = new_state;
096            }
097        }
098        cells = next;
099        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}