1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-07 03:12:36 +01:00

Merge pull request #570 from danhedron/test_up/IDE

Remove data dependency from LoaderIDE test suite
This commit is contained in:
Daniel Evans 2018-08-06 21:43:46 +01:00 committed by GitHub
commit 634a3940ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 112 additions and 64 deletions

View File

@ -172,7 +172,7 @@ public:
loddistances_[n] = d;
}
float getLodDistance(int n) {
float getLodDistance(int n) const {
RW_CHECK(n < 3, "Lod Index out of range");
return loddistances_[n];
}

View File

@ -15,9 +15,11 @@
bool LoaderIDE::load(const std::string &filename, const PedStatsList &stats) {
std::ifstream str(filename);
if (!str.is_open()) return false;
return load(str, stats);
}
bool LoaderIDE::load(std::istream& str, const PedStatsList& stats) {
auto find_stat_id = [&](const std::string &name) {
auto it =
std::find_if(stats.begin(), stats.end(),
@ -170,7 +172,7 @@ bool LoaderIDE::load(const std::string &filename, const PedStatsList &stats) {
getline(strstream, peds->animgroup_, ',');
getline(strstream, buff, ',');
peds->carsmask_ = lexical_cast<int>(buff);
peds->carsmask_ = lexical_cast<int>(buff, 16);
objects.emplace(peds->id(), std::move(peds));
break;

View File

@ -24,6 +24,8 @@ public:
// Load the IDE data into memory
bool load(const std::string& filename, const PedStatsList& stats);
bool load(std::istream& data, const PedStatsList& stats);
/**
* @brief objects loaded during the call to load()
*/

View File

@ -16,14 +16,22 @@ inline Dest bit_cast(Source const &source) {
template <class T, class S>
inline T lexical_cast(const S& s);
template <class T, class S>
inline T lexical_cast(const S& s, size_t base);
template <>
inline int lexical_cast(const std::string& source) {
inline int lexical_cast(const std::string& source, size_t base) {
char* end = nullptr; //for errors handling
int result = std::strtol(source.c_str(), &end, 10);
int result = std::strtol(source.c_str(), &end, base);
RW_CHECK(end != source.c_str(), "Problem with conversion " << *end << " to int");
return result;
}
template <>
inline int lexical_cast(const std::string& source) {
return lexical_cast<int>(source, 10);
}
template <>
inline float lexical_cast(const std::string& source) {
char* end = nullptr; //for errors handling

View File

@ -19,11 +19,11 @@ set(TESTS
Items
Lifetime
LoaderDFF
LoaderIDE
LoaderIPL
Logger
Menu
Object
ObjectData
Payphone
Pickup
Renderer

94
tests/test_LoaderIDE.cpp Normal file
View File

@ -0,0 +1,94 @@
#include <boost/test/unit_test.hpp>
#include <data/ModelData.hpp>
#include <loaders/LoaderIDE.hpp>
#include "test_Globals.hpp"
namespace {
constexpr auto kTestDataObjects = R"(
objs
1100, NAME, TXD, 1, 220, 0"
end
cars
90, vehicle, texture, car, HANDLING, NAME, richfamily, 10, 7, 0, 164, 0.8
end
peds
1, mod, txd, COP, STAT_COP, man, 7f
end
)";
template<size_t N>
void ASSERT_INSTANCE_IS(BaseModelInfo& info, const char* model, const char* txd,
const std::array<float, N>& lods, size_t flags) {
BOOST_ASSERT(info.type() == SimpleModelInfo::kType);
const auto& t = dynamic_cast<SimpleModelInfo&>(info);
BOOST_TEST(t.name == model);
BOOST_TEST(t.textureslot == txd);
BOOST_TEST(t.getNumAtomics() == lods.size());
for (auto i = 0u; i < lods.size(); ++i) {
BOOST_TEST(t.getLodDistance(i) == lods[i]);
}
BOOST_TEST(t.flags == flags);
}
void ASSERT_VEHICLE_IS(BaseModelInfo& info, const char* model, const char* txd,
VehicleModelInfo::VehicleType type, const char* handling, const char* name,
VehicleModelInfo::VehicleClass clas_, int frequency, ModelID wheel, float wheelScale) {
BOOST_ASSERT(info.type() == VehicleModelInfo::kType);
const auto& t = dynamic_cast<VehicleModelInfo&>(info);
BOOST_TEST(t.name == model);
BOOST_TEST(t.textureslot == txd);
BOOST_TEST(t.vehicletype_ == type);
BOOST_TEST(t.handling_ == handling);
BOOST_TEST(t.vehiclename_ == name);
BOOST_TEST(t.vehicleclass_ == clas_);
BOOST_TEST(t.frequency_ == frequency);
BOOST_TEST(t.wheelmodel_ == wheel);
BOOST_TEST(t.wheelscale_ == wheelScale);
}
void ASSERT_PED_IS(BaseModelInfo& info, const char* model, const char* txd,
PedModelInfo::PedType type, int statindex, const char* animgroup,
size_t carmask) {
BOOST_ASSERT(info.type() == PedModelInfo::kType);
const auto& t = dynamic_cast<PedModelInfo&>(info);
BOOST_TEST(t.name == model);
BOOST_TEST(t.textureslot == txd);
BOOST_TEST(t.pedtype_ == type);
BOOST_TEST(t.statindex_ == statindex);
BOOST_TEST(t.animgroup_ == animgroup);
BOOST_TEST(t.carsmask_ == carmask);
}
}
struct WithLoaderIDE {
LoaderIDE loader;
std::istringstream test_data_stream {kTestDataObjects};
};
BOOST_FIXTURE_TEST_SUITE(LoaderIDETests, WithLoaderIDE)
BOOST_AUTO_TEST_CASE(objects_contains_modelID) {
loader.load(test_data_stream, {});
BOOST_CHECK(loader.objects.find(1100) != loader.objects.end());
}
BOOST_AUTO_TEST_CASE(instance_data_is_correct) {
loader.load(test_data_stream, {});
ASSERT_INSTANCE_IS<1>(*loader.objects[1100], "NAME", "TXD", {220}, 0);
}
BOOST_AUTO_TEST_CASE(vehicle_data_is_correct) {
loader.load(test_data_stream, {});
ASSERT_VEHICLE_IS(*loader.objects[90], "vehicle", "texture", VehicleModelInfo::CAR, "HANDLING", "NAME",
VehicleModelInfo::RICHFAMILY, 10, 164, 0.8f);
}
BOOST_AUTO_TEST_CASE(pedestrian_data_is_correct) {
loader.load(test_data_stream, {});
ASSERT_PED_IS(*loader.objects[1], "mod", "txd", PedModelInfo::COP, -1, "man", 0x7f);
}
BOOST_AUTO_TEST_SUITE_END()

View File

@ -1,58 +0,0 @@
#include <boost/test/unit_test.hpp>
#include <data/ModelData.hpp>
#include <loaders/LoaderIDE.hpp>
#include "test_Globals.hpp"
BOOST_AUTO_TEST_SUITE(ObjectDataTests)
#if RW_TEST_WITH_DATA
BOOST_AUTO_TEST_CASE(test_object_data) {
{
LoaderIDE l;
l.load(Global::get().getGamePath() + "/data/maps/generic.ide", {});
BOOST_ASSERT(l.objects.find(1100) != l.objects.end());
auto obj = l.objects[1100].get();
auto def = dynamic_cast<SimpleModelInfo*>(obj);
BOOST_ASSERT(def->type() == ModelDataType::SimpleInfo);
BOOST_CHECK_EQUAL(def->name, "rd_Corner1");
BOOST_CHECK_EQUAL(def->textureslot, "generic");
BOOST_CHECK_EQUAL(def->getNumAtomics(), 1);
BOOST_CHECK_EQUAL(def->getLodDistance(0), 220);
BOOST_CHECK_EQUAL(def->flags, 0);
}
{
LoaderIDE l;
l.load(Global::get().getGamePath() + "/data/default.ide", {});
BOOST_ASSERT(l.objects.find(90) != l.objects.end());
auto obj = l.objects[90].get();
auto def = dynamic_cast<VehicleModelInfo*>(obj);
BOOST_ASSERT(def->type() == ModelDataType::VehicleInfo);
BOOST_CHECK_EQUAL(def->name, "landstal");
BOOST_CHECK_EQUAL(def->textureslot, "landstal");
BOOST_CHECK_EQUAL(def->vehicletype_, VehicleModelInfo::CAR);
BOOST_CHECK_EQUAL(def->handling_, "LANDSTAL");
BOOST_CHECK_EQUAL(def->vehiclename_, "LANDSTK");
BOOST_CHECK_EQUAL(def->vehicleclass_, VehicleModelInfo::RICHFAMILY);
BOOST_CHECK_EQUAL(def->frequency_, 10);
BOOST_CHECK_EQUAL(def->wheelmodel_, 164);
BOOST_CHECK_CLOSE(def->wheelscale_, 0.8f, 0.01f);
}
}
BOOST_AUTO_TEST_CASE(test_gamedata_data) {
}
#endif
BOOST_AUTO_TEST_SUITE_END()