Skip to content

Commit 44186e8

Browse files
committed
Adds more tests & make CustomStat non static + bug fix in off()
1 parent a30cf79 commit 44186e8

File tree

5 files changed

+124
-28
lines changed

5 files changed

+124
-28
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<excludes>
7878
<!-- Exclude the PhysicalCores class from code coverage because it is too tiedly coupled with the runtime environment -->
7979
<exclude>**/PhysicalCores.class</exclude>
80+
<exclude>**/experimental/*</exclude>
8081
</excludes>
8182
</configuration>
8283
</plugin>

src/main/java/com/fathzer/games/util/CustomStats.java

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,97 +5,106 @@
55
import java.util.Set;
66
import java.util.concurrent.atomic.AtomicLong;
77

8-
/** A utility class to create custom statistics.
8+
/** A class to create custom statistics.
99
* <br>For instance, you can use the {@link #increment(String)} in a class to count the number of instances created.
1010
* <br><b>Be aware that doing such a thing can result in an important performance penalty</b>.
11-
* Remove, or at least, call {@link CustomStats#off()} in production code.
11+
* Remove, or at least, call {@link CustomStats#off()} in production code.
12+
* <br><b>Warning:</b> Only {@link #increment(String)} and {@link #increment(String, long)} are thread safe.
1213
*/
13-
public abstract class CustomStats {
14-
private static boolean on = false;
15-
private static final Map<String, AtomicLong> MAP = new HashMap<>();
14+
public class CustomStats {
15+
private boolean on;
16+
private final Map<String, AtomicLong> map;
1617

17-
private CustomStats() {
18-
// Prevents subclassing
18+
public CustomStats() {
19+
map = new HashMap<>();
1920
}
2021

2122
/** Enables the statistics.
2223
* <br>Statistics are deactivated by default
2324
* @return The previous activation state
2425
*/
25-
public static boolean on() {
26+
public boolean on() {
2627
if (on) {
2728
return true;
2829
}
29-
final boolean old = on;
3030
on = true;
31-
return old;
31+
return false;
3232
}
3333

3434
/** Disables the statistics.
3535
* <br>Statistics are deactivated by default
3636
* @return The previous activation state
3737
*/
38-
public static boolean off() {
38+
public boolean off() {
3939
if (!on) {
4040
return false;
4141
}
42-
final boolean old = on;
43-
on = true;
44-
return old;
42+
on = false;
43+
return true;
4544
}
4645

4746
/** Checks whether the statistivs are enabled.
4847
* @return true if statistics are enabled
4948
*/
50-
public static boolean isOn() {
49+
public boolean isOn() {
5150
return on;
5251
}
5352

5453
/** Increments a statistics counter.
5554
* @param counter The name of the counter to increment
5655
*/
57-
public static void increment(String counter) {
56+
public void increment(String counter) {
5857
if (on) {
59-
MAP.computeIfAbsent(counter, k->new AtomicLong()).incrementAndGet();
58+
map.computeIfAbsent(counter, k->new AtomicLong()).incrementAndGet();
6059
}
6160
}
62-
61+
62+
/** Increments a statistics counter.
63+
* @param counter The name of the counter to increment
64+
*/
65+
public void increment(String counter, long count) {
66+
if (on) {
67+
map.computeIfAbsent(counter, k->new AtomicLong()).addAndGet(count);
68+
}
69+
}
70+
6371
/** Clears all counters.
6472
*/
65-
public static void clear() {
66-
MAP.clear();
73+
public void clear() {
74+
map.clear();
6775
}
6876

6977
/** Gets a counter.
7078
* @param counter The name of the counter
7179
* @return The counter's value
7280
*/
73-
public static long get(String counter) {
74-
final AtomicLong count = MAP.get(counter);
81+
public long get(String counter) {
82+
final AtomicLong count = map.get(counter);
7583
return count==null ? 0 : count.get();
7684
}
7785

7886
/** Clears a counter.
7987
* @param counter The name of the counter to clear
8088
* @return The counter's value before its removal
8189
*/
82-
public static long clear(String counter) {
83-
final AtomicLong count = MAP.remove(counter);
90+
public long clear(String counter) {
91+
final AtomicLong count = map.remove(counter);
8492
return count==null ? 0 : count.get();
8593
}
8694

8795
/** Gets the set of counter's.
8896
* @return A set of strings.
8997
* <br><b>Warning:</b> Removing or adding counters while iterating on the returned set may throw exceptions.
9098
*/
91-
public static Set<String> getCounters() {
92-
return MAP.keySet();
99+
public Set<String> getCounters() {
100+
return map.keySet();
93101
}
94102

95103
/** Returns a string representation of the counters.
96104
* @return a String
97105
*/
98-
public static String asString() {
99-
return MAP.toString();
106+
@Override
107+
public String toString() {
108+
return map.toString();
100109
}
101110
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.fathzer.games.ai.time;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
import com.fathzer.games.clock.CountDownState;
8+
9+
class TimeTest {
10+
11+
@Test
12+
void test() {
13+
final BasicTimeManager<Void> tm = new BasicTimeManager<>(x -> 3);
14+
assertEquals(500, tm.getMaxTime(null, new CountDownState(999, 1, 0)));
15+
assertEquals(600, tm.getMaxTime(null, new CountDownState(600, 0, 1)));
16+
17+
assertThrows(IllegalArgumentException.class, () -> new CountDownState(600, 0, -1));
18+
}
19+
20+
}

src/test/java/com/fathzer/games/movelibrary/AbstractMoveLibraryTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ static void setUp() {
2727
private static class MyMoveLibrary extends AbstractMoveLibrary<String, String, String> {
2828
private final String expected;
2929
private final List<String> results;
30+
private boolean newGameCalled;
3031

3132
public MyMoveLibrary(String expected, List<String> results) {
3233
this.expected = expected;
@@ -51,6 +52,12 @@ protected long getWeight(String move) {
5152
return super.getWeight(move);
5253
}
5354
}
55+
56+
@Override
57+
public void newGame() {
58+
newGameCalled = true;
59+
super.newGame();
60+
}
5461
}
5562

5663
@Test
@@ -76,5 +83,12 @@ void test() throws Exception {
7683
lib.setMoveSelector(lib.weightedMoveSelector());
7784
when(mockedRnd.nextLong(15)).thenReturn(4L);
7885
assertEquals("2", lib.apply("ok").get().getMove());
86+
assertEquals(5, lib.getMoves("ok").size());
87+
assertEquals(1, lib.getMoves("ko").size());
88+
assertTrue(lib.getMoves("unknown").isEmpty());
89+
90+
lib.newGame();
91+
assertTrue(lib.newGameCalled);
92+
assertTrue(next.newGameCalled);
7993
}
8094
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.fathzer.games.util;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import java.util.Set;
6+
7+
import org.junit.jupiter.api.Test;
8+
9+
class CustomStatsTest {
10+
11+
@Test
12+
void test() {
13+
var cs = new CustomStats();
14+
assertFalse(cs.isOn());
15+
final String key = "x";
16+
assertEquals(0, cs.get(key));
17+
cs.increment(key);
18+
assertEquals(0, cs.get(key));
19+
cs.increment(key, 10);
20+
assertEquals(0, cs.get(key));
21+
22+
assertFalse(cs.on());
23+
assertTrue(cs.on());
24+
assertTrue(cs.isOn());
25+
cs.increment(key);
26+
assertEquals(1, cs.get(key));
27+
cs.increment(key, 10);
28+
assertEquals(11, cs.get(key));
29+
30+
cs.increment("y");
31+
assertEquals(11, cs.get(key));
32+
assertEquals(1, cs.get("y"));
33+
34+
assertEquals(Set.of(key, "y"), cs.getCounters());
35+
36+
cs.clear(key);
37+
assertEquals(0, cs.get(key));
38+
assertEquals(1, cs.get("y"));
39+
cs.increment(key);
40+
41+
assertTrue(cs.off());
42+
assertFalse(cs.off());
43+
cs.increment("y");
44+
assertEquals(1, cs.get("y"));
45+
46+
cs.clear();
47+
assertTrue(cs.getCounters().isEmpty());
48+
assertEquals(0, cs.get(key));
49+
assertEquals(0, cs.get("y"));
50+
}
51+
52+
}

0 commit comments

Comments
 (0)