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