Skip to content

Commit db966ad

Browse files
committed
Added some uniit test outlines. Unable to fully implement until have
full gameboard data structure.
1 parent a9a53a0 commit db966ad

File tree

3 files changed

+56
-27
lines changed

3 files changed

+56
-27
lines changed

include/GameBoard.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <string>
99
#include <iostream>
1010
#include <fstream>
11+
#include <cstddef>
1112

1213
#include "Util.h"
1314
#include "GamePiece.h"
@@ -17,17 +18,17 @@ class GameBoard {
1718
std::map<Coordinate, std::unique_ptr<GamePiece>> pieces;
1819
std::vector<std::unique_ptr<const Road>> roads;
1920

20-
int constructBoardFromFile(std::ifstream &file);
21-
int constructFileFromBoard(std::ofstream &file);
21+
int constructBoardFromFile(std::ifstream * file);
22+
int constructFileFromBoard(std::ofstream * file);
2223

2324
public:
2425
GameBoard();
2526
GameBoard(GameBoard&) = delete;
2627
~GameBoard();
2728
GameBoard& operator=(GameBoard&) = delete;
2829

29-
int saveBoardToFile(std::string filename);
30-
int loadBoardFromFile(std::string filename);
30+
int save_Board(std::string filename);
31+
int load_Board(std::string filename);
3132
};
3233

3334
#endif

src/GameBoard.cpp

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,52 +8,58 @@ GameBoard::~GameBoard() {
88

99
}
1010

11-
int GameBoard::saveBoardToFile(std::string filename){
12-
std::ofstream file;
13-
file.exceptions(std::ofstream::failbit | std::ofstream::badbit);
11+
12+
int GameBoard::save_Board(std::string filename){
13+
std::ofstream * file = new std::ofstream;
14+
file->exceptions(std::ofstream::failbit | std::ofstream::badbit);
1415
try {
1516
filename = filename + ".wocs";
16-
file.open(filename.c_str(), std::fstream::out | std::fstream::trunc);
17+
file->open(filename.c_str());
1718
constructFileFromBoard(file);
18-
19-
} catch (std::ofstream::failure e){
20-
std::cerr << "Exception opening/reading/closing file \n";
19+
file->close();
20+
delete(file);
21+
return 0;
22+
} catch (std::ofstream::failure e) {
23+
std::cerr << "Exception opening/closing/writing file: " << e.what();
24+
delete (file);
2125
}
26+
return -1;
2227

23-
24-
25-
return 0;
2628
}
2729

28-
int GameBoard::loadBoardFromFile(std::string filename){
29-
std::ifstream file;
30-
file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
30+
int GameBoard::load_Board(std::string filename){
31+
std::ifstream * file = new std::ifstream;
3132
try {
3233
filename = filename + ".wocs";
33-
file.open(filename.c_str(), std::fstream::in);
34+
file->open(filename.c_str());
3435
constructBoardFromFile(file);
35-
36-
file.close();
36+
file->close();
37+
delete(file);
38+
return 0;
3739
} catch (std::ifstream::failure e) {
38-
std::cerr << "Exception opening/reading/closing file\n";
40+
std::cerr << "Exception opening/closing/reading file: " << e.what();
41+
delete (file);
3942
}
40-
return 0;
43+
return -1;
4144
}
4245

43-
int GameBoard::constructBoardFromFile(std::ifstream &file){
46+
int GameBoard::constructBoardFromFile(std::ifstream * file){
4447
//Parse and construct the board from the file
4548
//@ TODO
46-
while (!file.eof()) {
47-
std::cout << file.get();
49+
std::string line;
50+
if (file->is_open()) {
51+
while (getline(*file, line)) {
52+
std::cout << line << '\n';
53+
}
4854
}
4955
return 0;
5056
}
5157

5258

53-
int GameBoard::constructFileFromBoard(std::ofstream &file){
59+
int GameBoard::constructFileFromBoard(std::ofstream * file){
5460
//Construct the file based on the structure of the board
5561
//@ TODO
56-
file << "Hello World!";
62+
*file << "Hello World!";
5763
return 0;
5864
}
5965

tests/test_GameBoard.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* test_GameBoard.cpp
3+
*
4+
* Created on: Feb 22, 2014
5+
* Author: The Pickle
6+
*/
7+
8+
#include "GameBoard.h"
9+
#include "UnitTest++.h"
10+
11+
12+
TEST(saveFile_simple){
13+
GameBoard * test_board = new GameBoard;
14+
CHECK(test_board->save_Board("test_1") == 0);
15+
delete(test_board);
16+
}
17+
18+
TEST(loadFile_simple){
19+
GameBoard * test_board = new GameBoard;
20+
CHECK(test_board->load_Board("test_1") == 0);
21+
delete(test_board);
22+
}

0 commit comments

Comments
 (0)