1 #include "sliding_tile_lib.h"
3 #include "gtest/gtest.h"
4 #include "gmock/gmock.h"
9 using testing::ElementsAreArray
;
11 TEST(Step
, Adjacency
) {
12 const signed char LEFT
= -1;
13 const signed char RIGHT
= +1;
14 const signed char UP
= -BOARD_DIM
;
15 const signed char DOWN
= +BOARD_DIM
;
16 for (int i
= 0; i
< BOARD_SIZE
; i
++) {
18 std::vector
<signed char> expected
;
20 expected
.push_back(i
+ UP
);
22 if (i
< BOARD_SIZE
- BOARD_DIM
) {
23 expected
.push_back(i
+ DOWN
);
25 if (i
% BOARD_DIM
!= 0) {
26 expected
.push_back(i
+ LEFT
);
28 if (i
% BOARD_DIM
!= BOARD_DIM
- 1) {
29 expected
.push_back(i
+ RIGHT
);
32 std::vector
<signed char> actual
;
33 for (int j
= 0; Step::adjacent
[i
][j
] >= 0; j
++) {
34 actual
.push_back(Step::adjacent
[i
][j
]);
36 EXPECT_THAT(actual
, testing::UnorderedElementsAreArray(expected
));
41 Board b1
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
42 Board b2
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
43 EXPECT_TRUE(b1
== b2
);
44 EXPECT_FALSE(b1
!= b2
);
48 TEST(Board
, NotEqual
) {
49 Board b1
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
50 Board b2
{{1,2,3,4,5,6,7,8,10,9,11,12,13,14,15,0}};
51 EXPECT_FALSE(b1
== b2
);
52 EXPECT_TRUE(b1
!= b2
);
56 TEST(Board
, GoodInput
) {
57 std::istringstream is
{"15,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
60 EXPECT_FALSE(is
.fail());
61 EXPECT_TRUE(is
.eof());
62 EXPECT_THAT(b
.board
, ElementsAreArray({15,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10}));
65 TEST(Board
, ShortInput
) {
66 std::istringstream is
{"15,14,9,13,3,1,12,8,0,11,6,4,7,5,2"};
69 EXPECT_TRUE(is
.fail());
72 TEST(Board
, NonNumericInput
) {
73 std::istringstream is
{"15,14,foo,13,3,1,12,8,0,11,6,4,7,5,2,10"};
76 EXPECT_TRUE(is
.fail());
79 TEST(Board
, RepeatedTileInput
) {
80 std::istringstream is
{"15,15,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
83 EXPECT_TRUE(is
.fail());
86 TEST(Board
, LowTileInput
) {
87 std::istringstream is
{"-1,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
90 EXPECT_TRUE(is
.fail());
93 TEST(Board
, HighTileInput
) {
94 std::istringstream is
{"16,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
97 EXPECT_TRUE(is
.fail());
101 Board b
{{16,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10}};
102 EXPECT_EQ(8, b
.hole());
105 TEST(Board
, NoHole
) {
106 Board b
{{16,14,9,13,3,1,12,8,16,11,6,4,7,5,2,10}};
107 EXPECT_THROW(b
.hole(), std::runtime_error
);
110 TEST(Board
, ZeroDistance
) {
111 Board b
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
112 EXPECT_EQ(0, b
.distance(b
));
115 TEST(Board
, DistanceAdjacentTilesFlipped
) {
116 Board b1
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
117 Board b2
{{2,1,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
118 EXPECT_EQ(2, b1
.distance(b2
));
121 TEST(Board
, DistanceOneMoveRemaining
) {
122 Board b1
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
123 Board b2
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,15}};
124 EXPECT_EQ(2, b1
.distance(b2
));
127 TEST(Board
, DistanceCornersSwapped
) {
128 Board b1
{{0,2,3,13,5,6,7,8,9,10,11,12,4,14,15,1}};
129 Board b2
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
130 EXPECT_EQ(24, b1
.distance(b2
));
133 TEST(Board
, MaxDistance
) {
134 Board b1
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
135 Board b2
{{0,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1}};
136 EXPECT_EQ(64, b1
.distance(b2
));
139 TEST(Step
, TwoSuccessors
) {
140 auto s
= std::make_shared
<Step
>(Step
{{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}}, nullptr});
141 std::vector
<Step
*> suc
= s
->successors(s
);
142 EXPECT_THAT(suc
, testing::UnorderedElementsAre(
143 Field(&Step::board
, Field(&Board::board
, ElementsAreArray({1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,15}))),
144 Field(&Step::board
, Field(&Board::board
, ElementsAreArray({1,2,3,4,5,6,7,8,9,10,11,0,13,14,15,12})))));
147 TEST(Step
, FourSuccessors
) {
148 auto s
= std::make_shared
<Step
>(Step
{{{1,2,3,4,5,0,6,7,8,9,10,11,12,13,14,15}}, nullptr});
149 std::vector
<Step
*> suc
= s
->successors(s
);
150 EXPECT_THAT(suc
, testing::UnorderedElementsAre(
151 Field(&Step::board
, Field(&Board::board
, ElementsAreArray({1,2,3,4,0,5,6,7,8,9,10,11,12,13,14,15}))),
152 Field(&Step::board
, Field(&Board::board
, ElementsAreArray({1,2,3,4,5,6,0,7,8,9,10,11,12,13,14,15}))),
153 Field(&Step::board
, Field(&Board::board
, ElementsAreArray({1,0,3,4,5,2,6,7,8,9,10,11,12,13,14,15}))),
154 Field(&Step::board
, Field(&Board::board
, ElementsAreArray({1,2,3,4,5,9,6,7,8,0,10,11,12,13,14,15})))));
158 auto s1
= std::make_shared
<Step
>(Step
{{{1,2,3,4,5,7,11,8,9,6,0,12,13,10,14,15}}, nullptr});
159 auto s2
= std::make_shared
<Step
>(Step
{{{1,2,3,4,5,7,0,8,9,6,11,12,13,10,14,15}}, s1
});
160 auto s3
= std::make_shared
<Step
>(Step
{{{1,2,3,4,5,0,7,8,9,6,11,12,13,10,14,15}}, s2
});
161 auto s4
= std::make_shared
<Step
>(Step
{{{1,2,3,4,5,6,7,8,9,0,11,12,13,10,14,15}}, s3
});
162 auto s5
= std::make_shared
<Step
>(Step
{{{1,2,3,4,5,6,7,8,9,10,11,12,13,0,14,15}}, s4
});
163 auto s6
= std::make_shared
<Step
>(Step
{{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,15}}, s5
});
164 auto s7
= std::make_shared
<Step
>(Step
{{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}}, s6
});
165 std::ostringstream actual
;
167 std::ostringstream expected
;
168 expected
<< "11 down" << std::endl
169 << "7 right" << std::endl
170 << "6 up" << std::endl
171 << "10 up" << std::endl
172 << "14 left" << std::endl
173 << "15 left" << std::endl
;
174 EXPECT_EQ(expected
.str(), actual
.str());