xmltest.cpp 23 KB

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