C
C#17h ago
AizakkuIV

✅ Codes runs as expected, but Unit Test keeps failing

This is the unit test that keeps the helper methods from evaluating to green
[Fact]
public void SaveAndLoadGame()
{
var fileName = "TestGomoku.csv";
var field = InitAGame();
var fieldSaved = (int[,])field.Clone();

Gomoku.SaveGame(field, fileName);

var csvImport = new CsvImport<SavedGame>();
var csvExported = csvImport.Read(fileName);

csvExported.Should().HaveCount(Gomoku.GetStoneCount(field));

field.Should().BeEquivalentTo(fieldSaved, "save must not change field");
Gomoku.LoadGame(15, fileName).Should().BeEquivalentTo(field);
}
[Fact]
public void SaveAndLoadGame()
{
var fileName = "TestGomoku.csv";
var field = InitAGame();
var fieldSaved = (int[,])field.Clone();

Gomoku.SaveGame(field, fileName);

var csvImport = new CsvImport<SavedGame>();
var csvExported = csvImport.Read(fileName);

csvExported.Should().HaveCount(Gomoku.GetStoneCount(field));

field.Should().BeEquivalentTo(fieldSaved, "save must not change field");
Gomoku.LoadGame(15, fileName).Should().BeEquivalentTo(field);
}
And this is the only error message provided
No description
18 Replies
viceroypenguin
viceroypenguin17h ago
looks like your file TesetGomoku.csv has a column named 1, 0, 0, 0, which can't be mapped to a property value. check that the CSV is not invalid
Unknown User
Unknown User17h ago
Message Not Public
Sign In & Join Server To View
AizakkuIV
AizakkuIVOP16h ago
namespace UnitTest
{
public class SavedGame
{
public int No { get; set; }
public int Row { get; set; }
public int Col { get; set; }
public int Player { get; set; }
}
}
namespace UnitTest
{
public class SavedGame
{
public int No { get; set; }
public int Row { get; set; }
public int Col { get; set; }
public int Player { get; set; }
}
}
SavedGame class No;Row;Col;Player 1;0;0;0 2;1;1;1 3;2;2;0 4;3;3;1 5;4;4;0 6;5;5;1 This is the csv file that is used to test the whole program The save and load game methods also work without any issues
public static void SaveGame(int[,] field, string fileName)
{
string[] lines = new string[GetStoneCount(field)];
int idx = 0;
int move = 1;

for (int row = 0; row < field.GetLength(0); row++)
{
for (int col = 0; col < field.GetLength(1); col++)
{
if (field[row, col] != -1)
{
lines[idx] = $"{move};{row};{col};{field[row, col]}";
move++;
idx++;
}
}
}

File.WriteAllLines(fileName, lines);
}
public static void SaveGame(int[,] field, string fileName)
{
string[] lines = new string[GetStoneCount(field)];
int idx = 0;
int move = 1;

for (int row = 0; row < field.GetLength(0); row++)
{
for (int col = 0; col < field.GetLength(1); col++)
{
if (field[row, col] != -1)
{
lines[idx] = $"{move};{row};{col};{field[row, col]}";
move++;
idx++;
}
}
}

File.WriteAllLines(fileName, lines);
}
public static int[,] LoadGame(int boardSize, string fileName)
{
int[,] board = CreateBoard(boardSize);

if (!File.Exists(fileName))
{
return board;
}

string[] line = File.ReadAllLines(fileName);

for (int i = 0; i < line.Length; i++)
{
string[] parts = line[i].Split(';');

if (parts.Length == 4 &&
int.TryParse(parts[1], out int row) &&
int.TryParse(parts[2], out int col) &&
int.TryParse(parts[3], out int player))
{
board[row, col] = player;
}

}

return board;
}
public static int[,] LoadGame(int boardSize, string fileName)
{
int[,] board = CreateBoard(boardSize);

if (!File.Exists(fileName))
{
return board;
}

string[] line = File.ReadAllLines(fileName);

for (int i = 0; i < line.Length; i++)
{
string[] parts = line[i].Split(';');

if (parts.Length == 4 &&
int.TryParse(parts[1], out int row) &&
int.TryParse(parts[2], out int col) &&
int.TryParse(parts[3], out int player))
{
board[row, col] = player;
}

}

return board;
}
Unknown User
Unknown User16h ago
Message Not Public
Sign In & Join Server To View
AizakkuIV
AizakkuIVOP16h ago
The professor wrote the unit test, probably forgot “;” is used as separator and not “,”
Unknown User
Unknown User16h ago
Message Not Public
Sign In & Join Server To View
AizakkuIV
AizakkuIVOP16h ago
Thanks, will do
Unknown User
Unknown User16h ago
Message Not Public
Sign In & Join Server To View
AizakkuIV
AizakkuIVOP16h ago
We’ve never used it before
Unknown User
Unknown User16h ago
Message Not Public
Sign In & Join Server To View
AizakkuIV
AizakkuIVOP16h ago
Till now we’ve used normal arrays or jagged to solve these type of CSV assignments It was never said we’re not allowed to
Unknown User
Unknown User16h ago
Message Not Public
Sign In & Join Server To View
AizakkuIV
AizakkuIVOP16h ago
I’ve only sticked to the way we were taught, because that’s how we’ll be tested during exams And the unit tests are designed in such a way that you can use things we are supposed to know (To prevent people from cheating)
Unknown User
Unknown User16h ago
Message Not Public
Sign In & Join Server To View
AizakkuIV
AizakkuIVOP16h ago
I know what the problem might be, I’ll look into it tomorrow, thanks a lot
Unknown User
Unknown User16h ago
Message Not Public
Sign In & Join Server To View
AizakkuIV
AizakkuIVOP4h ago
I found what the mistake was. It was the header. Everytime the game state was saved, I saved it without adding the header No;Row;Col;Player <-- culprit 1;0;0;0 2;1;1;1 3;2;2;0 4;3;3;1 5;4;4;0 6;5;5;1 I just added the header first and when im loading the game, I start iterating from i = 1 to skip the header
public static void SaveGame(int[,] field, string fileName)
{
string[] lines = new string[GetStoneCount(field) + 1];
int idx = 0;
int move = 1;

lines[idx] = $"No;Row;Col;Player";

for (int row = 0; row < field.GetLength(0); row++)
{
for (int col = 0; col < field.GetLength(1); col++)
{
if (field[row, col] != -1)
{
lines[idx + 1] = $"{move};{row};{col};{field[row, col]}";
move++;
idx++;
}
}
}

File.WriteAllLines(fileName, lines);
}
public static void SaveGame(int[,] field, string fileName)
{
string[] lines = new string[GetStoneCount(field) + 1];
int idx = 0;
int move = 1;

lines[idx] = $"No;Row;Col;Player";

for (int row = 0; row < field.GetLength(0); row++)
{
for (int col = 0; col < field.GetLength(1); col++)
{
if (field[row, col] != -1)
{
lines[idx + 1] = $"{move};{row};{col};{field[row, col]}";
move++;
idx++;
}
}
}

File.WriteAllLines(fileName, lines);
}
public static int[,] LoadGame(int boardSize, string fileName)
{
int[,] board = CreateBoard(boardSize);

if (!File.Exists(fileName))
{
return board;
}

string[] line = File.ReadAllLines(fileName);

for (int i = 1; i < line.Length; i++)
{
string[] parts = line[i].Split(';');

if (parts.Length == 4 &&
int.TryParse(parts[1], out int row) &&
int.TryParse(parts[2], out int col) &&
int.TryParse(parts[3], out int player))
{
board[row, col] = player;
}
}

return board;
}
public static int[,] LoadGame(int boardSize, string fileName)
{
int[,] board = CreateBoard(boardSize);

if (!File.Exists(fileName))
{
return board;
}

string[] line = File.ReadAllLines(fileName);

for (int i = 1; i < line.Length; i++)
{
string[] parts = line[i].Split(';');

if (parts.Length == 4 &&
int.TryParse(parts[1], out int row) &&
int.TryParse(parts[2], out int col) &&
int.TryParse(parts[3], out int player))
{
board[row, col] = player;
}
}

return board;
}
@TeBeCo @viceroypenguin Thanks for helping
AizakkuIV
AizakkuIVOP4h ago
🤙
No description

Did you find this page helpful?