Warning: Can't use blame annotator:
svn blame failed on branches/fep_rpc_client/tinyxml/xmltest.cpp: ("Can't find a temporary directory: Internal error", 20014)

source: tmcsimulator/branches/fep_rpc_client/tinyxml/xmltest.cpp @ 79

Revision 79, 41.1 KB checked in by jtorres, 9 years ago (diff)

new atmsdriver package in trunk. fep_rpc_client dynamic data packing added in branches. fep_rpc_client refactored data packing code into static DataPacker? class. Added vds_data folder into config/. Added new project configuration for ATMSDriver.

RevLine 
1/*
2   Test program for TinyXML.
3*/
4
5
6#ifdef TIXML_USE_STL
7        #include <iostream>
8        #include <sstream>
9        using namespace std;
10#else
11        #include <stdio.h>
12#endif
13
14#if defined( WIN32 ) && defined( TUNE )
15        #include <crtdbg.h>
16        _CrtMemState startMemState;
17        _CrtMemState endMemState;
18#endif
19
20#include "tinyxml.h"
21
22bool XmlTest (const char* testString, const char* expected, const char* found, bool noEcho = false);
23bool XmlTest( const char* testString, int expected, int found, bool noEcho = false );
24
25static int gPass = 0;
26static int gFail = 0;
27
28
29
30bool XmlTest (const char* testString, const char* expected, const char* found, bool noEcho )
31{
32        bool pass = !strcmp( expected, found );
33        if ( pass )
34                printf ("[pass]");
35        else
36                printf ("[fail]");
37
38        if ( noEcho )
39                printf (" %s\n", testString);
40        else
41                printf (" %s [%s][%s]\n", testString, expected, found);
42
43        if ( pass )
44                ++gPass;
45        else
46                ++gFail;
47        return pass;
48}
49
50
51bool XmlTest( const char* testString, int expected, int found, bool noEcho )
52{
53        bool pass = ( expected == found );
54        if ( pass )
55                printf ("[pass]");
56        else
57                printf ("[fail]");
58
59        if ( noEcho )
60                printf (" %s\n", testString);
61        else
62                printf (" %s [%d][%d]\n", testString, expected, found);
63
64        if ( pass )
65                ++gPass;
66        else
67                ++gFail;
68        return pass;
69}
70
71
72void NullLineEndings( char* p )
73{
74        while( p && *p ) {
75                if ( *p == '\n' || *p == '\r' ) {
76                        *p = 0;
77                        return;
78                }
79                ++p;
80        }
81}
82
83//
84// This file demonstrates some basic functionality of TinyXml.
85// Note that the example is very contrived. It presumes you know
86// what is in the XML file. But it does test the basic operations,
87// and show how to add and remove nodes.
88//
89
90int main()
91{
92
93        //
94        // We start with the 'demoStart' todo list. Process it. And
95        // should hopefully end up with the todo list as illustrated.
96        //
97        const char* demoStart =
98                "<?xml version=\"1.0\"  standalone='no' >\n"
99                "<!-- Our to do list data -->"
100                "<ToDo>\n"
101                "<!-- Do I need a secure PDA? -->\n"
102                "<Item priority=\"1\" distance='close'> Go to the <bold>Toy store!</bold></Item>"
103                "<Item priority=\"2\" distance='none'> Do bills   </Item>"
104                "<Item priority=\"2\" distance='far &amp; back'> Look for Evil Dinosaurs! </Item>"
105                "</ToDo>";
106               
107        {
108
109        #ifdef TIXML_USE_STL
110                //      What the todo list should look like after processing.
111                // In stream (no formatting) representation.
112                const char* demoEnd =
113                        "<?xml version=\"1.0\" standalone=\"no\" ?>"
114                        "<!-- Our to do list data -->"
115                        "<ToDo>"
116                        "<!-- Do I need a secure PDA? -->"
117                        "<Item priority=\"2\" distance=\"close\">Go to the"
118                        "<bold>Toy store!"
119                        "</bold>"
120                        "</Item>"
121                        "<Item priority=\"1\" distance=\"far\">Talk to:"
122                        "<Meeting where=\"School\">"
123                        "<Attendee name=\"Marple\" position=\"teacher\" />"
124                        "<Attendee name=\"Voel\" position=\"counselor\" />"
125                        "</Meeting>"
126                        "<Meeting where=\"Lunch\" />"
127                        "</Item>"
128                        "<Item priority=\"2\" distance=\"here\">Do bills"
129                        "</Item>"
130                        "</ToDo>";
131        #endif
132
133                // The example parses from the character string (above):
134                #if defined( WIN32 ) && defined( TUNE )
135                _CrtMemCheckpoint( &startMemState );
136                #endif 
137
138                {
139                        // Write to a file and read it back, to check file I/O.
140
141                        TiXmlDocument doc( "demotest.xml" );
142                        doc.Parse( demoStart );
143
144                        if ( doc.Error() )
145                        {
146                                printf( "Error in %s: %s\n", doc.Value(), doc.ErrorDesc() );
147                                exit( 1 );
148                        }
149                        doc.SaveFile();
150                }
151
152                TiXmlDocument doc( "demotest.xml" );
153                bool loadOkay = doc.LoadFile();
154
155                if ( !loadOkay )
156                {
157                        printf( "Could not load test file 'demotest.xml'. Error='%s'. Exiting.\n", doc.ErrorDesc() );
158                        exit( 1 );
159                }
160
161                printf( "** Demo doc read from disk: ** \n\n" );
162                printf( "** Printing via doc.Print **\n" );
163                doc.Print( stdout );
164
165                {
166                        printf( "** Printing via TiXmlPrinter **\n" );
167                        TiXmlPrinter printer;
168                        doc.Accept( &printer );
169                        fprintf( stdout, "%s", printer.CStr() );
170                }
171                #ifdef TIXML_USE_STL   
172                {
173                        printf( "** Printing via operator<< **\n" );
174                        std::cout << doc;
175                }
176                #endif
177                TiXmlNode* node = 0;
178                TiXmlElement* todoElement = 0;
179                TiXmlElement* itemElement = 0;
180
181
182                // --------------------------------------------------------
183                // An example of changing existing attributes, and removing
184                // an element from the document.
185                // --------------------------------------------------------
186
187                // Get the "ToDo" element.
188                // It is a child of the document, and can be selected by name.
189                node = doc.FirstChild( "ToDo" );
190                assert( node );
191                todoElement = node->ToElement();
192                assert( todoElement  );
193
194                // Going to the toy store is now our second priority...
195                // So set the "priority" attribute of the first item in the list.
196                node = todoElement->FirstChildElement();        // This skips the "PDA" comment.
197                assert( node );
198                itemElement = node->ToElement();
199                assert( itemElement  );
200                itemElement->SetAttribute( "priority", 2 );
201
202                // Change the distance to "doing bills" from
203                // "none" to "here". It's the next sibling element.
204                itemElement = itemElement->NextSiblingElement();
205                assert( itemElement );
206                itemElement->SetAttribute( "distance", "here" );
207
208                // Remove the "Look for Evil Dinosaurs!" item.
209                // It is 1 more sibling away. We ask the parent to remove
210                // a particular child.
211                itemElement = itemElement->NextSiblingElement();
212                todoElement->RemoveChild( itemElement );
213
214                itemElement = 0;
215
216                // --------------------------------------------------------
217                // What follows is an example of created elements and text
218                // nodes and adding them to the document.
219                // --------------------------------------------------------
220
221                // Add some meetings.
222                TiXmlElement item( "Item" );
223                item.SetAttribute( "priority", "1" );
224                item.SetAttribute( "distance", "far" );
225
226                TiXmlText text( "Talk to:" );
227
228                TiXmlElement meeting1( "Meeting" );
229                meeting1.SetAttribute( "where", "School" );
230
231                TiXmlElement meeting2( "Meeting" );
232                meeting2.SetAttribute( "where", "Lunch" );
233
234                TiXmlElement attendee1( "Attendee" );
235                attendee1.SetAttribute( "name", "Marple" );
236                attendee1.SetAttribute( "position", "teacher" );
237
238                TiXmlElement attendee2( "Attendee" );
239                attendee2.SetAttribute( "name", "Voel" );
240                attendee2.SetAttribute( "position", "counselor" );
241
242                // Assemble the nodes we've created:
243                meeting1.InsertEndChild( attendee1 );
244                meeting1.InsertEndChild( attendee2 );
245
246                item.InsertEndChild( text );
247                item.InsertEndChild( meeting1 );
248                item.InsertEndChild( meeting2 );
249
250                // And add the node to the existing list after the first child.
251                node = todoElement->FirstChild( "Item" );
252                assert( node );
253                itemElement = node->ToElement();
254                assert( itemElement );
255
256                todoElement->InsertAfterChild( itemElement, item );
257
258                printf( "\n** Demo doc processed: ** \n\n" );
259                doc.Print( stdout );
260
261
262        #ifdef TIXML_USE_STL
263                printf( "** Demo doc processed to stream: ** \n\n" );
264                cout << doc << endl << endl;
265        #endif
266
267                // --------------------------------------------------------
268                // Different tests...do we have what we expect?
269                // --------------------------------------------------------
270
271                int count = 0;
272                TiXmlElement*   element;
273
274                //////////////////////////////////////////////////////
275
276        #ifdef TIXML_USE_STL
277                cout << "** Basic structure. **\n";
278                ostringstream outputStream( ostringstream::out );
279                outputStream << doc;
280                XmlTest( "Output stream correct.",      string( demoEnd ).c_str(),
281                                                                                        outputStream.str().c_str(), true );
282        #endif
283
284                node = doc.RootElement();
285                assert( node );
286                XmlTest( "Root element exists.", true, ( node != 0 && node->ToElement() ) );
287                XmlTest ( "Root element value is 'ToDo'.", "ToDo",  node->Value());
288
289                node = node->FirstChild();
290                XmlTest( "First child exists & is a comment.", true, ( node != 0 && node->ToComment() ) );
291                node = node->NextSibling();
292                XmlTest( "Sibling element exists & is an element.", true, ( node != 0 && node->ToElement() ) );
293                XmlTest ( "Value is 'Item'.", "Item", node->Value() );
294
295                node = node->FirstChild();
296                XmlTest ( "First child exists.", true, ( node != 0 && node->ToText() ) );
297                XmlTest ( "Value is 'Go to the'.", "Go to the", node->Value() );
298
299
300                //////////////////////////////////////////////////////
301                printf ("\n** Iterators. **\n");
302
303                // Walk all the top level nodes of the document.
304                count = 0;
305                for( node = doc.FirstChild();
306                         node;
307                         node = node->NextSibling() )
308                {
309                        count++;
310                }
311                XmlTest( "Top level nodes, using First / Next.", 3, count );
312
313                count = 0;
314                for( node = doc.LastChild();
315                         node;
316                         node = node->PreviousSibling() )
317                {
318                        count++;
319                }
320                XmlTest( "Top level nodes, using Last / Previous.", 3, count );
321
322                // Walk all the top level nodes of the document,
323                // using a different syntax.
324                count = 0;
325                for( node = doc.IterateChildren( 0 );
326                         node;
327                         node = doc.IterateChildren( node ) )
328                {
329                        count++;
330                }
331                XmlTest( "Top level nodes, using IterateChildren.", 3, count );
332
333                // Walk all the elements in a node.
334                count = 0;
335                for( element = todoElement->FirstChildElement();
336                         element;
337                         element = element->NextSiblingElement() )
338                {
339                        count++;
340                }
341                XmlTest( "Children of the 'ToDo' element, using First / Next.",
342                        3, count );
343
344                // Walk all the elements in a node by value.
345                count = 0;
346                for( node = todoElement->FirstChild( "Item" );
347                         node;
348                         node = node->NextSibling( "Item" ) )
349                {
350                        count++;
351                }
352                XmlTest( "'Item' children of the 'ToDo' element, using First/Next.", 3, count );
353
354                count = 0;
355                for( node = todoElement->LastChild( "Item" );
356                         node;
357                         node = node->PreviousSibling( "Item" ) )
358                {
359                        count++;
360                }
361                XmlTest( "'Item' children of the 'ToDo' element, using Last/Previous.", 3, count );
362
363        #ifdef TIXML_USE_STL
364                {
365                        cout << "\n** Parsing. **\n";
366                        istringstream parse0( "<Element0 attribute0='foo0' attribute1= noquotes attribute2 = '&gt;' />" );
367                        TiXmlElement element0( "default" );
368                        parse0 >> element0;
369
370                        XmlTest ( "Element parsed, value is 'Element0'.", "Element0", element0.Value() );
371                        XmlTest ( "Reads attribute 'attribute0=\"foo0\"'.", "foo0", element0.Attribute( "attribute0" ));
372                        XmlTest ( "Reads incorrectly formatted 'attribute1=noquotes'.", "noquotes", element0.Attribute( "attribute1" ) );
373                        XmlTest ( "Read attribute with entity value '>'.", ">", element0.Attribute( "attribute2" ) );
374                }
375        #endif
376
377                {
378                        const char* error =     "<?xml version=\"1.0\" standalone=\"no\" ?>\n"
379                                                                "<passages count=\"006\" formatversion=\"20020620\">\n"
380                                                                "    <wrong error>\n"
381                                                                "</passages>";
382
383                        TiXmlDocument docTest;
384                        docTest.Parse( error );
385                        XmlTest( "Error row", docTest.ErrorRow(), 3 );
386                        XmlTest( "Error column", docTest.ErrorCol(), 17 );
387                        //printf( "error=%d id='%s' row %d col%d\n", (int) doc.Error(), doc.ErrorDesc(), doc.ErrorRow()+1, doc.ErrorCol() + 1 );
388
389                }
390
391        #ifdef TIXML_USE_STL
392                {
393                        //////////////////////////////////////////////////////
394                        cout << "\n** Streaming. **\n";
395
396                        // Round trip check: stream in, then stream back out to verify. The stream
397                        // out has already been checked, above. We use the output
398
399                        istringstream inputStringStream( outputStream.str() );
400                        TiXmlDocument document0;
401
402                        inputStringStream >> document0;
403
404                        ostringstream outputStream0( ostringstream::out );
405                        outputStream0 << document0;
406
407                        XmlTest( "Stream round trip correct.",  string( demoEnd ).c_str(), 
408                                                                                                        outputStream0.str().c_str(), true );
409
410                        std::string str;
411                        str << document0;
412
413                        XmlTest( "String printing correct.", string( demoEnd ).c_str(), 
414                                                                                                 str.c_str(), true );
415                }
416        #endif
417        }
418
419        {
420                const char* str = "<doc attr0='1' attr1='2.0' attr2='foo' />";
421
422                TiXmlDocument doc;
423                doc.Parse( str );
424
425                TiXmlElement* ele = doc.FirstChildElement();
426
427                int iVal, result;
428                double dVal;
429
430                result = ele->QueryDoubleAttribute( "attr0", &dVal );
431                XmlTest( "Query attribute: int as double", result, TIXML_SUCCESS );
432                XmlTest( "Query attribute: int as double", (int)dVal, 1 );
433                result = ele->QueryDoubleAttribute( "attr1", &dVal );
434                XmlTest( "Query attribute: double as double", (int)dVal, 2 );
435                result = ele->QueryIntAttribute( "attr1", &iVal );
436                XmlTest( "Query attribute: double as int", result, TIXML_SUCCESS );
437                XmlTest( "Query attribute: double as int", iVal, 2 );
438                result = ele->QueryIntAttribute( "attr2", &iVal );
439                XmlTest( "Query attribute: not a number", result, TIXML_WRONG_TYPE );
440                result = ele->QueryIntAttribute( "bar", &iVal );
441                XmlTest( "Query attribute: does not exist", result, TIXML_NO_ATTRIBUTE );
442        }
443
444        {
445                const char* str = "<doc/>";
446
447                TiXmlDocument doc;
448                doc.Parse( str );
449
450                TiXmlElement* ele = doc.FirstChildElement();
451
452                int iVal;
453                double dVal;
454
455                ele->SetAttribute( "str", "strValue" );
456                ele->SetAttribute( "int", 1 );
457                ele->SetDoubleAttribute( "double", -1.0 );
458
459                const char* cStr = ele->Attribute( "str" );
460                ele->QueryIntAttribute( "int", &iVal );
461                ele->QueryDoubleAttribute( "double", &dVal );
462
463                XmlTest( "Attribute round trip. c-string.", "strValue", cStr );
464                XmlTest( "Attribute round trip. int.", 1, iVal );
465                XmlTest( "Attribute round trip. double.", -1, (int)dVal );
466        }
467       
468        {
469                const char* str =       "\t<?xml version=\"1.0\" standalone=\"no\" ?>\t<room doors='2'>\n"
470                                                        "</room>";
471
472                TiXmlDocument doc;
473                doc.SetTabSize( 8 );
474                doc.Parse( str );
475
476                TiXmlHandle docHandle( &doc );
477                TiXmlHandle roomHandle = docHandle.FirstChildElement( "room" );
478
479                assert( docHandle.Node() );
480                assert( roomHandle.Element() );
481
482                TiXmlElement* room = roomHandle.Element();
483                assert( room );
484                TiXmlAttribute* doors = room->FirstAttribute();
485                assert( doors );
486
487                XmlTest( "Location tracking: Tab 8: room row", room->Row(), 1 );
488                XmlTest( "Location tracking: Tab 8: room col", room->Column(), 49 );
489                XmlTest( "Location tracking: Tab 8: doors row", doors->Row(), 1 );
490                XmlTest( "Location tracking: Tab 8: doors col", doors->Column(), 55 );
491        }
492       
493        {
494                const char* str =       "\t<?xml version=\"1.0\" standalone=\"no\" ?>\t<room doors='2'>\n"
495                                                        "  <!-- Silly example -->\n"
496                                                        "    <door wall='north'>A great door!</door>\n"
497                                                        "\t<door wall='east'/>"
498                                                        "</room>";
499
500                TiXmlDocument doc;
501                doc.Parse( str );
502
503                TiXmlHandle docHandle( &doc );
504                TiXmlHandle roomHandle = docHandle.FirstChildElement( "room" );
505                TiXmlHandle commentHandle = docHandle.FirstChildElement( "room" ).FirstChild();
506                TiXmlHandle textHandle = docHandle.FirstChildElement( "room" ).ChildElement( "door", 0 ).FirstChild();
507                TiXmlHandle door0Handle = docHandle.FirstChildElement( "room" ).ChildElement( 0 );
508                TiXmlHandle door1Handle = docHandle.FirstChildElement( "room" ).ChildElement( 1 );
509
510                assert( docHandle.Node() );
511                assert( roomHandle.Element() );
512                assert( commentHandle.Node() );
513                assert( textHandle.Text() );
514                assert( door0Handle.Element() );
515                assert( door1Handle.Element() );
516
517                TiXmlDeclaration* declaration = doc.FirstChild()->ToDeclaration();
518                assert( declaration );
519                TiXmlElement* room = roomHandle.Element();
520                assert( room );
521                TiXmlAttribute* doors = room->FirstAttribute();
522                assert( doors );
523                TiXmlText* text = textHandle.Text();
524                TiXmlComment* comment = commentHandle.Node()->ToComment();
525                assert( comment );
526                TiXmlElement* door0 = door0Handle.Element();
527                TiXmlElement* door1 = door1Handle.Element();
528
529                XmlTest( "Location tracking: Declaration row", declaration->Row(), 1 );
530                XmlTest( "Location tracking: Declaration col", declaration->Column(), 5 );
531                XmlTest( "Location tracking: room row", room->Row(), 1 );
532                XmlTest( "Location tracking: room col", room->Column(), 45 );
533                XmlTest( "Location tracking: doors row", doors->Row(), 1 );
534                XmlTest( "Location tracking: doors col", doors->Column(), 51 );
535                XmlTest( "Location tracking: Comment row", comment->Row(), 2 );
536                XmlTest( "Location tracking: Comment col", comment->Column(), 3 );
537                XmlTest( "Location tracking: text row", text->Row(), 3 ); 
538                XmlTest( "Location tracking: text col", text->Column(), 24 );
539                XmlTest( "Location tracking: door0 row", door0->Row(), 3 );
540                XmlTest( "Location tracking: door0 col", door0->Column(), 5 );
541                XmlTest( "Location tracking: door1 row", door1->Row(), 4 );
542                XmlTest( "Location tracking: door1 col", door1->Column(), 5 );
543        }
544
545
546        // --------------------------------------------------------
547        // UTF-8 testing. It is important to test:
548        //      1. Making sure name, value, and text read correctly
549        //      2. Row, Col functionality
550        //      3. Correct output
551        // --------------------------------------------------------
552        printf ("\n** UTF-8 **\n");
553        {
554                TiXmlDocument doc( "utf8test.xml" );
555                doc.LoadFile();
556                if ( doc.Error() && doc.ErrorId() == TiXmlBase::TIXML_ERROR_OPENING_FILE ) {
557                        printf( "WARNING: File 'utf8test.xml' not found.\n"
558                                        "(Are you running the test from the wrong directory?)\n"
559                                    "Could not test UTF-8 functionality.\n" );
560                }
561                else
562                {
563                        TiXmlHandle docH( &doc );
564                        // Get the attribute "value" from the "Russian" element and check it.
565                        TiXmlElement* element = docH.FirstChildElement( "document" ).FirstChildElement( "Russian" ).Element();
566                        const unsigned char correctValue[] = {  0xd1U, 0x86U, 0xd0U, 0xb5U, 0xd0U, 0xbdU, 0xd0U, 0xbdU, 
567                                                                                                        0xd0U, 0xbeU, 0xd1U, 0x81U, 0xd1U, 0x82U, 0xd1U, 0x8cU, 0 };
568
569                        XmlTest( "UTF-8: Russian value.", (const char*)correctValue, element->Attribute( "value" ), true );
570                        XmlTest( "UTF-8: Russian value row.", 4, element->Row() );
571                        XmlTest( "UTF-8: Russian value column.", 5, element->Column() );
572
573                        const unsigned char russianElementName[] = {    0xd0U, 0xa0U, 0xd1U, 0x83U,
574                                                                                                                        0xd1U, 0x81U, 0xd1U, 0x81U,
575                                                                                                                        0xd0U, 0xbaU, 0xd0U, 0xb8U,
576                                                                                                                        0xd0U, 0xb9U, 0 };
577                        const char russianText[] = "<\xD0\xB8\xD0\xBC\xD0\xB5\xD0\xB5\xD1\x82>";
578
579                        TiXmlText* text = docH.FirstChildElement( "document" ).FirstChildElement( (const char*) russianElementName ).Child( 0 ).Text();
580                        XmlTest( "UTF-8: Browsing russian element name.",
581                                         russianText,
582                                         text->Value(),
583                                         true );
584                        XmlTest( "UTF-8: Russian element name row.", 7, text->Row() );
585                        XmlTest( "UTF-8: Russian element name column.", 47, text->Column() );
586
587                        TiXmlDeclaration* dec = docH.Child( 0 ).Node()->ToDeclaration();
588                        XmlTest( "UTF-8: Declaration column.", 1, dec->Column() );
589                        XmlTest( "UTF-8: Document column.", 1, doc.Column() );
590
591                        // Now try for a round trip.
592                        doc.SaveFile( "utf8testout.xml" );
593
594                        // Check the round trip.
595                        char savedBuf[256];
596                        char verifyBuf[256];
597                        int okay = 1;
598
599                        FILE* saved  = fopen( "utf8testout.xml", "r" );
600                        FILE* verify = fopen( "utf8testverify.xml", "r" );
601
602                        //bool firstLineBOM=true;
603                        if ( saved && verify )
604                        {
605                                while ( fgets( verifyBuf, 256, verify ) )
606                                {
607                                        fgets( savedBuf, 256, saved );
608                                        NullLineEndings( verifyBuf );
609                                        NullLineEndings( savedBuf );
610
611                                        if ( /*!firstLineBOM && */ strcmp( verifyBuf, savedBuf ) )
612                                        {
613                                                printf( "verify:%s<\n", verifyBuf );
614                                                printf( "saved :%s<\n", savedBuf );
615                                                okay = 0;
616                                                break;
617                                        }
618                                        //firstLineBOM = false;
619                                }
620                        }
621                        if ( saved )
622                                fclose( saved );
623                        if ( verify )
624                                fclose( verify );
625                        XmlTest( "UTF-8: Verified multi-language round trip.", 1, okay );
626
627                        // On most Western machines, this is an element that contains
628                        // the word "resume" with the correct accents, in a latin encoding.
629                        // It will be something else completely on non-wester machines,
630                        // which is why TinyXml is switching to UTF-8.
631                        const char latin[] = "<element>r\x82sum\x82</element>";
632
633                        TiXmlDocument latinDoc;
634                        latinDoc.Parse( latin, 0, TIXML_ENCODING_LEGACY );
635
636                        text = latinDoc.FirstChildElement()->FirstChild()->ToText();
637                        XmlTest( "Legacy encoding: Verify text element.", "r\x82sum\x82", text->Value() );
638                }
639        }               
640
641        //////////////////////
642        // Copy and assignment
643        //////////////////////
644        printf ("\n** Copy and Assignment **\n");
645        {
646                TiXmlElement element( "foo" );
647                element.Parse( "<element name='value' />", 0, TIXML_ENCODING_UNKNOWN );
648
649                TiXmlElement elementCopy( element );
650                TiXmlElement elementAssign( "foo" );
651                elementAssign.Parse( "<incorrect foo='bar'/>", 0, TIXML_ENCODING_UNKNOWN );
652                elementAssign = element;
653
654                XmlTest( "Copy/Assign: element copy #1.", "element", elementCopy.Value() );
655                XmlTest( "Copy/Assign: element copy #2.", "value", elementCopy.Attribute( "name" ) );
656                XmlTest( "Copy/Assign: element assign #1.", "element", elementAssign.Value() );
657                XmlTest( "Copy/Assign: element assign #2.", "value", elementAssign.Attribute( "name" ) );
658                XmlTest( "Copy/Assign: element assign #3.", true, ( 0 == elementAssign.Attribute( "foo" )) );
659
660                TiXmlComment comment;
661                comment.Parse( "<!--comment-->", 0, TIXML_ENCODING_UNKNOWN );
662                TiXmlComment commentCopy( comment );
663                TiXmlComment commentAssign;
664                commentAssign = commentCopy;
665                XmlTest( "Copy/Assign: comment copy.", "comment", commentCopy.Value() );
666                XmlTest( "Copy/Assign: comment assign.", "comment", commentAssign.Value() );
667
668                TiXmlUnknown unknown;
669                unknown.Parse( "<[unknown]>", 0, TIXML_ENCODING_UNKNOWN );
670                TiXmlUnknown unknownCopy( unknown );
671                TiXmlUnknown unknownAssign;
672                unknownAssign.Parse( "incorrect", 0, TIXML_ENCODING_UNKNOWN );
673                unknownAssign = unknownCopy;
674                XmlTest( "Copy/Assign: unknown copy.", "[unknown]", unknownCopy.Value() );
675                XmlTest( "Copy/Assign: unknown assign.", "[unknown]", unknownAssign.Value() );
676               
677                TiXmlText text( "TextNode" );
678                TiXmlText textCopy( text );
679                TiXmlText textAssign( "incorrect" );
680                textAssign = text;
681                XmlTest( "Copy/Assign: text copy.", "TextNode", textCopy.Value() );
682                XmlTest( "Copy/Assign: text assign.", "TextNode", textAssign.Value() );
683
684                TiXmlDeclaration dec;
685                dec.Parse( "<?xml version='1.0' encoding='UTF-8'?>", 0, TIXML_ENCODING_UNKNOWN );
686                TiXmlDeclaration decCopy( dec );
687                TiXmlDeclaration decAssign;
688                decAssign = dec;
689
690                XmlTest( "Copy/Assign: declaration copy.", "UTF-8", decCopy.Encoding() );
691                XmlTest( "Copy/Assign: text assign.", "UTF-8", decAssign.Encoding() );
692
693                TiXmlDocument doc;
694                elementCopy.InsertEndChild( textCopy );
695                doc.InsertEndChild( decAssign );
696                doc.InsertEndChild( elementCopy );
697                doc.InsertEndChild( unknownAssign );
698
699                TiXmlDocument docCopy( doc );
700                TiXmlDocument docAssign;
701                docAssign = docCopy;
702
703                #ifdef TIXML_USE_STL
704                std::string original, copy, assign;
705                original << doc;
706                copy << docCopy;
707                assign << docAssign;
708                XmlTest( "Copy/Assign: document copy.", original.c_str(), copy.c_str(), true );
709                XmlTest( "Copy/Assign: document assign.", original.c_str(), assign.c_str(), true );
710
711                #endif
712        }       
713
714        //////////////////////////////////////////////////////
715#ifdef TIXML_USE_STL
716        printf ("\n** Parsing, no Condense Whitespace **\n");
717        TiXmlBase::SetCondenseWhiteSpace( false );
718        {
719                istringstream parse1( "<start>This  is    \ntext</start>" );
720                TiXmlElement text1( "text" );
721                parse1 >> text1;
722
723                XmlTest ( "Condense white space OFF.", "This  is    \ntext",
724                                        text1.FirstChild()->Value(),
725                                        true );
726        }
727        TiXmlBase::SetCondenseWhiteSpace( true );
728#endif
729
730        //////////////////////////////////////////////////////
731        // GetText();
732        {
733                const char* str = "<foo>This is text</foo>";
734                TiXmlDocument doc;
735                doc.Parse( str );
736                const TiXmlElement* element = doc.RootElement();
737
738                XmlTest( "GetText() normal use.", "This is text", element->GetText() );
739
740                str = "<foo><b>This is text</b></foo>";
741                doc.Clear();
742                doc.Parse( str );
743                element = doc.RootElement();
744
745                XmlTest( "GetText() contained element.", element->GetText() == 0, true );
746
747                str = "<foo>This is <b>text</b></foo>";
748                doc.Clear();
749                TiXmlBase::SetCondenseWhiteSpace( false );
750                doc.Parse( str );
751                TiXmlBase::SetCondenseWhiteSpace( true );
752                element = doc.RootElement();
753
754                XmlTest( "GetText() partial.", "This is ", element->GetText() );
755        }
756
757
758        //////////////////////////////////////////////////////
759        // CDATA
760        {
761                const char* str =       "<xmlElement>"
762                                                                "<![CDATA["
763                                                                        "I am > the rules!\n"
764                                                                        "...since I make symbolic puns"
765                                                                "]]>"
766                                                        "</xmlElement>";
767                TiXmlDocument doc;
768                doc.Parse( str );
769                doc.Print();
770
771                XmlTest( "CDATA parse.", doc.FirstChildElement()->FirstChild()->Value(), 
772                                                                 "I am > the rules!\n...since I make symbolic puns",
773                                                                 true );
774
775                #ifdef TIXML_USE_STL
776                //cout << doc << '\n';
777
778                doc.Clear();
779
780                istringstream parse0( str );
781                parse0 >> doc;
782                //cout << doc << '\n';
783
784                XmlTest( "CDATA stream.", doc.FirstChildElement()->FirstChild()->Value(), 
785                                                                 "I am > the rules!\n...since I make symbolic puns",
786                                                                 true );
787                #endif
788
789                TiXmlDocument doc1 = doc;
790                //doc.Print();
791
792                XmlTest( "CDATA copy.", doc1.FirstChildElement()->FirstChild()->Value(), 
793                                                                 "I am > the rules!\n...since I make symbolic puns",
794                                                                 true );
795        }
796        {
797                // [ 1482728 ] Wrong wide char parsing
798                char buf[256];
799                buf[255] = 0;
800                for( int i=0; i<255; ++i ) {
801                        buf[i] = (char)((i>=32) ? i : 32);
802                }
803                TIXML_STRING str( "<xmlElement><![CDATA[" );
804                str += buf;
805                str += "]]></xmlElement>";
806
807                TiXmlDocument doc;
808                doc.Parse( str.c_str() );
809
810                TiXmlPrinter printer;
811                printer.SetStreamPrinting();
812                doc.Accept( &printer );
813
814                XmlTest( "CDATA with all bytes #1.", str.c_str(), printer.CStr(), true );
815
816                #ifdef TIXML_USE_STL
817                doc.Clear();
818                istringstream iss( printer.Str() );
819                iss >> doc;
820                std::string out;
821                out << doc;
822                XmlTest( "CDATA with all bytes #2.", out.c_str(), printer.CStr(), true );
823                #endif
824        }
825        {
826                // [ 1480107 ] Bug-fix for STL-streaming of CDATA that contains tags
827                // CDATA streaming had a couple of bugs, that this tests for.
828                const char* str =       "<xmlElement>"
829                                                                "<![CDATA["
830                                                                        "<b>I am > the rules!</b>\n"
831                                                                        "...since I make symbolic puns"
832                                                                "]]>"
833                                                        "</xmlElement>";
834                TiXmlDocument doc;
835                doc.Parse( str );
836                doc.Print();
837
838                XmlTest( "CDATA parse. [ 1480107 ]", doc.FirstChildElement()->FirstChild()->Value(), 
839                                                                 "<b>I am > the rules!</b>\n...since I make symbolic puns",
840                                                                 true );
841
842                #ifdef TIXML_USE_STL
843
844                doc.Clear();
845
846                istringstream parse0( str );
847                parse0 >> doc;
848
849                XmlTest( "CDATA stream. [ 1480107 ]", doc.FirstChildElement()->FirstChild()->Value(), 
850                                                                 "<b>I am > the rules!</b>\n...since I make symbolic puns",
851                                                                 true );
852                #endif
853
854                TiXmlDocument doc1 = doc;
855                //doc.Print();
856
857                XmlTest( "CDATA copy. [ 1480107 ]", doc1.FirstChildElement()->FirstChild()->Value(), 
858                                                                 "<b>I am > the rules!</b>\n...since I make symbolic puns",
859                                                                 true );
860        }
861        //////////////////////////////////////////////////////
862        // Visit()
863
864
865
866        //////////////////////////////////////////////////////
867        printf( "\n** Fuzzing... **\n" );
868
869        const int FUZZ_ITERATION = 300;
870
871        // The only goal is not to crash on bad input.
872        int len = (int) strlen( demoStart );
873        for( int i=0; i<FUZZ_ITERATION; ++i ) 
874        {
875                char* demoCopy = new char[ len+1 ];
876                strcpy( demoCopy, demoStart );
877
878                demoCopy[ i%len ] = (char)((i+1)*3);
879                demoCopy[ (i*7)%len ] = '>';
880                demoCopy[ (i*11)%len ] = '<';
881
882                TiXmlDocument xml;
883                xml.Parse( demoCopy );
884
885                delete [] demoCopy;
886        }
887        printf( "** Fuzzing Complete. **\n" );
888       
889        //////////////////////////////////////////////////////
890        printf ("\n** Bug regression tests **\n");
891
892        // InsertBeforeChild and InsertAfterChild causes crash.
893        {
894                TiXmlElement parent( "Parent" );
895                TiXmlElement childText0( "childText0" );
896                TiXmlElement childText1( "childText1" );
897                TiXmlNode* childNode0 = parent.InsertEndChild( childText0 );
898                TiXmlNode* childNode1 = parent.InsertBeforeChild( childNode0, childText1 );
899
900                XmlTest( "Test InsertBeforeChild on empty node.", ( childNode1 == parent.FirstChild() ), true );
901        }
902
903        {
904                // InsertBeforeChild and InsertAfterChild causes crash.
905                TiXmlElement parent( "Parent" );
906                TiXmlElement childText0( "childText0" );
907                TiXmlElement childText1( "childText1" );
908                TiXmlNode* childNode0 = parent.InsertEndChild( childText0 );
909                TiXmlNode* childNode1 = parent.InsertAfterChild( childNode0, childText1 );
910
911                XmlTest( "Test InsertAfterChild on empty node. ", ( childNode1 == parent.LastChild() ), true );
912        }
913
914        // Reports of missing constructors, irregular string problems.
915        {
916                // Missing constructor implementation. No test -- just compiles.
917                TiXmlText text( "Missing" );
918
919                #ifdef TIXML_USE_STL
920                        // Missing implementation:
921                        TiXmlDocument doc;
922                        string name = "missing";
923                        doc.LoadFile( name );
924
925                        TiXmlText textSTL( name );
926                #else
927                        // verifying some basic string functions:
928                        TiXmlString a;
929                        TiXmlString b( "Hello" );
930                        TiXmlString c( "ooga" );
931
932                        c = " World!";
933                        a = b;
934                        a += c;
935                        a = a;
936
937                        XmlTest( "Basic TiXmlString test. ", "Hello World!", a.c_str() );
938                #endif
939        }
940
941        // Long filenames crashing STL version
942        {
943                TiXmlDocument doc( "midsummerNightsDreamWithAVeryLongFilenameToConfuseTheStringHandlingRoutines.xml" );
944                bool loadOkay = doc.LoadFile();
945                loadOkay = true;        // get rid of compiler warning.
946                // Won't pass on non-dev systems. Just a "no crash" check.
947                //XmlTest( "Long filename. ", true, loadOkay );
948        }
949
950        {
951                // Entities not being written correctly.
952                // From Lynn Allen
953
954                const char* passages =
955                        "<?xml version=\"1.0\" standalone=\"no\" ?>"
956                        "<passages count=\"006\" formatversion=\"20020620\">"
957                                "<psg context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
958                                " It also has &lt;, &gt;, and &amp;, as well as a fake copyright &#xA9;.\"> </psg>"
959                        "</passages>";
960
961                TiXmlDocument doc( "passages.xml" );
962                doc.Parse( passages );
963                TiXmlElement* psg = doc.RootElement()->FirstChildElement();
964                const char* context = psg->Attribute( "context" );
965                const char* expected = "Line 5 has \"quotation marks\" and 'apostrophe marks'. It also has <, >, and &, as well as a fake copyright \xC2\xA9.";
966
967                XmlTest( "Entity transformation: read. ", expected, context, true );
968
969                FILE* textfile = fopen( "textfile.txt", "w" );
970                if ( textfile )
971                {
972                        psg->Print( textfile, 0 );
973                        fclose( textfile );
974                }
975                textfile = fopen( "textfile.txt", "r" );
976                assert( textfile );
977                if ( textfile )
978                {
979                        char buf[ 1024 ];
980                        fgets( buf, 1024, textfile );
981                        XmlTest( "Entity transformation: write. ",
982                                         "<psg context=\'Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;."
983                                         " It also has &lt;, &gt;, and &amp;, as well as a fake copyright \xC2\xA9.' />",
984                                         buf,
985                                         true );
986                }
987                fclose( textfile );
988        }
989
990    {
991                FILE* textfile = fopen( "test5.xml", "w" );
992                if ( textfile )
993                {
994            fputs("<?xml version='1.0'?><a.elem xmi.version='2.0'/>", textfile);
995            fclose(textfile);
996
997                        TiXmlDocument doc;
998            doc.LoadFile( "test5.xml" );
999            XmlTest( "dot in element attributes and names", doc.Error(), 0);
1000                }
1001    }
1002
1003        {
1004                FILE* textfile = fopen( "test6.xml", "w" );
1005                if ( textfile )
1006                {
1007            fputs("<element><Name>1.1 Start easy ignore fin thickness&#xA;</Name></element>", textfile );
1008            fclose(textfile);
1009
1010            TiXmlDocument doc;
1011            bool result = doc.LoadFile( "test6.xml" );
1012            XmlTest( "Entity with one digit.", result, true );
1013
1014                        TiXmlText* text = doc.FirstChildElement()->FirstChildElement()->FirstChild()->ToText();
1015                        XmlTest( "Entity with one digit.",
1016                                                text->Value(), "1.1 Start easy ignore fin thickness\n" );
1017                }
1018    }
1019
1020        {
1021                // DOCTYPE not preserved (950171)
1022                //
1023                const char* doctype =
1024                        "<?xml version=\"1.0\" ?>"
1025                        "<!DOCTYPE PLAY SYSTEM 'play.dtd'>"
1026                        "<!ELEMENT title (#PCDATA)>"
1027                        "<!ELEMENT books (title,authors)>"
1028                        "<element />";
1029
1030                TiXmlDocument doc;
1031                doc.Parse( doctype );
1032                doc.SaveFile( "test7.xml" );
1033                doc.Clear();
1034                doc.LoadFile( "test7.xml" );
1035               
1036                TiXmlHandle docH( &doc );
1037                TiXmlUnknown* unknown = docH.Child( 1 ).Unknown();
1038                XmlTest( "Correct value of unknown.", "!DOCTYPE PLAY SYSTEM 'play.dtd'", unknown->Value() );
1039                #ifdef TIXML_USE_STL
1040                TiXmlNode* node = docH.Child( 2 ).Node();
1041                std::string str;
1042                str << (*node);
1043                XmlTest( "Correct streaming of unknown.", "<!ELEMENT title (#PCDATA)>", str.c_str() );
1044                #endif
1045        }
1046
1047        {
1048                // [ 791411 ] Formatting bug
1049                // Comments do not stream out correctly.
1050                const char* doctype = 
1051                        "<!-- Somewhat<evil> -->";
1052                TiXmlDocument doc;
1053                doc.Parse( doctype );
1054
1055                TiXmlHandle docH( &doc );
1056                TiXmlComment* comment = docH.Child( 0 ).Node()->ToComment();
1057
1058                XmlTest( "Comment formatting.", " Somewhat<evil> ", comment->Value() );
1059                #ifdef TIXML_USE_STL
1060                std::string str;
1061                str << (*comment);
1062                XmlTest( "Comment streaming.", "<!-- Somewhat<evil> -->", str.c_str() );
1063                #endif
1064        }
1065
1066        {
1067                // [ 870502 ] White space issues
1068                TiXmlDocument doc;
1069                TiXmlText* text;
1070                TiXmlHandle docH( &doc );
1071       
1072                const char* doctype0 = "<element> This has leading and trailing space </element>";
1073                const char* doctype1 = "<element>This has  internal space</element>";
1074                const char* doctype2 = "<element> This has leading, trailing, and  internal space </element>";
1075
1076                TiXmlBase::SetCondenseWhiteSpace( false );
1077                doc.Clear();
1078                doc.Parse( doctype0 );
1079                text = docH.FirstChildElement( "element" ).Child( 0 ).Text();
1080                XmlTest( "White space kept.", " This has leading and trailing space ", text->Value() );
1081
1082                doc.Clear();
1083                doc.Parse( doctype1 );
1084                text = docH.FirstChildElement( "element" ).Child( 0 ).Text();
1085                XmlTest( "White space kept.", "This has  internal space", text->Value() );
1086
1087                doc.Clear();
1088                doc.Parse( doctype2 );
1089                text = docH.FirstChildElement( "element" ).Child( 0 ).Text();
1090                XmlTest( "White space kept.", " This has leading, trailing, and  internal space ", text->Value() );
1091
1092                TiXmlBase::SetCondenseWhiteSpace( true );
1093                doc.Clear();
1094                doc.Parse( doctype0 );
1095                text = docH.FirstChildElement( "element" ).Child( 0 ).Text();
1096                XmlTest( "White space condensed.", "This has leading and trailing space", text->Value() );
1097
1098                doc.Clear();
1099                doc.Parse( doctype1 );
1100                text = docH.FirstChildElement( "element" ).Child( 0 ).Text();
1101                XmlTest( "White space condensed.", "This has internal space", text->Value() );
1102
1103                doc.Clear();
1104                doc.Parse( doctype2 );
1105                text = docH.FirstChildElement( "element" ).Child( 0 ).Text();
1106                XmlTest( "White space condensed.", "This has leading, trailing, and internal space", text->Value() );
1107        }
1108
1109        {
1110                // Double attributes
1111                const char* doctype = "<element attr='red' attr='blue' />";
1112
1113                TiXmlDocument doc;
1114                doc.Parse( doctype );
1115               
1116                XmlTest( "Parsing repeated attributes.", true, doc.Error() );   // is an  error to tinyxml (didn't use to be, but caused issues)
1117                //XmlTest( "Parsing repeated attributes.", "blue", doc.FirstChildElement( "element" )->Attribute( "attr" ) );
1118        }
1119
1120        {
1121                // Embedded null in stream.
1122                const char* doctype = "<element att\0r='red' attr='blue' />";
1123
1124                TiXmlDocument doc;
1125                doc.Parse( doctype );
1126                XmlTest( "Embedded null throws error.", true, doc.Error() );
1127
1128                #ifdef TIXML_USE_STL
1129                istringstream strm( doctype );
1130                doc.Clear();
1131                doc.ClearError();
1132                strm >> doc;
1133                XmlTest( "Embedded null throws error.", true, doc.Error() );
1134                #endif
1135        }
1136
1137    {
1138            // Legacy mode test. (This test may only pass on a western system)
1139            const char* str =
1140                        "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"
1141                        "<ä>"
1142                        "CöntäntßäöüÄÖÜ"
1143                        "</ä>";
1144
1145            TiXmlDocument doc;
1146            doc.Parse( str );
1147
1148            TiXmlHandle docHandle( &doc );
1149            TiXmlHandle aHandle = docHandle.FirstChildElement( "ä" );
1150            TiXmlHandle tHandle = aHandle.Child( 0 );
1151            assert( aHandle.Element() );
1152            assert( tHandle.Text() );
1153            XmlTest( "ISO-8859-1 Parsing.", "CöntäntßäöüÄÖÜ", tHandle.Text()->Value() );
1154    }
1155
1156        {
1157                // Empty documents should return TIXML_ERROR_PARSING_EMPTY, bug 1070717
1158                const char* str = "    ";
1159                TiXmlDocument doc;
1160                doc.Parse( str );
1161                XmlTest( "Empty document error TIXML_ERROR_DOCUMENT_EMPTY", TiXmlBase::TIXML_ERROR_DOCUMENT_EMPTY, doc.ErrorId() );
1162        }
1163        #ifndef TIXML_USE_STL
1164        {
1165                // String equality. [ 1006409 ] string operator==/!= no worky in all cases
1166                TiXmlString temp;
1167                XmlTest( "Empty tinyxml string compare equal", ( temp == "" ), true );
1168
1169                TiXmlString    foo;
1170                TiXmlString    bar( "" );
1171                XmlTest( "Empty tinyxml string compare equal", ( foo == bar ), true );
1172        }
1173
1174        #endif
1175        {
1176                // Bug [ 1195696 ] from marlonism
1177                TiXmlBase::SetCondenseWhiteSpace(false); 
1178                TiXmlDocument xml; 
1179                xml.Parse("<text><break/>This hangs</text>"); 
1180                XmlTest( "Test safe error return.", xml.Error(), false );
1181        }
1182
1183        {
1184                // Bug [ 1243992 ] - another infinite loop
1185                TiXmlDocument doc;
1186                doc.SetCondenseWhiteSpace(false);
1187                doc.Parse("<p><pb></pb>test</p>");
1188        } 
1189        {
1190                // Low entities
1191                TiXmlDocument xml;
1192                xml.Parse( "<test>&#x0e;</test>" );
1193                const char result[] = { 0x0e, 0 };
1194                XmlTest( "Low entities.", xml.FirstChildElement()->GetText(), result );
1195                xml.Print();
1196        }
1197        {
1198                // Bug [ 1451649 ] Attribute values with trailing quotes not handled correctly
1199                TiXmlDocument xml;
1200                xml.Parse( "<foo attribute=bar\" />" );
1201                XmlTest( "Throw error with bad end quotes.", xml.Error(), true );
1202        }
1203        #ifdef TIXML_USE_STL
1204        {
1205                // Bug [ 1449463 ] Consider generic query
1206                TiXmlDocument xml;
1207                xml.Parse( "<foo bar='3' barStr='a string'/>" );
1208
1209                TiXmlElement* ele = xml.FirstChildElement();
1210                double d;
1211                int i;
1212                float f;
1213                bool b;
1214                std::string str;
1215
1216                XmlTest( "QueryValueAttribute", ele->QueryValueAttribute( "bar", &d ), TIXML_SUCCESS );
1217                XmlTest( "QueryValueAttribute", ele->QueryValueAttribute( "bar", &i ), TIXML_SUCCESS );
1218                XmlTest( "QueryValueAttribute", ele->QueryValueAttribute( "bar", &f ), TIXML_SUCCESS );
1219                XmlTest( "QueryValueAttribute", ele->QueryValueAttribute( "bar", &b ), TIXML_WRONG_TYPE );
1220                XmlTest( "QueryValueAttribute", ele->QueryValueAttribute( "nobar", &b ), TIXML_NO_ATTRIBUTE );
1221                XmlTest( "QueryValueAttribute", ele->QueryValueAttribute( "barStr", &str ), TIXML_SUCCESS );
1222
1223                XmlTest( "QueryValueAttribute", (d==3.0), true );
1224                XmlTest( "QueryValueAttribute", (i==3), true );
1225                XmlTest( "QueryValueAttribute", (f==3.0f), true );
1226                XmlTest( "QueryValueAttribute", (str==std::string( "a string" )), true );
1227        }
1228        #endif
1229
1230        #ifdef TIXML_USE_STL
1231        {
1232                // [ 1505267 ] redundant malloc in TiXmlElement::Attribute
1233                TiXmlDocument xml;
1234                xml.Parse( "<foo bar='3' />" );
1235                TiXmlElement* ele = xml.FirstChildElement();
1236                double d;
1237                int i;
1238
1239                std::string bar = "bar";
1240
1241                const std::string* atrrib = ele->Attribute( bar );
1242                ele->Attribute( bar, &d );
1243                ele->Attribute( bar, &i );
1244
1245                XmlTest( "Attribute", atrrib->empty(), false );
1246                XmlTest( "Attribute", (d==3.0), true );
1247                XmlTest( "Attribute", (i==3), true );
1248        }
1249        #endif
1250
1251        {
1252                // [ 1356059 ] Allow TiXMLDocument to only be at the top level
1253                TiXmlDocument xml, xml2;
1254                xml.InsertEndChild( xml2 );
1255                XmlTest( "Document only at top level.", xml.Error(), true );
1256                XmlTest( "Document only at top level.", xml.ErrorId(), TiXmlBase::TIXML_ERROR_DOCUMENT_TOP_ONLY );
1257        }
1258
1259        {
1260                // [ 1663758 ] Failure to report error on bad XML
1261                TiXmlDocument xml;
1262                xml.Parse("<x>");
1263                XmlTest("Missing end tag at end of input", xml.Error(), true);
1264                xml.Parse("<x> ");
1265                XmlTest("Missing end tag with trailing whitespace", xml.Error(), true);
1266        } 
1267
1268        {
1269                // [ 1635701 ] fail to parse files with a tag separated into two lines
1270                // I'm not sure this is a bug. Marked 'pending' for feedback.
1271                TiXmlDocument xml;
1272                xml.Parse( "<title><p>text</p\n><title>" );
1273                //xml.Print();
1274                //XmlTest( "Tag split by newline", xml.Error(), false );
1275        }
1276
1277        #ifdef TIXML_USE_STL
1278        {
1279                // [ 1475201 ] TinyXML parses entities in comments
1280                TiXmlDocument xml;
1281                istringstream parse1( "<!-- declarations for <head> & <body> -->"
1282                                                      "<!-- far &amp; away -->" );
1283                parse1 >> xml;
1284
1285                TiXmlNode* e0 = xml.FirstChild();
1286                TiXmlNode* e1 = e0->NextSibling();
1287                TiXmlComment* c0 = e0->ToComment();
1288                TiXmlComment* c1 = e1->ToComment();
1289
1290                XmlTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
1291                XmlTest( "Comments ignore entities.", " far &amp; away ", c1->Value(), true );
1292        }
1293        #endif
1294
1295        {
1296                // [ 1475201 ] TinyXML parses entities in comments
1297                TiXmlDocument xml;
1298                xml.Parse("<!-- declarations for <head> & <body> -->"
1299                                  "<!-- far &amp; away -->" );
1300
1301                TiXmlNode* e0 = xml.FirstChild();
1302                TiXmlNode* e1 = e0->NextSibling();
1303                TiXmlComment* c0 = e0->ToComment();
1304                TiXmlComment* c1 = e1->ToComment();
1305
1306                XmlTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
1307                XmlTest( "Comments ignore entities.", " far &amp; away ", c1->Value(), true );
1308        }
1309
1310        {
1311                TiXmlDocument xml;
1312                xml.Parse( "<Parent>"
1313                                                "<child1 att=''/>"
1314                                                "<!-- With this comment, child2 will not be parsed! -->"
1315                                                "<child2 att=''/>"
1316                                        "</Parent>" );
1317                int count = 0;
1318
1319                TiXmlNode* ele = 0;
1320                while ( (ele = xml.FirstChildElement( "Parent" )->IterateChildren( ele ) ) != 0 ) {
1321                        ++count;
1322                }
1323                XmlTest( "Comments iterate correctly.", 3, count );
1324        }
1325
1326        {
1327                // trying to repro ]1874301]. If it doesn't go into an infinite loop, all is well.
1328                unsigned char buf[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?><feed><![CDATA[Test XMLblablablalblbl";
1329                buf[60] = 239;
1330                buf[61] = 0;
1331
1332                TiXmlDocument doc;
1333                doc.Parse( (const char*)buf);
1334        } 
1335
1336
1337        {
1338                // bug 1827248 Error while parsing a little bit malformed file
1339                // Actually not malformed - should work.
1340                TiXmlDocument xml;
1341                xml.Parse( "<attributelist> </attributelist >" );
1342                XmlTest( "Handle end tag whitespace", false, xml.Error() );
1343        }
1344
1345        {
1346                // This one must not result in an infinite loop
1347                TiXmlDocument xml;
1348                xml.Parse( "<infinite>loop" );
1349                XmlTest( "Infinite loop test.", true, true );
1350        }
1351
1352        {
1353                // 1709904 - can not repro the crash
1354                {
1355                        TiXmlDocument xml;
1356                        xml.Parse( "<tag>/</tag>" );
1357                        XmlTest( "Odd XML parsing.", xml.FirstChild()->Value(), "tag" );
1358                }
1359                /* Could not repro. {
1360                        TiXmlDocument xml;
1361                        xml.LoadFile( "EQUI_Inventory.xml" );
1362                        //XmlTest( "Odd XML parsing.", xml.FirstChildElement()->Value(), "XML" );
1363                        TiXmlPrinter printer;
1364                        xml.Accept( &printer );
1365                        fprintf( stdout, "%s", printer.CStr() );
1366                }*/
1367        }
1368
1369        /*  1417717 experiment
1370        {
1371                TiXmlDocument xml;
1372                xml.Parse("<text>Dan & Tracie</text>");
1373                xml.Print(stdout);
1374        }
1375        {
1376                TiXmlDocument xml;
1377                xml.Parse("<text>Dan &foo; Tracie</text>");
1378                xml.Print(stdout);
1379        }
1380        */
1381
1382        #if defined( WIN32 ) && defined( TUNE )
1383        _CrtMemCheckpoint( &endMemState );
1384        //_CrtMemDumpStatistics( &endMemState );
1385
1386        _CrtMemState diffMemState;
1387        _CrtMemDifference( &diffMemState, &startMemState, &endMemState );
1388        _CrtMemDumpStatistics( &diffMemState );
1389        #endif
1390
1391        printf ("\nPass %d, Fail %d\n", gPass, gFail);
1392        return gFail;
1393}
Note: See TracBrowser for help on using the repository browser.