From: Scott Worley Date: Sat, 9 Jan 2016 09:48:08 +0000 (-0800) Subject: Board equality operator X-Git-Url: http://git.scottworley.com/slidingtile/commitdiff_plain/0e89d341b7be7fa1861892cbcb216bc188f02e60 Board equality operator --- diff --git a/sliding_tile_lib.cc b/sliding_tile_lib.cc index 82402a5..6b42f28 100644 --- a/sliding_tile_lib.cc +++ b/sliding_tile_lib.cc @@ -49,6 +49,19 @@ bool Board::is_valid() const { return true; } +bool Board::operator==(const Board& o) const { + for (int i = 0; i < BOARD_SIZE; i++) { + if (board[i] != o.board[i]) { + return false; + } + } + return true; +} + +bool Board::operator!=(const Board& o) const { + return !operator==(o); +} + std::istream& operator>>(std::istream& is, Board& board) { for (int i = 0; i < BOARD_SIZE; i++) { if (!is.good()) { diff --git a/sliding_tile_lib.h b/sliding_tile_lib.h index c926761..300ecdd 100644 --- a/sliding_tile_lib.h +++ b/sliding_tile_lib.h @@ -20,6 +20,8 @@ struct Board { InvertedBoard invert() const; int distance(const Board& o) const; int distance(const InvertedBoard& invo) const; + bool operator==(const Board& o) const; + bool operator!=(const Board& o) const; }; std::istream& operator>>(std::istream& is, Board& board); std::ostream& operator<<(std::ostream& os, const Board& board); diff --git a/sliding_tile_lib_test.cc b/sliding_tile_lib_test.cc index 8e6cb89..a27361d 100644 --- a/sliding_tile_lib_test.cc +++ b/sliding_tile_lib_test.cc @@ -37,6 +37,22 @@ TEST(Step, Adjacency) { } } +TEST(Board, Equal) { + Board b1{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}}; + Board b2{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}}; + EXPECT_TRUE(b1 == b2); + EXPECT_FALSE(b1 != b2); + EXPECT_EQ(b1, b2); +} + +TEST(Board, NotEqual) { + Board b1{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}}; + Board b2{{1,2,3,4,5,6,7,8,10,9,11,12,13,14,15,0}}; + EXPECT_FALSE(b1 == b2); + EXPECT_TRUE(b1 != b2); + EXPECT_NE(b1, b2); +} + TEST(Board, GoodInput) { std::istringstream is{"15,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10"}; Board b;