blob: 18a049479b1ba7155fe9dcfaaa4c5e1f8ce3935d [file] [log] [blame]
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001#include "tinyxml2.h"
2
Arkadiy Shapkinef1c69c2012-07-25 22:10:39 +04003#include <direct.h>
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -07004#include <cstdlib>
5#include <cstring>
6#include <ctime>
U-Lama\Leee13c3e62011-12-28 14:36:55 -08007
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08008#if defined( _MSC_VER )
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;
14#endif
Lee Thomasone9ecdab2012-02-13 18:11:20 -080015
U-Lama\Leee13c3e62011-12-28 14:36:55 -080016using namespace tinyxml2;
Lee Thomasonec5a7b42012-02-13 18:16:52 -080017int gPass = 0;
18int gFail = 0;
19
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -080020
U-Stream\Lee09a11c52012-02-17 08:31:16 -080021bool XMLTest (const char* testString, const char* expected, const char* found, bool echo=true )
Lee Thomason1ff38e02012-02-14 18:18:16 -080022{
23 bool pass = !strcmp( expected, found );
24 if ( pass )
25 printf ("[pass]");
26 else
27 printf ("[fail]");
28
U-Stream\Lee09a11c52012-02-17 08:31:16 -080029 if ( !echo )
Lee Thomason1ff38e02012-02-14 18:18:16 -080030 printf (" %s\n", testString);
31 else
32 printf (" %s [%s][%s]\n", testString, expected, found);
33
34 if ( pass )
35 ++gPass;
36 else
37 ++gFail;
38 return pass;
39}
40
41
Lee Thomason21be8822012-07-15 17:27:22 -070042template< class T > bool XMLTest( const char* testString, T expected, T found, bool echo=true )
Lee Thomason1ff38e02012-02-14 18:18:16 -080043{
44 bool pass = ( expected == found );
45 if ( pass )
46 printf ("[pass]");
47 else
48 printf ("[fail]");
49
U-Stream\Lee09a11c52012-02-17 08:31:16 -080050 if ( !echo )
Lee Thomason1ff38e02012-02-14 18:18:16 -080051 printf (" %s\n", testString);
52 else
Lee Thomasonc8312792012-07-16 12:44:41 -070053 printf (" %s [%d][%d]\n", testString, static_cast<int>(expected), static_cast<int>(found) );
Lee Thomason1ff38e02012-02-14 18:18:16 -080054
55 if ( pass )
56 ++gPass;
57 else
58 ++gFail;
59 return pass;
60}
Lee Thomasonec5a7b42012-02-13 18:16:52 -080061
U-Lama\Leee13c3e62011-12-28 14:36:55 -080062
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -080063void NullLineEndings( char* p )
64{
65 while( p && *p ) {
66 if ( *p == '\n' || *p == '\r' ) {
67 *p = 0;
68 return;
69 }
70 ++p;
71 }
72}
73
74
Lee Thomason (grinliz)6a22be22012-04-04 12:39:05 -070075// Comments in the header. (Don't know how to get Doxygen to read comments in this file.)
76int example_1()
77{
78 XMLDocument doc;
Bruno Diasa2d4e6e2012-05-07 04:58:11 -030079 doc.LoadFile( "resources/dream.xml" );
Lee Thomason (grinliz)6a22be22012-04-04 12:39:05 -070080
81 return doc.ErrorID();
82}
83
84
85// Comments in the header. (Don't know how to get Doxygen to read comments in this file.)
86int example_2()
87{
88 static const char* xml = "<element/>";
89 XMLDocument doc;
90 doc.Parse( xml );
91
92 return doc.ErrorID();
93}
94
95
96int example_3()
97{
Lee Thomason (grinliz)a4a36ba2012-04-06 21:24:29 -070098 static const char* xml =
99 "<?xml version=\"1.0\"?>"
100 "<!DOCTYPE PLAY SYSTEM \"play.dtd\">"
101 "<PLAY>"
102 "<TITLE>A Midsummer Night's Dream</TITLE>"
103 "</PLAY>";
Lee Thomason (grinliz)6a22be22012-04-04 12:39:05 -0700104
105 XMLDocument doc;
106 doc.Parse( xml );
107
108 XMLElement* titleElement = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" );
109 const char* title = titleElement->GetText();
110 printf( "Name of play (1): %s\n", title );
111
112 XMLText* textNode = titleElement->FirstChild()->ToText();
113 title = textNode->Value();
114 printf( "Name of play (2): %s\n", title );
115
116 return doc.ErrorID();
117}
118
119
Lee Thomason21be8822012-07-15 17:27:22 -0700120bool example_4()
121{
122 static const char* xml =
123 "<information>"
124 " <attributeApproach v='2' />"
125 " <textApproach>"
126 " <v>2</v>"
127 " </textApproach>"
128 "</information>";
129
130 XMLDocument doc;
131 doc.Parse( xml );
132
133 int v0 = 0;
134 int v1 = 0;
135
136 XMLElement* attributeApproachElement = doc.FirstChildElement()->FirstChildElement( "attributeApproach" );
137 attributeApproachElement->QueryIntAttribute( "v", &v0 );
138
139 XMLElement* textApproachElement = doc.FirstChildElement()->FirstChildElement( "textApproach" );
140 textApproachElement->FirstChildElement( "v" )->QueryIntText( &v1 );
141
142 printf( "Both values are the same: %d and %d\n", v0, v1 );
143
144 return !doc.Error() && ( v0 == v1 );
145}
146
147
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700148int main( int /*argc*/, const char ** /*argv*/ )
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800149{
Lee Thomason (grinliz)0a4df402012-02-27 20:50:52 -0800150 #if defined( _MSC_VER ) && defined( DEBUG )
Lee Thomason1ff38e02012-02-14 18:18:16 -0800151 _CrtMemCheckpoint( &startMemState );
152 #endif
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800153
Lee Thomason7f7b1622012-03-24 12:49:03 -0700154 #if defined(_MSC_VER)
155 #pragma warning ( push )
156 #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
157 #endif
158
Arkadiy Shapkinef1c69c2012-07-25 22:10:39 +0400159 mkdir( "resources/out/" );
Arkadiy Shapkinff72d1f2012-07-24 00:24:07 +0400160
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300161 FILE* fp = fopen( "resources/dream.xml", "r" );
Lee Thomason7f7b1622012-03-24 12:49:03 -0700162 if ( !fp ) {
163 printf( "Error opening test file 'dream.xml'.\n"
164 "Is your working directory the same as where \n"
165 "the xmltest.cpp and dream.xml file are?\n\n"
166 #if defined( _MSC_VER )
167 "In windows Visual Studio you may need to set\n"
168 "Properties->Debugging->Working Directory to '..'\n"
169 #endif
170 );
171 exit( 1 );
172 }
173 fclose( fp );
174
175 #if defined(_MSC_VER)
176 #pragma warning ( pop )
177 #endif
178
Lee Thomason (grinliz)6a22be22012-04-04 12:39:05 -0700179 XMLTest( "Example-1", 0, example_1() );
180 XMLTest( "Example-2", 0, example_2() );
181 XMLTest( "Example-3", 0, example_3() );
Lee Thomason21be8822012-07-15 17:27:22 -0700182 XMLTest( "Example-4", true, example_4() );
Lee Thomason87e475a2012-03-20 11:55:29 -0700183
Lee Thomason (grinliz)6a22be22012-04-04 12:39:05 -0700184 /* ------ Example 2: Lookup information. ---- */
Lee Thomason87e475a2012-03-20 11:55:29 -0700185
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800186 {
Lee Thomason43f59302012-02-06 18:18:11 -0800187 static const char* test[] = { "<element />",
Arkadiy Shapkinef1c69c2012-07-25 22:10:39 +0400188 "<element></element>",
Lee Thomason43f59302012-02-06 18:18:11 -0800189 "<element><subelement/></element>",
Arkadiy Shapkinef1c69c2012-07-25 22:10:39 +0400190 "<element><subelement></subelement></element>",
191 "<element><subelement><subsub/></subelement></element>",
192 "<!--comment beside elements--><element><subelement></subelement></element>",
193 "<!--comment beside elements, this time with spaces--> \n <element> <subelement> \n </subelement> </element>",
194 "<element attrib1='foo' attrib2=\"bar\" ></element>",
195 "<element attrib1='foo' attrib2=\"bar\" ><subelement attrib3='yeehaa' /></element>",
Lee Thomason43f59302012-02-06 18:18:11 -0800196 "<element>Text inside element.</element>",
197 "<element><b></b></element>",
198 "<element>Text inside and <b>bolded</b> in the element.</element>",
199 "<outer><element>Text inside and <b>bolded</b> in the element.</element></outer>",
Lee Thomason8ee79892012-01-25 17:44:30 -0800200 "<element>This &amp; That.</element>",
Lee Thomason18d68bd2012-01-26 18:17:26 -0800201 "<element attrib='This&lt;That' />",
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800202 0
203 };
Lee Thomason6ee99fc2012-01-21 18:45:16 -0800204 for( int i=0; test[i]; ++i ) {
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800205 XMLDocument doc;
Lee Thomason6ee99fc2012-01-21 18:45:16 -0800206 doc.Parse( test[i] );
Lee Thomason5cae8972012-01-24 18:03:07 -0800207 doc.Print();
Lee Thomasonec975ce2012-01-23 11:42:06 -0800208 printf( "----------------------------------------------\n" );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800209 }
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800210 }
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800211#if 1
Lee Thomasond6277762012-02-22 16:00:12 -0800212 {
213 static const char* test = "<!--hello world\n"
Arkadiy Shapkinef1c69c2012-07-25 22:10:39 +0400214 " line 2\r"
215 " line 3\r\n"
216 " line 4\n\r"
217 " line 5\r-->";
Lee Thomasond6277762012-02-22 16:00:12 -0800218
219 XMLDocument doc;
220 doc.Parse( test );
221 doc.Print();
222 }
223
Lee Thomason2c85a712012-01-31 08:24:24 -0800224 {
225 static const char* test = "<element>Text before.</element>";
226 XMLDocument doc;
227 doc.Parse( test );
228 XMLElement* root = doc.FirstChildElement();
229 XMLElement* newElement = doc.NewElement( "Subelement" );
230 root->InsertEndChild( newElement );
231 doc.Print();
232 }
Lee Thomasond1983222012-02-06 08:41:24 -0800233 {
234 XMLDocument* doc = new XMLDocument();
235 static const char* test = "<element><sub/></element>";
236 doc->Parse( test );
237 delete doc;
238 }
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800239 {
Lee Thomason1ff38e02012-02-14 18:18:16 -0800240 // Test: Programmatic DOM
Lee Thomasonec5a7b42012-02-13 18:16:52 -0800241 // Build:
242 // <element>
243 // <!--comment-->
244 // <sub attrib="1" />
245 // <sub attrib="2" />
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800246 // <sub attrib="3" >& Text!</sub>
Lee Thomasonec5a7b42012-02-13 18:16:52 -0800247 // <element>
248
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800249 XMLDocument* doc = new XMLDocument();
Lee Thomason1ff38e02012-02-14 18:18:16 -0800250 XMLNode* element = doc->InsertEndChild( doc->NewElement( "element" ) );
251
252 XMLElement* sub[3] = { doc->NewElement( "sub" ), doc->NewElement( "sub" ), doc->NewElement( "sub" ) };
253 for( int i=0; i<3; ++i ) {
254 sub[i]->SetAttribute( "attrib", i );
255 }
256 element->InsertEndChild( sub[2] );
257 XMLNode* comment = element->InsertFirstChild( doc->NewComment( "comment" ) );
258 element->InsertAfterChild( comment, sub[0] );
259 element->InsertAfterChild( sub[0], sub[1] );
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800260 sub[2]->InsertFirstChild( doc->NewText( "& Text!" ));
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800261 doc->Print();
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800262 XMLTest( "Programmatic DOM", "comment", doc->FirstChildElement( "element" )->FirstChild()->Value() );
263 XMLTest( "Programmatic DOM", "0", doc->FirstChildElement( "element" )->FirstChildElement()->Attribute( "attrib" ) );
264 XMLTest( "Programmatic DOM", 2, doc->FirstChildElement()->LastChildElement( "sub" )->IntAttribute( "attrib" ) );
265 XMLTest( "Programmatic DOM", "& Text!",
266 doc->FirstChildElement()->LastChildElement( "sub" )->FirstChild()->ToText()->Value() );
U-Stream\Leeae25a442012-02-17 17:48:16 -0800267
268 // And now deletion:
269 element->DeleteChild( sub[2] );
270 doc->DeleteNode( comment );
271
272 element->FirstChildElement()->SetAttribute( "attrib", true );
273 element->LastChildElement()->DeleteAttribute( "attrib" );
274
275 XMLTest( "Programmatic DOM", true, doc->FirstChildElement()->FirstChildElement()->BoolAttribute( "attrib" ) );
276 int value = 10;
277 int result = doc->FirstChildElement()->LastChildElement()->QueryIntAttribute( "attrib", &value );
Lee Thomason21be8822012-07-15 17:27:22 -0700278 XMLTest( "Programmatic DOM", result, (int)XML_NO_ATTRIBUTE );
U-Stream\Leeae25a442012-02-17 17:48:16 -0800279 XMLTest( "Programmatic DOM", value, 10 );
280
281 doc->Print();
282
Lee Thomason7b1b86a2012-06-04 17:01:38 -0700283 {
284 XMLPrinter streamer;
285 doc->Print( &streamer );
286 printf( "%s", streamer.CStr() );
287 }
288 {
289 XMLPrinter streamer( 0, true );
290 doc->Print( &streamer );
291 XMLTest( "Compact mode", "<element><sub attrib=\"1\"/><sub/></element>", streamer.CStr(), false );
292 }
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800293 delete doc;
294 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800295 {
296 // Test: Dream
297 // XML1 : 1,187,569 bytes in 31,209 allocations
298 // XML2 : 469,073 bytes in 323 allocations
299 //int newStart = gNew;
300 XMLDocument doc;
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300301 doc.LoadFile( "resources/dream.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800302
Arkadiy Shapkinff72d1f2012-07-24 00:24:07 +0400303 doc.SaveFile( "resources/out/dreamout.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800304 doc.PrintError();
305
306 XMLTest( "Dream", "xml version=\"1.0\"",
Arkadiy Shapkinef1c69c2012-07-25 22:10:39 +0400307 doc.FirstChild()->ToDeclaration()->Value() );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800308 XMLTest( "Dream", true, doc.FirstChild()->NextSibling()->ToUnknown() ? true : false );
309 XMLTest( "Dream", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
310 doc.FirstChild()->NextSibling()->ToUnknown()->Value() );
311 XMLTest( "Dream", "And Robin shall restore amends.",
Arkadiy Shapkinef1c69c2012-07-25 22:10:39 +0400312 doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800313 XMLTest( "Dream", "And Robin shall restore amends.",
Arkadiy Shapkinef1c69c2012-07-25 22:10:39 +0400314 doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800315
316 XMLDocument doc2;
Arkadiy Shapkinff72d1f2012-07-24 00:24:07 +0400317 doc2.LoadFile( "resources/out/dreamout.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800318 XMLTest( "Dream-out", "xml version=\"1.0\"",
Arkadiy Shapkinef1c69c2012-07-25 22:10:39 +0400319 doc2.FirstChild()->ToDeclaration()->Value() );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800320 XMLTest( "Dream-out", true, doc2.FirstChild()->NextSibling()->ToUnknown() ? true : false );
321 XMLTest( "Dream-out", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
322 doc2.FirstChild()->NextSibling()->ToUnknown()->Value() );
323 XMLTest( "Dream-out", "And Robin shall restore amends.",
Arkadiy Shapkinef1c69c2012-07-25 22:10:39 +0400324 doc2.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800325
326 //gNewTotal = gNew - newStart;
327 }
Lee Thomason6f381b72012-03-02 12:59:39 -0800328
329
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800330 {
331 const char* error = "<?xml version=\"1.0\" standalone=\"no\" ?>\n"
332 "<passages count=\"006\" formatversion=\"20020620\">\n"
333 " <wrong error>\n"
334 "</passages>";
335
336 XMLDocument doc;
337 doc.Parse( error );
Lee Thomason21be8822012-07-15 17:27:22 -0700338 XMLTest( "Bad XML", doc.ErrorID(), (int)XML_ERROR_PARSING_ATTRIBUTE );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800339 }
340
341 {
342 const char* str = "<doc attr0='1' attr1='2.0' attr2='foo' />";
343
344 XMLDocument doc;
345 doc.Parse( str );
346
347 XMLElement* ele = doc.FirstChildElement();
348
349 int iVal, result;
350 double dVal;
351
352 result = ele->QueryDoubleAttribute( "attr0", &dVal );
Lee Thomason21be8822012-07-15 17:27:22 -0700353 XMLTest( "Query attribute: int as double", result, (int)XML_NO_ERROR );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800354 XMLTest( "Query attribute: int as double", (int)dVal, 1 );
355 result = ele->QueryDoubleAttribute( "attr1", &dVal );
356 XMLTest( "Query attribute: double as double", (int)dVal, 2 );
357 result = ele->QueryIntAttribute( "attr1", &iVal );
Lee Thomason21be8822012-07-15 17:27:22 -0700358 XMLTest( "Query attribute: double as int", result, (int)XML_NO_ERROR );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800359 XMLTest( "Query attribute: double as int", iVal, 2 );
360 result = ele->QueryIntAttribute( "attr2", &iVal );
Lee Thomason21be8822012-07-15 17:27:22 -0700361 XMLTest( "Query attribute: not a number", result, (int)XML_WRONG_ATTRIBUTE_TYPE );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800362 result = ele->QueryIntAttribute( "bar", &iVal );
Lee Thomason21be8822012-07-15 17:27:22 -0700363 XMLTest( "Query attribute: does not exist", result, (int)XML_NO_ATTRIBUTE );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800364 }
365
366 {
367 const char* str = "<doc/>";
368
369 XMLDocument doc;
370 doc.Parse( str );
371
372 XMLElement* ele = doc.FirstChildElement();
373
374 int iVal;
375 double dVal;
376
377 ele->SetAttribute( "str", "strValue" );
378 ele->SetAttribute( "int", 1 );
379 ele->SetAttribute( "double", -1.0 );
380
381 const char* cStr = ele->Attribute( "str" );
382 ele->QueryIntAttribute( "int", &iVal );
383 ele->QueryDoubleAttribute( "double", &dVal );
384
Lee Thomason8ba7f7d2012-03-24 13:04:04 -0700385 XMLTest( "Attribute match test", ele->Attribute( "str", "strValue" ), "strValue" );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800386 XMLTest( "Attribute round trip. c-string.", "strValue", cStr );
387 XMLTest( "Attribute round trip. int.", 1, iVal );
388 XMLTest( "Attribute round trip. double.", -1, (int)dVal );
389 }
390
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800391 {
392 XMLDocument doc;
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300393 doc.LoadFile( "resources/utf8test.xml" );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800394
395 // Get the attribute "value" from the "Russian" element and check it.
396 XMLElement* element = doc.FirstChildElement( "document" )->FirstChildElement( "Russian" );
397 const unsigned char correctValue[] = { 0xd1U, 0x86U, 0xd0U, 0xb5U, 0xd0U, 0xbdU, 0xd0U, 0xbdU,
398 0xd0U, 0xbeU, 0xd1U, 0x81U, 0xd1U, 0x82U, 0xd1U, 0x8cU, 0 };
399
400 XMLTest( "UTF-8: Russian value.", (const char*)correctValue, element->Attribute( "value" ) );
401
402 const unsigned char russianElementName[] = { 0xd0U, 0xa0U, 0xd1U, 0x83U,
403 0xd1U, 0x81U, 0xd1U, 0x81U,
404 0xd0U, 0xbaU, 0xd0U, 0xb8U,
405 0xd0U, 0xb9U, 0 };
406 const char russianText[] = "<\xD0\xB8\xD0\xBC\xD0\xB5\xD0\xB5\xD1\x82>";
407
408 XMLText* text = doc.FirstChildElement( "document" )->FirstChildElement( (const char*) russianElementName )->FirstChild()->ToText();
409 XMLTest( "UTF-8: Browsing russian element name.",
410 russianText,
411 text->Value() );
412
413 // Now try for a round trip.
Arkadiy Shapkinff72d1f2012-07-24 00:24:07 +0400414 doc.SaveFile( "resources/out/utf8testout.xml" );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800415
416 // Check the round trip.
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800417 int okay = 0;
418
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800419
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800420#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800421#pragma warning ( push )
422#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800423#endif
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300424 FILE* saved = fopen( "resources/utf8testout.xml", "r" );
425 FILE* verify = fopen( "resources/utf8testverify.xml", "r" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800426#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800427#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800428#endif
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800429
430 if ( saved && verify )
431 {
432 okay = 1;
PKEuSc28ba3a2012-07-16 03:08:47 -0700433 char verifyBuf[256];
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800434 while ( fgets( verifyBuf, 256, verify ) )
435 {
PKEuSc28ba3a2012-07-16 03:08:47 -0700436 char savedBuf[256];
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800437 fgets( savedBuf, 256, saved );
438 NullLineEndings( verifyBuf );
439 NullLineEndings( savedBuf );
440
441 if ( strcmp( verifyBuf, savedBuf ) )
442 {
443 printf( "verify:%s<\n", verifyBuf );
444 printf( "saved :%s<\n", savedBuf );
445 okay = 0;
446 break;
447 }
448 }
449 }
450 if ( saved )
451 fclose( saved );
452 if ( verify )
453 fclose( verify );
454 XMLTest( "UTF-8: Verified multi-language round trip.", 1, okay );
455 }
456
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800457 // --------GetText()-----------
458 {
459 const char* str = "<foo>This is text</foo>";
460 XMLDocument doc;
461 doc.Parse( str );
462 const XMLElement* element = doc.RootElement();
463
464 XMLTest( "GetText() normal use.", "This is text", element->GetText() );
465
466 str = "<foo><b>This is text</b></foo>";
467 doc.Parse( str );
468 element = doc.RootElement();
469
470 XMLTest( "GetText() contained element.", element->GetText() == 0, true );
471 }
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800472
Lee Thomasond6277762012-02-22 16:00:12 -0800473
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800474 // ---------- CDATA ---------------
475 {
476 const char* str = "<xmlElement>"
477 "<![CDATA["
478 "I am > the rules!\n"
479 "...since I make symbolic puns"
480 "]]>"
481 "</xmlElement>";
482 XMLDocument doc;
483 doc.Parse( str );
484 doc.Print();
Lee Thomasond6277762012-02-22 16:00:12 -0800485
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800486 XMLTest( "CDATA parse.", doc.FirstChildElement()->FirstChild()->Value(),
487 "I am > the rules!\n...since I make symbolic puns",
Lee Thomasond6277762012-02-22 16:00:12 -0800488 false );
489 }
490
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800491 // ----------- CDATA -------------
492 {
493 const char* str = "<xmlElement>"
494 "<![CDATA["
495 "<b>I am > the rules!</b>\n"
496 "...since I make symbolic puns"
497 "]]>"
498 "</xmlElement>";
499 XMLDocument doc;
500 doc.Parse( str );
501 doc.Print();
502
503 XMLTest( "CDATA parse. [ tixml1:1480107 ]", doc.FirstChildElement()->FirstChild()->Value(),
504 "<b>I am > the rules!</b>\n...since I make symbolic puns",
505 false );
506 }
507
508 // InsertAfterChild causes crash.
509 {
510 // InsertBeforeChild and InsertAfterChild causes crash.
511 XMLDocument doc;
512 XMLElement* parent = doc.NewElement( "Parent" );
513 doc.InsertFirstChild( parent );
514
515 XMLElement* childText0 = doc.NewElement( "childText0" );
516 XMLElement* childText1 = doc.NewElement( "childText1" );
517
518 XMLNode* childNode0 = parent->InsertEndChild( childText0 );
519 XMLNode* childNode1 = parent->InsertAfterChild( childNode0, childText1 );
520
521 XMLTest( "Test InsertAfterChild on empty node. ", ( childNode1 == parent->LastChild() ), true );
522 }
Lee Thomasond6277762012-02-22 16:00:12 -0800523
524 {
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800525 // Entities not being written correctly.
526 // From Lynn Allen
Lee Thomasond6277762012-02-22 16:00:12 -0800527
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800528 const char* passages =
529 "<?xml version=\"1.0\" standalone=\"no\" ?>"
530 "<passages count=\"006\" formatversion=\"20020620\">"
531 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
532 " It also has &lt;, &gt;, and &amp;, as well as a fake copyright &#xA9;.\"> </psg>"
533 "</passages>";
Lee Thomasond6277762012-02-22 16:00:12 -0800534
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800535 XMLDocument doc;
536 doc.Parse( passages );
537 XMLElement* psg = doc.RootElement()->FirstChildElement();
538 const char* context = psg->Attribute( "context" );
539 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 -0800540
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800541 XMLTest( "Entity transformation: read. ", expected, context, true );
Lee Thomasond6277762012-02-22 16:00:12 -0800542
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800543#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800544#pragma warning ( push )
545#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800546#endif
Arkadiy Shapkinff72d1f2012-07-24 00:24:07 +0400547 FILE* textfile = fopen( "resources/out/textfile.txt", "w" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800548#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800549#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800550#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800551 if ( textfile )
552 {
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800553 XMLPrinter streamer( textfile );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800554 psg->Accept( &streamer );
555 fclose( textfile );
556 }
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800557#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800558#pragma warning ( push )
559#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800560#endif
Arkadiy Shapkinff72d1f2012-07-24 00:24:07 +0400561 textfile = fopen( "resources/out/textfile.txt", "r" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800562#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800563#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800564#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800565 TIXMLASSERT( textfile );
566 if ( textfile )
567 {
568 char buf[ 1024 ];
569 fgets( buf, 1024, textfile );
570 XMLTest( "Entity transformation: write. ",
571 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
572 " It also has &lt;, &gt;, and &amp;, as well as a fake copyright \xC2\xA9.\"/>\n",
573 buf, false );
PKEuSc28ba3a2012-07-16 03:08:47 -0700574 fclose( textfile );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800575 }
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800576 }
577
578 {
Lee Thomason6f381b72012-03-02 12:59:39 -0800579 // Suppress entities.
580 const char* passages =
581 "<?xml version=\"1.0\" standalone=\"no\" ?>"
582 "<passages count=\"006\" formatversion=\"20020620\">"
583 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;.\">Crazy &ttk;</psg>"
584 "</passages>";
585
586 XMLDocument doc( false );
587 doc.Parse( passages );
588
589 XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->Attribute( "context" ),
590 "Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;." );
591 XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->FirstChild()->Value(),
592 "Crazy &ttk;" );
593 doc.Print();
594 }
595
596 {
Arkadiy Shapkinef1c69c2012-07-25 22:10:39 +0400597 const char* test = "<?xml version='1.0'?><a.elem xmi.version='2.0'/>";
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800598
599 XMLDocument doc;
Arkadiy Shapkinef1c69c2012-07-25 22:10:39 +0400600 doc.Parse( test );
601 XMLTest( "dot in names", doc.Error(), false );
602 XMLTest( "dot in names", doc.FirstChildElement()->Name(), "a.elem" );
603 XMLTest( "dot in names", doc.FirstChildElement()->Attribute( "xmi.version" ), "2.0" );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800604 }
605
606 {
Arkadiy Shapkinef1c69c2012-07-25 22:10:39 +0400607 const char* test = "<element><Name>1.1 Start easy ignore fin thickness&#xA;</Name></element>";
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800608
Arkadiy Shapkinef1c69c2012-07-25 22:10:39 +0400609 XMLDocument doc;
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800610 doc.Parse( test );
611
612 XMLText* text = doc.FirstChildElement()->FirstChildElement()->FirstChild()->ToText();
613 XMLTest( "Entity with one digit.",
614 text->Value(), "1.1 Start easy ignore fin thickness\n",
615 false );
Arkadiy Shapkinef1c69c2012-07-25 22:10:39 +0400616 }
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800617
618 {
619 // DOCTYPE not preserved (950171)
620 //
621 const char* doctype =
622 "<?xml version=\"1.0\" ?>"
623 "<!DOCTYPE PLAY SYSTEM 'play.dtd'>"
624 "<!ELEMENT title (#PCDATA)>"
625 "<!ELEMENT books (title,authors)>"
626 "<element />";
627
628 XMLDocument doc;
629 doc.Parse( doctype );
Arkadiy Shapkinff72d1f2012-07-24 00:24:07 +0400630 doc.SaveFile( "resources/out/test7.xml" );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800631 doc.DeleteChild( doc.RootElement() );
Arkadiy Shapkinff72d1f2012-07-24 00:24:07 +0400632 doc.LoadFile( "resources/out/test7.xml" );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800633 doc.Print();
634
635 const XMLUnknown* decl = doc.FirstChild()->NextSibling()->ToUnknown();
636 XMLTest( "Correct value of unknown.", "DOCTYPE PLAY SYSTEM 'play.dtd'", decl->Value() );
637
638 }
639
640 {
641 // Comments do not stream out correctly.
642 const char* doctype =
643 "<!-- Somewhat<evil> -->";
644 XMLDocument doc;
645 doc.Parse( doctype );
646
647 XMLComment* comment = doc.FirstChild()->ToComment();
648
649 XMLTest( "Comment formatting.", " Somewhat<evil> ", comment->Value() );
650 }
651 {
652 // Double attributes
653 const char* doctype = "<element attr='red' attr='blue' />";
654
655 XMLDocument doc;
656 doc.Parse( doctype );
657
Lee Thomason21be8822012-07-15 17:27:22 -0700658 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 -0800659 doc.PrintError();
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800660 }
661
662 {
663 // Embedded null in stream.
664 const char* doctype = "<element att\0r='red' attr='blue' />";
665
666 XMLDocument doc;
667 doc.Parse( doctype );
668 XMLTest( "Embedded null throws error.", true, doc.Error() );
669 }
670
671 {
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700672 // Empty documents should return TIXML_XML_ERROR_PARSING_EMPTY, bug 1070717
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800673 const char* str = " ";
674 XMLDocument doc;
675 doc.Parse( str );
Lee Thomason21be8822012-07-15 17:27:22 -0700676 XMLTest( "Empty document error", (int)XML_ERROR_EMPTY_DOCUMENT, doc.ErrorID() );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800677 }
678
679 {
680 // Low entities
681 XMLDocument doc;
682 doc.Parse( "<test>&#x0e;</test>" );
683 const char result[] = { 0x0e, 0 };
684 XMLTest( "Low entities.", doc.FirstChildElement()->GetText(), result );
685 doc.Print();
686 }
687
688 {
689 // Attribute values with trailing quotes not handled correctly
690 XMLDocument doc;
691 doc.Parse( "<foo attribute=bar\" />" );
692 XMLTest( "Throw error with bad end quotes.", doc.Error(), true );
693 }
694
695 {
696 // [ 1663758 ] Failure to report error on bad XML
697 XMLDocument xml;
698 xml.Parse("<x>");
699 XMLTest("Missing end tag at end of input", xml.Error(), true);
700 xml.Parse("<x> ");
701 XMLTest("Missing end tag with trailing whitespace", xml.Error(), true);
702 xml.Parse("<x></y>");
Lee Thomason21be8822012-07-15 17:27:22 -0700703 XMLTest("Mismatched tags", xml.ErrorID(), (int)XML_ERROR_MISMATCHED_ELEMENT);
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800704 }
705
706
707 {
708 // [ 1475201 ] TinyXML parses entities in comments
709 XMLDocument xml;
710 xml.Parse("<!-- declarations for <head> & <body> -->"
711 "<!-- far &amp; away -->" );
712
713 XMLNode* e0 = xml.FirstChild();
714 XMLNode* e1 = e0->NextSibling();
715 XMLComment* c0 = e0->ToComment();
716 XMLComment* c1 = e1->ToComment();
717
718 XMLTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
719 XMLTest( "Comments ignore entities.", " far &amp; away ", c1->Value(), true );
720 }
721
722 {
723 XMLDocument xml;
724 xml.Parse( "<Parent>"
725 "<child1 att=''/>"
726 "<!-- With this comment, child2 will not be parsed! -->"
727 "<child2 att=''/>"
728 "</Parent>" );
729 xml.Print();
730
731 int count = 0;
732
733 for( XMLNode* ele = xml.FirstChildElement( "Parent" )->FirstChild();
734 ele;
735 ele = ele->NextSibling() )
736 {
737 ++count;
738 }
739
740 XMLTest( "Comments iterate correctly.", 3, count );
741 }
742
743 {
744 // trying to repro ]1874301]. If it doesn't go into an infinite loop, all is well.
745 unsigned char buf[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?><feed><![CDATA[Test XMLblablablalblbl";
746 buf[60] = 239;
747 buf[61] = 0;
748
749 XMLDocument doc;
750 doc.Parse( (const char*)buf);
751 }
752
753
754 {
755 // bug 1827248 Error while parsing a little bit malformed file
756 // Actually not malformed - should work.
757 XMLDocument xml;
758 xml.Parse( "<attributelist> </attributelist >" );
759 XMLTest( "Handle end tag whitespace", false, xml.Error() );
760 }
761
762 {
763 // This one must not result in an infinite loop
764 XMLDocument xml;
765 xml.Parse( "<infinite>loop" );
766 XMLTest( "Infinite loop test.", true, true );
767 }
768#endif
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800769 {
770 const char* pub = "<?xml version='1.0'?> <element><sub/></element> <!--comment--> <!DOCTYPE>";
771 XMLDocument doc;
772 doc.Parse( pub );
773
774 XMLDocument clone;
775 for( const XMLNode* node=doc.FirstChild(); node; node=node->NextSibling() ) {
776 XMLNode* copy = node->ShallowClone( &clone );
777 clone.InsertEndChild( copy );
778 }
779
780 clone.Print();
781
782 int count=0;
783 const XMLNode* a=clone.FirstChild();
784 const XMLNode* b=doc.FirstChild();
785 for( ; a && b; a=a->NextSibling(), b=b->NextSibling() ) {
786 ++count;
787 XMLTest( "Clone and Equal", true, a->ShallowEqual( b ));
788 }
789 XMLTest( "Clone and Equal", 4, count );
790 }
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800791
Lee Thomason (grinliz)a4a36ba2012-04-06 21:24:29 -0700792 {
793 // This shouldn't crash.
794 XMLDocument doc;
795 if(XML_NO_ERROR != doc.LoadFile( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ))
796 {
797 doc.PrintError();
798 }
799 XMLTest( "Error in snprinf handling.", true, doc.Error() );
800 }
Lee Thomason5e3803c2012-04-16 08:57:05 -0700801
802 {
803 // Attribute ordering.
804 static const char* xml = "<element attrib1=\"1\" attrib2=\"2\" attrib3=\"3\" />";
805 XMLDocument doc;
806 doc.Parse( xml );
807 XMLElement* ele = doc.FirstChildElement();
808
809 const XMLAttribute* a = ele->FirstAttribute();
810 XMLTest( "Attribute order", "1", a->Value() );
811 a = a->Next();
812 XMLTest( "Attribute order", "2", a->Value() );
813 a = a->Next();
814 XMLTest( "Attribute order", "3", a->Value() );
815 XMLTest( "Attribute order", "attrib3", a->Name() );
816
817 ele->DeleteAttribute( "attrib2" );
818 a = ele->FirstAttribute();
819 XMLTest( "Attribute order", "1", a->Value() );
820 a = a->Next();
821 XMLTest( "Attribute order", "3", a->Value() );
822
823 ele->DeleteAttribute( "attrib1" );
824 ele->DeleteAttribute( "attrib3" );
825 XMLTest( "Attribute order (empty)", false, ele->FirstAttribute() ? true : false );
826 }
Lee Thomason (grinliz)a4a36ba2012-04-06 21:24:29 -0700827
Lee Thomason (grinliz)390e9782012-07-01 21:22:53 -0700828 {
829 // Make sure an attribute with a space in it succeeds.
Lee Thomason78a773d2012-07-02 10:10:19 -0700830 static const char* xml0 = "<element attribute1= \"Test Attribute\"/>";
831 static const char* xml1 = "<element attribute1 =\"Test Attribute\"/>";
832 static const char* xml2 = "<element attribute1 = \"Test Attribute\"/>";
833 XMLDocument doc0;
834 doc0.Parse( xml0 );
835 XMLDocument doc1;
836 doc1.Parse( xml1 );
837 XMLDocument doc2;
838 doc2.Parse( xml2 );
Lee Thomason (grinliz)390e9782012-07-01 21:22:53 -0700839
Lee Thomason78a773d2012-07-02 10:10:19 -0700840 XMLElement* ele = 0;
841 ele = doc0.FirstChildElement();
842 XMLTest( "Attribute with space #1", "Test Attribute", ele->Attribute( "attribute1" ) );
843 ele = doc1.FirstChildElement();
844 XMLTest( "Attribute with space #2", "Test Attribute", ele->Attribute( "attribute1" ) );
845 ele = doc2.FirstChildElement();
846 XMLTest( "Attribute with space #3", "Test Attribute", ele->Attribute( "attribute1" ) );
Lee Thomason (grinliz)390e9782012-07-01 21:22:53 -0700847 }
848
849 {
850 // Make sure we don't go into an infinite loop.
851 static const char* xml = "<doc><element attribute='attribute'/><element attribute='attribute'/></doc>";
852 XMLDocument doc;
853 doc.Parse( xml );
854 XMLElement* ele0 = doc.FirstChildElement()->FirstChildElement();
855 XMLElement* ele1 = ele0->NextSiblingElement();
856 bool equal = ele0->ShallowEqual( ele1 );
857
858 XMLTest( "Infinite loop in shallow equal.", true, equal );
859 }
860
Lee Thomason5708f812012-03-28 17:46:41 -0700861 // -------- Handles ------------
862 {
863 static const char* xml = "<element attrib='bar'><sub>Text</sub></element>";
864 XMLDocument doc;
865 doc.Parse( xml );
Lee Thomason5708f812012-03-28 17:46:41 -0700866
867 XMLElement* ele = XMLHandle( doc ).FirstChildElement( "element" ).FirstChild().ToElement();
868 XMLTest( "Handle, success, mutable", ele->Value(), "sub" );
869
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -0700870 XMLHandle docH( doc );
871 ele = docH.FirstChildElement( "none" ).FirstChildElement( "element" ).ToElement();
Lee Thomasond0b19df2012-04-12 08:41:37 -0700872 XMLTest( "Handle, dne, mutable", false, ele != 0 );
Lee Thomason5708f812012-03-28 17:46:41 -0700873 }
874
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -0700875 {
876 static const char* xml = "<element attrib='bar'><sub>Text</sub></element>";
877 XMLDocument doc;
878 doc.Parse( xml );
879 XMLConstHandle docH( doc );
880
881 const XMLElement* ele = docH.FirstChildElement( "element" ).FirstChild().ToElement();
882 XMLTest( "Handle, success, const", ele->Value(), "sub" );
883
884 ele = docH.FirstChildElement( "none" ).FirstChildElement( "element" ).ToElement();
Lee Thomasond0b19df2012-04-12 08:41:37 -0700885 XMLTest( "Handle, dne, const", false, ele != 0 );
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -0700886 }
Lee Thomasonf68c4382012-04-28 14:37:11 -0700887 {
888 // Default Declaration & BOM
889 XMLDocument doc;
890 doc.InsertEndChild( doc.NewDeclaration() );
891 doc.SetBOM( true );
892
893 XMLPrinter printer;
894 doc.Print( &printer );
895
896 static const char* result = "\xef\xbb\xbf<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
897 XMLTest( "BOM and default declaration", printer.CStr(), result, false );
Lee Thomason (grinliz)48ea0bc2012-05-26 14:41:14 -0700898 XMLTest( "CStrSize", printer.CStrSize(), 42, false );
Lee Thomasonf68c4382012-04-28 14:37:11 -0700899 }
Lee Thomason21be8822012-07-15 17:27:22 -0700900 {
901 const char* xml = "<ipxml ws='1'><info bla=' /></ipxml>";
902 XMLDocument doc;
903 doc.Parse( xml );
904 XMLTest( "Ill formed XML", true, doc.Error() );
905 }
906
907 // QueryXYZText
908 {
909 const char* xml = "<point> <x>1.2</x> <y>1</y> <z>38</z> <valid>true</valid> </point>";
910 XMLDocument doc;
911 doc.Parse( xml );
912
913 const XMLElement* pointElement = doc.RootElement();
914
915 int intValue = 0;
916 unsigned unsignedValue = 0;
917 float floatValue = 0;
918 double doubleValue = 0;
919 bool boolValue = false;
920
921 pointElement->FirstChildElement( "y" )->QueryIntText( &intValue );
922 pointElement->FirstChildElement( "y" )->QueryUnsignedText( &unsignedValue );
923 pointElement->FirstChildElement( "x" )->QueryFloatText( &floatValue );
924 pointElement->FirstChildElement( "x" )->QueryDoubleText( &doubleValue );
925 pointElement->FirstChildElement( "valid" )->QueryBoolText( &boolValue );
926
927
928 XMLTest( "QueryIntText", intValue, 1, false );
929 XMLTest( "QueryUnsignedText", unsignedValue, (unsigned)1, false );
930 XMLTest( "QueryFloatText", floatValue, 1.2f, false );
931 XMLTest( "QueryDoubleText", doubleValue, 1.2, false );
932 XMLTest( "QueryBoolText", boolValue, true, false );
933 }
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -0700934
935
Lee Thomason6f381b72012-03-02 12:59:39 -0800936 // ----------- Performance tracking --------------
937 {
938#if defined( _MSC_VER )
939 __int64 start, end, freq;
940 QueryPerformanceFrequency( (LARGE_INTEGER*) &freq );
941#endif
942
943#if defined(_MSC_VER)
944#pragma warning ( push )
945#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
946#endif
Bruno Diasa2d4e6e2012-05-07 04:58:11 -0300947 FILE* fp = fopen( "resources/dream.xml", "r" );
Lee Thomason6f381b72012-03-02 12:59:39 -0800948#if defined(_MSC_VER)
949#pragma warning ( pop )
950#endif
951 fseek( fp, 0, SEEK_END );
952 long size = ftell( fp );
953 fseek( fp, 0, SEEK_SET );
954
955 char* mem = new char[size+1];
956 fread( mem, size, 1, fp );
957 fclose( fp );
958 mem[size] = 0;
959
960#if defined( _MSC_VER )
961 QueryPerformanceCounter( (LARGE_INTEGER*) &start );
962#else
963 clock_t cstart = clock();
964#endif
965 static const int COUNT = 10;
966 for( int i=0; i<COUNT; ++i ) {
967 XMLDocument doc;
968 doc.Parse( mem );
969 }
970#if defined( _MSC_VER )
971 QueryPerformanceCounter( (LARGE_INTEGER*) &end );
972#else
973 clock_t cend = clock();
974#endif
975
976 delete [] mem;
977
978 static const char* note =
979#ifdef DEBUG
980 "DEBUG";
981#else
982 "Release";
983#endif
984
985#if defined( _MSC_VER )
986 printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, 1000.0 * (double)(end-start) / ( (double)freq * (double)COUNT) );
987#else
988 printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, (double)(cend - cstart)/(double)COUNT );
989#endif
990 }
991
Lee Thomason (grinliz)7ca55582012-03-07 21:54:57 -0800992 #if defined( _MSC_VER ) && defined( DEBUG )
Lee Thomason1ff38e02012-02-14 18:18:16 -0800993 _CrtMemCheckpoint( &endMemState );
994 //_CrtMemDumpStatistics( &endMemState );
995
996 _CrtMemState diffMemState;
997 _CrtMemDifference( &diffMemState, &startMemState, &endMemState );
998 _CrtMemDumpStatistics( &diffMemState );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800999 //printf( "new total=%d\n", gNewTotal );
Lee Thomason1ff38e02012-02-14 18:18:16 -08001000 #endif
1001
1002 printf ("\nPass %d, Fail %d\n", gPass, gFail);
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001003 return 0;
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -08001004}