Skip to content

Commit e8f7ffd

Browse files
committed
fixed linting and tests
1 parent fb472d1 commit e8f7ffd

File tree

8 files changed

+178
-133
lines changed

8 files changed

+178
-133
lines changed

include/bitbishop/bitboard.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ class Bitboard {
5353
[[nodiscard]] constexpr uint64_t value() const { return m_bb; }
5454

5555
/** @brief Returns a Bitboard instance with all bits set to zero. */
56-
static constexpr Bitboard Zeros() noexcept { return Bitboard(0ULL); }
56+
static constexpr Bitboard Zeros() noexcept { return {0ULL}; }
5757

5858
/** @brief Returns a Bitbaord instance with all bits set to one. */
59-
static constexpr Bitboard Ones() noexcept { return Bitboard(1ULL); }
59+
static constexpr Bitboard Ones() noexcept { return {~0ULL}; }
6060

6161
/**
6262
* @brief Sets a bit (places a piece) on a given square.

include/bitbishop/board.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class Board {
6363
*/
6464
Board(const std::string& fen);
6565

66-
[[nodiscard]] static Board Empty() noexcept { return Board("8/8/8/8/8/8/8/8 w KQkq - 0 1"); }
66+
[[nodiscard]] static Board Empty() noexcept { return {"8/8/8/8/8/8/8/8 w KQkq - 0 1"}; }
6767

6868
/**
6969
* @brief Retrieves the piece on a given square.

include/bitbishop/lookups/between_squares.hpp

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace Lookups {
1616
* - +-8 for vertical movement (same file)
1717
* - +-9 for NE–SW diagonal movement
1818
* - +-7 for NW–SE diagonal movement
19+
* - 0 if source and destination square are identical
1920
*
2021
* This function assumes that the two squares are aligned. Its primary use
2122
* is internal traversal of rays when building geometric lookup tables such
@@ -26,10 +27,23 @@ namespace Lookups {
2627
* @return The signed step value to advance one square toward @p to.
2728
*/
2829
constexpr int direction(Square from, Square to) {
29-
if (from.same_rank(to)) return (to.value() > from.value()) ? +1 : -1;
30-
if (from.same_file(to)) return (to.value() > from.value()) ? +8 : -8;
31-
if (from.same_ne_sw_diag(to)) return (to.value() > from.value()) ? +9 : -9;
32-
return (to.value() > from.value()) ? +7 : -7;
30+
if (from == to) {
31+
return 0;
32+
}
33+
if (from.same_rank(to)) {
34+
return (to.file() > from.file()) ? +1 : -1;
35+
}
36+
if (from.same_file(to)) {
37+
return (to.rank() > from.rank()) ? +Const::BOARD_WIDTH : -Const::BOARD_WIDTH;
38+
}
39+
if (from.same_ne_sw_diag(to)) {
40+
return (to.rank() > from.rank()) ? +(Const::BOARD_WIDTH + 1) : -(Const::BOARD_WIDTH + 1);
41+
}
42+
if (from.same_nw_se_diag(to)) {
43+
return (to.rank() > from.rank()) ? +(Const::BOARD_WIDTH - 1) : -(Const::BOARD_WIDTH - 1);
44+
}
45+
46+
return 0;
3347
}
3448

3549
/**
@@ -50,14 +64,20 @@ constexpr int direction(Square from, Square to) {
5064
* @return A bitboard of squares strictly between @p from and @p to.
5165
*/
5266
constexpr Bitboard ray_between(Square from, Square to) {
53-
if (from == to) return Bitboard::Zeros();
67+
if (from == to) {
68+
return Bitboard::Zeros();
69+
}
5470

55-
if (!(from.same_file(to) || from.same_rank(to) || from.same_diag(to))) return Bitboard::Zeros();
71+
if (!(from.same_file(to) || from.same_rank(to) || from.same_diag(to))) {
72+
return Bitboard::Zeros();
73+
}
5674

5775
const int step = direction(from, to);
5876
Bitboard ray = Bitboard::Zeros();
5977

60-
for (std::uint8_t square = from.value() + step; square != to.value(); square += step) ray.set(square);
78+
for (std::uint8_t square = from.value() + step; square != to.value(); square += step) {
79+
ray.set(square);
80+
}
6181

6282
return ray;
6383
}

src/bitbishop/movegen/check_mask.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ Bitboard compute_check_mask(Square king_sq, const Bitboard& checkers, const Boar
1616
board.knights(Color::WHITE) | board.knights(Color::BLACK) | board.pawns(Color::WHITE) | board.pawns(Color::BLACK);
1717
const bool is_knight_or_pawn = knights_and_pawns.test(checker_sq);
1818
if (is_knight_or_pawn) {
19-
return Bitboard(checker_sq.value());
19+
return checkers;
2020
}
2121

22-
return Lookups::ray_between(checker_sq, king_sq) | Bitboard(checker_sq.value());
22+
return Lookups::ray_between(checker_sq, king_sq) | checkers;
2323
}

tests/bitbishop/bitboard/test_bb_constructors.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,6 @@ TEST(BitboardTest, ZerosStaticConstructor) {
9090
TEST(BitboardTest, OnesStaticConstructor) {
9191
constexpr Bitboard bb = Bitboard::Ones();
9292

93-
static_assert(bb.value() == 1ULL, "Bitboard::Ones() static constructor must be constexpr");
94-
EXPECT_EQ(bb.value(), 1ULL);
93+
static_assert(bb.value() == (~0ULL), "Bitboard::Ones() static constructor must be constexpr");
94+
EXPECT_EQ(bb.value(), (~0ULL));
9595
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <gtest/gtest.h>
2+
3+
#include <bitbishop/bitboard.hpp>
4+
#include <bitbishop/lookups/between_squares.hpp>
5+
#include <bitbishop/square.hpp>
6+
7+
using namespace Squares;
8+
using namespace Lookups;
9+
10+
/**
11+
* @test BETWEEN table for same rank lookups.
12+
* @brief Confirms BETWEEN[from][to] contains correct horizontal rays.
13+
*/
14+
TEST(BetweenLookupTest, BetweenTableSameRank) {
15+
const Bitboard& ray = BETWEEN[A1.value()][E1.value()];
16+
EXPECT_TRUE(ray.test(B1));
17+
EXPECT_TRUE(ray.test(C1));
18+
EXPECT_TRUE(ray.test(D1));
19+
EXPECT_FALSE(ray.test(A1));
20+
EXPECT_FALSE(ray.test(E1));
21+
}
22+
23+
/**
24+
* @test BETWEEN table for same file lookups.
25+
* @brief Confirms BETWEEN[from][to] contains correct vertical rays.
26+
*/
27+
TEST(BetweenLookupTest, BetweenTableSameFile) {
28+
const Bitboard& ray = BETWEEN[D1.value()][D5.value()];
29+
EXPECT_TRUE(ray.test(D2));
30+
EXPECT_TRUE(ray.test(D3));
31+
EXPECT_TRUE(ray.test(D4));
32+
EXPECT_FALSE(ray.test(D1));
33+
EXPECT_FALSE(ray.test(D5));
34+
}
35+
36+
/**
37+
* @test BETWEEN table for diagonal lookups.
38+
* @brief Confirms BETWEEN[from][to] contains correct diagonal rays.
39+
*/
40+
TEST(BetweenLookupTest, BetweenTableDiagonal) {
41+
const Bitboard& ray = BETWEEN[A1.value()][D4.value()];
42+
EXPECT_TRUE(ray.test(B2));
43+
EXPECT_TRUE(ray.test(C3));
44+
EXPECT_EQ(ray.count(), 2);
45+
}
46+
47+
/**
48+
* @test BETWEEN table for non-aligned squares.
49+
* @brief Confirms BETWEEN[from][to] is empty for unaligned squares.
50+
*/
51+
TEST(BetweenLookupTest, BetweenTableNonAligned) {
52+
EXPECT_EQ(BETWEEN[A1.value()][B3.value()], Bitboard::Zeros());
53+
EXPECT_EQ(BETWEEN[E4.value()][F6.value()], Bitboard::Zeros());
54+
}
55+
56+
/**
57+
* @test BETWEEN table symmetry.
58+
* @brief Confirms BETWEEN[a][b] == BETWEEN[b][a].
59+
*/
60+
TEST(BetweenLookupTest, BetweenTableSymmetry) {
61+
EXPECT_EQ(BETWEEN[A1.value()][H1.value()], BETWEEN[H1.value()][A1.value()]);
62+
EXPECT_EQ(BETWEEN[A1.value()][H8.value()], BETWEEN[H8.value()][A1.value()]);
63+
}
64+
65+
/**
66+
* @test BETWEEN table diagonal property.
67+
* @brief Confirms BETWEEN[x][x] is empty for all squares.
68+
*/
69+
TEST(BetweenLookupTest, BetweenTableDiagonalSameSquare) {
70+
for (int i = 0; i < 64; ++i) {
71+
EXPECT_EQ(BETWEEN[i][i], Bitboard::Zeros());
72+
}
73+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <gtest/gtest.h>
2+
3+
#include <bitbishop/bitboard.hpp>
4+
#include <bitbishop/lookups/between_squares.hpp>
5+
#include <bitbishop/square.hpp>
6+
7+
using namespace Squares;
8+
using namespace Lookups;
9+
10+
/**
11+
* @test Seeking for the direction using identical source and destination squares should return .
12+
*/
13+
TEST(DirectionTest, DirectionSameSquare) {
14+
EXPECT_EQ(direction(E4, E4), 0);
15+
EXPECT_EQ(direction(A1, A1), 0);
16+
EXPECT_EQ(direction(H8, H8), 0);
17+
}
18+
19+
/**
20+
* @test Horizontal direction on same rank (step = +-1).
21+
* @brief Confirms direction() returns +1 for rightward and -1 for leftward movement.
22+
*/
23+
TEST(LookupsTest, DirectionSameRank) {
24+
EXPECT_EQ(direction(A1, H1), +1);
25+
EXPECT_EQ(direction(H1, A1), -1);
26+
EXPECT_EQ(direction(D4, G4), +1);
27+
EXPECT_EQ(direction(E5, B5), -1);
28+
}
29+
30+
/**
31+
* @test Vertical direction on same file (step = +-8).
32+
* @brief Confirms direction() returns +8 for upward and -8 for downward movement.
33+
*/
34+
TEST(LookupsTest, DirectionSameFile) {
35+
EXPECT_EQ(direction(A1, A8), +8);
36+
EXPECT_EQ(direction(A8, A1), -8);
37+
EXPECT_EQ(direction(E2, E6), +8);
38+
EXPECT_EQ(direction(H7, H3), -8);
39+
}
40+
41+
/**
42+
* @test NE-SW diagonal direction (step = +-9).
43+
* @brief Confirms direction() returns +9 for NE and -9 for SW movement.
44+
*/
45+
TEST(LookupsTest, DirectionNESWDiagonal) {
46+
EXPECT_EQ(direction(A1, H8), +9);
47+
EXPECT_EQ(direction(H8, A1), -9);
48+
EXPECT_EQ(direction(C3, E5), +9);
49+
EXPECT_EQ(direction(F6, D4), -9);
50+
}
51+
52+
/**
53+
* @test NW-SE diagonal direction (step = +-7).
54+
* @brief Confirms direction() returns +7 for NW and -7 for SE movement.
55+
*/
56+
TEST(LookupsTest, DirectionNWSEDiagonal) {
57+
EXPECT_EQ(direction(A8, H1), -7);
58+
EXPECT_EQ(direction(H1, A8), +7);
59+
EXPECT_EQ(direction(B6, E3), -7);
60+
EXPECT_EQ(direction(F4, C7), +7);
61+
}

0 commit comments

Comments
 (0)