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