Moved the testing source to the yaml-reader folder.

This commit is contained in:
Jesse Beder
2008-07-14 05:18:25 +00:00
parent 2e2c489e29
commit 2eab1e025e
11 changed files with 13 additions and 192 deletions

View File

@@ -1,93 +0,0 @@
#include "parser.h"
#include "tests.h"
#include <fstream>
#include <iostream>
#ifdef _DEBUG
#pragma comment(lib, "yamlcppd.lib")
#else
#pragma comment(lib, "yamlcpp.lib")
#endif
struct Vec3 {
float x, y, z;
friend std::ostream& operator << (std::ostream& out, const Vec3& v) {
out << v.x << " " << v.y << " " << v.z;
return out;
}
};
void operator >> (const YAML::Node& node, Vec3& v)
{
node[0] >> v.x;
node[1] >> v.y;
node[2] >> v.z;
}
struct Room {
std::string name;
Vec3 pos, size;
float height;
friend std::ostream& operator << (std::ostream& out, const Room& room) {
out << "Name: " << room.name << std::endl;
out << "Pos: " << room.pos << std::endl;
out << "Size: " << room.size << std::endl;
out << "Height: " << room.height << std::endl;
return out;
}
};
void operator >> (const YAML::Node& node, Room& room)
{
node["name"] >> room.name;
node["pos"] >> room.pos;
node["size"] >> room.size;
node["height"] >> room.height;
}
struct Level {
std::vector <Room> rooms;
friend std::ostream& operator << (std::ostream& out, const Level& level) {
for(unsigned i=0;i<level.rooms.size();i++) {
out << level.rooms[i];
out << "---------------------------------------\n";
}
return out;
}
};
void operator >> (const YAML::Node& node, Level& level)
{
const YAML::Node& rooms = node["rooms"];
for(unsigned i=0;i<rooms.size();i++) {
Room room;
rooms[i] >> room;
level.rooms.push_back(room);
}
}
int main()
{
YAML::Test::RunAll();
//std::ifstream fin("test.yaml");
//try {
// YAML::Parser parser(fin);
// if(!parser)
// return 0;
// YAML::Node doc;
// parser.GetNextDocument(doc);
// std::cout << doc;
//} catch(YAML::Exception&) {
// std::cout << "Error parsing the yaml!\n";
//}
getchar();
return 0;
}

View File

@@ -1,81 +0,0 @@
#include "tests.h"
#include "parser.h"
#include <fstream>
#include <sstream>
#include <vector>
#include <iostream>
namespace YAML
{
namespace Test
{
// runs all the tests on all data we have
void RunAll()
{
std::vector <std::string> files;
files.push_back("tests/simple.yaml");
files.push_back("tests/mixed.yaml");
files.push_back("tests/scalars.yaml");
files.push_back("tests/directives.yaml");
bool passed = true;
for(unsigned i=0;i<files.size();i++) {
if(!YAML::Test::Inout(files[i])) {
std::cout << "Inout test failed on " << files[i] << std::endl;
passed = false;
}
}
if(passed)
std::cout << "All tests passed!\n";
}
// loads the given YAML file, outputs it, and then loads the outputted file,
// outputs again, and makes sure that the two outputs are the same
bool Inout(const std::string& file)
{
std::ifstream fin(file.c_str());
try {
// read and output
Parser parser(fin);
if(!parser)
return false;
Node doc;
parser.GetNextDocument(doc);
std::stringstream out;
out << doc;
// and save
std::string firstTry = out.str();
// and now again
parser.Load(out);
if(!parser)
return false;
parser.GetNextDocument(doc);
std::stringstream out2;
out2 << doc;
// and save
std::string secondTry = out2.str();
// now compare
if(firstTry == secondTry)
return true;
std::ofstream fout("tests/out.yaml");
fout << "---\n";
fout << firstTry << std::endl;
fout << "---\n";
fout << secondTry << std::endl;
} catch(ParserException& e) {
std::cout << file << " (line " << e.line + 1 << ", col " << e.column + 1 << "): " << e.msg << std::endl;
return false;
}
return true;
}
}
}

View File

@@ -1,10 +0,0 @@
#include <string>
namespace YAML
{
namespace Test {
void RunAll();
bool Inout(const std::string& file);
}
}