1
0

xmltest.cpp 22 KB

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