queue scan added, Catch added for EntityConverter tests

This commit is contained in:
suhrke 2017-07-03 22:19:50 -07:00
parent 991cb4d02a
commit 3fd631c42a
7 changed files with 138 additions and 22 deletions

3
.gitmodules vendored
View File

@ -1,3 +1,6 @@
[submodule "ReflexToQ3/includes/cxxopts"] [submodule "ReflexToQ3/includes/cxxopts"]
path = ReflexToQ3/includes/cxxopts path = ReflexToQ3/includes/cxxopts
url = https://github.com/jarro2783/cxxopts.git url = https://github.com/jarro2783/cxxopts.git
[submodule "ReflexToQ3/includes/Catch"]
path = ReflexToQ3/includes/Catch
url = https://github.com/philsquared/Catch.git

View File

@ -14,3 +14,6 @@
# executables # executables
reflex2q3 reflex2q3
test/catch
test/test-parser

View File

@ -1,7 +1,8 @@
EX=reflex2q3 EX=reflex2q3
CC=g++ 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 TESTEX=test/test-parser
UNITEX=test/catch
all: main 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 test: planes.o brushdef.o oopless-parser.o test-parser.o
$(CC) $^ $(CFLAGS) -o $(TESTEX) $(CC) $^ $(CFLAGS) -o $(TESTEX)
unittest: EntityConverter.o catch.o
$(CC) $^ $(CFLAGS) -o $(UNITEX)
test-parser.o: test/test-parser.cpp test-parser.o: test/test-parser.cpp
$(CC) -c $^ $(CFLAGS) $(CC) -c $^ $(CFLAGS)
catch.o: test/catch.cpp
$(CC) -c $^ $(CFLAGS)
oopless-parser.o: includes/oopless-parser.cpp oopless-parser.o: includes/oopless-parser.cpp
$(CC) -c $^ $(CFLAGS) $(CC) -c $^ $(CFLAGS)
@ -27,5 +34,5 @@ EntityConverter.o: includes/EntityConverter.cpp
$(CC) -c $^ $(CFLAGS) $(CC) -c $^ $(CFLAGS)
clean: clean:
rm *.o *.log $(EX) $(TESTEX) rm *.o *.log $(EX) $(TESTEX) $(UNITEX)

@ -0,0 +1 @@
Subproject commit 6f32db35af06b30701d159b9e16a21e76d82aada

View File

@ -20,6 +20,7 @@
#include <exception> #include <exception>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <iterator>
#include <sstream> #include <sstream>
@ -49,25 +50,8 @@ EntityConverter::EntityConverter(std::string entityMapFile, std::string reflexMa
if ( fin.is_open() ) { if ( fin.is_open() ) {
//Extract the source type of targets (teleporters or jump pads) //Extract the source type of targets (teleporters or jump pads)
std::string line; std::string line;
std::string trash;
std::string targetName;
while (std::getline(fin, line)) { while (std::getline(fin, line)) {
if ( line.find("type Teleporter") != std::string::npos) { addIfRelated(line, fin);
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<std::string, std::string>(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<std::string, std::string>(targetName, "JumpPad") );
}
} }
} }
else { 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 void
@ -95,8 +83,22 @@ EntityConverter::matchRelated(std::queue<std::vector<std::string>> entities)
std::cerr << "Related entities are already matched, doing nothing" << std::endl; std::cerr << "Related entities are already matched, doing nothing" << std::endl;
} }
else { else {
//Same as pre-scan or convert and pass back all converted entities? while ( ! entities.empty() ) {
std::vector<std::string> entity = entities.front();
entities.pop();
std::stringstream ss;
std::copy(entity.begin(), entity.end(),
std::ostream_iterator<std::string>(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<std::string, std::string>(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<std::string, std::string>(targetName, "JumpPad") );
}
}
std::vector<std::string> std::vector<std::string>
EntityConverter::convertPickup(std::vector<std::string> &lines) EntityConverter::convertPickup(std::vector<std::string> &lines)
{ {

View File

@ -107,6 +107,17 @@ class EntityConverter
*-------------------------------------------------------------------------------------- *--------------------------------------------------------------------------------------
*/ */
void mapEntities(std::string mapFile); 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);
/* /*
*-------------------------------------------------------------------------------------- *--------------------------------------------------------------------------------------

63
ReflexToQ3/test/catch.cpp Normal file
View File

@ -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 <queue>
#include <vector>
#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<std::string> 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<std::vector<std::string>> q;
// Match related entities (none)
ec.matchRelated( q );
// (only a single pickup entity)
std::vector<std::string> converted = ec.convert(entity);
REQUIRE( converted[0] == "\"classname\" \"weapon_grenadelauncher\"\n" );
}
#endif //CATCH_CONFIG_MAIN