aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.core.test/src/org/jacoco/core/test/perf/targets/Target03.java
blob: 9ce6bff31f48e1519ffebeb3d81d06dc8fdb55d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*******************************************************************************
 * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Marc R. Hoffmann - initial API and implementation
 *    
 *******************************************************************************/
package org.jacoco.core.test.perf.targets;

import java.util.Random;
import java.util.concurrent.Callable;

/**
 * "Game of Life" implementation as a more reference scenario. Obviously the
 * implementation could be more elegant using several classes, but the test
 * runner targets one class only. Also one could think about more efficient
 * implementations which again is not the focus here.
 */
public class Target03 implements Callable<Void> {

	private final int width;

	private final int height;

	private boolean[][] field;

	public Target03(int width, int height) {
		this.width = width;
		this.height = height;
		this.field = createField();
	}

	public Target03() {
		this(64, 64);
	}

	private boolean[][] createField() {
		boolean[][] f = new boolean[height][];
		for (int i = 0; i < height; i++) {
			f[i] = new boolean[width];
		}
		return f;
	}

	public void set(int x, int y, boolean flag) {
		field[wrap(x, width)][wrap(y, height)] = flag;
	}

	public boolean get(int x, int y) {
		return field[wrap(x, width)][wrap(y, height)];
	}

	public void clear() {
		field = createField();
	}

	public void randomFill(long seed, int count) {
		Random r = new Random(seed);
		for (int i = 0; i < count; i++) {
			set(r.nextInt(), r.nextInt(), true);
		}

	}

	public void tick() {
		boolean[][] next = createField();
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				final int n = getNeighbors(x, y);
				if (get(x, y)) {
					next[x][y] = 2 <= n && n <= 3;
				} else {
					next[x][y] = n == 3;
				}
			}
		}
		field = next;
	}

	// Neighbor
	private int getNeighbors(int x, int y) {
		int count = 0;
		if (get(x - 1, y - 1)) {
			count++;
		}
		if (get(x + 0, y - 1)) {
			count++;
		}
		if (get(x + 1, y - 1)) {
			count++;
		}
		if (get(x + 1, y + 0)) {
			count++;
		}
		if (get(x + 1, y + 1)) {
			count++;
		}
		if (get(x + 0, y + 1)) {
			count++;
		}
		if (get(x - 1, y + 1)) {
			count++;
		}
		if (get(x - 1, y + 0)) {
			count++;
		}
		return count;
	}

	private int wrap(int value, int size) {
		int res = value % size;
		if (res < 0) {
			res += size;
		}
		return res;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				sb.append(get(x, y) ? 'O' : '.');
			}
			sb.append('\n');
		}
		return sb.toString();
	}

	public Void call() throws Exception {
		clear();
		randomFill(123, width * height / 2);
		for (int i = 0; i < 20; i++) {
			tick();
		}
		return null;
	}

	// Demo
	public static void main(String[] args) {
		Target03 t = new Target03(10, 10);
		t.randomFill(123, 20);

		for (int i = 0; i < 10; i++) {
			System.out.println("Generation " + i + ":");
			System.out.println(t);
			t.tick();
		}
	}

}