]> git.scottworley.com Git - slidingtile/blob - sliding_tile_lib_test.cc
operator< for Board
[slidingtile] / sliding_tile_lib_test.cc
1 #include "sliding_tile_lib.h"
2
3 #include "gtest/gtest.h"
4 #include "gmock/gmock.h"
5 #include <vector>
6 #include <sstream>
7
8 using testing::Field;
9
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++) {
16 SCOPED_TRACE(i);
17 std::vector<signed char> expected;
18 if (i >= BOARD_DIM) {
19 expected.push_back(i + UP);
20 }
21 if (i < BOARD_SIZE - BOARD_DIM) {
22 expected.push_back(i + DOWN);
23 }
24 if (i % BOARD_DIM != 0) {
25 expected.push_back(i + LEFT);
26 }
27 if (i % BOARD_DIM != BOARD_DIM - 1) {
28 expected.push_back(i + RIGHT);
29 }
30
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]);
34 }
35 EXPECT_THAT(actual, testing::UnorderedElementsAreArray(expected));
36 }
37 }
38
39 TEST(Board, Equal) {
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);
44 EXPECT_EQ(b1, b2);
45 EXPECT_FALSE(b1 < b2);
46 EXPECT_FALSE(b2 < b1);
47 }
48
49 TEST(Board, NotEqual) {
50 Board b1{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
51 Board b2{{1,2,3,4,5,6,7,8,10,9,11,12,13,14,15,0}};
52 EXPECT_FALSE(b1 == b2);
53 EXPECT_TRUE(b1 != b2);
54 EXPECT_NE(b1, b2);
55 EXPECT_TRUE(b1 < b2);
56 EXPECT_FALSE(b2 < b1);
57 }
58
59 TEST(Board, GoodInput) {
60 std::istringstream is{"15,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
61 Board b;
62 is >> b;
63 EXPECT_FALSE(is.fail());
64 EXPECT_TRUE(is.eof());
65 EXPECT_EQ((Board{{15,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10}}), b);
66 }
67
68 TEST(Board, ShortInput) {
69 std::istringstream is{"15,14,9,13,3,1,12,8,0,11,6,4,7,5,2"};
70 Board b;
71 is >> b;
72 EXPECT_TRUE(is.fail());
73 }
74
75 TEST(Board, NonNumericInput) {
76 std::istringstream is{"15,14,foo,13,3,1,12,8,0,11,6,4,7,5,2,10"};
77 Board b;
78 is >> b;
79 EXPECT_TRUE(is.fail());
80 }
81
82 TEST(Board, RepeatedTileInput) {
83 std::istringstream is{"15,15,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
84 Board b;
85 is >> b;
86 EXPECT_TRUE(is.fail());
87 }
88
89 TEST(Board, LowTileInput) {
90 std::istringstream is{"-1,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
91 Board b;
92 is >> b;
93 EXPECT_TRUE(is.fail());
94 }
95
96 TEST(Board, HighTileInput) {
97 std::istringstream is{"16,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
98 Board b;
99 is >> b;
100 EXPECT_TRUE(is.fail());
101 }
102
103 TEST(Board, Hole) {
104 Board b{{16,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10}};
105 EXPECT_EQ(8, b.hole());
106 }
107
108 TEST(Board, NoHole) {
109 Board b{{16,14,9,13,3,1,12,8,16,11,6,4,7,5,2,10}};
110 EXPECT_THROW(b.hole(), std::runtime_error);
111 }
112
113 TEST(Board, ZeroDistance) {
114 Board b{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
115 EXPECT_EQ(0, b.distance(b));
116 }
117
118 TEST(Board, DistanceAdjacentTilesFlipped) {
119 Board b1{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
120 Board b2{{2,1,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
121 EXPECT_EQ(2, b1.distance(b2));
122 }
123
124 TEST(Board, DistanceOneMoveRemaining) {
125 Board b1{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
126 Board b2{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,15}};
127 EXPECT_EQ(2, b1.distance(b2));
128 }
129
130 TEST(Board, DistanceCornersSwapped) {
131 Board b1{{0,2,3,13,5,6,7,8,9,10,11,12,4,14,15,1}};
132 Board b2{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
133 EXPECT_EQ(24, b1.distance(b2));
134 }
135
136 TEST(Board, MaxDistance) {
137 Board b1{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
138 Board b2{{0,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1}};
139 EXPECT_EQ(64, b1.distance(b2));
140 }
141
142 TEST(Step, TwoSuccessors) {
143 auto s = std::make_shared<Step>(Step{{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}}, nullptr});
144 std::vector<Step*> suc = s->successors(s);
145 EXPECT_THAT(suc, testing::UnorderedElementsAre(
146 Field(&Step::board, Board{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,15}}),
147 Field(&Step::board, Board{{1,2,3,4,5,6,7,8,9,10,11,0,13,14,15,12}})));
148 }
149
150 TEST(Step, FourSuccessors) {
151 auto s = std::make_shared<Step>(Step{{{1,2,3,4,5,0,6,7,8,9,10,11,12,13,14,15}}, nullptr});
152 std::vector<Step*> suc = s->successors(s);
153 EXPECT_THAT(suc, testing::UnorderedElementsAre(
154 Field(&Step::board, Board{{1,2,3,4,0,5,6,7,8,9,10,11,12,13,14,15}}),
155 Field(&Step::board, Board{{1,2,3,4,5,6,0,7,8,9,10,11,12,13,14,15}}),
156 Field(&Step::board, Board{{1,0,3,4,5,2,6,7,8,9,10,11,12,13,14,15}}),
157 Field(&Step::board, Board{{1,2,3,4,5,9,6,7,8,0,10,11,12,13,14,15}})));
158 }
159
160 TEST(Step, Output) {
161 auto s1 = std::make_shared<Step>(Step{{{1,2,3,4,5,7,11,8,9,6,0,12,13,10,14,15}}, nullptr});
162 auto s2 = std::make_shared<Step>(Step{{{1,2,3,4,5,7,0,8,9,6,11,12,13,10,14,15}}, s1});
163 auto s3 = std::make_shared<Step>(Step{{{1,2,3,4,5,0,7,8,9,6,11,12,13,10,14,15}}, s2});
164 auto s4 = std::make_shared<Step>(Step{{{1,2,3,4,5,6,7,8,9,0,11,12,13,10,14,15}}, s3});
165 auto s5 = std::make_shared<Step>(Step{{{1,2,3,4,5,6,7,8,9,10,11,12,13,0,14,15}}, s4});
166 auto s6 = std::make_shared<Step>(Step{{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,15}}, s5});
167 auto s7 = std::make_shared<Step>(Step{{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}}, s6});
168 std::ostringstream actual;
169 actual << *s7;
170 std::ostringstream expected;
171 expected << "11 down" << std::endl
172 << "7 right" << std::endl
173 << "6 up" << std::endl
174 << "10 up" << std::endl
175 << "14 left" << std::endl
176 << "15 left" << std::endl;
177 EXPECT_EQ(expected.str(), actual.str());
178 }