]> git.scottworley.com Git - slidingtile/blame - sliding_tile_lib_test.cc
successors is const
[slidingtile] / sliding_tile_lib_test.cc
CommitLineData
e86755d7
SW
1#include "sliding_tile_lib.h"
2
3#include "gtest/gtest.h"
4#include "gmock/gmock.h"
5#include <vector>
49358f36 6#include <sstream>
e86755d7 7
5d2f7c7c 8using testing::Field;
5d2f7c7c
SW
9
10TEST(Step, Adjacency) {
e86755d7
SW
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;
5d2f7c7c
SW
32 for (int j = 0; Step::adjacent[i][j] >= 0; j++) {
33 actual.push_back(Step::adjacent[i][j]);
e86755d7
SW
34 }
35 EXPECT_THAT(actual, testing::UnorderedElementsAreArray(expected));
36 }
37}
32688d85 38
0e89d341
SW
39TEST(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);
310a7132
SW
45 EXPECT_FALSE(b1 < b2);
46 EXPECT_FALSE(b2 < b1);
0e89d341
SW
47}
48
49TEST(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);
310a7132
SW
55 EXPECT_TRUE(b1 < b2);
56 EXPECT_FALSE(b2 < b1);
0e89d341
SW
57}
58
32688d85
SW
59TEST(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());
9b1ce995 65 EXPECT_EQ((Board{{15,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10}}), b);
32688d85
SW
66}
67
68TEST(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
75TEST(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}
b18667f2
SW
81
82TEST(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
89TEST(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
96TEST(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}
f3f55aff
SW
102
103TEST(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
108TEST(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}
9c32325f
SW
112
113TEST(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
118TEST(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
124TEST(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
130TEST(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
136TEST(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}
5d2f7c7c
SW
141
142TEST(Step, TwoSuccessors) {
f548f940 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});
5d2f7c7c
SW
144 std::vector<Step*> suc = s->successors(s);
145 EXPECT_THAT(suc, testing::UnorderedElementsAre(
9b1ce995
SW
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}})));
5d2f7c7c
SW
148}
149
150TEST(Step, FourSuccessors) {
f548f940 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});
5d2f7c7c
SW
152 std::vector<Step*> suc = s->successors(s);
153 EXPECT_THAT(suc, testing::UnorderedElementsAre(
9b1ce995
SW
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}})));
5d2f7c7c 158}
49358f36
SW
159
160TEST(Step, Output) {
f548f940
SW
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});
49358f36
SW
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}