xmltest.cpp 22 KB

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