blob: 7916a053b101b0543153943b933754e0acb9a85e [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
Lee Thomason21be8822012-07-15 17:27:22 -070041template< class T > bool XMLTest( const char* testString, T expected, T 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
Lee Thomason21be8822012-07-15 17:27:22 -0700119bool example_4()
120{
121 static const char* xml =
122 "<information>"
123 " <attributeApproach v='2' />"
124 " <textApproach>"
125 " <v>2</v>"
126 " </textApproach>"
127 "</information>";
128
129 XMLDocument doc;
130 doc.Parse( xml );
131
132 int v0 = 0;
133 int v1 = 0;
134
135 XMLElement* attributeApproachElement = doc.FirstChildElement()->FirstChildElement( "attributeApproach" );
136 attributeApproachElement->QueryIntAttribute( "v", &v0 );
137
138 XMLElement* textApproachElement = doc.FirstChildElement()->FirstChildElement( "textApproach" );
139 textApproachElement->FirstChildElement( "v" )->QueryIntText( &v1 );
140
141 printf( "Both values are the same: %d and %d\n", v0, v1 );
142
143 return !doc.Error() && ( v0 == v1 );
144}
145
146
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700147int main( int /*argc*/, const char ** /*argv*/ )
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800148{
Lee Thomason (grinliz)0a4df402012-02-27 20:50:52 -0800149 #if defined( _MSC_VER ) && defined( DEBUG )
Lee Thomason1ff38e02012-02-14 18:18:16 -0800150 _CrtMemCheckpoint( &startMemState );
151 #endif
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800152
Lee Thomason7f7b1622012-03-24 12:49:03 -0700153 #if defined(_MSC_VER)
154 #pragma warning ( push )
155 #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
156 #endif
157
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300158 FILE* fp = fopen( "resources/dream.xml", "r" );
Lee Thomason7f7b1622012-03-24 12:49:03 -0700159 if ( !fp ) {
160 printf( "Error opening test file 'dream.xml'.\n"
161 "Is your working directory the same as where \n"
162 "the xmltest.cpp and dream.xml file are?\n\n"
163 #if defined( _MSC_VER )
164 "In windows Visual Studio you may need to set\n"
165 "Properties->Debugging->Working Directory to '..'\n"
166 #endif
167 );
168 exit( 1 );
169 }
170 fclose( fp );
171
172 #if defined(_MSC_VER)
173 #pragma warning ( pop )
174 #endif
175
Lee Thomason (grinliz)6a22be22012-04-04 12:39:05 -0700176 XMLTest( "Example-1", 0, example_1() );
177 XMLTest( "Example-2", 0, example_2() );
178 XMLTest( "Example-3", 0, example_3() );
Lee Thomason21be8822012-07-15 17:27:22 -0700179 XMLTest( "Example-4", true, example_4() );
Lee Thomason87e475a2012-03-20 11:55:29 -0700180
Lee Thomason (grinliz)6a22be22012-04-04 12:39:05 -0700181 /* ------ Example 2: Lookup information. ---- */
Lee Thomason87e475a2012-03-20 11:55:29 -0700182
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800183 {
Lee Thomason43f59302012-02-06 18:18:11 -0800184 static const char* test[] = { "<element />",
185 "<element></element>",
186 "<element><subelement/></element>",
187 "<element><subelement></subelement></element>",
188 "<element><subelement><subsub/></subelement></element>",
189 "<!--comment beside elements--><element><subelement></subelement></element>",
190 "<!--comment beside elements, this time with spaces--> \n <element> <subelement> \n </subelement> </element>",
191 "<element attrib1='foo' attrib2=\"bar\" ></element>",
192 "<element attrib1='foo' attrib2=\"bar\" ><subelement attrib3='yeehaa' /></element>",
193 "<element>Text inside element.</element>",
194 "<element><b></b></element>",
195 "<element>Text inside and <b>bolded</b> in the element.</element>",
196 "<outer><element>Text inside and <b>bolded</b> in the element.</element></outer>",
Lee Thomason8ee79892012-01-25 17:44:30 -0800197 "<element>This &amp; That.</element>",
Lee Thomason18d68bd2012-01-26 18:17:26 -0800198 "<element attrib='This&lt;That' />",
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800199 0
200 };
Lee Thomason6ee99fc2012-01-21 18:45:16 -0800201 for( int i=0; test[i]; ++i ) {
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800202 XMLDocument doc;
Lee Thomason6ee99fc2012-01-21 18:45:16 -0800203 doc.Parse( test[i] );
Lee Thomason5cae8972012-01-24 18:03:07 -0800204 doc.Print();
Lee Thomasonec975ce2012-01-23 11:42:06 -0800205 printf( "----------------------------------------------\n" );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800206 }
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800207 }
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800208#if 1
Lee Thomasond6277762012-02-22 16:00:12 -0800209 {
210 static const char* test = "<!--hello world\n"
211 " line 2\r"
212 " line 3\r\n"
213 " line 4\n\r"
214 " line 5\r-->";
215
216 XMLDocument doc;
217 doc.Parse( test );
218 doc.Print();
219 }
220
Lee Thomason2c85a712012-01-31 08:24:24 -0800221 {
222 static const char* test = "<element>Text before.</element>";
223 XMLDocument doc;
224 doc.Parse( test );
225 XMLElement* root = doc.FirstChildElement();
226 XMLElement* newElement = doc.NewElement( "Subelement" );
227 root->InsertEndChild( newElement );
228 doc.Print();
229 }
Lee Thomasond1983222012-02-06 08:41:24 -0800230 {
231 XMLDocument* doc = new XMLDocument();
232 static const char* test = "<element><sub/></element>";
233 doc->Parse( test );
234 delete doc;
235 }
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800236 {
Lee Thomason1ff38e02012-02-14 18:18:16 -0800237 // Test: Programmatic DOM
Lee Thomasonec5a7b42012-02-13 18:16:52 -0800238 // Build:
239 // <element>
240 // <!--comment-->
241 // <sub attrib="1" />
242 // <sub attrib="2" />
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800243 // <sub attrib="3" >& Text!</sub>
Lee Thomasonec5a7b42012-02-13 18:16:52 -0800244 // <element>
245
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800246 XMLDocument* doc = new XMLDocument();
Lee Thomason1ff38e02012-02-14 18:18:16 -0800247 XMLNode* element = doc->InsertEndChild( doc->NewElement( "element" ) );
248
249 XMLElement* sub[3] = { doc->NewElement( "sub" ), doc->NewElement( "sub" ), doc->NewElement( "sub" ) };
250 for( int i=0; i<3; ++i ) {
251 sub[i]->SetAttribute( "attrib", i );
252 }
253 element->InsertEndChild( sub[2] );
254 XMLNode* comment = element->InsertFirstChild( doc->NewComment( "comment" ) );
255 element->InsertAfterChild( comment, sub[0] );
256 element->InsertAfterChild( sub[0], sub[1] );
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800257 sub[2]->InsertFirstChild( doc->NewText( "& Text!" ));
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800258 doc->Print();
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800259 XMLTest( "Programmatic DOM", "comment", doc->FirstChildElement( "element" )->FirstChild()->Value() );
260 XMLTest( "Programmatic DOM", "0", doc->FirstChildElement( "element" )->FirstChildElement()->Attribute( "attrib" ) );
261 XMLTest( "Programmatic DOM", 2, doc->FirstChildElement()->LastChildElement( "sub" )->IntAttribute( "attrib" ) );
262 XMLTest( "Programmatic DOM", "& Text!",
263 doc->FirstChildElement()->LastChildElement( "sub" )->FirstChild()->ToText()->Value() );
U-Stream\Leeae25a442012-02-17 17:48:16 -0800264
265 // And now deletion:
266 element->DeleteChild( sub[2] );
267 doc->DeleteNode( comment );
268
269 element->FirstChildElement()->SetAttribute( "attrib", true );
270 element->LastChildElement()->DeleteAttribute( "attrib" );
271
272 XMLTest( "Programmatic DOM", true, doc->FirstChildElement()->FirstChildElement()->BoolAttribute( "attrib" ) );
273 int value = 10;
274 int result = doc->FirstChildElement()->LastChildElement()->QueryIntAttribute( "attrib", &value );
Lee Thomason21be8822012-07-15 17:27:22 -0700275 XMLTest( "Programmatic DOM", result, (int)XML_NO_ATTRIBUTE );
U-Stream\Leeae25a442012-02-17 17:48:16 -0800276 XMLTest( "Programmatic DOM", value, 10 );
277
278 doc->Print();
279
Lee Thomason7b1b86a2012-06-04 17:01:38 -0700280 {
281 XMLPrinter streamer;
282 doc->Print( &streamer );
283 printf( "%s", streamer.CStr() );
284 }
285 {
286 XMLPrinter streamer( 0, true );
287 doc->Print( &streamer );
288 XMLTest( "Compact mode", "<element><sub attrib=\"1\"/><sub/></element>", streamer.CStr(), false );
289 }
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800290 delete doc;
291 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800292 {
293 // Test: Dream
294 // XML1 : 1,187,569 bytes in 31,209 allocations
295 // XML2 : 469,073 bytes in 323 allocations
296 //int newStart = gNew;
297 XMLDocument doc;
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300298 doc.LoadFile( "resources/dream.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800299
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300300 doc.SaveFile( "resources/dreamout.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800301 doc.PrintError();
302
303 XMLTest( "Dream", "xml version=\"1.0\"",
304 doc.FirstChild()->ToDeclaration()->Value() );
305 XMLTest( "Dream", true, doc.FirstChild()->NextSibling()->ToUnknown() ? true : false );
306 XMLTest( "Dream", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
307 doc.FirstChild()->NextSibling()->ToUnknown()->Value() );
308 XMLTest( "Dream", "And Robin shall restore amends.",
309 doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
310 XMLTest( "Dream", "And Robin shall restore amends.",
311 doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
312
313 XMLDocument doc2;
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300314 doc2.LoadFile( "resources/dreamout.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800315 XMLTest( "Dream-out", "xml version=\"1.0\"",
316 doc2.FirstChild()->ToDeclaration()->Value() );
317 XMLTest( "Dream-out", true, doc2.FirstChild()->NextSibling()->ToUnknown() ? true : false );
318 XMLTest( "Dream-out", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
319 doc2.FirstChild()->NextSibling()->ToUnknown()->Value() );
320 XMLTest( "Dream-out", "And Robin shall restore amends.",
321 doc2.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
322
323 //gNewTotal = gNew - newStart;
324 }
Lee Thomason6f381b72012-03-02 12:59:39 -0800325
326
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800327 {
328 const char* error = "<?xml version=\"1.0\" standalone=\"no\" ?>\n"
329 "<passages count=\"006\" formatversion=\"20020620\">\n"
330 " <wrong error>\n"
331 "</passages>";
332
333 XMLDocument doc;
334 doc.Parse( error );
Lee Thomason21be8822012-07-15 17:27:22 -0700335 XMLTest( "Bad XML", doc.ErrorID(), (int)XML_ERROR_PARSING_ATTRIBUTE );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800336 }
337
338 {
339 const char* str = "<doc attr0='1' attr1='2.0' attr2='foo' />";
340
341 XMLDocument doc;
342 doc.Parse( str );
343
344 XMLElement* ele = doc.FirstChildElement();
345
346 int iVal, result;
347 double dVal;
348
349 result = ele->QueryDoubleAttribute( "attr0", &dVal );
Lee Thomason21be8822012-07-15 17:27:22 -0700350 XMLTest( "Query attribute: int as double", result, (int)XML_NO_ERROR );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800351 XMLTest( "Query attribute: int as double", (int)dVal, 1 );
352 result = ele->QueryDoubleAttribute( "attr1", &dVal );
353 XMLTest( "Query attribute: double as double", (int)dVal, 2 );
354 result = ele->QueryIntAttribute( "attr1", &iVal );
Lee Thomason21be8822012-07-15 17:27:22 -0700355 XMLTest( "Query attribute: double as int", result, (int)XML_NO_ERROR );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800356 XMLTest( "Query attribute: double as int", iVal, 2 );
357 result = ele->QueryIntAttribute( "attr2", &iVal );
Lee Thomason21be8822012-07-15 17:27:22 -0700358 XMLTest( "Query attribute: not a number", result, (int)XML_WRONG_ATTRIBUTE_TYPE );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800359 result = ele->QueryIntAttribute( "bar", &iVal );
Lee Thomason21be8822012-07-15 17:27:22 -0700360 XMLTest( "Query attribute: does not exist", result, (int)XML_NO_ATTRIBUTE );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800361 }
362
363 {
364 const char* str = "<doc/>";
365
366 XMLDocument doc;
367 doc.Parse( str );
368
369 XMLElement* ele = doc.FirstChildElement();
370
371 int iVal;
372 double dVal;
373
374 ele->SetAttribute( "str", "strValue" );
375 ele->SetAttribute( "int", 1 );
376 ele->SetAttribute( "double", -1.0 );
377
378 const char* cStr = ele->Attribute( "str" );
379 ele->QueryIntAttribute( "int", &iVal );
380 ele->QueryDoubleAttribute( "double", &dVal );
381
Lee Thomason8ba7f7d2012-03-24 13:04:04 -0700382 XMLTest( "Attribute match test", ele->Attribute( "str", "strValue" ), "strValue" );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800383 XMLTest( "Attribute round trip. c-string.", "strValue", cStr );
384 XMLTest( "Attribute round trip. int.", 1, iVal );
385 XMLTest( "Attribute round trip. double.", -1, (int)dVal );
386 }
387
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800388 {
389 XMLDocument doc;
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300390 doc.LoadFile( "resources/utf8test.xml" );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800391
392 // Get the attribute "value" from the "Russian" element and check it.
393 XMLElement* element = doc.FirstChildElement( "document" )->FirstChildElement( "Russian" );
394 const unsigned char correctValue[] = { 0xd1U, 0x86U, 0xd0U, 0xb5U, 0xd0U, 0xbdU, 0xd0U, 0xbdU,
395 0xd0U, 0xbeU, 0xd1U, 0x81U, 0xd1U, 0x82U, 0xd1U, 0x8cU, 0 };
396
397 XMLTest( "UTF-8: Russian value.", (const char*)correctValue, element->Attribute( "value" ) );
398
399 const unsigned char russianElementName[] = { 0xd0U, 0xa0U, 0xd1U, 0x83U,
400 0xd1U, 0x81U, 0xd1U, 0x81U,
401 0xd0U, 0xbaU, 0xd0U, 0xb8U,
402 0xd0U, 0xb9U, 0 };
403 const char russianText[] = "<\xD0\xB8\xD0\xBC\xD0\xB5\xD0\xB5\xD1\x82>";
404
405 XMLText* text = doc.FirstChildElement( "document" )->FirstChildElement( (const char*) russianElementName )->FirstChild()->ToText();
406 XMLTest( "UTF-8: Browsing russian element name.",
407 russianText,
408 text->Value() );
409
410 // Now try for a round trip.
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300411 doc.SaveFile( "resources/utf8testout.xml" );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800412
413 // Check the round trip.
414 char savedBuf[256];
415 char verifyBuf[256];
416 int okay = 0;
417
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800418
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800419#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800420#pragma warning ( push )
421#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800422#endif
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300423 FILE* saved = fopen( "resources/utf8testout.xml", "r" );
424 FILE* verify = fopen( "resources/utf8testverify.xml", "r" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800425#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800426#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800427#endif
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800428
429 if ( saved && verify )
430 {
431 okay = 1;
432 while ( fgets( verifyBuf, 256, verify ) )
433 {
434 fgets( savedBuf, 256, saved );
435 NullLineEndings( verifyBuf );
436 NullLineEndings( savedBuf );
437
438 if ( strcmp( verifyBuf, savedBuf ) )
439 {
440 printf( "verify:%s<\n", verifyBuf );
441 printf( "saved :%s<\n", savedBuf );
442 okay = 0;
443 break;
444 }
445 }
446 }
447 if ( saved )
448 fclose( saved );
449 if ( verify )
450 fclose( verify );
451 XMLTest( "UTF-8: Verified multi-language round trip.", 1, okay );
452 }
453
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800454 // --------GetText()-----------
455 {
456 const char* str = "<foo>This is text</foo>";
457 XMLDocument doc;
458 doc.Parse( str );
459 const XMLElement* element = doc.RootElement();
460
461 XMLTest( "GetText() normal use.", "This is text", element->GetText() );
462
463 str = "<foo><b>This is text</b></foo>";
464 doc.Parse( str );
465 element = doc.RootElement();
466
467 XMLTest( "GetText() contained element.", element->GetText() == 0, true );
468 }
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800469
Lee Thomasond6277762012-02-22 16:00:12 -0800470
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800471 // ---------- CDATA ---------------
472 {
473 const char* str = "<xmlElement>"
474 "<![CDATA["
475 "I am > the rules!\n"
476 "...since I make symbolic puns"
477 "]]>"
478 "</xmlElement>";
479 XMLDocument doc;
480 doc.Parse( str );
481 doc.Print();
Lee Thomasond6277762012-02-22 16:00:12 -0800482
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800483 XMLTest( "CDATA parse.", doc.FirstChildElement()->FirstChild()->Value(),
484 "I am > the rules!\n...since I make symbolic puns",
Lee Thomasond6277762012-02-22 16:00:12 -0800485 false );
486 }
487
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800488 // ----------- CDATA -------------
489 {
490 const char* str = "<xmlElement>"
491 "<![CDATA["
492 "<b>I am > the rules!</b>\n"
493 "...since I make symbolic puns"
494 "]]>"
495 "</xmlElement>";
496 XMLDocument doc;
497 doc.Parse( str );
498 doc.Print();
499
500 XMLTest( "CDATA parse. [ tixml1:1480107 ]", doc.FirstChildElement()->FirstChild()->Value(),
501 "<b>I am > the rules!</b>\n...since I make symbolic puns",
502 false );
503 }
504
505 // InsertAfterChild causes crash.
506 {
507 // InsertBeforeChild and InsertAfterChild causes crash.
508 XMLDocument doc;
509 XMLElement* parent = doc.NewElement( "Parent" );
510 doc.InsertFirstChild( parent );
511
512 XMLElement* childText0 = doc.NewElement( "childText0" );
513 XMLElement* childText1 = doc.NewElement( "childText1" );
514
515 XMLNode* childNode0 = parent->InsertEndChild( childText0 );
516 XMLNode* childNode1 = parent->InsertAfterChild( childNode0, childText1 );
517
518 XMLTest( "Test InsertAfterChild on empty node. ", ( childNode1 == parent->LastChild() ), true );
519 }
Lee Thomasond6277762012-02-22 16:00:12 -0800520
521 {
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800522 // Entities not being written correctly.
523 // From Lynn Allen
Lee Thomasond6277762012-02-22 16:00:12 -0800524
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800525 const char* passages =
526 "<?xml version=\"1.0\" standalone=\"no\" ?>"
527 "<passages count=\"006\" formatversion=\"20020620\">"
528 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
529 " It also has &lt;, &gt;, and &amp;, as well as a fake copyright &#xA9;.\"> </psg>"
530 "</passages>";
Lee Thomasond6277762012-02-22 16:00:12 -0800531
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800532 XMLDocument doc;
533 doc.Parse( passages );
534 XMLElement* psg = doc.RootElement()->FirstChildElement();
535 const char* context = psg->Attribute( "context" );
536 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 -0800537
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800538 XMLTest( "Entity transformation: read. ", expected, context, true );
Lee Thomasond6277762012-02-22 16:00:12 -0800539
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800540#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800541#pragma warning ( push )
542#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800543#endif
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300544 FILE* textfile = fopen( "resources/textfile.txt", "w" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800545#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800546#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800547#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800548 if ( textfile )
549 {
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800550 XMLPrinter streamer( textfile );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800551 psg->Accept( &streamer );
552 fclose( textfile );
553 }
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800554#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800555#pragma warning ( push )
556#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800557#endif
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300558 textfile = fopen( "resources/textfile.txt", "r" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800559#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800560#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800561#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800562 TIXMLASSERT( textfile );
563 if ( textfile )
564 {
565 char buf[ 1024 ];
566 fgets( buf, 1024, textfile );
567 XMLTest( "Entity transformation: write. ",
568 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
569 " It also has &lt;, &gt;, and &amp;, as well as a fake copyright \xC2\xA9.\"/>\n",
570 buf, false );
571 }
572 fclose( textfile );
573 }
574
575 {
Lee Thomason6f381b72012-03-02 12:59:39 -0800576 // Suppress entities.
577 const char* passages =
578 "<?xml version=\"1.0\" standalone=\"no\" ?>"
579 "<passages count=\"006\" formatversion=\"20020620\">"
580 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;.\">Crazy &ttk;</psg>"
581 "</passages>";
582
583 XMLDocument doc( false );
584 doc.Parse( passages );
585
586 XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->Attribute( "context" ),
587 "Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;." );
588 XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->FirstChild()->Value(),
589 "Crazy &ttk;" );
590 doc.Print();
591 }
592
593 {
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800594 const char* test = "<?xml version='1.0'?><a.elem xmi.version='2.0'/>";
595
596 XMLDocument doc;
597 doc.Parse( test );
Lee Thomason21be8822012-07-15 17:27:22 -0700598 XMLTest( "dot in names", doc.Error(), false );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800599 XMLTest( "dot in names", doc.FirstChildElement()->Name(), "a.elem" );
600 XMLTest( "dot in names", doc.FirstChildElement()->Attribute( "xmi.version" ), "2.0" );
601 }
602
603 {
604 const char* test = "<element><Name>1.1 Start easy ignore fin thickness&#xA;</Name></element>";
605
606 XMLDocument doc;
607 doc.Parse( test );
608
609 XMLText* text = doc.FirstChildElement()->FirstChildElement()->FirstChild()->ToText();
610 XMLTest( "Entity with one digit.",
611 text->Value(), "1.1 Start easy ignore fin thickness\n",
612 false );
613 }
614
615 {
616 // DOCTYPE not preserved (950171)
617 //
618 const char* doctype =
619 "<?xml version=\"1.0\" ?>"
620 "<!DOCTYPE PLAY SYSTEM 'play.dtd'>"
621 "<!ELEMENT title (#PCDATA)>"
622 "<!ELEMENT books (title,authors)>"
623 "<element />";
624
625 XMLDocument doc;
626 doc.Parse( doctype );
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300627 doc.SaveFile( "resources/test7.xml" );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800628 doc.DeleteChild( doc.RootElement() );
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300629 doc.LoadFile( "resources/test7.xml" );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800630 doc.Print();
631
632 const XMLUnknown* decl = doc.FirstChild()->NextSibling()->ToUnknown();
633 XMLTest( "Correct value of unknown.", "DOCTYPE PLAY SYSTEM 'play.dtd'", decl->Value() );
634
635 }
636
637 {
638 // Comments do not stream out correctly.
639 const char* doctype =
640 "<!-- Somewhat<evil> -->";
641 XMLDocument doc;
642 doc.Parse( doctype );
643
644 XMLComment* comment = doc.FirstChild()->ToComment();
645
646 XMLTest( "Comment formatting.", " Somewhat<evil> ", comment->Value() );
647 }
648 {
649 // Double attributes
650 const char* doctype = "<element attr='red' attr='blue' />";
651
652 XMLDocument doc;
653 doc.Parse( doctype );
654
Lee Thomason21be8822012-07-15 17:27:22 -0700655 XMLTest( "Parsing repeated attributes.", (int)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 -0800656 doc.PrintError();
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800657 }
658
659 {
660 // Embedded null in stream.
661 const char* doctype = "<element att\0r='red' attr='blue' />";
662
663 XMLDocument doc;
664 doc.Parse( doctype );
665 XMLTest( "Embedded null throws error.", true, doc.Error() );
666 }
667
668 {
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700669 // Empty documents should return TIXML_XML_ERROR_PARSING_EMPTY, bug 1070717
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800670 const char* str = " ";
671 XMLDocument doc;
672 doc.Parse( str );
Lee Thomason21be8822012-07-15 17:27:22 -0700673 XMLTest( "Empty document error", (int)XML_ERROR_EMPTY_DOCUMENT, doc.ErrorID() );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800674 }
675
676 {
677 // Low entities
678 XMLDocument doc;
679 doc.Parse( "<test>&#x0e;</test>" );
680 const char result[] = { 0x0e, 0 };
681 XMLTest( "Low entities.", doc.FirstChildElement()->GetText(), result );
682 doc.Print();
683 }
684
685 {
686 // Attribute values with trailing quotes not handled correctly
687 XMLDocument doc;
688 doc.Parse( "<foo attribute=bar\" />" );
689 XMLTest( "Throw error with bad end quotes.", doc.Error(), true );
690 }
691
692 {
693 // [ 1663758 ] Failure to report error on bad XML
694 XMLDocument xml;
695 xml.Parse("<x>");
696 XMLTest("Missing end tag at end of input", xml.Error(), true);
697 xml.Parse("<x> ");
698 XMLTest("Missing end tag with trailing whitespace", xml.Error(), true);
699 xml.Parse("<x></y>");
Lee Thomason21be8822012-07-15 17:27:22 -0700700 XMLTest("Mismatched tags", xml.ErrorID(), (int)XML_ERROR_MISMATCHED_ELEMENT);
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800701 }
702
703
704 {
705 // [ 1475201 ] TinyXML parses entities in comments
706 XMLDocument xml;
707 xml.Parse("<!-- declarations for <head> & <body> -->"
708 "<!-- far &amp; away -->" );
709
710 XMLNode* e0 = xml.FirstChild();
711 XMLNode* e1 = e0->NextSibling();
712 XMLComment* c0 = e0->ToComment();
713 XMLComment* c1 = e1->ToComment();
714
715 XMLTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
716 XMLTest( "Comments ignore entities.", " far &amp; away ", c1->Value(), true );
717 }
718
719 {
720 XMLDocument xml;
721 xml.Parse( "<Parent>"
722 "<child1 att=''/>"
723 "<!-- With this comment, child2 will not be parsed! -->"
724 "<child2 att=''/>"
725 "</Parent>" );
726 xml.Print();
727
728 int count = 0;
729
730 for( XMLNode* ele = xml.FirstChildElement( "Parent" )->FirstChild();
731 ele;
732 ele = ele->NextSibling() )
733 {
734 ++count;
735 }
736
737 XMLTest( "Comments iterate correctly.", 3, count );
738 }
739
740 {
741 // trying to repro ]1874301]. If it doesn't go into an infinite loop, all is well.
742 unsigned char buf[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?><feed><![CDATA[Test XMLblablablalblbl";
743 buf[60] = 239;
744 buf[61] = 0;
745
746 XMLDocument doc;
747 doc.Parse( (const char*)buf);
748 }
749
750
751 {
752 // bug 1827248 Error while parsing a little bit malformed file
753 // Actually not malformed - should work.
754 XMLDocument xml;
755 xml.Parse( "<attributelist> </attributelist >" );
756 XMLTest( "Handle end tag whitespace", false, xml.Error() );
757 }
758
759 {
760 // This one must not result in an infinite loop
761 XMLDocument xml;
762 xml.Parse( "<infinite>loop" );
763 XMLTest( "Infinite loop test.", true, true );
764 }
765#endif
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800766 {
767 const char* pub = "<?xml version='1.0'?> <element><sub/></element> <!--comment--> <!DOCTYPE>";
768 XMLDocument doc;
769 doc.Parse( pub );
770
771 XMLDocument clone;
772 for( const XMLNode* node=doc.FirstChild(); node; node=node->NextSibling() ) {
773 XMLNode* copy = node->ShallowClone( &clone );
774 clone.InsertEndChild( copy );
775 }
776
777 clone.Print();
778
779 int count=0;
780 const XMLNode* a=clone.FirstChild();
781 const XMLNode* b=doc.FirstChild();
782 for( ; a && b; a=a->NextSibling(), b=b->NextSibling() ) {
783 ++count;
784 XMLTest( "Clone and Equal", true, a->ShallowEqual( b ));
785 }
786 XMLTest( "Clone and Equal", 4, count );
787 }
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800788
Lee Thomason (grinliz)a4a36ba2012-04-06 21:24:29 -0700789 {
790 // This shouldn't crash.
791 XMLDocument doc;
792 if(XML_NO_ERROR != doc.LoadFile( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ))
793 {
794 doc.PrintError();
795 }
796 XMLTest( "Error in snprinf handling.", true, doc.Error() );
797 }
Lee Thomason5e3803c2012-04-16 08:57:05 -0700798
799 {
800 // Attribute ordering.
801 static const char* xml = "<element attrib1=\"1\" attrib2=\"2\" attrib3=\"3\" />";
802 XMLDocument doc;
803 doc.Parse( xml );
804 XMLElement* ele = doc.FirstChildElement();
805
806 const XMLAttribute* a = ele->FirstAttribute();
807 XMLTest( "Attribute order", "1", a->Value() );
808 a = a->Next();
809 XMLTest( "Attribute order", "2", a->Value() );
810 a = a->Next();
811 XMLTest( "Attribute order", "3", a->Value() );
812 XMLTest( "Attribute order", "attrib3", a->Name() );
813
814 ele->DeleteAttribute( "attrib2" );
815 a = ele->FirstAttribute();
816 XMLTest( "Attribute order", "1", a->Value() );
817 a = a->Next();
818 XMLTest( "Attribute order", "3", a->Value() );
819
820 ele->DeleteAttribute( "attrib1" );
821 ele->DeleteAttribute( "attrib3" );
822 XMLTest( "Attribute order (empty)", false, ele->FirstAttribute() ? true : false );
823 }
Lee Thomason (grinliz)a4a36ba2012-04-06 21:24:29 -0700824
Lee Thomason (grinliz)390e9782012-07-01 21:22:53 -0700825 {
826 // Make sure an attribute with a space in it succeeds.
Lee Thomason78a773d2012-07-02 10:10:19 -0700827 static const char* xml0 = "<element attribute1= \"Test Attribute\"/>";
828 static const char* xml1 = "<element attribute1 =\"Test Attribute\"/>";
829 static const char* xml2 = "<element attribute1 = \"Test Attribute\"/>";
830 XMLDocument doc0;
831 doc0.Parse( xml0 );
832 XMLDocument doc1;
833 doc1.Parse( xml1 );
834 XMLDocument doc2;
835 doc2.Parse( xml2 );
Lee Thomason (grinliz)390e9782012-07-01 21:22:53 -0700836
Lee Thomason78a773d2012-07-02 10:10:19 -0700837 XMLElement* ele = 0;
838 ele = doc0.FirstChildElement();
839 XMLTest( "Attribute with space #1", "Test Attribute", ele->Attribute( "attribute1" ) );
840 ele = doc1.FirstChildElement();
841 XMLTest( "Attribute with space #2", "Test Attribute", ele->Attribute( "attribute1" ) );
842 ele = doc2.FirstChildElement();
843 XMLTest( "Attribute with space #3", "Test Attribute", ele->Attribute( "attribute1" ) );
Lee Thomason (grinliz)390e9782012-07-01 21:22:53 -0700844 }
845
846 {
847 // Make sure we don't go into an infinite loop.
848 static const char* xml = "<doc><element attribute='attribute'/><element attribute='attribute'/></doc>";
849 XMLDocument doc;
850 doc.Parse( xml );
851 XMLElement* ele0 = doc.FirstChildElement()->FirstChildElement();
852 XMLElement* ele1 = ele0->NextSiblingElement();
853 bool equal = ele0->ShallowEqual( ele1 );
854
855 XMLTest( "Infinite loop in shallow equal.", true, equal );
856 }
857
Lee Thomason5708f812012-03-28 17:46:41 -0700858 // -------- Handles ------------
859 {
860 static const char* xml = "<element attrib='bar'><sub>Text</sub></element>";
861 XMLDocument doc;
862 doc.Parse( xml );
Lee Thomason5708f812012-03-28 17:46:41 -0700863
864 XMLElement* ele = XMLHandle( doc ).FirstChildElement( "element" ).FirstChild().ToElement();
865 XMLTest( "Handle, success, mutable", ele->Value(), "sub" );
866
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -0700867 XMLHandle docH( doc );
868 ele = docH.FirstChildElement( "none" ).FirstChildElement( "element" ).ToElement();
Lee Thomasond0b19df2012-04-12 08:41:37 -0700869 XMLTest( "Handle, dne, mutable", false, ele != 0 );
Lee Thomason5708f812012-03-28 17:46:41 -0700870 }
871
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -0700872 {
873 static const char* xml = "<element attrib='bar'><sub>Text</sub></element>";
874 XMLDocument doc;
875 doc.Parse( xml );
876 XMLConstHandle docH( doc );
877
878 const XMLElement* ele = docH.FirstChildElement( "element" ).FirstChild().ToElement();
879 XMLTest( "Handle, success, const", ele->Value(), "sub" );
880
881 ele = docH.FirstChildElement( "none" ).FirstChildElement( "element" ).ToElement();
Lee Thomasond0b19df2012-04-12 08:41:37 -0700882 XMLTest( "Handle, dne, const", false, ele != 0 );
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -0700883 }
Lee Thomasonf68c4382012-04-28 14:37:11 -0700884 {
885 // Default Declaration & BOM
886 XMLDocument doc;
887 doc.InsertEndChild( doc.NewDeclaration() );
888 doc.SetBOM( true );
889
890 XMLPrinter printer;
891 doc.Print( &printer );
892
893 static const char* result = "\xef\xbb\xbf<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
894 XMLTest( "BOM and default declaration", printer.CStr(), result, false );
Lee Thomason (grinliz)48ea0bc2012-05-26 14:41:14 -0700895 XMLTest( "CStrSize", printer.CStrSize(), 42, false );
Lee Thomasonf68c4382012-04-28 14:37:11 -0700896 }
Lee Thomason21be8822012-07-15 17:27:22 -0700897 {
898 const char* xml = "<ipxml ws='1'><info bla=' /></ipxml>";
899 XMLDocument doc;
900 doc.Parse( xml );
901 XMLTest( "Ill formed XML", true, doc.Error() );
902 }
903
904 // QueryXYZText
905 {
906 const char* xml = "<point> <x>1.2</x> <y>1</y> <z>38</z> <valid>true</valid> </point>";
907 XMLDocument doc;
908 doc.Parse( xml );
909
910 const XMLElement* pointElement = doc.RootElement();
911
912 int intValue = 0;
913 unsigned unsignedValue = 0;
914 float floatValue = 0;
915 double doubleValue = 0;
916 bool boolValue = false;
917
918 pointElement->FirstChildElement( "y" )->QueryIntText( &intValue );
919 pointElement->FirstChildElement( "y" )->QueryUnsignedText( &unsignedValue );
920 pointElement->FirstChildElement( "x" )->QueryFloatText( &floatValue );
921 pointElement->FirstChildElement( "x" )->QueryDoubleText( &doubleValue );
922 pointElement->FirstChildElement( "valid" )->QueryBoolText( &boolValue );
923
924
925 XMLTest( "QueryIntText", intValue, 1, false );
926 XMLTest( "QueryUnsignedText", unsignedValue, (unsigned)1, false );
927 XMLTest( "QueryFloatText", floatValue, 1.2f, false );
928 XMLTest( "QueryDoubleText", doubleValue, 1.2, false );
929 XMLTest( "QueryBoolText", boolValue, true, false );
930 }
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -0700931
932
Lee Thomason6f381b72012-03-02 12:59:39 -0800933 // ----------- Performance tracking --------------
934 {
935#if defined( _MSC_VER )
936 __int64 start, end, freq;
937 QueryPerformanceFrequency( (LARGE_INTEGER*) &freq );
938#endif
939
940#if defined(_MSC_VER)
941#pragma warning ( push )
942#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
943#endif
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300944 FILE* fp = fopen( "resources/dream.xml", "r" );
Lee Thomason6f381b72012-03-02 12:59:39 -0800945#if defined(_MSC_VER)
946#pragma warning ( pop )
947#endif
948 fseek( fp, 0, SEEK_END );
949 long size = ftell( fp );
950 fseek( fp, 0, SEEK_SET );
951
952 char* mem = new char[size+1];
953 fread( mem, size, 1, fp );
954 fclose( fp );
955 mem[size] = 0;
956
957#if defined( _MSC_VER )
958 QueryPerformanceCounter( (LARGE_INTEGER*) &start );
959#else
960 clock_t cstart = clock();
961#endif
962 static const int COUNT = 10;
963 for( int i=0; i<COUNT; ++i ) {
964 XMLDocument doc;
965 doc.Parse( mem );
966 }
967#if defined( _MSC_VER )
968 QueryPerformanceCounter( (LARGE_INTEGER*) &end );
969#else
970 clock_t cend = clock();
971#endif
972
973 delete [] mem;
974
975 static const char* note =
976#ifdef DEBUG
977 "DEBUG";
978#else
979 "Release";
980#endif
981
982#if defined( _MSC_VER )
983 printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, 1000.0 * (double)(end-start) / ( (double)freq * (double)COUNT) );
984#else
985 printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, (double)(cend - cstart)/(double)COUNT );
986#endif
987 }
988
Lee Thomason (grinliz)7ca55582012-03-07 21:54:57 -0800989 #if defined( _MSC_VER ) && defined( DEBUG )
Lee Thomason1ff38e02012-02-14 18:18:16 -0800990 _CrtMemCheckpoint( &endMemState );
991 //_CrtMemDumpStatistics( &endMemState );
992
993 _CrtMemState diffMemState;
994 _CrtMemDifference( &diffMemState, &startMemState, &endMemState );
995 _CrtMemDumpStatistics( &diffMemState );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800996 //printf( "new total=%d\n", gNewTotal );
Lee Thomason1ff38e02012-02-14 18:18:16 -0800997 #endif
998
999 printf ("\nPass %d, Fail %d\n", gPass, gFail);
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001000 return 0;
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -08001001}