blob: 81018aa5e7971a3c8b1a12d53e464f48b8b4df43 [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 Thomasone9699e62012-07-25 12:24:23 -07008 #include <direct.h> // _mkdir
Lee Thomason1ff38e02012-02-14 18:18:16 -08009 #include <crtdbg.h>
Lee Thomason6f381b72012-03-02 12:59:39 -080010 #define WIN32_LEAN_AND_MEAN
11 #include <windows.h>
Lee Thomason1ff38e02012-02-14 18:18:16 -080012 _CrtMemState startMemState;
13 _CrtMemState endMemState;
Lee Thomasone9699e62012-07-25 12:24:23 -070014#else
15 #include <sys/stat.h> // mkdir
Lee Thomason1ff38e02012-02-14 18:18:16 -080016#endif
Lee Thomasone9ecdab2012-02-13 18:11:20 -080017
U-Lama\Leee13c3e62011-12-28 14:36:55 -080018using namespace tinyxml2;
Lee Thomasonec5a7b42012-02-13 18:16:52 -080019int gPass = 0;
20int gFail = 0;
21
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -080022
U-Stream\Lee09a11c52012-02-17 08:31:16 -080023bool XMLTest (const char* testString, const char* expected, const char* found, bool echo=true )
Lee Thomason1ff38e02012-02-14 18:18:16 -080024{
25 bool pass = !strcmp( expected, found );
26 if ( pass )
27 printf ("[pass]");
28 else
29 printf ("[fail]");
30
U-Stream\Lee09a11c52012-02-17 08:31:16 -080031 if ( !echo )
Lee Thomason1ff38e02012-02-14 18:18:16 -080032 printf (" %s\n", testString);
33 else
34 printf (" %s [%s][%s]\n", testString, expected, found);
35
36 if ( pass )
37 ++gPass;
38 else
39 ++gFail;
40 return pass;
41}
42
43
Lee Thomason21be8822012-07-15 17:27:22 -070044template< class T > bool XMLTest( const char* testString, T expected, T found, bool echo=true )
Lee Thomason1ff38e02012-02-14 18:18:16 -080045{
46 bool pass = ( expected == found );
47 if ( pass )
48 printf ("[pass]");
49 else
50 printf ("[fail]");
51
U-Stream\Lee09a11c52012-02-17 08:31:16 -080052 if ( !echo )
Lee Thomason1ff38e02012-02-14 18:18:16 -080053 printf (" %s\n", testString);
54 else
Lee Thomasonc8312792012-07-16 12:44:41 -070055 printf (" %s [%d][%d]\n", testString, static_cast<int>(expected), static_cast<int>(found) );
Lee Thomason1ff38e02012-02-14 18:18:16 -080056
57 if ( pass )
58 ++gPass;
59 else
60 ++gFail;
61 return pass;
62}
Lee Thomasonec5a7b42012-02-13 18:16:52 -080063
U-Lama\Leee13c3e62011-12-28 14:36:55 -080064
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -080065void NullLineEndings( char* p )
66{
67 while( p && *p ) {
68 if ( *p == '\n' || *p == '\r' ) {
69 *p = 0;
70 return;
71 }
72 ++p;
73 }
74}
75
76
Lee Thomason (grinliz)6a22be22012-04-04 12:39:05 -070077// Comments in the header. (Don't know how to get Doxygen to read comments in this file.)
78int example_1()
79{
80 XMLDocument doc;
Bruno Diasa2d4e6e2012-05-07 04:58:11 -030081 doc.LoadFile( "resources/dream.xml" );
Lee Thomason (grinliz)6a22be22012-04-04 12:39:05 -070082
83 return doc.ErrorID();
84}
85
86
87// Comments in the header. (Don't know how to get Doxygen to read comments in this file.)
88int example_2()
89{
90 static const char* xml = "<element/>";
91 XMLDocument doc;
92 doc.Parse( xml );
93
94 return doc.ErrorID();
95}
96
97
98int example_3()
99{
Lee Thomason (grinliz)a4a36ba2012-04-06 21:24:29 -0700100 static const char* xml =
101 "<?xml version=\"1.0\"?>"
102 "<!DOCTYPE PLAY SYSTEM \"play.dtd\">"
103 "<PLAY>"
104 "<TITLE>A Midsummer Night's Dream</TITLE>"
105 "</PLAY>";
Lee Thomason (grinliz)6a22be22012-04-04 12:39:05 -0700106
107 XMLDocument doc;
108 doc.Parse( xml );
109
110 XMLElement* titleElement = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" );
111 const char* title = titleElement->GetText();
112 printf( "Name of play (1): %s\n", title );
113
114 XMLText* textNode = titleElement->FirstChild()->ToText();
115 title = textNode->Value();
116 printf( "Name of play (2): %s\n", title );
117
118 return doc.ErrorID();
119}
120
121
Lee Thomason21be8822012-07-15 17:27:22 -0700122bool example_4()
123{
124 static const char* xml =
125 "<information>"
126 " <attributeApproach v='2' />"
127 " <textApproach>"
128 " <v>2</v>"
129 " </textApproach>"
130 "</information>";
131
132 XMLDocument doc;
133 doc.Parse( xml );
134
135 int v0 = 0;
136 int v1 = 0;
137
138 XMLElement* attributeApproachElement = doc.FirstChildElement()->FirstChildElement( "attributeApproach" );
139 attributeApproachElement->QueryIntAttribute( "v", &v0 );
140
141 XMLElement* textApproachElement = doc.FirstChildElement()->FirstChildElement( "textApproach" );
142 textApproachElement->FirstChildElement( "v" )->QueryIntText( &v1 );
143
144 printf( "Both values are the same: %d and %d\n", v0, v1 );
145
146 return !doc.Error() && ( v0 == v1 );
147}
148
149
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700150int main( int /*argc*/, const char ** /*argv*/ )
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800151{
Lee Thomason (grinliz)0a4df402012-02-27 20:50:52 -0800152 #if defined( _MSC_VER ) && defined( DEBUG )
Lee Thomason1ff38e02012-02-14 18:18:16 -0800153 _CrtMemCheckpoint( &startMemState );
154 #endif
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800155
Lee Thomason7f7b1622012-03-24 12:49:03 -0700156 #if defined(_MSC_VER)
157 #pragma warning ( push )
158 #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
159 #endif
160
Lee Thomasone9699e62012-07-25 12:24:23 -0700161 #if defined(_MSC_VER)
162 _mkdir( "resources/out/" );
163 #else
164 mkdir( "resources/out/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
165 #endif
Arkadiy Shapkinff72d1f2012-07-24 00:24:07 +0400166
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300167 FILE* fp = fopen( "resources/dream.xml", "r" );
Lee Thomason7f7b1622012-03-24 12:49:03 -0700168 if ( !fp ) {
169 printf( "Error opening test file 'dream.xml'.\n"
170 "Is your working directory the same as where \n"
171 "the xmltest.cpp and dream.xml file are?\n\n"
172 #if defined( _MSC_VER )
173 "In windows Visual Studio you may need to set\n"
174 "Properties->Debugging->Working Directory to '..'\n"
175 #endif
176 );
177 exit( 1 );
178 }
179 fclose( fp );
180
181 #if defined(_MSC_VER)
182 #pragma warning ( pop )
183 #endif
184
Lee Thomason (grinliz)6a22be22012-04-04 12:39:05 -0700185 XMLTest( "Example-1", 0, example_1() );
186 XMLTest( "Example-2", 0, example_2() );
187 XMLTest( "Example-3", 0, example_3() );
Lee Thomason21be8822012-07-15 17:27:22 -0700188 XMLTest( "Example-4", true, example_4() );
Lee Thomason87e475a2012-03-20 11:55:29 -0700189
Lee Thomason (grinliz)6a22be22012-04-04 12:39:05 -0700190 /* ------ Example 2: Lookup information. ---- */
Lee Thomason87e475a2012-03-20 11:55:29 -0700191
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800192 {
Lee Thomason43f59302012-02-06 18:18:11 -0800193 static const char* test[] = { "<element />",
Arkadiy Shapkinef1c69c2012-07-25 22:10:39 +0400194 "<element></element>",
Lee Thomason43f59302012-02-06 18:18:11 -0800195 "<element><subelement/></element>",
Arkadiy Shapkinef1c69c2012-07-25 22:10:39 +0400196 "<element><subelement></subelement></element>",
197 "<element><subelement><subsub/></subelement></element>",
198 "<!--comment beside elements--><element><subelement></subelement></element>",
199 "<!--comment beside elements, this time with spaces--> \n <element> <subelement> \n </subelement> </element>",
200 "<element attrib1='foo' attrib2=\"bar\" ></element>",
201 "<element attrib1='foo' attrib2=\"bar\" ><subelement attrib3='yeehaa' /></element>",
Lee Thomason43f59302012-02-06 18:18:11 -0800202 "<element>Text inside element.</element>",
203 "<element><b></b></element>",
204 "<element>Text inside and <b>bolded</b> in the element.</element>",
205 "<outer><element>Text inside and <b>bolded</b> in the element.</element></outer>",
Lee Thomason8ee79892012-01-25 17:44:30 -0800206 "<element>This &amp; That.</element>",
Lee Thomason18d68bd2012-01-26 18:17:26 -0800207 "<element attrib='This&lt;That' />",
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800208 0
209 };
Lee Thomason6ee99fc2012-01-21 18:45:16 -0800210 for( int i=0; test[i]; ++i ) {
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800211 XMLDocument doc;
Lee Thomason6ee99fc2012-01-21 18:45:16 -0800212 doc.Parse( test[i] );
Lee Thomason5cae8972012-01-24 18:03:07 -0800213 doc.Print();
Lee Thomasonec975ce2012-01-23 11:42:06 -0800214 printf( "----------------------------------------------\n" );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800215 }
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800216 }
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800217#if 1
Lee Thomasond6277762012-02-22 16:00:12 -0800218 {
219 static const char* test = "<!--hello world\n"
Arkadiy Shapkinef1c69c2012-07-25 22:10:39 +0400220 " line 2\r"
221 " line 3\r\n"
222 " line 4\n\r"
223 " line 5\r-->";
Lee Thomasond6277762012-02-22 16:00:12 -0800224
225 XMLDocument doc;
226 doc.Parse( test );
227 doc.Print();
228 }
229
Lee Thomason2c85a712012-01-31 08:24:24 -0800230 {
231 static const char* test = "<element>Text before.</element>";
232 XMLDocument doc;
233 doc.Parse( test );
234 XMLElement* root = doc.FirstChildElement();
235 XMLElement* newElement = doc.NewElement( "Subelement" );
236 root->InsertEndChild( newElement );
237 doc.Print();
238 }
Lee Thomasond1983222012-02-06 08:41:24 -0800239 {
240 XMLDocument* doc = new XMLDocument();
241 static const char* test = "<element><sub/></element>";
242 doc->Parse( test );
243 delete doc;
244 }
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800245 {
Lee Thomason1ff38e02012-02-14 18:18:16 -0800246 // Test: Programmatic DOM
Lee Thomasonec5a7b42012-02-13 18:16:52 -0800247 // Build:
248 // <element>
249 // <!--comment-->
250 // <sub attrib="1" />
251 // <sub attrib="2" />
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800252 // <sub attrib="3" >& Text!</sub>
Lee Thomasonec5a7b42012-02-13 18:16:52 -0800253 // <element>
254
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800255 XMLDocument* doc = new XMLDocument();
Lee Thomason1ff38e02012-02-14 18:18:16 -0800256 XMLNode* element = doc->InsertEndChild( doc->NewElement( "element" ) );
257
258 XMLElement* sub[3] = { doc->NewElement( "sub" ), doc->NewElement( "sub" ), doc->NewElement( "sub" ) };
259 for( int i=0; i<3; ++i ) {
260 sub[i]->SetAttribute( "attrib", i );
261 }
262 element->InsertEndChild( sub[2] );
263 XMLNode* comment = element->InsertFirstChild( doc->NewComment( "comment" ) );
264 element->InsertAfterChild( comment, sub[0] );
265 element->InsertAfterChild( sub[0], sub[1] );
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800266 sub[2]->InsertFirstChild( doc->NewText( "& Text!" ));
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800267 doc->Print();
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800268 XMLTest( "Programmatic DOM", "comment", doc->FirstChildElement( "element" )->FirstChild()->Value() );
269 XMLTest( "Programmatic DOM", "0", doc->FirstChildElement( "element" )->FirstChildElement()->Attribute( "attrib" ) );
270 XMLTest( "Programmatic DOM", 2, doc->FirstChildElement()->LastChildElement( "sub" )->IntAttribute( "attrib" ) );
271 XMLTest( "Programmatic DOM", "& Text!",
272 doc->FirstChildElement()->LastChildElement( "sub" )->FirstChild()->ToText()->Value() );
U-Stream\Leeae25a442012-02-17 17:48:16 -0800273
274 // And now deletion:
275 element->DeleteChild( sub[2] );
276 doc->DeleteNode( comment );
277
278 element->FirstChildElement()->SetAttribute( "attrib", true );
279 element->LastChildElement()->DeleteAttribute( "attrib" );
280
281 XMLTest( "Programmatic DOM", true, doc->FirstChildElement()->FirstChildElement()->BoolAttribute( "attrib" ) );
282 int value = 10;
283 int result = doc->FirstChildElement()->LastChildElement()->QueryIntAttribute( "attrib", &value );
Lee Thomason21be8822012-07-15 17:27:22 -0700284 XMLTest( "Programmatic DOM", result, (int)XML_NO_ATTRIBUTE );
U-Stream\Leeae25a442012-02-17 17:48:16 -0800285 XMLTest( "Programmatic DOM", value, 10 );
286
287 doc->Print();
288
Lee Thomason7b1b86a2012-06-04 17:01:38 -0700289 {
290 XMLPrinter streamer;
291 doc->Print( &streamer );
292 printf( "%s", streamer.CStr() );
293 }
294 {
295 XMLPrinter streamer( 0, true );
296 doc->Print( &streamer );
297 XMLTest( "Compact mode", "<element><sub attrib=\"1\"/><sub/></element>", streamer.CStr(), false );
298 }
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800299 delete doc;
300 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800301 {
302 // Test: Dream
303 // XML1 : 1,187,569 bytes in 31,209 allocations
304 // XML2 : 469,073 bytes in 323 allocations
305 //int newStart = gNew;
306 XMLDocument doc;
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300307 doc.LoadFile( "resources/dream.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800308
Arkadiy Shapkinff72d1f2012-07-24 00:24:07 +0400309 doc.SaveFile( "resources/out/dreamout.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800310 doc.PrintError();
311
312 XMLTest( "Dream", "xml version=\"1.0\"",
Arkadiy Shapkinef1c69c2012-07-25 22:10:39 +0400313 doc.FirstChild()->ToDeclaration()->Value() );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800314 XMLTest( "Dream", true, doc.FirstChild()->NextSibling()->ToUnknown() ? true : false );
315 XMLTest( "Dream", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
316 doc.FirstChild()->NextSibling()->ToUnknown()->Value() );
317 XMLTest( "Dream", "And Robin shall restore amends.",
Arkadiy Shapkinef1c69c2012-07-25 22:10:39 +0400318 doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800319 XMLTest( "Dream", "And Robin shall restore amends.",
Arkadiy Shapkinef1c69c2012-07-25 22:10:39 +0400320 doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800321
322 XMLDocument doc2;
Arkadiy Shapkinff72d1f2012-07-24 00:24:07 +0400323 doc2.LoadFile( "resources/out/dreamout.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800324 XMLTest( "Dream-out", "xml version=\"1.0\"",
Arkadiy Shapkinef1c69c2012-07-25 22:10:39 +0400325 doc2.FirstChild()->ToDeclaration()->Value() );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800326 XMLTest( "Dream-out", true, doc2.FirstChild()->NextSibling()->ToUnknown() ? true : false );
327 XMLTest( "Dream-out", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
328 doc2.FirstChild()->NextSibling()->ToUnknown()->Value() );
329 XMLTest( "Dream-out", "And Robin shall restore amends.",
Arkadiy Shapkinef1c69c2012-07-25 22:10:39 +0400330 doc2.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800331
332 //gNewTotal = gNew - newStart;
333 }
Lee Thomason6f381b72012-03-02 12:59:39 -0800334
335
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800336 {
337 const char* error = "<?xml version=\"1.0\" standalone=\"no\" ?>\n"
338 "<passages count=\"006\" formatversion=\"20020620\">\n"
339 " <wrong error>\n"
340 "</passages>";
341
342 XMLDocument doc;
343 doc.Parse( error );
Lee Thomason21be8822012-07-15 17:27:22 -0700344 XMLTest( "Bad XML", doc.ErrorID(), (int)XML_ERROR_PARSING_ATTRIBUTE );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800345 }
346
347 {
348 const char* str = "<doc attr0='1' attr1='2.0' attr2='foo' />";
349
350 XMLDocument doc;
351 doc.Parse( str );
352
353 XMLElement* ele = doc.FirstChildElement();
354
355 int iVal, result;
356 double dVal;
357
358 result = ele->QueryDoubleAttribute( "attr0", &dVal );
Lee Thomason21be8822012-07-15 17:27:22 -0700359 XMLTest( "Query attribute: int as double", result, (int)XML_NO_ERROR );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800360 XMLTest( "Query attribute: int as double", (int)dVal, 1 );
361 result = ele->QueryDoubleAttribute( "attr1", &dVal );
362 XMLTest( "Query attribute: double as double", (int)dVal, 2 );
363 result = ele->QueryIntAttribute( "attr1", &iVal );
Lee Thomason21be8822012-07-15 17:27:22 -0700364 XMLTest( "Query attribute: double as int", result, (int)XML_NO_ERROR );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800365 XMLTest( "Query attribute: double as int", iVal, 2 );
366 result = ele->QueryIntAttribute( "attr2", &iVal );
Lee Thomason21be8822012-07-15 17:27:22 -0700367 XMLTest( "Query attribute: not a number", result, (int)XML_WRONG_ATTRIBUTE_TYPE );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800368 result = ele->QueryIntAttribute( "bar", &iVal );
Lee Thomason21be8822012-07-15 17:27:22 -0700369 XMLTest( "Query attribute: does not exist", result, (int)XML_NO_ATTRIBUTE );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800370 }
371
372 {
373 const char* str = "<doc/>";
374
375 XMLDocument doc;
376 doc.Parse( str );
377
378 XMLElement* ele = doc.FirstChildElement();
379
380 int iVal;
381 double dVal;
382
383 ele->SetAttribute( "str", "strValue" );
384 ele->SetAttribute( "int", 1 );
385 ele->SetAttribute( "double", -1.0 );
386
387 const char* cStr = ele->Attribute( "str" );
388 ele->QueryIntAttribute( "int", &iVal );
389 ele->QueryDoubleAttribute( "double", &dVal );
390
Lee Thomason8ba7f7d2012-03-24 13:04:04 -0700391 XMLTest( "Attribute match test", ele->Attribute( "str", "strValue" ), "strValue" );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800392 XMLTest( "Attribute round trip. c-string.", "strValue", cStr );
393 XMLTest( "Attribute round trip. int.", 1, iVal );
394 XMLTest( "Attribute round trip. double.", -1, (int)dVal );
395 }
396
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800397 {
398 XMLDocument doc;
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300399 doc.LoadFile( "resources/utf8test.xml" );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800400
401 // Get the attribute "value" from the "Russian" element and check it.
402 XMLElement* element = doc.FirstChildElement( "document" )->FirstChildElement( "Russian" );
403 const unsigned char correctValue[] = { 0xd1U, 0x86U, 0xd0U, 0xb5U, 0xd0U, 0xbdU, 0xd0U, 0xbdU,
404 0xd0U, 0xbeU, 0xd1U, 0x81U, 0xd1U, 0x82U, 0xd1U, 0x8cU, 0 };
405
406 XMLTest( "UTF-8: Russian value.", (const char*)correctValue, element->Attribute( "value" ) );
407
408 const unsigned char russianElementName[] = { 0xd0U, 0xa0U, 0xd1U, 0x83U,
409 0xd1U, 0x81U, 0xd1U, 0x81U,
410 0xd0U, 0xbaU, 0xd0U, 0xb8U,
411 0xd0U, 0xb9U, 0 };
412 const char russianText[] = "<\xD0\xB8\xD0\xBC\xD0\xB5\xD0\xB5\xD1\x82>";
413
414 XMLText* text = doc.FirstChildElement( "document" )->FirstChildElement( (const char*) russianElementName )->FirstChild()->ToText();
415 XMLTest( "UTF-8: Browsing russian element name.",
416 russianText,
417 text->Value() );
418
419 // Now try for a round trip.
Arkadiy Shapkinff72d1f2012-07-24 00:24:07 +0400420 doc.SaveFile( "resources/out/utf8testout.xml" );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800421
422 // Check the round trip.
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800423 int okay = 0;
424
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800425
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800426#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800427#pragma warning ( push )
428#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800429#endif
Thomas Roßa6dd8c62012-07-26 20:42:18 +0200430 FILE* saved = fopen( "resources/out/utf8testout.xml", "r" );
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300431 FILE* verify = fopen( "resources/utf8testverify.xml", "r" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800432#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800433#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800434#endif
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800435
436 if ( saved && verify )
437 {
438 okay = 1;
PKEuSc28ba3a2012-07-16 03:08:47 -0700439 char verifyBuf[256];
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800440 while ( fgets( verifyBuf, 256, verify ) )
441 {
PKEuSc28ba3a2012-07-16 03:08:47 -0700442 char savedBuf[256];
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800443 fgets( savedBuf, 256, saved );
444 NullLineEndings( verifyBuf );
445 NullLineEndings( savedBuf );
446
447 if ( strcmp( verifyBuf, savedBuf ) )
448 {
449 printf( "verify:%s<\n", verifyBuf );
450 printf( "saved :%s<\n", savedBuf );
451 okay = 0;
452 break;
453 }
454 }
455 }
456 if ( saved )
457 fclose( saved );
458 if ( verify )
459 fclose( verify );
460 XMLTest( "UTF-8: Verified multi-language round trip.", 1, okay );
461 }
462
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800463 // --------GetText()-----------
464 {
465 const char* str = "<foo>This is text</foo>";
466 XMLDocument doc;
467 doc.Parse( str );
468 const XMLElement* element = doc.RootElement();
469
470 XMLTest( "GetText() normal use.", "This is text", element->GetText() );
471
472 str = "<foo><b>This is text</b></foo>";
473 doc.Parse( str );
474 element = doc.RootElement();
475
476 XMLTest( "GetText() contained element.", element->GetText() == 0, true );
477 }
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800478
Lee Thomasond6277762012-02-22 16:00:12 -0800479
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800480 // ---------- CDATA ---------------
481 {
482 const char* str = "<xmlElement>"
483 "<![CDATA["
484 "I am > the rules!\n"
485 "...since I make symbolic puns"
486 "]]>"
487 "</xmlElement>";
488 XMLDocument doc;
489 doc.Parse( str );
490 doc.Print();
Lee Thomasond6277762012-02-22 16:00:12 -0800491
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800492 XMLTest( "CDATA parse.", doc.FirstChildElement()->FirstChild()->Value(),
493 "I am > the rules!\n...since I make symbolic puns",
Lee Thomasond6277762012-02-22 16:00:12 -0800494 false );
495 }
496
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800497 // ----------- CDATA -------------
498 {
499 const char* str = "<xmlElement>"
500 "<![CDATA["
501 "<b>I am > the rules!</b>\n"
502 "...since I make symbolic puns"
503 "]]>"
504 "</xmlElement>";
505 XMLDocument doc;
506 doc.Parse( str );
507 doc.Print();
508
509 XMLTest( "CDATA parse. [ tixml1:1480107 ]", doc.FirstChildElement()->FirstChild()->Value(),
510 "<b>I am > the rules!</b>\n...since I make symbolic puns",
511 false );
512 }
513
514 // InsertAfterChild causes crash.
515 {
516 // InsertBeforeChild and InsertAfterChild causes crash.
517 XMLDocument doc;
518 XMLElement* parent = doc.NewElement( "Parent" );
519 doc.InsertFirstChild( parent );
520
521 XMLElement* childText0 = doc.NewElement( "childText0" );
522 XMLElement* childText1 = doc.NewElement( "childText1" );
523
524 XMLNode* childNode0 = parent->InsertEndChild( childText0 );
525 XMLNode* childNode1 = parent->InsertAfterChild( childNode0, childText1 );
526
527 XMLTest( "Test InsertAfterChild on empty node. ", ( childNode1 == parent->LastChild() ), true );
528 }
Lee Thomasond6277762012-02-22 16:00:12 -0800529
530 {
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800531 // Entities not being written correctly.
532 // From Lynn Allen
Lee Thomasond6277762012-02-22 16:00:12 -0800533
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800534 const char* passages =
535 "<?xml version=\"1.0\" standalone=\"no\" ?>"
536 "<passages count=\"006\" formatversion=\"20020620\">"
537 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
538 " It also has &lt;, &gt;, and &amp;, as well as a fake copyright &#xA9;.\"> </psg>"
539 "</passages>";
Lee Thomasond6277762012-02-22 16:00:12 -0800540
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800541 XMLDocument doc;
542 doc.Parse( passages );
543 XMLElement* psg = doc.RootElement()->FirstChildElement();
544 const char* context = psg->Attribute( "context" );
545 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 -0800546
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800547 XMLTest( "Entity transformation: read. ", expected, context, true );
Lee Thomasond6277762012-02-22 16:00:12 -0800548
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800549#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800550#pragma warning ( push )
551#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800552#endif
Arkadiy Shapkinff72d1f2012-07-24 00:24:07 +0400553 FILE* textfile = fopen( "resources/out/textfile.txt", "w" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800554#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800555#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800556#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800557 if ( textfile )
558 {
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800559 XMLPrinter streamer( textfile );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800560 psg->Accept( &streamer );
561 fclose( textfile );
562 }
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800563#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800564#pragma warning ( push )
565#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800566#endif
Arkadiy Shapkinff72d1f2012-07-24 00:24:07 +0400567 textfile = fopen( "resources/out/textfile.txt", "r" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800568#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800569#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800570#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800571 TIXMLASSERT( textfile );
572 if ( textfile )
573 {
574 char buf[ 1024 ];
575 fgets( buf, 1024, textfile );
576 XMLTest( "Entity transformation: write. ",
577 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
578 " It also has &lt;, &gt;, and &amp;, as well as a fake copyright \xC2\xA9.\"/>\n",
579 buf, false );
PKEuSc28ba3a2012-07-16 03:08:47 -0700580 fclose( textfile );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800581 }
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800582 }
583
584 {
Lee Thomason6f381b72012-03-02 12:59:39 -0800585 // Suppress entities.
586 const char* passages =
587 "<?xml version=\"1.0\" standalone=\"no\" ?>"
588 "<passages count=\"006\" formatversion=\"20020620\">"
589 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;.\">Crazy &ttk;</psg>"
590 "</passages>";
591
592 XMLDocument doc( false );
593 doc.Parse( passages );
594
595 XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->Attribute( "context" ),
596 "Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;." );
597 XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->FirstChild()->Value(),
598 "Crazy &ttk;" );
599 doc.Print();
600 }
601
602 {
Arkadiy Shapkinef1c69c2012-07-25 22:10:39 +0400603 const char* test = "<?xml version='1.0'?><a.elem xmi.version='2.0'/>";
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800604
605 XMLDocument doc;
Arkadiy Shapkinef1c69c2012-07-25 22:10:39 +0400606 doc.Parse( test );
607 XMLTest( "dot in names", doc.Error(), false );
608 XMLTest( "dot in names", doc.FirstChildElement()->Name(), "a.elem" );
609 XMLTest( "dot in names", doc.FirstChildElement()->Attribute( "xmi.version" ), "2.0" );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800610 }
611
612 {
Arkadiy Shapkinef1c69c2012-07-25 22:10:39 +0400613 const char* test = "<element><Name>1.1 Start easy ignore fin thickness&#xA;</Name></element>";
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800614
Arkadiy Shapkinef1c69c2012-07-25 22:10:39 +0400615 XMLDocument doc;
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800616 doc.Parse( test );
617
618 XMLText* text = doc.FirstChildElement()->FirstChildElement()->FirstChild()->ToText();
619 XMLTest( "Entity with one digit.",
620 text->Value(), "1.1 Start easy ignore fin thickness\n",
621 false );
Arkadiy Shapkinef1c69c2012-07-25 22:10:39 +0400622 }
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800623
624 {
625 // DOCTYPE not preserved (950171)
626 //
627 const char* doctype =
628 "<?xml version=\"1.0\" ?>"
629 "<!DOCTYPE PLAY SYSTEM 'play.dtd'>"
630 "<!ELEMENT title (#PCDATA)>"
631 "<!ELEMENT books (title,authors)>"
632 "<element />";
633
634 XMLDocument doc;
635 doc.Parse( doctype );
Arkadiy Shapkinff72d1f2012-07-24 00:24:07 +0400636 doc.SaveFile( "resources/out/test7.xml" );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800637 doc.DeleteChild( doc.RootElement() );
Arkadiy Shapkinff72d1f2012-07-24 00:24:07 +0400638 doc.LoadFile( "resources/out/test7.xml" );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800639 doc.Print();
640
641 const XMLUnknown* decl = doc.FirstChild()->NextSibling()->ToUnknown();
642 XMLTest( "Correct value of unknown.", "DOCTYPE PLAY SYSTEM 'play.dtd'", decl->Value() );
643
644 }
645
646 {
647 // Comments do not stream out correctly.
648 const char* doctype =
649 "<!-- Somewhat<evil> -->";
650 XMLDocument doc;
651 doc.Parse( doctype );
652
653 XMLComment* comment = doc.FirstChild()->ToComment();
654
655 XMLTest( "Comment formatting.", " Somewhat<evil> ", comment->Value() );
656 }
657 {
658 // Double attributes
659 const char* doctype = "<element attr='red' attr='blue' />";
660
661 XMLDocument doc;
662 doc.Parse( doctype );
663
Lee Thomason21be8822012-07-15 17:27:22 -0700664 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 -0800665 doc.PrintError();
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800666 }
667
668 {
669 // Embedded null in stream.
670 const char* doctype = "<element att\0r='red' attr='blue' />";
671
672 XMLDocument doc;
673 doc.Parse( doctype );
674 XMLTest( "Embedded null throws error.", true, doc.Error() );
675 }
676
677 {
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700678 // Empty documents should return TIXML_XML_ERROR_PARSING_EMPTY, bug 1070717
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800679 const char* str = " ";
680 XMLDocument doc;
681 doc.Parse( str );
Lee Thomason21be8822012-07-15 17:27:22 -0700682 XMLTest( "Empty document error", (int)XML_ERROR_EMPTY_DOCUMENT, doc.ErrorID() );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800683 }
684
685 {
686 // Low entities
687 XMLDocument doc;
688 doc.Parse( "<test>&#x0e;</test>" );
689 const char result[] = { 0x0e, 0 };
690 XMLTest( "Low entities.", doc.FirstChildElement()->GetText(), result );
691 doc.Print();
692 }
693
694 {
695 // Attribute values with trailing quotes not handled correctly
696 XMLDocument doc;
697 doc.Parse( "<foo attribute=bar\" />" );
698 XMLTest( "Throw error with bad end quotes.", doc.Error(), true );
699 }
700
701 {
702 // [ 1663758 ] Failure to report error on bad XML
703 XMLDocument xml;
704 xml.Parse("<x>");
705 XMLTest("Missing end tag at end of input", xml.Error(), true);
706 xml.Parse("<x> ");
707 XMLTest("Missing end tag with trailing whitespace", xml.Error(), true);
708 xml.Parse("<x></y>");
Lee Thomason21be8822012-07-15 17:27:22 -0700709 XMLTest("Mismatched tags", xml.ErrorID(), (int)XML_ERROR_MISMATCHED_ELEMENT);
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800710 }
711
712
713 {
714 // [ 1475201 ] TinyXML parses entities in comments
715 XMLDocument xml;
716 xml.Parse("<!-- declarations for <head> & <body> -->"
717 "<!-- far &amp; away -->" );
718
719 XMLNode* e0 = xml.FirstChild();
720 XMLNode* e1 = e0->NextSibling();
721 XMLComment* c0 = e0->ToComment();
722 XMLComment* c1 = e1->ToComment();
723
724 XMLTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
725 XMLTest( "Comments ignore entities.", " far &amp; away ", c1->Value(), true );
726 }
727
728 {
729 XMLDocument xml;
730 xml.Parse( "<Parent>"
731 "<child1 att=''/>"
732 "<!-- With this comment, child2 will not be parsed! -->"
733 "<child2 att=''/>"
734 "</Parent>" );
735 xml.Print();
736
737 int count = 0;
738
739 for( XMLNode* ele = xml.FirstChildElement( "Parent" )->FirstChild();
740 ele;
741 ele = ele->NextSibling() )
742 {
743 ++count;
744 }
745
746 XMLTest( "Comments iterate correctly.", 3, count );
747 }
748
749 {
750 // trying to repro ]1874301]. If it doesn't go into an infinite loop, all is well.
751 unsigned char buf[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?><feed><![CDATA[Test XMLblablablalblbl";
752 buf[60] = 239;
753 buf[61] = 0;
754
755 XMLDocument doc;
756 doc.Parse( (const char*)buf);
757 }
758
759
760 {
761 // bug 1827248 Error while parsing a little bit malformed file
762 // Actually not malformed - should work.
763 XMLDocument xml;
764 xml.Parse( "<attributelist> </attributelist >" );
765 XMLTest( "Handle end tag whitespace", false, xml.Error() );
766 }
767
768 {
769 // This one must not result in an infinite loop
770 XMLDocument xml;
771 xml.Parse( "<infinite>loop" );
772 XMLTest( "Infinite loop test.", true, true );
773 }
774#endif
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800775 {
776 const char* pub = "<?xml version='1.0'?> <element><sub/></element> <!--comment--> <!DOCTYPE>";
777 XMLDocument doc;
778 doc.Parse( pub );
779
780 XMLDocument clone;
781 for( const XMLNode* node=doc.FirstChild(); node; node=node->NextSibling() ) {
782 XMLNode* copy = node->ShallowClone( &clone );
783 clone.InsertEndChild( copy );
784 }
785
786 clone.Print();
787
788 int count=0;
789 const XMLNode* a=clone.FirstChild();
790 const XMLNode* b=doc.FirstChild();
791 for( ; a && b; a=a->NextSibling(), b=b->NextSibling() ) {
792 ++count;
793 XMLTest( "Clone and Equal", true, a->ShallowEqual( b ));
794 }
795 XMLTest( "Clone and Equal", 4, count );
796 }
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800797
Lee Thomason (grinliz)a4a36ba2012-04-06 21:24:29 -0700798 {
799 // This shouldn't crash.
800 XMLDocument doc;
801 if(XML_NO_ERROR != doc.LoadFile( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ))
802 {
803 doc.PrintError();
804 }
805 XMLTest( "Error in snprinf handling.", true, doc.Error() );
806 }
Lee Thomason5e3803c2012-04-16 08:57:05 -0700807
808 {
809 // Attribute ordering.
810 static const char* xml = "<element attrib1=\"1\" attrib2=\"2\" attrib3=\"3\" />";
811 XMLDocument doc;
812 doc.Parse( xml );
813 XMLElement* ele = doc.FirstChildElement();
814
815 const XMLAttribute* a = ele->FirstAttribute();
816 XMLTest( "Attribute order", "1", a->Value() );
817 a = a->Next();
818 XMLTest( "Attribute order", "2", a->Value() );
819 a = a->Next();
820 XMLTest( "Attribute order", "3", a->Value() );
821 XMLTest( "Attribute order", "attrib3", a->Name() );
822
823 ele->DeleteAttribute( "attrib2" );
824 a = ele->FirstAttribute();
825 XMLTest( "Attribute order", "1", a->Value() );
826 a = a->Next();
827 XMLTest( "Attribute order", "3", a->Value() );
828
829 ele->DeleteAttribute( "attrib1" );
830 ele->DeleteAttribute( "attrib3" );
831 XMLTest( "Attribute order (empty)", false, ele->FirstAttribute() ? true : false );
832 }
Lee Thomason (grinliz)a4a36ba2012-04-06 21:24:29 -0700833
Lee Thomason (grinliz)390e9782012-07-01 21:22:53 -0700834 {
835 // Make sure an attribute with a space in it succeeds.
Lee Thomason78a773d2012-07-02 10:10:19 -0700836 static const char* xml0 = "<element attribute1= \"Test Attribute\"/>";
837 static const char* xml1 = "<element attribute1 =\"Test Attribute\"/>";
838 static const char* xml2 = "<element attribute1 = \"Test Attribute\"/>";
839 XMLDocument doc0;
840 doc0.Parse( xml0 );
841 XMLDocument doc1;
842 doc1.Parse( xml1 );
843 XMLDocument doc2;
844 doc2.Parse( xml2 );
Lee Thomason (grinliz)390e9782012-07-01 21:22:53 -0700845
Lee Thomason78a773d2012-07-02 10:10:19 -0700846 XMLElement* ele = 0;
847 ele = doc0.FirstChildElement();
848 XMLTest( "Attribute with space #1", "Test Attribute", ele->Attribute( "attribute1" ) );
849 ele = doc1.FirstChildElement();
850 XMLTest( "Attribute with space #2", "Test Attribute", ele->Attribute( "attribute1" ) );
851 ele = doc2.FirstChildElement();
852 XMLTest( "Attribute with space #3", "Test Attribute", ele->Attribute( "attribute1" ) );
Lee Thomason (grinliz)390e9782012-07-01 21:22:53 -0700853 }
854
855 {
856 // Make sure we don't go into an infinite loop.
857 static const char* xml = "<doc><element attribute='attribute'/><element attribute='attribute'/></doc>";
858 XMLDocument doc;
859 doc.Parse( xml );
860 XMLElement* ele0 = doc.FirstChildElement()->FirstChildElement();
861 XMLElement* ele1 = ele0->NextSiblingElement();
862 bool equal = ele0->ShallowEqual( ele1 );
863
864 XMLTest( "Infinite loop in shallow equal.", true, equal );
865 }
866
Lee Thomason5708f812012-03-28 17:46:41 -0700867 // -------- Handles ------------
868 {
869 static const char* xml = "<element attrib='bar'><sub>Text</sub></element>";
870 XMLDocument doc;
871 doc.Parse( xml );
Lee Thomason5708f812012-03-28 17:46:41 -0700872
873 XMLElement* ele = XMLHandle( doc ).FirstChildElement( "element" ).FirstChild().ToElement();
874 XMLTest( "Handle, success, mutable", ele->Value(), "sub" );
875
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -0700876 XMLHandle docH( doc );
877 ele = docH.FirstChildElement( "none" ).FirstChildElement( "element" ).ToElement();
Lee Thomasond0b19df2012-04-12 08:41:37 -0700878 XMLTest( "Handle, dne, mutable", false, ele != 0 );
Lee Thomason5708f812012-03-28 17:46:41 -0700879 }
880
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -0700881 {
882 static const char* xml = "<element attrib='bar'><sub>Text</sub></element>";
883 XMLDocument doc;
884 doc.Parse( xml );
885 XMLConstHandle docH( doc );
886
887 const XMLElement* ele = docH.FirstChildElement( "element" ).FirstChild().ToElement();
888 XMLTest( "Handle, success, const", ele->Value(), "sub" );
889
890 ele = docH.FirstChildElement( "none" ).FirstChildElement( "element" ).ToElement();
Lee Thomasond0b19df2012-04-12 08:41:37 -0700891 XMLTest( "Handle, dne, const", false, ele != 0 );
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -0700892 }
Lee Thomasonf68c4382012-04-28 14:37:11 -0700893 {
894 // Default Declaration & BOM
895 XMLDocument doc;
896 doc.InsertEndChild( doc.NewDeclaration() );
897 doc.SetBOM( true );
898
899 XMLPrinter printer;
900 doc.Print( &printer );
901
902 static const char* result = "\xef\xbb\xbf<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
903 XMLTest( "BOM and default declaration", printer.CStr(), result, false );
Lee Thomason (grinliz)48ea0bc2012-05-26 14:41:14 -0700904 XMLTest( "CStrSize", printer.CStrSize(), 42, false );
Lee Thomasonf68c4382012-04-28 14:37:11 -0700905 }
Lee Thomason21be8822012-07-15 17:27:22 -0700906 {
907 const char* xml = "<ipxml ws='1'><info bla=' /></ipxml>";
908 XMLDocument doc;
909 doc.Parse( xml );
910 XMLTest( "Ill formed XML", true, doc.Error() );
911 }
912
913 // QueryXYZText
914 {
915 const char* xml = "<point> <x>1.2</x> <y>1</y> <z>38</z> <valid>true</valid> </point>";
916 XMLDocument doc;
917 doc.Parse( xml );
918
919 const XMLElement* pointElement = doc.RootElement();
920
921 int intValue = 0;
922 unsigned unsignedValue = 0;
923 float floatValue = 0;
924 double doubleValue = 0;
925 bool boolValue = false;
926
927 pointElement->FirstChildElement( "y" )->QueryIntText( &intValue );
928 pointElement->FirstChildElement( "y" )->QueryUnsignedText( &unsignedValue );
929 pointElement->FirstChildElement( "x" )->QueryFloatText( &floatValue );
930 pointElement->FirstChildElement( "x" )->QueryDoubleText( &doubleValue );
931 pointElement->FirstChildElement( "valid" )->QueryBoolText( &boolValue );
932
933
934 XMLTest( "QueryIntText", intValue, 1, false );
935 XMLTest( "QueryUnsignedText", unsignedValue, (unsigned)1, false );
936 XMLTest( "QueryFloatText", floatValue, 1.2f, false );
937 XMLTest( "QueryDoubleText", doubleValue, 1.2, false );
938 XMLTest( "QueryBoolText", boolValue, true, false );
939 }
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -0700940
941
Lee Thomason6f381b72012-03-02 12:59:39 -0800942 // ----------- Performance tracking --------------
943 {
944#if defined( _MSC_VER )
945 __int64 start, end, freq;
946 QueryPerformanceFrequency( (LARGE_INTEGER*) &freq );
947#endif
948
949#if defined(_MSC_VER)
950#pragma warning ( push )
951#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
952#endif
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300953 FILE* fp = fopen( "resources/dream.xml", "r" );
Lee Thomason6f381b72012-03-02 12:59:39 -0800954#if defined(_MSC_VER)
955#pragma warning ( pop )
956#endif
957 fseek( fp, 0, SEEK_END );
958 long size = ftell( fp );
959 fseek( fp, 0, SEEK_SET );
960
961 char* mem = new char[size+1];
962 fread( mem, size, 1, fp );
963 fclose( fp );
964 mem[size] = 0;
965
966#if defined( _MSC_VER )
967 QueryPerformanceCounter( (LARGE_INTEGER*) &start );
968#else
969 clock_t cstart = clock();
970#endif
971 static const int COUNT = 10;
972 for( int i=0; i<COUNT; ++i ) {
973 XMLDocument doc;
974 doc.Parse( mem );
975 }
976#if defined( _MSC_VER )
977 QueryPerformanceCounter( (LARGE_INTEGER*) &end );
978#else
979 clock_t cend = clock();
980#endif
981
982 delete [] mem;
983
984 static const char* note =
985#ifdef DEBUG
986 "DEBUG";
987#else
988 "Release";
989#endif
990
991#if defined( _MSC_VER )
992 printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, 1000.0 * (double)(end-start) / ( (double)freq * (double)COUNT) );
993#else
994 printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, (double)(cend - cstart)/(double)COUNT );
995#endif
996 }
997
Lee Thomason (grinliz)7ca55582012-03-07 21:54:57 -0800998 #if defined( _MSC_VER ) && defined( DEBUG )
Lee Thomason1ff38e02012-02-14 18:18:16 -0800999 _CrtMemCheckpoint( &endMemState );
1000 //_CrtMemDumpStatistics( &endMemState );
1001
1002 _CrtMemState diffMemState;
1003 _CrtMemDifference( &diffMemState, &startMemState, &endMemState );
1004 _CrtMemDumpStatistics( &diffMemState );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001005 //printf( "new total=%d\n", gNewTotal );
Lee Thomason1ff38e02012-02-14 18:18:16 -08001006 #endif
1007
1008 printf ("\nPass %d, Fail %d\n", gPass, gFail);
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001009 return 0;
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -08001010}