blob: 9585d04edb7c69161ccd8234d4cc66d502867b00 [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
Lee Thomason (grinliz)6a22be22012-04-04 12:39:05 -070074// Comments in the header. (Don't know how to get Doxygen to read comments in this file.)
75int example_1()
76{
77 XMLDocument doc;
Bruno Diasa2d4e6e2012-05-07 04:58:11 -030078 doc.LoadFile( "resources/dream.xml" );
Lee Thomason (grinliz)6a22be22012-04-04 12:39:05 -070079
80 return doc.ErrorID();
81}
82
83
84// Comments in the header. (Don't know how to get Doxygen to read comments in this file.)
85int example_2()
86{
87 static const char* xml = "<element/>";
88 XMLDocument doc;
89 doc.Parse( xml );
90
91 return doc.ErrorID();
92}
93
94
95int example_3()
96{
Lee Thomason (grinliz)a4a36ba2012-04-06 21:24:29 -070097 static const char* xml =
98 "<?xml version=\"1.0\"?>"
99 "<!DOCTYPE PLAY SYSTEM \"play.dtd\">"
100 "<PLAY>"
101 "<TITLE>A Midsummer Night's Dream</TITLE>"
102 "</PLAY>";
Lee Thomason (grinliz)6a22be22012-04-04 12:39:05 -0700103
104 XMLDocument doc;
105 doc.Parse( xml );
106
107 XMLElement* titleElement = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" );
108 const char* title = titleElement->GetText();
109 printf( "Name of play (1): %s\n", title );
110
111 XMLText* textNode = titleElement->FirstChild()->ToText();
112 title = textNode->Value();
113 printf( "Name of play (2): %s\n", title );
114
115 return doc.ErrorID();
116}
117
118
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700119int main( int /*argc*/, const char ** /*argv*/ )
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800120{
Lee Thomason (grinliz)0a4df402012-02-27 20:50:52 -0800121 #if defined( _MSC_VER ) && defined( DEBUG )
Lee Thomason1ff38e02012-02-14 18:18:16 -0800122 _CrtMemCheckpoint( &startMemState );
123 #endif
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800124
Lee Thomason7f7b1622012-03-24 12:49:03 -0700125 #if defined(_MSC_VER)
126 #pragma warning ( push )
127 #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
128 #endif
129
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300130 FILE* fp = fopen( "resources/dream.xml", "r" );
Lee Thomason7f7b1622012-03-24 12:49:03 -0700131 if ( !fp ) {
132 printf( "Error opening test file 'dream.xml'.\n"
133 "Is your working directory the same as where \n"
134 "the xmltest.cpp and dream.xml file are?\n\n"
135 #if defined( _MSC_VER )
136 "In windows Visual Studio you may need to set\n"
137 "Properties->Debugging->Working Directory to '..'\n"
138 #endif
139 );
140 exit( 1 );
141 }
142 fclose( fp );
143
144 #if defined(_MSC_VER)
145 #pragma warning ( pop )
146 #endif
147
Lee Thomason (grinliz)6a22be22012-04-04 12:39:05 -0700148 XMLTest( "Example-1", 0, example_1() );
149 XMLTest( "Example-2", 0, example_2() );
150 XMLTest( "Example-3", 0, example_3() );
Lee Thomason87e475a2012-03-20 11:55:29 -0700151
Lee Thomason (grinliz)6a22be22012-04-04 12:39:05 -0700152 /* ------ Example 2: Lookup information. ---- */
Lee Thomason87e475a2012-03-20 11:55:29 -0700153
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800154 {
Lee Thomason43f59302012-02-06 18:18:11 -0800155 static const char* test[] = { "<element />",
156 "<element></element>",
157 "<element><subelement/></element>",
158 "<element><subelement></subelement></element>",
159 "<element><subelement><subsub/></subelement></element>",
160 "<!--comment beside elements--><element><subelement></subelement></element>",
161 "<!--comment beside elements, this time with spaces--> \n <element> <subelement> \n </subelement> </element>",
162 "<element attrib1='foo' attrib2=\"bar\" ></element>",
163 "<element attrib1='foo' attrib2=\"bar\" ><subelement attrib3='yeehaa' /></element>",
164 "<element>Text inside element.</element>",
165 "<element><b></b></element>",
166 "<element>Text inside and <b>bolded</b> in the element.</element>",
167 "<outer><element>Text inside and <b>bolded</b> in the element.</element></outer>",
Lee Thomason8ee79892012-01-25 17:44:30 -0800168 "<element>This &amp; That.</element>",
Lee Thomason18d68bd2012-01-26 18:17:26 -0800169 "<element attrib='This&lt;That' />",
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800170 0
171 };
Lee Thomason6ee99fc2012-01-21 18:45:16 -0800172 for( int i=0; test[i]; ++i ) {
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800173 XMLDocument doc;
Lee Thomason6ee99fc2012-01-21 18:45:16 -0800174 doc.Parse( test[i] );
Lee Thomason5cae8972012-01-24 18:03:07 -0800175 doc.Print();
Lee Thomasonec975ce2012-01-23 11:42:06 -0800176 printf( "----------------------------------------------\n" );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800177 }
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800178 }
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800179#if 1
Lee Thomasond6277762012-02-22 16:00:12 -0800180 {
181 static const char* test = "<!--hello world\n"
182 " line 2\r"
183 " line 3\r\n"
184 " line 4\n\r"
185 " line 5\r-->";
186
187 XMLDocument doc;
188 doc.Parse( test );
189 doc.Print();
190 }
191
Lee Thomason2c85a712012-01-31 08:24:24 -0800192 {
193 static const char* test = "<element>Text before.</element>";
194 XMLDocument doc;
195 doc.Parse( test );
196 XMLElement* root = doc.FirstChildElement();
197 XMLElement* newElement = doc.NewElement( "Subelement" );
198 root->InsertEndChild( newElement );
199 doc.Print();
200 }
Lee Thomasond1983222012-02-06 08:41:24 -0800201 {
202 XMLDocument* doc = new XMLDocument();
203 static const char* test = "<element><sub/></element>";
204 doc->Parse( test );
205 delete doc;
206 }
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800207 {
Lee Thomason1ff38e02012-02-14 18:18:16 -0800208 // Test: Programmatic DOM
Lee Thomasonec5a7b42012-02-13 18:16:52 -0800209 // Build:
210 // <element>
211 // <!--comment-->
212 // <sub attrib="1" />
213 // <sub attrib="2" />
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800214 // <sub attrib="3" >& Text!</sub>
Lee Thomasonec5a7b42012-02-13 18:16:52 -0800215 // <element>
216
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800217 XMLDocument* doc = new XMLDocument();
Lee Thomason1ff38e02012-02-14 18:18:16 -0800218 XMLNode* element = doc->InsertEndChild( doc->NewElement( "element" ) );
219
220 XMLElement* sub[3] = { doc->NewElement( "sub" ), doc->NewElement( "sub" ), doc->NewElement( "sub" ) };
221 for( int i=0; i<3; ++i ) {
222 sub[i]->SetAttribute( "attrib", i );
223 }
224 element->InsertEndChild( sub[2] );
225 XMLNode* comment = element->InsertFirstChild( doc->NewComment( "comment" ) );
226 element->InsertAfterChild( comment, sub[0] );
227 element->InsertAfterChild( sub[0], sub[1] );
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800228 sub[2]->InsertFirstChild( doc->NewText( "& Text!" ));
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800229 doc->Print();
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800230 XMLTest( "Programmatic DOM", "comment", doc->FirstChildElement( "element" )->FirstChild()->Value() );
231 XMLTest( "Programmatic DOM", "0", doc->FirstChildElement( "element" )->FirstChildElement()->Attribute( "attrib" ) );
232 XMLTest( "Programmatic DOM", 2, doc->FirstChildElement()->LastChildElement( "sub" )->IntAttribute( "attrib" ) );
233 XMLTest( "Programmatic DOM", "& Text!",
234 doc->FirstChildElement()->LastChildElement( "sub" )->FirstChild()->ToText()->Value() );
U-Stream\Leeae25a442012-02-17 17:48:16 -0800235
236 // And now deletion:
237 element->DeleteChild( sub[2] );
238 doc->DeleteNode( comment );
239
240 element->FirstChildElement()->SetAttribute( "attrib", true );
241 element->LastChildElement()->DeleteAttribute( "attrib" );
242
243 XMLTest( "Programmatic DOM", true, doc->FirstChildElement()->FirstChildElement()->BoolAttribute( "attrib" ) );
244 int value = 10;
245 int result = doc->FirstChildElement()->LastChildElement()->QueryIntAttribute( "attrib", &value );
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700246 XMLTest( "Programmatic DOM", result, XML_NO_ATTRIBUTE );
U-Stream\Leeae25a442012-02-17 17:48:16 -0800247 XMLTest( "Programmatic DOM", value, 10 );
248
249 doc->Print();
250
Lee Thomason7b1b86a2012-06-04 17:01:38 -0700251 {
252 XMLPrinter streamer;
253 doc->Print( &streamer );
254 printf( "%s", streamer.CStr() );
255 }
256 {
257 XMLPrinter streamer( 0, true );
258 doc->Print( &streamer );
259 XMLTest( "Compact mode", "<element><sub attrib=\"1\"/><sub/></element>", streamer.CStr(), false );
260 }
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800261 delete doc;
262 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800263 {
264 // Test: Dream
265 // XML1 : 1,187,569 bytes in 31,209 allocations
266 // XML2 : 469,073 bytes in 323 allocations
267 //int newStart = gNew;
268 XMLDocument doc;
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300269 doc.LoadFile( "resources/dream.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800270
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300271 doc.SaveFile( "resources/dreamout.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800272 doc.PrintError();
273
274 XMLTest( "Dream", "xml version=\"1.0\"",
275 doc.FirstChild()->ToDeclaration()->Value() );
276 XMLTest( "Dream", true, doc.FirstChild()->NextSibling()->ToUnknown() ? true : false );
277 XMLTest( "Dream", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
278 doc.FirstChild()->NextSibling()->ToUnknown()->Value() );
279 XMLTest( "Dream", "And Robin shall restore amends.",
280 doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
281 XMLTest( "Dream", "And Robin shall restore amends.",
282 doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
283
284 XMLDocument doc2;
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300285 doc2.LoadFile( "resources/dreamout.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800286 XMLTest( "Dream-out", "xml version=\"1.0\"",
287 doc2.FirstChild()->ToDeclaration()->Value() );
288 XMLTest( "Dream-out", true, doc2.FirstChild()->NextSibling()->ToUnknown() ? true : false );
289 XMLTest( "Dream-out", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
290 doc2.FirstChild()->NextSibling()->ToUnknown()->Value() );
291 XMLTest( "Dream-out", "And Robin shall restore amends.",
292 doc2.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
293
294 //gNewTotal = gNew - newStart;
295 }
Lee Thomason6f381b72012-03-02 12:59:39 -0800296
297
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800298 {
299 const char* error = "<?xml version=\"1.0\" standalone=\"no\" ?>\n"
300 "<passages count=\"006\" formatversion=\"20020620\">\n"
301 " <wrong error>\n"
302 "</passages>";
303
304 XMLDocument doc;
305 doc.Parse( error );
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700306 XMLTest( "Bad XML", doc.ErrorID(), XML_ERROR_PARSING_ATTRIBUTE );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800307 }
308
309 {
310 const char* str = "<doc attr0='1' attr1='2.0' attr2='foo' />";
311
312 XMLDocument doc;
313 doc.Parse( str );
314
315 XMLElement* ele = doc.FirstChildElement();
316
317 int iVal, result;
318 double dVal;
319
320 result = ele->QueryDoubleAttribute( "attr0", &dVal );
321 XMLTest( "Query attribute: int as double", result, XML_NO_ERROR );
322 XMLTest( "Query attribute: int as double", (int)dVal, 1 );
323 result = ele->QueryDoubleAttribute( "attr1", &dVal );
324 XMLTest( "Query attribute: double as double", (int)dVal, 2 );
325 result = ele->QueryIntAttribute( "attr1", &iVal );
326 XMLTest( "Query attribute: double as int", result, XML_NO_ERROR );
327 XMLTest( "Query attribute: double as int", iVal, 2 );
328 result = ele->QueryIntAttribute( "attr2", &iVal );
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700329 XMLTest( "Query attribute: not a number", result, XML_WRONG_ATTRIBUTE_TYPE );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800330 result = ele->QueryIntAttribute( "bar", &iVal );
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700331 XMLTest( "Query attribute: does not exist", result, XML_NO_ATTRIBUTE );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800332 }
333
334 {
335 const char* str = "<doc/>";
336
337 XMLDocument doc;
338 doc.Parse( str );
339
340 XMLElement* ele = doc.FirstChildElement();
341
342 int iVal;
343 double dVal;
344
345 ele->SetAttribute( "str", "strValue" );
346 ele->SetAttribute( "int", 1 );
347 ele->SetAttribute( "double", -1.0 );
348
349 const char* cStr = ele->Attribute( "str" );
350 ele->QueryIntAttribute( "int", &iVal );
351 ele->QueryDoubleAttribute( "double", &dVal );
352
Lee Thomason8ba7f7d2012-03-24 13:04:04 -0700353 XMLTest( "Attribute match test", ele->Attribute( "str", "strValue" ), "strValue" );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800354 XMLTest( "Attribute round trip. c-string.", "strValue", cStr );
355 XMLTest( "Attribute round trip. int.", 1, iVal );
356 XMLTest( "Attribute round trip. double.", -1, (int)dVal );
357 }
358
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800359 {
360 XMLDocument doc;
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300361 doc.LoadFile( "resources/utf8test.xml" );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800362
363 // Get the attribute "value" from the "Russian" element and check it.
364 XMLElement* element = doc.FirstChildElement( "document" )->FirstChildElement( "Russian" );
365 const unsigned char correctValue[] = { 0xd1U, 0x86U, 0xd0U, 0xb5U, 0xd0U, 0xbdU, 0xd0U, 0xbdU,
366 0xd0U, 0xbeU, 0xd1U, 0x81U, 0xd1U, 0x82U, 0xd1U, 0x8cU, 0 };
367
368 XMLTest( "UTF-8: Russian value.", (const char*)correctValue, element->Attribute( "value" ) );
369
370 const unsigned char russianElementName[] = { 0xd0U, 0xa0U, 0xd1U, 0x83U,
371 0xd1U, 0x81U, 0xd1U, 0x81U,
372 0xd0U, 0xbaU, 0xd0U, 0xb8U,
373 0xd0U, 0xb9U, 0 };
374 const char russianText[] = "<\xD0\xB8\xD0\xBC\xD0\xB5\xD0\xB5\xD1\x82>";
375
376 XMLText* text = doc.FirstChildElement( "document" )->FirstChildElement( (const char*) russianElementName )->FirstChild()->ToText();
377 XMLTest( "UTF-8: Browsing russian element name.",
378 russianText,
379 text->Value() );
380
381 // Now try for a round trip.
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300382 doc.SaveFile( "resources/utf8testout.xml" );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800383
384 // Check the round trip.
385 char savedBuf[256];
386 char verifyBuf[256];
387 int okay = 0;
388
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800389
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800390#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800391#pragma warning ( push )
392#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800393#endif
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300394 FILE* saved = fopen( "resources/utf8testout.xml", "r" );
395 FILE* verify = fopen( "resources/utf8testverify.xml", "r" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800396#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800397#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800398#endif
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800399
400 if ( saved && verify )
401 {
402 okay = 1;
403 while ( fgets( verifyBuf, 256, verify ) )
404 {
405 fgets( savedBuf, 256, saved );
406 NullLineEndings( verifyBuf );
407 NullLineEndings( savedBuf );
408
409 if ( strcmp( verifyBuf, savedBuf ) )
410 {
411 printf( "verify:%s<\n", verifyBuf );
412 printf( "saved :%s<\n", savedBuf );
413 okay = 0;
414 break;
415 }
416 }
417 }
418 if ( saved )
419 fclose( saved );
420 if ( verify )
421 fclose( verify );
422 XMLTest( "UTF-8: Verified multi-language round trip.", 1, okay );
423 }
424
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800425 // --------GetText()-----------
426 {
427 const char* str = "<foo>This is text</foo>";
428 XMLDocument doc;
429 doc.Parse( str );
430 const XMLElement* element = doc.RootElement();
431
432 XMLTest( "GetText() normal use.", "This is text", element->GetText() );
433
434 str = "<foo><b>This is text</b></foo>";
435 doc.Parse( str );
436 element = doc.RootElement();
437
438 XMLTest( "GetText() contained element.", element->GetText() == 0, true );
439 }
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800440
Lee Thomasond6277762012-02-22 16:00:12 -0800441
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800442 // ---------- CDATA ---------------
443 {
444 const char* str = "<xmlElement>"
445 "<![CDATA["
446 "I am > the rules!\n"
447 "...since I make symbolic puns"
448 "]]>"
449 "</xmlElement>";
450 XMLDocument doc;
451 doc.Parse( str );
452 doc.Print();
Lee Thomasond6277762012-02-22 16:00:12 -0800453
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800454 XMLTest( "CDATA parse.", doc.FirstChildElement()->FirstChild()->Value(),
455 "I am > the rules!\n...since I make symbolic puns",
Lee Thomasond6277762012-02-22 16:00:12 -0800456 false );
457 }
458
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800459 // ----------- CDATA -------------
460 {
461 const char* str = "<xmlElement>"
462 "<![CDATA["
463 "<b>I am > the rules!</b>\n"
464 "...since I make symbolic puns"
465 "]]>"
466 "</xmlElement>";
467 XMLDocument doc;
468 doc.Parse( str );
469 doc.Print();
470
471 XMLTest( "CDATA parse. [ tixml1:1480107 ]", doc.FirstChildElement()->FirstChild()->Value(),
472 "<b>I am > the rules!</b>\n...since I make symbolic puns",
473 false );
474 }
475
476 // InsertAfterChild causes crash.
477 {
478 // InsertBeforeChild and InsertAfterChild causes crash.
479 XMLDocument doc;
480 XMLElement* parent = doc.NewElement( "Parent" );
481 doc.InsertFirstChild( parent );
482
483 XMLElement* childText0 = doc.NewElement( "childText0" );
484 XMLElement* childText1 = doc.NewElement( "childText1" );
485
486 XMLNode* childNode0 = parent->InsertEndChild( childText0 );
487 XMLNode* childNode1 = parent->InsertAfterChild( childNode0, childText1 );
488
489 XMLTest( "Test InsertAfterChild on empty node. ", ( childNode1 == parent->LastChild() ), true );
490 }
Lee Thomasond6277762012-02-22 16:00:12 -0800491
492 {
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800493 // Entities not being written correctly.
494 // From Lynn Allen
Lee Thomasond6277762012-02-22 16:00:12 -0800495
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800496 const char* passages =
497 "<?xml version=\"1.0\" standalone=\"no\" ?>"
498 "<passages count=\"006\" formatversion=\"20020620\">"
499 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
500 " It also has &lt;, &gt;, and &amp;, as well as a fake copyright &#xA9;.\"> </psg>"
501 "</passages>";
Lee Thomasond6277762012-02-22 16:00:12 -0800502
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800503 XMLDocument doc;
504 doc.Parse( passages );
505 XMLElement* psg = doc.RootElement()->FirstChildElement();
506 const char* context = psg->Attribute( "context" );
507 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 -0800508
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800509 XMLTest( "Entity transformation: read. ", expected, context, true );
Lee Thomasond6277762012-02-22 16:00:12 -0800510
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800511#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800512#pragma warning ( push )
513#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800514#endif
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300515 FILE* textfile = fopen( "resources/textfile.txt", "w" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800516#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800517#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800518#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800519 if ( textfile )
520 {
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800521 XMLPrinter streamer( textfile );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800522 psg->Accept( &streamer );
523 fclose( textfile );
524 }
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800525#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800526#pragma warning ( push )
527#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800528#endif
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300529 textfile = fopen( "resources/textfile.txt", "r" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800530#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800531#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800532#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800533 TIXMLASSERT( textfile );
534 if ( textfile )
535 {
536 char buf[ 1024 ];
537 fgets( buf, 1024, textfile );
538 XMLTest( "Entity transformation: write. ",
539 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
540 " It also has &lt;, &gt;, and &amp;, as well as a fake copyright \xC2\xA9.\"/>\n",
541 buf, false );
542 }
543 fclose( textfile );
544 }
545
546 {
Lee Thomason6f381b72012-03-02 12:59:39 -0800547 // Suppress entities.
548 const char* passages =
549 "<?xml version=\"1.0\" standalone=\"no\" ?>"
550 "<passages count=\"006\" formatversion=\"20020620\">"
551 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;.\">Crazy &ttk;</psg>"
552 "</passages>";
553
554 XMLDocument doc( false );
555 doc.Parse( passages );
556
557 XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->Attribute( "context" ),
558 "Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;." );
559 XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->FirstChild()->Value(),
560 "Crazy &ttk;" );
561 doc.Print();
562 }
563
564 {
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800565 const char* test = "<?xml version='1.0'?><a.elem xmi.version='2.0'/>";
566
567 XMLDocument doc;
568 doc.Parse( test );
569 XMLTest( "dot in names", doc.Error(), 0);
570 XMLTest( "dot in names", doc.FirstChildElement()->Name(), "a.elem" );
571 XMLTest( "dot in names", doc.FirstChildElement()->Attribute( "xmi.version" ), "2.0" );
572 }
573
574 {
575 const char* test = "<element><Name>1.1 Start easy ignore fin thickness&#xA;</Name></element>";
576
577 XMLDocument doc;
578 doc.Parse( test );
579
580 XMLText* text = doc.FirstChildElement()->FirstChildElement()->FirstChild()->ToText();
581 XMLTest( "Entity with one digit.",
582 text->Value(), "1.1 Start easy ignore fin thickness\n",
583 false );
584 }
585
586 {
587 // DOCTYPE not preserved (950171)
588 //
589 const char* doctype =
590 "<?xml version=\"1.0\" ?>"
591 "<!DOCTYPE PLAY SYSTEM 'play.dtd'>"
592 "<!ELEMENT title (#PCDATA)>"
593 "<!ELEMENT books (title,authors)>"
594 "<element />";
595
596 XMLDocument doc;
597 doc.Parse( doctype );
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300598 doc.SaveFile( "resources/test7.xml" );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800599 doc.DeleteChild( doc.RootElement() );
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300600 doc.LoadFile( "resources/test7.xml" );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800601 doc.Print();
602
603 const XMLUnknown* decl = doc.FirstChild()->NextSibling()->ToUnknown();
604 XMLTest( "Correct value of unknown.", "DOCTYPE PLAY SYSTEM 'play.dtd'", decl->Value() );
605
606 }
607
608 {
609 // Comments do not stream out correctly.
610 const char* doctype =
611 "<!-- Somewhat<evil> -->";
612 XMLDocument doc;
613 doc.Parse( doctype );
614
615 XMLComment* comment = doc.FirstChild()->ToComment();
616
617 XMLTest( "Comment formatting.", " Somewhat<evil> ", comment->Value() );
618 }
619 {
620 // Double attributes
621 const char* doctype = "<element attr='red' attr='blue' />";
622
623 XMLDocument doc;
624 doc.Parse( doctype );
625
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700626 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 -0800627 doc.PrintError();
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800628 }
629
630 {
631 // Embedded null in stream.
632 const char* doctype = "<element att\0r='red' attr='blue' />";
633
634 XMLDocument doc;
635 doc.Parse( doctype );
636 XMLTest( "Embedded null throws error.", true, doc.Error() );
637 }
638
639 {
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700640 // Empty documents should return TIXML_XML_ERROR_PARSING_EMPTY, bug 1070717
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800641 const char* str = " ";
642 XMLDocument doc;
643 doc.Parse( str );
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700644 XMLTest( "Empty document error", XML_ERROR_EMPTY_DOCUMENT, doc.ErrorID() );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800645 }
646
647 {
648 // Low entities
649 XMLDocument doc;
650 doc.Parse( "<test>&#x0e;</test>" );
651 const char result[] = { 0x0e, 0 };
652 XMLTest( "Low entities.", doc.FirstChildElement()->GetText(), result );
653 doc.Print();
654 }
655
656 {
657 // Attribute values with trailing quotes not handled correctly
658 XMLDocument doc;
659 doc.Parse( "<foo attribute=bar\" />" );
660 XMLTest( "Throw error with bad end quotes.", doc.Error(), true );
661 }
662
663 {
664 // [ 1663758 ] Failure to report error on bad XML
665 XMLDocument xml;
666 xml.Parse("<x>");
667 XMLTest("Missing end tag at end of input", xml.Error(), true);
668 xml.Parse("<x> ");
669 XMLTest("Missing end tag with trailing whitespace", xml.Error(), true);
670 xml.Parse("<x></y>");
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700671 XMLTest("Mismatched tags", xml.ErrorID(), XML_ERROR_MISMATCHED_ELEMENT);
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800672 }
673
674
675 {
676 // [ 1475201 ] TinyXML parses entities in comments
677 XMLDocument xml;
678 xml.Parse("<!-- declarations for <head> & <body> -->"
679 "<!-- far &amp; away -->" );
680
681 XMLNode* e0 = xml.FirstChild();
682 XMLNode* e1 = e0->NextSibling();
683 XMLComment* c0 = e0->ToComment();
684 XMLComment* c1 = e1->ToComment();
685
686 XMLTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
687 XMLTest( "Comments ignore entities.", " far &amp; away ", c1->Value(), true );
688 }
689
690 {
691 XMLDocument xml;
692 xml.Parse( "<Parent>"
693 "<child1 att=''/>"
694 "<!-- With this comment, child2 will not be parsed! -->"
695 "<child2 att=''/>"
696 "</Parent>" );
697 xml.Print();
698
699 int count = 0;
700
701 for( XMLNode* ele = xml.FirstChildElement( "Parent" )->FirstChild();
702 ele;
703 ele = ele->NextSibling() )
704 {
705 ++count;
706 }
707
708 XMLTest( "Comments iterate correctly.", 3, count );
709 }
710
711 {
712 // trying to repro ]1874301]. If it doesn't go into an infinite loop, all is well.
713 unsigned char buf[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?><feed><![CDATA[Test XMLblablablalblbl";
714 buf[60] = 239;
715 buf[61] = 0;
716
717 XMLDocument doc;
718 doc.Parse( (const char*)buf);
719 }
720
721
722 {
723 // bug 1827248 Error while parsing a little bit malformed file
724 // Actually not malformed - should work.
725 XMLDocument xml;
726 xml.Parse( "<attributelist> </attributelist >" );
727 XMLTest( "Handle end tag whitespace", false, xml.Error() );
728 }
729
730 {
731 // This one must not result in an infinite loop
732 XMLDocument xml;
733 xml.Parse( "<infinite>loop" );
734 XMLTest( "Infinite loop test.", true, true );
735 }
736#endif
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800737 {
738 const char* pub = "<?xml version='1.0'?> <element><sub/></element> <!--comment--> <!DOCTYPE>";
739 XMLDocument doc;
740 doc.Parse( pub );
741
742 XMLDocument clone;
743 for( const XMLNode* node=doc.FirstChild(); node; node=node->NextSibling() ) {
744 XMLNode* copy = node->ShallowClone( &clone );
745 clone.InsertEndChild( copy );
746 }
747
748 clone.Print();
749
750 int count=0;
751 const XMLNode* a=clone.FirstChild();
752 const XMLNode* b=doc.FirstChild();
753 for( ; a && b; a=a->NextSibling(), b=b->NextSibling() ) {
754 ++count;
755 XMLTest( "Clone and Equal", true, a->ShallowEqual( b ));
756 }
757 XMLTest( "Clone and Equal", 4, count );
758 }
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800759
Lee Thomason (grinliz)a4a36ba2012-04-06 21:24:29 -0700760 {
761 // This shouldn't crash.
762 XMLDocument doc;
763 if(XML_NO_ERROR != doc.LoadFile( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ))
764 {
765 doc.PrintError();
766 }
767 XMLTest( "Error in snprinf handling.", true, doc.Error() );
768 }
Lee Thomason5e3803c2012-04-16 08:57:05 -0700769
770 {
771 // Attribute ordering.
772 static const char* xml = "<element attrib1=\"1\" attrib2=\"2\" attrib3=\"3\" />";
773 XMLDocument doc;
774 doc.Parse( xml );
775 XMLElement* ele = doc.FirstChildElement();
776
777 const XMLAttribute* a = ele->FirstAttribute();
778 XMLTest( "Attribute order", "1", a->Value() );
779 a = a->Next();
780 XMLTest( "Attribute order", "2", a->Value() );
781 a = a->Next();
782 XMLTest( "Attribute order", "3", a->Value() );
783 XMLTest( "Attribute order", "attrib3", a->Name() );
784
785 ele->DeleteAttribute( "attrib2" );
786 a = ele->FirstAttribute();
787 XMLTest( "Attribute order", "1", a->Value() );
788 a = a->Next();
789 XMLTest( "Attribute order", "3", a->Value() );
790
791 ele->DeleteAttribute( "attrib1" );
792 ele->DeleteAttribute( "attrib3" );
793 XMLTest( "Attribute order (empty)", false, ele->FirstAttribute() ? true : false );
794 }
Lee Thomason (grinliz)a4a36ba2012-04-06 21:24:29 -0700795
Lee Thomason (grinliz)390e9782012-07-01 21:22:53 -0700796 {
797 // Make sure an attribute with a space in it succeeds.
798 static const char* xml = "<element attribute1=\"Test Attribute\"/>";
799 XMLDocument doc;
800 doc.Parse( xml );
801
802 XMLElement* ele = doc.FirstChildElement();
803 XMLTest( "Attribute with space", "Test Attribute", ele->Attribute( "attribute1" ) );
804 }
805
806 {
807 // Make sure we don't go into an infinite loop.
808 static const char* xml = "<doc><element attribute='attribute'/><element attribute='attribute'/></doc>";
809 XMLDocument doc;
810 doc.Parse( xml );
811 XMLElement* ele0 = doc.FirstChildElement()->FirstChildElement();
812 XMLElement* ele1 = ele0->NextSiblingElement();
813 bool equal = ele0->ShallowEqual( ele1 );
814
815 XMLTest( "Infinite loop in shallow equal.", true, equal );
816 }
817
Lee Thomason5708f812012-03-28 17:46:41 -0700818 // -------- Handles ------------
819 {
820 static const char* xml = "<element attrib='bar'><sub>Text</sub></element>";
821 XMLDocument doc;
822 doc.Parse( xml );
Lee Thomason5708f812012-03-28 17:46:41 -0700823
824 XMLElement* ele = XMLHandle( doc ).FirstChildElement( "element" ).FirstChild().ToElement();
825 XMLTest( "Handle, success, mutable", ele->Value(), "sub" );
826
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -0700827 XMLHandle docH( doc );
828 ele = docH.FirstChildElement( "none" ).FirstChildElement( "element" ).ToElement();
Lee Thomasond0b19df2012-04-12 08:41:37 -0700829 XMLTest( "Handle, dne, mutable", false, ele != 0 );
Lee Thomason5708f812012-03-28 17:46:41 -0700830 }
831
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -0700832 {
833 static const char* xml = "<element attrib='bar'><sub>Text</sub></element>";
834 XMLDocument doc;
835 doc.Parse( xml );
836 XMLConstHandle docH( doc );
837
838 const XMLElement* ele = docH.FirstChildElement( "element" ).FirstChild().ToElement();
839 XMLTest( "Handle, success, const", ele->Value(), "sub" );
840
841 ele = docH.FirstChildElement( "none" ).FirstChildElement( "element" ).ToElement();
Lee Thomasond0b19df2012-04-12 08:41:37 -0700842 XMLTest( "Handle, dne, const", false, ele != 0 );
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -0700843 }
Lee Thomasonf68c4382012-04-28 14:37:11 -0700844 {
845 // Default Declaration & BOM
846 XMLDocument doc;
847 doc.InsertEndChild( doc.NewDeclaration() );
848 doc.SetBOM( true );
849
850 XMLPrinter printer;
851 doc.Print( &printer );
852
853 static const char* result = "\xef\xbb\xbf<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
854 XMLTest( "BOM and default declaration", printer.CStr(), result, false );
Lee Thomason (grinliz)48ea0bc2012-05-26 14:41:14 -0700855 XMLTest( "CStrSize", printer.CStrSize(), 42, false );
Lee Thomasonf68c4382012-04-28 14:37:11 -0700856 }
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -0700857
858
Lee Thomason6f381b72012-03-02 12:59:39 -0800859 // ----------- Performance tracking --------------
860 {
861#if defined( _MSC_VER )
862 __int64 start, end, freq;
863 QueryPerformanceFrequency( (LARGE_INTEGER*) &freq );
864#endif
865
866#if defined(_MSC_VER)
867#pragma warning ( push )
868#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
869#endif
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300870 FILE* fp = fopen( "resources/dream.xml", "r" );
Lee Thomason6f381b72012-03-02 12:59:39 -0800871#if defined(_MSC_VER)
872#pragma warning ( pop )
873#endif
874 fseek( fp, 0, SEEK_END );
875 long size = ftell( fp );
876 fseek( fp, 0, SEEK_SET );
877
878 char* mem = new char[size+1];
879 fread( mem, size, 1, fp );
880 fclose( fp );
881 mem[size] = 0;
882
883#if defined( _MSC_VER )
884 QueryPerformanceCounter( (LARGE_INTEGER*) &start );
885#else
886 clock_t cstart = clock();
887#endif
888 static const int COUNT = 10;
889 for( int i=0; i<COUNT; ++i ) {
890 XMLDocument doc;
891 doc.Parse( mem );
892 }
893#if defined( _MSC_VER )
894 QueryPerformanceCounter( (LARGE_INTEGER*) &end );
895#else
896 clock_t cend = clock();
897#endif
898
899 delete [] mem;
900
901 static const char* note =
902#ifdef DEBUG
903 "DEBUG";
904#else
905 "Release";
906#endif
907
908#if defined( _MSC_VER )
909 printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, 1000.0 * (double)(end-start) / ( (double)freq * (double)COUNT) );
910#else
911 printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, (double)(cend - cstart)/(double)COUNT );
912#endif
913 }
914
Lee Thomason (grinliz)7ca55582012-03-07 21:54:57 -0800915 #if defined( _MSC_VER ) && defined( DEBUG )
Lee Thomason1ff38e02012-02-14 18:18:16 -0800916 _CrtMemCheckpoint( &endMemState );
917 //_CrtMemDumpStatistics( &endMemState );
918
919 _CrtMemState diffMemState;
920 _CrtMemDifference( &diffMemState, &startMemState, &endMemState );
921 _CrtMemDumpStatistics( &diffMemState );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800922 //printf( "new total=%d\n", gNewTotal );
Lee Thomason1ff38e02012-02-14 18:18:16 -0800923 #endif
924
925 printf ("\nPass %d, Fail %d\n", gPass, gFail);
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800926 return 0;
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -0800927}