-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbitboard_test.go
155 lines (142 loc) · 3.47 KB
/
bitboard_test.go
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
package chess
import (
"reflect"
"testing"
)
type bitboardTestPair struct {
initial uint64
reversed uint64
}
type bitboardTestNew struct {
smap map[Square]bool
bits uint64
}
var (
tests = []bitboardTestPair{
{
uint64(1),
uint64(9223372036854775808),
},
{
uint64(18446744073709551615),
uint64(18446744073709551615),
},
{
uint64(0),
uint64(0),
},
}
newBitboardTests = []bitboardTestNew{
{
map[Square]bool{},
0b_00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000000,
// all zeroes
},
{
map[Square]bool{
A1: true,
},
0b_10000000_00000000_00000000_00000000_00000000_00000000_00000000_00000000,
// ^
},
{
map[Square]bool{
B1: true,
},
0b_01000000_00000000_00000000_00000000_00000000_00000000_00000000_00000000,
// ^
},
{
map[Square]bool{
B3: true,
},
0b_00000000_00000000_01000000_00000000_00000000_00000000_00000000_00000000,
// ^
},
{
map[Square]bool{
H8: true,
},
0b_00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000001,
// ^
},
{
map[Square]bool{
A1: true,
B3: true,
H8: true,
},
0b_10000000_00000000_01000000_00000000_00000000_00000000_00000000_00000001,
// ^ ^ ^
},
}
)
func TestBitboardReverse(t *testing.T) {
for _, p := range tests {
r := uint64(bitboard(p.initial).Reverse())
if r != p.reversed {
t.Fatalf("bitboard reverse of %s expected %s but got %s", intStr(p.initial), intStr(p.reversed), intStr(r))
}
}
}
func TestBitboardOccupied(t *testing.T) {
m := map[Square]bool{
B3: true,
}
bb := newBitboard(m)
if bb.Occupied(B3) != true {
t.Fatalf("bitboard occupied of %s expected %t but got %t", bb, true, false)
}
}
func BenchmarkBitboardReverse(b *testing.B) {
for i := 0; i < b.N; i++ {
u := uint64(9223372036854775807)
bitboard(u).Reverse()
}
}
func intStr(i uint64) string {
return bitboard(i).String()
}
func TestBitboardNew(t *testing.T) {
for _, c := range newBitboardTests {
bb := newBitboard(c.smap)
if uint64(bb) != c.bits {
t.Fatalf("new bitboard from %v expected %s but got %s", c.smap, intStr(c.bits), bb)
}
}
}
func TestMappingEmptyBitboard(t *testing.T) {
bb := bitboard(0)
expected := map[Square]bool{}
result := bb.Mapping()
if !reflect.DeepEqual(result, expected) {
t.Fatalf("expected %v but got %v", expected, result)
}
}
func TestMappingSingleSquare(t *testing.T) {
bb := bitboard(0b_10000000_00000000_00000000_00000000_00000000_00000000_00000000_00000000)
expected := map[Square]bool{A1: true}
result := bb.Mapping()
if !reflect.DeepEqual(result, expected) {
t.Fatalf("expected %v but got %v", expected, result)
}
}
func TestMappingMultipleSquares(t *testing.T) {
bb := bitboard(0b_10000000_00000000_01000000_00000000_00000000_00000000_00000000_00000001)
expected := map[Square]bool{A1: true, B3: true, H8: true}
result := bb.Mapping()
if !reflect.DeepEqual(result, expected) {
t.Fatalf("expected %v but got %v", expected, result)
}
}
func TestMappingFullBitboard(t *testing.T) {
bb := bitboard(0b_11111111_11111111_11111111_11111111_11111111_11111111_11111111_11111111)
expected := map[Square]bool{}
for sq := range numOfSquaresInBoard {
expected[Square(sq)] = true
}
result := bb.Mapping()
if !reflect.DeepEqual(result, expected) {
t.Fatalf("expected %v but got %v", expected, result)
}
}