1
0

xmltest.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  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 match test", ele->Attribute( "str", "strValue" ), "strValue" );
  270. XMLTest( "Attribute round trip. c-string.", "strValue", cStr );
  271. XMLTest( "Attribute round trip. int.", 1, iVal );
  272. XMLTest( "Attribute round trip. double.", -1, (int)dVal );
  273. }
  274. {
  275. XMLDocument doc;
  276. doc.LoadFile( "utf8test.xml" );
  277. // Get the attribute "value" from the "Russian" element and check it.
  278. XMLElement* element = doc.FirstChildElement( "document" )->FirstChildElement( "Russian" );
  279. const unsigned char correctValue[] = { 0xd1U, 0x86U, 0xd0U, 0xb5U, 0xd0U, 0xbdU, 0xd0U, 0xbdU,
  280. 0xd0U, 0xbeU, 0xd1U, 0x81U, 0xd1U, 0x82U, 0xd1U, 0x8cU, 0 };
  281. XMLTest( "UTF-8: Russian value.", (const char*)correctValue, element->Attribute( "value" ) );
  282. const unsigned char russianElementName[] = { 0xd0U, 0xa0U, 0xd1U, 0x83U,
  283. 0xd1U, 0x81U, 0xd1U, 0x81U,
  284. 0xd0U, 0xbaU, 0xd0U, 0xb8U,
  285. 0xd0U, 0xb9U, 0 };
  286. const char russianText[] = "<\xD0\xB8\xD0\xBC\xD0\xB5\xD0\xB5\xD1\x82>";
  287. XMLText* text = doc.FirstChildElement( "document" )->FirstChildElement( (const char*) russianElementName )->FirstChild()->ToText();
  288. XMLTest( "UTF-8: Browsing russian element name.",
  289. russianText,
  290. text->Value() );
  291. // Now try for a round trip.
  292. doc.SaveFile( "utf8testout.xml" );
  293. // Check the round trip.
  294. char savedBuf[256];
  295. char verifyBuf[256];
  296. int okay = 0;
  297. #if defined(_MSC_VER)
  298. #pragma warning ( push )
  299. #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
  300. #endif
  301. FILE* saved = fopen( "utf8testout.xml", "r" );
  302. FILE* verify = fopen( "utf8testverify.xml", "r" );
  303. #if defined(_MSC_VER)
  304. #pragma warning ( pop )
  305. #endif
  306. if ( saved && verify )
  307. {
  308. okay = 1;
  309. while ( fgets( verifyBuf, 256, verify ) )
  310. {
  311. fgets( savedBuf, 256, saved );
  312. NullLineEndings( verifyBuf );
  313. NullLineEndings( savedBuf );
  314. if ( strcmp( verifyBuf, savedBuf ) )
  315. {
  316. printf( "verify:%s<\n", verifyBuf );
  317. printf( "saved :%s<\n", savedBuf );
  318. okay = 0;
  319. break;
  320. }
  321. }
  322. }
  323. if ( saved )
  324. fclose( saved );
  325. if ( verify )
  326. fclose( verify );
  327. XMLTest( "UTF-8: Verified multi-language round trip.", 1, okay );
  328. }
  329. // --------GetText()-----------
  330. {
  331. const char* str = "<foo>This is text</foo>";
  332. XMLDocument doc;
  333. doc.Parse( str );
  334. const XMLElement* element = doc.RootElement();
  335. XMLTest( "GetText() normal use.", "This is text", element->GetText() );
  336. str = "<foo><b>This is text</b></foo>";
  337. doc.Parse( str );
  338. element = doc.RootElement();
  339. XMLTest( "GetText() contained element.", element->GetText() == 0, true );
  340. }
  341. // ---------- CDATA ---------------
  342. {
  343. const char* str = "<xmlElement>"
  344. "<![CDATA["
  345. "I am > the rules!\n"
  346. "...since I make symbolic puns"
  347. "]]>"
  348. "</xmlElement>";
  349. XMLDocument doc;
  350. doc.Parse( str );
  351. doc.Print();
  352. XMLTest( "CDATA parse.", doc.FirstChildElement()->FirstChild()->Value(),
  353. "I am > the rules!\n...since I make symbolic puns",
  354. false );
  355. }
  356. // ----------- CDATA -------------
  357. {
  358. const char* str = "<xmlElement>"
  359. "<![CDATA["
  360. "<b>I am > the rules!</b>\n"
  361. "...since I make symbolic puns"
  362. "]]>"
  363. "</xmlElement>";
  364. XMLDocument doc;
  365. doc.Parse( str );
  366. doc.Print();
  367. XMLTest( "CDATA parse. [ tixml1:1480107 ]", doc.FirstChildElement()->FirstChild()->Value(),
  368. "<b>I am > the rules!</b>\n...since I make symbolic puns",
  369. false );
  370. }
  371. // InsertAfterChild causes crash.
  372. {
  373. // InsertBeforeChild and InsertAfterChild causes crash.
  374. XMLDocument doc;
  375. XMLElement* parent = doc.NewElement( "Parent" );
  376. doc.InsertFirstChild( parent );
  377. XMLElement* childText0 = doc.NewElement( "childText0" );
  378. XMLElement* childText1 = doc.NewElement( "childText1" );
  379. XMLNode* childNode0 = parent->InsertEndChild( childText0 );
  380. XMLNode* childNode1 = parent->InsertAfterChild( childNode0, childText1 );
  381. XMLTest( "Test InsertAfterChild on empty node. ", ( childNode1 == parent->LastChild() ), true );
  382. }
  383. {
  384. // Entities not being written correctly.
  385. // From Lynn Allen
  386. const char* passages =
  387. "<?xml version=\"1.0\" standalone=\"no\" ?>"
  388. "<passages count=\"006\" formatversion=\"20020620\">"
  389. "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
  390. " It also has &lt;, &gt;, and &amp;, as well as a fake copyright &#xA9;.\"> </psg>"
  391. "</passages>";
  392. XMLDocument doc;
  393. doc.Parse( passages );
  394. XMLElement* psg = doc.RootElement()->FirstChildElement();
  395. const char* context = psg->Attribute( "context" );
  396. const char* expected = "Line 5 has \"quotation marks\" and 'apostrophe marks'. It also has <, >, and &, as well as a fake copyright \xC2\xA9.";
  397. XMLTest( "Entity transformation: read. ", expected, context, true );
  398. #if defined(_MSC_VER)
  399. #pragma warning ( push )
  400. #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
  401. #endif
  402. FILE* textfile = fopen( "textfile.txt", "w" );
  403. #if defined(_MSC_VER)
  404. #pragma warning ( pop )
  405. #endif
  406. if ( textfile )
  407. {
  408. XMLPrinter streamer( textfile );
  409. psg->Accept( &streamer );
  410. fclose( textfile );
  411. }
  412. #if defined(_MSC_VER)
  413. #pragma warning ( push )
  414. #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
  415. #endif
  416. textfile = fopen( "textfile.txt", "r" );
  417. #if defined(_MSC_VER)
  418. #pragma warning ( pop )
  419. #endif
  420. TIXMLASSERT( textfile );
  421. if ( textfile )
  422. {
  423. char buf[ 1024 ];
  424. fgets( buf, 1024, textfile );
  425. XMLTest( "Entity transformation: write. ",
  426. "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
  427. " It also has &lt;, &gt;, and &amp;, as well as a fake copyright \xC2\xA9.\"/>\n",
  428. buf, false );
  429. }
  430. fclose( textfile );
  431. }
  432. {
  433. // Suppress entities.
  434. const char* passages =
  435. "<?xml version=\"1.0\" standalone=\"no\" ?>"
  436. "<passages count=\"006\" formatversion=\"20020620\">"
  437. "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;.\">Crazy &ttk;</psg>"
  438. "</passages>";
  439. XMLDocument doc( false );
  440. doc.Parse( passages );
  441. XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->Attribute( "context" ),
  442. "Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;." );
  443. XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->FirstChild()->Value(),
  444. "Crazy &ttk;" );
  445. doc.Print();
  446. }
  447. {
  448. const char* test = "<?xml version='1.0'?><a.elem xmi.version='2.0'/>";
  449. XMLDocument doc;
  450. doc.Parse( test );
  451. XMLTest( "dot in names", doc.Error(), 0);
  452. XMLTest( "dot in names", doc.FirstChildElement()->Name(), "a.elem" );
  453. XMLTest( "dot in names", doc.FirstChildElement()->Attribute( "xmi.version" ), "2.0" );
  454. }
  455. {
  456. const char* test = "<element><Name>1.1 Start easy ignore fin thickness&#xA;</Name></element>";
  457. XMLDocument doc;
  458. doc.Parse( test );
  459. XMLText* text = doc.FirstChildElement()->FirstChildElement()->FirstChild()->ToText();
  460. XMLTest( "Entity with one digit.",
  461. text->Value(), "1.1 Start easy ignore fin thickness\n",
  462. false );
  463. }
  464. {
  465. // DOCTYPE not preserved (950171)
  466. //
  467. const char* doctype =
  468. "<?xml version=\"1.0\" ?>"
  469. "<!DOCTYPE PLAY SYSTEM 'play.dtd'>"
  470. "<!ELEMENT title (#PCDATA)>"
  471. "<!ELEMENT books (title,authors)>"
  472. "<element />";
  473. XMLDocument doc;
  474. doc.Parse( doctype );
  475. doc.SaveFile( "test7.xml" );
  476. doc.DeleteChild( doc.RootElement() );
  477. doc.LoadFile( "test7.xml" );
  478. doc.Print();
  479. const XMLUnknown* decl = doc.FirstChild()->NextSibling()->ToUnknown();
  480. XMLTest( "Correct value of unknown.", "DOCTYPE PLAY SYSTEM 'play.dtd'", decl->Value() );
  481. }
  482. {
  483. // Comments do not stream out correctly.
  484. const char* doctype =
  485. "<!-- Somewhat<evil> -->";
  486. XMLDocument doc;
  487. doc.Parse( doctype );
  488. XMLComment* comment = doc.FirstChild()->ToComment();
  489. XMLTest( "Comment formatting.", " Somewhat<evil> ", comment->Value() );
  490. }
  491. {
  492. // Double attributes
  493. const char* doctype = "<element attr='red' attr='blue' />";
  494. XMLDocument doc;
  495. doc.Parse( doctype );
  496. XMLTest( "Parsing repeated attributes.", XML_ERROR_PARSING_ATTRIBUTE, doc.ErrorID() ); // is an error to tinyxml (didn't use to be, but caused issues)
  497. doc.PrintError();
  498. }
  499. {
  500. // Embedded null in stream.
  501. const char* doctype = "<element att\0r='red' attr='blue' />";
  502. XMLDocument doc;
  503. doc.Parse( doctype );
  504. XMLTest( "Embedded null throws error.", true, doc.Error() );
  505. }
  506. {
  507. // Empty documents should return TIXML_XML_ERROR_PARSING_EMPTY, bug 1070717
  508. const char* str = " ";
  509. XMLDocument doc;
  510. doc.Parse( str );
  511. XMLTest( "Empty document error", XML_ERROR_EMPTY_DOCUMENT, doc.ErrorID() );
  512. }
  513. {
  514. // Low entities
  515. XMLDocument doc;
  516. doc.Parse( "<test>&#x0e;</test>" );
  517. const char result[] = { 0x0e, 0 };
  518. XMLTest( "Low entities.", doc.FirstChildElement()->GetText(), result );
  519. doc.Print();
  520. }
  521. {
  522. // Attribute values with trailing quotes not handled correctly
  523. XMLDocument doc;
  524. doc.Parse( "<foo attribute=bar\" />" );
  525. XMLTest( "Throw error with bad end quotes.", doc.Error(), true );
  526. }
  527. {
  528. // [ 1663758 ] Failure to report error on bad XML
  529. XMLDocument xml;
  530. xml.Parse("<x>");
  531. XMLTest("Missing end tag at end of input", xml.Error(), true);
  532. xml.Parse("<x> ");
  533. XMLTest("Missing end tag with trailing whitespace", xml.Error(), true);
  534. xml.Parse("<x></y>");
  535. XMLTest("Mismatched tags", xml.ErrorID(), XML_ERROR_MISMATCHED_ELEMENT);
  536. }
  537. {
  538. // [ 1475201 ] TinyXML parses entities in comments
  539. XMLDocument xml;
  540. xml.Parse("<!-- declarations for <head> & <body> -->"
  541. "<!-- far &amp; away -->" );
  542. XMLNode* e0 = xml.FirstChild();
  543. XMLNode* e1 = e0->NextSibling();
  544. XMLComment* c0 = e0->ToComment();
  545. XMLComment* c1 = e1->ToComment();
  546. XMLTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
  547. XMLTest( "Comments ignore entities.", " far &amp; away ", c1->Value(), true );
  548. }
  549. {
  550. XMLDocument xml;
  551. xml.Parse( "<Parent>"
  552. "<child1 att=''/>"
  553. "<!-- With this comment, child2 will not be parsed! -->"
  554. "<child2 att=''/>"
  555. "</Parent>" );
  556. xml.Print();
  557. int count = 0;
  558. for( XMLNode* ele = xml.FirstChildElement( "Parent" )->FirstChild();
  559. ele;
  560. ele = ele->NextSibling() )
  561. {
  562. ++count;
  563. }
  564. XMLTest( "Comments iterate correctly.", 3, count );
  565. }
  566. {
  567. // trying to repro ]1874301]. If it doesn't go into an infinite loop, all is well.
  568. unsigned char buf[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?><feed><![CDATA[Test XMLblablablalblbl";
  569. buf[60] = 239;
  570. buf[61] = 0;
  571. XMLDocument doc;
  572. doc.Parse( (const char*)buf);
  573. }
  574. {
  575. // bug 1827248 Error while parsing a little bit malformed file
  576. // Actually not malformed - should work.
  577. XMLDocument xml;
  578. xml.Parse( "<attributelist> </attributelist >" );
  579. XMLTest( "Handle end tag whitespace", false, xml.Error() );
  580. }
  581. {
  582. // This one must not result in an infinite loop
  583. XMLDocument xml;
  584. xml.Parse( "<infinite>loop" );
  585. XMLTest( "Infinite loop test.", true, true );
  586. }
  587. #endif
  588. {
  589. const char* pub = "<?xml version='1.0'?> <element><sub/></element> <!--comment--> <!DOCTYPE>";
  590. XMLDocument doc;
  591. doc.Parse( pub );
  592. XMLDocument clone;
  593. for( const XMLNode* node=doc.FirstChild(); node; node=node->NextSibling() ) {
  594. XMLNode* copy = node->ShallowClone( &clone );
  595. clone.InsertEndChild( copy );
  596. }
  597. clone.Print();
  598. int count=0;
  599. const XMLNode* a=clone.FirstChild();
  600. const XMLNode* b=doc.FirstChild();
  601. for( ; a && b; a=a->NextSibling(), b=b->NextSibling() ) {
  602. ++count;
  603. XMLTest( "Clone and Equal", true, a->ShallowEqual( b ));
  604. }
  605. XMLTest( "Clone and Equal", 4, count );
  606. }
  607. // -------- Handles ------------
  608. {
  609. static const char* xml = "<element attrib='bar'><sub>Text</sub></element>";
  610. XMLDocument doc;
  611. doc.Parse( xml );
  612. const XMLDocument& docC = doc;
  613. XMLElement* ele = XMLHandle( doc ).FirstChildElement( "element" ).FirstChild().ToElement();
  614. XMLTest( "Handle, success, mutable", ele->Value(), "sub" );
  615. const XMLElement* eleC = XMLHandle( docC ).FirstChildElement( "element" ).FirstChild().ToElement();
  616. XMLTest( "Handle, success, mutable", ele->Value(), "sub" );
  617. }
  618. // ----------- Performance tracking --------------
  619. {
  620. #if defined( _MSC_VER )
  621. __int64 start, end, freq;
  622. QueryPerformanceFrequency( (LARGE_INTEGER*) &freq );
  623. #endif
  624. #if defined(_MSC_VER)
  625. #pragma warning ( push )
  626. #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
  627. #endif
  628. FILE* fp = fopen( "dream.xml", "r" );
  629. #if defined(_MSC_VER)
  630. #pragma warning ( pop )
  631. #endif
  632. fseek( fp, 0, SEEK_END );
  633. long size = ftell( fp );
  634. fseek( fp, 0, SEEK_SET );
  635. char* mem = new char[size+1];
  636. fread( mem, size, 1, fp );
  637. fclose( fp );
  638. mem[size] = 0;
  639. #if defined( _MSC_VER )
  640. QueryPerformanceCounter( (LARGE_INTEGER*) &start );
  641. #else
  642. clock_t cstart = clock();
  643. #endif
  644. static const int COUNT = 10;
  645. for( int i=0; i<COUNT; ++i ) {
  646. XMLDocument doc;
  647. doc.Parse( mem );
  648. }
  649. #if defined( _MSC_VER )
  650. QueryPerformanceCounter( (LARGE_INTEGER*) &end );
  651. #else
  652. clock_t cend = clock();
  653. #endif
  654. delete [] mem;
  655. static const char* note =
  656. #ifdef DEBUG
  657. "DEBUG";
  658. #else
  659. "Release";
  660. #endif
  661. #if defined( _MSC_VER )
  662. printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, 1000.0 * (double)(end-start) / ( (double)freq * (double)COUNT) );
  663. #else
  664. printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, (double)(cend - cstart)/(double)COUNT );
  665. #endif
  666. }
  667. #if defined( _MSC_VER ) && defined( DEBUG )
  668. _CrtMemCheckpoint( &endMemState );
  669. //_CrtMemDumpStatistics( &endMemState );
  670. _CrtMemState diffMemState;
  671. _CrtMemDifference( &diffMemState, &startMemState, &endMemState );
  672. _CrtMemDumpStatistics( &diffMemState );
  673. //printf( "new total=%d\n", gNewTotal );
  674. #endif
  675. printf ("\nPass %d, Fail %d\n", gPass, gFail);
  676. return 0;
  677. }