blob: 969aa9e3ad3d4a0fe8d44d854dea0ead5f34e4bd [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
Lee Thomasonc8312792012-07-16 12:44:41 -070052 printf (" %s [%d][%d]\n", testString, static_cast<int>(expected), static_cast<int>(found) );
Lee Thomason1ff38e02012-02-14 18:18:16 -080053
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
Arkadiy Shapkinff72d1f2012-07-24 00:24:07 +0400158 CreateDirectory( L"resources/out/", NULL );
159
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300160 FILE* fp = fopen( "resources/dream.xml", "r" );
Lee Thomason7f7b1622012-03-24 12:49:03 -0700161 if ( !fp ) {
162 printf( "Error opening test file 'dream.xml'.\n"
163 "Is your working directory the same as where \n"
164 "the xmltest.cpp and dream.xml file are?\n\n"
165 #if defined( _MSC_VER )
166 "In windows Visual Studio you may need to set\n"
167 "Properties->Debugging->Working Directory to '..'\n"
168 #endif
169 );
170 exit( 1 );
171 }
172 fclose( fp );
173
174 #if defined(_MSC_VER)
175 #pragma warning ( pop )
176 #endif
177
Lee Thomason (grinliz)6a22be22012-04-04 12:39:05 -0700178 XMLTest( "Example-1", 0, example_1() );
179 XMLTest( "Example-2", 0, example_2() );
180 XMLTest( "Example-3", 0, example_3() );
Lee Thomason21be8822012-07-15 17:27:22 -0700181 XMLTest( "Example-4", true, example_4() );
Lee Thomason87e475a2012-03-20 11:55:29 -0700182
Lee Thomason (grinliz)6a22be22012-04-04 12:39:05 -0700183 /* ------ Example 2: Lookup information. ---- */
Lee Thomason87e475a2012-03-20 11:55:29 -0700184
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800185 {
Lee Thomason43f59302012-02-06 18:18:11 -0800186 static const char* test[] = { "<element />",
187 "<element></element>",
188 "<element><subelement/></element>",
189 "<element><subelement></subelement></element>",
190 "<element><subelement><subsub/></subelement></element>",
191 "<!--comment beside elements--><element><subelement></subelement></element>",
192 "<!--comment beside elements, this time with spaces--> \n <element> <subelement> \n </subelement> </element>",
193 "<element attrib1='foo' attrib2=\"bar\" ></element>",
194 "<element attrib1='foo' attrib2=\"bar\" ><subelement attrib3='yeehaa' /></element>",
195 "<element>Text inside element.</element>",
196 "<element><b></b></element>",
197 "<element>Text inside and <b>bolded</b> in the element.</element>",
198 "<outer><element>Text inside and <b>bolded</b> in the element.</element></outer>",
Lee Thomason8ee79892012-01-25 17:44:30 -0800199 "<element>This &amp; That.</element>",
Lee Thomason18d68bd2012-01-26 18:17:26 -0800200 "<element attrib='This&lt;That' />",
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800201 0
202 };
Lee Thomason6ee99fc2012-01-21 18:45:16 -0800203 for( int i=0; test[i]; ++i ) {
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800204 XMLDocument doc;
Lee Thomason6ee99fc2012-01-21 18:45:16 -0800205 doc.Parse( test[i] );
Lee Thomason5cae8972012-01-24 18:03:07 -0800206 doc.Print();
Lee Thomasonec975ce2012-01-23 11:42:06 -0800207 printf( "----------------------------------------------\n" );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800208 }
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800209 }
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800210#if 1
Lee Thomasond6277762012-02-22 16:00:12 -0800211 {
212 static const char* test = "<!--hello world\n"
213 " line 2\r"
214 " line 3\r\n"
215 " line 4\n\r"
216 " line 5\r-->";
217
218 XMLDocument doc;
219 doc.Parse( test );
220 doc.Print();
221 }
222
Lee Thomason2c85a712012-01-31 08:24:24 -0800223 {
224 static const char* test = "<element>Text before.</element>";
225 XMLDocument doc;
226 doc.Parse( test );
227 XMLElement* root = doc.FirstChildElement();
228 XMLElement* newElement = doc.NewElement( "Subelement" );
229 root->InsertEndChild( newElement );
230 doc.Print();
231 }
Lee Thomasond1983222012-02-06 08:41:24 -0800232 {
233 XMLDocument* doc = new XMLDocument();
234 static const char* test = "<element><sub/></element>";
235 doc->Parse( test );
236 delete doc;
237 }
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800238 {
Lee Thomason1ff38e02012-02-14 18:18:16 -0800239 // Test: Programmatic DOM
Lee Thomasonec5a7b42012-02-13 18:16:52 -0800240 // Build:
241 // <element>
242 // <!--comment-->
243 // <sub attrib="1" />
244 // <sub attrib="2" />
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800245 // <sub attrib="3" >& Text!</sub>
Lee Thomasonec5a7b42012-02-13 18:16:52 -0800246 // <element>
247
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800248 XMLDocument* doc = new XMLDocument();
Lee Thomason1ff38e02012-02-14 18:18:16 -0800249 XMLNode* element = doc->InsertEndChild( doc->NewElement( "element" ) );
250
251 XMLElement* sub[3] = { doc->NewElement( "sub" ), doc->NewElement( "sub" ), doc->NewElement( "sub" ) };
252 for( int i=0; i<3; ++i ) {
253 sub[i]->SetAttribute( "attrib", i );
254 }
255 element->InsertEndChild( sub[2] );
256 XMLNode* comment = element->InsertFirstChild( doc->NewComment( "comment" ) );
257 element->InsertAfterChild( comment, sub[0] );
258 element->InsertAfterChild( sub[0], sub[1] );
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800259 sub[2]->InsertFirstChild( doc->NewText( "& Text!" ));
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800260 doc->Print();
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800261 XMLTest( "Programmatic DOM", "comment", doc->FirstChildElement( "element" )->FirstChild()->Value() );
262 XMLTest( "Programmatic DOM", "0", doc->FirstChildElement( "element" )->FirstChildElement()->Attribute( "attrib" ) );
263 XMLTest( "Programmatic DOM", 2, doc->FirstChildElement()->LastChildElement( "sub" )->IntAttribute( "attrib" ) );
264 XMLTest( "Programmatic DOM", "& Text!",
265 doc->FirstChildElement()->LastChildElement( "sub" )->FirstChild()->ToText()->Value() );
U-Stream\Leeae25a442012-02-17 17:48:16 -0800266
267 // And now deletion:
268 element->DeleteChild( sub[2] );
269 doc->DeleteNode( comment );
270
271 element->FirstChildElement()->SetAttribute( "attrib", true );
272 element->LastChildElement()->DeleteAttribute( "attrib" );
273
274 XMLTest( "Programmatic DOM", true, doc->FirstChildElement()->FirstChildElement()->BoolAttribute( "attrib" ) );
275 int value = 10;
276 int result = doc->FirstChildElement()->LastChildElement()->QueryIntAttribute( "attrib", &value );
Lee Thomason21be8822012-07-15 17:27:22 -0700277 XMLTest( "Programmatic DOM", result, (int)XML_NO_ATTRIBUTE );
U-Stream\Leeae25a442012-02-17 17:48:16 -0800278 XMLTest( "Programmatic DOM", value, 10 );
279
280 doc->Print();
281
Lee Thomason7b1b86a2012-06-04 17:01:38 -0700282 {
283 XMLPrinter streamer;
284 doc->Print( &streamer );
285 printf( "%s", streamer.CStr() );
286 }
287 {
288 XMLPrinter streamer( 0, true );
289 doc->Print( &streamer );
290 XMLTest( "Compact mode", "<element><sub attrib=\"1\"/><sub/></element>", streamer.CStr(), false );
291 }
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800292 delete doc;
293 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800294 {
295 // Test: Dream
296 // XML1 : 1,187,569 bytes in 31,209 allocations
297 // XML2 : 469,073 bytes in 323 allocations
298 //int newStart = gNew;
299 XMLDocument doc;
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300300 doc.LoadFile( "resources/dream.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800301
Arkadiy Shapkinff72d1f2012-07-24 00:24:07 +0400302 doc.SaveFile( "resources/out/dreamout.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800303 doc.PrintError();
304
305 XMLTest( "Dream", "xml version=\"1.0\"",
306 doc.FirstChild()->ToDeclaration()->Value() );
307 XMLTest( "Dream", true, doc.FirstChild()->NextSibling()->ToUnknown() ? true : false );
308 XMLTest( "Dream", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
309 doc.FirstChild()->NextSibling()->ToUnknown()->Value() );
310 XMLTest( "Dream", "And Robin shall restore amends.",
311 doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
312 XMLTest( "Dream", "And Robin shall restore amends.",
313 doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
314
315 XMLDocument doc2;
Arkadiy Shapkinff72d1f2012-07-24 00:24:07 +0400316 doc2.LoadFile( "resources/out/dreamout.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800317 XMLTest( "Dream-out", "xml version=\"1.0\"",
318 doc2.FirstChild()->ToDeclaration()->Value() );
319 XMLTest( "Dream-out", true, doc2.FirstChild()->NextSibling()->ToUnknown() ? true : false );
320 XMLTest( "Dream-out", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
321 doc2.FirstChild()->NextSibling()->ToUnknown()->Value() );
322 XMLTest( "Dream-out", "And Robin shall restore amends.",
323 doc2.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
324
325 //gNewTotal = gNew - newStart;
326 }
Lee Thomason6f381b72012-03-02 12:59:39 -0800327
328
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800329 {
330 const char* error = "<?xml version=\"1.0\" standalone=\"no\" ?>\n"
331 "<passages count=\"006\" formatversion=\"20020620\">\n"
332 " <wrong error>\n"
333 "</passages>";
334
335 XMLDocument doc;
336 doc.Parse( error );
Lee Thomason21be8822012-07-15 17:27:22 -0700337 XMLTest( "Bad XML", doc.ErrorID(), (int)XML_ERROR_PARSING_ATTRIBUTE );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800338 }
339
340 {
341 const char* str = "<doc attr0='1' attr1='2.0' attr2='foo' />";
342
343 XMLDocument doc;
344 doc.Parse( str );
345
346 XMLElement* ele = doc.FirstChildElement();
347
348 int iVal, result;
349 double dVal;
350
351 result = ele->QueryDoubleAttribute( "attr0", &dVal );
Lee Thomason21be8822012-07-15 17:27:22 -0700352 XMLTest( "Query attribute: int as double", result, (int)XML_NO_ERROR );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800353 XMLTest( "Query attribute: int as double", (int)dVal, 1 );
354 result = ele->QueryDoubleAttribute( "attr1", &dVal );
355 XMLTest( "Query attribute: double as double", (int)dVal, 2 );
356 result = ele->QueryIntAttribute( "attr1", &iVal );
Lee Thomason21be8822012-07-15 17:27:22 -0700357 XMLTest( "Query attribute: double as int", result, (int)XML_NO_ERROR );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800358 XMLTest( "Query attribute: double as int", iVal, 2 );
359 result = ele->QueryIntAttribute( "attr2", &iVal );
Lee Thomason21be8822012-07-15 17:27:22 -0700360 XMLTest( "Query attribute: not a number", result, (int)XML_WRONG_ATTRIBUTE_TYPE );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800361 result = ele->QueryIntAttribute( "bar", &iVal );
Lee Thomason21be8822012-07-15 17:27:22 -0700362 XMLTest( "Query attribute: does not exist", result, (int)XML_NO_ATTRIBUTE );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800363 }
364
365 {
366 const char* str = "<doc/>";
367
368 XMLDocument doc;
369 doc.Parse( str );
370
371 XMLElement* ele = doc.FirstChildElement();
372
373 int iVal;
374 double dVal;
375
376 ele->SetAttribute( "str", "strValue" );
377 ele->SetAttribute( "int", 1 );
378 ele->SetAttribute( "double", -1.0 );
379
380 const char* cStr = ele->Attribute( "str" );
381 ele->QueryIntAttribute( "int", &iVal );
382 ele->QueryDoubleAttribute( "double", &dVal );
383
Lee Thomason8ba7f7d2012-03-24 13:04:04 -0700384 XMLTest( "Attribute match test", ele->Attribute( "str", "strValue" ), "strValue" );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800385 XMLTest( "Attribute round trip. c-string.", "strValue", cStr );
386 XMLTest( "Attribute round trip. int.", 1, iVal );
387 XMLTest( "Attribute round trip. double.", -1, (int)dVal );
388 }
389
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800390 {
391 XMLDocument doc;
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300392 doc.LoadFile( "resources/utf8test.xml" );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800393
394 // Get the attribute "value" from the "Russian" element and check it.
395 XMLElement* element = doc.FirstChildElement( "document" )->FirstChildElement( "Russian" );
396 const unsigned char correctValue[] = { 0xd1U, 0x86U, 0xd0U, 0xb5U, 0xd0U, 0xbdU, 0xd0U, 0xbdU,
397 0xd0U, 0xbeU, 0xd1U, 0x81U, 0xd1U, 0x82U, 0xd1U, 0x8cU, 0 };
398
399 XMLTest( "UTF-8: Russian value.", (const char*)correctValue, element->Attribute( "value" ) );
400
401 const unsigned char russianElementName[] = { 0xd0U, 0xa0U, 0xd1U, 0x83U,
402 0xd1U, 0x81U, 0xd1U, 0x81U,
403 0xd0U, 0xbaU, 0xd0U, 0xb8U,
404 0xd0U, 0xb9U, 0 };
405 const char russianText[] = "<\xD0\xB8\xD0\xBC\xD0\xB5\xD0\xB5\xD1\x82>";
406
407 XMLText* text = doc.FirstChildElement( "document" )->FirstChildElement( (const char*) russianElementName )->FirstChild()->ToText();
408 XMLTest( "UTF-8: Browsing russian element name.",
409 russianText,
410 text->Value() );
411
412 // Now try for a round trip.
Arkadiy Shapkinff72d1f2012-07-24 00:24:07 +0400413 doc.SaveFile( "resources/out/utf8testout.xml" );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800414
415 // Check the round trip.
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800416 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;
PKEuSc28ba3a2012-07-16 03:08:47 -0700432 char verifyBuf[256];
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800433 while ( fgets( verifyBuf, 256, verify ) )
434 {
PKEuSc28ba3a2012-07-16 03:08:47 -0700435 char savedBuf[256];
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800436 fgets( savedBuf, 256, saved );
437 NullLineEndings( verifyBuf );
438 NullLineEndings( savedBuf );
439
440 if ( strcmp( verifyBuf, savedBuf ) )
441 {
442 printf( "verify:%s<\n", verifyBuf );
443 printf( "saved :%s<\n", savedBuf );
444 okay = 0;
445 break;
446 }
447 }
448 }
449 if ( saved )
450 fclose( saved );
451 if ( verify )
452 fclose( verify );
453 XMLTest( "UTF-8: Verified multi-language round trip.", 1, okay );
454 }
455
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800456 // --------GetText()-----------
457 {
458 const char* str = "<foo>This is text</foo>";
459 XMLDocument doc;
460 doc.Parse( str );
461 const XMLElement* element = doc.RootElement();
462
463 XMLTest( "GetText() normal use.", "This is text", element->GetText() );
464
465 str = "<foo><b>This is text</b></foo>";
466 doc.Parse( str );
467 element = doc.RootElement();
468
469 XMLTest( "GetText() contained element.", element->GetText() == 0, true );
470 }
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800471
Lee Thomasond6277762012-02-22 16:00:12 -0800472
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800473 // ---------- CDATA ---------------
474 {
475 const char* str = "<xmlElement>"
476 "<![CDATA["
477 "I am > the rules!\n"
478 "...since I make symbolic puns"
479 "]]>"
480 "</xmlElement>";
481 XMLDocument doc;
482 doc.Parse( str );
483 doc.Print();
Lee Thomasond6277762012-02-22 16:00:12 -0800484
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800485 XMLTest( "CDATA parse.", doc.FirstChildElement()->FirstChild()->Value(),
486 "I am > the rules!\n...since I make symbolic puns",
Lee Thomasond6277762012-02-22 16:00:12 -0800487 false );
488 }
489
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800490 // ----------- CDATA -------------
491 {
492 const char* str = "<xmlElement>"
493 "<![CDATA["
494 "<b>I am > the rules!</b>\n"
495 "...since I make symbolic puns"
496 "]]>"
497 "</xmlElement>";
498 XMLDocument doc;
499 doc.Parse( str );
500 doc.Print();
501
502 XMLTest( "CDATA parse. [ tixml1:1480107 ]", doc.FirstChildElement()->FirstChild()->Value(),
503 "<b>I am > the rules!</b>\n...since I make symbolic puns",
504 false );
505 }
506
507 // InsertAfterChild causes crash.
508 {
509 // InsertBeforeChild and InsertAfterChild causes crash.
510 XMLDocument doc;
511 XMLElement* parent = doc.NewElement( "Parent" );
512 doc.InsertFirstChild( parent );
513
514 XMLElement* childText0 = doc.NewElement( "childText0" );
515 XMLElement* childText1 = doc.NewElement( "childText1" );
516
517 XMLNode* childNode0 = parent->InsertEndChild( childText0 );
518 XMLNode* childNode1 = parent->InsertAfterChild( childNode0, childText1 );
519
520 XMLTest( "Test InsertAfterChild on empty node. ", ( childNode1 == parent->LastChild() ), true );
521 }
Lee Thomasond6277762012-02-22 16:00:12 -0800522
523 {
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800524 // Entities not being written correctly.
525 // From Lynn Allen
Lee Thomasond6277762012-02-22 16:00:12 -0800526
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800527 const char* passages =
528 "<?xml version=\"1.0\" standalone=\"no\" ?>"
529 "<passages count=\"006\" formatversion=\"20020620\">"
530 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
531 " It also has &lt;, &gt;, and &amp;, as well as a fake copyright &#xA9;.\"> </psg>"
532 "</passages>";
Lee Thomasond6277762012-02-22 16:00:12 -0800533
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800534 XMLDocument doc;
535 doc.Parse( passages );
536 XMLElement* psg = doc.RootElement()->FirstChildElement();
537 const char* context = psg->Attribute( "context" );
538 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 -0800539
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800540 XMLTest( "Entity transformation: read. ", expected, context, true );
Lee Thomasond6277762012-02-22 16:00:12 -0800541
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800542#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800543#pragma warning ( push )
544#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800545#endif
Arkadiy Shapkinff72d1f2012-07-24 00:24:07 +0400546 FILE* textfile = fopen( "resources/out/textfile.txt", "w" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800547#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800548#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800549#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800550 if ( textfile )
551 {
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800552 XMLPrinter streamer( textfile );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800553 psg->Accept( &streamer );
554 fclose( textfile );
555 }
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800556#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800557#pragma warning ( push )
558#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800559#endif
Arkadiy Shapkinff72d1f2012-07-24 00:24:07 +0400560 textfile = fopen( "resources/out/textfile.txt", "r" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800561#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800562#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800563#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800564 TIXMLASSERT( textfile );
565 if ( textfile )
566 {
567 char buf[ 1024 ];
568 fgets( buf, 1024, textfile );
569 XMLTest( "Entity transformation: write. ",
570 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
571 " It also has &lt;, &gt;, and &amp;, as well as a fake copyright \xC2\xA9.\"/>\n",
572 buf, false );
PKEuSc28ba3a2012-07-16 03:08:47 -0700573 fclose( textfile );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800574 }
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800575 }
576
577 {
Lee Thomason6f381b72012-03-02 12:59:39 -0800578 // Suppress entities.
579 const char* passages =
580 "<?xml version=\"1.0\" standalone=\"no\" ?>"
581 "<passages count=\"006\" formatversion=\"20020620\">"
582 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;.\">Crazy &ttk;</psg>"
583 "</passages>";
584
585 XMLDocument doc( false );
586 doc.Parse( passages );
587
588 XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->Attribute( "context" ),
589 "Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;." );
590 XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->FirstChild()->Value(),
591 "Crazy &ttk;" );
592 doc.Print();
593 }
594
595 {
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800596 const char* test = "<?xml version='1.0'?><a.elem xmi.version='2.0'/>";
597
598 XMLDocument doc;
599 doc.Parse( test );
Lee Thomason21be8822012-07-15 17:27:22 -0700600 XMLTest( "dot in names", doc.Error(), false );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800601 XMLTest( "dot in names", doc.FirstChildElement()->Name(), "a.elem" );
602 XMLTest( "dot in names", doc.FirstChildElement()->Attribute( "xmi.version" ), "2.0" );
603 }
604
605 {
606 const char* test = "<element><Name>1.1 Start easy ignore fin thickness&#xA;</Name></element>";
607
608 XMLDocument doc;
609 doc.Parse( test );
610
611 XMLText* text = doc.FirstChildElement()->FirstChildElement()->FirstChild()->ToText();
612 XMLTest( "Entity with one digit.",
613 text->Value(), "1.1 Start easy ignore fin thickness\n",
614 false );
615 }
616
617 {
618 // DOCTYPE not preserved (950171)
619 //
620 const char* doctype =
621 "<?xml version=\"1.0\" ?>"
622 "<!DOCTYPE PLAY SYSTEM 'play.dtd'>"
623 "<!ELEMENT title (#PCDATA)>"
624 "<!ELEMENT books (title,authors)>"
625 "<element />";
626
627 XMLDocument doc;
628 doc.Parse( doctype );
Arkadiy Shapkinff72d1f2012-07-24 00:24:07 +0400629 doc.SaveFile( "resources/out/test7.xml" );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800630 doc.DeleteChild( doc.RootElement() );
Arkadiy Shapkinff72d1f2012-07-24 00:24:07 +0400631 doc.LoadFile( "resources/out/test7.xml" );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800632 doc.Print();
633
634 const XMLUnknown* decl = doc.FirstChild()->NextSibling()->ToUnknown();
635 XMLTest( "Correct value of unknown.", "DOCTYPE PLAY SYSTEM 'play.dtd'", decl->Value() );
636
637 }
638
639 {
640 // Comments do not stream out correctly.
641 const char* doctype =
642 "<!-- Somewhat<evil> -->";
643 XMLDocument doc;
644 doc.Parse( doctype );
645
646 XMLComment* comment = doc.FirstChild()->ToComment();
647
648 XMLTest( "Comment formatting.", " Somewhat<evil> ", comment->Value() );
649 }
650 {
651 // Double attributes
652 const char* doctype = "<element attr='red' attr='blue' />";
653
654 XMLDocument doc;
655 doc.Parse( doctype );
656
Lee Thomason21be8822012-07-15 17:27:22 -0700657 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 -0800658 doc.PrintError();
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800659 }
660
661 {
662 // Embedded null in stream.
663 const char* doctype = "<element att\0r='red' attr='blue' />";
664
665 XMLDocument doc;
666 doc.Parse( doctype );
667 XMLTest( "Embedded null throws error.", true, doc.Error() );
668 }
669
670 {
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700671 // Empty documents should return TIXML_XML_ERROR_PARSING_EMPTY, bug 1070717
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800672 const char* str = " ";
673 XMLDocument doc;
674 doc.Parse( str );
Lee Thomason21be8822012-07-15 17:27:22 -0700675 XMLTest( "Empty document error", (int)XML_ERROR_EMPTY_DOCUMENT, doc.ErrorID() );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800676 }
677
678 {
679 // Low entities
680 XMLDocument doc;
681 doc.Parse( "<test>&#x0e;</test>" );
682 const char result[] = { 0x0e, 0 };
683 XMLTest( "Low entities.", doc.FirstChildElement()->GetText(), result );
684 doc.Print();
685 }
686
687 {
688 // Attribute values with trailing quotes not handled correctly
689 XMLDocument doc;
690 doc.Parse( "<foo attribute=bar\" />" );
691 XMLTest( "Throw error with bad end quotes.", doc.Error(), true );
692 }
693
694 {
695 // [ 1663758 ] Failure to report error on bad XML
696 XMLDocument xml;
697 xml.Parse("<x>");
698 XMLTest("Missing end tag at end of input", xml.Error(), true);
699 xml.Parse("<x> ");
700 XMLTest("Missing end tag with trailing whitespace", xml.Error(), true);
701 xml.Parse("<x></y>");
Lee Thomason21be8822012-07-15 17:27:22 -0700702 XMLTest("Mismatched tags", xml.ErrorID(), (int)XML_ERROR_MISMATCHED_ELEMENT);
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800703 }
704
705
706 {
707 // [ 1475201 ] TinyXML parses entities in comments
708 XMLDocument xml;
709 xml.Parse("<!-- declarations for <head> & <body> -->"
710 "<!-- far &amp; away -->" );
711
712 XMLNode* e0 = xml.FirstChild();
713 XMLNode* e1 = e0->NextSibling();
714 XMLComment* c0 = e0->ToComment();
715 XMLComment* c1 = e1->ToComment();
716
717 XMLTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
718 XMLTest( "Comments ignore entities.", " far &amp; away ", c1->Value(), true );
719 }
720
721 {
722 XMLDocument xml;
723 xml.Parse( "<Parent>"
724 "<child1 att=''/>"
725 "<!-- With this comment, child2 will not be parsed! -->"
726 "<child2 att=''/>"
727 "</Parent>" );
728 xml.Print();
729
730 int count = 0;
731
732 for( XMLNode* ele = xml.FirstChildElement( "Parent" )->FirstChild();
733 ele;
734 ele = ele->NextSibling() )
735 {
736 ++count;
737 }
738
739 XMLTest( "Comments iterate correctly.", 3, count );
740 }
741
742 {
743 // trying to repro ]1874301]. If it doesn't go into an infinite loop, all is well.
744 unsigned char buf[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?><feed><![CDATA[Test XMLblablablalblbl";
745 buf[60] = 239;
746 buf[61] = 0;
747
748 XMLDocument doc;
749 doc.Parse( (const char*)buf);
750 }
751
752
753 {
754 // bug 1827248 Error while parsing a little bit malformed file
755 // Actually not malformed - should work.
756 XMLDocument xml;
757 xml.Parse( "<attributelist> </attributelist >" );
758 XMLTest( "Handle end tag whitespace", false, xml.Error() );
759 }
760
761 {
762 // This one must not result in an infinite loop
763 XMLDocument xml;
764 xml.Parse( "<infinite>loop" );
765 XMLTest( "Infinite loop test.", true, true );
766 }
767#endif
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800768 {
769 const char* pub = "<?xml version='1.0'?> <element><sub/></element> <!--comment--> <!DOCTYPE>";
770 XMLDocument doc;
771 doc.Parse( pub );
772
773 XMLDocument clone;
774 for( const XMLNode* node=doc.FirstChild(); node; node=node->NextSibling() ) {
775 XMLNode* copy = node->ShallowClone( &clone );
776 clone.InsertEndChild( copy );
777 }
778
779 clone.Print();
780
781 int count=0;
782 const XMLNode* a=clone.FirstChild();
783 const XMLNode* b=doc.FirstChild();
784 for( ; a && b; a=a->NextSibling(), b=b->NextSibling() ) {
785 ++count;
786 XMLTest( "Clone and Equal", true, a->ShallowEqual( b ));
787 }
788 XMLTest( "Clone and Equal", 4, count );
789 }
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800790
Lee Thomason (grinliz)a4a36ba2012-04-06 21:24:29 -0700791 {
792 // This shouldn't crash.
793 XMLDocument doc;
794 if(XML_NO_ERROR != doc.LoadFile( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ))
795 {
796 doc.PrintError();
797 }
798 XMLTest( "Error in snprinf handling.", true, doc.Error() );
799 }
Lee Thomason5e3803c2012-04-16 08:57:05 -0700800
801 {
802 // Attribute ordering.
803 static const char* xml = "<element attrib1=\"1\" attrib2=\"2\" attrib3=\"3\" />";
804 XMLDocument doc;
805 doc.Parse( xml );
806 XMLElement* ele = doc.FirstChildElement();
807
808 const XMLAttribute* a = ele->FirstAttribute();
809 XMLTest( "Attribute order", "1", a->Value() );
810 a = a->Next();
811 XMLTest( "Attribute order", "2", a->Value() );
812 a = a->Next();
813 XMLTest( "Attribute order", "3", a->Value() );
814 XMLTest( "Attribute order", "attrib3", a->Name() );
815
816 ele->DeleteAttribute( "attrib2" );
817 a = ele->FirstAttribute();
818 XMLTest( "Attribute order", "1", a->Value() );
819 a = a->Next();
820 XMLTest( "Attribute order", "3", a->Value() );
821
822 ele->DeleteAttribute( "attrib1" );
823 ele->DeleteAttribute( "attrib3" );
824 XMLTest( "Attribute order (empty)", false, ele->FirstAttribute() ? true : false );
825 }
Lee Thomason (grinliz)a4a36ba2012-04-06 21:24:29 -0700826
Lee Thomason (grinliz)390e9782012-07-01 21:22:53 -0700827 {
828 // Make sure an attribute with a space in it succeeds.
Lee Thomason78a773d2012-07-02 10:10:19 -0700829 static const char* xml0 = "<element attribute1= \"Test Attribute\"/>";
830 static const char* xml1 = "<element attribute1 =\"Test Attribute\"/>";
831 static const char* xml2 = "<element attribute1 = \"Test Attribute\"/>";
832 XMLDocument doc0;
833 doc0.Parse( xml0 );
834 XMLDocument doc1;
835 doc1.Parse( xml1 );
836 XMLDocument doc2;
837 doc2.Parse( xml2 );
Lee Thomason (grinliz)390e9782012-07-01 21:22:53 -0700838
Lee Thomason78a773d2012-07-02 10:10:19 -0700839 XMLElement* ele = 0;
840 ele = doc0.FirstChildElement();
841 XMLTest( "Attribute with space #1", "Test Attribute", ele->Attribute( "attribute1" ) );
842 ele = doc1.FirstChildElement();
843 XMLTest( "Attribute with space #2", "Test Attribute", ele->Attribute( "attribute1" ) );
844 ele = doc2.FirstChildElement();
845 XMLTest( "Attribute with space #3", "Test Attribute", ele->Attribute( "attribute1" ) );
Lee Thomason (grinliz)390e9782012-07-01 21:22:53 -0700846 }
847
848 {
849 // Make sure we don't go into an infinite loop.
850 static const char* xml = "<doc><element attribute='attribute'/><element attribute='attribute'/></doc>";
851 XMLDocument doc;
852 doc.Parse( xml );
853 XMLElement* ele0 = doc.FirstChildElement()->FirstChildElement();
854 XMLElement* ele1 = ele0->NextSiblingElement();
855 bool equal = ele0->ShallowEqual( ele1 );
856
857 XMLTest( "Infinite loop in shallow equal.", true, equal );
858 }
859
Lee Thomason5708f812012-03-28 17:46:41 -0700860 // -------- Handles ------------
861 {
862 static const char* xml = "<element attrib='bar'><sub>Text</sub></element>";
863 XMLDocument doc;
864 doc.Parse( xml );
Lee Thomason5708f812012-03-28 17:46:41 -0700865
866 XMLElement* ele = XMLHandle( doc ).FirstChildElement( "element" ).FirstChild().ToElement();
867 XMLTest( "Handle, success, mutable", ele->Value(), "sub" );
868
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -0700869 XMLHandle docH( doc );
870 ele = docH.FirstChildElement( "none" ).FirstChildElement( "element" ).ToElement();
Lee Thomasond0b19df2012-04-12 08:41:37 -0700871 XMLTest( "Handle, dne, mutable", false, ele != 0 );
Lee Thomason5708f812012-03-28 17:46:41 -0700872 }
873
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -0700874 {
875 static const char* xml = "<element attrib='bar'><sub>Text</sub></element>";
876 XMLDocument doc;
877 doc.Parse( xml );
878 XMLConstHandle docH( doc );
879
880 const XMLElement* ele = docH.FirstChildElement( "element" ).FirstChild().ToElement();
881 XMLTest( "Handle, success, const", ele->Value(), "sub" );
882
883 ele = docH.FirstChildElement( "none" ).FirstChildElement( "element" ).ToElement();
Lee Thomasond0b19df2012-04-12 08:41:37 -0700884 XMLTest( "Handle, dne, const", false, ele != 0 );
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -0700885 }
Lee Thomasonf68c4382012-04-28 14:37:11 -0700886 {
887 // Default Declaration & BOM
888 XMLDocument doc;
889 doc.InsertEndChild( doc.NewDeclaration() );
890 doc.SetBOM( true );
891
892 XMLPrinter printer;
893 doc.Print( &printer );
894
895 static const char* result = "\xef\xbb\xbf<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
896 XMLTest( "BOM and default declaration", printer.CStr(), result, false );
Lee Thomason (grinliz)48ea0bc2012-05-26 14:41:14 -0700897 XMLTest( "CStrSize", printer.CStrSize(), 42, false );
Lee Thomasonf68c4382012-04-28 14:37:11 -0700898 }
Lee Thomason21be8822012-07-15 17:27:22 -0700899 {
900 const char* xml = "<ipxml ws='1'><info bla=' /></ipxml>";
901 XMLDocument doc;
902 doc.Parse( xml );
903 XMLTest( "Ill formed XML", true, doc.Error() );
904 }
905
906 // QueryXYZText
907 {
908 const char* xml = "<point> <x>1.2</x> <y>1</y> <z>38</z> <valid>true</valid> </point>";
909 XMLDocument doc;
910 doc.Parse( xml );
911
912 const XMLElement* pointElement = doc.RootElement();
913
914 int intValue = 0;
915 unsigned unsignedValue = 0;
916 float floatValue = 0;
917 double doubleValue = 0;
918 bool boolValue = false;
919
920 pointElement->FirstChildElement( "y" )->QueryIntText( &intValue );
921 pointElement->FirstChildElement( "y" )->QueryUnsignedText( &unsignedValue );
922 pointElement->FirstChildElement( "x" )->QueryFloatText( &floatValue );
923 pointElement->FirstChildElement( "x" )->QueryDoubleText( &doubleValue );
924 pointElement->FirstChildElement( "valid" )->QueryBoolText( &boolValue );
925
926
927 XMLTest( "QueryIntText", intValue, 1, false );
928 XMLTest( "QueryUnsignedText", unsignedValue, (unsigned)1, false );
929 XMLTest( "QueryFloatText", floatValue, 1.2f, false );
930 XMLTest( "QueryDoubleText", doubleValue, 1.2, false );
931 XMLTest( "QueryBoolText", boolValue, true, false );
932 }
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -0700933
934
Lee Thomason6f381b72012-03-02 12:59:39 -0800935 // ----------- Performance tracking --------------
936 {
937#if defined( _MSC_VER )
938 __int64 start, end, freq;
939 QueryPerformanceFrequency( (LARGE_INTEGER*) &freq );
940#endif
941
942#if defined(_MSC_VER)
943#pragma warning ( push )
944#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
945#endif
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300946 FILE* fp = fopen( "resources/dream.xml", "r" );
Lee Thomason6f381b72012-03-02 12:59:39 -0800947#if defined(_MSC_VER)
948#pragma warning ( pop )
949#endif
950 fseek( fp, 0, SEEK_END );
951 long size = ftell( fp );
952 fseek( fp, 0, SEEK_SET );
953
954 char* mem = new char[size+1];
955 fread( mem, size, 1, fp );
956 fclose( fp );
957 mem[size] = 0;
958
959#if defined( _MSC_VER )
960 QueryPerformanceCounter( (LARGE_INTEGER*) &start );
961#else
962 clock_t cstart = clock();
963#endif
964 static const int COUNT = 10;
965 for( int i=0; i<COUNT; ++i ) {
966 XMLDocument doc;
967 doc.Parse( mem );
968 }
969#if defined( _MSC_VER )
970 QueryPerformanceCounter( (LARGE_INTEGER*) &end );
971#else
972 clock_t cend = clock();
973#endif
974
975 delete [] mem;
976
977 static const char* note =
978#ifdef DEBUG
979 "DEBUG";
980#else
981 "Release";
982#endif
983
984#if defined( _MSC_VER )
985 printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, 1000.0 * (double)(end-start) / ( (double)freq * (double)COUNT) );
986#else
987 printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, (double)(cend - cstart)/(double)COUNT );
988#endif
989 }
990
Lee Thomason (grinliz)7ca55582012-03-07 21:54:57 -0800991 #if defined( _MSC_VER ) && defined( DEBUG )
Lee Thomason1ff38e02012-02-14 18:18:16 -0800992 _CrtMemCheckpoint( &endMemState );
993 //_CrtMemDumpStatistics( &endMemState );
994
995 _CrtMemState diffMemState;
996 _CrtMemDifference( &diffMemState, &startMemState, &endMemState );
997 _CrtMemDumpStatistics( &diffMemState );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800998 //printf( "new total=%d\n", gNewTotal );
Lee Thomason1ff38e02012-02-14 18:18:16 -0800999 #endif
1000
1001 printf ("\nPass %d, Fail %d\n", gPass, gFail);
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001002 return 0;
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -08001003}