serializeAttributes.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. #include "testUtils.h"
  2. using namespace irr;
  3. using namespace core;
  4. using namespace io;
  5. #define COMPARE(a, b) if ( (a) != (b) ) { logTestString("Not identical %s in %s:%d\n", #a, __FILE__, __LINE__ ); return false; }
  6. const u32 BINARY_BLOCK_SIZE = 10;
  7. enum EMockEnum
  8. {
  9. EME_NONE,
  10. EME_ONE,
  11. EME_COUNT
  12. };
  13. const c8* const MockEnumNames[EME_COUNT+1] =
  14. {
  15. "none",
  16. "one",
  17. 0,
  18. };
  19. class SerializableMock : public virtual io::IAttributeExchangingObject
  20. {
  21. public:
  22. SerializableMock(bool comparePointers=true) : ComparePointers(comparePointers)
  23. {
  24. }
  25. virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const
  26. {
  27. out->addInt("ValInt", ValInt);
  28. out->addFloat("ValFloat", ValFloat);
  29. out->addString("ValString", ValString.c_str());
  30. out->addString("ValStringW", ValStringW.c_str());
  31. out->addBinary("ValBinary", (void*)ValBinary, BINARY_BLOCK_SIZE);
  32. out->addArray("ValStringWArray", ValStringWArray);
  33. out->addBool("ValBool", ValBool);
  34. out->addEnum("ValEnum", ValEnum, MockEnumNames);
  35. out->addColor("ValColor", ValColor);
  36. out->addColorf("ValColorf", ValColorf);
  37. out->addVector3d("ValVector3df", ValVector3df);
  38. out->addVector2d("ValVector2df", ValVector2df);
  39. out->addDimension2d("ValDimension2du", ValDimension2du);
  40. out->addPosition2d("ValPosition2di", ValPosition2di);
  41. out->addRect("ValRect", ValRect);
  42. out->addMatrix("ValMatrix", ValMatrix);
  43. out->addQuaternion("ValQuaternion", ValQuaternion);
  44. out->addBox3d("ValAabbox3df", ValAabbox3df);
  45. out->addPlane3d("ValPlane3df", ValPlane3df);
  46. out->addTriangle3d("ValTriangle3df", ValTriangle3df);
  47. out->addLine2d("ValLine2df", ValLine2df);
  48. out->addLine3d("ValLine3df", ValLine3df);
  49. out->addTexture("ValTexture", ValTexture );
  50. out->addUserPointer("ValPointer", ValPointer);
  51. }
  52. virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
  53. {
  54. ValInt = in->getAttributeAsInt("ValInt");
  55. ValFloat = in->getAttributeAsFloat("ValFloat");
  56. ValString = in->getAttributeAsString("ValString");
  57. ValStringW = in->getAttributeAsStringW("ValStringW");
  58. in->getAttributeAsBinaryData("ValBinary", ValBinary, BINARY_BLOCK_SIZE);
  59. ValStringWArray = in->getAttributeAsArray("ValStringWArray");
  60. ValBool = in->getAttributeAsBool("ValBool");
  61. ValEnum = (EMockEnum)in->getAttributeAsEnumeration("ValEnum", MockEnumNames);
  62. ValColor = in->getAttributeAsColor("ValColor");
  63. ValColorf = in->getAttributeAsColorf("ValColorf");
  64. ValVector3df = in->getAttributeAsVector3d("ValVector3df");
  65. ValVector2df = in->getAttributeAsVector2d("ValVector2df");
  66. ValDimension2du = in->getAttributeAsDimension2d("ValDimension2du");
  67. ValPosition2di = in->getAttributeAsPosition2d("ValPosition2di");
  68. ValRect = in->getAttributeAsRect("ValRect");
  69. ValMatrix = in->getAttributeAsMatrix("ValMatrix");
  70. ValQuaternion = in->getAttributeAsQuaternion("ValQuaternion");
  71. ValAabbox3df = in->getAttributeAsBox3d("ValAabbox3df");
  72. ValPlane3df = in->getAttributeAsPlane3d("ValPlane3df");
  73. ValTriangle3df = in->getAttributeAsTriangle3d("ValTriangle3df");
  74. ValLine2df = in->getAttributeAsLine2d("ValLine2df");
  75. ValLine3df = in->getAttributeAsLine3d("ValLine3df");
  76. ValTexture = in->getAttributeAsTexture("ValTexture");
  77. ValPointer = in->getAttributeAsUserPointer("ValPointer");
  78. }
  79. bool operator==(const SerializableMock& other)
  80. {
  81. COMPARE(ValInt, other.ValInt);
  82. COMPARE(ValFloat, other.ValFloat);
  83. COMPARE(ValString, other.ValString);
  84. COMPARE(ValStringW, other.ValStringW);
  85. if ( memcmp( ValBinary, other.ValBinary, BINARY_BLOCK_SIZE) != 0 )
  86. {
  87. logTestString("Not identical %s in %s:%d", "ValBinary", __FILE__, __LINE__ );
  88. return false;
  89. }
  90. COMPARE(ValStringWArray, other.ValStringWArray);
  91. COMPARE(ValBool, other.ValBool);
  92. COMPARE(ValEnum, other.ValEnum);
  93. COMPARE(ValColor, other.ValColor);
  94. if ( ValColorf.r != other.ValColorf.r || ValColorf.g != other.ValColorf.g || ValColorf.b != other.ValColorf.b || ValColorf.a != other.ValColorf.a )
  95. {
  96. logTestString("Not identical %s in %s:%d", "ValColorf", __FILE__, __LINE__ );
  97. return false;
  98. }
  99. COMPARE(ValVector3df, other.ValVector3df);
  100. COMPARE(ValVector2df, other.ValVector2df);
  101. COMPARE(ValDimension2du, other.ValDimension2du);
  102. COMPARE(ValPosition2di, other.ValPosition2di);
  103. COMPARE(ValRect, other.ValRect);
  104. COMPARE(ValMatrix, other.ValMatrix);
  105. COMPARE(ValQuaternion, other.ValQuaternion);
  106. COMPARE(ValAabbox3df, other.ValAabbox3df);
  107. COMPARE(ValPlane3df, other.ValPlane3df);
  108. COMPARE(ValTriangle3df, other.ValTriangle3df);
  109. COMPARE(ValLine2df, other.ValLine2df);
  110. COMPARE(ValLine3df, other.ValLine3df);
  111. // ValTexture; // TODO
  112. if ( ComparePointers )
  113. COMPARE(ValPointer, other.ValPointer);
  114. return true;
  115. }
  116. void reset()
  117. {
  118. ValInt = 0;
  119. ValFloat = 0.f;
  120. ValString = "";
  121. ValStringW = L"";
  122. memset(ValBinary, 0, BINARY_BLOCK_SIZE);
  123. ValStringWArray.clear();
  124. ValBool = false;
  125. ValEnum = EME_NONE;
  126. ValColor.set(0,0,0,0);
  127. ValColorf.set(0.f, 0.f, 0.f, 0.f);
  128. ValVector3df.set(0.f, 0.f, 0.f);
  129. ValVector2df.set(0.f, 0.f);
  130. ValDimension2du.set(0, 0);
  131. ValPosition2di.set(0,0);
  132. ValRect = core::rect<s32>(0,0,0,0);
  133. ValMatrix.makeIdentity();
  134. ValQuaternion.set(0,0,0,0);
  135. ValAabbox3df.reset(0,0,0);
  136. ValPlane3df.setPlane(vector3df(0.f,0.f,0.f), 0.f);
  137. ValTriangle3df.set( vector3df(0.f,0.f,0.f), vector3df(0.f,0.f,0.f), vector3df(0.f,0.f,0.f) );
  138. ValLine2df.setLine(0.f, 0.f, 0.f, 0.f);
  139. ValLine3df.setLine(0.f, 0.f, 0.f, 0.f, 0.f, 0.f);
  140. ValTexture = NULL;
  141. ValPointer = 0;
  142. }
  143. void set()
  144. {
  145. ValInt = 152722522;
  146. ValFloat = 1.f;
  147. ValString = "one";
  148. ValStringW = L"ONE";
  149. memset(ValBinary, 0xff, BINARY_BLOCK_SIZE);
  150. ValStringWArray.push_back( stringw("ONE") );
  151. ValStringWArray.push_back( stringw("TWO") );
  152. ValStringWArray.push_back( stringw("THREE") );
  153. ValBool = true;
  154. ValEnum = EME_ONE;
  155. ValColor.set(1,2,3,4);
  156. ValColorf.set(1.f, 2.f, 3.f, 4.f);
  157. ValVector3df.set(1.f, 2.f, 3.f);
  158. ValVector2df.set(1.f, 2.f);
  159. ValDimension2du.set(1, 2);
  160. ValPosition2di.set(1,2);
  161. ValRect = core::rect<s32>(1,2,3,4);
  162. ValMatrix = 99.9f;
  163. ValQuaternion.set(1,2,3,4);
  164. ValAabbox3df.reset(1,2,3);
  165. ValPlane3df.setPlane(vector3df(1.f,2.f,3.f), 4.f);
  166. ValTriangle3df.set( vector3df(1.f,2.f,3.f), vector3df(4.f,5.f,6.f), vector3df(7.f,8.f,9.f) );
  167. ValLine2df.setLine(1.f, 2.f, 3.f, 4.f);
  168. ValLine3df.setLine(1.f, 2.f, 3.f, 4.f, 5.f, 6.f);
  169. ValTexture = NULL; // TODO
  170. ValPointer = (void*)0xffffff;
  171. }
  172. s32 ValInt;
  173. f32 ValFloat;
  174. core::stringc ValString;
  175. core::stringw ValStringW;
  176. char ValBinary[BINARY_BLOCK_SIZE];
  177. core::array<core::stringw> ValStringWArray;
  178. bool ValBool;
  179. EMockEnum ValEnum;
  180. video::SColor ValColor;
  181. video::SColorf ValColorf;
  182. core::vector3df ValVector3df;
  183. core::vector2df ValVector2df;
  184. core::dimension2du ValDimension2du;
  185. core::position2di ValPosition2di;
  186. core::rect<s32> ValRect;
  187. core::matrix4 ValMatrix;
  188. core::quaternion ValQuaternion;
  189. core::aabbox3df ValAabbox3df;
  190. core::plane3df ValPlane3df;
  191. core::triangle3df ValTriangle3df;
  192. core::line2df ValLine2df;
  193. core::line3df ValLine3df;
  194. video::ITexture* ValTexture;
  195. void* ValPointer;
  196. bool ComparePointers;
  197. };
  198. // Serialization in memory
  199. bool MemorySerialization(io::IFileSystem * fs )
  200. {
  201. SerializableMock origMock, copyMock;
  202. origMock.set();
  203. copyMock.reset();
  204. io::IAttributes* attr = fs->createEmptyAttributes();
  205. origMock.serializeAttributes(attr, 0);
  206. copyMock.deserializeAttributes(attr, 0);
  207. attr->drop();
  208. return origMock == copyMock;
  209. }
  210. // Serialization to/from an xml-file
  211. bool XmlSerialization(io::IFileSystem * fs, video::IVideoDriver * driver )
  212. {
  213. const io::path XML_FILENAME("results/attributeSerialization.xml");
  214. SerializableMock origMock(false), copyMock;
  215. origMock.set();
  216. copyMock.reset();
  217. // write to xml
  218. io::IWriteFile* fileWrite = fs->createAndWriteFile(XML_FILENAME);
  219. if (!fileWrite)
  220. {
  221. logTestString("Could not create xml in %s:%d\n", __FILE__, __LINE__ );
  222. return false;
  223. }
  224. io::IXMLWriter* writer = fs->createXMLWriter(fileWrite);
  225. if (!writer)
  226. {
  227. logTestString("Could not create xml-writer in %s:%d\n", __FILE__, __LINE__ );
  228. return false;
  229. }
  230. writer->writeXMLHeader();
  231. writer->writeLineBreak();
  232. io::IAttributes* attrToXml = fs->createEmptyAttributes();
  233. origMock.serializeAttributes(attrToXml, 0);
  234. attrToXml->write(writer);
  235. attrToXml->drop();
  236. writer->writeLineBreak();
  237. writer->drop();
  238. fileWrite->drop();
  239. // read from xml
  240. io::IReadFile* fileRead = fs->createAndOpenFile(XML_FILENAME);
  241. if (!fileRead)
  242. {
  243. logTestString("Could not open xml-file in %s:%d\n", __FILE__, __LINE__ );
  244. return false;
  245. }
  246. io::IXMLReader* reader = fs->createXMLReader(fileRead);
  247. if (!reader)
  248. {
  249. logTestString("createXMLReader failed in %s:%d\n", __FILE__, __LINE__ );
  250. return false;
  251. }
  252. while(reader->read())
  253. {
  254. switch (reader->getNodeType())
  255. {
  256. case EXN_ELEMENT:
  257. {
  258. // read attributes
  259. io::IAttributes* attr = fs->createEmptyAttributes(driver);
  260. if ( attr->read(reader, true) )
  261. {
  262. copyMock.deserializeAttributes(attr, 0);
  263. }
  264. else
  265. {
  266. logTestString("attr->read failed in %s:%d\n", __FILE__, __LINE__ );
  267. }
  268. attr->drop();
  269. }
  270. break;
  271. default:
  272. break;
  273. }
  274. }
  275. reader->drop();
  276. fileRead->drop();
  277. return origMock == copyMock;
  278. }
  279. // All attributes can also be read/written in string format
  280. bool stringSerialization(io::IFileSystem * fs)
  281. {
  282. SerializableMock mock;
  283. mock.set();
  284. io::IAttributes* attr = fs->createEmptyAttributes();
  285. mock.serializeAttributes(attr, 0);
  286. for ( s32 i=0; i< (s32)attr->getAttributeCount(); ++i )
  287. {
  288. core::stringw value(attr->getAttributeAsString(i));
  289. attr->setAttribute(i, value.c_str() );
  290. core::stringw value2(attr->getAttributeAsString(i));
  291. if ( value != value2 )
  292. {
  293. logTestString("old-string: %s new-string: %s for %d.%s in %s:%d\n"
  294. , core::stringc(value).c_str(), core::stringc(value2).c_str(), i, attr->getAttributeName(i), __FILE__, __LINE__ );
  295. return false;
  296. }
  297. else
  298. {
  299. // TODO: We can't catch yet if getAttributeAsString and setAttribute both change nothing
  300. // Except if string returned is empty - which would be fine in some cases (0 pointers or if string was empty originally)
  301. // But right now at least stringw arrays don't do stringSerialization which is a bug
  302. //if ( value.empty() )
  303. //return false;
  304. }
  305. }
  306. attr->drop();
  307. return true;
  308. }
  309. bool serializeAttributes()
  310. {
  311. bool result = true;
  312. IrrlichtDevice * device = irr::createDevice(video::EDT_NULL, dimension2d<u32>(1, 1));
  313. assert(device);
  314. if(!device)
  315. {
  316. logTestString("device creation failed in %s:%d\n", __FILE__, __LINE__ );
  317. return false;
  318. }
  319. io::IFileSystem * fs = device->getFileSystem ();
  320. if ( !fs )
  321. {
  322. return false;
  323. }
  324. result &= MemorySerialization(fs);
  325. if ( !result )
  326. {
  327. logTestString("MemorySerialization failed in %s:%d\n", __FILE__, __LINE__ );
  328. }
  329. result &= XmlSerialization(fs, device->getVideoDriver());
  330. if ( !result )
  331. {
  332. logTestString("XmlSerialization failed in %s:%d\n", __FILE__, __LINE__ );
  333. }
  334. result &= stringSerialization(fs);
  335. if ( !result )
  336. {
  337. logTestString("stringSerialization failed in %s:%d\n", __FILE__, __LINE__ );
  338. }
  339. device->closeDevice();
  340. device->run();
  341. device->drop();
  342. return result;
  343. }