Skip to content

Commit

Permalink
Tests: Add schematic unittests
Browse files Browse the repository at this point in the history
Improve schematic file-saving interface
Add ability to create temporary test files
  • Loading branch information
kwolekr committed May 8, 2015
1 parent 33c1141 commit 2dba29e
Show file tree
Hide file tree
Showing 8 changed files with 329 additions and 32 deletions.
1 change: 1 addition & 0 deletions build/android/jni/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ LOCAL_SRC_FILES := \
jni/src/unittest/test_objdef.cpp \
jni/src/unittest/test_profiler.cpp \
jni/src/unittest/test_random.cpp \
jni/src/unittest/test_schematic.cpp \
jni/src/unittest/test_serialization.cpp \
jni/src/unittest/test_settings.cpp \
jni/src/unittest/test_socket.cpp \
Expand Down
28 changes: 16 additions & 12 deletions src/mg_schematic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ void Schematic::resolveNodeNames()
}


void Schematic::blitToVManip(v3s16 p, MMVManip *vm,
Rotation rot, bool force_placement)
void Schematic::blitToVManip(v3s16 p, MMVManip *vm, Rotation rot, bool force_place)
{
sanity_check(m_ndef != NULL);

Expand Down Expand Up @@ -151,7 +150,7 @@ void Schematic::blitToVManip(v3s16 p, MMVManip *vm,
if (schemdata[i].param1 == MTSCHEM_PROB_NEVER)
continue;

if (!force_placement) {
if (!force_place) {
content_t c = vm->m_data[vi].getContent();
if (c != CONTENT_AIR && c != CONTENT_IGNORE)
continue;
Expand All @@ -174,7 +173,7 @@ void Schematic::blitToVManip(v3s16 p, MMVManip *vm,


void Schematic::placeStructure(Map *map, v3s16 p, u32 flags,
Rotation rot, bool force_placement)
Rotation rot, bool force_place)
{
assert(schemdata != NULL); // Pre-condition
sanity_check(m_ndef != NULL);
Expand All @@ -198,7 +197,7 @@ void Schematic::placeStructure(Map *map, v3s16 p, u32 flags,
v3s16 bp2 = getNodeBlockPos(p + s - v3s16(1,1,1));
vm->initialEmerge(bp1, bp2);

blitToVManip(p, vm, rot, force_placement);
blitToVManip(p, vm, rot, force_place);

std::map<v3s16, MapBlock *> lighting_modified_blocks;
std::map<v3s16, MapBlock *> modified_blocks;
Expand Down Expand Up @@ -405,35 +404,40 @@ bool Schematic::loadSchematicFromFile(const std::string &filename,
}


bool Schematic::saveSchematicToFile(const std::string &filename)
bool Schematic::saveSchematicToFile(const std::string &filename,
INodeDefManager *ndef)
{
MapNode *orig_schemdata = schemdata;
std::vector<std::string> ndef_nodenames;
std::vector<std::string> *names;

// Only carry out the modification if we know the nodes
// were resolved at this point
if (m_resolve_done) {
if (m_resolve_done && ndef == NULL)
ndef = m_ndef;

if (ndef) {
names = &ndef_nodenames;

u32 volume = size.X * size.Y * size.Z;
schemdata = new MapNode[volume];
for (u32 i = 0; i != volume; i++)
schemdata[i] = orig_schemdata[i];

generate_nodelist_and_update_ids(schemdata, volume, names, m_ndef);
generate_nodelist_and_update_ids(schemdata, volume, names, ndef);
} else { // otherwise, use the names we have on hand in the list
names = &m_nodenames;
}

std::ostringstream os(std::ios_base::binary);
serializeToMts(&os, *names);
bool status = serializeToMts(&os, *names);

if (m_resolve_done) {
if (ndef) {
delete []schemdata;
schemdata = orig_schemdata;
}

if (!status)
return false;

return fs::safeWriteToFile(filename, os.str());
}

Expand Down
27 changes: 11 additions & 16 deletions src/mg_schematic.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,38 +85,33 @@ enum SchematicFormatType {

class Schematic : public ObjDef, public NodeResolver {
public:
std::vector<content_t> c_nodes;

u32 flags;
v3s16 size;
MapNode *schemdata;
u8 *slice_probs;

Schematic();
virtual ~Schematic();

virtual void resolveNodeNames();

void updateContentIds();

void blitToVManip(v3s16 p, MMVManip *vm,
Rotation rot, bool force_placement);

bool loadSchematicFromFile(const std::string &filename, INodeDefManager *ndef,
StringMap *replace_names);
bool saveSchematicToFile(const std::string &filename);
StringMap *replace_names=NULL);
bool saveSchematicToFile(const std::string &filename, INodeDefManager *ndef);
bool getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2);

bool deserializeFromMts(std::istream *is, std::vector<std::string> *names);
bool serializeToMts(std::ostream *os, const std::vector<std::string> &names);
bool serializeToLua(std::ostream *os, const std::vector<std::string> &names,
bool use_comments, u32 indent_spaces);

void placeStructure(Map *map, v3s16 p, u32 flags,
Rotation rot, bool force_placement);
void blitToVManip(v3s16 p, MMVManip *vm, Rotation rot, bool force_place);
void placeStructure(Map *map, v3s16 p, u32 flags, Rotation rot, bool force_place);

void applyProbabilities(v3s16 p0,
std::vector<std::pair<v3s16, u8> > *plist,
std::vector<std::pair<s16, u8> > *splist);

std::vector<content_t> c_nodes;
u32 flags;
v3s16 size;
MapNode *schemdata;
u8 *slice_probs;
};

class SchematicManager : public ObjDefManager {
Expand Down
7 changes: 3 additions & 4 deletions src/script/lua_api/l_mapgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1031,10 +1031,9 @@ int ModApiMapgen::l_generate_decorations(lua_State *L)
// create_schematic(p1, p2, probability_list, filename, y_slice_prob_list)
int ModApiMapgen::l_create_schematic(lua_State *L)
{
Schematic schem;
schem.m_ndef = getServer(L)->getNodeDefManager();

INodeDefManager *ndef = getServer(L)->getNodeDefManager();
Map *map = &(getEnv(L)->getMap());
Schematic schem;

v3s16 p1 = check_v3s16(L, 1);
v3s16 p2 = check_v3s16(L, 2);
Expand Down Expand Up @@ -1081,7 +1080,7 @@ int ModApiMapgen::l_create_schematic(lua_State *L)

schem.applyProbabilities(p1, &prob_list, &slice_prob_list);

schem.saveSchematicToFile(filename);
schem.saveSchematicToFile(filename, ndef);
actionstream << "create_schematic: saved schematic file '"
<< filename << "'." << std::endl;

Expand Down
1 change: 1 addition & 0 deletions src/unittest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ set (UNITTEST_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/test_objdef.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_profiler.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_random.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_schematic.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_serialization.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_settings.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_socket.cpp
Expand Down
26 changes: 26 additions & 0 deletions src/unittest/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,35 @@ bool TestBase::testModule(IGameDef *gamedef)
<< " failures / " << num_tests_run << " tests) - " << tdiff
<< "ms" << std::endl;

if (!m_test_dir.empty())
fs::RecursiveDelete(m_test_dir);

return num_tests_failed == 0;
}

std::string TestBase::getTestTempDirectory()
{
if (!m_test_dir.empty())
return m_test_dir;

char buf[32];
snprintf(buf, sizeof(buf), "%08X", myrand());

m_test_dir = fs::TempPath() + DIR_DELIM "mttest_" + buf;
if (!fs::CreateDir(m_test_dir))
throw TestFailedException();

return m_test_dir;
}

std::string TestBase::getTestTempFile()
{
char buf[32];
snprintf(buf, sizeof(buf), "%08X", myrand());

return getTestTempDirectory() + DIR_DELIM + buf + ".tmp";
}


/*
NOTE: These tests became non-working then NodeContainer was removed.
Expand Down
6 changes: 6 additions & 0 deletions src/unittest/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,17 @@ class IGameDef;
class TestBase {
public:
bool testModule(IGameDef *gamedef);
std::string getTestTempDirectory();
std::string getTestTempFile();

virtual void runTests(IGameDef *gamedef) = 0;
virtual const char *getName() = 0;

u32 num_tests_failed;
u32 num_tests_run;

private:
std::string m_test_dir;
};

class TestManager {
Expand Down
Loading

0 comments on commit 2dba29e

Please sign in to comment.