]> git.scottworley.com Git - slidingtile/blame - sliding_tile_lib_test.cc
Done with this tool
[slidingtile] / sliding_tile_lib_test.cc
CommitLineData
e86755d7
SW
1#include "sliding_tile_lib.h"
2
3#include "gtest/gtest.h"
4#include "gmock/gmock.h"
5#include <vector>
6
7TEST(Adjacency, Adjacency) {
8 const signed char LEFT = -1;
9 const signed char RIGHT = +1;
10 const signed char UP = -BOARD_DIM;
11 const signed char DOWN = +BOARD_DIM;
12 for (int i = 0; i < BOARD_SIZE; i++) {
13 SCOPED_TRACE(i);
14 std::vector<signed char> expected;
15 if (i >= BOARD_DIM) {
16 expected.push_back(i + UP);
17 }
18 if (i < BOARD_SIZE - BOARD_DIM) {
19 expected.push_back(i + DOWN);
20 }
21 if (i % BOARD_DIM != 0) {
22 expected.push_back(i + LEFT);
23 }
24 if (i % BOARD_DIM != BOARD_DIM - 1) {
25 expected.push_back(i + RIGHT);
26 }
27
28 std::vector<signed char> actual;
29 for (int j = 0; adjacent[i][j] >= 0; j++) {
30 actual.push_back(adjacent[i][j]);
31 }
32 EXPECT_THAT(actual, testing::UnorderedElementsAreArray(expected));
33 }
34}
32688d85
SW
35
36TEST(Board, GoodInput) {
37 std::istringstream is{"15,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
38 Board b;
39 is >> b;
40 EXPECT_FALSE(is.fail());
41 EXPECT_TRUE(is.eof());
19bd29f9 42 EXPECT_THAT(b.board, testing::ElementsAreArray({15,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10}));
32688d85
SW
43}
44
45TEST(Board, ShortInput) {
46 std::istringstream is{"15,14,9,13,3,1,12,8,0,11,6,4,7,5,2"};
47 Board b;
48 is >> b;
49 EXPECT_TRUE(is.fail());
50}
51
52TEST(Board, NonNumericInput) {
53 std::istringstream is{"15,14,foo,13,3,1,12,8,0,11,6,4,7,5,2,10"};
54 Board b;
55 is >> b;
56 EXPECT_TRUE(is.fail());
57}
b18667f2
SW
58
59TEST(Board, RepeatedTileInput) {
60 std::istringstream is{"15,15,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
61 Board b;
62 is >> b;
63 EXPECT_TRUE(is.fail());
64}
65
66TEST(Board, LowTileInput) {
67 std::istringstream is{"-1,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
68 Board b;
69 is >> b;
70 EXPECT_TRUE(is.fail());
71}
72
73TEST(Board, HighTileInput) {
74 std::istringstream is{"16,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
75 Board b;
76 is >> b;
77 EXPECT_TRUE(is.fail());
78}
f3f55aff
SW
79
80TEST(Board, Hole) {
81 Board b{{16,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10}};
82 EXPECT_EQ(8, b.hole());
83}
84
85TEST(Board, NoHole) {
86 Board b{{16,14,9,13,3,1,12,8,16,11,6,4,7,5,2,10}};
87 EXPECT_THROW(b.hole(), std::runtime_error);
88}
9c32325f
SW
89
90TEST(Board, ZeroDistance) {
91 Board b{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
92 EXPECT_EQ(0, b.distance(b));
93}
94
95TEST(Board, DistanceAdjacentTilesFlipped) {
96 Board b1{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
97 Board b2{{2,1,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
98 EXPECT_EQ(2, b1.distance(b2));
99}
100
101TEST(Board, DistanceOneMoveRemaining) {
102 Board b1{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
103 Board b2{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,15}};
104 EXPECT_EQ(2, b1.distance(b2));
105}
106
107TEST(Board, DistanceCornersSwapped) {
108 Board b1{{0,2,3,13,5,6,7,8,9,10,11,12,4,14,15,1}};
109 Board b2{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
110 EXPECT_EQ(24, b1.distance(b2));
111}
112
113TEST(Board, MaxDistance) {
114 Board b1{{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0}};
115 Board b2{{0,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1}};
116 EXPECT_EQ(64, b1.distance(b2));
117}