]>
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> | |
6 | ||
5d2f7c7c SW |
7 | using testing::Field; |
8 | using testing::ElementsAreArray; | |
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 SW |
38 | |
39 | TEST(Board, GoodInput) { | |
40 | std::istringstream is{"15,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10"}; | |
41 | Board b; | |
42 | is >> b; | |
43 | EXPECT_FALSE(is.fail()); | |
44 | EXPECT_TRUE(is.eof()); | |
5d2f7c7c | 45 | EXPECT_THAT(b.board, ElementsAreArray({15,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10})); |
32688d85 SW |
46 | } |
47 | ||
48 | TEST(Board, ShortInput) { | |
49 | std::istringstream is{"15,14,9,13,3,1,12,8,0,11,6,4,7,5,2"}; | |
50 | Board b; | |
51 | is >> b; | |
52 | EXPECT_TRUE(is.fail()); | |
53 | } | |
54 | ||
55 | TEST(Board, NonNumericInput) { | |
56 | std::istringstream is{"15,14,foo,13,3,1,12,8,0,11,6,4,7,5,2,10"}; | |
57 | Board b; | |
58 | is >> b; | |
59 | EXPECT_TRUE(is.fail()); | |
60 | } | |
b18667f2 SW |
61 | |
62 | TEST(Board, RepeatedTileInput) { | |
63 | std::istringstream is{"15,15,9,13,3,1,12,8,0,11,6,4,7,5,2,10"}; | |
64 | Board b; | |
65 | is >> b; | |
66 | EXPECT_TRUE(is.fail()); | |
67 | } | |
68 | ||
69 | TEST(Board, LowTileInput) { | |
70 | std::istringstream is{"-1,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10"}; | |
71 | Board b; | |
72 | is >> b; | |
73 | EXPECT_TRUE(is.fail()); | |
74 | } | |
75 | ||
76 | TEST(Board, HighTileInput) { | |
77 | std::istringstream is{"16,14,9,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 | } | |
f3f55aff SW |
82 | |
83 | TEST(Board, Hole) { | |
84 | Board b{{16,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10}}; | |
85 | EXPECT_EQ(8, b.hole()); | |
86 | } | |
87 | ||
88 | TEST(Board, NoHole) { | |
89 | Board b{{16,14,9,13,3,1,12,8,16,11,6,4,7,5,2,10}}; | |
90 | EXPECT_THROW(b.hole(), std::runtime_error); | |
91 | } | |
9c32325f SW |
92 | |
93 | TEST(Board, ZeroDistance) { | |
94 | Board b{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}}; | |
95 | EXPECT_EQ(0, b.distance(b)); | |
96 | } | |
97 | ||
98 | TEST(Board, DistanceAdjacentTilesFlipped) { | |
99 | Board b1{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}}; | |
100 | Board b2{{2,1,3,4,5,6,7,8,9,10,11,12,13,14,15,0}}; | |
101 | EXPECT_EQ(2, b1.distance(b2)); | |
102 | } | |
103 | ||
104 | TEST(Board, DistanceOneMoveRemaining) { | |
105 | Board b1{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}}; | |
106 | Board b2{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,15}}; | |
107 | EXPECT_EQ(2, b1.distance(b2)); | |
108 | } | |
109 | ||
110 | TEST(Board, DistanceCornersSwapped) { | |
111 | Board b1{{0,2,3,13,5,6,7,8,9,10,11,12,4,14,15,1}}; | |
112 | Board b2{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}}; | |
113 | EXPECT_EQ(24, b1.distance(b2)); | |
114 | } | |
115 | ||
116 | TEST(Board, MaxDistance) { | |
117 | Board b1{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}}; | |
118 | Board b2{{0,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1}}; | |
119 | EXPECT_EQ(64, b1.distance(b2)); | |
120 | } | |
5d2f7c7c SW |
121 | |
122 | TEST(Step, TwoSuccessors) { | |
123 | 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}); | |
124 | std::vector<Step*> suc = s->successors(s); | |
125 | EXPECT_THAT(suc, testing::UnorderedElementsAre( | |
126 | Field(&Step::board, Field(&Board::board, ElementsAreArray({1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,15}))), | |
127 | Field(&Step::board, Field(&Board::board, ElementsAreArray({1,2,3,4,5,6,7,8,9,10,11,0,13,14,15,12}))))); | |
128 | } | |
129 | ||
130 | TEST(Step, FourSuccessors) { | |
131 | 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}); | |
132 | std::vector<Step*> suc = s->successors(s); | |
133 | EXPECT_THAT(suc, testing::UnorderedElementsAre( | |
134 | Field(&Step::board, Field(&Board::board, ElementsAreArray({1,2,3,4,0,5,6,7,8,9,10,11,12,13,14,15}))), | |
135 | Field(&Step::board, Field(&Board::board, ElementsAreArray({1,2,3,4,5,6,0,7,8,9,10,11,12,13,14,15}))), | |
136 | Field(&Step::board, Field(&Board::board, ElementsAreArray({1,0,3,4,5,2,6,7,8,9,10,11,12,13,14,15}))), | |
137 | Field(&Step::board, Field(&Board::board, ElementsAreArray({1,2,3,4,5,9,6,7,8,0,10,11,12,13,14,15}))))); | |
138 | } |