xmltest.cpp 23 KB

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