1 #include "sliding_tile_lib.h"
3 #include "gtest/gtest.h"
4 #include "gmock/gmock.h"
9 using testing::Pointee
;
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
);
46 EXPECT_FALSE(b1
< b2
);
47 EXPECT_FALSE(b2
< b1
);
50 TEST(Board
, NotEqual
) {
51 Board b1
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
52 Board b2
{{1,2,3,4,5,6,7,8,10,9,11,12,13,14,15,0}};
53 EXPECT_FALSE(b1
== b2
);
54 EXPECT_TRUE(b1
!= b2
);
57 EXPECT_FALSE(b2
< b1
);
60 TEST(Board
, GoodInput
) {
61 std::istringstream is
{"15,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
64 EXPECT_FALSE(is
.fail());
65 EXPECT_TRUE(is
.eof());
66 EXPECT_EQ((Board
{{15,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10}}), b
);
69 TEST(Board
, ShortInput
) {
70 std::istringstream is
{"15,14,9,13,3,1,12,8,0,11,6,4,7,5,2"};
73 EXPECT_TRUE(is
.fail());
76 TEST(Board
, NonNumericInput
) {
77 std::istringstream is
{"15,14,foo,13,3,1,12,8,0,11,6,4,7,5,2,10"};
80 EXPECT_TRUE(is
.fail());
83 TEST(Board
, RepeatedTileInput
) {
84 std::istringstream is
{"15,15,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
87 EXPECT_TRUE(is
.fail());
90 TEST(Board
, LowTileInput
) {
91 std::istringstream is
{"-1,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
94 EXPECT_TRUE(is
.fail());
97 TEST(Board
, HighTileInput
) {
98 std::istringstream is
{"16,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
101 EXPECT_TRUE(is
.fail());
105 Board b
{{16,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10}};
106 EXPECT_EQ(8, b
.hole());
109 TEST(Board
, NoHole
) {
110 Board b
{{16,14,9,13,3,1,12,8,16,11,6,4,7,5,2,10}};
111 EXPECT_THROW(b
.hole(), std::runtime_error
);
114 TEST(Board
, ZeroDistance
) {
115 Board b
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
116 EXPECT_EQ(0, b
.distance(b
));
119 TEST(Board
, DistanceAdjacentTilesFlipped
) {
120 Board b1
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
121 Board b2
{{2,1,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
122 EXPECT_EQ(2, b1
.distance(b2
));
125 TEST(Board
, DistanceOneMoveRemaining
) {
126 Board b1
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
127 Board b2
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,15}};
128 EXPECT_EQ(2, b1
.distance(b2
));
131 TEST(Board
, DistanceCornersSwapped
) {
132 Board b1
{{0,2,3,13,5,6,7,8,9,10,11,12,4,14,15,1}};
133 Board b2
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
134 EXPECT_EQ(24, b1
.distance(b2
));
137 TEST(Board
, MaxDistance
) {
138 Board b1
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
139 Board b2
{{0,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1}};
140 EXPECT_EQ(64, b1
.distance(b2
));
143 TEST(Step
, TwoSuccessors
) {
144 auto s
= std::make_shared
<Step
>(Step
{{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}}, nullptr});
145 std::vector
<std::shared_ptr
<Step
>> suc
= s
->successors(s
);
146 EXPECT_THAT(suc
, testing::UnorderedElementsAre(
147 Pointee(Field(&Step::board
, Board
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,15}})),
148 Pointee(Field(&Step::board
, Board
{{1,2,3,4,5,6,7,8,9,10,11,0,13,14,15,12}}))));
151 TEST(Step
, FourSuccessors
) {
152 auto s
= std::make_shared
<Step
>(Step
{{{1,2,3,4,5,0,6,7,8,9,10,11,12,13,14,15}}, nullptr});
153 std::vector
<std::shared_ptr
<Step
>> suc
= s
->successors(s
);
154 EXPECT_THAT(suc
, testing::UnorderedElementsAre(
155 Pointee(Field(&Step::board
, Board
{{1,2,3,4,0,5,6,7,8,9,10,11,12,13,14,15}})),
156 Pointee(Field(&Step::board
, Board
{{1,2,3,4,5,6,0,7,8,9,10,11,12,13,14,15}})),
157 Pointee(Field(&Step::board
, Board
{{1,0,3,4,5,2,6,7,8,9,10,11,12,13,14,15}})),
158 Pointee(Field(&Step::board
, Board
{{1,2,3,4,5,9,6,7,8,0,10,11,12,13,14,15}}))));
162 auto s1
= std::make_shared
<Step
>(Step
{{{1,2,3,4,5,7,11,8,9,6,0,12,13,10,14,15}}, nullptr});
163 auto s2
= std::make_shared
<Step
>(Step
{{{1,2,3,4,5,7,0,8,9,6,11,12,13,10,14,15}}, s1
});
164 auto s3
= std::make_shared
<Step
>(Step
{{{1,2,3,4,5,0,7,8,9,6,11,12,13,10,14,15}}, s2
});
165 auto s4
= std::make_shared
<Step
>(Step
{{{1,2,3,4,5,6,7,8,9,0,11,12,13,10,14,15}}, s3
});
166 auto s5
= std::make_shared
<Step
>(Step
{{{1,2,3,4,5,6,7,8,9,10,11,12,13,0,14,15}}, s4
});
167 auto s6
= std::make_shared
<Step
>(Step
{{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,15}}, s5
});
168 auto s7
= std::make_shared
<Step
>(Step
{{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}}, s6
});
169 std::ostringstream actual
;
171 std::ostringstream expected
;
172 expected
<< "11 down" << std::endl
173 << "7 right" << std::endl
174 << "6 up" << std::endl
175 << "10 up" << std::endl
176 << "14 left" << std::endl
177 << "15 left" << std::endl
;
178 EXPECT_EQ(expected
.str(), actual
.str());