From 3fd631c42a7d660a3bca83b87b87c362273fa78c Mon Sep 17 00:00:00 2001 From: suhrke Date: Mon, 3 Jul 2017 22:19:50 -0700 Subject: [PATCH] queue scan added, Catch added for EntityConverter tests --- .gitmodules | 3 ++ ReflexToQ3/.gitignore | 3 ++ ReflexToQ3/Makefile | 11 +++- ReflexToQ3/includes/Catch | 1 + ReflexToQ3/includes/EntityConverter.cpp | 68 +++++++++++++++++-------- ReflexToQ3/includes/EntityConverter.hpp | 11 ++++ ReflexToQ3/test/catch.cpp | 63 +++++++++++++++++++++++ 7 files changed, 138 insertions(+), 22 deletions(-) create mode 160000 ReflexToQ3/includes/Catch create mode 100644 ReflexToQ3/test/catch.cpp diff --git a/.gitmodules b/.gitmodules index 82fc687..b4d4ce2 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "ReflexToQ3/includes/cxxopts"] path = ReflexToQ3/includes/cxxopts url = https://github.com/jarro2783/cxxopts.git +[submodule "ReflexToQ3/includes/Catch"] + path = ReflexToQ3/includes/Catch + url = https://github.com/philsquared/Catch.git diff --git a/ReflexToQ3/.gitignore b/ReflexToQ3/.gitignore index ce562d2..1f243d5 100644 --- a/ReflexToQ3/.gitignore +++ b/ReflexToQ3/.gitignore @@ -14,3 +14,6 @@ # executables reflex2q3 +test/catch +test/test-parser + diff --git a/ReflexToQ3/Makefile b/ReflexToQ3/Makefile index 8c016ed..9a3061a 100644 --- a/ReflexToQ3/Makefile +++ b/ReflexToQ3/Makefile @@ -1,7 +1,8 @@ EX=reflex2q3 CC=g++ -CFLAGS=-std=c++11 -I"./includes" -I"./includes/cxxopts/include" -I"/usr/include/eigen3" +CFLAGS=-std=c++11 -I"./includes" -I"./includes/Catch/single_include" -I"./includes/cxxopts/include" -I"/usr/include/eigen3" TESTEX=test/test-parser +UNITEX=test/catch all: main @@ -11,9 +12,15 @@ main: planes.o brushdef.o oopless-parser.o EntityConverter.o test: planes.o brushdef.o oopless-parser.o test-parser.o $(CC) $^ $(CFLAGS) -o $(TESTEX) +unittest: EntityConverter.o catch.o + $(CC) $^ $(CFLAGS) -o $(UNITEX) + test-parser.o: test/test-parser.cpp $(CC) -c $^ $(CFLAGS) +catch.o: test/catch.cpp + $(CC) -c $^ $(CFLAGS) + oopless-parser.o: includes/oopless-parser.cpp $(CC) -c $^ $(CFLAGS) @@ -27,5 +34,5 @@ EntityConverter.o: includes/EntityConverter.cpp $(CC) -c $^ $(CFLAGS) clean: - rm *.o *.log $(EX) $(TESTEX) + rm *.o *.log $(EX) $(TESTEX) $(UNITEX) diff --git a/ReflexToQ3/includes/Catch b/ReflexToQ3/includes/Catch new file mode 160000 index 0000000..6f32db3 --- /dev/null +++ b/ReflexToQ3/includes/Catch @@ -0,0 +1 @@ +Subproject commit 6f32db35af06b30701d159b9e16a21e76d82aada diff --git a/ReflexToQ3/includes/EntityConverter.cpp b/ReflexToQ3/includes/EntityConverter.cpp index 709c939..6018d62 100644 --- a/ReflexToQ3/includes/EntityConverter.cpp +++ b/ReflexToQ3/includes/EntityConverter.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include @@ -49,25 +50,8 @@ EntityConverter::EntityConverter(std::string entityMapFile, std::string reflexMa if ( fin.is_open() ) { //Extract the source type of targets (teleporters or jump pads) std::string line; - std::string trash; - std::string targetName; while (std::getline(fin, line)) { - if ( line.find("type Teleporter") != std::string::npos) { - std::getline(fin, line); - std::istringstream iss(line); - if ( ! (iss >> trash >> trash >> targetName)) { - throw std::runtime_error( "format error in .map file"); - } - targetMap_.insert ( std::pair(targetName, "Teleporter") ); - } - else if ( line.find("type JumpPad") != std::string::npos) { - std::getline(fin, line); - std::istringstream iss(line); - if ( ! (iss >> trash >> trash >> targetName)) { - throw std::runtime_error( "format error in .map file"); - } - targetMap_.insert ( std::pair(targetName, "JumpPad") ); - } + addIfRelated(line, fin); } } else { @@ -85,7 +69,11 @@ EntityConverter::EntityConverter(std::string entityMapFile, std::string reflexMa /* *-------------------------------------------------------------------------------------- - * !-- Not sure if this is used the same way as the pre-scan was --! + * Class: EntityConverter + * Method: EntityConverter :: matchRelated + * Description: Read through entities, matching related as necessary + * Note: For now, accomplishes the same goal as the pre-scan + * constructor *-------------------------------------------------------------------------------------- */ void @@ -95,8 +83,22 @@ EntityConverter::matchRelated(std::queue> entities) std::cerr << "Related entities are already matched, doing nothing" << std::endl; } else { - //Same as pre-scan or convert and pass back all converted entities? + while ( ! entities.empty() ) { + std::vector entity = entities.front(); + entities.pop(); + + std::stringstream ss; + std::copy(entity.begin(), entity.end(), + std::ostream_iterator(ss, "\n")); + + std::string nextLine; + if ( getline(ss, nextLine )) { + addIfRelated(nextLine, ss); + } + } } + + areEntitiesMatched_ = true; } @@ -197,6 +199,32 @@ EntityConverter::mapEntities(std::string mapFile) +void +EntityConverter::addIfRelated(std::string &line, std::istream &is) +{ + std::string trash; + std::string targetName; + if ( line.find("type Teleporter") != std::string::npos) { + std::getline(is, line); + std::istringstream iss(line); + if ( ! (iss >> trash >> trash >> targetName)) { + throw std::runtime_error( "format error in .map file"); + } + targetMap_.insert ( std::pair(targetName, "Teleporter") ); + } + else if ( line.find("type JumpPad") != std::string::npos) { + std::getline(is, line); + std::istringstream iss(line); + if ( ! (iss >> trash >> trash >> targetName)) { + throw std::runtime_error( "format error in .map file"); + } + targetMap_.insert ( std::pair(targetName, "JumpPad") ); + } + +} + + + std::vector EntityConverter::convertPickup(std::vector &lines) { diff --git a/ReflexToQ3/includes/EntityConverter.hpp b/ReflexToQ3/includes/EntityConverter.hpp index 898d227..b05216f 100644 --- a/ReflexToQ3/includes/EntityConverter.hpp +++ b/ReflexToQ3/includes/EntityConverter.hpp @@ -107,6 +107,17 @@ class EntityConverter *-------------------------------------------------------------------------------------- */ void mapEntities(std::string mapFile); + /* + *-------------------------------------------------------------------------------------- + * Class: EntityConverter + * Method: EntityConverter :: addIfRelated + * Description: If the entity contains a related target/etc, add to map + * Paramater: string line, the previous line (contains entity type) + * Parameter: istream is, an ifstream or a stringstream containing a + * single entity + *-------------------------------------------------------------------------------------- + */ + void addIfRelated(std::string &line, std::istream &is); /* *-------------------------------------------------------------------------------------- diff --git a/ReflexToQ3/test/catch.cpp b/ReflexToQ3/test/catch.cpp new file mode 100644 index 0000000..bde52d3 --- /dev/null +++ b/ReflexToQ3/test/catch.cpp @@ -0,0 +1,63 @@ +/* + * ===================================================================================== + * + * Filename: catch.cpp + * + * Description: Unit Tests for EntityConverter + * + * Version: 0.1 + * Created: 07/03/2017 08:25:04 PM + * Revision: none + * Compiler: gcc + * + * Author: suhrke@teknik.io + * + * ===================================================================================== + */ + +#ifndef CATCH_CONFIG_MAIN +#define CATCH_CONFIG_MAIN +#include "catch.hpp" +#include +#include + +#include "EntityConverter.hpp" + +#define PICKUP_FILENAME "r2x.pck" + + + +TEST_CASE( "r2x: a single pickup entity can be converted", "[EntityConverter]" ) { + + // Instantiate object + EntityConverter ec (PICKUP_FILENAME); + + // Mock up entity + std::vector entity; + entity.push_back(" type Pickup"); + entity.push_back(" Vector3 position -216.00000 -132.00000 -1488.000488"); + entity.push_back(" Vector3 angles 180.00000 0.00000 0.00000"); + entity.push_back(" UInt8 pickupType 2"); + + // Mock up entity queue + std::queue> q; + + // Match related entities (none) + ec.matchRelated( q ); + + // (only a single pickup entity) + std::vector converted = ec.convert(entity); + + REQUIRE( converted[0] == "\"classname\" \"weapon_grenadelauncher\"\n" ); +} + + + + + + + + + + +#endif //CATCH_CONFIG_MAIN