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