From e7afb972b8e8ac8b51ca83cff4b4c1108552463e Mon Sep 17 00:00:00 2001 From: cutealien Date: Wed, 9 Dec 2009 01:32:44 +0000 Subject: [PATCH] Add xml-serialization to the serializeAttributes test. Add comment to readAttributeFromXML that userPointers are intentionally set to 0. git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@3033 dfc29bdd-3216-0410-991c-e03cc46cb475 --- source/Irrlicht/CAttributes.cpp | 6 + tests/serializeAttributes.cpp | 399 ++++++++++++++++++++------------ tests/tests-last-passed-at.txt | 2 +- 3 files changed, 259 insertions(+), 148 deletions(-) diff --git a/source/Irrlicht/CAttributes.cpp b/source/Irrlicht/CAttributes.cpp index a775c300..712d9910 100644 --- a/source/Irrlicht/CAttributes.cpp +++ b/source/Irrlicht/CAttributes.cpp @@ -1487,6 +1487,12 @@ void CAttributes::readAttributeFromXML(io::IXMLReader* reader) } addArray(name.c_str(),tmpArray); } + else + if (element == L"userPointer") + { + // It's debatable if a pointer should be set or not, but it's more likely that adding it now would wreck user-applications. + // Also it probably doesn't makes sense setting this to a value when it comes from file. + } } //! Write these attributes into a xml file diff --git a/tests/serializeAttributes.cpp b/tests/serializeAttributes.cpp index 9fc8933d..dd39b5c0 100644 --- a/tests/serializeAttributes.cpp +++ b/tests/serializeAttributes.cpp @@ -4,7 +4,7 @@ using namespace irr; using namespace core; using namespace io; -#define COMPARE(a, b) if ( (a) != (b) ) { logTestString("Not identical %s in %s:%d", #a, __FILE__, __LINE__ ); return false; } +#define COMPARE(a, b) if ( (a) != (b) ) { logTestString("Not identical %s in %s:%d\n", #a, __FILE__, __LINE__ ); return false; } const u32 BINARY_BLOCK_SIZE = 10; @@ -25,183 +25,181 @@ const c8* const MockEnumNames[EME_COUNT+1] = class SerializableMock : public virtual io::IAttributeExchangingObject { public: + SerializableMock(bool comparePointers=true) : ComparePointers(comparePointers) + { + } virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const { - out->addInt("valInt", valInt); - out->addFloat("valFloat", valFloat); - out->addString("valString", valString.c_str()); - out->addString("valStringW", valStringW.c_str()); - out->addBinary("valBinary", (void*)valBinary, BINARY_BLOCK_SIZE); - out->addArray("valStringWArray", valStringWArray); - out->addBool("valBool", valBool); - out->addEnum("valEnum", valEnum, MockEnumNames); - out->addColor("valColor", valColor); - out->addColorf("valColorf", valColorf); - out->addVector3d("valVector3df", valVector3df); - out->addPosition2d("valPosition2di", valPosition2di); - out->addRect("valRect", valRect); - out->addMatrix("valMatrix", valMatrix); - out->addQuaternion("valQuaternion", valQuaternion); - out->addBox3d("valAabbox3df", valAabbox3df); - out->addPlane3d("valPlane3df", valPlane3df); - out->addTriangle3d("valTriangle3df", valTriangle3df); - out->addLine2d("valLine2df", valLine2df); - out->addLine3d("valLine3df", valLine3df); - out->addTexture("valTexture", valTexture ); - out->addUserPointer("valPointer", valPointer); + out->addInt("ValInt", ValInt); + out->addFloat("ValFloat", ValFloat); + out->addString("ValString", ValString.c_str()); + out->addString("ValStringW", ValStringW.c_str()); + out->addBinary("ValBinary", (void*)ValBinary, BINARY_BLOCK_SIZE); + out->addArray("ValStringWArray", ValStringWArray); + out->addBool("ValBool", ValBool); + out->addEnum("ValEnum", ValEnum, MockEnumNames); + out->addColor("ValColor", ValColor); + out->addColorf("ValColorf", ValColorf); + out->addVector3d("ValVector3df", ValVector3df); + out->addPosition2d("ValPosition2di", ValPosition2di); + out->addRect("ValRect", ValRect); + out->addMatrix("ValMatrix", ValMatrix); + out->addQuaternion("ValQuaternion", ValQuaternion); + out->addBox3d("ValAabbox3df", ValAabbox3df); + out->addPlane3d("ValPlane3df", ValPlane3df); + out->addTriangle3d("ValTriangle3df", ValTriangle3df); + out->addLine2d("ValLine2df", ValLine2df); + out->addLine3d("ValLine3df", ValLine3df); + out->addTexture("ValTexture", ValTexture ); + out->addUserPointer("ValPointer", ValPointer); } virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options) { - valInt = in->getAttributeAsInt("valInt"); - valFloat = in->getAttributeAsFloat("valFloat"); - valString = in->getAttributeAsString("valString"); - valStringW = in->getAttributeAsStringW("valStringW"); - in->getAttributeAsBinaryData("valBinary", valBinary, BINARY_BLOCK_SIZE); - valStringWArray = in->getAttributeAsArray("valStringWArray"); - valBool = in->getAttributeAsBool("valBool"); - valEnum = (EMockEnum)in->getAttributeAsEnumeration("valEnum", MockEnumNames); - valColor = in->getAttributeAsColor("valColor"); - valColorf = in->getAttributeAsColorf("valColorf"); - valVector3df = in->getAttributeAsVector3d("valVector3df"); - valPosition2di = in->getAttributeAsPosition2d("valPosition2di"); - valRect = in->getAttributeAsRect("valRect"); - valMatrix = in->getAttributeAsMatrix("valMatrix"); - valQuaternion = in->getAttributeAsQuaternion("valQuaternion"); - valAabbox3df = in->getAttributeAsBox3d("valAabbox3df"); - valPlane3df = in->getAttributeAsPlane3d("valPlane3df"); - valTriangle3df = in->getAttributeAsTriangle3d("valTriangle3df"); - valLine2df = in->getAttributeAsLine2d("valLine2df"); - valLine3df = in->getAttributeAsLine3d("valLine3df"); - valTexture = in->getAttributeAsTexture("valTexture"); - valPointer = in->getAttributeAsUserPointer("valPointer"); + ValInt = in->getAttributeAsInt("ValInt"); + ValFloat = in->getAttributeAsFloat("ValFloat"); + ValString = in->getAttributeAsString("ValString"); + ValStringW = in->getAttributeAsStringW("ValStringW"); + in->getAttributeAsBinaryData("ValBinary", ValBinary, BINARY_BLOCK_SIZE); + ValStringWArray = in->getAttributeAsArray("ValStringWArray"); + ValBool = in->getAttributeAsBool("ValBool"); + ValEnum = (EMockEnum)in->getAttributeAsEnumeration("ValEnum", MockEnumNames); + ValColor = in->getAttributeAsColor("ValColor"); + ValColorf = in->getAttributeAsColorf("ValColorf"); + ValVector3df = in->getAttributeAsVector3d("ValVector3df"); + ValPosition2di = in->getAttributeAsPosition2d("ValPosition2di"); + ValRect = in->getAttributeAsRect("ValRect"); + ValMatrix = in->getAttributeAsMatrix("ValMatrix"); + ValQuaternion = in->getAttributeAsQuaternion("ValQuaternion"); + ValAabbox3df = in->getAttributeAsBox3d("ValAabbox3df"); + ValPlane3df = in->getAttributeAsPlane3d("ValPlane3df"); + ValTriangle3df = in->getAttributeAsTriangle3d("ValTriangle3df"); + ValLine2df = in->getAttributeAsLine2d("ValLine2df"); + ValLine3df = in->getAttributeAsLine3d("ValLine3df"); + ValTexture = in->getAttributeAsTexture("ValTexture"); + ValPointer = in->getAttributeAsUserPointer("ValPointer"); } bool operator==(const SerializableMock& other) { - COMPARE(valInt, other.valInt); - COMPARE(valFloat, other.valFloat); - COMPARE(valString, other.valString); - COMPARE(valStringW, other.valStringW); - if ( memcmp( valBinary, other.valBinary, BINARY_BLOCK_SIZE) != 0 ) + COMPARE(ValInt, other.ValInt); + COMPARE(ValFloat, other.ValFloat); + COMPARE(ValString, other.ValString); + COMPARE(ValStringW, other.ValStringW); + if ( memcmp( ValBinary, other.ValBinary, BINARY_BLOCK_SIZE) != 0 ) { - logTestString("Not identical %s in %s:%d", "valBinary", __FILE__, __LINE__ ); + logTestString("Not identical %s in %s:%d", "ValBinary", __FILE__, __LINE__ ); return false; } - COMPARE(valStringWArray, other.valStringWArray); - COMPARE(valBool, other.valBool); - COMPARE(valEnum, other.valEnum); - COMPARE(valColor, other.valColor); - if ( valColorf.r != other.valColorf.r || valColorf.g != other.valColorf.g || valColorf.b != other.valColorf.b || valColorf.a != other.valColorf.a ) + COMPARE(ValStringWArray, other.ValStringWArray); + COMPARE(ValBool, other.ValBool); + COMPARE(ValEnum, other.ValEnum); + COMPARE(ValColor, other.ValColor); + if ( ValColorf.r != other.ValColorf.r || ValColorf.g != other.ValColorf.g || ValColorf.b != other.ValColorf.b || ValColorf.a != other.ValColorf.a ) { - logTestString("Not identical %s in %s:%d", "valColorf", __FILE__, __LINE__ ); + logTestString("Not identical %s in %s:%d", "ValColorf", __FILE__, __LINE__ ); return false; } - COMPARE(valVector3df, other.valVector3df); - COMPARE(valPosition2di, other.valPosition2di); - COMPARE(valRect, other.valRect); - COMPARE(valMatrix, other.valMatrix); - COMPARE(valQuaternion, other.valQuaternion); - COMPARE(valAabbox3df, other.valAabbox3df); - COMPARE(valPlane3df, other.valPlane3df); - COMPARE(valTriangle3df, other.valTriangle3df); - COMPARE(valLine2df, other.valLine2df); - COMPARE(valLine3df, other.valLine3df); -// valTexture; - COMPARE(valPointer, other.valPointer); + COMPARE(ValVector3df, other.ValVector3df); + COMPARE(ValPosition2di, other.ValPosition2di); + COMPARE(ValRect, other.ValRect); + COMPARE(ValMatrix, other.ValMatrix); + COMPARE(ValQuaternion, other.ValQuaternion); + COMPARE(ValAabbox3df, other.ValAabbox3df); + COMPARE(ValPlane3df, other.ValPlane3df); + COMPARE(ValTriangle3df, other.ValTriangle3df); + COMPARE(ValLine2df, other.ValLine2df); + COMPARE(ValLine3df, other.ValLine3df); +// ValTexture; // TODO + if ( ComparePointers ) + COMPARE(ValPointer, other.ValPointer); return true; } void reset() { - valInt = 0; - valFloat = 0.f; - valString = ""; - valStringW = L""; - memset(valBinary, 0, BINARY_BLOCK_SIZE); - valStringWArray.clear(); - valBool = false; - valEnum = EME_NONE; - valColor.set(0,0,0,0); - valColorf.set(0.f, 0.f, 0.f, 0.f); - valVector3df.set(0.f, 0.f, 0.f); - valPosition2di.set(0,0); - valRect = core::rect(0,0,0,0); - valMatrix.makeIdentity(); - valQuaternion.set(0,0,0,0); - valAabbox3df.reset(0,0,0); - valPlane3df.setPlane(vector3df(0.f,0.f,0.f), 0.f); - valTriangle3df.set( vector3df(0.f,0.f,0.f), vector3df(0.f,0.f,0.f), vector3df(0.f,0.f,0.f) ); - valLine2df.setLine(0.f, 0.f, 0.f, 0.f); - valLine3df.setLine(0.f, 0.f, 0.f, 0.f, 0.f, 0.f); - valTexture = NULL; - valPointer = 0; + ValInt = 0; + ValFloat = 0.f; + ValString = ""; + ValStringW = L""; + memset(ValBinary, 0, BINARY_BLOCK_SIZE); + ValStringWArray.clear(); + ValBool = false; + ValEnum = EME_NONE; + ValColor.set(0,0,0,0); + ValColorf.set(0.f, 0.f, 0.f, 0.f); + ValVector3df.set(0.f, 0.f, 0.f); + ValPosition2di.set(0,0); + ValRect = core::rect(0,0,0,0); + ValMatrix.makeIdentity(); + ValQuaternion.set(0,0,0,0); + ValAabbox3df.reset(0,0,0); + ValPlane3df.setPlane(vector3df(0.f,0.f,0.f), 0.f); + ValTriangle3df.set( vector3df(0.f,0.f,0.f), vector3df(0.f,0.f,0.f), vector3df(0.f,0.f,0.f) ); + ValLine2df.setLine(0.f, 0.f, 0.f, 0.f); + ValLine3df.setLine(0.f, 0.f, 0.f, 0.f, 0.f, 0.f); + ValTexture = NULL; + ValPointer = 0; } void set() { - valInt = 1; - valFloat = 1.f; - valString = "one"; - valStringW = L"ONE"; - memset(valBinary, 0xff, BINARY_BLOCK_SIZE); - valStringWArray.push_back( stringw("ONE") ); - valStringWArray.push_back( stringw("TWO") ); - valStringWArray.push_back( stringw("THREE") ); - valBool = true; - valEnum = EME_ONE; - valColor.set(1,2,3,4); - valColorf.set(1.f, 2.f, 3.f, 4.f); - valVector3df.set(1.f, 2.f, 3.f); - valPosition2di.set(1,2); - valRect = core::rect(1,2,3,4); - valMatrix = 99.9f; - valQuaternion.set(1,2,3,4); - valAabbox3df.reset(1,2,3); - valPlane3df.setPlane(vector3df(1.f,2.f,3.f), 4.f); - valTriangle3df.set( vector3df(1.f,2.f,3.f), vector3df(4.f,5.f,6.f), vector3df(7.f,8.f,9.f) ); - valLine2df.setLine(1.f, 2.f, 3.f, 4.f); - valLine3df.setLine(1.f, 2.f, 3.f, 4.f, 5.f, 6.f); - valTexture = NULL; // TODO - valPointer = (void*)0xffffff; + ValInt = 1; + ValFloat = 1.f; + ValString = "one"; + ValStringW = L"ONE"; + memset(ValBinary, 0xff, BINARY_BLOCK_SIZE); + ValStringWArray.push_back( stringw("ONE") ); + ValStringWArray.push_back( stringw("TWO") ); + ValStringWArray.push_back( stringw("THREE") ); + ValBool = true; + ValEnum = EME_ONE; + ValColor.set(1,2,3,4); + ValColorf.set(1.f, 2.f, 3.f, 4.f); + ValVector3df.set(1.f, 2.f, 3.f); + ValPosition2di.set(1,2); + ValRect = core::rect(1,2,3,4); + ValMatrix = 99.9f; + ValQuaternion.set(1,2,3,4); + ValAabbox3df.reset(1,2,3); + ValPlane3df.setPlane(vector3df(1.f,2.f,3.f), 4.f); + ValTriangle3df.set( vector3df(1.f,2.f,3.f), vector3df(4.f,5.f,6.f), vector3df(7.f,8.f,9.f) ); + ValLine2df.setLine(1.f, 2.f, 3.f, 4.f); + ValLine3df.setLine(1.f, 2.f, 3.f, 4.f, 5.f, 6.f); + ValTexture = NULL; // TODO + ValPointer = (void*)0xffffff; } - s32 valInt; - f32 valFloat; - core::stringc valString; - core::stringw valStringW; - char valBinary[BINARY_BLOCK_SIZE]; - core::array valStringWArray; - bool valBool; - EMockEnum valEnum; - video::SColor valColor; - video::SColorf valColorf; - core::vector3df valVector3df; - core::position2di valPosition2di; - core::rect valRect; - core::matrix4 valMatrix; - core::quaternion valQuaternion; - core::aabbox3df valAabbox3df; - core::plane3df valPlane3df; - core::triangle3df valTriangle3df; - core::line2df valLine2df; - core::line3df valLine3df; - video::ITexture* valTexture; - void* valPointer; + s32 ValInt; + f32 ValFloat; + core::stringc ValString; + core::stringw ValStringW; + char ValBinary[BINARY_BLOCK_SIZE]; + core::array ValStringWArray; + bool ValBool; + EMockEnum ValEnum; + video::SColor ValColor; + video::SColorf ValColorf; + core::vector3df ValVector3df; + core::position2di ValPosition2di; + core::rect ValRect; + core::matrix4 ValMatrix; + core::quaternion ValQuaternion; + core::aabbox3df ValAabbox3df; + core::plane3df ValPlane3df; + core::triangle3df ValTriangle3df; + core::line2df ValLine2df; + core::line3df ValLine3df; + video::ITexture* ValTexture; + void* ValPointer; + + bool ComparePointers; }; -bool serializeAttributes() +// Serialization in memory +bool MemorySerialization(io::IFileSystem * fs ) { - IrrlichtDevice * device = irr::createDevice(video::EDT_NULL, dimension2d(1, 1)); - assert(device); - if(!device) - return false; - - io::IFileSystem * fs = device->getFileSystem (); - if ( !fs ) - return false; - SerializableMock origMock, copyMock; origMock.set(); copyMock.reset(); @@ -209,6 +207,113 @@ bool serializeAttributes() io::IAttributes* attr = fs->createEmptyAttributes(); origMock.serializeAttributes(attr, 0); copyMock.deserializeAttributes(attr, 0); + attr->drop(); return origMock == copyMock; } + +// Serialization to/from an xml-file +bool XmlSerialization(io::IFileSystem * fs, video::IVideoDriver * driver ) +{ + const io::path XML_FILENAME("media/attributeSerialization.xml"); + SerializableMock origMock(false), copyMock; + origMock.set(); + copyMock.reset(); + + // write to xml + io::IWriteFile* fileWrite = fs->createAndWriteFile(XML_FILENAME); + if (!fileWrite) + { + logTestString("Could not create xml in %s:%d\n", __FILE__, __LINE__ ); + return false; + } + io::IXMLWriter* writer = fs->createXMLWriter(fileWrite); + if (!writer) + { + logTestString("Could not create xml-writer in %s:%d\n", __FILE__, __LINE__ ); + return false; + } + writer->writeXMLHeader(); + writer->writeLineBreak(); + io::IAttributes* attrToXml = fs->createEmptyAttributes(); + origMock.serializeAttributes(attrToXml, 0); + attrToXml->write(writer); + attrToXml->drop(); + writer->writeLineBreak(); + writer->drop(); + fileWrite->drop(); + + // read from xml + io::IReadFile* fileRead = fs->createAndOpenFile(XML_FILENAME); + if (!fileRead) + { + logTestString("Could not open xml-file in %s:%d\n", __FILE__, __LINE__ ); + return false; + } + io::IXMLReader* reader = fs->createXMLReader(fileRead); + if (!reader) + { + logTestString("createXMLReader failed in %s:%d\n", __FILE__, __LINE__ ); + return false; + } + while(reader->read()) + { + switch (reader->getNodeType()) + { + case EXN_ELEMENT: + { + // read attributes + io::IAttributes* attr = fs->createEmptyAttributes(driver); + if ( attr->read(reader, true) ) + { + copyMock.deserializeAttributes(attr, 0); + } + else + { + logTestString("attr->read failed in %s:%d\n", __FILE__, __LINE__ ); + } + attr->drop(); + } + break; + default: + break; + } + } + reader->drop(); + fileRead->drop(); + + return origMock == copyMock; +} + +bool serializeAttributes() +{ + bool result = true; + + IrrlichtDevice * device = irr::createDevice(video::EDT_NULL, dimension2d(1, 1)); + assert(device); + if(!device) + { + logTestString("device creation failed in %s:%d\n", __FILE__, __LINE__ ); + return false; + } + + io::IFileSystem * fs = device->getFileSystem (); + if ( !fs ) + { + return false; + } + + result &= MemorySerialization(fs); + if ( !result ) + { + logTestString("MemorySerialization failed in %s:%d\n", __FILE__, __LINE__ ); + } + + result &= XmlSerialization(fs, device->getVideoDriver()); + if ( !result ) + { + logTestString("XmlSerialization failed in %s:%d\n", __FILE__, __LINE__ ); + } + + return result; +} diff --git a/tests/tests-last-passed-at.txt b/tests/tests-last-passed-at.txt index e6926097..98cff1ac 100644 --- a/tests/tests-last-passed-at.txt +++ b/tests/tests-last-passed-at.txt @@ -1,2 +1,2 @@ -Test suite pass at GMT Tue Dec 8 04:35:12 2009 +Test suite pass at GMT Wed Dec 9 01:28:55 2009