#include "sliding_tile_lib.h"
+#include <istream>
+
signed char adjacent[BOARD_SIZE][5] = {
1, 4, -1, -1, -1,
0, 2, 5, -1, -1,
10, 13, 15, -1, -1,
11, 14, -1, -1, -1,
};
+
+std::istream& operator>>(std::istream& is, Board& board) {
+ for (int i = 0; i < BOARD_SIZE; i++) {
+ if (!is.good()) {
+ is.setstate(std::istream::failbit);
+ break;
+ }
+ if (i > 0 && is.get() != ',') {
+ is.setstate(std::istream::failbit);
+ break;
+ }
+ int numeric;
+ is >> numeric;
+ board[i] = numeric;
+ }
+ return is;
+}
#ifndef _SLIDING_TILE_LIB_H
#define _SLIDING_TILE_LIB_H
+#include <istream>
+
const int BOARD_DIM = 4;
const int BOARD_SIZE = BOARD_DIM * BOARD_DIM;
+
+typedef signed char Board[BOARD_SIZE];
+std::istream& operator>>(std::istream& is, Board& board);
+
extern signed char adjacent[BOARD_SIZE][5];
#endif /* _SLIDING_TILE_LIB_H */
EXPECT_THAT(actual, testing::UnorderedElementsAreArray(expected));
}
}
+
+TEST(Board, GoodInput) {
+ std::istringstream is{"15,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10"};
+ Board b;
+ is >> b;
+ EXPECT_FALSE(is.fail());
+ EXPECT_TRUE(is.eof());
+ EXPECT_THAT(b, testing::ElementsAreArray({15,14,9,13,3,1,12,8,0,11,6,4,7,5,2,10}));
+}
+
+TEST(Board, ShortInput) {
+ std::istringstream is{"15,14,9,13,3,1,12,8,0,11,6,4,7,5,2"};
+ Board b;
+ is >> b;
+ EXPECT_TRUE(is.fail());
+}
+
+TEST(Board, NonNumericInput) {
+ std::istringstream is{"15,14,foo,13,3,1,12,8,0,11,6,4,7,5,2,10"};
+ Board b;
+ is >> b;
+ EXPECT_TRUE(is.fail());
+}