Skip to content
This repository was archived by the owner on Mar 11, 2021. It is now read-only.

Commit e413022

Browse files
authored
Fix bugs in BoardVisitor & GroupVisitor. Add Tests. (#234)
1 parent 9e34dd6 commit e413022

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

cc/position.h

+12-4
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ class BoardVisitor {
5353
// Starts a new visit around the board.
5454
void Begin() {
5555
MG_DCHECK(Done());
56-
if (epoch_++ == 0) {
56+
if (++epoch_ == 0) {
5757
memset(visited_.data(), 0, sizeof(visited_));
58+
epoch_ = 1;
5859
}
5960
}
6061

@@ -83,7 +84,10 @@ class BoardVisitor {
8384
private:
8485
inline_vector<Coord, kN * kN> stack_;
8586
std::array<uint8_t, kN * kN> visited_;
86-
uint8_t epoch_ = 0;
87+
88+
// Initializing to 0xff means the visited_ array will get initialized on the
89+
// first call to Begin().
90+
uint8_t epoch_ = 0xff;
8791
};
8892

8993
// GroupVisitor simply keeps track of which groups have been visited since the
@@ -94,8 +98,9 @@ class GroupVisitor {
9498
GroupVisitor() = default;
9599

96100
void Begin() {
97-
if (epoch_++ == 0) {
101+
if (++epoch_ == 0) {
98102
memset(visited_.data(), 0, sizeof(visited_));
103+
epoch_ = 1;
99104
}
100105
}
101106

@@ -108,8 +113,11 @@ class GroupVisitor {
108113
}
109114

110115
private:
111-
uint8_t epoch_ = 0;
112116
std::array<uint8_t, Group::kMaxNumGroups> visited_;
117+
118+
// Initializing to 0xff means the visited_ array will get initialized on the
119+
// first call to Begin().
120+
uint8_t epoch_ = 0xff;
113121
};
114122

115123
// Position represents a single board position.

cc/position_test.cc

+29
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,35 @@
2727
namespace minigo {
2828
namespace {
2929

30+
TEST(BoardVisitorTest, TestEpochRollover) {
31+
for (int j = 0; j <= 256; ++j) {
32+
BoardVisitor bv;
33+
for (int i = 0; i < j; ++i) {
34+
bv.Begin();
35+
}
36+
for (int i = 0; i < kN * kN; ++i) {
37+
auto c = Coord(i);
38+
ASSERT_TRUE(bv.Visit(c));
39+
ASSERT_EQ(c, bv.Next());
40+
ASSERT_FALSE(bv.Visit(c));
41+
}
42+
}
43+
}
44+
45+
TEST(GroupVisitorTest, TestEpochRollover) {
46+
for (int j = 0; j <= 256; ++j) {
47+
GroupVisitor gv;
48+
for (int i = 0; i < j; ++i) {
49+
gv.Begin();
50+
}
51+
for (int i = 0; i < Group::kMaxNumGroups; ++i) {
52+
auto g = GroupId(i);
53+
ASSERT_TRUE(gv.Visit(g));
54+
ASSERT_FALSE(gv.Visit(g));
55+
}
56+
}
57+
}
58+
3059
TEST(PositionTest, TestIsKoish) {
3160
auto board = TestablePosition(R"(
3261
.X.O.O.O.

0 commit comments

Comments
 (0)