blob: 93b55999ad6dec98e74db50b20668f432874f761 [file] [log] [blame]
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001#include "tinyxml2.h"
2
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -07003#include <cstdlib>
4#include <cstring>
5#include <ctime>
U-Lama\Leee13c3e62011-12-28 14:36:55 -08006
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08007#if defined( _MSC_VER )
Lee Thomason1ff38e02012-02-14 18:18:16 -08008 #include <crtdbg.h>
Lee Thomason6f381b72012-03-02 12:59:39 -08009 #define WIN32_LEAN_AND_MEAN
10 #include <windows.h>
Lee Thomason1ff38e02012-02-14 18:18:16 -080011 _CrtMemState startMemState;
12 _CrtMemState endMemState;
13#endif
Lee Thomasone9ecdab2012-02-13 18:11:20 -080014
U-Lama\Leee13c3e62011-12-28 14:36:55 -080015using namespace tinyxml2;
Lee Thomasonec5a7b42012-02-13 18:16:52 -080016int gPass = 0;
17int gFail = 0;
18
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -080019
U-Stream\Lee09a11c52012-02-17 08:31:16 -080020bool XMLTest (const char* testString, const char* expected, const char* found, bool echo=true )
Lee Thomason1ff38e02012-02-14 18:18:16 -080021{
22 bool pass = !strcmp( expected, found );
23 if ( pass )
24 printf ("[pass]");
25 else
26 printf ("[fail]");
27
U-Stream\Lee09a11c52012-02-17 08:31:16 -080028 if ( !echo )
Lee Thomason1ff38e02012-02-14 18:18:16 -080029 printf (" %s\n", testString);
30 else
31 printf (" %s [%s][%s]\n", testString, expected, found);
32
33 if ( pass )
34 ++gPass;
35 else
36 ++gFail;
37 return pass;
38}
39
40
U-Stream\Lee09a11c52012-02-17 08:31:16 -080041bool XMLTest( const char* testString, int expected, int found, bool echo=true )
Lee Thomason1ff38e02012-02-14 18:18:16 -080042{
43 bool pass = ( expected == found );
44 if ( pass )
45 printf ("[pass]");
46 else
47 printf ("[fail]");
48
U-Stream\Lee09a11c52012-02-17 08:31:16 -080049 if ( !echo )
Lee Thomason1ff38e02012-02-14 18:18:16 -080050 printf (" %s\n", testString);
51 else
52 printf (" %s [%d][%d]\n", testString, expected, found);
53
54 if ( pass )
55 ++gPass;
56 else
57 ++gFail;
58 return pass;
59}
Lee Thomasonec5a7b42012-02-13 18:16:52 -080060
U-Lama\Leee13c3e62011-12-28 14:36:55 -080061
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -080062void NullLineEndings( char* p )
63{
64 while( p && *p ) {
65 if ( *p == '\n' || *p == '\r' ) {
66 *p = 0;
67 return;
68 }
69 ++p;
70 }
71}
72
73
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -070074int main( int /*argc*/, const char ** /*argv*/ )
U-Lama\Leee13c3e62011-12-28 14:36:55 -080075{
Lee Thomason (grinliz)0a4df402012-02-27 20:50:52 -080076 #if defined( _MSC_VER ) && defined( DEBUG )
Lee Thomason1ff38e02012-02-14 18:18:16 -080077 _CrtMemCheckpoint( &startMemState );
78 #endif
Lee Thomason8a5dfee2012-01-18 17:43:40 -080079
Lee Thomason7f7b1622012-03-24 12:49:03 -070080 #if defined(_MSC_VER)
81 #pragma warning ( push )
82 #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
83 #endif
84
85 FILE* fp = fopen( "dream.xml", "r" );
86 if ( !fp ) {
87 printf( "Error opening test file 'dream.xml'.\n"
88 "Is your working directory the same as where \n"
89 "the xmltest.cpp and dream.xml file are?\n\n"
90 #if defined( _MSC_VER )
91 "In windows Visual Studio you may need to set\n"
92 "Properties->Debugging->Working Directory to '..'\n"
93 #endif
94 );
95 exit( 1 );
96 }
97 fclose( fp );
98
99 #if defined(_MSC_VER)
100 #pragma warning ( pop )
101 #endif
102
Lee Thomason87e475a2012-03-20 11:55:29 -0700103 /* ------ Example 1: Load and parse an XML file. ---- */
104 {
105 XMLDocument doc;
106 doc.LoadFile( "dream.xml" );
107 }
108
109 /* ------ Example 2: Lookup information. ---- */
110 {
111 XMLDocument doc;
112 doc.LoadFile( "dream.xml" );
113
114 // Structure of the XML file:
115 // - Element "PLAY" the root Element
116 // - - Element "TITLE" child of the root PLAY Element
117 // - - - Text child of the TITLE Element
118
119 // Navigate to the title, using the convenience function, with a dangerous lack of error checking.
120 const char* title = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" )->GetText();
121 printf( "Name of play (1): %s\n", title );
122
123 // Text is just another Node to TinyXML-2. The more general way to get to the XMLText:
124 XMLText* textNode = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" )->FirstChild()->ToText();
125 title = textNode->Value();
126 printf( "Name of play (2): %s\n", title );
127 }
128
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800129 {
Lee Thomason43f59302012-02-06 18:18:11 -0800130 static const char* test[] = { "<element />",
131 "<element></element>",
132 "<element><subelement/></element>",
133 "<element><subelement></subelement></element>",
134 "<element><subelement><subsub/></subelement></element>",
135 "<!--comment beside elements--><element><subelement></subelement></element>",
136 "<!--comment beside elements, this time with spaces--> \n <element> <subelement> \n </subelement> </element>",
137 "<element attrib1='foo' attrib2=\"bar\" ></element>",
138 "<element attrib1='foo' attrib2=\"bar\" ><subelement attrib3='yeehaa' /></element>",
139 "<element>Text inside element.</element>",
140 "<element><b></b></element>",
141 "<element>Text inside and <b>bolded</b> in the element.</element>",
142 "<outer><element>Text inside and <b>bolded</b> in the element.</element></outer>",
Lee Thomason8ee79892012-01-25 17:44:30 -0800143 "<element>This &amp; That.</element>",
Lee Thomason18d68bd2012-01-26 18:17:26 -0800144 "<element attrib='This&lt;That' />",
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800145 0
146 };
Lee Thomason6ee99fc2012-01-21 18:45:16 -0800147 for( int i=0; test[i]; ++i ) {
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800148 XMLDocument doc;
Lee Thomason6ee99fc2012-01-21 18:45:16 -0800149 doc.Parse( test[i] );
Lee Thomason5cae8972012-01-24 18:03:07 -0800150 doc.Print();
Lee Thomasonec975ce2012-01-23 11:42:06 -0800151 printf( "----------------------------------------------\n" );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800152 }
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800153 }
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800154#if 1
Lee Thomasond6277762012-02-22 16:00:12 -0800155 {
156 static const char* test = "<!--hello world\n"
157 " line 2\r"
158 " line 3\r\n"
159 " line 4\n\r"
160 " line 5\r-->";
161
162 XMLDocument doc;
163 doc.Parse( test );
164 doc.Print();
165 }
166
Lee Thomason2c85a712012-01-31 08:24:24 -0800167 {
168 static const char* test = "<element>Text before.</element>";
169 XMLDocument doc;
170 doc.Parse( test );
171 XMLElement* root = doc.FirstChildElement();
172 XMLElement* newElement = doc.NewElement( "Subelement" );
173 root->InsertEndChild( newElement );
174 doc.Print();
175 }
Lee Thomasond1983222012-02-06 08:41:24 -0800176 {
177 XMLDocument* doc = new XMLDocument();
178 static const char* test = "<element><sub/></element>";
179 doc->Parse( test );
180 delete doc;
181 }
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800182 {
Lee Thomason1ff38e02012-02-14 18:18:16 -0800183 // Test: Programmatic DOM
Lee Thomasonec5a7b42012-02-13 18:16:52 -0800184 // Build:
185 // <element>
186 // <!--comment-->
187 // <sub attrib="1" />
188 // <sub attrib="2" />
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800189 // <sub attrib="3" >& Text!</sub>
Lee Thomasonec5a7b42012-02-13 18:16:52 -0800190 // <element>
191
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800192 XMLDocument* doc = new XMLDocument();
Lee Thomason1ff38e02012-02-14 18:18:16 -0800193 XMLNode* element = doc->InsertEndChild( doc->NewElement( "element" ) );
194
195 XMLElement* sub[3] = { doc->NewElement( "sub" ), doc->NewElement( "sub" ), doc->NewElement( "sub" ) };
196 for( int i=0; i<3; ++i ) {
197 sub[i]->SetAttribute( "attrib", i );
198 }
199 element->InsertEndChild( sub[2] );
200 XMLNode* comment = element->InsertFirstChild( doc->NewComment( "comment" ) );
201 element->InsertAfterChild( comment, sub[0] );
202 element->InsertAfterChild( sub[0], sub[1] );
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800203 sub[2]->InsertFirstChild( doc->NewText( "& Text!" ));
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800204 doc->Print();
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800205 XMLTest( "Programmatic DOM", "comment", doc->FirstChildElement( "element" )->FirstChild()->Value() );
206 XMLTest( "Programmatic DOM", "0", doc->FirstChildElement( "element" )->FirstChildElement()->Attribute( "attrib" ) );
207 XMLTest( "Programmatic DOM", 2, doc->FirstChildElement()->LastChildElement( "sub" )->IntAttribute( "attrib" ) );
208 XMLTest( "Programmatic DOM", "& Text!",
209 doc->FirstChildElement()->LastChildElement( "sub" )->FirstChild()->ToText()->Value() );
U-Stream\Leeae25a442012-02-17 17:48:16 -0800210
211 // And now deletion:
212 element->DeleteChild( sub[2] );
213 doc->DeleteNode( comment );
214
215 element->FirstChildElement()->SetAttribute( "attrib", true );
216 element->LastChildElement()->DeleteAttribute( "attrib" );
217
218 XMLTest( "Programmatic DOM", true, doc->FirstChildElement()->FirstChildElement()->BoolAttribute( "attrib" ) );
219 int value = 10;
220 int result = doc->FirstChildElement()->LastChildElement()->QueryIntAttribute( "attrib", &value );
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700221 XMLTest( "Programmatic DOM", result, XML_NO_ATTRIBUTE );
U-Stream\Leeae25a442012-02-17 17:48:16 -0800222 XMLTest( "Programmatic DOM", value, 10 );
223
224 doc->Print();
225
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800226 XMLPrinter streamer;
U-Stream\Leeae25a442012-02-17 17:48:16 -0800227 doc->Print( &streamer );
228 printf( "%s", streamer.CStr() );
229
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800230 delete doc;
231 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800232 {
233 // Test: Dream
234 // XML1 : 1,187,569 bytes in 31,209 allocations
235 // XML2 : 469,073 bytes in 323 allocations
236 //int newStart = gNew;
237 XMLDocument doc;
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800238 doc.LoadFile( "dream.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800239
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800240 doc.SaveFile( "dreamout.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800241 doc.PrintError();
242
243 XMLTest( "Dream", "xml version=\"1.0\"",
244 doc.FirstChild()->ToDeclaration()->Value() );
245 XMLTest( "Dream", true, doc.FirstChild()->NextSibling()->ToUnknown() ? true : false );
246 XMLTest( "Dream", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
247 doc.FirstChild()->NextSibling()->ToUnknown()->Value() );
248 XMLTest( "Dream", "And Robin shall restore amends.",
249 doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
250 XMLTest( "Dream", "And Robin shall restore amends.",
251 doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
252
253 XMLDocument doc2;
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800254 doc2.LoadFile( "dreamout.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800255 XMLTest( "Dream-out", "xml version=\"1.0\"",
256 doc2.FirstChild()->ToDeclaration()->Value() );
257 XMLTest( "Dream-out", true, doc2.FirstChild()->NextSibling()->ToUnknown() ? true : false );
258 XMLTest( "Dream-out", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
259 doc2.FirstChild()->NextSibling()->ToUnknown()->Value() );
260 XMLTest( "Dream-out", "And Robin shall restore amends.",
261 doc2.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
262
263 //gNewTotal = gNew - newStart;
264 }
Lee Thomason6f381b72012-03-02 12:59:39 -0800265
266
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800267 {
268 const char* error = "<?xml version=\"1.0\" standalone=\"no\" ?>\n"
269 "<passages count=\"006\" formatversion=\"20020620\">\n"
270 " <wrong error>\n"
271 "</passages>";
272
273 XMLDocument doc;
274 doc.Parse( error );
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700275 XMLTest( "Bad XML", doc.ErrorID(), XML_ERROR_PARSING_ATTRIBUTE );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800276 }
277
278 {
279 const char* str = "<doc attr0='1' attr1='2.0' attr2='foo' />";
280
281 XMLDocument doc;
282 doc.Parse( str );
283
284 XMLElement* ele = doc.FirstChildElement();
285
286 int iVal, result;
287 double dVal;
288
289 result = ele->QueryDoubleAttribute( "attr0", &dVal );
290 XMLTest( "Query attribute: int as double", result, 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, XML_NO_ERROR );
296 XMLTest( "Query attribute: double as int", iVal, 2 );
297 result = ele->QueryIntAttribute( "attr2", &iVal );
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700298 XMLTest( "Query attribute: not a number", result, XML_WRONG_ATTRIBUTE_TYPE );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800299 result = ele->QueryIntAttribute( "bar", &iVal );
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700300 XMLTest( "Query attribute: does not exist", result, XML_NO_ATTRIBUTE );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800301 }
302
303 {
304 const char* str = "<doc/>";
305
306 XMLDocument doc;
307 doc.Parse( str );
308
309 XMLElement* ele = doc.FirstChildElement();
310
311 int iVal;
312 double dVal;
313
314 ele->SetAttribute( "str", "strValue" );
315 ele->SetAttribute( "int", 1 );
316 ele->SetAttribute( "double", -1.0 );
317
318 const char* cStr = ele->Attribute( "str" );
319 ele->QueryIntAttribute( "int", &iVal );
320 ele->QueryDoubleAttribute( "double", &dVal );
321
322 XMLTest( "Attribute round trip. c-string.", "strValue", cStr );
323 XMLTest( "Attribute round trip. int.", 1, iVal );
324 XMLTest( "Attribute round trip. double.", -1, (int)dVal );
325 }
326
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800327 {
328 XMLDocument doc;
329 doc.LoadFile( "utf8test.xml" );
330
331 // Get the attribute "value" from the "Russian" element and check it.
332 XMLElement* element = doc.FirstChildElement( "document" )->FirstChildElement( "Russian" );
333 const unsigned char correctValue[] = { 0xd1U, 0x86U, 0xd0U, 0xb5U, 0xd0U, 0xbdU, 0xd0U, 0xbdU,
334 0xd0U, 0xbeU, 0xd1U, 0x81U, 0xd1U, 0x82U, 0xd1U, 0x8cU, 0 };
335
336 XMLTest( "UTF-8: Russian value.", (const char*)correctValue, element->Attribute( "value" ) );
337
338 const unsigned char russianElementName[] = { 0xd0U, 0xa0U, 0xd1U, 0x83U,
339 0xd1U, 0x81U, 0xd1U, 0x81U,
340 0xd0U, 0xbaU, 0xd0U, 0xb8U,
341 0xd0U, 0xb9U, 0 };
342 const char russianText[] = "<\xD0\xB8\xD0\xBC\xD0\xB5\xD0\xB5\xD1\x82>";
343
344 XMLText* text = doc.FirstChildElement( "document" )->FirstChildElement( (const char*) russianElementName )->FirstChild()->ToText();
345 XMLTest( "UTF-8: Browsing russian element name.",
346 russianText,
347 text->Value() );
348
349 // Now try for a round trip.
350 doc.SaveFile( "utf8testout.xml" );
351
352 // Check the round trip.
353 char savedBuf[256];
354 char verifyBuf[256];
355 int okay = 0;
356
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800357
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800358#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800359#pragma warning ( push )
360#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800361#endif
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800362 FILE* saved = fopen( "utf8testout.xml", "r" );
363 FILE* verify = fopen( "utf8testverify.xml", "r" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800364#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800365#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800366#endif
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800367
368 if ( saved && verify )
369 {
370 okay = 1;
371 while ( fgets( verifyBuf, 256, verify ) )
372 {
373 fgets( savedBuf, 256, saved );
374 NullLineEndings( verifyBuf );
375 NullLineEndings( savedBuf );
376
377 if ( strcmp( verifyBuf, savedBuf ) )
378 {
379 printf( "verify:%s<\n", verifyBuf );
380 printf( "saved :%s<\n", savedBuf );
381 okay = 0;
382 break;
383 }
384 }
385 }
386 if ( saved )
387 fclose( saved );
388 if ( verify )
389 fclose( verify );
390 XMLTest( "UTF-8: Verified multi-language round trip.", 1, okay );
391 }
392
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800393 // --------GetText()-----------
394 {
395 const char* str = "<foo>This is text</foo>";
396 XMLDocument doc;
397 doc.Parse( str );
398 const XMLElement* element = doc.RootElement();
399
400 XMLTest( "GetText() normal use.", "This is text", element->GetText() );
401
402 str = "<foo><b>This is text</b></foo>";
403 doc.Parse( str );
404 element = doc.RootElement();
405
406 XMLTest( "GetText() contained element.", element->GetText() == 0, true );
407 }
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800408
Lee Thomasond6277762012-02-22 16:00:12 -0800409
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800410 // ---------- CDATA ---------------
411 {
412 const char* str = "<xmlElement>"
413 "<![CDATA["
414 "I am > the rules!\n"
415 "...since I make symbolic puns"
416 "]]>"
417 "</xmlElement>";
418 XMLDocument doc;
419 doc.Parse( str );
420 doc.Print();
Lee Thomasond6277762012-02-22 16:00:12 -0800421
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800422 XMLTest( "CDATA parse.", doc.FirstChildElement()->FirstChild()->Value(),
423 "I am > the rules!\n...since I make symbolic puns",
Lee Thomasond6277762012-02-22 16:00:12 -0800424 false );
425 }
426
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800427 // ----------- CDATA -------------
428 {
429 const char* str = "<xmlElement>"
430 "<![CDATA["
431 "<b>I am > the rules!</b>\n"
432 "...since I make symbolic puns"
433 "]]>"
434 "</xmlElement>";
435 XMLDocument doc;
436 doc.Parse( str );
437 doc.Print();
438
439 XMLTest( "CDATA parse. [ tixml1:1480107 ]", doc.FirstChildElement()->FirstChild()->Value(),
440 "<b>I am > the rules!</b>\n...since I make symbolic puns",
441 false );
442 }
443
444 // InsertAfterChild causes crash.
445 {
446 // InsertBeforeChild and InsertAfterChild causes crash.
447 XMLDocument doc;
448 XMLElement* parent = doc.NewElement( "Parent" );
449 doc.InsertFirstChild( parent );
450
451 XMLElement* childText0 = doc.NewElement( "childText0" );
452 XMLElement* childText1 = doc.NewElement( "childText1" );
453
454 XMLNode* childNode0 = parent->InsertEndChild( childText0 );
455 XMLNode* childNode1 = parent->InsertAfterChild( childNode0, childText1 );
456
457 XMLTest( "Test InsertAfterChild on empty node. ", ( childNode1 == parent->LastChild() ), true );
458 }
Lee Thomasond6277762012-02-22 16:00:12 -0800459
460 {
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800461 // Entities not being written correctly.
462 // From Lynn Allen
Lee Thomasond6277762012-02-22 16:00:12 -0800463
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800464 const char* passages =
465 "<?xml version=\"1.0\" standalone=\"no\" ?>"
466 "<passages count=\"006\" formatversion=\"20020620\">"
467 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
468 " It also has &lt;, &gt;, and &amp;, as well as a fake copyright &#xA9;.\"> </psg>"
469 "</passages>";
Lee Thomasond6277762012-02-22 16:00:12 -0800470
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800471 XMLDocument doc;
472 doc.Parse( passages );
473 XMLElement* psg = doc.RootElement()->FirstChildElement();
474 const char* context = psg->Attribute( "context" );
475 const char* expected = "Line 5 has \"quotation marks\" and 'apostrophe marks'. It also has <, >, and &, as well as a fake copyright \xC2\xA9.";
Lee Thomasond6277762012-02-22 16:00:12 -0800476
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800477 XMLTest( "Entity transformation: read. ", expected, context, true );
Lee Thomasond6277762012-02-22 16:00:12 -0800478
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800479#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800480#pragma warning ( push )
481#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800482#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800483 FILE* textfile = fopen( "textfile.txt", "w" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800484#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800485#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800486#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800487 if ( textfile )
488 {
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800489 XMLPrinter streamer( textfile );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800490 psg->Accept( &streamer );
491 fclose( textfile );
492 }
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800493#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800494#pragma warning ( push )
495#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800496#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800497 textfile = fopen( "textfile.txt", "r" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800498#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800499#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800500#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800501 TIXMLASSERT( textfile );
502 if ( textfile )
503 {
504 char buf[ 1024 ];
505 fgets( buf, 1024, textfile );
506 XMLTest( "Entity transformation: write. ",
507 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
508 " It also has &lt;, &gt;, and &amp;, as well as a fake copyright \xC2\xA9.\"/>\n",
509 buf, false );
510 }
511 fclose( textfile );
512 }
513
514 {
Lee Thomason6f381b72012-03-02 12:59:39 -0800515 // Suppress entities.
516 const char* passages =
517 "<?xml version=\"1.0\" standalone=\"no\" ?>"
518 "<passages count=\"006\" formatversion=\"20020620\">"
519 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;.\">Crazy &ttk;</psg>"
520 "</passages>";
521
522 XMLDocument doc( false );
523 doc.Parse( passages );
524
525 XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->Attribute( "context" ),
526 "Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;." );
527 XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->FirstChild()->Value(),
528 "Crazy &ttk;" );
529 doc.Print();
530 }
531
532 {
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800533 const char* test = "<?xml version='1.0'?><a.elem xmi.version='2.0'/>";
534
535 XMLDocument doc;
536 doc.Parse( test );
537 XMLTest( "dot in names", doc.Error(), 0);
538 XMLTest( "dot in names", doc.FirstChildElement()->Name(), "a.elem" );
539 XMLTest( "dot in names", doc.FirstChildElement()->Attribute( "xmi.version" ), "2.0" );
540 }
541
542 {
543 const char* test = "<element><Name>1.1 Start easy ignore fin thickness&#xA;</Name></element>";
544
545 XMLDocument doc;
546 doc.Parse( test );
547
548 XMLText* text = doc.FirstChildElement()->FirstChildElement()->FirstChild()->ToText();
549 XMLTest( "Entity with one digit.",
550 text->Value(), "1.1 Start easy ignore fin thickness\n",
551 false );
552 }
553
554 {
555 // DOCTYPE not preserved (950171)
556 //
557 const char* doctype =
558 "<?xml version=\"1.0\" ?>"
559 "<!DOCTYPE PLAY SYSTEM 'play.dtd'>"
560 "<!ELEMENT title (#PCDATA)>"
561 "<!ELEMENT books (title,authors)>"
562 "<element />";
563
564 XMLDocument doc;
565 doc.Parse( doctype );
566 doc.SaveFile( "test7.xml" );
567 doc.DeleteChild( doc.RootElement() );
568 doc.LoadFile( "test7.xml" );
569 doc.Print();
570
571 const XMLUnknown* decl = doc.FirstChild()->NextSibling()->ToUnknown();
572 XMLTest( "Correct value of unknown.", "DOCTYPE PLAY SYSTEM 'play.dtd'", decl->Value() );
573
574 }
575
576 {
577 // Comments do not stream out correctly.
578 const char* doctype =
579 "<!-- Somewhat<evil> -->";
580 XMLDocument doc;
581 doc.Parse( doctype );
582
583 XMLComment* comment = doc.FirstChild()->ToComment();
584
585 XMLTest( "Comment formatting.", " Somewhat<evil> ", comment->Value() );
586 }
587 {
588 // Double attributes
589 const char* doctype = "<element attr='red' attr='blue' />";
590
591 XMLDocument doc;
592 doc.Parse( doctype );
593
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700594 XMLTest( "Parsing repeated attributes.", XML_ERROR_PARSING_ATTRIBUTE, doc.ErrorID() ); // is an error to tinyxml (didn't use to be, but caused issues)
Lee Thomason (grinliz)0a4df402012-02-27 20:50:52 -0800595 doc.PrintError();
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800596 }
597
598 {
599 // Embedded null in stream.
600 const char* doctype = "<element att\0r='red' attr='blue' />";
601
602 XMLDocument doc;
603 doc.Parse( doctype );
604 XMLTest( "Embedded null throws error.", true, doc.Error() );
605 }
606
607 {
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700608 // Empty documents should return TIXML_XML_ERROR_PARSING_EMPTY, bug 1070717
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800609 const char* str = " ";
610 XMLDocument doc;
611 doc.Parse( str );
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700612 XMLTest( "Empty document error", XML_ERROR_EMPTY_DOCUMENT, doc.ErrorID() );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800613 }
614
615 {
616 // Low entities
617 XMLDocument doc;
618 doc.Parse( "<test>&#x0e;</test>" );
619 const char result[] = { 0x0e, 0 };
620 XMLTest( "Low entities.", doc.FirstChildElement()->GetText(), result );
621 doc.Print();
622 }
623
624 {
625 // Attribute values with trailing quotes not handled correctly
626 XMLDocument doc;
627 doc.Parse( "<foo attribute=bar\" />" );
628 XMLTest( "Throw error with bad end quotes.", doc.Error(), true );
629 }
630
631 {
632 // [ 1663758 ] Failure to report error on bad XML
633 XMLDocument xml;
634 xml.Parse("<x>");
635 XMLTest("Missing end tag at end of input", xml.Error(), true);
636 xml.Parse("<x> ");
637 XMLTest("Missing end tag with trailing whitespace", xml.Error(), true);
638 xml.Parse("<x></y>");
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700639 XMLTest("Mismatched tags", xml.ErrorID(), XML_ERROR_MISMATCHED_ELEMENT);
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800640 }
641
642
643 {
644 // [ 1475201 ] TinyXML parses entities in comments
645 XMLDocument xml;
646 xml.Parse("<!-- declarations for <head> & <body> -->"
647 "<!-- far &amp; away -->" );
648
649 XMLNode* e0 = xml.FirstChild();
650 XMLNode* e1 = e0->NextSibling();
651 XMLComment* c0 = e0->ToComment();
652 XMLComment* c1 = e1->ToComment();
653
654 XMLTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
655 XMLTest( "Comments ignore entities.", " far &amp; away ", c1->Value(), true );
656 }
657
658 {
659 XMLDocument xml;
660 xml.Parse( "<Parent>"
661 "<child1 att=''/>"
662 "<!-- With this comment, child2 will not be parsed! -->"
663 "<child2 att=''/>"
664 "</Parent>" );
665 xml.Print();
666
667 int count = 0;
668
669 for( XMLNode* ele = xml.FirstChildElement( "Parent" )->FirstChild();
670 ele;
671 ele = ele->NextSibling() )
672 {
673 ++count;
674 }
675
676 XMLTest( "Comments iterate correctly.", 3, count );
677 }
678
679 {
680 // trying to repro ]1874301]. If it doesn't go into an infinite loop, all is well.
681 unsigned char buf[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?><feed><![CDATA[Test XMLblablablalblbl";
682 buf[60] = 239;
683 buf[61] = 0;
684
685 XMLDocument doc;
686 doc.Parse( (const char*)buf);
687 }
688
689
690 {
691 // bug 1827248 Error while parsing a little bit malformed file
692 // Actually not malformed - should work.
693 XMLDocument xml;
694 xml.Parse( "<attributelist> </attributelist >" );
695 XMLTest( "Handle end tag whitespace", false, xml.Error() );
696 }
697
698 {
699 // This one must not result in an infinite loop
700 XMLDocument xml;
701 xml.Parse( "<infinite>loop" );
702 XMLTest( "Infinite loop test.", true, true );
703 }
704#endif
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800705 {
706 const char* pub = "<?xml version='1.0'?> <element><sub/></element> <!--comment--> <!DOCTYPE>";
707 XMLDocument doc;
708 doc.Parse( pub );
709
710 XMLDocument clone;
711 for( const XMLNode* node=doc.FirstChild(); node; node=node->NextSibling() ) {
712 XMLNode* copy = node->ShallowClone( &clone );
713 clone.InsertEndChild( copy );
714 }
715
716 clone.Print();
717
718 int count=0;
719 const XMLNode* a=clone.FirstChild();
720 const XMLNode* b=doc.FirstChild();
721 for( ; a && b; a=a->NextSibling(), b=b->NextSibling() ) {
722 ++count;
723 XMLTest( "Clone and Equal", true, a->ShallowEqual( b ));
724 }
725 XMLTest( "Clone and Equal", 4, count );
726 }
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800727
Lee Thomason6f381b72012-03-02 12:59:39 -0800728 // ----------- Performance tracking --------------
729 {
730#if defined( _MSC_VER )
731 __int64 start, end, freq;
732 QueryPerformanceFrequency( (LARGE_INTEGER*) &freq );
733#endif
734
735#if defined(_MSC_VER)
736#pragma warning ( push )
737#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
738#endif
739 FILE* fp = fopen( "dream.xml", "r" );
740#if defined(_MSC_VER)
741#pragma warning ( pop )
742#endif
743 fseek( fp, 0, SEEK_END );
744 long size = ftell( fp );
745 fseek( fp, 0, SEEK_SET );
746
747 char* mem = new char[size+1];
748 fread( mem, size, 1, fp );
749 fclose( fp );
750 mem[size] = 0;
751
752#if defined( _MSC_VER )
753 QueryPerformanceCounter( (LARGE_INTEGER*) &start );
754#else
755 clock_t cstart = clock();
756#endif
757 static const int COUNT = 10;
758 for( int i=0; i<COUNT; ++i ) {
759 XMLDocument doc;
760 doc.Parse( mem );
761 }
762#if defined( _MSC_VER )
763 QueryPerformanceCounter( (LARGE_INTEGER*) &end );
764#else
765 clock_t cend = clock();
766#endif
767
768 delete [] mem;
769
770 static const char* note =
771#ifdef DEBUG
772 "DEBUG";
773#else
774 "Release";
775#endif
776
777#if defined( _MSC_VER )
778 printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, 1000.0 * (double)(end-start) / ( (double)freq * (double)COUNT) );
779#else
780 printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, (double)(cend - cstart)/(double)COUNT );
781#endif
782 }
783
Lee Thomason (grinliz)7ca55582012-03-07 21:54:57 -0800784 #if defined( _MSC_VER ) && defined( DEBUG )
Lee Thomason1ff38e02012-02-14 18:18:16 -0800785 _CrtMemCheckpoint( &endMemState );
786 //_CrtMemDumpStatistics( &endMemState );
787
788 _CrtMemState diffMemState;
789 _CrtMemDifference( &diffMemState, &startMemState, &endMemState );
790 _CrtMemDumpStatistics( &diffMemState );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800791 //printf( "new total=%d\n", gNewTotal );
Lee Thomason1ff38e02012-02-14 18:18:16 -0800792 #endif
793
794 printf ("\nPass %d, Fail %d\n", gPass, gFail);
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800795 return 0;
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -0800796}