testXML.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright (C) 2009-2012 Christian Stehno
  2. // No rights reserved: this software is in the public domain.
  3. #include "testUtils.h"
  4. using namespace irr;
  5. using namespace core;
  6. bool simple_xml( irr::io::IFileSystem * fs )
  7. {
  8. io::IXMLReaderUTF8* reader = fs->createXMLReaderUTF8("media/test.xml");
  9. if (!reader)
  10. {
  11. logTestString("Could not create XML reader.\n");
  12. return false;
  13. }
  14. const core::stringc expected[] = {
  15. "a", "b", "c"
  16. };
  17. bool retVal = true;
  18. u32 i=0;
  19. while(reader->read())
  20. {
  21. if (reader->getNodeType() == io::EXN_ELEMENT)
  22. {
  23. if (expected[i++] != reader->getNodeName())
  24. {
  25. logTestString("Did not find expected string in XML element name.\n");
  26. retVal = false;
  27. break;
  28. }
  29. }
  30. }
  31. reader->drop();
  32. return retVal;
  33. }
  34. // CDATA should return everything between "![CDATA[" and "]]>" as it's in the file
  35. bool cdata( irr::io::IFileSystem * fs )
  36. {
  37. io::IXMLReaderUTF8* reader = fs->createXMLReaderUTF8("media/cdata.xml");
  38. if (!reader)
  39. {
  40. logTestString("Could not create XML reader.\n");
  41. return false;
  42. }
  43. const core::stringc textNode("text");
  44. core::array< core::stringc > compareStrings;
  45. compareStrings.push_back("simple");
  46. compareStrings.push_back("");
  47. compareStrings.push_back("] ]> ");
  48. compareStrings.push_back("]\n]> ");
  49. compareStrings.push_back("\nNewlines\n\tand tabs\n\t\tgogogo");
  50. compareStrings.push_back("&&#@#$%*()@#$%*()#$%*(");
  51. compareStrings.push_back("& & && &&& &a &ü &ä &ö &&#");
  52. bool result = true;
  53. size_t count = 0;
  54. while(reader->read())
  55. {
  56. if (reader->getNodeType() == io::EXN_ELEMENT)
  57. {
  58. if ( core::stringc(reader->getNodeName()) == textNode )
  59. {
  60. while(reader->read())
  61. {
  62. if (reader->getNodeType() == io::EXN_CDATA)
  63. {
  64. core::stringc data = reader->getNodeData();
  65. core::stringc name = reader->getNodeName();
  66. if ( count == compareStrings.size() )
  67. {
  68. logTestString("too many cdata elements for reading in %s:%d\n", __FILE__, __LINE__);
  69. }
  70. else if ( count < compareStrings.size() )
  71. {
  72. core::stringc cmpString(compareStrings[count]);
  73. // some (unused) variables to ease debugging
  74. // const c8* dataRaw = data.c_str();
  75. // const c8* cmpRaw = cmpString.c_str();
  76. if ( cmpString != data )
  77. {
  78. result = false;
  79. logTestString("cdata read failed for string %d in %s:%d\n", count, __FILE__, __LINE__);
  80. }
  81. }
  82. ++count;
  83. }
  84. if ( reader->getNodeType() == io::EXN_ELEMENT_END )
  85. {
  86. break;
  87. }
  88. }
  89. }
  90. }
  91. }
  92. reader->drop();
  93. return result;
  94. }
  95. bool attributeValues(irr::io::IFileSystem * fs)
  96. {
  97. io::IXMLReaderUTF8* reader = fs->createXMLReaderUTF8("media/attributes.xml");
  98. if (!reader)
  99. {
  100. logTestString("Could not create XML reader.\n");
  101. return false;
  102. }
  103. bool result = true;
  104. bool hasNode = false;
  105. while (reader->read())
  106. {
  107. if (io::EXN_ELEMENT == reader->getNodeType() )
  108. {
  109. if ( core::stringc(reader->getNodeName()) == core::stringc("element_position") )
  110. {
  111. hasNode = true;
  112. int id1 = reader->getAttributeValueAsInt("id1");
  113. if ( id1 != 152722522 )
  114. {
  115. logTestString("id1 is %d in %s:%d\n", id1, __FILE__, __LINE__);
  116. result = false;
  117. }
  118. int id2 = reader->getAttributeValueAsInt("id2");
  119. result &= id2 == 3;
  120. int x = reader->getAttributeValueAsInt("x");
  121. result &= x == 301;
  122. int y = reader->getAttributeValueAsInt("y");
  123. result &= y == 118;
  124. }
  125. }
  126. }
  127. if ( !hasNode )
  128. {
  129. logTestString("missing node in xml in %s:%d\n", __FILE__, __LINE__);
  130. return false;
  131. }
  132. reader->drop();
  133. return result;
  134. }
  135. /** Tests for XML handling */
  136. bool testXML(void)
  137. {
  138. IrrlichtDevice *device = createDevice(video::EDT_NULL, dimension2du(400, 200));
  139. bool result = true;
  140. logTestString("Test simple XML reader features.\n");
  141. result &= simple_xml(device->getFileSystem());
  142. logTestString("Test XML reader CDATA support.\n");
  143. result &= cdata(device->getFileSystem());
  144. logTestString("Test XML reader attribute support.\n");
  145. result &= attributeValues(device->getFileSystem());
  146. device->closeDevice();
  147. device->run();
  148. device->drop();
  149. return result;
  150. }