cyphesis/tests/BaseWorldtest.cpp

215 lines
5.5 KiB
C++
Raw Permalink Normal View History

// Cyphesis Online RPG Server and AI Engine
// Copyright (C) 2006 Alistair Riddoch
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2009-04-09 12:42:44 +01:00
// $Id$
2011-02-15 09:30:46 +00:00
#ifdef NDEBUG
#undef NDEBUG
#endif
#ifndef DEBUG
#define DEBUG
#endif
2010-03-20 16:15:34 +00:00
#include "common/BaseWorld.h"
#include <Atlas/Objects/RootOperation.h>
#include <sigc++/functors/ptr_fun.h>
2010-03-20 16:15:34 +00:00
#include <cstdlib>
#include <cassert>
2010-03-20 16:15:34 +00:00
static void test_function(Atlas::Objects::Operation::RootOperation)
{
}
class LocatedEntity
2010-03-20 16:15:34 +00:00
{
public:
const std::string m_id;
const long m_intId;
explicit LocatedEntity(const std::string & id, long intId) : m_id(id),
m_intId(intId) { }
2010-03-20 16:15:34 +00:00
const std::string & getId() const {
return m_id;
}
long getIntId() const {
return m_intId;
}
};
class TestWorld : public BaseWorld {
public:
explicit TestWorld(LocatedEntity & gw) : BaseWorld(gw) {
2010-03-20 16:15:34 +00:00
m_eobjects[m_gameWorld.getIntId()] = &m_gameWorld;
}
2011-10-31 12:47:56 +01:00
virtual bool idle(const SystemTime &) { return false; }
virtual LocatedEntity * addEntity(LocatedEntity * ent) {
2010-03-20 16:15:34 +00:00
m_eobjects[ent->getIntId()] = ent;
return 0;
}
virtual LocatedEntity * addNewEntity(const std::string &,
2010-03-20 16:15:34 +00:00
const Atlas::Objects::Entity::RootEntity &) {
return 0;
}
int createSpawnPoint(const Atlas::Message::MapType & data,
LocatedEntity *) { return 0; }
2010-03-20 16:15:34 +00:00
int getSpawnList(Atlas::Message::ListType & data) { return 0; }
LocatedEntity * spawnNewEntity(const std::string & name,
2010-03-20 16:15:34 +00:00
const std::string & type,
const Atlas::Objects::Entity::RootEntity & desc) {
return addNewEntity(type, desc);
}
virtual Task * newTask(const std::string &, LocatedEntity &) { return 0; }
2010-03-20 16:15:34 +00:00
virtual Task * activateTask(const std::string &, const std::string &,
LocatedEntity *, LocatedEntity &) { return 0; }
virtual ArithmeticScript * newArithmetic(const std::string &, LocatedEntity *) {
return 0;
}
virtual void message(const Atlas::Objects::Operation::RootOperation & op,
LocatedEntity & ent) { }
virtual LocatedEntity * findByName(const std::string & name) { return 0; }
virtual LocatedEntity * findByType(const std::string & type) { return 0; }
virtual void addPerceptive(LocatedEntity *) { }
2010-03-20 16:15:34 +00:00
};
int main()
{
// We have to use the TestWorld class, as it implements the functions
// missing from BaseWorld interface.
{
// Test constructor
LocatedEntity wrld("1", 1);
TestWorld tw(wrld);
}
{
// Test destructor
LocatedEntity wrld("1", 1);
BaseWorld * tw = new TestWorld(wrld);
delete tw;
}
{
// Test constructor sets singleton pointer
LocatedEntity wrld("1", 1);
TestWorld tw(wrld);
assert(&BaseWorld::instance() == &tw);
}
{
// Test constructor installs reference to world entity
LocatedEntity wrld("1", 1);
TestWorld tw(wrld);
assert(&tw.m_gameWorld == &wrld);
}
{
// Test retrieving non existant entity by string ID is ok
LocatedEntity wrld("1", 1);
TestWorld tw(wrld);
assert(tw.getEntity("2") == 0);
}
2009-05-05 23:02:34 +01:00
{
2010-03-20 16:17:48 +00:00
// Test retrieving existant entity by string ID is ok
LocatedEntity wrld("1", 1);
2009-05-05 23:02:34 +01:00
TestWorld tw(wrld);
LocatedEntity * tc = new LocatedEntity("2", 2);
2009-05-05 23:02:34 +01:00
tw.addEntity(tc);
assert(tw.getEntity("2") == tc);
2010-03-20 16:17:48 +00:00
}
{
// Test retrieving existant entity by integer ID is ok
LocatedEntity wrld("1", 1);
2010-03-20 16:17:48 +00:00
TestWorld tw(wrld);
LocatedEntity * tc = new LocatedEntity("2", 2);
2010-03-20 16:17:48 +00:00
tw.addEntity(tc);
assert(tw.getEntity(2) == tc);
2009-05-05 23:02:34 +01:00
}
{
// Test retrieving non existant entity by integer ID is ok
LocatedEntity wrld("1", 1);
TestWorld tw(wrld);
assert(tw.getEntity(2) == 0);
}
{
// Test retrieving reference to all entities is okay and empty
LocatedEntity wrld("1", 1);
TestWorld tw(wrld);
assert(tw.getEntities().size() == 1);
}
{
// Test getting the time
LocatedEntity wrld("1", 1);
TestWorld tw(wrld);
tw.getTime();
}
{
// Test getting the uptime
LocatedEntity wrld("1", 1);
TestWorld tw(wrld);
tw.upTime();
}
{
// Test connecting to the dispatch signal
LocatedEntity wrld("1", 1);
TestWorld tw(wrld);
tw.Dispatching.connect(sigc::ptr_fun(&test_function));
}
return 0;
}
2010-03-20 16:15:34 +00:00
long integerId(const std::string & id)
{
long intId = strtol(id.c_str(), 0, 10);
if (intId == 0 && id != "0") {
intId = -1L;
}
return intId;
}
int timeoffset = 0;