xmltest.cpp 27 KB

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