aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitlab-ci.yml2
-rw-r--r--CMakeLists.txt1
-rw-r--r--src/pgnp.cpp19
-rw-r--r--src/pgnp.hpp6
-rw-r--r--tests/CMakeLists.txt1
-rw-r--r--tests/pgn_files/valid/pgn1.pgn2
-rw-r--r--tests/tests.cpp10
7 files changed, 33 insertions, 8 deletions
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index b14b443..7add331 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -3,4 +3,4 @@ archlinux:
before_script:
- pacman -Sy base-devel cmake --noconfirm --needed
script:
- - mkdir build && cd build && cmake ../ && make && make test && ctest
+ - mkdir build && cd build && cmake ../ && make && make test && cd tests && ./pgnp_tests
diff --git a/CMakeLists.txt b/CMakeLists.txt
index b86b74c..237f223 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -12,5 +12,4 @@ configure_file(src/pgnp.hpp ${PGNP_INCLUDE_DIR} COPYONLY)
include_directories(${PGNP_INCLUDE_DIR})
# Unit tests
-enable_testing()
add_subdirectory(./tests)
diff --git a/src/pgnp.cpp b/src/pgnp.cpp
index 5336cd6..7f9c450 100644
--- a/src/pgnp.cpp
+++ b/src/pgnp.cpp
@@ -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);
}
}
diff --git a/src/pgnp.hpp b/src/pgnp.hpp
index 0e9c7a5..53a7a59 100644
--- a/src/pgnp.hpp
+++ b/src/pgnp.hpp
@@ -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) {
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index fe07b37..4b556aa 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -6,4 +6,3 @@ file(COPY pgn_files DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/)
# Run tests
add_executable(pgnp_tests tests.cpp ./catch3/catch_amalgamated.cpp)
target_link_libraries(pgnp_tests pgnp)
-add_test(PGNP_Tests pgnp_tests)
diff --git a/tests/pgn_files/valid/pgn1.pgn b/tests/pgn_files/valid/pgn1.pgn
index f593f02..d9808a5 100644
--- a/tests/pgn_files/valid/pgn1.pgn
+++ b/tests/pgn_files/valid/pgn1.pgn
@@ -17,4 +17,4 @@
[Termination "Normal"]
[Annotator "lichess.org"]
-1. g3 d5 2. Bg2 Nf6 3. c4 c6
+1. g3 d5 2. Bg2 Nf6 3. c4 c6 *
diff --git a/tests/tests.cpp b/tests/tests.cpp
index 3838ef6..d766abd 100644
--- a/tests/tests.cpp
+++ b/tests/tests.cpp
@@ -3,7 +3,7 @@
using namespace pgnp;
-TEST_CASE("Valid PGN", "[pgn1]") {
+TEST_CASE("Valid PGN", "[valid/pgn1]") {
PGN pgn;
REQUIRE_NOTHROW(pgn.FromFile("pgn_files/valid/pgn1.pgn"));
REQUIRE_THROWS(pgn.STRCheck());
@@ -55,18 +55,22 @@ TEST_CASE("Valid PGN", "[pgn1]") {
CHECK(pgn.GetTagValue("TimeControl") == "600+5");
CHECK_THROWS_AS(pgn.GetTagValue("InvalidTagName"), InvalidTagName);
}
+
+ CHECK(pgn.GetResult() == "*");
}
-TEST_CASE("Valid PGN", "[pgn2]") {
+TEST_CASE("Valid PGN", "[valid/pgn2]") {
PGN pgn;
REQUIRE_NOTHROW(pgn.FromFile("pgn_files/valid/pgn2.pgn"));
REQUIRE_THROWS(pgn.STRCheck());
REQUIRE(pgn.GetMoves()->GetLength() == 66);
+ CHECK(pgn.GetResult() == "0-1");
}
-TEST_CASE("Seven Tag Roster", "[pgn1]") {
+TEST_CASE("Seven Tag Roster", "[std/pgn1]") {
PGN pgn;
REQUIRE_NOTHROW(pgn.FromFile("pgn_files/str/pgn1.pgn"));
REQUIRE_NOTHROW(pgn.STRCheck());
REQUIRE(pgn.GetMoves()->GetLength() == 85);
+ CHECK(pgn.GetResult() == "1/2-1/2");
}