blob: d244efa1851b448ce2957c0e748468ae44f8f6c5 [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>
U-Lama\Leee13c3e62011-12-28 14:36:55 -08006
Lee Thomason1ff38e02012-02-14 18:18:16 -08007#if defined( WIN32 )
8 #include <crtdbg.h>
9 _CrtMemState startMemState;
10 _CrtMemState endMemState;
11#endif
Lee Thomasone9ecdab2012-02-13 18:11:20 -080012
U-Lama\Leee13c3e62011-12-28 14:36:55 -080013using namespace tinyxml2;
Lee Thomasonec5a7b42012-02-13 18:16:52 -080014int gPass = 0;
15int gFail = 0;
16
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -080017//#define DREAM_ONLY
18
19/*
20int gNew = 0;
21int gNewTotal = 0;
22
23void* operator new( size_t size )
24{
25 ++gNew;
26 return malloc( size );
27}
28
29void* operator new[]( size_t size )
30{
31 ++gNew;
32 return malloc( size );
33}
34
35void operator delete[]( void* mem )
36{
37 free( mem );
38}
39
40void operator delete( void* mem )
41{
42 free( mem );
43}
44*/
45
46
U-Stream\Lee09a11c52012-02-17 08:31:16 -080047bool XMLTest (const char* testString, const char* expected, const char* found, bool echo=true )
Lee Thomason1ff38e02012-02-14 18:18:16 -080048{
49 bool pass = !strcmp( expected, found );
50 if ( pass )
51 printf ("[pass]");
52 else
53 printf ("[fail]");
54
U-Stream\Lee09a11c52012-02-17 08:31:16 -080055 if ( !echo )
Lee Thomason1ff38e02012-02-14 18:18:16 -080056 printf (" %s\n", testString);
57 else
58 printf (" %s [%s][%s]\n", testString, expected, found);
59
60 if ( pass )
61 ++gPass;
62 else
63 ++gFail;
64 return pass;
65}
66
67
U-Stream\Lee09a11c52012-02-17 08:31:16 -080068bool XMLTest( const char* testString, int expected, int found, bool echo=true )
Lee Thomason1ff38e02012-02-14 18:18:16 -080069{
70 bool pass = ( expected == found );
71 if ( pass )
72 printf ("[pass]");
73 else
74 printf ("[fail]");
75
U-Stream\Lee09a11c52012-02-17 08:31:16 -080076 if ( !echo )
Lee Thomason1ff38e02012-02-14 18:18:16 -080077 printf (" %s\n", testString);
78 else
79 printf (" %s [%d][%d]\n", testString, expected, found);
80
81 if ( pass )
82 ++gPass;
83 else
84 ++gFail;
85 return pass;
86}
Lee Thomasonec5a7b42012-02-13 18:16:52 -080087
U-Lama\Leee13c3e62011-12-28 14:36:55 -080088
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -080089void NullLineEndings( char* p )
90{
91 while( p && *p ) {
92 if ( *p == '\n' || *p == '\r' ) {
93 *p = 0;
94 return;
95 }
96 ++p;
97 }
98}
99
100
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800101int main( int argc, const char* argv )
102{
Lee Thomason1ff38e02012-02-14 18:18:16 -0800103 #if defined( WIN32 )
104 _CrtMemCheckpoint( &startMemState );
105 #endif
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800106
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 Thomasond6277762012-02-22 16:00:12 -0800132
133 {
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
204 XMLStreamer streamer;
205 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 Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800243 {
244 const char* error = "<?xml version=\"1.0\" standalone=\"no\" ?>\n"
245 "<passages count=\"006\" formatversion=\"20020620\">\n"
246 " <wrong error>\n"
247 "</passages>";
248
249 XMLDocument doc;
250 doc.Parse( error );
251 XMLTest( "Bad XML", doc.ErrorID(), ERROR_PARSING_ATTRIBUTE );
252 }
253
254 {
255 const char* str = "<doc attr0='1' attr1='2.0' attr2='foo' />";
256
257 XMLDocument doc;
258 doc.Parse( str );
259
260 XMLElement* ele = doc.FirstChildElement();
261
262 int iVal, result;
263 double dVal;
264
265 result = ele->QueryDoubleAttribute( "attr0", &dVal );
266 XMLTest( "Query attribute: int as double", result, XML_NO_ERROR );
267 XMLTest( "Query attribute: int as double", (int)dVal, 1 );
268 result = ele->QueryDoubleAttribute( "attr1", &dVal );
269 XMLTest( "Query attribute: double as double", (int)dVal, 2 );
270 result = ele->QueryIntAttribute( "attr1", &iVal );
271 XMLTest( "Query attribute: double as int", result, XML_NO_ERROR );
272 XMLTest( "Query attribute: double as int", iVal, 2 );
273 result = ele->QueryIntAttribute( "attr2", &iVal );
274 XMLTest( "Query attribute: not a number", result, WRONG_ATTRIBUTE_TYPE );
275 result = ele->QueryIntAttribute( "bar", &iVal );
276 XMLTest( "Query attribute: does not exist", result, NO_ATTRIBUTE );
277 }
278
279 {
280 const char* str = "<doc/>";
281
282 XMLDocument doc;
283 doc.Parse( str );
284
285 XMLElement* ele = doc.FirstChildElement();
286
287 int iVal;
288 double dVal;
289
290 ele->SetAttribute( "str", "strValue" );
291 ele->SetAttribute( "int", 1 );
292 ele->SetAttribute( "double", -1.0 );
293
294 const char* cStr = ele->Attribute( "str" );
295 ele->QueryIntAttribute( "int", &iVal );
296 ele->QueryDoubleAttribute( "double", &dVal );
297
298 XMLTest( "Attribute round trip. c-string.", "strValue", cStr );
299 XMLTest( "Attribute round trip. int.", 1, iVal );
300 XMLTest( "Attribute round trip. double.", -1, (int)dVal );
301 }
302
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800303 {
304 XMLDocument doc;
305 doc.LoadFile( "utf8test.xml" );
306
307 // Get the attribute "value" from the "Russian" element and check it.
308 XMLElement* element = doc.FirstChildElement( "document" )->FirstChildElement( "Russian" );
309 const unsigned char correctValue[] = { 0xd1U, 0x86U, 0xd0U, 0xb5U, 0xd0U, 0xbdU, 0xd0U, 0xbdU,
310 0xd0U, 0xbeU, 0xd1U, 0x81U, 0xd1U, 0x82U, 0xd1U, 0x8cU, 0 };
311
312 XMLTest( "UTF-8: Russian value.", (const char*)correctValue, element->Attribute( "value" ) );
313
314 const unsigned char russianElementName[] = { 0xd0U, 0xa0U, 0xd1U, 0x83U,
315 0xd1U, 0x81U, 0xd1U, 0x81U,
316 0xd0U, 0xbaU, 0xd0U, 0xb8U,
317 0xd0U, 0xb9U, 0 };
318 const char russianText[] = "<\xD0\xB8\xD0\xBC\xD0\xB5\xD0\xB5\xD1\x82>";
319
320 XMLText* text = doc.FirstChildElement( "document" )->FirstChildElement( (const char*) russianElementName )->FirstChild()->ToText();
321 XMLTest( "UTF-8: Browsing russian element name.",
322 russianText,
323 text->Value() );
324
325 // Now try for a round trip.
326 doc.SaveFile( "utf8testout.xml" );
327
328 // Check the round trip.
329 char savedBuf[256];
330 char verifyBuf[256];
331 int okay = 0;
332
333 FILE* saved = fopen( "utf8testout.xml", "r" );
334 FILE* verify = fopen( "utf8testverify.xml", "r" );
335
336 if ( saved && verify )
337 {
338 okay = 1;
339 while ( fgets( verifyBuf, 256, verify ) )
340 {
341 fgets( savedBuf, 256, saved );
342 NullLineEndings( verifyBuf );
343 NullLineEndings( savedBuf );
344
345 if ( strcmp( verifyBuf, savedBuf ) )
346 {
347 printf( "verify:%s<\n", verifyBuf );
348 printf( "saved :%s<\n", savedBuf );
349 okay = 0;
350 break;
351 }
352 }
353 }
354 if ( saved )
355 fclose( saved );
356 if ( verify )
357 fclose( verify );
358 XMLTest( "UTF-8: Verified multi-language round trip.", 1, okay );
359 }
360
Lee Thomasond6277762012-02-22 16:00:12 -0800361 // --------GetText()-----------
362 {
363 const char* str = "<foo>This is text</foo>";
364 XMLDocument doc;
365 doc.Parse( str );
366 const XMLElement* element = doc.RootElement();
367
368 XMLTest( "GetText() normal use.", "This is text", element->GetText() );
369
370 str = "<foo><b>This is text</b></foo>";
371 doc.Parse( str );
372 element = doc.RootElement();
373
374 XMLTest( "GetText() contained element.", element->GetText() == 0, true );
375 }
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800376
Lee Thomasond6277762012-02-22 16:00:12 -0800377
378 // ---------- CDATA ---------------
379 {
380 const char* str = "<xmlElement>"
381 "<![CDATA["
382 "I am > the rules!\n"
383 "...since I make symbolic puns"
384 "]]>"
385 "</xmlElement>";
386 XMLDocument doc;
387 doc.Parse( str );
388 doc.Print();
389
390 XMLTest( "CDATA parse.", doc.FirstChildElement()->FirstChild()->Value(),
391 "I am > the rules!\n...since I make symbolic puns",
392 false );
393 }
394
395 // ----------- CDATA -------------
396 {
397 const char* str = "<xmlElement>"
398 "<![CDATA["
399 "<b>I am > the rules!</b>\n"
400 "...since I make symbolic puns"
401 "]]>"
402 "</xmlElement>";
403 XMLDocument doc;
404 doc.Parse( str );
405 doc.Print();
406
407 XMLTest( "CDATA parse. [ tixml1:1480107 ]", doc.FirstChildElement()->FirstChild()->Value(),
408 "<b>I am > the rules!</b>\n...since I make symbolic puns",
409 false );
410 }
411
412 // InsertAfterChild causes crash.
413 {
414 // InsertBeforeChild and InsertAfterChild causes crash.
415 XMLDocument doc;
416 XMLElement* parent = doc.NewElement( "Parent" );
417 doc.InsertFirstChild( parent );
418
419 XMLElement* childText0 = doc.NewElement( "childText0" );
420 XMLElement* childText1 = doc.NewElement( "childText1" );
421
422 XMLNode* childNode0 = parent->InsertEndChild( childText0 );
423 XMLNode* childNode1 = parent->InsertAfterChild( childNode0, childText1 );
424
425 XMLTest( "Test InsertAfterChild on empty node. ", ( childNode1 == parent->LastChild() ), true );
426 }
427
428 {
429 // Entities not being written correctly.
430 // From Lynn Allen
431
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
439 XMLDocument doc;
440 doc.Parse( passages );
441 XMLElement* psg = doc.RootElement()->FirstChildElement();
442 const char* context = psg->Attribute( "context" );
443 const char* expected = "Line 5 has \"quotation marks\" and 'apostrophe marks'. It also has <, >, and &, as well as a fake copyright \xC2\xA9.";
444
445 XMLTest( "Entity transformation: read. ", expected, context, true );
446
447 FILE* textfile = fopen( "textfile.txt", "w" );
448 if ( textfile )
449 {
450 XMLStreamer streamer( textfile );
451 psg->Accept( &streamer );
452 fclose( textfile );
453 }
454 textfile = fopen( "textfile.txt", "r" );
455 TIXMLASSERT( textfile );
456 if ( textfile )
457 {
458 char buf[ 1024 ];
459 fgets( buf, 1024, textfile );
460 XMLTest( "Entity transformation: write. ",
461 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
462 " It also has &lt;, &gt;, and &amp;, as well as a fake copyright \xC2\xA9.\"/>\n",
463 buf, false );
464 }
465 fclose( textfile );
466 }
467
468 {
469 const char* test = "<?xml version='1.0'?><a.elem xmi.version='2.0'/>";
470
471 XMLDocument doc;
472 doc.Parse( test );
473 XMLTest( "dot in names", doc.Error(), 0);
474 XMLTest( "dot in names", doc.FirstChildElement()->Name(), "a.elem" );
475 XMLTest( "dot in names", doc.FirstChildElement()->Attribute( "xmi.version" ), "2.0" );
476 }
477
478 {
479 const char* test = "<element><Name>1.1 Start easy ignore fin thickness&#xA;</Name></element>";
480
481 XMLDocument doc;
482 doc.Parse( test );
483
484 XMLText* text = doc.FirstChildElement()->FirstChildElement()->FirstChild()->ToText();
485 XMLTest( "Entity with one digit.",
486 text->Value(), "1.1 Start easy ignore fin thickness\n",
487 false );
488 }
489
490 {
491 // DOCTYPE not preserved (950171)
492 //
493 const char* doctype =
494 "<?xml version=\"1.0\" ?>"
495 "<!DOCTYPE PLAY SYSTEM 'play.dtd'>"
496 "<!ELEMENT title (#PCDATA)>"
497 "<!ELEMENT books (title,authors)>"
498 "<element />";
499
500 XMLDocument doc;
501 doc.Parse( doctype );
502 doc.SaveFile( "test7.xml" );
503 doc.DeleteChild( doc.RootElement() );
504 doc.LoadFile( "test7.xml" );
505 doc.Print();
506
507 const XMLUnknown* decl = doc.FirstChild()->NextSibling()->ToUnknown();
508 XMLTest( "Correct value of unknown.", "DOCTYPE PLAY SYSTEM 'play.dtd'", decl->Value() );
509
510 }
511
512 {
513 // Comments do not stream out correctly.
514 const char* doctype =
515 "<!-- Somewhat<evil> -->";
516 XMLDocument doc;
517 doc.Parse( doctype );
518
519 XMLComment* comment = doc.FirstChild()->ToComment();
520
521 XMLTest( "Comment formatting.", " Somewhat<evil> ", comment->Value() );
522 }
523 {
524 // Double attributes
525 const char* doctype = "<element attr='red' attr='blue' />";
526
527 XMLDocument doc;
528 doc.Parse( doctype );
529
530 XMLTest( "Parsing repeated attributes.", ERROR_PARSING_ATTRIBUTE, doc.ErrorID() ); // is an error to tinyxml (didn't use to be, but caused issues)
531 }
532
533 {
534 // Embedded null in stream.
535 const char* doctype = "<element att\0r='red' attr='blue' />";
536
537 XMLDocument doc;
538 doc.Parse( doctype );
539 XMLTest( "Embedded null throws error.", true, doc.Error() );
540 }
541
542 {
543 // Empty documents should return TIXML_ERROR_PARSING_EMPTY, bug 1070717
544 const char* str = " ";
545 XMLDocument doc;
546 doc.Parse( str );
547 XMLTest( "Empty document error", ERROR_EMPTY_DOCUMENT, doc.ErrorID() );
548 }
549
550 {
551 // Low entities
552 XMLDocument doc;
553 doc.Parse( "<test>&#x0e;</test>" );
554 const char result[] = { 0x0e, 0 };
555 XMLTest( "Low entities.", doc.FirstChildElement()->GetText(), result );
556 doc.Print();
557 }
558
559 {
560 // Attribute values with trailing quotes not handled correctly
561 XMLDocument doc;
562 doc.Parse( "<foo attribute=bar\" />" );
563 XMLTest( "Throw error with bad end quotes.", doc.Error(), true );
564 }
565
566 {
567 // [ 1663758 ] Failure to report error on bad XML
568 XMLDocument xml;
569 xml.Parse("<x>");
570 XMLTest("Missing end tag at end of input", xml.Error(), true);
571 xml.Parse("<x> ");
572 XMLTest("Missing end tag with trailing whitespace", xml.Error(), true);
573 xml.Parse("<x></y>");
574 XMLTest("Mismatched tags", xml.ErrorID(), ERROR_MISMATCHED_ELEMENT);
575 }
576
577
578 {
579 // [ 1475201 ] TinyXML parses entities in comments
580 XMLDocument xml;
581 xml.Parse("<!-- declarations for <head> & <body> -->"
582 "<!-- far &amp; away -->" );
583
584 XMLNode* e0 = xml.FirstChild();
585 XMLNode* e1 = e0->NextSibling();
586 XMLComment* c0 = e0->ToComment();
587 XMLComment* c1 = e1->ToComment();
588
589 XMLTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
590 XMLTest( "Comments ignore entities.", " far &amp; away ", c1->Value(), true );
591 }
592
593 {
594 XMLDocument xml;
595 xml.Parse( "<Parent>"
596 "<child1 att=''/>"
597 "<!-- With this comment, child2 will not be parsed! -->"
598 "<child2 att=''/>"
599 "</Parent>" );
600 int count = 0;
601
602 for( XMLNode* ele = xml.FirstChildElement( "Parent" )->FirstChild();
603 ele;
604 ele = ele->NextSibling() )
605 {
606 ++count;
607 }
608
609 XMLTest( "Comments iterate correctly.", 3, count );
610 }
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
618 XMLDocument doc;
619 doc.Parse( (const char*)buf);
620 }
621
622
623 {
624 // bug 1827248 Error while parsing a little bit malformed file
625 // Actually not malformed - should work.
626 XMLDocument xml;
627 xml.Parse( "<attributelist> </attributelist >" );
628 XMLTest( "Handle end tag whitespace", false, xml.Error() );
629 }
630
631 {
632 // This one must not result in an infinite loop
633 XMLDocument xml;
634 xml.Parse( "<infinite>loop" );
635 XMLTest( "Infinite loop test.", true, true );
636 }
637
638 #if defined( WIN32 )
Lee Thomason1ff38e02012-02-14 18:18:16 -0800639 _CrtMemCheckpoint( &endMemState );
640 //_CrtMemDumpStatistics( &endMemState );
641
642 _CrtMemState diffMemState;
643 _CrtMemDifference( &diffMemState, &startMemState, &endMemState );
644 _CrtMemDumpStatistics( &diffMemState );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800645 //printf( "new total=%d\n", gNewTotal );
Lee Thomason1ff38e02012-02-14 18:18:16 -0800646 #endif
647
648 printf ("\nPass %d, Fail %d\n", gPass, gFail);
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800649 return 0;
650}