1 #include "sliding_tile_lib.h"
3 #include "gtest/gtest.h"
4 #include "gmock/gmock.h"
8 using testing::ElementsAreArray
;
10 TEST(Step
, Adjacency
) {
11 const signed char LEFT
= -1;
12 const signed char RIGHT
= +1;
13 const signed char UP
= -BOARD_DIM
;
14 const signed char DOWN
= +BOARD_DIM
;
15 for (int i
= 0; i
< BOARD_SIZE
; i
++) {
17 std::vector
<signed char> expected
;
19 expected
.push_back(i
+ UP
);
21 if (i
< BOARD_SIZE
- BOARD_DIM
) {
22 expected
.push_back(i
+ DOWN
);
24 if (i
% BOARD_DIM
!= 0) {
25 expected
.push_back(i
+ LEFT
);
27 if (i
% BOARD_DIM
!= BOARD_DIM
- 1) {
28 expected
.push_back(i
+ RIGHT
);
31 std::vector
<signed char> actual
;
32 for (int j
= 0; Step::adjacent
[i
][j
] >= 0; j
++) {
33 actual
.push_back(Step::adjacent
[i
][j
]);
35 EXPECT_THAT(actual
, testing::UnorderedElementsAreArray(expected
));
39 TEST(Board
, GoodInput
) {
40 std::istringstream is
{"15,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
43 EXPECT_FALSE(is
.fail());
44 EXPECT_TRUE(is
.eof());
45 EXPECT_THAT(b
.board
, ElementsAreArray({15,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10}));
48 TEST(Board
, ShortInput
) {
49 std::istringstream is
{"15,14,9,13,3,1,12,8,0,11,6,4,7,5,2"};
52 EXPECT_TRUE(is
.fail());
55 TEST(Board
, NonNumericInput
) {
56 std::istringstream is
{"15,14,foo,13,3,1,12,8,0,11,6,4,7,5,2,10"};
59 EXPECT_TRUE(is
.fail());
62 TEST(Board
, RepeatedTileInput
) {
63 std::istringstream is
{"15,15,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
66 EXPECT_TRUE(is
.fail());
69 TEST(Board
, LowTileInput
) {
70 std::istringstream is
{"-1,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
73 EXPECT_TRUE(is
.fail());
76 TEST(Board
, HighTileInput
) {
77 std::istringstream is
{"16,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
80 EXPECT_TRUE(is
.fail());
84 Board b
{{16,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10}};
85 EXPECT_EQ(8, b
.hole());
89 Board b
{{16,14,9,13,3,1,12,8,16,11,6,4,7,5,2,10}};
90 EXPECT_THROW(b
.hole(), std::runtime_error
);
93 TEST(Board
, ZeroDistance
) {
94 Board b
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
95 EXPECT_EQ(0, b
.distance(b
));
98 TEST(Board
, DistanceAdjacentTilesFlipped
) {
99 Board b1
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
100 Board b2
{{2,1,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
101 EXPECT_EQ(2, b1
.distance(b2
));
104 TEST(Board
, DistanceOneMoveRemaining
) {
105 Board b1
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
106 Board b2
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,15}};
107 EXPECT_EQ(2, b1
.distance(b2
));
110 TEST(Board
, DistanceCornersSwapped
) {
111 Board b1
{{0,2,3,13,5,6,7,8,9,10,11,12,4,14,15,1}};
112 Board b2
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
113 EXPECT_EQ(24, b1
.distance(b2
));
116 TEST(Board
, MaxDistance
) {
117 Board b1
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
118 Board b2
{{0,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1}};
119 EXPECT_EQ(64, b1
.distance(b2
));
122 TEST(Step
, TwoSuccessors
) {
123 auto s
= std::shared_ptr
<Step
>(new Step
{{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}}, nullptr});
124 std::vector
<Step
*> suc
= s
->successors(s
);
125 EXPECT_THAT(suc
, testing::UnorderedElementsAre(
126 Field(&Step::board
, Field(&Board::board
, ElementsAreArray({1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,15}))),
127 Field(&Step::board
, Field(&Board::board
, ElementsAreArray({1,2,3,4,5,6,7,8,9,10,11,0,13,14,15,12})))));
130 TEST(Step
, FourSuccessors
) {
131 auto s
= std::shared_ptr
<Step
>(new Step
{{{1,2,3,4,5,0,6,7,8,9,10,11,12,13,14,15}}, nullptr});
132 std::vector
<Step
*> suc
= s
->successors(s
);
133 EXPECT_THAT(suc
, testing::UnorderedElementsAre(
134 Field(&Step::board
, Field(&Board::board
, ElementsAreArray({1,2,3,4,0,5,6,7,8,9,10,11,12,13,14,15}))),
135 Field(&Step::board
, Field(&Board::board
, ElementsAreArray({1,2,3,4,5,6,0,7,8,9,10,11,12,13,14,15}))),
136 Field(&Step::board
, Field(&Board::board
, ElementsAreArray({1,0,3,4,5,2,6,7,8,9,10,11,12,13,14,15}))),
137 Field(&Step::board
, Field(&Board::board
, ElementsAreArray({1,2,3,4,5,9,6,7,8,0,10,11,12,13,14,15})))));