xmltest.cpp 23 KB

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