blob: f1b13e0753710cb222a4a4c66937e9b424bc16e8 [file] [log] [blame]
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001#include "tinyxml2.h"
2
3#include <stdio.h>
4#include <stdlib.h>
Lee Thomasonec5a7b42012-02-13 18:16:52 -08005#include <string.h>
Lee Thomason6f381b72012-03-02 12:59:39 -08006#include <time.h>
U-Lama\Leee13c3e62011-12-28 14:36:55 -08007
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08008#if defined( _MSC_VER )
Lee Thomason1ff38e02012-02-14 18:18:16 -08009 #include <crtdbg.h>
Lee Thomason6f381b72012-03-02 12:59:39 -080010 #define WIN32_LEAN_AND_MEAN
11 #include <windows.h>
Lee Thomason1ff38e02012-02-14 18:18:16 -080012 _CrtMemState startMemState;
13 _CrtMemState endMemState;
14#endif
Lee Thomasone9ecdab2012-02-13 18:11:20 -080015
U-Lama\Leee13c3e62011-12-28 14:36:55 -080016using namespace tinyxml2;
Lee Thomasonec5a7b42012-02-13 18:16:52 -080017int gPass = 0;
18int gFail = 0;
19
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -080020
U-Stream\Lee09a11c52012-02-17 08:31:16 -080021bool XMLTest (const char* testString, const char* expected, const char* found, bool echo=true )
Lee Thomason1ff38e02012-02-14 18:18:16 -080022{
23 bool pass = !strcmp( expected, found );
24 if ( pass )
25 printf ("[pass]");
26 else
27 printf ("[fail]");
28
U-Stream\Lee09a11c52012-02-17 08:31:16 -080029 if ( !echo )
Lee Thomason1ff38e02012-02-14 18:18:16 -080030 printf (" %s\n", testString);
31 else
32 printf (" %s [%s][%s]\n", testString, expected, found);
33
34 if ( pass )
35 ++gPass;
36 else
37 ++gFail;
38 return pass;
39}
40
41
U-Stream\Lee09a11c52012-02-17 08:31:16 -080042bool XMLTest( const char* testString, int expected, int found, bool echo=true )
Lee Thomason1ff38e02012-02-14 18:18:16 -080043{
44 bool pass = ( expected == found );
45 if ( pass )
46 printf ("[pass]");
47 else
48 printf ("[fail]");
49
U-Stream\Lee09a11c52012-02-17 08:31:16 -080050 if ( !echo )
Lee Thomason1ff38e02012-02-14 18:18:16 -080051 printf (" %s\n", testString);
52 else
53 printf (" %s [%d][%d]\n", testString, expected, found);
54
55 if ( pass )
56 ++gPass;
57 else
58 ++gFail;
59 return pass;
60}
Lee Thomasonec5a7b42012-02-13 18:16:52 -080061
U-Lama\Leee13c3e62011-12-28 14:36:55 -080062
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -080063void NullLineEndings( char* p )
64{
65 while( p && *p ) {
66 if ( *p == '\n' || *p == '\r' ) {
67 *p = 0;
68 return;
69 }
70 ++p;
71 }
72}
73
74
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -080075int main( int /*argc*/, const char* /*argv*/ )
U-Lama\Leee13c3e62011-12-28 14:36:55 -080076{
Lee Thomason (grinliz)0a4df402012-02-27 20:50:52 -080077 #if defined( _MSC_VER ) && defined( DEBUG )
Lee Thomason1ff38e02012-02-14 18:18:16 -080078 _CrtMemCheckpoint( &startMemState );
79 #endif
Lee Thomason8a5dfee2012-01-18 17:43:40 -080080
Lee Thomason87e475a2012-03-20 11:55:29 -070081 /* ------ Example 1: Load and parse an XML file. ---- */
82 {
83 XMLDocument doc;
84 doc.LoadFile( "dream.xml" );
85 }
86
87 /* ------ Example 2: Lookup information. ---- */
88 {
89 XMLDocument doc;
90 doc.LoadFile( "dream.xml" );
91
92 // Structure of the XML file:
93 // - Element "PLAY" the root Element
94 // - - Element "TITLE" child of the root PLAY Element
95 // - - - Text child of the TITLE Element
96
97 // Navigate to the title, using the convenience function, with a dangerous lack of error checking.
98 const char* title = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" )->GetText();
99 printf( "Name of play (1): %s\n", title );
100
101 // Text is just another Node to TinyXML-2. The more general way to get to the XMLText:
102 XMLText* textNode = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" )->FirstChild()->ToText();
103 title = textNode->Value();
104 printf( "Name of play (2): %s\n", title );
105 }
106
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800107 {
Lee Thomason43f59302012-02-06 18:18:11 -0800108 static const char* test[] = { "<element />",
109 "<element></element>",
110 "<element><subelement/></element>",
111 "<element><subelement></subelement></element>",
112 "<element><subelement><subsub/></subelement></element>",
113 "<!--comment beside elements--><element><subelement></subelement></element>",
114 "<!--comment beside elements, this time with spaces--> \n <element> <subelement> \n </subelement> </element>",
115 "<element attrib1='foo' attrib2=\"bar\" ></element>",
116 "<element attrib1='foo' attrib2=\"bar\" ><subelement attrib3='yeehaa' /></element>",
117 "<element>Text inside element.</element>",
118 "<element><b></b></element>",
119 "<element>Text inside and <b>bolded</b> in the element.</element>",
120 "<outer><element>Text inside and <b>bolded</b> in the element.</element></outer>",
Lee Thomason8ee79892012-01-25 17:44:30 -0800121 "<element>This &amp; That.</element>",
Lee Thomason18d68bd2012-01-26 18:17:26 -0800122 "<element attrib='This&lt;That' />",
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800123 0
124 };
Lee Thomason6ee99fc2012-01-21 18:45:16 -0800125 for( int i=0; test[i]; ++i ) {
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800126 XMLDocument doc;
Lee Thomason6ee99fc2012-01-21 18:45:16 -0800127 doc.Parse( test[i] );
Lee Thomason5cae8972012-01-24 18:03:07 -0800128 doc.Print();
Lee Thomasonec975ce2012-01-23 11:42:06 -0800129 printf( "----------------------------------------------\n" );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800130 }
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800131 }
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800132#if 1
Lee Thomasond6277762012-02-22 16:00:12 -0800133 {
134 static const char* test = "<!--hello world\n"
135 " line 2\r"
136 " line 3\r\n"
137 " line 4\n\r"
138 " line 5\r-->";
139
140 XMLDocument doc;
141 doc.Parse( test );
142 doc.Print();
143 }
144
Lee Thomason2c85a712012-01-31 08:24:24 -0800145 {
146 static const char* test = "<element>Text before.</element>";
147 XMLDocument doc;
148 doc.Parse( test );
149 XMLElement* root = doc.FirstChildElement();
150 XMLElement* newElement = doc.NewElement( "Subelement" );
151 root->InsertEndChild( newElement );
152 doc.Print();
153 }
Lee Thomasond1983222012-02-06 08:41:24 -0800154 {
155 XMLDocument* doc = new XMLDocument();
156 static const char* test = "<element><sub/></element>";
157 doc->Parse( test );
158 delete doc;
159 }
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800160 {
Lee Thomason1ff38e02012-02-14 18:18:16 -0800161 // Test: Programmatic DOM
Lee Thomasonec5a7b42012-02-13 18:16:52 -0800162 // Build:
163 // <element>
164 // <!--comment-->
165 // <sub attrib="1" />
166 // <sub attrib="2" />
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800167 // <sub attrib="3" >& Text!</sub>
Lee Thomasonec5a7b42012-02-13 18:16:52 -0800168 // <element>
169
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800170 XMLDocument* doc = new XMLDocument();
Lee Thomason1ff38e02012-02-14 18:18:16 -0800171 XMLNode* element = doc->InsertEndChild( doc->NewElement( "element" ) );
172
173 XMLElement* sub[3] = { doc->NewElement( "sub" ), doc->NewElement( "sub" ), doc->NewElement( "sub" ) };
174 for( int i=0; i<3; ++i ) {
175 sub[i]->SetAttribute( "attrib", i );
176 }
177 element->InsertEndChild( sub[2] );
178 XMLNode* comment = element->InsertFirstChild( doc->NewComment( "comment" ) );
179 element->InsertAfterChild( comment, sub[0] );
180 element->InsertAfterChild( sub[0], sub[1] );
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800181 sub[2]->InsertFirstChild( doc->NewText( "& Text!" ));
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800182 doc->Print();
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800183 XMLTest( "Programmatic DOM", "comment", doc->FirstChildElement( "element" )->FirstChild()->Value() );
184 XMLTest( "Programmatic DOM", "0", doc->FirstChildElement( "element" )->FirstChildElement()->Attribute( "attrib" ) );
185 XMLTest( "Programmatic DOM", 2, doc->FirstChildElement()->LastChildElement( "sub" )->IntAttribute( "attrib" ) );
186 XMLTest( "Programmatic DOM", "& Text!",
187 doc->FirstChildElement()->LastChildElement( "sub" )->FirstChild()->ToText()->Value() );
U-Stream\Leeae25a442012-02-17 17:48:16 -0800188
189 // And now deletion:
190 element->DeleteChild( sub[2] );
191 doc->DeleteNode( comment );
192
193 element->FirstChildElement()->SetAttribute( "attrib", true );
194 element->LastChildElement()->DeleteAttribute( "attrib" );
195
196 XMLTest( "Programmatic DOM", true, doc->FirstChildElement()->FirstChildElement()->BoolAttribute( "attrib" ) );
197 int value = 10;
198 int result = doc->FirstChildElement()->LastChildElement()->QueryIntAttribute( "attrib", &value );
199 XMLTest( "Programmatic DOM", result, NO_ATTRIBUTE );
200 XMLTest( "Programmatic DOM", value, 10 );
201
202 doc->Print();
203
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800204 XMLPrinter streamer;
U-Stream\Leeae25a442012-02-17 17:48:16 -0800205 doc->Print( &streamer );
206 printf( "%s", streamer.CStr() );
207
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800208 delete doc;
209 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800210 {
211 // Test: Dream
212 // XML1 : 1,187,569 bytes in 31,209 allocations
213 // XML2 : 469,073 bytes in 323 allocations
214 //int newStart = gNew;
215 XMLDocument doc;
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800216 doc.LoadFile( "dream.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800217
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800218 doc.SaveFile( "dreamout.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800219 doc.PrintError();
220
221 XMLTest( "Dream", "xml version=\"1.0\"",
222 doc.FirstChild()->ToDeclaration()->Value() );
223 XMLTest( "Dream", true, doc.FirstChild()->NextSibling()->ToUnknown() ? true : false );
224 XMLTest( "Dream", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
225 doc.FirstChild()->NextSibling()->ToUnknown()->Value() );
226 XMLTest( "Dream", "And Robin shall restore amends.",
227 doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
228 XMLTest( "Dream", "And Robin shall restore amends.",
229 doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
230
231 XMLDocument doc2;
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800232 doc2.LoadFile( "dreamout.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800233 XMLTest( "Dream-out", "xml version=\"1.0\"",
234 doc2.FirstChild()->ToDeclaration()->Value() );
235 XMLTest( "Dream-out", true, doc2.FirstChild()->NextSibling()->ToUnknown() ? true : false );
236 XMLTest( "Dream-out", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
237 doc2.FirstChild()->NextSibling()->ToUnknown()->Value() );
238 XMLTest( "Dream-out", "And Robin shall restore amends.",
239 doc2.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
240
241 //gNewTotal = gNew - newStart;
242 }
Lee Thomason6f381b72012-03-02 12:59:39 -0800243
244
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800245 {
246 const char* error = "<?xml version=\"1.0\" standalone=\"no\" ?>\n"
247 "<passages count=\"006\" formatversion=\"20020620\">\n"
248 " <wrong error>\n"
249 "</passages>";
250
251 XMLDocument doc;
252 doc.Parse( error );
253 XMLTest( "Bad XML", doc.ErrorID(), ERROR_PARSING_ATTRIBUTE );
254 }
255
256 {
257 const char* str = "<doc attr0='1' attr1='2.0' attr2='foo' />";
258
259 XMLDocument doc;
260 doc.Parse( str );
261
262 XMLElement* ele = doc.FirstChildElement();
263
264 int iVal, result;
265 double dVal;
266
267 result = ele->QueryDoubleAttribute( "attr0", &dVal );
268 XMLTest( "Query attribute: int as double", result, XML_NO_ERROR );
269 XMLTest( "Query attribute: int as double", (int)dVal, 1 );
270 result = ele->QueryDoubleAttribute( "attr1", &dVal );
271 XMLTest( "Query attribute: double as double", (int)dVal, 2 );
272 result = ele->QueryIntAttribute( "attr1", &iVal );
273 XMLTest( "Query attribute: double as int", result, XML_NO_ERROR );
274 XMLTest( "Query attribute: double as int", iVal, 2 );
275 result = ele->QueryIntAttribute( "attr2", &iVal );
276 XMLTest( "Query attribute: not a number", result, WRONG_ATTRIBUTE_TYPE );
277 result = ele->QueryIntAttribute( "bar", &iVal );
278 XMLTest( "Query attribute: does not exist", result, NO_ATTRIBUTE );
279 }
280
281 {
282 const char* str = "<doc/>";
283
284 XMLDocument doc;
285 doc.Parse( str );
286
287 XMLElement* ele = doc.FirstChildElement();
288
289 int iVal;
290 double dVal;
291
292 ele->SetAttribute( "str", "strValue" );
293 ele->SetAttribute( "int", 1 );
294 ele->SetAttribute( "double", -1.0 );
295
296 const char* cStr = ele->Attribute( "str" );
297 ele->QueryIntAttribute( "int", &iVal );
298 ele->QueryDoubleAttribute( "double", &dVal );
299
300 XMLTest( "Attribute round trip. c-string.", "strValue", cStr );
301 XMLTest( "Attribute round trip. int.", 1, iVal );
302 XMLTest( "Attribute round trip. double.", -1, (int)dVal );
303 }
304
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800305 {
306 XMLDocument doc;
307 doc.LoadFile( "utf8test.xml" );
308
309 // Get the attribute "value" from the "Russian" element and check it.
310 XMLElement* element = doc.FirstChildElement( "document" )->FirstChildElement( "Russian" );
311 const unsigned char correctValue[] = { 0xd1U, 0x86U, 0xd0U, 0xb5U, 0xd0U, 0xbdU, 0xd0U, 0xbdU,
312 0xd0U, 0xbeU, 0xd1U, 0x81U, 0xd1U, 0x82U, 0xd1U, 0x8cU, 0 };
313
314 XMLTest( "UTF-8: Russian value.", (const char*)correctValue, element->Attribute( "value" ) );
315
316 const unsigned char russianElementName[] = { 0xd0U, 0xa0U, 0xd1U, 0x83U,
317 0xd1U, 0x81U, 0xd1U, 0x81U,
318 0xd0U, 0xbaU, 0xd0U, 0xb8U,
319 0xd0U, 0xb9U, 0 };
320 const char russianText[] = "<\xD0\xB8\xD0\xBC\xD0\xB5\xD0\xB5\xD1\x82>";
321
322 XMLText* text = doc.FirstChildElement( "document" )->FirstChildElement( (const char*) russianElementName )->FirstChild()->ToText();
323 XMLTest( "UTF-8: Browsing russian element name.",
324 russianText,
325 text->Value() );
326
327 // Now try for a round trip.
328 doc.SaveFile( "utf8testout.xml" );
329
330 // Check the round trip.
331 char savedBuf[256];
332 char verifyBuf[256];
333 int okay = 0;
334
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800335
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800336#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800337#pragma warning ( push )
338#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800339#endif
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800340 FILE* saved = fopen( "utf8testout.xml", "r" );
341 FILE* verify = fopen( "utf8testverify.xml", "r" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800342#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800343#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800344#endif
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800345
346 if ( saved && verify )
347 {
348 okay = 1;
349 while ( fgets( verifyBuf, 256, verify ) )
350 {
351 fgets( savedBuf, 256, saved );
352 NullLineEndings( verifyBuf );
353 NullLineEndings( savedBuf );
354
355 if ( strcmp( verifyBuf, savedBuf ) )
356 {
357 printf( "verify:%s<\n", verifyBuf );
358 printf( "saved :%s<\n", savedBuf );
359 okay = 0;
360 break;
361 }
362 }
363 }
364 if ( saved )
365 fclose( saved );
366 if ( verify )
367 fclose( verify );
368 XMLTest( "UTF-8: Verified multi-language round trip.", 1, okay );
369 }
370
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800371 // --------GetText()-----------
372 {
373 const char* str = "<foo>This is text</foo>";
374 XMLDocument doc;
375 doc.Parse( str );
376 const XMLElement* element = doc.RootElement();
377
378 XMLTest( "GetText() normal use.", "This is text", element->GetText() );
379
380 str = "<foo><b>This is text</b></foo>";
381 doc.Parse( str );
382 element = doc.RootElement();
383
384 XMLTest( "GetText() contained element.", element->GetText() == 0, true );
385 }
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800386
Lee Thomasond6277762012-02-22 16:00:12 -0800387
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800388 // ---------- CDATA ---------------
389 {
390 const char* str = "<xmlElement>"
391 "<![CDATA["
392 "I am > the rules!\n"
393 "...since I make symbolic puns"
394 "]]>"
395 "</xmlElement>";
396 XMLDocument doc;
397 doc.Parse( str );
398 doc.Print();
Lee Thomasond6277762012-02-22 16:00:12 -0800399
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800400 XMLTest( "CDATA parse.", doc.FirstChildElement()->FirstChild()->Value(),
401 "I am > the rules!\n...since I make symbolic puns",
Lee Thomasond6277762012-02-22 16:00:12 -0800402 false );
403 }
404
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800405 // ----------- CDATA -------------
406 {
407 const char* str = "<xmlElement>"
408 "<![CDATA["
409 "<b>I am > the rules!</b>\n"
410 "...since I make symbolic puns"
411 "]]>"
412 "</xmlElement>";
413 XMLDocument doc;
414 doc.Parse( str );
415 doc.Print();
416
417 XMLTest( "CDATA parse. [ tixml1:1480107 ]", doc.FirstChildElement()->FirstChild()->Value(),
418 "<b>I am > the rules!</b>\n...since I make symbolic puns",
419 false );
420 }
421
422 // InsertAfterChild causes crash.
423 {
424 // InsertBeforeChild and InsertAfterChild causes crash.
425 XMLDocument doc;
426 XMLElement* parent = doc.NewElement( "Parent" );
427 doc.InsertFirstChild( parent );
428
429 XMLElement* childText0 = doc.NewElement( "childText0" );
430 XMLElement* childText1 = doc.NewElement( "childText1" );
431
432 XMLNode* childNode0 = parent->InsertEndChild( childText0 );
433 XMLNode* childNode1 = parent->InsertAfterChild( childNode0, childText1 );
434
435 XMLTest( "Test InsertAfterChild on empty node. ", ( childNode1 == parent->LastChild() ), true );
436 }
Lee Thomasond6277762012-02-22 16:00:12 -0800437
438 {
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800439 // Entities not being written correctly.
440 // From Lynn Allen
Lee Thomasond6277762012-02-22 16:00:12 -0800441
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800442 const char* passages =
443 "<?xml version=\"1.0\" standalone=\"no\" ?>"
444 "<passages count=\"006\" formatversion=\"20020620\">"
445 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
446 " It also has &lt;, &gt;, and &amp;, as well as a fake copyright &#xA9;.\"> </psg>"
447 "</passages>";
Lee Thomasond6277762012-02-22 16:00:12 -0800448
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800449 XMLDocument doc;
450 doc.Parse( passages );
451 XMLElement* psg = doc.RootElement()->FirstChildElement();
452 const char* context = psg->Attribute( "context" );
453 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 -0800454
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800455 XMLTest( "Entity transformation: read. ", expected, context, true );
Lee Thomasond6277762012-02-22 16:00:12 -0800456
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800457#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800458#pragma warning ( push )
459#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800460#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800461 FILE* textfile = fopen( "textfile.txt", "w" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800462#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800463#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800464#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800465 if ( textfile )
466 {
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800467 XMLPrinter streamer( textfile );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800468 psg->Accept( &streamer );
469 fclose( textfile );
470 }
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800471#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800472#pragma warning ( push )
473#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800474#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800475 textfile = fopen( "textfile.txt", "r" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800476#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800477#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800478#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800479 TIXMLASSERT( textfile );
480 if ( textfile )
481 {
482 char buf[ 1024 ];
483 fgets( buf, 1024, textfile );
484 XMLTest( "Entity transformation: write. ",
485 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
486 " It also has &lt;, &gt;, and &amp;, as well as a fake copyright \xC2\xA9.\"/>\n",
487 buf, false );
488 }
489 fclose( textfile );
490 }
491
492 {
Lee Thomason6f381b72012-03-02 12:59:39 -0800493 // Suppress entities.
494 const char* passages =
495 "<?xml version=\"1.0\" standalone=\"no\" ?>"
496 "<passages count=\"006\" formatversion=\"20020620\">"
497 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;.\">Crazy &ttk;</psg>"
498 "</passages>";
499
500 XMLDocument doc( false );
501 doc.Parse( passages );
502
503 XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->Attribute( "context" ),
504 "Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;." );
505 XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->FirstChild()->Value(),
506 "Crazy &ttk;" );
507 doc.Print();
508 }
509
510 {
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800511 const char* test = "<?xml version='1.0'?><a.elem xmi.version='2.0'/>";
512
513 XMLDocument doc;
514 doc.Parse( test );
515 XMLTest( "dot in names", doc.Error(), 0);
516 XMLTest( "dot in names", doc.FirstChildElement()->Name(), "a.elem" );
517 XMLTest( "dot in names", doc.FirstChildElement()->Attribute( "xmi.version" ), "2.0" );
518 }
519
520 {
521 const char* test = "<element><Name>1.1 Start easy ignore fin thickness&#xA;</Name></element>";
522
523 XMLDocument doc;
524 doc.Parse( test );
525
526 XMLText* text = doc.FirstChildElement()->FirstChildElement()->FirstChild()->ToText();
527 XMLTest( "Entity with one digit.",
528 text->Value(), "1.1 Start easy ignore fin thickness\n",
529 false );
530 }
531
532 {
533 // DOCTYPE not preserved (950171)
534 //
535 const char* doctype =
536 "<?xml version=\"1.0\" ?>"
537 "<!DOCTYPE PLAY SYSTEM 'play.dtd'>"
538 "<!ELEMENT title (#PCDATA)>"
539 "<!ELEMENT books (title,authors)>"
540 "<element />";
541
542 XMLDocument doc;
543 doc.Parse( doctype );
544 doc.SaveFile( "test7.xml" );
545 doc.DeleteChild( doc.RootElement() );
546 doc.LoadFile( "test7.xml" );
547 doc.Print();
548
549 const XMLUnknown* decl = doc.FirstChild()->NextSibling()->ToUnknown();
550 XMLTest( "Correct value of unknown.", "DOCTYPE PLAY SYSTEM 'play.dtd'", decl->Value() );
551
552 }
553
554 {
555 // Comments do not stream out correctly.
556 const char* doctype =
557 "<!-- Somewhat<evil> -->";
558 XMLDocument doc;
559 doc.Parse( doctype );
560
561 XMLComment* comment = doc.FirstChild()->ToComment();
562
563 XMLTest( "Comment formatting.", " Somewhat<evil> ", comment->Value() );
564 }
565 {
566 // Double attributes
567 const char* doctype = "<element attr='red' attr='blue' />";
568
569 XMLDocument doc;
570 doc.Parse( doctype );
571
572 XMLTest( "Parsing repeated attributes.", 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 -0800573 doc.PrintError();
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800574 }
575
576 {
577 // Embedded null in stream.
578 const char* doctype = "<element att\0r='red' attr='blue' />";
579
580 XMLDocument doc;
581 doc.Parse( doctype );
582 XMLTest( "Embedded null throws error.", true, doc.Error() );
583 }
584
585 {
586 // Empty documents should return TIXML_ERROR_PARSING_EMPTY, bug 1070717
587 const char* str = " ";
588 XMLDocument doc;
589 doc.Parse( str );
590 XMLTest( "Empty document error", ERROR_EMPTY_DOCUMENT, doc.ErrorID() );
591 }
592
593 {
594 // Low entities
595 XMLDocument doc;
596 doc.Parse( "<test>&#x0e;</test>" );
597 const char result[] = { 0x0e, 0 };
598 XMLTest( "Low entities.", doc.FirstChildElement()->GetText(), result );
599 doc.Print();
600 }
601
602 {
603 // Attribute values with trailing quotes not handled correctly
604 XMLDocument doc;
605 doc.Parse( "<foo attribute=bar\" />" );
606 XMLTest( "Throw error with bad end quotes.", doc.Error(), true );
607 }
608
609 {
610 // [ 1663758 ] Failure to report error on bad XML
611 XMLDocument xml;
612 xml.Parse("<x>");
613 XMLTest("Missing end tag at end of input", xml.Error(), true);
614 xml.Parse("<x> ");
615 XMLTest("Missing end tag with trailing whitespace", xml.Error(), true);
616 xml.Parse("<x></y>");
617 XMLTest("Mismatched tags", xml.ErrorID(), ERROR_MISMATCHED_ELEMENT);
618 }
619
620
621 {
622 // [ 1475201 ] TinyXML parses entities in comments
623 XMLDocument xml;
624 xml.Parse("<!-- declarations for <head> & <body> -->"
625 "<!-- far &amp; away -->" );
626
627 XMLNode* e0 = xml.FirstChild();
628 XMLNode* e1 = e0->NextSibling();
629 XMLComment* c0 = e0->ToComment();
630 XMLComment* c1 = e1->ToComment();
631
632 XMLTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
633 XMLTest( "Comments ignore entities.", " far &amp; away ", c1->Value(), true );
634 }
635
636 {
637 XMLDocument xml;
638 xml.Parse( "<Parent>"
639 "<child1 att=''/>"
640 "<!-- With this comment, child2 will not be parsed! -->"
641 "<child2 att=''/>"
642 "</Parent>" );
643 xml.Print();
644
645 int count = 0;
646
647 for( XMLNode* ele = xml.FirstChildElement( "Parent" )->FirstChild();
648 ele;
649 ele = ele->NextSibling() )
650 {
651 ++count;
652 }
653
654 XMLTest( "Comments iterate correctly.", 3, count );
655 }
656
657 {
658 // trying to repro ]1874301]. If it doesn't go into an infinite loop, all is well.
659 unsigned char buf[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?><feed><![CDATA[Test XMLblablablalblbl";
660 buf[60] = 239;
661 buf[61] = 0;
662
663 XMLDocument doc;
664 doc.Parse( (const char*)buf);
665 }
666
667
668 {
669 // bug 1827248 Error while parsing a little bit malformed file
670 // Actually not malformed - should work.
671 XMLDocument xml;
672 xml.Parse( "<attributelist> </attributelist >" );
673 XMLTest( "Handle end tag whitespace", false, xml.Error() );
674 }
675
676 {
677 // This one must not result in an infinite loop
678 XMLDocument xml;
679 xml.Parse( "<infinite>loop" );
680 XMLTest( "Infinite loop test.", true, true );
681 }
682#endif
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800683 {
684 const char* pub = "<?xml version='1.0'?> <element><sub/></element> <!--comment--> <!DOCTYPE>";
685 XMLDocument doc;
686 doc.Parse( pub );
687
688 XMLDocument clone;
689 for( const XMLNode* node=doc.FirstChild(); node; node=node->NextSibling() ) {
690 XMLNode* copy = node->ShallowClone( &clone );
691 clone.InsertEndChild( copy );
692 }
693
694 clone.Print();
695
696 int count=0;
697 const XMLNode* a=clone.FirstChild();
698 const XMLNode* b=doc.FirstChild();
699 for( ; a && b; a=a->NextSibling(), b=b->NextSibling() ) {
700 ++count;
701 XMLTest( "Clone and Equal", true, a->ShallowEqual( b ));
702 }
703 XMLTest( "Clone and Equal", 4, count );
704 }
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800705
Lee Thomason6f381b72012-03-02 12:59:39 -0800706 // ----------- Performance tracking --------------
707 {
708#if defined( _MSC_VER )
709 __int64 start, end, freq;
710 QueryPerformanceFrequency( (LARGE_INTEGER*) &freq );
711#endif
712
713#if defined(_MSC_VER)
714#pragma warning ( push )
715#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
716#endif
717 FILE* fp = fopen( "dream.xml", "r" );
718#if defined(_MSC_VER)
719#pragma warning ( pop )
720#endif
721 fseek( fp, 0, SEEK_END );
722 long size = ftell( fp );
723 fseek( fp, 0, SEEK_SET );
724
725 char* mem = new char[size+1];
726 fread( mem, size, 1, fp );
727 fclose( fp );
728 mem[size] = 0;
729
730#if defined( _MSC_VER )
731 QueryPerformanceCounter( (LARGE_INTEGER*) &start );
732#else
733 clock_t cstart = clock();
734#endif
735 static const int COUNT = 10;
736 for( int i=0; i<COUNT; ++i ) {
737 XMLDocument doc;
738 doc.Parse( mem );
739 }
740#if defined( _MSC_VER )
741 QueryPerformanceCounter( (LARGE_INTEGER*) &end );
742#else
743 clock_t cend = clock();
744#endif
745
746 delete [] mem;
747
748 static const char* note =
749#ifdef DEBUG
750 "DEBUG";
751#else
752 "Release";
753#endif
754
755#if defined( _MSC_VER )
756 printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, 1000.0 * (double)(end-start) / ( (double)freq * (double)COUNT) );
757#else
758 printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, (double)(cend - cstart)/(double)COUNT );
759#endif
760 }
761
Lee Thomason (grinliz)7ca55582012-03-07 21:54:57 -0800762 #if defined( _MSC_VER ) && defined( DEBUG )
Lee Thomason1ff38e02012-02-14 18:18:16 -0800763 _CrtMemCheckpoint( &endMemState );
764 //_CrtMemDumpStatistics( &endMemState );
765
766 _CrtMemState diffMemState;
767 _CrtMemDifference( &diffMemState, &startMemState, &endMemState );
768 _CrtMemDumpStatistics( &diffMemState );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800769 //printf( "new total=%d\n", gNewTotal );
Lee Thomason1ff38e02012-02-14 18:18:16 -0800770 #endif
771
772 printf ("\nPass %d, Fail %d\n", gPass, gFail);
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800773 return 0;
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -0800774}