xmltest.cpp 30 KB

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