xmltest.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. #include "tinyxml2.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #if defined( _MSC_VER )
  6. #include <crtdbg.h>
  7. _CrtMemState startMemState;
  8. _CrtMemState endMemState;
  9. #endif
  10. using namespace tinyxml2;
  11. int gPass = 0;
  12. int gFail = 0;
  13. bool XMLTest (const char* testString, const char* expected, const char* found, bool echo=true )
  14. {
  15. bool pass = !strcmp( expected, found );
  16. if ( pass )
  17. printf ("[pass]");
  18. else
  19. printf ("[fail]");
  20. if ( !echo )
  21. printf (" %s\n", testString);
  22. else
  23. printf (" %s [%s][%s]\n", testString, expected, found);
  24. if ( pass )
  25. ++gPass;
  26. else
  27. ++gFail;
  28. return pass;
  29. }
  30. bool XMLTest( const char* testString, int expected, int found, bool echo=true )
  31. {
  32. bool pass = ( expected == found );
  33. if ( pass )
  34. printf ("[pass]");
  35. else
  36. printf ("[fail]");
  37. if ( !echo )
  38. printf (" %s\n", testString);
  39. else
  40. printf (" %s [%d][%d]\n", testString, expected, found);
  41. if ( pass )
  42. ++gPass;
  43. else
  44. ++gFail;
  45. return pass;
  46. }
  47. void NullLineEndings( char* p )
  48. {
  49. while( p && *p ) {
  50. if ( *p == '\n' || *p == '\r' ) {
  51. *p = 0;
  52. return;
  53. }
  54. ++p;
  55. }
  56. }
  57. int main( int /*argc*/, const char* /*argv*/ )
  58. {
  59. #if defined( _MSC_VER )
  60. _CrtMemCheckpoint( &startMemState );
  61. #endif
  62. {
  63. static const char* test[] = { "<element />",
  64. "<element></element>",
  65. "<element><subelement/></element>",
  66. "<element><subelement></subelement></element>",
  67. "<element><subelement><subsub/></subelement></element>",
  68. "<!--comment beside elements--><element><subelement></subelement></element>",
  69. "<!--comment beside elements, this time with spaces--> \n <element> <subelement> \n </subelement> </element>",
  70. "<element attrib1='foo' attrib2=\"bar\" ></element>",
  71. "<element attrib1='foo' attrib2=\"bar\" ><subelement attrib3='yeehaa' /></element>",
  72. "<element>Text inside element.</element>",
  73. "<element><b></b></element>",
  74. "<element>Text inside and <b>bolded</b> in the element.</element>",
  75. "<outer><element>Text inside and <b>bolded</b> in the element.</element></outer>",
  76. "<element>This &amp; That.</element>",
  77. "<element attrib='This&lt;That' />",
  78. 0
  79. };
  80. for( int i=0; test[i]; ++i ) {
  81. XMLDocument doc;
  82. doc.Parse( test[i] );
  83. doc.Print();
  84. printf( "----------------------------------------------\n" );
  85. }
  86. }
  87. #if 1
  88. {
  89. static const char* test = "<!--hello world\n"
  90. " line 2\r"
  91. " line 3\r\n"
  92. " line 4\n\r"
  93. " line 5\r-->";
  94. XMLDocument doc;
  95. doc.Parse( test );
  96. doc.Print();
  97. }
  98. {
  99. static const char* test = "<element>Text before.</element>";
  100. XMLDocument doc;
  101. doc.Parse( test );
  102. XMLElement* root = doc.FirstChildElement();
  103. XMLElement* newElement = doc.NewElement( "Subelement" );
  104. root->InsertEndChild( newElement );
  105. doc.Print();
  106. }
  107. {
  108. XMLDocument* doc = new XMLDocument();
  109. static const char* test = "<element><sub/></element>";
  110. doc->Parse( test );
  111. delete doc;
  112. }
  113. {
  114. // Test: Programmatic DOM
  115. // Build:
  116. // <element>
  117. // <!--comment-->
  118. // <sub attrib="1" />
  119. // <sub attrib="2" />
  120. // <sub attrib="3" >& Text!</sub>
  121. // <element>
  122. XMLDocument* doc = new XMLDocument();
  123. XMLNode* element = doc->InsertEndChild( doc->NewElement( "element" ) );
  124. XMLElement* sub[3] = { doc->NewElement( "sub" ), doc->NewElement( "sub" ), doc->NewElement( "sub" ) };
  125. for( int i=0; i<3; ++i ) {
  126. sub[i]->SetAttribute( "attrib", i );
  127. }
  128. element->InsertEndChild( sub[2] );
  129. XMLNode* comment = element->InsertFirstChild( doc->NewComment( "comment" ) );
  130. element->InsertAfterChild( comment, sub[0] );
  131. element->InsertAfterChild( sub[0], sub[1] );
  132. sub[2]->InsertFirstChild( doc->NewText( "& Text!" ));
  133. doc->Print();
  134. XMLTest( "Programmatic DOM", "comment", doc->FirstChildElement( "element" )->FirstChild()->Value() );
  135. XMLTest( "Programmatic DOM", "0", doc->FirstChildElement( "element" )->FirstChildElement()->Attribute( "attrib" ) );
  136. XMLTest( "Programmatic DOM", 2, doc->FirstChildElement()->LastChildElement( "sub" )->IntAttribute( "attrib" ) );
  137. XMLTest( "Programmatic DOM", "& Text!",
  138. doc->FirstChildElement()->LastChildElement( "sub" )->FirstChild()->ToText()->Value() );
  139. // And now deletion:
  140. element->DeleteChild( sub[2] );
  141. doc->DeleteNode( comment );
  142. element->FirstChildElement()->SetAttribute( "attrib", true );
  143. element->LastChildElement()->DeleteAttribute( "attrib" );
  144. XMLTest( "Programmatic DOM", true, doc->FirstChildElement()->FirstChildElement()->BoolAttribute( "attrib" ) );
  145. int value = 10;
  146. int result = doc->FirstChildElement()->LastChildElement()->QueryIntAttribute( "attrib", &value );
  147. XMLTest( "Programmatic DOM", result, NO_ATTRIBUTE );
  148. XMLTest( "Programmatic DOM", value, 10 );
  149. doc->Print();
  150. XMLPrinter streamer;
  151. doc->Print( &streamer );
  152. printf( "%s", streamer.CStr() );
  153. delete doc;
  154. }
  155. {
  156. // Test: Dream
  157. // XML1 : 1,187,569 bytes in 31,209 allocations
  158. // XML2 : 469,073 bytes in 323 allocations
  159. //int newStart = gNew;
  160. XMLDocument doc;
  161. doc.LoadFile( "dream.xml" );
  162. doc.SaveFile( "dreamout.xml" );
  163. doc.PrintError();
  164. XMLTest( "Dream", "xml version=\"1.0\"",
  165. doc.FirstChild()->ToDeclaration()->Value() );
  166. XMLTest( "Dream", true, doc.FirstChild()->NextSibling()->ToUnknown() ? true : false );
  167. XMLTest( "Dream", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
  168. doc.FirstChild()->NextSibling()->ToUnknown()->Value() );
  169. XMLTest( "Dream", "And Robin shall restore amends.",
  170. doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
  171. XMLTest( "Dream", "And Robin shall restore amends.",
  172. doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
  173. XMLDocument doc2;
  174. doc2.LoadFile( "dreamout.xml" );
  175. XMLTest( "Dream-out", "xml version=\"1.0\"",
  176. doc2.FirstChild()->ToDeclaration()->Value() );
  177. XMLTest( "Dream-out", true, doc2.FirstChild()->NextSibling()->ToUnknown() ? true : false );
  178. XMLTest( "Dream-out", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
  179. doc2.FirstChild()->NextSibling()->ToUnknown()->Value() );
  180. XMLTest( "Dream-out", "And Robin shall restore amends.",
  181. doc2.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
  182. //gNewTotal = gNew - newStart;
  183. }
  184. {
  185. const char* error = "<?xml version=\"1.0\" standalone=\"no\" ?>\n"
  186. "<passages count=\"006\" formatversion=\"20020620\">\n"
  187. " <wrong error>\n"
  188. "</passages>";
  189. XMLDocument doc;
  190. doc.Parse( error );
  191. XMLTest( "Bad XML", doc.ErrorID(), ERROR_PARSING_ATTRIBUTE );
  192. }
  193. {
  194. const char* str = "<doc attr0='1' attr1='2.0' attr2='foo' />";
  195. XMLDocument doc;
  196. doc.Parse( str );
  197. XMLElement* ele = doc.FirstChildElement();
  198. int iVal, result;
  199. double dVal;
  200. result = ele->QueryDoubleAttribute( "attr0", &dVal );
  201. XMLTest( "Query attribute: int as double", result, XML_NO_ERROR );
  202. XMLTest( "Query attribute: int as double", (int)dVal, 1 );
  203. result = ele->QueryDoubleAttribute( "attr1", &dVal );
  204. XMLTest( "Query attribute: double as double", (int)dVal, 2 );
  205. result = ele->QueryIntAttribute( "attr1", &iVal );
  206. XMLTest( "Query attribute: double as int", result, XML_NO_ERROR );
  207. XMLTest( "Query attribute: double as int", iVal, 2 );
  208. result = ele->QueryIntAttribute( "attr2", &iVal );
  209. XMLTest( "Query attribute: not a number", result, WRONG_ATTRIBUTE_TYPE );
  210. result = ele->QueryIntAttribute( "bar", &iVal );
  211. XMLTest( "Query attribute: does not exist", result, NO_ATTRIBUTE );
  212. }
  213. {
  214. const char* str = "<doc/>";
  215. XMLDocument doc;
  216. doc.Parse( str );
  217. XMLElement* ele = doc.FirstChildElement();
  218. int iVal;
  219. double dVal;
  220. ele->SetAttribute( "str", "strValue" );
  221. ele->SetAttribute( "int", 1 );
  222. ele->SetAttribute( "double", -1.0 );
  223. const char* cStr = ele->Attribute( "str" );
  224. ele->QueryIntAttribute( "int", &iVal );
  225. ele->QueryDoubleAttribute( "double", &dVal );
  226. XMLTest( "Attribute round trip. c-string.", "strValue", cStr );
  227. XMLTest( "Attribute round trip. int.", 1, iVal );
  228. XMLTest( "Attribute round trip. double.", -1, (int)dVal );
  229. }
  230. {
  231. XMLDocument doc;
  232. doc.LoadFile( "utf8test.xml" );
  233. // Get the attribute "value" from the "Russian" element and check it.
  234. XMLElement* element = doc.FirstChildElement( "document" )->FirstChildElement( "Russian" );
  235. const unsigned char correctValue[] = { 0xd1U, 0x86U, 0xd0U, 0xb5U, 0xd0U, 0xbdU, 0xd0U, 0xbdU,
  236. 0xd0U, 0xbeU, 0xd1U, 0x81U, 0xd1U, 0x82U, 0xd1U, 0x8cU, 0 };
  237. XMLTest( "UTF-8: Russian value.", (const char*)correctValue, element->Attribute( "value" ) );
  238. const unsigned char russianElementName[] = { 0xd0U, 0xa0U, 0xd1U, 0x83U,
  239. 0xd1U, 0x81U, 0xd1U, 0x81U,
  240. 0xd0U, 0xbaU, 0xd0U, 0xb8U,
  241. 0xd0U, 0xb9U, 0 };
  242. const char russianText[] = "<\xD0\xB8\xD0\xBC\xD0\xB5\xD0\xB5\xD1\x82>";
  243. XMLText* text = doc.FirstChildElement( "document" )->FirstChildElement( (const char*) russianElementName )->FirstChild()->ToText();
  244. XMLTest( "UTF-8: Browsing russian element name.",
  245. russianText,
  246. text->Value() );
  247. // Now try for a round trip.
  248. doc.SaveFile( "utf8testout.xml" );
  249. // Check the round trip.
  250. char savedBuf[256];
  251. char verifyBuf[256];
  252. int okay = 0;
  253. #if defined(_MSC_VER)
  254. #pragma warning ( push )
  255. #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
  256. #endif
  257. FILE* saved = fopen( "utf8testout.xml", "r" );
  258. FILE* verify = fopen( "utf8testverify.xml", "r" );
  259. #if defined(_MSC_VER)
  260. #pragma warning ( pop )
  261. #endif
  262. if ( saved && verify )
  263. {
  264. okay = 1;
  265. while ( fgets( verifyBuf, 256, verify ) )
  266. {
  267. fgets( savedBuf, 256, saved );
  268. NullLineEndings( verifyBuf );
  269. NullLineEndings( savedBuf );
  270. if ( strcmp( verifyBuf, savedBuf ) )
  271. {
  272. printf( "verify:%s<\n", verifyBuf );
  273. printf( "saved :%s<\n", savedBuf );
  274. okay = 0;
  275. break;
  276. }
  277. }
  278. }
  279. if ( saved )
  280. fclose( saved );
  281. if ( verify )
  282. fclose( verify );
  283. XMLTest( "UTF-8: Verified multi-language round trip.", 1, okay );
  284. }
  285. // --------GetText()-----------
  286. {
  287. const char* str = "<foo>This is text</foo>";
  288. XMLDocument doc;
  289. doc.Parse( str );
  290. const XMLElement* element = doc.RootElement();
  291. XMLTest( "GetText() normal use.", "This is text", element->GetText() );
  292. str = "<foo><b>This is text</b></foo>";
  293. doc.Parse( str );
  294. element = doc.RootElement();
  295. XMLTest( "GetText() contained element.", element->GetText() == 0, true );
  296. }
  297. // ---------- CDATA ---------------
  298. {
  299. const char* str = "<xmlElement>"
  300. "<![CDATA["
  301. "I am > the rules!\n"
  302. "...since I make symbolic puns"
  303. "]]>"
  304. "</xmlElement>";
  305. XMLDocument doc;
  306. doc.Parse( str );
  307. doc.Print();
  308. XMLTest( "CDATA parse.", doc.FirstChildElement()->FirstChild()->Value(),
  309. "I am > the rules!\n...since I make symbolic puns",
  310. false );
  311. }
  312. // ----------- CDATA -------------
  313. {
  314. const char* str = "<xmlElement>"
  315. "<![CDATA["
  316. "<b>I am > the rules!</b>\n"
  317. "...since I make symbolic puns"
  318. "]]>"
  319. "</xmlElement>";
  320. XMLDocument doc;
  321. doc.Parse( str );
  322. doc.Print();
  323. XMLTest( "CDATA parse. [ tixml1:1480107 ]", doc.FirstChildElement()->FirstChild()->Value(),
  324. "<b>I am > the rules!</b>\n...since I make symbolic puns",
  325. false );
  326. }
  327. // InsertAfterChild causes crash.
  328. {
  329. // InsertBeforeChild and InsertAfterChild causes crash.
  330. XMLDocument doc;
  331. XMLElement* parent = doc.NewElement( "Parent" );
  332. doc.InsertFirstChild( parent );
  333. XMLElement* childText0 = doc.NewElement( "childText0" );
  334. XMLElement* childText1 = doc.NewElement( "childText1" );
  335. XMLNode* childNode0 = parent->InsertEndChild( childText0 );
  336. XMLNode* childNode1 = parent->InsertAfterChild( childNode0, childText1 );
  337. XMLTest( "Test InsertAfterChild on empty node. ", ( childNode1 == parent->LastChild() ), true );
  338. }
  339. {
  340. // Entities not being written correctly.
  341. // From Lynn Allen
  342. const char* passages =
  343. "<?xml version=\"1.0\" standalone=\"no\" ?>"
  344. "<passages count=\"006\" formatversion=\"20020620\">"
  345. "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
  346. " It also has &lt;, &gt;, and &amp;, as well as a fake copyright &#xA9;.\"> </psg>"
  347. "</passages>";
  348. XMLDocument doc;
  349. doc.Parse( passages );
  350. XMLElement* psg = doc.RootElement()->FirstChildElement();
  351. const char* context = psg->Attribute( "context" );
  352. const char* expected = "Line 5 has \"quotation marks\" and 'apostrophe marks'. It also has <, >, and &, as well as a fake copyright \xC2\xA9.";
  353. XMLTest( "Entity transformation: read. ", expected, context, true );
  354. #if defined(_MSC_VER)
  355. #pragma warning ( push )
  356. #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
  357. #endif
  358. FILE* textfile = fopen( "textfile.txt", "w" );
  359. #if defined(_MSC_VER)
  360. #pragma warning ( pop )
  361. #endif
  362. if ( textfile )
  363. {
  364. XMLPrinter streamer( textfile );
  365. psg->Accept( &streamer );
  366. fclose( textfile );
  367. }
  368. #if defined(_MSC_VER)
  369. #pragma warning ( push )
  370. #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
  371. #endif
  372. textfile = fopen( "textfile.txt", "r" );
  373. #if defined(_MSC_VER)
  374. #pragma warning ( pop )
  375. #endif
  376. TIXMLASSERT( textfile );
  377. if ( textfile )
  378. {
  379. char buf[ 1024 ];
  380. fgets( buf, 1024, textfile );
  381. XMLTest( "Entity transformation: write. ",
  382. "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
  383. " It also has &lt;, &gt;, and &amp;, as well as a fake copyright \xC2\xA9.\"/>\n",
  384. buf, false );
  385. }
  386. fclose( textfile );
  387. }
  388. {
  389. const char* test = "<?xml version='1.0'?><a.elem xmi.version='2.0'/>";
  390. XMLDocument doc;
  391. doc.Parse( test );
  392. XMLTest( "dot in names", doc.Error(), 0);
  393. XMLTest( "dot in names", doc.FirstChildElement()->Name(), "a.elem" );
  394. XMLTest( "dot in names", doc.FirstChildElement()->Attribute( "xmi.version" ), "2.0" );
  395. }
  396. {
  397. const char* test = "<element><Name>1.1 Start easy ignore fin thickness&#xA;</Name></element>";
  398. XMLDocument doc;
  399. doc.Parse( test );
  400. XMLText* text = doc.FirstChildElement()->FirstChildElement()->FirstChild()->ToText();
  401. XMLTest( "Entity with one digit.",
  402. text->Value(), "1.1 Start easy ignore fin thickness\n",
  403. false );
  404. }
  405. {
  406. // DOCTYPE not preserved (950171)
  407. //
  408. const char* doctype =
  409. "<?xml version=\"1.0\" ?>"
  410. "<!DOCTYPE PLAY SYSTEM 'play.dtd'>"
  411. "<!ELEMENT title (#PCDATA)>"
  412. "<!ELEMENT books (title,authors)>"
  413. "<element />";
  414. XMLDocument doc;
  415. doc.Parse( doctype );
  416. doc.SaveFile( "test7.xml" );
  417. doc.DeleteChild( doc.RootElement() );
  418. doc.LoadFile( "test7.xml" );
  419. doc.Print();
  420. const XMLUnknown* decl = doc.FirstChild()->NextSibling()->ToUnknown();
  421. XMLTest( "Correct value of unknown.", "DOCTYPE PLAY SYSTEM 'play.dtd'", decl->Value() );
  422. }
  423. {
  424. // Comments do not stream out correctly.
  425. const char* doctype =
  426. "<!-- Somewhat<evil> -->";
  427. XMLDocument doc;
  428. doc.Parse( doctype );
  429. XMLComment* comment = doc.FirstChild()->ToComment();
  430. XMLTest( "Comment formatting.", " Somewhat<evil> ", comment->Value() );
  431. }
  432. {
  433. // Double attributes
  434. const char* doctype = "<element attr='red' attr='blue' />";
  435. XMLDocument doc;
  436. doc.Parse( doctype );
  437. XMLTest( "Parsing repeated attributes.", ERROR_PARSING_ATTRIBUTE, doc.ErrorID() ); // is an error to tinyxml (didn't use to be, but caused issues)
  438. }
  439. {
  440. // Embedded null in stream.
  441. const char* doctype = "<element att\0r='red' attr='blue' />";
  442. XMLDocument doc;
  443. doc.Parse( doctype );
  444. XMLTest( "Embedded null throws error.", true, doc.Error() );
  445. }
  446. {
  447. // Empty documents should return TIXML_ERROR_PARSING_EMPTY, bug 1070717
  448. const char* str = " ";
  449. XMLDocument doc;
  450. doc.Parse( str );
  451. XMLTest( "Empty document error", ERROR_EMPTY_DOCUMENT, doc.ErrorID() );
  452. }
  453. {
  454. // Low entities
  455. XMLDocument doc;
  456. doc.Parse( "<test>&#x0e;</test>" );
  457. const char result[] = { 0x0e, 0 };
  458. XMLTest( "Low entities.", doc.FirstChildElement()->GetText(), result );
  459. doc.Print();
  460. }
  461. {
  462. // Attribute values with trailing quotes not handled correctly
  463. XMLDocument doc;
  464. doc.Parse( "<foo attribute=bar\" />" );
  465. XMLTest( "Throw error with bad end quotes.", doc.Error(), true );
  466. }
  467. {
  468. // [ 1663758 ] Failure to report error on bad XML
  469. XMLDocument xml;
  470. xml.Parse("<x>");
  471. XMLTest("Missing end tag at end of input", xml.Error(), true);
  472. xml.Parse("<x> ");
  473. XMLTest("Missing end tag with trailing whitespace", xml.Error(), true);
  474. xml.Parse("<x></y>");
  475. XMLTest("Mismatched tags", xml.ErrorID(), ERROR_MISMATCHED_ELEMENT);
  476. }
  477. {
  478. // [ 1475201 ] TinyXML parses entities in comments
  479. XMLDocument xml;
  480. xml.Parse("<!-- declarations for <head> & <body> -->"
  481. "<!-- far &amp; away -->" );
  482. XMLNode* e0 = xml.FirstChild();
  483. XMLNode* e1 = e0->NextSibling();
  484. XMLComment* c0 = e0->ToComment();
  485. XMLComment* c1 = e1->ToComment();
  486. XMLTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
  487. XMLTest( "Comments ignore entities.", " far &amp; away ", c1->Value(), true );
  488. }
  489. {
  490. XMLDocument xml;
  491. xml.Parse( "<Parent>"
  492. "<child1 att=''/>"
  493. "<!-- With this comment, child2 will not be parsed! -->"
  494. "<child2 att=''/>"
  495. "</Parent>" );
  496. xml.Print();
  497. int count = 0;
  498. for( XMLNode* ele = xml.FirstChildElement( "Parent" )->FirstChild();
  499. ele;
  500. ele = ele->NextSibling() )
  501. {
  502. ++count;
  503. }
  504. XMLTest( "Comments iterate correctly.", 3, count );
  505. }
  506. {
  507. // trying to repro ]1874301]. If it doesn't go into an infinite loop, all is well.
  508. unsigned char buf[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?><feed><![CDATA[Test XMLblablablalblbl";
  509. buf[60] = 239;
  510. buf[61] = 0;
  511. XMLDocument doc;
  512. doc.Parse( (const char*)buf);
  513. }
  514. {
  515. // bug 1827248 Error while parsing a little bit malformed file
  516. // Actually not malformed - should work.
  517. XMLDocument xml;
  518. xml.Parse( "<attributelist> </attributelist >" );
  519. XMLTest( "Handle end tag whitespace", false, xml.Error() );
  520. }
  521. {
  522. // This one must not result in an infinite loop
  523. XMLDocument xml;
  524. xml.Parse( "<infinite>loop" );
  525. XMLTest( "Infinite loop test.", true, true );
  526. }
  527. #endif
  528. {
  529. const char* pub = "<?xml version='1.0'?> <element><sub/></element> <!--comment--> <!DOCTYPE>";
  530. XMLDocument doc;
  531. doc.Parse( pub );
  532. XMLDocument clone;
  533. for( const XMLNode* node=doc.FirstChild(); node; node=node->NextSibling() ) {
  534. XMLNode* copy = node->ShallowClone( &clone );
  535. clone.InsertEndChild( copy );
  536. }
  537. clone.Print();
  538. int count=0;
  539. const XMLNode* a=clone.FirstChild();
  540. const XMLNode* b=doc.FirstChild();
  541. for( ; a && b; a=a->NextSibling(), b=b->NextSibling() ) {
  542. ++count;
  543. XMLTest( "Clone and Equal", true, a->ShallowEqual( b ));
  544. }
  545. XMLTest( "Clone and Equal", 4, count );
  546. }
  547. #if defined( _MSC_VER )
  548. _CrtMemCheckpoint( &endMemState );
  549. //_CrtMemDumpStatistics( &endMemState );
  550. _CrtMemState diffMemState;
  551. _CrtMemDifference( &diffMemState, &startMemState, &endMemState );
  552. _CrtMemDumpStatistics( &diffMemState );
  553. //printf( "new total=%d\n", gNewTotal );
  554. #endif
  555. printf ("\nPass %d, Fail %d\n", gPass, gFail);
  556. return 0;
  557. }