]> git.scottworley.com Git - slidingtile/blob - sliding_tile_lib_test.cc
Tweak the default max_frontier
[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 using testing::Pointee;
10
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++) {
17 SCOPED_TRACE(i);
18 std::vector<signed char> expected;
19 if (i >= BOARD_DIM) {
20 expected.push_back(i + UP);
21 }
22 if (i < BOARD_SIZE - BOARD_DIM) {
23 expected.push_back(i + DOWN);
24 }
25 if (i % BOARD_DIM != 0) {
26 expected.push_back(i + LEFT);
27 }
28 if (i % BOARD_DIM != BOARD_DIM - 1) {
29 expected.push_back(i + RIGHT);
30 }
31
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]);
35 }
36 EXPECT_THAT(actual, testing::UnorderedElementsAreArray(expected));
37 }
38 }
39
40 TEST(Board, Equal) {
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);
45 EXPECT_EQ(b1, b2);
46 EXPECT_FALSE(b1 < b2);
47 EXPECT_FALSE(b2 < b1);
48 }
49
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);
55 EXPECT_NE(b1, b2);
56 EXPECT_TRUE(b1 < b2);
57 EXPECT_FALSE(b2 < b1);
58 }
59
60 TEST(Board, GoodInput) {
61 std::istringstream is{"15,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
62 Board b;
63 is >> b;
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);
67 }
68
69 TEST(Board, ShortInput) {
70 std::istringstream is{"15,14,9,13,3,1,12,8,0,11,6,4,7,5,2"};
71 Board b;
72 is >> b;
73 EXPECT_TRUE(is.fail());
74 }
75
76 TEST(Board, NonNumericInput) {
77 std::istringstream is{"15,14,foo,13,3,1,12,8,0,11,6,4,7,5,2,10"};
78 Board b;
79 is >> b;
80 EXPECT_TRUE(is.fail());
81 }
82
83 TEST(Board, RepeatedTileInput) {
84 std::istringstream is{"15,15,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
85 Board b;
86 is >> b;
87 EXPECT_TRUE(is.fail());
88 }
89
90 TEST(Board, LowTileInput) {
91 std::istringstream is{"-1,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
92 Board b;
93 is >> b;
94 EXPECT_TRUE(is.fail());
95 }
96
97 TEST(Board, HighTileInput) {
98 std::istringstream is{"16,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
99 Board b;
100 is >> b;
101 EXPECT_TRUE(is.fail());
102 }
103
104 TEST(Board, Hole) {
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());
107 }
108
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);
112 }
113
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));
117 }
118
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));
123 }
124
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));
129 }
130
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));
135 }
136
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));
141 }
142
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}}))));
149 }
150
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}}))));
159 }
160
161 TEST(Step, Output) {
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;
170 actual << *s7;
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());
179 }