public static void SerializeWithNullStream() { var board = new Board(); var formatter = new BoardFormatter(); Assert.Throws <ArgumentNullException>(() => formatter.Serialize(null, board)); }
public void Roundtrip() { var board = new Board(); board.MovePiece(new Coordinate(0, 0, 0), new Coordinate(0, 4, 0)); board.MovePiece(new Coordinate(0, 0, 3), new Coordinate(0, 4, 3)); board.MovePiece(new Coordinate(1, 0, 0), new Coordinate(1, 4, 0)); board.MovePiece(new Coordinate(1, 0, 3), new Coordinate(1, 4, 3)); board.MovePiece(new Coordinate(2, 0, 0), new Coordinate(2, 4, 0)); board.MovePiece(new Coordinate(2, 0, 3), new Coordinate(2, 4, 3)); var formatter = new BoardFormatter(); Board newBoard = null; using (var stream = new MemoryStream()) { formatter.Serialize(stream, board); stream.Position = 0; newBoard = formatter.Deserialize(stream) as Board; } Assert.IsNotNull(newBoard); Assert.AreEqual(board.MoveHistory.Count, newBoard.MoveHistory.Count); Move move = board.MoveHistory[2]; Assert.AreEqual(new Coordinate(1, 0, 0), move.Source); Assert.AreEqual(new Coordinate(1, 4, 0), move.Destination); }
public static void SerializeInvalidType() { var board = "0,0:0,4|4,0:4,4|0,0:0,4"; var formatter = new BoardFormatter(); Assert.Throws <ArgumentException>(() => formatter.Serialize(new MemoryStream(), board)); }
protected static void SaveBoard(Board board, string file) { BoardFormatter formatter = new BoardFormatter(); using (Stream stream = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)) { formatter.Serialize(stream, board); } }
public void Serialize() { var board = new Board(); board.MovePiece(new Coordinate(0, 0, 0), new Coordinate(0, 4, 0)); board.MovePiece(new Coordinate(0, 0, 3), new Coordinate(0, 4, 3)); board.MovePiece(new Coordinate(1, 0, 0), new Coordinate(1, 4, 0)); board.MovePiece(new Coordinate(1, 0, 3), new Coordinate(1, 4, 3)); board.MovePiece(new Coordinate(2, 0, 0), new Coordinate(2, 4, 0)); board.MovePiece(new Coordinate(2, 0, 3), new Coordinate(2, 4, 3)); var formatter = new BoardFormatter(); using (var stream = new MemoryStream()) { formatter.Serialize(stream, board); } }
/// <summary> /// Persists the game to disk at the specified path. /// </summary> /// <param name="path"></param> public void SaveGame(string path) { FileStream stream = null; try { this.savePath = path; BoardFormatter formatter = new BoardFormatter(); stream = new FileStream(this.savePath, FileMode.Create); formatter.Serialize(stream, this.board); this.isDirty = false; } finally { if (stream != null) { stream.Dispose(); } } }
public static void RoundTripWithCustomFormatter() { MemoryStream outStream = null, inStream = null; Board board = new Board(), newBoard = null; board.MovePiece(new Point(0, 0), new Point(0, 4)); board.MovePiece(new Point(4, 0), new Point(4, 4)); board.MovePiece(new Point(0, 0), new Point(0, 4)); board.MovePiece(new Point(4, 0), new Point(4, 4)); board.MovePiece(new Point(0, 0), new Point(0, 4)); var boardFormatter = new BoardFormatter(); outStream = new MemoryStream(); boardFormatter.Serialize(outStream, board); inStream = new MemoryStream(outStream.ToArray()); newBoard = (Board)boardFormatter.Deserialize(inStream); Assert.IsNotNull(newBoard, "The new board is null."); Assert.AreEqual(5, newBoard.Moves.Count, "The move history is incorrect."); Assert.AreEqual(Player.O, newBoard.CurrentPlayer, "The current player is incorrect."); Assert.AreEqual(Player.None, newBoard.WinningPlayer, "The winning player is incorrect."); board.MovePiece(new Point(4, 0), new Point(4, 4)); board.MovePiece(new Point(0, 0), new Point(0, 4)); board.MovePiece(new Point(4, 0), new Point(4, 4)); board.MovePiece(new Point(0, 0), new Point(0, 4)); var binaryFormatter = new BinaryFormatter(); outStream = new MemoryStream(); binaryFormatter.Serialize(outStream, board); inStream = new MemoryStream(outStream.ToArray()); newBoard = (Board)binaryFormatter.Deserialize(inStream); Assert.AreEqual(9, newBoard.Moves.Count, "The move history (after the win) is incorrect."); Assert.AreEqual(Player.None, newBoard.CurrentPlayer, "The current player (after the win) is incorrect."); Assert.AreEqual(Player.X, newBoard.WinningPlayer, "The winning player (after the win) is incorrect."); }
public void RoundTripWithCustomFormatter() { IFormatter formatter = null; MemoryStream outStream = null, inStream = null; Board board = new Board(), newBoard = null; board.MovePiece(new Point(0, 0), new Point(0, 4)); board.MovePiece(new Point(4, 0), new Point(4, 4)); board.MovePiece(new Point(0, 0), new Point(0, 4)); board.MovePiece(new Point(4, 0), new Point(4, 4)); board.MovePiece(new Point(0, 0), new Point(0, 4)); formatter = new BoardFormatter(); outStream = new MemoryStream(); formatter.Serialize(outStream, board); inStream = new MemoryStream(outStream.ToArray()); newBoard = (Board)formatter.Deserialize(inStream); Assert.IsNotNull(newBoard, "The new board is null."); Assert.AreEqual(5, newBoard.Moves.Count, "The move history is incorrect."); Assert.AreEqual(Player.O, newBoard.CurrentPlayer, "The current player is incorrect."); Assert.AreEqual(Player.None, newBoard.WinningPlayer, "The winning player is incorrect."); board.MovePiece(new Point(4, 0), new Point(4, 4)); board.MovePiece(new Point(0, 0), new Point(0, 4)); board.MovePiece(new Point(4, 0), new Point(4, 4)); board.MovePiece(new Point(0, 0), new Point(0, 4)); formatter = new BinaryFormatter(); outStream = new MemoryStream(); formatter.Serialize(outStream, board); inStream = new MemoryStream(outStream.ToArray()); newBoard = (Board)formatter.Deserialize(inStream); Assert.AreEqual(9, newBoard.Moves.Count, "The move history (after the win) is incorrect."); Assert.AreEqual(Player.None, newBoard.CurrentPlayer, "The current player (after the win) is incorrect."); Assert.AreEqual(Player.X, newBoard.WinningPlayer, "The winning player (after the win) is incorrect."); }
public void SerializeWithNullStream() { Board board = new Board(); IFormatter formatter = new BoardFormatter(); Stream outStream = null; Assert.Throws<ArgumentNullException>(() => formatter.Serialize(outStream, board)); }
public void SerializeInvalidType() { string board = "0,0:0,4|4,0:4,4|0,0:0,4"; IFormatter formatter = new BoardFormatter(); Stream outStream = new MemoryStream(); Assert.Throws<ArgumentException>(() => formatter.Serialize(outStream, board)); }