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
));
40 TEST(Board
, GoodInput
) {
41 std::istringstream is
{"15,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
44 EXPECT_FALSE(is
.fail());
45 EXPECT_TRUE(is
.eof());
46 EXPECT_THAT(b
.board
, ElementsAreArray({15,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10}));
49 TEST(Board
, ShortInput
) {
50 std::istringstream is
{"15,14,9,13,3,1,12,8,0,11,6,4,7,5,2"};
53 EXPECT_TRUE(is
.fail());
56 TEST(Board
, NonNumericInput
) {
57 std::istringstream is
{"15,14,foo,13,3,1,12,8,0,11,6,4,7,5,2,10"};
60 EXPECT_TRUE(is
.fail());
63 TEST(Board
, RepeatedTileInput
) {
64 std::istringstream is
{"15,15,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
67 EXPECT_TRUE(is
.fail());
70 TEST(Board
, LowTileInput
) {
71 std::istringstream is
{"-1,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
74 EXPECT_TRUE(is
.fail());
77 TEST(Board
, HighTileInput
) {
78 std::istringstream is
{"16,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
81 EXPECT_TRUE(is
.fail());
85 Board b
{{16,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10}};
86 EXPECT_EQ(8, b
.hole());
90 Board b
{{16,14,9,13,3,1,12,8,16,11,6,4,7,5,2,10}};
91 EXPECT_THROW(b
.hole(), std::runtime_error
);
94 TEST(Board
, ZeroDistance
) {
95 Board b
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
96 EXPECT_EQ(0, b
.distance(b
));
99 TEST(Board
, DistanceAdjacentTilesFlipped
) {
100 Board b1
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
101 Board b2
{{2,1,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
102 EXPECT_EQ(2, b1
.distance(b2
));
105 TEST(Board
, DistanceOneMoveRemaining
) {
106 Board b1
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
107 Board b2
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,15}};
108 EXPECT_EQ(2, b1
.distance(b2
));
111 TEST(Board
, DistanceCornersSwapped
) {
112 Board b1
{{0,2,3,13,5,6,7,8,9,10,11,12,4,14,15,1}};
113 Board b2
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
114 EXPECT_EQ(24, b1
.distance(b2
));
117 TEST(Board
, MaxDistance
) {
118 Board b1
{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
119 Board b2
{{0,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1}};
120 EXPECT_EQ(64, b1
.distance(b2
));
123 TEST(Step
, TwoSuccessors
) {
124 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});
125 std::vector
<Step
*> suc
= s
->successors(s
);
126 EXPECT_THAT(suc
, testing::UnorderedElementsAre(
127 Field(&Step::board
, Field(&Board::board
, ElementsAreArray({1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,15}))),
128 Field(&Step::board
, Field(&Board::board
, ElementsAreArray({1,2,3,4,5,6,7,8,9,10,11,0,13,14,15,12})))));
131 TEST(Step
, FourSuccessors
) {
132 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});
133 std::vector
<Step
*> suc
= s
->successors(s
);
134 EXPECT_THAT(suc
, testing::UnorderedElementsAre(
135 Field(&Step::board
, Field(&Board::board
, ElementsAreArray({1,2,3,4,0,5,6,7,8,9,10,11,12,13,14,15}))),
136 Field(&Step::board
, Field(&Board::board
, ElementsAreArray({1,2,3,4,5,6,0,7,8,9,10,11,12,13,14,15}))),
137 Field(&Step::board
, Field(&Board::board
, ElementsAreArray({1,0,3,4,5,2,6,7,8,9,10,11,12,13,14,15}))),
138 Field(&Step::board
, Field(&Board::board
, ElementsAreArray({1,2,3,4,5,9,6,7,8,0,10,11,12,13,14,15})))));
142 auto s1
= std::shared_ptr
<Step
>(new Step
{{{1,2,3,4,5,7,11,8,9,6,0,12,13,10,14,15}}, nullptr});
143 auto s2
= std::shared_ptr
<Step
>(new Step
{{{1,2,3,4,5,7,0,8,9,6,11,12,13,10,14,15}}, s1
});
144 auto s3
= std::shared_ptr
<Step
>(new Step
{{{1,2,3,4,5,0,7,8,9,6,11,12,13,10,14,15}}, s2
});
145 auto s4
= std::shared_ptr
<Step
>(new Step
{{{1,2,3,4,5,6,7,8,9,0,11,12,13,10,14,15}}, s3
});
146 auto s5
= std::shared_ptr
<Step
>(new Step
{{{1,2,3,4,5,6,7,8,9,10,11,12,13,0,14,15}}, s4
});
147 auto s6
= std::shared_ptr
<Step
>(new Step
{{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,15}}, s5
});
148 auto s7
= std::shared_ptr
<Step
>(new Step
{{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}}, s6
});
149 std::ostringstream actual
;
151 std::ostringstream expected
;
152 expected
<< "11 down" << std::endl
153 << "7 right" << std::endl
154 << "6 up" << std::endl
155 << "10 up" << std::endl
156 << "14 left" << std::endl
157 << "15 left" << std::endl
;
158 EXPECT_EQ(expected
.str(), actual
.str());