blob: 8fc3cfb34295b3be67cb3595729d70b3b5d88002 [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 Thomason87e475a2012-03-20 11:55:29 -070080 /* ------ Example 1: Load and parse an XML file. ---- */
81 {
82 XMLDocument doc;
83 doc.LoadFile( "dream.xml" );
84 }
85
86 /* ------ Example 2: Lookup information. ---- */
87 {
88 XMLDocument doc;
89 doc.LoadFile( "dream.xml" );
90
91 // Structure of the XML file:
92 // - Element "PLAY" the root Element
93 // - - Element "TITLE" child of the root PLAY Element
94 // - - - Text child of the TITLE Element
95
96 // Navigate to the title, using the convenience function, with a dangerous lack of error checking.
97 const char* title = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" )->GetText();
98 printf( "Name of play (1): %s\n", title );
99
100 // Text is just another Node to TinyXML-2. The more general way to get to the XMLText:
101 XMLText* textNode = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" )->FirstChild()->ToText();
102 title = textNode->Value();
103 printf( "Name of play (2): %s\n", title );
104 }
105
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800106 {
Lee Thomason43f59302012-02-06 18:18:11 -0800107 static const char* test[] = { "<element />",
108 "<element></element>",
109 "<element><subelement/></element>",
110 "<element><subelement></subelement></element>",
111 "<element><subelement><subsub/></subelement></element>",
112 "<!--comment beside elements--><element><subelement></subelement></element>",
113 "<!--comment beside elements, this time with spaces--> \n <element> <subelement> \n </subelement> </element>",
114 "<element attrib1='foo' attrib2=\"bar\" ></element>",
115 "<element attrib1='foo' attrib2=\"bar\" ><subelement attrib3='yeehaa' /></element>",
116 "<element>Text inside element.</element>",
117 "<element><b></b></element>",
118 "<element>Text inside and <b>bolded</b> in the element.</element>",
119 "<outer><element>Text inside and <b>bolded</b> in the element.</element></outer>",
Lee Thomason8ee79892012-01-25 17:44:30 -0800120 "<element>This &amp; That.</element>",
Lee Thomason18d68bd2012-01-26 18:17:26 -0800121 "<element attrib='This&lt;That' />",
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800122 0
123 };
Lee Thomason6ee99fc2012-01-21 18:45:16 -0800124 for( int i=0; test[i]; ++i ) {
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800125 XMLDocument doc;
Lee Thomason6ee99fc2012-01-21 18:45:16 -0800126 doc.Parse( test[i] );
Lee Thomason5cae8972012-01-24 18:03:07 -0800127 doc.Print();
Lee Thomasonec975ce2012-01-23 11:42:06 -0800128 printf( "----------------------------------------------\n" );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800129 }
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800130 }
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800131#if 1
Lee Thomasond6277762012-02-22 16:00:12 -0800132 {
133 static const char* test = "<!--hello world\n"
134 " line 2\r"
135 " line 3\r\n"
136 " line 4\n\r"
137 " line 5\r-->";
138
139 XMLDocument doc;
140 doc.Parse( test );
141 doc.Print();
142 }
143
Lee Thomason2c85a712012-01-31 08:24:24 -0800144 {
145 static const char* test = "<element>Text before.</element>";
146 XMLDocument doc;
147 doc.Parse( test );
148 XMLElement* root = doc.FirstChildElement();
149 XMLElement* newElement = doc.NewElement( "Subelement" );
150 root->InsertEndChild( newElement );
151 doc.Print();
152 }
Lee Thomasond1983222012-02-06 08:41:24 -0800153 {
154 XMLDocument* doc = new XMLDocument();
155 static const char* test = "<element><sub/></element>";
156 doc->Parse( test );
157 delete doc;
158 }
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800159 {
Lee Thomason1ff38e02012-02-14 18:18:16 -0800160 // Test: Programmatic DOM
Lee Thomasonec5a7b42012-02-13 18:16:52 -0800161 // Build:
162 // <element>
163 // <!--comment-->
164 // <sub attrib="1" />
165 // <sub attrib="2" />
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800166 // <sub attrib="3" >& Text!</sub>
Lee Thomasonec5a7b42012-02-13 18:16:52 -0800167 // <element>
168
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800169 XMLDocument* doc = new XMLDocument();
Lee Thomason1ff38e02012-02-14 18:18:16 -0800170 XMLNode* element = doc->InsertEndChild( doc->NewElement( "element" ) );
171
172 XMLElement* sub[3] = { doc->NewElement( "sub" ), doc->NewElement( "sub" ), doc->NewElement( "sub" ) };
173 for( int i=0; i<3; ++i ) {
174 sub[i]->SetAttribute( "attrib", i );
175 }
176 element->InsertEndChild( sub[2] );
177 XMLNode* comment = element->InsertFirstChild( doc->NewComment( "comment" ) );
178 element->InsertAfterChild( comment, sub[0] );
179 element->InsertAfterChild( sub[0], sub[1] );
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800180 sub[2]->InsertFirstChild( doc->NewText( "& Text!" ));
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800181 doc->Print();
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800182 XMLTest( "Programmatic DOM", "comment", doc->FirstChildElement( "element" )->FirstChild()->Value() );
183 XMLTest( "Programmatic DOM", "0", doc->FirstChildElement( "element" )->FirstChildElement()->Attribute( "attrib" ) );
184 XMLTest( "Programmatic DOM", 2, doc->FirstChildElement()->LastChildElement( "sub" )->IntAttribute( "attrib" ) );
185 XMLTest( "Programmatic DOM", "& Text!",
186 doc->FirstChildElement()->LastChildElement( "sub" )->FirstChild()->ToText()->Value() );
U-Stream\Leeae25a442012-02-17 17:48:16 -0800187
188 // And now deletion:
189 element->DeleteChild( sub[2] );
190 doc->DeleteNode( comment );
191
192 element->FirstChildElement()->SetAttribute( "attrib", true );
193 element->LastChildElement()->DeleteAttribute( "attrib" );
194
195 XMLTest( "Programmatic DOM", true, doc->FirstChildElement()->FirstChildElement()->BoolAttribute( "attrib" ) );
196 int value = 10;
197 int result = doc->FirstChildElement()->LastChildElement()->QueryIntAttribute( "attrib", &value );
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700198 XMLTest( "Programmatic DOM", result, XML_NO_ATTRIBUTE );
U-Stream\Leeae25a442012-02-17 17:48:16 -0800199 XMLTest( "Programmatic DOM", value, 10 );
200
201 doc->Print();
202
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800203 XMLPrinter streamer;
U-Stream\Leeae25a442012-02-17 17:48:16 -0800204 doc->Print( &streamer );
205 printf( "%s", streamer.CStr() );
206
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800207 delete doc;
208 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800209 {
210 // Test: Dream
211 // XML1 : 1,187,569 bytes in 31,209 allocations
212 // XML2 : 469,073 bytes in 323 allocations
213 //int newStart = gNew;
214 XMLDocument doc;
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800215 doc.LoadFile( "dream.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800216
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800217 doc.SaveFile( "dreamout.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800218 doc.PrintError();
219
220 XMLTest( "Dream", "xml version=\"1.0\"",
221 doc.FirstChild()->ToDeclaration()->Value() );
222 XMLTest( "Dream", true, doc.FirstChild()->NextSibling()->ToUnknown() ? true : false );
223 XMLTest( "Dream", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
224 doc.FirstChild()->NextSibling()->ToUnknown()->Value() );
225 XMLTest( "Dream", "And Robin shall restore amends.",
226 doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
227 XMLTest( "Dream", "And Robin shall restore amends.",
228 doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
229
230 XMLDocument doc2;
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800231 doc2.LoadFile( "dreamout.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800232 XMLTest( "Dream-out", "xml version=\"1.0\"",
233 doc2.FirstChild()->ToDeclaration()->Value() );
234 XMLTest( "Dream-out", true, doc2.FirstChild()->NextSibling()->ToUnknown() ? true : false );
235 XMLTest( "Dream-out", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
236 doc2.FirstChild()->NextSibling()->ToUnknown()->Value() );
237 XMLTest( "Dream-out", "And Robin shall restore amends.",
238 doc2.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
239
240 //gNewTotal = gNew - newStart;
241 }
Lee Thomason6f381b72012-03-02 12:59:39 -0800242
243
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800244 {
245 const char* error = "<?xml version=\"1.0\" standalone=\"no\" ?>\n"
246 "<passages count=\"006\" formatversion=\"20020620\">\n"
247 " <wrong error>\n"
248 "</passages>";
249
250 XMLDocument doc;
251 doc.Parse( error );
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700252 XMLTest( "Bad XML", doc.ErrorID(), XML_ERROR_PARSING_ATTRIBUTE );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800253 }
254
255 {
256 const char* str = "<doc attr0='1' attr1='2.0' attr2='foo' />";
257
258 XMLDocument doc;
259 doc.Parse( str );
260
261 XMLElement* ele = doc.FirstChildElement();
262
263 int iVal, result;
264 double dVal;
265
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 );
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700275 XMLTest( "Query attribute: not a number", result, XML_WRONG_ATTRIBUTE_TYPE );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800276 result = ele->QueryIntAttribute( "bar", &iVal );
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700277 XMLTest( "Query attribute: does not exist", result, XML_NO_ATTRIBUTE );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800278 }
279
280 {
281 const char* str = "<doc/>";
282
283 XMLDocument doc;
284 doc.Parse( str );
285
286 XMLElement* ele = doc.FirstChildElement();
287
288 int iVal;
289 double dVal;
290
291 ele->SetAttribute( "str", "strValue" );
292 ele->SetAttribute( "int", 1 );
293 ele->SetAttribute( "double", -1.0 );
294
295 const char* cStr = ele->Attribute( "str" );
296 ele->QueryIntAttribute( "int", &iVal );
297 ele->QueryDoubleAttribute( "double", &dVal );
298
299 XMLTest( "Attribute round trip. c-string.", "strValue", cStr );
300 XMLTest( "Attribute round trip. int.", 1, iVal );
301 XMLTest( "Attribute round trip. double.", -1, (int)dVal );
302 }
303
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800304 {
305 XMLDocument doc;
306 doc.LoadFile( "utf8test.xml" );
307
308 // Get the attribute "value" from the "Russian" element and check it.
309 XMLElement* element = doc.FirstChildElement( "document" )->FirstChildElement( "Russian" );
310 const unsigned char correctValue[] = { 0xd1U, 0x86U, 0xd0U, 0xb5U, 0xd0U, 0xbdU, 0xd0U, 0xbdU,
311 0xd0U, 0xbeU, 0xd1U, 0x81U, 0xd1U, 0x82U, 0xd1U, 0x8cU, 0 };
312
313 XMLTest( "UTF-8: Russian value.", (const char*)correctValue, element->Attribute( "value" ) );
314
315 const unsigned char russianElementName[] = { 0xd0U, 0xa0U, 0xd1U, 0x83U,
316 0xd1U, 0x81U, 0xd1U, 0x81U,
317 0xd0U, 0xbaU, 0xd0U, 0xb8U,
318 0xd0U, 0xb9U, 0 };
319 const char russianText[] = "<\xD0\xB8\xD0\xBC\xD0\xB5\xD0\xB5\xD1\x82>";
320
321 XMLText* text = doc.FirstChildElement( "document" )->FirstChildElement( (const char*) russianElementName )->FirstChild()->ToText();
322 XMLTest( "UTF-8: Browsing russian element name.",
323 russianText,
324 text->Value() );
325
326 // Now try for a round trip.
327 doc.SaveFile( "utf8testout.xml" );
328
329 // Check the round trip.
330 char savedBuf[256];
331 char verifyBuf[256];
332 int okay = 0;
333
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800334
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800335#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800336#pragma warning ( push )
337#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800338#endif
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800339 FILE* saved = fopen( "utf8testout.xml", "r" );
340 FILE* verify = fopen( "utf8testverify.xml", "r" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800341#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800342#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800343#endif
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800344
345 if ( saved && verify )
346 {
347 okay = 1;
348 while ( fgets( verifyBuf, 256, verify ) )
349 {
350 fgets( savedBuf, 256, saved );
351 NullLineEndings( verifyBuf );
352 NullLineEndings( savedBuf );
353
354 if ( strcmp( verifyBuf, savedBuf ) )
355 {
356 printf( "verify:%s<\n", verifyBuf );
357 printf( "saved :%s<\n", savedBuf );
358 okay = 0;
359 break;
360 }
361 }
362 }
363 if ( saved )
364 fclose( saved );
365 if ( verify )
366 fclose( verify );
367 XMLTest( "UTF-8: Verified multi-language round trip.", 1, okay );
368 }
369
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800370 // --------GetText()-----------
371 {
372 const char* str = "<foo>This is text</foo>";
373 XMLDocument doc;
374 doc.Parse( str );
375 const XMLElement* element = doc.RootElement();
376
377 XMLTest( "GetText() normal use.", "This is text", element->GetText() );
378
379 str = "<foo><b>This is text</b></foo>";
380 doc.Parse( str );
381 element = doc.RootElement();
382
383 XMLTest( "GetText() contained element.", element->GetText() == 0, true );
384 }
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800385
Lee Thomasond6277762012-02-22 16:00:12 -0800386
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800387 // ---------- 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();
Lee Thomasond6277762012-02-22 16:00:12 -0800398
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800399 XMLTest( "CDATA parse.", doc.FirstChildElement()->FirstChild()->Value(),
400 "I am > the rules!\n...since I make symbolic puns",
Lee Thomasond6277762012-02-22 16:00:12 -0800401 false );
402 }
403
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800404 // ----------- CDATA -------------
405 {
406 const char* str = "<xmlElement>"
407 "<![CDATA["
408 "<b>I am > the rules!</b>\n"
409 "...since I make symbolic puns"
410 "]]>"
411 "</xmlElement>";
412 XMLDocument doc;
413 doc.Parse( str );
414 doc.Print();
415
416 XMLTest( "CDATA parse. [ tixml1:1480107 ]", doc.FirstChildElement()->FirstChild()->Value(),
417 "<b>I am > the rules!</b>\n...since I make symbolic puns",
418 false );
419 }
420
421 // InsertAfterChild causes crash.
422 {
423 // InsertBeforeChild and InsertAfterChild causes crash.
424 XMLDocument doc;
425 XMLElement* parent = doc.NewElement( "Parent" );
426 doc.InsertFirstChild( parent );
427
428 XMLElement* childText0 = doc.NewElement( "childText0" );
429 XMLElement* childText1 = doc.NewElement( "childText1" );
430
431 XMLNode* childNode0 = parent->InsertEndChild( childText0 );
432 XMLNode* childNode1 = parent->InsertAfterChild( childNode0, childText1 );
433
434 XMLTest( "Test InsertAfterChild on empty node. ", ( childNode1 == parent->LastChild() ), true );
435 }
Lee Thomasond6277762012-02-22 16:00:12 -0800436
437 {
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800438 // Entities not being written correctly.
439 // From Lynn Allen
Lee Thomasond6277762012-02-22 16:00:12 -0800440
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800441 const char* passages =
442 "<?xml version=\"1.0\" standalone=\"no\" ?>"
443 "<passages count=\"006\" formatversion=\"20020620\">"
444 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
445 " It also has &lt;, &gt;, and &amp;, as well as a fake copyright &#xA9;.\"> </psg>"
446 "</passages>";
Lee Thomasond6277762012-02-22 16:00:12 -0800447
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800448 XMLDocument doc;
449 doc.Parse( passages );
450 XMLElement* psg = doc.RootElement()->FirstChildElement();
451 const char* context = psg->Attribute( "context" );
452 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 -0800453
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800454 XMLTest( "Entity transformation: read. ", expected, context, true );
Lee Thomasond6277762012-02-22 16:00:12 -0800455
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800456#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800457#pragma warning ( push )
458#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800459#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800460 FILE* textfile = fopen( "textfile.txt", "w" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800461#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800462#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800463#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800464 if ( textfile )
465 {
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800466 XMLPrinter streamer( textfile );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800467 psg->Accept( &streamer );
468 fclose( textfile );
469 }
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800470#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800471#pragma warning ( push )
472#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800473#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800474 textfile = fopen( "textfile.txt", "r" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800475#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800476#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800477#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800478 TIXMLASSERT( textfile );
479 if ( textfile )
480 {
481 char buf[ 1024 ];
482 fgets( buf, 1024, textfile );
483 XMLTest( "Entity transformation: write. ",
484 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
485 " It also has &lt;, &gt;, and &amp;, as well as a fake copyright \xC2\xA9.\"/>\n",
486 buf, false );
487 }
488 fclose( textfile );
489 }
490
491 {
Lee Thomason6f381b72012-03-02 12:59:39 -0800492 // Suppress entities.
493 const char* passages =
494 "<?xml version=\"1.0\" standalone=\"no\" ?>"
495 "<passages count=\"006\" formatversion=\"20020620\">"
496 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;.\">Crazy &ttk;</psg>"
497 "</passages>";
498
499 XMLDocument doc( false );
500 doc.Parse( passages );
501
502 XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->Attribute( "context" ),
503 "Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;." );
504 XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->FirstChild()->Value(),
505 "Crazy &ttk;" );
506 doc.Print();
507 }
508
509 {
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800510 const char* test = "<?xml version='1.0'?><a.elem xmi.version='2.0'/>";
511
512 XMLDocument doc;
513 doc.Parse( test );
514 XMLTest( "dot in names", doc.Error(), 0);
515 XMLTest( "dot in names", doc.FirstChildElement()->Name(), "a.elem" );
516 XMLTest( "dot in names", doc.FirstChildElement()->Attribute( "xmi.version" ), "2.0" );
517 }
518
519 {
520 const char* test = "<element><Name>1.1 Start easy ignore fin thickness&#xA;</Name></element>";
521
522 XMLDocument doc;
523 doc.Parse( test );
524
525 XMLText* text = doc.FirstChildElement()->FirstChildElement()->FirstChild()->ToText();
526 XMLTest( "Entity with one digit.",
527 text->Value(), "1.1 Start easy ignore fin thickness\n",
528 false );
529 }
530
531 {
532 // DOCTYPE not preserved (950171)
533 //
534 const char* doctype =
535 "<?xml version=\"1.0\" ?>"
536 "<!DOCTYPE PLAY SYSTEM 'play.dtd'>"
537 "<!ELEMENT title (#PCDATA)>"
538 "<!ELEMENT books (title,authors)>"
539 "<element />";
540
541 XMLDocument doc;
542 doc.Parse( doctype );
543 doc.SaveFile( "test7.xml" );
544 doc.DeleteChild( doc.RootElement() );
545 doc.LoadFile( "test7.xml" );
546 doc.Print();
547
548 const XMLUnknown* decl = doc.FirstChild()->NextSibling()->ToUnknown();
549 XMLTest( "Correct value of unknown.", "DOCTYPE PLAY SYSTEM 'play.dtd'", decl->Value() );
550
551 }
552
553 {
554 // Comments do not stream out correctly.
555 const char* doctype =
556 "<!-- Somewhat<evil> -->";
557 XMLDocument doc;
558 doc.Parse( doctype );
559
560 XMLComment* comment = doc.FirstChild()->ToComment();
561
562 XMLTest( "Comment formatting.", " Somewhat<evil> ", comment->Value() );
563 }
564 {
565 // Double attributes
566 const char* doctype = "<element attr='red' attr='blue' />";
567
568 XMLDocument doc;
569 doc.Parse( doctype );
570
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700571 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 -0800572 doc.PrintError();
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800573 }
574
575 {
576 // Embedded null in stream.
577 const char* doctype = "<element att\0r='red' attr='blue' />";
578
579 XMLDocument doc;
580 doc.Parse( doctype );
581 XMLTest( "Embedded null throws error.", true, doc.Error() );
582 }
583
584 {
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700585 // Empty documents should return TIXML_XML_ERROR_PARSING_EMPTY, bug 1070717
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800586 const char* str = " ";
587 XMLDocument doc;
588 doc.Parse( str );
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700589 XMLTest( "Empty document error", XML_ERROR_EMPTY_DOCUMENT, doc.ErrorID() );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800590 }
591
592 {
593 // Low entities
594 XMLDocument doc;
595 doc.Parse( "<test>&#x0e;</test>" );
596 const char result[] = { 0x0e, 0 };
597 XMLTest( "Low entities.", doc.FirstChildElement()->GetText(), result );
598 doc.Print();
599 }
600
601 {
602 // Attribute values with trailing quotes not handled correctly
603 XMLDocument doc;
604 doc.Parse( "<foo attribute=bar\" />" );
605 XMLTest( "Throw error with bad end quotes.", doc.Error(), true );
606 }
607
608 {
609 // [ 1663758 ] Failure to report error on bad XML
610 XMLDocument xml;
611 xml.Parse("<x>");
612 XMLTest("Missing end tag at end of input", xml.Error(), true);
613 xml.Parse("<x> ");
614 XMLTest("Missing end tag with trailing whitespace", xml.Error(), true);
615 xml.Parse("<x></y>");
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700616 XMLTest("Mismatched tags", xml.ErrorID(), XML_ERROR_MISMATCHED_ELEMENT);
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800617 }
618
619
620 {
621 // [ 1475201 ] TinyXML parses entities in comments
622 XMLDocument xml;
623 xml.Parse("<!-- declarations for <head> & <body> -->"
624 "<!-- far &amp; away -->" );
625
626 XMLNode* e0 = xml.FirstChild();
627 XMLNode* e1 = e0->NextSibling();
628 XMLComment* c0 = e0->ToComment();
629 XMLComment* c1 = e1->ToComment();
630
631 XMLTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
632 XMLTest( "Comments ignore entities.", " far &amp; away ", c1->Value(), true );
633 }
634
635 {
636 XMLDocument xml;
637 xml.Parse( "<Parent>"
638 "<child1 att=''/>"
639 "<!-- With this comment, child2 will not be parsed! -->"
640 "<child2 att=''/>"
641 "</Parent>" );
642 xml.Print();
643
644 int count = 0;
645
646 for( XMLNode* ele = xml.FirstChildElement( "Parent" )->FirstChild();
647 ele;
648 ele = ele->NextSibling() )
649 {
650 ++count;
651 }
652
653 XMLTest( "Comments iterate correctly.", 3, count );
654 }
655
656 {
657 // trying to repro ]1874301]. If it doesn't go into an infinite loop, all is well.
658 unsigned char buf[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?><feed><![CDATA[Test XMLblablablalblbl";
659 buf[60] = 239;
660 buf[61] = 0;
661
662 XMLDocument doc;
663 doc.Parse( (const char*)buf);
664 }
665
666
667 {
668 // bug 1827248 Error while parsing a little bit malformed file
669 // Actually not malformed - should work.
670 XMLDocument xml;
671 xml.Parse( "<attributelist> </attributelist >" );
672 XMLTest( "Handle end tag whitespace", false, xml.Error() );
673 }
674
675 {
676 // This one must not result in an infinite loop
677 XMLDocument xml;
678 xml.Parse( "<infinite>loop" );
679 XMLTest( "Infinite loop test.", true, true );
680 }
681#endif
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800682 {
683 const char* pub = "<?xml version='1.0'?> <element><sub/></element> <!--comment--> <!DOCTYPE>";
684 XMLDocument doc;
685 doc.Parse( pub );
686
687 XMLDocument clone;
688 for( const XMLNode* node=doc.FirstChild(); node; node=node->NextSibling() ) {
689 XMLNode* copy = node->ShallowClone( &clone );
690 clone.InsertEndChild( copy );
691 }
692
693 clone.Print();
694
695 int count=0;
696 const XMLNode* a=clone.FirstChild();
697 const XMLNode* b=doc.FirstChild();
698 for( ; a && b; a=a->NextSibling(), b=b->NextSibling() ) {
699 ++count;
700 XMLTest( "Clone and Equal", true, a->ShallowEqual( b ));
701 }
702 XMLTest( "Clone and Equal", 4, count );
703 }
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800704
Lee Thomason6f381b72012-03-02 12:59:39 -0800705 // ----------- Performance tracking --------------
706 {
707#if defined( _MSC_VER )
708 __int64 start, end, freq;
709 QueryPerformanceFrequency( (LARGE_INTEGER*) &freq );
710#endif
711
712#if defined(_MSC_VER)
713#pragma warning ( push )
714#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
715#endif
716 FILE* fp = fopen( "dream.xml", "r" );
717#if defined(_MSC_VER)
718#pragma warning ( pop )
719#endif
720 fseek( fp, 0, SEEK_END );
721 long size = ftell( fp );
722 fseek( fp, 0, SEEK_SET );
723
724 char* mem = new char[size+1];
725 fread( mem, size, 1, fp );
726 fclose( fp );
727 mem[size] = 0;
728
729#if defined( _MSC_VER )
730 QueryPerformanceCounter( (LARGE_INTEGER*) &start );
731#else
732 clock_t cstart = clock();
733#endif
734 static const int COUNT = 10;
735 for( int i=0; i<COUNT; ++i ) {
736 XMLDocument doc;
737 doc.Parse( mem );
738 }
739#if defined( _MSC_VER )
740 QueryPerformanceCounter( (LARGE_INTEGER*) &end );
741#else
742 clock_t cend = clock();
743#endif
744
745 delete [] mem;
746
747 static const char* note =
748#ifdef DEBUG
749 "DEBUG";
750#else
751 "Release";
752#endif
753
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
Lee Thomason (grinliz)7ca55582012-03-07 21:54:57 -0800761 #if defined( _MSC_VER ) && defined( DEBUG )
Lee Thomason1ff38e02012-02-14 18:18:16 -0800762 _CrtMemCheckpoint( &endMemState );
763 //_CrtMemDumpStatistics( &endMemState );
764
765 _CrtMemState diffMemState;
766 _CrtMemDifference( &diffMemState, &startMemState, &endMemState );
767 _CrtMemDumpStatistics( &diffMemState );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800768 //printf( "new total=%d\n", gNewTotal );
Lee Thomason1ff38e02012-02-14 18:18:16 -0800769 #endif
770
771 printf ("\nPass %d, Fail %d\n", gPass, gFail);
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800772 return 0;
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -0800773}