]>
Commit | Line | Data |
---|---|---|
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 | 8 | using testing::Field; |
537a8dc7 | 9 | using testing::Pointee; |
5d2f7c7c SW |
10 | |
11 | TEST(Step, Adjacency) { | |
e86755d7 SW |
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; | |
5d2f7c7c SW |
33 | for (int j = 0; Step::adjacent[i][j] >= 0; j++) { |
34 | actual.push_back(Step::adjacent[i][j]); | |
e86755d7 SW |
35 | } |
36 | EXPECT_THAT(actual, testing::UnorderedElementsAreArray(expected)); | |
37 | } | |
38 | } | |
32688d85 | 39 | |
0e89d341 SW |
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); | |
310a7132 SW |
46 | EXPECT_FALSE(b1 < b2); |
47 | EXPECT_FALSE(b2 < b1); | |
0e89d341 SW |
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); | |
310a7132 SW |
56 | EXPECT_TRUE(b1 < b2); |
57 | EXPECT_FALSE(b2 < b1); | |
0e89d341 SW |
58 | } |
59 | ||
32688d85 SW |
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()); | |
9b1ce995 | 66 | EXPECT_EQ((Board{{15,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10}}), b); |
32688d85 SW |
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 | } | |
b18667f2 SW |
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 | } | |
f3f55aff SW |
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 | } | |
9c32325f SW |
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 | } | |
5d2f7c7c SW |
142 | |
143 | TEST(Step, TwoSuccessors) { | |
f548f940 | 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}); |
537a8dc7 | 145 | std::vector<std::shared_ptr<Step>> suc = s->successors(s); |
5d2f7c7c | 146 | EXPECT_THAT(suc, testing::UnorderedElementsAre( |
537a8dc7 SW |
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}})))); | |
5d2f7c7c SW |
149 | } |
150 | ||
151 | TEST(Step, FourSuccessors) { | |
f548f940 | 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}); |
537a8dc7 | 153 | std::vector<std::shared_ptr<Step>> suc = s->successors(s); |
5d2f7c7c | 154 | EXPECT_THAT(suc, testing::UnorderedElementsAre( |
537a8dc7 SW |
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}})))); | |
5d2f7c7c | 159 | } |
49358f36 SW |
160 | |
161 | TEST(Step, Output) { | |
f548f940 SW |
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}); | |
49358f36 SW |
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 | } |