Cleaning tests

This commit is contained in:
Loic Guegan 2022-01-24 18:32:05 +01:00
parent af333f9ff1
commit 5d4a7d66cb
7 changed files with 33 additions and 8 deletions

View file

@ -1,6 +1,7 @@
#include "pgnp.hpp"
#include <iostream>
#include <string>
#define IS_BLANK(c) (c == ' ' || c == '\n' || c == '\t')
#define IS_DIGIT(c) \
@ -59,6 +60,8 @@ PGN::~PGN() {
delete moves;
}
std::string PGN::GetResult() { return (result); }
void PGN::FromFile(std::string filepath) {
std::ifstream file(filepath);
@ -84,6 +87,9 @@ void PGN::FromString(std::string pgn_content) {
}
loc++;
}
if (result.size() <= 0) {
throw InvalidGameResult();
}
}
void PGN::STRCheck() {
@ -124,7 +130,18 @@ int PGN::ParseLine(int loc, HalfMove *hm) {
// Check if we reach score entry (* or 1-0 or 0-1 or 1/2-1/2)
if (!IS_EOF(loc + 1)) {
char nc = pgn_content[loc + 1]; // Next c
if ((IS_DIGIT(c) && nc == '-') or (IS_DIGIT(c) && nc == '/')) {
if ((IS_DIGIT(c) && nc == '-') or (IS_DIGIT(c) && nc == '/') or c == '*') {
if (c == '*') {
result = "*";
} else if (nc == '-') {
if (c == '1') {
result = "1-0";
} else {
result = "0-1";
}
} else {
result = "1/2-1/2";
}
return (loc);
}
}

View file

@ -35,6 +35,7 @@ class PGN {
private:
std::unordered_map<std::string, std::string> tags;
std::vector<std::string> tagkeys;
std::string result;
HalfMove *moves;
std::string pgn_content;
@ -50,6 +51,7 @@ public:
void Dump();
std::vector<std::string> GetTagList();
std::string GetTagValue(std::string);
std::string GetResult();
HalfMove *GetMoves();
private:
@ -72,6 +74,10 @@ struct InvalidTagName : public std::exception {
const char *what() const throw() { return "Invalid tag name"; }
};
struct InvalidGameResult : public std::exception {
const char *what() const throw() { return "Invalid game result"; }
};
struct UnexpectedCharacter : public std::exception {
std::string msg;
UnexpectedCharacter(char actual, char required, int loc) {