xmltest.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  1. /*
  2. Test program for TinyXML.
  3. */
  4. #ifdef TIXML_USE_STL
  5. #include <iostream>
  6. #include <sstream>
  7. using namespace std;
  8. #else
  9. #include <stdio.h>
  10. #endif
  11. #if defined( WIN32 ) && defined( TUNE )
  12. #include <crtdbg.h>
  13. _CrtMemState startMemState;
  14. _CrtMemState endMemState;
  15. #endif
  16. #include "tinyxml.h"
  17. static int gPass = 0;
  18. static int gFail = 0;
  19. using namespace Polycode;
  20. bool XmlTest (const char* testString, const char* expected, const char* found, bool noEcho = false)
  21. {
  22. bool pass = !strcmp( expected, found );
  23. if ( pass )
  24. Logger::log ("[pass]");
  25. else
  26. Logger::log ("[fail]");
  27. if ( noEcho )
  28. Logger::log (" %s\n", testString);
  29. else
  30. Logger::log (" %s [%s][%s]\n", testString, expected, found);
  31. if ( pass )
  32. ++gPass;
  33. else
  34. ++gFail;
  35. return pass;
  36. }
  37. bool XmlTest( const char* testString, int expected, int found, bool noEcho = false )
  38. {
  39. bool pass = ( expected == found );
  40. if ( pass )
  41. Logger::log ("[pass]");
  42. else
  43. Logger::log ("[fail]");
  44. if ( noEcho )
  45. Logger::log (" %s\n", testString);
  46. else
  47. Logger::log (" %s [%d][%d]\n", testString, expected, found);
  48. if ( pass )
  49. ++gPass;
  50. else
  51. ++gFail;
  52. return pass;
  53. }
  54. //
  55. // This file demonstrates some basic functionality of TinyXml.
  56. // Note that the example is very contrived. It presumes you know
  57. // what is in the XML file. But it does test the basic operations,
  58. // and show how to add and remove nodes.
  59. //
  60. int main()
  61. {
  62. //
  63. // We start with the 'demoStart' todo list. Process it. And
  64. // should hopefully end up with the todo list as illustrated.
  65. //
  66. const char* demoStart =
  67. "<?xml version=\"1.0\" standalone='no' >\n"
  68. "<!-- Our to do list data -->"
  69. "<ToDo>\n"
  70. "<!-- Do I need a secure PDA? -->\n"
  71. "<Item priority=\"1\" distance='close'> Go to the <bold>Toy store!</bold></Item>"
  72. "<Item priority=\"2\" distance='none'> Do bills </Item>"
  73. "<Item priority=\"2\" distance='far &amp; back'> Look for Evil Dinosaurs! </Item>"
  74. "</ToDo>";
  75. {
  76. #ifdef TIXML_USE_STL
  77. /* What the todo list should look like after processing.
  78. In stream (no formatting) representation. */
  79. const char* demoEnd =
  80. "<?xml version=\"1.0\" standalone=\"no\" ?>"
  81. "<!-- Our to do list data -->"
  82. "<ToDo>"
  83. "<!-- Do I need a secure PDA? -->"
  84. "<Item priority=\"2\" distance=\"close\">Go to the"
  85. "<bold>Toy store!"
  86. "</bold>"
  87. "</Item>"
  88. "<Item priority=\"1\" distance=\"far\">Talk to:"
  89. "<Meeting where=\"School\">"
  90. "<Attendee name=\"Marple\" position=\"teacher\" />"
  91. "<Attendee name=\"Voel\" position=\"counselor\" />"
  92. "</Meeting>"
  93. "<Meeting where=\"Lunch\" />"
  94. "</Item>"
  95. "<Item priority=\"2\" distance=\"here\">Do bills"
  96. "</Item>"
  97. "</ToDo>";
  98. #endif
  99. // The example parses from the character string (above):
  100. #if defined( WIN32 ) && defined( TUNE )
  101. _CrtMemCheckpoint( &startMemState );
  102. #endif
  103. {
  104. // Write to a file and read it back, to check file I/O.
  105. TiXmlDocument doc( "demotest.xml" );
  106. doc.Parse( demoStart );
  107. if ( doc.Error() )
  108. {
  109. Logger::log( "Error in %s: %s\n", doc.Value(), doc.ErrorDesc() );
  110. exit( 1 );
  111. }
  112. doc.SaveFile();
  113. }
  114. TiXmlDocument doc( "demotest.xml" );
  115. bool loadOkay = doc.LoadFile();
  116. if ( !loadOkay )
  117. {
  118. Logger::log( "Could not load test file 'demotest.xml'. Error='%s'. Exiting.\n", doc.ErrorDesc() );
  119. exit( 1 );
  120. }
  121. Logger::log( "** Demo doc read from disk: ** \n\n" );
  122. Logger::log( "** Printing via doc.Print **\n" );
  123. doc.Print( stdout );
  124. {
  125. Logger::log( "** Printing via TiXmlPrinter **\n" );
  126. TiXmlPrinter printer;
  127. doc.Accept( &printer );
  128. // fLogger::log( stdout, "%s", printer.CStr() );
  129. }
  130. #ifdef TIXML_USE_STL
  131. {
  132. Logger::log( "** Printing via operator<< **\n" );
  133. std::cout << doc;
  134. }
  135. #endif
  136. TiXmlNode* node = 0;
  137. TiXmlElement* todoElement = 0;
  138. TiXmlElement* itemElement = 0;
  139. // --------------------------------------------------------
  140. // An example of changing existing attributes, and removing
  141. // an element from the document.
  142. // --------------------------------------------------------
  143. // Get the "ToDo" element.
  144. // It is a child of the document, and can be selected by name.
  145. node = doc.FirstChild( "ToDo" );
  146. assert( node );
  147. todoElement = node->ToElement();
  148. assert( todoElement );
  149. // Going to the toy store is now our second priority...
  150. // So set the "priority" attribute of the first item in the list.
  151. node = todoElement->FirstChildElement(); // This skips the "PDA" comment.
  152. assert( node );
  153. itemElement = node->ToElement();
  154. assert( itemElement );
  155. itemElement->SetAttribute( "priority", 2 );
  156. // Change the distance to "doing bills" from
  157. // "none" to "here". It's the next sibling element.
  158. itemElement = itemElement->NextSiblingElement();
  159. assert( itemElement );
  160. itemElement->SetAttribute( "distance", "here" );
  161. // Remove the "Look for Evil Dinosaurs!" item.
  162. // It is 1 more sibling away. We ask the parent to remove
  163. // a particular child.
  164. itemElement = itemElement->NextSiblingElement();
  165. todoElement->RemoveChild( itemElement );
  166. itemElement = 0;
  167. // --------------------------------------------------------
  168. // What follows is an example of created elements and text
  169. // nodes and adding them to the document.
  170. // --------------------------------------------------------
  171. // Add some meetings.
  172. TiXmlElement item( "Item" );
  173. item.SetAttribute( "priority", "1" );
  174. item.SetAttribute( "distance", "far" );
  175. TiXmlText text( "Talk to:" );
  176. TiXmlElement meeting1( "Meeting" );
  177. meeting1.SetAttribute( "where", "School" );
  178. TiXmlElement meeting2( "Meeting" );
  179. meeting2.SetAttribute( "where", "Lunch" );
  180. TiXmlElement attendee1( "Attendee" );
  181. attendee1.SetAttribute( "name", "Marple" );
  182. attendee1.SetAttribute( "position", "teacher" );
  183. TiXmlElement attendee2( "Attendee" );
  184. attendee2.SetAttribute( "name", "Voel" );
  185. attendee2.SetAttribute( "position", "counselor" );
  186. // Assemble the nodes we've created:
  187. meeting1.InsertEndChild( attendee1 );
  188. meeting1.InsertEndChild( attendee2 );
  189. item.InsertEndChild( text );
  190. item.InsertEndChild( meeting1 );
  191. item.InsertEndChild( meeting2 );
  192. // And add the node to the existing list after the first child.
  193. node = todoElement->FirstChild( "Item" );
  194. assert( node );
  195. itemElement = node->ToElement();
  196. assert( itemElement );
  197. todoElement->InsertAfterChild( itemElement, item );
  198. Logger::log( "\n** Demo doc processed: ** \n\n" );
  199. doc.Print( stdout );
  200. #ifdef TIXML_USE_STL
  201. Logger::log( "** Demo doc processed to stream: ** \n\n" );
  202. cout << doc << endl << endl;
  203. #endif
  204. // --------------------------------------------------------
  205. // Different tests...do we have what we expect?
  206. // --------------------------------------------------------
  207. int count = 0;
  208. TiXmlElement* element;
  209. //////////////////////////////////////////////////////
  210. #ifdef TIXML_USE_STL
  211. cout << "** Basic structure. **\n";
  212. ostringstream outputStream( ostringstream::out );
  213. outputStream << doc;
  214. XmlTest( "Output stream correct.", string( demoEnd ).c_str(),
  215. outputStream.str().c_str(), true );
  216. #endif
  217. node = doc.RootElement();
  218. assert( node );
  219. XmlTest( "Root element exists.", true, ( node != 0 && node->ToElement() ) );
  220. XmlTest ( "Root element value is 'ToDo'.", "ToDo", node->Value());
  221. node = node->FirstChild();
  222. XmlTest( "First child exists & is a comment.", true, ( node != 0 && node->ToComment() ) );
  223. node = node->NextSibling();
  224. XmlTest( "Sibling element exists & is an element.", true, ( node != 0 && node->ToElement() ) );
  225. XmlTest ( "Value is 'Item'.", "Item", node->Value() );
  226. node = node->FirstChild();
  227. XmlTest ( "First child exists.", true, ( node != 0 && node->ToText() ) );
  228. XmlTest ( "Value is 'Go to the'.", "Go to the", node->Value() );
  229. //////////////////////////////////////////////////////
  230. Logger::log ("\n** Iterators. **\n");
  231. // Walk all the top level nodes of the document.
  232. count = 0;
  233. for( node = doc.FirstChild();
  234. node;
  235. node = node->NextSibling() )
  236. {
  237. count++;
  238. }
  239. XmlTest( "Top level nodes, using First / Next.", 3, count );
  240. count = 0;
  241. for( node = doc.LastChild();
  242. node;
  243. node = node->PreviousSibling() )
  244. {
  245. count++;
  246. }
  247. XmlTest( "Top level nodes, using Last / Previous.", 3, count );
  248. // Walk all the top level nodes of the document,
  249. // using a different syntax.
  250. count = 0;
  251. for( node = doc.IterateChildren( 0 );
  252. node;
  253. node = doc.IterateChildren( node ) )
  254. {
  255. count++;
  256. }
  257. XmlTest( "Top level nodes, using IterateChildren.", 3, count );
  258. // Walk all the elements in a node.
  259. count = 0;
  260. for( element = todoElement->FirstChildElement();
  261. element;
  262. element = element->NextSiblingElement() )
  263. {
  264. count++;
  265. }
  266. XmlTest( "Children of the 'ToDo' element, using First / Next.",
  267. 3, count );
  268. // Walk all the elements in a node by value.
  269. count = 0;
  270. for( node = todoElement->FirstChild( "Item" );
  271. node;
  272. node = node->NextSibling( "Item" ) )
  273. {
  274. count++;
  275. }
  276. XmlTest( "'Item' children of the 'ToDo' element, using First/Next.", 3, count );
  277. count = 0;
  278. for( node = todoElement->LastChild( "Item" );
  279. node;
  280. node = node->PreviousSibling( "Item" ) )
  281. {
  282. count++;
  283. }
  284. XmlTest( "'Item' children of the 'ToDo' element, using Last/Previous.", 3, count );
  285. #ifdef TIXML_USE_STL
  286. {
  287. cout << "\n** Parsing. **\n";
  288. istringstream parse0( "<Element0 attribute0='foo0' attribute1= noquotes attribute2 = '&gt;' />" );
  289. TiXmlElement element0( "default" );
  290. parse0 >> element0;
  291. XmlTest ( "Element parsed, value is 'Element0'.", "Element0", element0.Value() );
  292. XmlTest ( "Reads attribute 'attribute0=\"foo0\"'.", "foo0", element0.Attribute( "attribute0" ));
  293. XmlTest ( "Reads incorrectly formatted 'attribute1=noquotes'.", "noquotes", element0.Attribute( "attribute1" ) );
  294. XmlTest ( "Read attribute with entity value '>'.", ">", element0.Attribute( "attribute2" ) );
  295. }
  296. #endif
  297. {
  298. const char* error = "<?xml version=\"1.0\" standalone=\"no\" ?>\n"
  299. "<passages count=\"006\" formatversion=\"20020620\">\n"
  300. " <wrong error>\n"
  301. "</passages>";
  302. TiXmlDocument docTest;
  303. docTest.Parse( error );
  304. XmlTest( "Error row", docTest.ErrorRow(), 3 );
  305. XmlTest( "Error column", docTest.ErrorCol(), 17 );
  306. //Logger::log( "error=%d id='%s' row %d col%d\n", (int) doc.Error(), doc.ErrorDesc(), doc.ErrorRow()+1, doc.ErrorCol() + 1 );
  307. }
  308. #ifdef TIXML_USE_STL
  309. {
  310. //////////////////////////////////////////////////////
  311. cout << "\n** Streaming. **\n";
  312. // Round trip check: stream in, then stream back out to verify. The stream
  313. // out has already been checked, above. We use the output
  314. istringstream inputStringStream( outputStream.str() );
  315. TiXmlDocument document0;
  316. inputStringStream >> document0;
  317. ostringstream outputStream0( ostringstream::out );
  318. outputStream0 << document0;
  319. XmlTest( "Stream round trip correct.", string( demoEnd ).c_str(),
  320. outputStream0.str().c_str(), true );
  321. std::string str;
  322. str << document0;
  323. XmlTest( "String printing correct.", string( demoEnd ).c_str(),
  324. str.c_str(), true );
  325. }
  326. #endif
  327. }
  328. {
  329. const char* str = "<doc attr0='1' attr1='2.0' attr2='foo' />";
  330. TiXmlDocument doc;
  331. doc.Parse( str );
  332. TiXmlElement* ele = doc.FirstChildElement();
  333. int iVal, result;
  334. double dVal;
  335. result = ele->QueryDoubleAttribute( "attr0", &dVal );
  336. XmlTest( "Query attribute: int as double", result, TIXML_SUCCESS );
  337. XmlTest( "Query attribute: int as double", (int)dVal, 1 );
  338. result = ele->QueryDoubleAttribute( "attr1", &dVal );
  339. XmlTest( "Query attribute: double as double", (int)dVal, 2 );
  340. result = ele->QueryIntAttribute( "attr1", &iVal );
  341. XmlTest( "Query attribute: double as int", result, TIXML_SUCCESS );
  342. XmlTest( "Query attribute: double as int", iVal, 2 );
  343. result = ele->QueryIntAttribute( "attr2", &iVal );
  344. XmlTest( "Query attribute: not a number", result, TIXML_WRONG_TYPE );
  345. result = ele->QueryIntAttribute( "bar", &iVal );
  346. XmlTest( "Query attribute: does not exist", result, TIXML_NO_ATTRIBUTE );
  347. }
  348. {
  349. const char* str = "\t<?xml version=\"1.0\" standalone=\"no\" ?>\t<room doors='2'>\n"
  350. "</room>";
  351. TiXmlDocument doc;
  352. doc.SetTabSize( 8 );
  353. doc.Parse( str );
  354. TiXmlHandle docHandle( &doc );
  355. TiXmlHandle roomHandle = docHandle.FirstChildElement( "room" );
  356. assert( docHandle.Node() );
  357. assert( roomHandle.Element() );
  358. TiXmlElement* room = roomHandle.Element();
  359. assert( room );
  360. TiXmlAttribute* doors = room->FirstAttribute();
  361. assert( doors );
  362. XmlTest( "Location tracking: Tab 8: room row", room->Row(), 1 );
  363. XmlTest( "Location tracking: Tab 8: room col", room->Column(), 49 );
  364. XmlTest( "Location tracking: Tab 8: doors row", doors->Row(), 1 );
  365. XmlTest( "Location tracking: Tab 8: doors col", doors->Column(), 55 );
  366. }
  367. {
  368. const char* str = "\t<?xml version=\"1.0\" standalone=\"no\" ?>\t<room doors='2'>\n"
  369. " <!-- Silly example -->\n"
  370. " <door wall='north'>A great door!</door>\n"
  371. "\t<door wall='east'/>"
  372. "</room>";
  373. TiXmlDocument doc;
  374. doc.Parse( str );
  375. TiXmlHandle docHandle( &doc );
  376. TiXmlHandle roomHandle = docHandle.FirstChildElement( "room" );
  377. TiXmlHandle commentHandle = docHandle.FirstChildElement( "room" ).FirstChild();
  378. TiXmlHandle textHandle = docHandle.FirstChildElement( "room" ).ChildElement( "door", 0 ).FirstChild();
  379. TiXmlHandle door0Handle = docHandle.FirstChildElement( "room" ).ChildElement( 0 );
  380. TiXmlHandle door1Handle = docHandle.FirstChildElement( "room" ).ChildElement( 1 );
  381. assert( docHandle.Node() );
  382. assert( roomHandle.Element() );
  383. assert( commentHandle.Node() );
  384. assert( textHandle.Text() );
  385. assert( door0Handle.Element() );
  386. assert( door1Handle.Element() );
  387. TiXmlDeclaration* declaration = doc.FirstChild()->ToDeclaration();
  388. assert( declaration );
  389. TiXmlElement* room = roomHandle.Element();
  390. assert( room );
  391. TiXmlAttribute* doors = room->FirstAttribute();
  392. assert( doors );
  393. TiXmlText* text = textHandle.Text();
  394. TiXmlComment* comment = commentHandle.Node()->ToComment();
  395. assert( comment );
  396. TiXmlElement* door0 = door0Handle.Element();
  397. TiXmlElement* door1 = door1Handle.Element();
  398. XmlTest( "Location tracking: Declaration row", declaration->Row(), 1 );
  399. XmlTest( "Location tracking: Declaration col", declaration->Column(), 5 );
  400. XmlTest( "Location tracking: room row", room->Row(), 1 );
  401. XmlTest( "Location tracking: room col", room->Column(), 45 );
  402. XmlTest( "Location tracking: doors row", doors->Row(), 1 );
  403. XmlTest( "Location tracking: doors col", doors->Column(), 51 );
  404. XmlTest( "Location tracking: Comment row", comment->Row(), 2 );
  405. XmlTest( "Location tracking: Comment col", comment->Column(), 3 );
  406. XmlTest( "Location tracking: text row", text->Row(), 3 );
  407. XmlTest( "Location tracking: text col", text->Column(), 24 );
  408. XmlTest( "Location tracking: door0 row", door0->Row(), 3 );
  409. XmlTest( "Location tracking: door0 col", door0->Column(), 5 );
  410. XmlTest( "Location tracking: door1 row", door1->Row(), 4 );
  411. XmlTest( "Location tracking: door1 col", door1->Column(), 5 );
  412. }
  413. // --------------------------------------------------------
  414. // UTF-8 testing. It is important to test:
  415. // 1. Making sure name, value, and text read correctly
  416. // 2. Row, Col functionality
  417. // 3. Correct output
  418. // --------------------------------------------------------
  419. Logger::log ("\n** UTF-8 **\n");
  420. {
  421. TiXmlDocument doc( "utf8test.xml" );
  422. doc.LoadFile();
  423. if ( doc.Error() && doc.ErrorId() == TiXmlBase::TIXML_ERROR_OPENING_FILE ) {
  424. Logger::log( "WARNING: File 'utf8test.xml' not found.\n"
  425. "(Are you running the test from the wrong directory?)\n"
  426. "Could not test UTF-8 functionality.\n" );
  427. }
  428. else
  429. {
  430. TiXmlHandle docH( &doc );
  431. // Get the attribute "value" from the "Russian" element and check it.
  432. TiXmlElement* element = docH.FirstChildElement( "document" ).FirstChildElement( "Russian" ).Element();
  433. const unsigned char correctValue[] = { 0xd1U, 0x86U, 0xd0U, 0xb5U, 0xd0U, 0xbdU, 0xd0U, 0xbdU,
  434. 0xd0U, 0xbeU, 0xd1U, 0x81U, 0xd1U, 0x82U, 0xd1U, 0x8cU, 0 };
  435. XmlTest( "UTF-8: Russian value.", (const char*)correctValue, element->Attribute( "value" ), true );
  436. XmlTest( "UTF-8: Russian value row.", 4, element->Row() );
  437. XmlTest( "UTF-8: Russian value column.", 5, element->Column() );
  438. const unsigned char russianElementName[] = { 0xd0U, 0xa0U, 0xd1U, 0x83U,
  439. 0xd1U, 0x81U, 0xd1U, 0x81U,
  440. 0xd0U, 0xbaU, 0xd0U, 0xb8U,
  441. 0xd0U, 0xb9U, 0 };
  442. const char russianText[] = "<\xD0\xB8\xD0\xBC\xD0\xB5\xD0\xB5\xD1\x82>";
  443. TiXmlText* text = docH.FirstChildElement( "document" ).FirstChildElement( (const char*) russianElementName ).Child( 0 ).Text();
  444. XmlTest( "UTF-8: Browsing russian element name.",
  445. russianText,
  446. text->Value(),
  447. true );
  448. XmlTest( "UTF-8: Russian element name row.", 7, text->Row() );
  449. XmlTest( "UTF-8: Russian element name column.", 47, text->Column() );
  450. TiXmlDeclaration* dec = docH.Child( 0 ).Node()->ToDeclaration();
  451. XmlTest( "UTF-8: Declaration column.", 1, dec->Column() );
  452. XmlTest( "UTF-8: Document column.", 1, doc.Column() );
  453. // Now try for a round trip.
  454. doc.SaveFile( "utf8testout.xml" );
  455. // Check the round trip.
  456. char savedBuf[256];
  457. char verifyBuf[256];
  458. int okay = 1;
  459. FILE* saved = fopen( "utf8testout.xml", "r" );
  460. FILE* verify = fopen( "utf8testverify.xml", "r" );
  461. if ( saved && verify )
  462. {
  463. while ( fgets( verifyBuf, 256, verify ) )
  464. {
  465. fgets( savedBuf, 256, saved );
  466. if ( strcmp( verifyBuf, savedBuf ) )
  467. {
  468. okay = 0;
  469. break;
  470. }
  471. }
  472. fclose( saved );
  473. fclose( verify );
  474. }
  475. XmlTest( "UTF-8: Verified multi-language round trip.", 1, okay );
  476. // On most Western machines, this is an element that contains
  477. // the word "resume" with the correct accents, in a latin encoding.
  478. // It will be something else completely on non-wester machines,
  479. // which is why TinyXml is switching to UTF-8.
  480. const char latin[] = "<element>r\x82sum\x82</element>";
  481. TiXmlDocument latinDoc;
  482. latinDoc.Parse( latin, 0, TIXML_ENCODING_LEGACY );
  483. text = latinDoc.FirstChildElement()->FirstChild()->ToText();
  484. XmlTest( "Legacy encoding: Verify text element.", "r\x82sum\x82", text->Value() );
  485. }
  486. }
  487. //////////////////////
  488. // Copy and assignment
  489. //////////////////////
  490. Logger::log ("\n** Copy and Assignment **\n");
  491. {
  492. TiXmlElement element( "foo" );
  493. element.Parse( "<element name='value' />", 0, TIXML_ENCODING_UNKNOWN );
  494. TiXmlElement elementCopy( element );
  495. TiXmlElement elementAssign( "foo" );
  496. elementAssign.Parse( "<incorrect foo='bar'/>", 0, TIXML_ENCODING_UNKNOWN );
  497. elementAssign = element;
  498. XmlTest( "Copy/Assign: element copy #1.", "element", elementCopy.Value() );
  499. XmlTest( "Copy/Assign: element copy #2.", "value", elementCopy.Attribute( "name" ) );
  500. XmlTest( "Copy/Assign: element assign #1.", "element", elementAssign.Value() );
  501. XmlTest( "Copy/Assign: element assign #2.", "value", elementAssign.Attribute( "name" ) );
  502. XmlTest( "Copy/Assign: element assign #3.", true, ( 0 == elementAssign.Attribute( "foo" )) );
  503. TiXmlComment comment;
  504. comment.Parse( "<!--comment-->", 0, TIXML_ENCODING_UNKNOWN );
  505. TiXmlComment commentCopy( comment );
  506. TiXmlComment commentAssign;
  507. commentAssign = commentCopy;
  508. XmlTest( "Copy/Assign: comment copy.", "comment", commentCopy.Value() );
  509. XmlTest( "Copy/Assign: comment assign.", "comment", commentAssign.Value() );
  510. TiXmlUnknown unknown;
  511. unknown.Parse( "<[unknown]>", 0, TIXML_ENCODING_UNKNOWN );
  512. TiXmlUnknown unknownCopy( unknown );
  513. TiXmlUnknown unknownAssign;
  514. unknownAssign.Parse( "incorrect", 0, TIXML_ENCODING_UNKNOWN );
  515. unknownAssign = unknownCopy;
  516. XmlTest( "Copy/Assign: unknown copy.", "[unknown]", unknownCopy.Value() );
  517. XmlTest( "Copy/Assign: unknown assign.", "[unknown]", unknownAssign.Value() );
  518. TiXmlText text( "TextNode" );
  519. TiXmlText textCopy( text );
  520. TiXmlText textAssign( "incorrect" );
  521. textAssign = text;
  522. XmlTest( "Copy/Assign: text copy.", "TextNode", textCopy.Value() );
  523. XmlTest( "Copy/Assign: text assign.", "TextNode", textAssign.Value() );
  524. TiXmlDeclaration dec;
  525. dec.Parse( "<?xml version='1.0' encoding='UTF-8'?>", 0, TIXML_ENCODING_UNKNOWN );
  526. TiXmlDeclaration decCopy( dec );
  527. TiXmlDeclaration decAssign;
  528. decAssign = dec;
  529. XmlTest( "Copy/Assign: declaration copy.", "UTF-8", decCopy.Encoding() );
  530. XmlTest( "Copy/Assign: text assign.", "UTF-8", decAssign.Encoding() );
  531. TiXmlDocument doc;
  532. elementCopy.InsertEndChild( textCopy );
  533. doc.InsertEndChild( decAssign );
  534. doc.InsertEndChild( elementCopy );
  535. doc.InsertEndChild( unknownAssign );
  536. TiXmlDocument docCopy( doc );
  537. TiXmlDocument docAssign;
  538. docAssign = docCopy;
  539. #ifdef TIXML_USE_STL
  540. std::string original, copy, assign;
  541. original << doc;
  542. copy << docCopy;
  543. assign << docAssign;
  544. XmlTest( "Copy/Assign: document copy.", original.c_str(), copy.c_str(), true );
  545. XmlTest( "Copy/Assign: document assign.", original.c_str(), assign.c_str(), true );
  546. #endif
  547. }
  548. //////////////////////////////////////////////////////
  549. #ifdef TIXML_USE_STL
  550. Logger::log ("\n** Parsing, no Condense Whitespace **\n");
  551. TiXmlBase::SetCondenseWhiteSpace( false );
  552. {
  553. istringstream parse1( "<start>This is \ntext</start>" );
  554. TiXmlElement text1( "text" );
  555. parse1 >> text1;
  556. XmlTest ( "Condense white space OFF.", "This is \ntext",
  557. text1.FirstChild()->Value(),
  558. true );
  559. }
  560. TiXmlBase::SetCondenseWhiteSpace( true );
  561. #endif
  562. //////////////////////////////////////////////////////
  563. // GetText();
  564. {
  565. const char* str = "<foo>This is text</foo>";
  566. TiXmlDocument doc;
  567. doc.Parse( str );
  568. const TiXmlElement* element = doc.RootElement();
  569. XmlTest( "GetText() normal use.", "This is text", element->GetText() );
  570. str = "<foo><b>This is text</b></foo>";
  571. doc.Clear();
  572. doc.Parse( str );
  573. element = doc.RootElement();
  574. XmlTest( "GetText() contained element.", element->GetText() == 0, true );
  575. str = "<foo>This is <b>text</b></foo>";
  576. doc.Clear();
  577. TiXmlBase::SetCondenseWhiteSpace( false );
  578. doc.Parse( str );
  579. TiXmlBase::SetCondenseWhiteSpace( true );
  580. element = doc.RootElement();
  581. XmlTest( "GetText() partial.", "This is ", element->GetText() );
  582. }
  583. //////////////////////////////////////////////////////
  584. // CDATA
  585. {
  586. const char* str = "<xmlElement>"
  587. "<![CDATA["
  588. "I am > the rules!\n"
  589. "...since I make symbolic puns"
  590. "]]>"
  591. "</xmlElement>";
  592. TiXmlDocument doc;
  593. doc.Parse( str );
  594. doc.Print();
  595. XmlTest( "CDATA parse.", doc.FirstChildElement()->FirstChild()->Value(),
  596. "I am > the rules!\n...since I make symbolic puns",
  597. true );
  598. #ifdef TIXML_USE_STL
  599. //cout << doc << '\n';
  600. doc.Clear();
  601. istringstream parse0( str );
  602. parse0 >> doc;
  603. //cout << doc << '\n';
  604. XmlTest( "CDATA stream.", doc.FirstChildElement()->FirstChild()->Value(),
  605. "I am > the rules!\n...since I make symbolic puns",
  606. true );
  607. #endif
  608. TiXmlDocument doc1 = doc;
  609. //doc.Print();
  610. XmlTest( "CDATA copy.", doc1.FirstChildElement()->FirstChild()->Value(),
  611. "I am > the rules!\n...since I make symbolic puns",
  612. true );
  613. }
  614. {
  615. // [ 1482728 ] Wrong wide char parsing
  616. char buf[256];
  617. buf[255] = 0;
  618. for( int i=0; i<255; ++i ) {
  619. buf[i] = (char)((i>=32) ? i : 32);
  620. }
  621. TIXML_STRING str( "<xmlElement><![CDATA[" );
  622. str += buf;
  623. str += "]]></xmlElement>";
  624. TiXmlDocument doc;
  625. doc.Parse( str.c_str() );
  626. TiXmlPrinter printer;
  627. printer.SetStreamPrinting();
  628. doc.Accept( &printer );
  629. XmlTest( "CDATA with all bytes #1.", str.c_str(), printer.CStr(), true );
  630. #ifdef TIXML_USE_STL
  631. doc.Clear();
  632. istringstream iss( printer.Str() );
  633. iss >> doc;
  634. std::string out;
  635. out << doc;
  636. XmlTest( "CDATA with all bytes #2.", out.c_str(), printer.CStr(), true );
  637. #endif
  638. }
  639. {
  640. // [ 1480107 ] Bug-fix for STL-streaming of CDATA that contains tags
  641. // CDATA streaming had a couple of bugs, that this tests for.
  642. const char* str = "<xmlElement>"
  643. "<![CDATA["
  644. "<b>I am > the rules!</b>\n"
  645. "...since I make symbolic puns"
  646. "]]>"
  647. "</xmlElement>";
  648. TiXmlDocument doc;
  649. doc.Parse( str );
  650. doc.Print();
  651. XmlTest( "CDATA parse. [ 1480107 ]", doc.FirstChildElement()->FirstChild()->Value(),
  652. "<b>I am > the rules!</b>\n...since I make symbolic puns",
  653. true );
  654. #ifdef TIXML_USE_STL
  655. doc.Clear();
  656. istringstream parse0( str );
  657. parse0 >> doc;
  658. XmlTest( "CDATA stream. [ 1480107 ]", doc.FirstChildElement()->FirstChild()->Value(),
  659. "<b>I am > the rules!</b>\n...since I make symbolic puns",
  660. true );
  661. #endif
  662. TiXmlDocument doc1 = doc;
  663. //doc.Print();
  664. XmlTest( "CDATA copy. [ 1480107 ]", doc1.FirstChildElement()->FirstChild()->Value(),
  665. "<b>I am > the rules!</b>\n...since I make symbolic puns",
  666. true );
  667. }
  668. //////////////////////////////////////////////////////
  669. // Visit()
  670. //////////////////////////////////////////////////////
  671. Logger::log( "\n** Fuzzing... **\n" );
  672. const int FUZZ_ITERATION = 300;
  673. // The only goal is not to crash on bad input.
  674. int len = (int) strlen( demoStart );
  675. for( int i=0; i<FUZZ_ITERATION; ++i )
  676. {
  677. char* demoCopy = new char[ len+1 ];
  678. strcpy( demoCopy, demoStart );
  679. demoCopy[ i%len ] = (char)((i+1)*3);
  680. demoCopy[ (i*7)%len ] = '>';
  681. demoCopy[ (i*11)%len ] = '<';
  682. TiXmlDocument xml;
  683. xml.Parse( demoCopy );
  684. delete [] demoCopy;
  685. }
  686. Logger::log( "** Fuzzing Complete. **\n" );
  687. //////////////////////////////////////////////////////
  688. Logger::log ("\n** Bug regression tests **\n");
  689. // InsertBeforeChild and InsertAfterChild causes crash.
  690. {
  691. TiXmlElement parent( "Parent" );
  692. TiXmlElement childText0( "childText0" );
  693. TiXmlElement childText1( "childText1" );
  694. TiXmlNode* childNode0 = parent.InsertEndChild( childText0 );
  695. TiXmlNode* childNode1 = parent.InsertBeforeChild( childNode0, childText1 );
  696. XmlTest( "Test InsertBeforeChild on empty node.", ( childNode1 == parent.FirstChild() ), true );
  697. }
  698. {
  699. // InsertBeforeChild and InsertAfterChild causes crash.
  700. TiXmlElement parent( "Parent" );
  701. TiXmlElement childText0( "childText0" );
  702. TiXmlElement childText1( "childText1" );
  703. TiXmlNode* childNode0 = parent.InsertEndChild( childText0 );
  704. TiXmlNode* childNode1 = parent.InsertAfterChild( childNode0, childText1 );
  705. XmlTest( "Test InsertAfterChild on empty node. ", ( childNode1 == parent.LastChild() ), true );
  706. }
  707. // Reports of missing constructors, irregular string problems.
  708. {
  709. // Missing constructor implementation. No test -- just compiles.
  710. TiXmlText text( "Missing" );
  711. #ifdef TIXML_USE_STL
  712. // Missing implementation:
  713. TiXmlDocument doc;
  714. string name = "missing";
  715. doc.LoadFile( name );
  716. TiXmlText textSTL( name );
  717. #else
  718. // verifying some basic string functions:
  719. TiXmlString a;
  720. TiXmlString b( "Hello" );
  721. TiXmlString c( "ooga" );
  722. c = " World!";
  723. a = b;
  724. a += c;
  725. a = a;
  726. XmlTest( "Basic TiXmlString test. ", "Hello World!", a.c_str() );
  727. #endif
  728. }
  729. // Long filenames crashing STL version
  730. {
  731. TiXmlDocument doc( "midsummerNightsDreamWithAVeryLongFilenameToConfuseTheStringHandlingRoutines.xml" );
  732. bool loadOkay = doc.LoadFile();
  733. loadOkay = true; // get rid of compiler warning.
  734. // Won't pass on non-dev systems. Just a "no crash" check.
  735. //XmlTest( "Long filename. ", true, loadOkay );
  736. }
  737. {
  738. // Entities not being written correctly.
  739. // From Lynn Allen
  740. const char* passages =
  741. "<?xml version=\"1.0\" standalone=\"no\" ?>"
  742. "<passages count=\"006\" formatversion=\"20020620\">"
  743. "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
  744. " It also has &lt;, &gt;, and &amp;, as well as a fake copyright &#xA9;.\"> </psg>"
  745. "</passages>";
  746. TiXmlDocument doc( "passages.xml" );
  747. doc.Parse( passages );
  748. TiXmlElement* psg = doc.RootElement()->FirstChildElement();
  749. const char* context = psg->Attribute( "context" );
  750. const char* expected = "Line 5 has \"quotation marks\" and 'apostrophe marks'. It also has <, >, and &, as well as a fake copyright \xC2\xA9.";
  751. XmlTest( "Entity transformation: read. ", expected, context, true );
  752. FILE* textfile = fopen( "textfile.txt", "w" );
  753. if ( textfile )
  754. {
  755. psg->Print( textfile, 0 );
  756. fclose( textfile );
  757. }
  758. textfile = fopen( "textfile.txt", "r" );
  759. assert( textfile );
  760. if ( textfile )
  761. {
  762. char buf[ 1024 ];
  763. fgets( buf, 1024, textfile );
  764. XmlTest( "Entity transformation: write. ",
  765. "<psg context=\'Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
  766. " It also has &lt;, &gt;, and &amp;, as well as a fake copyright \xC2\xA9.' />",
  767. buf,
  768. true );
  769. }
  770. fclose( textfile );
  771. }
  772. {
  773. FILE* textfile = fopen( "test5.xml", "w" );
  774. if ( textfile )
  775. {
  776. fputs("<?xml version='1.0'?><a.elem xmi.version='2.0'/>", textfile);
  777. fclose(textfile);
  778. TiXmlDocument doc;
  779. doc.LoadFile( "test5.xml" );
  780. XmlTest( "dot in element attributes and names", doc.Error(), 0);
  781. }
  782. }
  783. {
  784. FILE* textfile = fopen( "test6.xml", "w" );
  785. if ( textfile )
  786. {
  787. fputs("<element><Name>1.1 Start easy ignore fin thickness&#xA;</Name></element>", textfile );
  788. fclose(textfile);
  789. TiXmlDocument doc;
  790. bool result = doc.LoadFile( "test6.xml" );
  791. XmlTest( "Entity with one digit.", result, true );
  792. TiXmlText* text = doc.FirstChildElement()->FirstChildElement()->FirstChild()->ToText();
  793. XmlTest( "Entity with one digit.",
  794. text->Value(), "1.1 Start easy ignore fin thickness\n" );
  795. }
  796. }
  797. {
  798. // DOCTYPE not preserved (950171)
  799. //
  800. const char* doctype =
  801. "<?xml version=\"1.0\" ?>"
  802. "<!DOCTYPE PLAY SYSTEM 'play.dtd'>"
  803. "<!ELEMENT title (#PCDATA)>"
  804. "<!ELEMENT books (title,authors)>"
  805. "<element />";
  806. TiXmlDocument doc;
  807. doc.Parse( doctype );
  808. doc.SaveFile( "test7.xml" );
  809. doc.Clear();
  810. doc.LoadFile( "test7.xml" );
  811. TiXmlHandle docH( &doc );
  812. TiXmlUnknown* unknown = docH.Child( 1 ).Unknown();
  813. XmlTest( "Correct value of unknown.", "!DOCTYPE PLAY SYSTEM 'play.dtd'", unknown->Value() );
  814. #ifdef TIXML_USE_STL
  815. TiXmlNode* node = docH.Child( 2 ).Node();
  816. std::string str;
  817. str << (*node);
  818. XmlTest( "Correct streaming of unknown.", "<!ELEMENT title (#PCDATA)>", str.c_str() );
  819. #endif
  820. }
  821. {
  822. // [ 791411 ] Formatting bug
  823. // Comments do not stream out correctly.
  824. const char* doctype =
  825. "<!-- Somewhat<evil> -->";
  826. TiXmlDocument doc;
  827. doc.Parse( doctype );
  828. TiXmlHandle docH( &doc );
  829. TiXmlComment* comment = docH.Child( 0 ).Node()->ToComment();
  830. XmlTest( "Comment formatting.", " Somewhat<evil> ", comment->Value() );
  831. #ifdef TIXML_USE_STL
  832. std::string str;
  833. str << (*comment);
  834. XmlTest( "Comment streaming.", "<!-- Somewhat<evil> -->", str.c_str() );
  835. #endif
  836. }
  837. {
  838. // [ 870502 ] White space issues
  839. TiXmlDocument doc;
  840. TiXmlText* text;
  841. TiXmlHandle docH( &doc );
  842. const char* doctype0 = "<element> This has leading and trailing space </element>";
  843. const char* doctype1 = "<element>This has internal space</element>";
  844. const char* doctype2 = "<element> This has leading, trailing, and internal space </element>";
  845. TiXmlBase::SetCondenseWhiteSpace( false );
  846. doc.Clear();
  847. doc.Parse( doctype0 );
  848. text = docH.FirstChildElement( "element" ).Child( 0 ).Text();
  849. XmlTest( "White space kept.", " This has leading and trailing space ", text->Value() );
  850. doc.Clear();
  851. doc.Parse( doctype1 );
  852. text = docH.FirstChildElement( "element" ).Child( 0 ).Text();
  853. XmlTest( "White space kept.", "This has internal space", text->Value() );
  854. doc.Clear();
  855. doc.Parse( doctype2 );
  856. text = docH.FirstChildElement( "element" ).Child( 0 ).Text();
  857. XmlTest( "White space kept.", " This has leading, trailing, and internal space ", text->Value() );
  858. TiXmlBase::SetCondenseWhiteSpace( true );
  859. doc.Clear();
  860. doc.Parse( doctype0 );
  861. text = docH.FirstChildElement( "element" ).Child( 0 ).Text();
  862. XmlTest( "White space condensed.", "This has leading and trailing space", text->Value() );
  863. doc.Clear();
  864. doc.Parse( doctype1 );
  865. text = docH.FirstChildElement( "element" ).Child( 0 ).Text();
  866. XmlTest( "White space condensed.", "This has internal space", text->Value() );
  867. doc.Clear();
  868. doc.Parse( doctype2 );
  869. text = docH.FirstChildElement( "element" ).Child( 0 ).Text();
  870. XmlTest( "White space condensed.", "This has leading, trailing, and internal space", text->Value() );
  871. }
  872. {
  873. // Double attributes
  874. const char* doctype = "<element attr='red' attr='blue' />";
  875. TiXmlDocument doc;
  876. doc.Parse( doctype );
  877. XmlTest( "Parsing repeated attributes.", 0, (int)doc.Error() ); // not an error to tinyxml
  878. XmlTest( "Parsing repeated attributes.", "blue", doc.FirstChildElement( "element" )->Attribute( "attr" ) );
  879. }
  880. {
  881. // Embedded null in stream.
  882. const char* doctype = "<element att\0r='red' attr='blue' />";
  883. TiXmlDocument doc;
  884. doc.Parse( doctype );
  885. XmlTest( "Embedded null throws error.", true, doc.Error() );
  886. #ifdef TIXML_USE_STL
  887. istringstream strm( doctype );
  888. doc.Clear();
  889. doc.ClearError();
  890. strm >> doc;
  891. XmlTest( "Embedded null throws error.", true, doc.Error() );
  892. #endif
  893. }
  894. {
  895. // Legacy mode test. (This test may only pass on a western system)
  896. const char* str =
  897. "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"
  898. "<ä>"
  899. "CöntäntßäöüÄÖÜ"
  900. "</ä>";
  901. TiXmlDocument doc;
  902. doc.Parse( str );
  903. TiXmlHandle docHandle( &doc );
  904. TiXmlHandle aHandle = docHandle.FirstChildElement( "ä" );
  905. TiXmlHandle tHandle = aHandle.Child( 0 );
  906. assert( aHandle.Element() );
  907. assert( tHandle.Text() );
  908. XmlTest( "ISO-8859-1 Parsing.", "CöntäntßäöüÄÖÜ", tHandle.Text()->Value() );
  909. }
  910. {
  911. // Empty documents should return TIXML_ERROR_PARSING_EMPTY, bug 1070717
  912. const char* str = " ";
  913. TiXmlDocument doc;
  914. doc.Parse( str );
  915. XmlTest( "Empty document error TIXML_ERROR_DOCUMENT_EMPTY", TiXmlBase::TIXML_ERROR_DOCUMENT_EMPTY, doc.ErrorId() );
  916. }
  917. #ifndef TIXML_USE_STL
  918. {
  919. // String equality. [ 1006409 ] string operator==/!= no worky in all cases
  920. TiXmlString temp;
  921. XmlTest( "Empty tinyxml string compare equal", ( temp == "" ), true );
  922. TiXmlString foo;
  923. TiXmlString bar( "" );
  924. XmlTest( "Empty tinyxml string compare equal", ( foo == bar ), true );
  925. }
  926. #endif
  927. {
  928. // Bug [ 1195696 ] from marlonism
  929. TiXmlBase::SetCondenseWhiteSpace(false);
  930. TiXmlDocument xml;
  931. xml.Parse("<text><break/>This hangs</text>");
  932. XmlTest( "Test safe error return.", xml.Error(), false );
  933. }
  934. {
  935. // Bug [ 1243992 ] - another infinite loop
  936. TiXmlDocument doc;
  937. doc.SetCondenseWhiteSpace(false);
  938. doc.Parse("<p><pb></pb>test</p>");
  939. }
  940. {
  941. // Low entities
  942. TiXmlDocument xml;
  943. xml.Parse( "<test>&#x0e;</test>" );
  944. const char result[] = { 0x0e, 0 };
  945. XmlTest( "Low entities.", xml.FirstChildElement()->GetText(), result );
  946. xml.Print();
  947. }
  948. {
  949. // Bug [ 1451649 ] Attribute values with trailing quotes not handled correctly
  950. TiXmlDocument xml;
  951. xml.Parse( "<foo attribute=bar\" />" );
  952. XmlTest( "Throw error with bad end quotes.", xml.Error(), true );
  953. }
  954. #ifdef TIXML_USE_STL
  955. {
  956. // Bug [ 1449463 ] Consider generic query
  957. TiXmlDocument xml;
  958. xml.Parse( "<foo bar='3' barStr='a string'/>" );
  959. TiXmlElement* ele = xml.FirstChildElement();
  960. double d;
  961. int i;
  962. float f;
  963. bool b;
  964. //std::string str;
  965. XmlTest( "QueryValueAttribute", ele->QueryValueAttribute( "bar", &d ), TIXML_SUCCESS );
  966. XmlTest( "QueryValueAttribute", ele->QueryValueAttribute( "bar", &i ), TIXML_SUCCESS );
  967. XmlTest( "QueryValueAttribute", ele->QueryValueAttribute( "bar", &f ), TIXML_SUCCESS );
  968. XmlTest( "QueryValueAttribute", ele->QueryValueAttribute( "bar", &b ), TIXML_WRONG_TYPE );
  969. XmlTest( "QueryValueAttribute", ele->QueryValueAttribute( "nobar", &b ), TIXML_NO_ATTRIBUTE );
  970. //XmlTest( "QueryValueAttribute", ele->QueryValueAttribute( "barStr", &str ), TIXML_SUCCESS );
  971. XmlTest( "QueryValueAttribute", (d==3.0), true );
  972. XmlTest( "QueryValueAttribute", (i==3), true );
  973. XmlTest( "QueryValueAttribute", (f==3.0f), true );
  974. //XmlTest( "QueryValueAttribute", (str==std::string( "a string" )), true );
  975. }
  976. #endif
  977. #ifdef TIXML_USE_STL
  978. {
  979. // [ 1505267 ] redundant malloc in TiXmlElement::Attribute
  980. TiXmlDocument xml;
  981. xml.Parse( "<foo bar='3' />" );
  982. TiXmlElement* ele = xml.FirstChildElement();
  983. double d;
  984. int i;
  985. std::string bar = "bar";
  986. const std::string* atrrib = ele->Attribute( bar );
  987. ele->Attribute( bar, &d );
  988. ele->Attribute( bar, &i );
  989. XmlTest( "Attribute", atrrib->empty(), false );
  990. XmlTest( "Attribute", (d==3.0), true );
  991. XmlTest( "Attribute", (i==3), true );
  992. }
  993. #endif
  994. {
  995. // [ 1356059 ] Allow TiXMLDocument to only be at the top level
  996. TiXmlDocument xml, xml2;
  997. xml.InsertEndChild( xml2 );
  998. XmlTest( "Document only at top level.", xml.Error(), true );
  999. XmlTest( "Document only at top level.", xml.ErrorId(), TiXmlBase::TIXML_ERROR_DOCUMENT_TOP_ONLY );
  1000. }
  1001. {
  1002. // [ 1663758 ] Failure to report error on bad XML
  1003. TiXmlDocument xml;
  1004. xml.Parse("<x>");
  1005. XmlTest("Missing end tag at end of input", xml.Error(), true);
  1006. xml.Parse("<x> ");
  1007. XmlTest("Missing end tag with trailing whitespace", xml.Error(), true);
  1008. }
  1009. {
  1010. // [ 1635701 ] fail to parse files with a tag separated into two lines
  1011. // I'm not sure this is a bug. Marked 'pending' for feedback.
  1012. TiXmlDocument xml;
  1013. xml.Parse( "<title><p>text</p\n><title>" );
  1014. //xml.Print();
  1015. //XmlTest( "Tag split by newline", xml.Error(), false );
  1016. }
  1017. #ifdef TIXML_USE_STL
  1018. {
  1019. // [ 1475201 ] TinyXML parses entities in comments
  1020. TiXmlDocument xml;
  1021. istringstream parse1( "<!-- declarations for <head> & <body> -->"
  1022. "<!-- far &amp; away -->" );
  1023. parse1 >> xml;
  1024. TiXmlNode* e0 = xml.FirstChild();
  1025. TiXmlNode* e1 = e0->NextSibling();
  1026. TiXmlComment* c0 = e0->ToComment();
  1027. TiXmlComment* c1 = e1->ToComment();
  1028. XmlTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
  1029. XmlTest( "Comments ignore entities.", " far &amp; away ", c1->Value(), true );
  1030. }
  1031. #endif
  1032. {
  1033. // [ 1475201 ] TinyXML parses entities in comments
  1034. TiXmlDocument xml;
  1035. xml.Parse("<!-- declarations for <head> & <body> -->"
  1036. "<!-- far &amp; away -->" );
  1037. TiXmlNode* e0 = xml.FirstChild();
  1038. TiXmlNode* e1 = e0->NextSibling();
  1039. TiXmlComment* c0 = e0->ToComment();
  1040. TiXmlComment* c1 = e1->ToComment();
  1041. XmlTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
  1042. XmlTest( "Comments ignore entities.", " far &amp; away ", c1->Value(), true );
  1043. }
  1044. /*
  1045. {
  1046. TiXmlDocument xml;
  1047. xml.Parse( "<tag>/</tag>" );
  1048. xml.Print();
  1049. xml.FirstChild()->Print( stdout, 0 );
  1050. xml.FirstChild()->Type();
  1051. }
  1052. */
  1053. /* 1417717 experiment
  1054. {
  1055. TiXmlDocument xml;
  1056. xml.Parse("<text>Dan & Tracie</text>");
  1057. xml.Print(stdout);
  1058. }
  1059. {
  1060. TiXmlDocument xml;
  1061. xml.Parse("<text>Dan &foo; Tracie</text>");
  1062. xml.Print(stdout);
  1063. }
  1064. */
  1065. #if defined( WIN32 ) && defined( TUNE )
  1066. _CrtMemCheckpoint( &endMemState );
  1067. //_CrtMemDumpStatistics( &endMemState );
  1068. _CrtMemState diffMemState;
  1069. _CrtMemDifference( &diffMemState, &startMemState, &endMemState );
  1070. _CrtMemDumpStatistics( &diffMemState );
  1071. #endif
  1072. Logger::log ("\nPass %d, Fail %d\n", gPass, gFail);
  1073. return gFail;
  1074. }