1 #include "sliding_tile_lib.h"
3 #include "gtest/gtest.h"
4 #include "gmock/gmock.h"
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
));
40 Board b1
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
41 Board b2
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
42 EXPECT_TRUE(b1
== b2
);
43 EXPECT_FALSE(b1
!= b2
);
47 TEST(Board
, NotEqual
) {
48 Board b1
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
49 Board b2
{{1,2,3,4,5,6,7,8,10,9,11,12,13,14,15,0}};
50 EXPECT_FALSE(b1
== b2
);
51 EXPECT_TRUE(b1
!= b2
);
55 TEST(Board
, GoodInput
) {
56 std::istringstream is
{"15,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
59 EXPECT_FALSE(is
.fail());
60 EXPECT_TRUE(is
.eof());
61 EXPECT_EQ((Board
{{15,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10}}), b
);
64 TEST(Board
, ShortInput
) {
65 std::istringstream is
{"15,14,9,13,3,1,12,8,0,11,6,4,7,5,2"};
68 EXPECT_TRUE(is
.fail());
71 TEST(Board
, NonNumericInput
) {
72 std::istringstream is
{"15,14,foo,13,3,1,12,8,0,11,6,4,7,5,2,10"};
75 EXPECT_TRUE(is
.fail());
78 TEST(Board
, RepeatedTileInput
) {
79 std::istringstream is
{"15,15,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
82 EXPECT_TRUE(is
.fail());
85 TEST(Board
, LowTileInput
) {
86 std::istringstream is
{"-1,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
89 EXPECT_TRUE(is
.fail());
92 TEST(Board
, HighTileInput
) {
93 std::istringstream is
{"16,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
96 EXPECT_TRUE(is
.fail());
100 Board b
{{16,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10}};
101 EXPECT_EQ(8, b
.hole());
104 TEST(Board
, NoHole
) {
105 Board b
{{16,14,9,13,3,1,12,8,16,11,6,4,7,5,2,10}};
106 EXPECT_THROW(b
.hole(), std::runtime_error
);
109 TEST(Board
, ZeroDistance
) {
110 Board b
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
111 EXPECT_EQ(0, b
.distance(b
));
114 TEST(Board
, DistanceAdjacentTilesFlipped
) {
115 Board b1
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
116 Board b2
{{2,1,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
117 EXPECT_EQ(2, b1
.distance(b2
));
120 TEST(Board
, DistanceOneMoveRemaining
) {
121 Board b1
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
122 Board b2
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,15}};
123 EXPECT_EQ(2, b1
.distance(b2
));
126 TEST(Board
, DistanceCornersSwapped
) {
127 Board b1
{{0,2,3,13,5,6,7,8,9,10,11,12,4,14,15,1}};
128 Board b2
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
129 EXPECT_EQ(24, b1
.distance(b2
));
132 TEST(Board
, MaxDistance
) {
133 Board b1
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
134 Board b2
{{0,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1}};
135 EXPECT_EQ(64, b1
.distance(b2
));
138 TEST(Step
, TwoSuccessors
) {
139 auto s
= std::make_shared
<Step
>(Step
{{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}}, nullptr});
140 std::vector
<Step
*> suc
= s
->successors(s
);
141 EXPECT_THAT(suc
, testing::UnorderedElementsAre(
142 Field(&Step::board
, Board
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,15}}),
143 Field(&Step::board
, Board
{{1,2,3,4,5,6,7,8,9,10,11,0,13,14,15,12}})));
146 TEST(Step
, FourSuccessors
) {
147 auto s
= std::make_shared
<Step
>(Step
{{{1,2,3,4,5,0,6,7,8,9,10,11,12,13,14,15}}, nullptr});
148 std::vector
<Step
*> suc
= s
->successors(s
);
149 EXPECT_THAT(suc
, testing::UnorderedElementsAre(
150 Field(&Step::board
, Board
{{1,2,3,4,0,5,6,7,8,9,10,11,12,13,14,15}}),
151 Field(&Step::board
, Board
{{1,2,3,4,5,6,0,7,8,9,10,11,12,13,14,15}}),
152 Field(&Step::board
, Board
{{1,0,3,4,5,2,6,7,8,9,10,11,12,13,14,15}}),
153 Field(&Step::board
, Board
{{1,2,3,4,5,9,6,7,8,0,10,11,12,13,14,15}})));
157 auto s1
= std::make_shared
<Step
>(Step
{{{1,2,3,4,5,7,11,8,9,6,0,12,13,10,14,15}}, nullptr});
158 auto s2
= std::make_shared
<Step
>(Step
{{{1,2,3,4,5,7,0,8,9,6,11,12,13,10,14,15}}, s1
});
159 auto s3
= std::make_shared
<Step
>(Step
{{{1,2,3,4,5,0,7,8,9,6,11,12,13,10,14,15}}, s2
});
160 auto s4
= std::make_shared
<Step
>(Step
{{{1,2,3,4,5,6,7,8,9,0,11,12,13,10,14,15}}, s3
});
161 auto s5
= std::make_shared
<Step
>(Step
{{{1,2,3,4,5,6,7,8,9,10,11,12,13,0,14,15}}, s4
});
162 auto s6
= std::make_shared
<Step
>(Step
{{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,15}}, s5
});
163 auto s7
= std::make_shared
<Step
>(Step
{{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}}, s6
});
164 std::ostringstream actual
;
166 std::ostringstream expected
;
167 expected
<< "11 down" << std::endl
168 << "7 right" << std::endl
169 << "6 up" << std::endl
170 << "10 up" << std::endl
171 << "14 left" << std::endl
172 << "15 left" << std::endl
;
173 EXPECT_EQ(expected
.str(), actual
.str());