blob: ac5931b5ed4c2290a69e79bfc2a0ddcbcc00bd73 [file] [log] [blame]
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001#include "tinyxml2.h"
2
3#include <stdio.h>
4#include <stdlib.h>
Lee Thomasonec5a7b42012-02-13 18:16:52 -08005#include <string.h>
Lee Thomason6f381b72012-03-02 12:59:39 -08006#include <time.h>
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
U-Stream\Lee09a11c52012-02-17 08:31:16 -080042bool XMLTest( const char* testString, int expected, int 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
53 printf (" %s [%d][%d]\n", testString, expected, found);
54
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)9b093cc2012-02-25 21:30:18 -080075int main( int /*argc*/, const char* /*argv*/ )
U-Lama\Leee13c3e62011-12-28 14:36:55 -080076{
Lee Thomason (grinliz)0a4df402012-02-27 20:50:52 -080077 #if defined( _MSC_VER ) && defined( DEBUG )
Lee Thomason1ff38e02012-02-14 18:18:16 -080078 _CrtMemCheckpoint( &startMemState );
79 #endif
Lee Thomason8a5dfee2012-01-18 17:43:40 -080080
Lee Thomason8a5dfee2012-01-18 17:43:40 -080081 {
Lee Thomason43f59302012-02-06 18:18:11 -080082 static const char* test[] = { "<element />",
83 "<element></element>",
84 "<element><subelement/></element>",
85 "<element><subelement></subelement></element>",
86 "<element><subelement><subsub/></subelement></element>",
87 "<!--comment beside elements--><element><subelement></subelement></element>",
88 "<!--comment beside elements, this time with spaces--> \n <element> <subelement> \n </subelement> </element>",
89 "<element attrib1='foo' attrib2=\"bar\" ></element>",
90 "<element attrib1='foo' attrib2=\"bar\" ><subelement attrib3='yeehaa' /></element>",
91 "<element>Text inside element.</element>",
92 "<element><b></b></element>",
93 "<element>Text inside and <b>bolded</b> in the element.</element>",
94 "<outer><element>Text inside and <b>bolded</b> in the element.</element></outer>",
Lee Thomason8ee79892012-01-25 17:44:30 -080095 "<element>This &amp; That.</element>",
Lee Thomason18d68bd2012-01-26 18:17:26 -080096 "<element attrib='This&lt;That' />",
Lee Thomasondadcdfa2012-01-18 17:55:48 -080097 0
98 };
Lee Thomason6ee99fc2012-01-21 18:45:16 -080099 for( int i=0; test[i]; ++i ) {
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800100 XMLDocument doc;
Lee Thomason6ee99fc2012-01-21 18:45:16 -0800101 doc.Parse( test[i] );
Lee Thomason5cae8972012-01-24 18:03:07 -0800102 doc.Print();
Lee Thomasonec975ce2012-01-23 11:42:06 -0800103 printf( "----------------------------------------------\n" );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800104 }
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800105 }
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800106#if 1
Lee Thomasond6277762012-02-22 16:00:12 -0800107 {
108 static const char* test = "<!--hello world\n"
109 " line 2\r"
110 " line 3\r\n"
111 " line 4\n\r"
112 " line 5\r-->";
113
114 XMLDocument doc;
115 doc.Parse( test );
116 doc.Print();
117 }
118
Lee Thomason2c85a712012-01-31 08:24:24 -0800119 {
120 static const char* test = "<element>Text before.</element>";
121 XMLDocument doc;
122 doc.Parse( test );
123 XMLElement* root = doc.FirstChildElement();
124 XMLElement* newElement = doc.NewElement( "Subelement" );
125 root->InsertEndChild( newElement );
126 doc.Print();
127 }
Lee Thomasond1983222012-02-06 08:41:24 -0800128 {
129 XMLDocument* doc = new XMLDocument();
130 static const char* test = "<element><sub/></element>";
131 doc->Parse( test );
132 delete doc;
133 }
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800134 {
Lee Thomason1ff38e02012-02-14 18:18:16 -0800135 // Test: Programmatic DOM
Lee Thomasonec5a7b42012-02-13 18:16:52 -0800136 // Build:
137 // <element>
138 // <!--comment-->
139 // <sub attrib="1" />
140 // <sub attrib="2" />
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800141 // <sub attrib="3" >& Text!</sub>
Lee Thomasonec5a7b42012-02-13 18:16:52 -0800142 // <element>
143
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800144 XMLDocument* doc = new XMLDocument();
Lee Thomason1ff38e02012-02-14 18:18:16 -0800145 XMLNode* element = doc->InsertEndChild( doc->NewElement( "element" ) );
146
147 XMLElement* sub[3] = { doc->NewElement( "sub" ), doc->NewElement( "sub" ), doc->NewElement( "sub" ) };
148 for( int i=0; i<3; ++i ) {
149 sub[i]->SetAttribute( "attrib", i );
150 }
151 element->InsertEndChild( sub[2] );
152 XMLNode* comment = element->InsertFirstChild( doc->NewComment( "comment" ) );
153 element->InsertAfterChild( comment, sub[0] );
154 element->InsertAfterChild( sub[0], sub[1] );
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800155 sub[2]->InsertFirstChild( doc->NewText( "& Text!" ));
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800156 doc->Print();
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800157 XMLTest( "Programmatic DOM", "comment", doc->FirstChildElement( "element" )->FirstChild()->Value() );
158 XMLTest( "Programmatic DOM", "0", doc->FirstChildElement( "element" )->FirstChildElement()->Attribute( "attrib" ) );
159 XMLTest( "Programmatic DOM", 2, doc->FirstChildElement()->LastChildElement( "sub" )->IntAttribute( "attrib" ) );
160 XMLTest( "Programmatic DOM", "& Text!",
161 doc->FirstChildElement()->LastChildElement( "sub" )->FirstChild()->ToText()->Value() );
U-Stream\Leeae25a442012-02-17 17:48:16 -0800162
163 // And now deletion:
164 element->DeleteChild( sub[2] );
165 doc->DeleteNode( comment );
166
167 element->FirstChildElement()->SetAttribute( "attrib", true );
168 element->LastChildElement()->DeleteAttribute( "attrib" );
169
170 XMLTest( "Programmatic DOM", true, doc->FirstChildElement()->FirstChildElement()->BoolAttribute( "attrib" ) );
171 int value = 10;
172 int result = doc->FirstChildElement()->LastChildElement()->QueryIntAttribute( "attrib", &value );
173 XMLTest( "Programmatic DOM", result, NO_ATTRIBUTE );
174 XMLTest( "Programmatic DOM", value, 10 );
175
176 doc->Print();
177
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800178 XMLPrinter streamer;
U-Stream\Leeae25a442012-02-17 17:48:16 -0800179 doc->Print( &streamer );
180 printf( "%s", streamer.CStr() );
181
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800182 delete doc;
183 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800184 {
185 // Test: Dream
186 // XML1 : 1,187,569 bytes in 31,209 allocations
187 // XML2 : 469,073 bytes in 323 allocations
188 //int newStart = gNew;
189 XMLDocument doc;
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800190 doc.LoadFile( "dream.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800191
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800192 doc.SaveFile( "dreamout.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800193 doc.PrintError();
194
195 XMLTest( "Dream", "xml version=\"1.0\"",
196 doc.FirstChild()->ToDeclaration()->Value() );
197 XMLTest( "Dream", true, doc.FirstChild()->NextSibling()->ToUnknown() ? true : false );
198 XMLTest( "Dream", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
199 doc.FirstChild()->NextSibling()->ToUnknown()->Value() );
200 XMLTest( "Dream", "And Robin shall restore amends.",
201 doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
202 XMLTest( "Dream", "And Robin shall restore amends.",
203 doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
204
205 XMLDocument doc2;
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800206 doc2.LoadFile( "dreamout.xml" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800207 XMLTest( "Dream-out", "xml version=\"1.0\"",
208 doc2.FirstChild()->ToDeclaration()->Value() );
209 XMLTest( "Dream-out", true, doc2.FirstChild()->NextSibling()->ToUnknown() ? true : false );
210 XMLTest( "Dream-out", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
211 doc2.FirstChild()->NextSibling()->ToUnknown()->Value() );
212 XMLTest( "Dream-out", "And Robin shall restore amends.",
213 doc2.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
214
215 //gNewTotal = gNew - newStart;
216 }
Lee Thomason6f381b72012-03-02 12:59:39 -0800217
218
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800219 {
220 const char* error = "<?xml version=\"1.0\" standalone=\"no\" ?>\n"
221 "<passages count=\"006\" formatversion=\"20020620\">\n"
222 " <wrong error>\n"
223 "</passages>";
224
225 XMLDocument doc;
226 doc.Parse( error );
227 XMLTest( "Bad XML", doc.ErrorID(), ERROR_PARSING_ATTRIBUTE );
228 }
229
230 {
231 const char* str = "<doc attr0='1' attr1='2.0' attr2='foo' />";
232
233 XMLDocument doc;
234 doc.Parse( str );
235
236 XMLElement* ele = doc.FirstChildElement();
237
238 int iVal, result;
239 double dVal;
240
241 result = ele->QueryDoubleAttribute( "attr0", &dVal );
242 XMLTest( "Query attribute: int as double", result, XML_NO_ERROR );
243 XMLTest( "Query attribute: int as double", (int)dVal, 1 );
244 result = ele->QueryDoubleAttribute( "attr1", &dVal );
245 XMLTest( "Query attribute: double as double", (int)dVal, 2 );
246 result = ele->QueryIntAttribute( "attr1", &iVal );
247 XMLTest( "Query attribute: double as int", result, XML_NO_ERROR );
248 XMLTest( "Query attribute: double as int", iVal, 2 );
249 result = ele->QueryIntAttribute( "attr2", &iVal );
250 XMLTest( "Query attribute: not a number", result, WRONG_ATTRIBUTE_TYPE );
251 result = ele->QueryIntAttribute( "bar", &iVal );
252 XMLTest( "Query attribute: does not exist", result, NO_ATTRIBUTE );
253 }
254
255 {
256 const char* str = "<doc/>";
257
258 XMLDocument doc;
259 doc.Parse( str );
260
261 XMLElement* ele = doc.FirstChildElement();
262
263 int iVal;
264 double dVal;
265
266 ele->SetAttribute( "str", "strValue" );
267 ele->SetAttribute( "int", 1 );
268 ele->SetAttribute( "double", -1.0 );
269
270 const char* cStr = ele->Attribute( "str" );
271 ele->QueryIntAttribute( "int", &iVal );
272 ele->QueryDoubleAttribute( "double", &dVal );
273
274 XMLTest( "Attribute round trip. c-string.", "strValue", cStr );
275 XMLTest( "Attribute round trip. int.", 1, iVal );
276 XMLTest( "Attribute round trip. double.", -1, (int)dVal );
277 }
278
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800279 {
280 XMLDocument doc;
281 doc.LoadFile( "utf8test.xml" );
282
283 // Get the attribute "value" from the "Russian" element and check it.
284 XMLElement* element = doc.FirstChildElement( "document" )->FirstChildElement( "Russian" );
285 const unsigned char correctValue[] = { 0xd1U, 0x86U, 0xd0U, 0xb5U, 0xd0U, 0xbdU, 0xd0U, 0xbdU,
286 0xd0U, 0xbeU, 0xd1U, 0x81U, 0xd1U, 0x82U, 0xd1U, 0x8cU, 0 };
287
288 XMLTest( "UTF-8: Russian value.", (const char*)correctValue, element->Attribute( "value" ) );
289
290 const unsigned char russianElementName[] = { 0xd0U, 0xa0U, 0xd1U, 0x83U,
291 0xd1U, 0x81U, 0xd1U, 0x81U,
292 0xd0U, 0xbaU, 0xd0U, 0xb8U,
293 0xd0U, 0xb9U, 0 };
294 const char russianText[] = "<\xD0\xB8\xD0\xBC\xD0\xB5\xD0\xB5\xD1\x82>";
295
296 XMLText* text = doc.FirstChildElement( "document" )->FirstChildElement( (const char*) russianElementName )->FirstChild()->ToText();
297 XMLTest( "UTF-8: Browsing russian element name.",
298 russianText,
299 text->Value() );
300
301 // Now try for a round trip.
302 doc.SaveFile( "utf8testout.xml" );
303
304 // Check the round trip.
305 char savedBuf[256];
306 char verifyBuf[256];
307 int okay = 0;
308
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800309
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800310#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800311#pragma warning ( push )
312#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800313#endif
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800314 FILE* saved = fopen( "utf8testout.xml", "r" );
315 FILE* verify = fopen( "utf8testverify.xml", "r" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800316#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800317#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800318#endif
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800319
320 if ( saved && verify )
321 {
322 okay = 1;
323 while ( fgets( verifyBuf, 256, verify ) )
324 {
325 fgets( savedBuf, 256, saved );
326 NullLineEndings( verifyBuf );
327 NullLineEndings( savedBuf );
328
329 if ( strcmp( verifyBuf, savedBuf ) )
330 {
331 printf( "verify:%s<\n", verifyBuf );
332 printf( "saved :%s<\n", savedBuf );
333 okay = 0;
334 break;
335 }
336 }
337 }
338 if ( saved )
339 fclose( saved );
340 if ( verify )
341 fclose( verify );
342 XMLTest( "UTF-8: Verified multi-language round trip.", 1, okay );
343 }
344
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800345 // --------GetText()-----------
346 {
347 const char* str = "<foo>This is text</foo>";
348 XMLDocument doc;
349 doc.Parse( str );
350 const XMLElement* element = doc.RootElement();
351
352 XMLTest( "GetText() normal use.", "This is text", element->GetText() );
353
354 str = "<foo><b>This is text</b></foo>";
355 doc.Parse( str );
356 element = doc.RootElement();
357
358 XMLTest( "GetText() contained element.", element->GetText() == 0, true );
359 }
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800360
Lee Thomasond6277762012-02-22 16:00:12 -0800361
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800362 // ---------- CDATA ---------------
363 {
364 const char* str = "<xmlElement>"
365 "<![CDATA["
366 "I am > the rules!\n"
367 "...since I make symbolic puns"
368 "]]>"
369 "</xmlElement>";
370 XMLDocument doc;
371 doc.Parse( str );
372 doc.Print();
Lee Thomasond6277762012-02-22 16:00:12 -0800373
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800374 XMLTest( "CDATA parse.", doc.FirstChildElement()->FirstChild()->Value(),
375 "I am > the rules!\n...since I make symbolic puns",
Lee Thomasond6277762012-02-22 16:00:12 -0800376 false );
377 }
378
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800379 // ----------- CDATA -------------
380 {
381 const char* str = "<xmlElement>"
382 "<![CDATA["
383 "<b>I am > the rules!</b>\n"
384 "...since I make symbolic puns"
385 "]]>"
386 "</xmlElement>";
387 XMLDocument doc;
388 doc.Parse( str );
389 doc.Print();
390
391 XMLTest( "CDATA parse. [ tixml1:1480107 ]", doc.FirstChildElement()->FirstChild()->Value(),
392 "<b>I am > the rules!</b>\n...since I make symbolic puns",
393 false );
394 }
395
396 // InsertAfterChild causes crash.
397 {
398 // InsertBeforeChild and InsertAfterChild causes crash.
399 XMLDocument doc;
400 XMLElement* parent = doc.NewElement( "Parent" );
401 doc.InsertFirstChild( parent );
402
403 XMLElement* childText0 = doc.NewElement( "childText0" );
404 XMLElement* childText1 = doc.NewElement( "childText1" );
405
406 XMLNode* childNode0 = parent->InsertEndChild( childText0 );
407 XMLNode* childNode1 = parent->InsertAfterChild( childNode0, childText1 );
408
409 XMLTest( "Test InsertAfterChild on empty node. ", ( childNode1 == parent->LastChild() ), true );
410 }
Lee Thomasond6277762012-02-22 16:00:12 -0800411
412 {
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800413 // Entities not being written correctly.
414 // From Lynn Allen
Lee Thomasond6277762012-02-22 16:00:12 -0800415
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800416 const char* passages =
417 "<?xml version=\"1.0\" standalone=\"no\" ?>"
418 "<passages count=\"006\" formatversion=\"20020620\">"
419 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
420 " It also has &lt;, &gt;, and &amp;, as well as a fake copyright &#xA9;.\"> </psg>"
421 "</passages>";
Lee Thomasond6277762012-02-22 16:00:12 -0800422
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800423 XMLDocument doc;
424 doc.Parse( passages );
425 XMLElement* psg = doc.RootElement()->FirstChildElement();
426 const char* context = psg->Attribute( "context" );
427 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 -0800428
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800429 XMLTest( "Entity transformation: read. ", expected, context, true );
Lee Thomasond6277762012-02-22 16:00:12 -0800430
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800431#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800432#pragma warning ( push )
433#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800434#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800435 FILE* textfile = fopen( "textfile.txt", "w" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800436#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800437#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800438#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800439 if ( textfile )
440 {
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800441 XMLPrinter streamer( textfile );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800442 psg->Accept( &streamer );
443 fclose( textfile );
444 }
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800445#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800446#pragma warning ( push )
447#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800448#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800449 textfile = fopen( "textfile.txt", "r" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800450#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -0800451#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800452#endif
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800453 TIXMLASSERT( textfile );
454 if ( textfile )
455 {
456 char buf[ 1024 ];
457 fgets( buf, 1024, textfile );
458 XMLTest( "Entity transformation: write. ",
459 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
460 " It also has &lt;, &gt;, and &amp;, as well as a fake copyright \xC2\xA9.\"/>\n",
461 buf, false );
462 }
463 fclose( textfile );
464 }
465
466 {
Lee Thomason6f381b72012-03-02 12:59:39 -0800467 // Suppress entities.
468 const char* passages =
469 "<?xml version=\"1.0\" standalone=\"no\" ?>"
470 "<passages count=\"006\" formatversion=\"20020620\">"
471 "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;.\">Crazy &ttk;</psg>"
472 "</passages>";
473
474 XMLDocument doc( false );
475 doc.Parse( passages );
476
477 XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->Attribute( "context" ),
478 "Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;." );
479 XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->FirstChild()->Value(),
480 "Crazy &ttk;" );
481 doc.Print();
482 }
483
484 {
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800485 const char* test = "<?xml version='1.0'?><a.elem xmi.version='2.0'/>";
486
487 XMLDocument doc;
488 doc.Parse( test );
489 XMLTest( "dot in names", doc.Error(), 0);
490 XMLTest( "dot in names", doc.FirstChildElement()->Name(), "a.elem" );
491 XMLTest( "dot in names", doc.FirstChildElement()->Attribute( "xmi.version" ), "2.0" );
492 }
493
494 {
495 const char* test = "<element><Name>1.1 Start easy ignore fin thickness&#xA;</Name></element>";
496
497 XMLDocument doc;
498 doc.Parse( test );
499
500 XMLText* text = doc.FirstChildElement()->FirstChildElement()->FirstChild()->ToText();
501 XMLTest( "Entity with one digit.",
502 text->Value(), "1.1 Start easy ignore fin thickness\n",
503 false );
504 }
505
506 {
507 // DOCTYPE not preserved (950171)
508 //
509 const char* doctype =
510 "<?xml version=\"1.0\" ?>"
511 "<!DOCTYPE PLAY SYSTEM 'play.dtd'>"
512 "<!ELEMENT title (#PCDATA)>"
513 "<!ELEMENT books (title,authors)>"
514 "<element />";
515
516 XMLDocument doc;
517 doc.Parse( doctype );
518 doc.SaveFile( "test7.xml" );
519 doc.DeleteChild( doc.RootElement() );
520 doc.LoadFile( "test7.xml" );
521 doc.Print();
522
523 const XMLUnknown* decl = doc.FirstChild()->NextSibling()->ToUnknown();
524 XMLTest( "Correct value of unknown.", "DOCTYPE PLAY SYSTEM 'play.dtd'", decl->Value() );
525
526 }
527
528 {
529 // Comments do not stream out correctly.
530 const char* doctype =
531 "<!-- Somewhat<evil> -->";
532 XMLDocument doc;
533 doc.Parse( doctype );
534
535 XMLComment* comment = doc.FirstChild()->ToComment();
536
537 XMLTest( "Comment formatting.", " Somewhat<evil> ", comment->Value() );
538 }
539 {
540 // Double attributes
541 const char* doctype = "<element attr='red' attr='blue' />";
542
543 XMLDocument doc;
544 doc.Parse( doctype );
545
546 XMLTest( "Parsing repeated attributes.", 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 -0800547 doc.PrintError();
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800548 }
549
550 {
551 // Embedded null in stream.
552 const char* doctype = "<element att\0r='red' attr='blue' />";
553
554 XMLDocument doc;
555 doc.Parse( doctype );
556 XMLTest( "Embedded null throws error.", true, doc.Error() );
557 }
558
559 {
560 // Empty documents should return TIXML_ERROR_PARSING_EMPTY, bug 1070717
561 const char* str = " ";
562 XMLDocument doc;
563 doc.Parse( str );
564 XMLTest( "Empty document error", ERROR_EMPTY_DOCUMENT, doc.ErrorID() );
565 }
566
567 {
568 // Low entities
569 XMLDocument doc;
570 doc.Parse( "<test>&#x0e;</test>" );
571 const char result[] = { 0x0e, 0 };
572 XMLTest( "Low entities.", doc.FirstChildElement()->GetText(), result );
573 doc.Print();
574 }
575
576 {
577 // Attribute values with trailing quotes not handled correctly
578 XMLDocument doc;
579 doc.Parse( "<foo attribute=bar\" />" );
580 XMLTest( "Throw error with bad end quotes.", doc.Error(), true );
581 }
582
583 {
584 // [ 1663758 ] Failure to report error on bad XML
585 XMLDocument xml;
586 xml.Parse("<x>");
587 XMLTest("Missing end tag at end of input", xml.Error(), true);
588 xml.Parse("<x> ");
589 XMLTest("Missing end tag with trailing whitespace", xml.Error(), true);
590 xml.Parse("<x></y>");
591 XMLTest("Mismatched tags", xml.ErrorID(), ERROR_MISMATCHED_ELEMENT);
592 }
593
594
595 {
596 // [ 1475201 ] TinyXML parses entities in comments
597 XMLDocument xml;
598 xml.Parse("<!-- declarations for <head> & <body> -->"
599 "<!-- far &amp; away -->" );
600
601 XMLNode* e0 = xml.FirstChild();
602 XMLNode* e1 = e0->NextSibling();
603 XMLComment* c0 = e0->ToComment();
604 XMLComment* c1 = e1->ToComment();
605
606 XMLTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
607 XMLTest( "Comments ignore entities.", " far &amp; away ", c1->Value(), true );
608 }
609
610 {
611 XMLDocument xml;
612 xml.Parse( "<Parent>"
613 "<child1 att=''/>"
614 "<!-- With this comment, child2 will not be parsed! -->"
615 "<child2 att=''/>"
616 "</Parent>" );
617 xml.Print();
618
619 int count = 0;
620
621 for( XMLNode* ele = xml.FirstChildElement( "Parent" )->FirstChild();
622 ele;
623 ele = ele->NextSibling() )
624 {
625 ++count;
626 }
627
628 XMLTest( "Comments iterate correctly.", 3, count );
629 }
630
631 {
632 // trying to repro ]1874301]. If it doesn't go into an infinite loop, all is well.
633 unsigned char buf[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?><feed><![CDATA[Test XMLblablablalblbl";
634 buf[60] = 239;
635 buf[61] = 0;
636
637 XMLDocument doc;
638 doc.Parse( (const char*)buf);
639 }
640
641
642 {
643 // bug 1827248 Error while parsing a little bit malformed file
644 // Actually not malformed - should work.
645 XMLDocument xml;
646 xml.Parse( "<attributelist> </attributelist >" );
647 XMLTest( "Handle end tag whitespace", false, xml.Error() );
648 }
649
650 {
651 // This one must not result in an infinite loop
652 XMLDocument xml;
653 xml.Parse( "<infinite>loop" );
654 XMLTest( "Infinite loop test.", true, true );
655 }
656#endif
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800657 {
658 const char* pub = "<?xml version='1.0'?> <element><sub/></element> <!--comment--> <!DOCTYPE>";
659 XMLDocument doc;
660 doc.Parse( pub );
661
662 XMLDocument clone;
663 for( const XMLNode* node=doc.FirstChild(); node; node=node->NextSibling() ) {
664 XMLNode* copy = node->ShallowClone( &clone );
665 clone.InsertEndChild( copy );
666 }
667
668 clone.Print();
669
670 int count=0;
671 const XMLNode* a=clone.FirstChild();
672 const XMLNode* b=doc.FirstChild();
673 for( ; a && b; a=a->NextSibling(), b=b->NextSibling() ) {
674 ++count;
675 XMLTest( "Clone and Equal", true, a->ShallowEqual( b ));
676 }
677 XMLTest( "Clone and Equal", 4, count );
678 }
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800679
Lee Thomason6f381b72012-03-02 12:59:39 -0800680 // ----------- Performance tracking --------------
681 {
682#if defined( _MSC_VER )
683 __int64 start, end, freq;
684 QueryPerformanceFrequency( (LARGE_INTEGER*) &freq );
685#endif
686
687#if defined(_MSC_VER)
688#pragma warning ( push )
689#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
690#endif
691 FILE* fp = fopen( "dream.xml", "r" );
692#if defined(_MSC_VER)
693#pragma warning ( pop )
694#endif
695 fseek( fp, 0, SEEK_END );
696 long size = ftell( fp );
697 fseek( fp, 0, SEEK_SET );
698
699 char* mem = new char[size+1];
700 fread( mem, size, 1, fp );
701 fclose( fp );
702 mem[size] = 0;
703
704#if defined( _MSC_VER )
705 QueryPerformanceCounter( (LARGE_INTEGER*) &start );
706#else
707 clock_t cstart = clock();
708#endif
709 static const int COUNT = 10;
710 for( int i=0; i<COUNT; ++i ) {
711 XMLDocument doc;
712 doc.Parse( mem );
713 }
714#if defined( _MSC_VER )
715 QueryPerformanceCounter( (LARGE_INTEGER*) &end );
716#else
717 clock_t cend = clock();
718#endif
719
720 delete [] mem;
721
722 static const char* note =
723#ifdef DEBUG
724 "DEBUG";
725#else
726 "Release";
727#endif
728
729#if defined( _MSC_VER )
730 printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, 1000.0 * (double)(end-start) / ( (double)freq * (double)COUNT) );
731#else
732 printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, (double)(cend - cstart)/(double)COUNT );
733#endif
734 }
735
Lee Thomason (grinliz)7ca55582012-03-07 21:54:57 -0800736 #if defined( _MSC_VER ) && defined( DEBUG )
Lee Thomason1ff38e02012-02-14 18:18:16 -0800737 _CrtMemCheckpoint( &endMemState );
738 //_CrtMemDumpStatistics( &endMemState );
739
740 _CrtMemState diffMemState;
741 _CrtMemDifference( &diffMemState, &startMemState, &endMemState );
742 _CrtMemDumpStatistics( &diffMemState );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800743 //printf( "new total=%d\n", gNewTotal );
Lee Thomason1ff38e02012-02-14 18:18:16 -0800744 #endif
745
746 printf ("\nPass %d, Fail %d\n", gPass, gFail);
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800747 return 0;
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -0800748}