U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1 | #include "tinyxml2.h"
|
| 2 |
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 3 | #include <cstdlib>
|
| 4 | #include <cstring>
|
| 5 | #include <ctime>
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 6 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 7 | #if defined( _MSC_VER )
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 8 | #include <crtdbg.h>
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 9 | #define WIN32_LEAN_AND_MEAN
|
| 10 | #include <windows.h>
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 11 | _CrtMemState startMemState;
|
| 12 | _CrtMemState endMemState;
|
| 13 | #endif
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 14 |
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 15 | using namespace tinyxml2;
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 16 | int gPass = 0;
|
| 17 | int gFail = 0;
|
| 18 |
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 19 |
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 20 | bool XMLTest (const char* testString, const char* expected, const char* found, bool echo=true )
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 21 | {
|
| 22 | bool pass = !strcmp( expected, found );
|
| 23 | if ( pass )
|
| 24 | printf ("[pass]");
|
| 25 | else
|
| 26 | printf ("[fail]");
|
| 27 |
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 28 | if ( !echo )
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 29 | printf (" %s\n", testString);
|
| 30 | else
|
| 31 | printf (" %s [%s][%s]\n", testString, expected, found);
|
| 32 |
|
| 33 | if ( pass )
|
| 34 | ++gPass;
|
| 35 | else
|
| 36 | ++gFail;
|
| 37 | return pass;
|
| 38 | }
|
| 39 |
|
| 40 |
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 41 | bool XMLTest( const char* testString, int expected, int found, bool echo=true )
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 42 | {
|
| 43 | bool pass = ( expected == found );
|
| 44 | if ( pass )
|
| 45 | printf ("[pass]");
|
| 46 | else
|
| 47 | printf ("[fail]");
|
| 48 |
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 49 | if ( !echo )
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 50 | printf (" %s\n", testString);
|
| 51 | else
|
| 52 | printf (" %s [%d][%d]\n", testString, expected, found);
|
| 53 |
|
| 54 | if ( pass )
|
| 55 | ++gPass;
|
| 56 | else
|
| 57 | ++gFail;
|
| 58 | return pass;
|
| 59 | }
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 60 |
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 61 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 62 | void NullLineEndings( char* p )
|
| 63 | {
|
| 64 | while( p && *p ) {
|
| 65 | if ( *p == '\n' || *p == '\r' ) {
|
| 66 | *p = 0;
|
| 67 | return;
|
| 68 | }
|
| 69 | ++p;
|
| 70 | }
|
| 71 | }
|
| 72 |
|
| 73 |
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 74 | int main( int /*argc*/, const char ** /*argv*/ )
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 75 | {
|
Lee Thomason (grinliz) | 0a4df40 | 2012-02-27 20:50:52 -0800 | [diff] [blame] | 76 | #if defined( _MSC_VER ) && defined( DEBUG )
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 77 | _CrtMemCheckpoint( &startMemState );
|
| 78 | #endif
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 79 |
|
Lee Thomason | 7f7b162 | 2012-03-24 12:49:03 -0700 | [diff] [blame] | 80 | #if defined(_MSC_VER)
|
| 81 | #pragma warning ( push )
|
| 82 | #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
|
| 83 | #endif
|
| 84 |
|
| 85 | FILE* fp = fopen( "dream.xml", "r" );
|
| 86 | if ( !fp ) {
|
| 87 | printf( "Error opening test file 'dream.xml'.\n"
|
| 88 | "Is your working directory the same as where \n"
|
| 89 | "the xmltest.cpp and dream.xml file are?\n\n"
|
| 90 | #if defined( _MSC_VER )
|
| 91 | "In windows Visual Studio you may need to set\n"
|
| 92 | "Properties->Debugging->Working Directory to '..'\n"
|
| 93 | #endif
|
| 94 | );
|
| 95 | exit( 1 );
|
| 96 | }
|
| 97 | fclose( fp );
|
| 98 |
|
| 99 | #if defined(_MSC_VER)
|
| 100 | #pragma warning ( pop )
|
| 101 | #endif
|
| 102 |
|
Lee Thomason | 87e475a | 2012-03-20 11:55:29 -0700 | [diff] [blame] | 103 | /* ------ Example 1: Load and parse an XML file. ---- */
|
| 104 | {
|
| 105 | XMLDocument doc;
|
| 106 | doc.LoadFile( "dream.xml" );
|
| 107 | }
|
| 108 |
|
| 109 | /* ------ Example 2: Lookup information. ---- */
|
| 110 | {
|
| 111 | XMLDocument doc;
|
| 112 | doc.LoadFile( "dream.xml" );
|
| 113 |
|
| 114 | // Structure of the XML file:
|
| 115 | // - Element "PLAY" the root Element
|
| 116 | // - - Element "TITLE" child of the root PLAY Element
|
| 117 | // - - - Text child of the TITLE Element
|
| 118 |
|
| 119 | // Navigate to the title, using the convenience function, with a dangerous lack of error checking.
|
| 120 | const char* title = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" )->GetText();
|
| 121 | printf( "Name of play (1): %s\n", title );
|
| 122 |
|
| 123 | // Text is just another Node to TinyXML-2. The more general way to get to the XMLText:
|
| 124 | XMLText* textNode = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" )->FirstChild()->ToText();
|
| 125 | title = textNode->Value();
|
| 126 | printf( "Name of play (2): %s\n", title );
|
| 127 | }
|
| 128 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 129 | {
|
Lee Thomason | 43f5930 | 2012-02-06 18:18:11 -0800 | [diff] [blame] | 130 | static const char* test[] = { "<element />",
|
| 131 | "<element></element>",
|
| 132 | "<element><subelement/></element>",
|
| 133 | "<element><subelement></subelement></element>",
|
| 134 | "<element><subelement><subsub/></subelement></element>",
|
| 135 | "<!--comment beside elements--><element><subelement></subelement></element>",
|
| 136 | "<!--comment beside elements, this time with spaces--> \n <element> <subelement> \n </subelement> </element>",
|
| 137 | "<element attrib1='foo' attrib2=\"bar\" ></element>",
|
| 138 | "<element attrib1='foo' attrib2=\"bar\" ><subelement attrib3='yeehaa' /></element>",
|
| 139 | "<element>Text inside element.</element>",
|
| 140 | "<element><b></b></element>",
|
| 141 | "<element>Text inside and <b>bolded</b> in the element.</element>",
|
| 142 | "<outer><element>Text inside and <b>bolded</b> in the element.</element></outer>",
|
Lee Thomason | 8ee7989 | 2012-01-25 17:44:30 -0800 | [diff] [blame] | 143 | "<element>This & That.</element>",
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 144 | "<element attrib='This<That' />",
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 145 | 0
|
| 146 | };
|
Lee Thomason | 6ee99fc | 2012-01-21 18:45:16 -0800 | [diff] [blame] | 147 | for( int i=0; test[i]; ++i ) {
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 148 | XMLDocument doc;
|
Lee Thomason | 6ee99fc | 2012-01-21 18:45:16 -0800 | [diff] [blame] | 149 | doc.Parse( test[i] );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 150 | doc.Print();
|
Lee Thomason | ec975ce | 2012-01-23 11:42:06 -0800 | [diff] [blame] | 151 | printf( "----------------------------------------------\n" );
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 152 | }
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 153 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 154 | #if 1
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 155 | {
|
| 156 | static const char* test = "<!--hello world\n"
|
| 157 | " line 2\r"
|
| 158 | " line 3\r\n"
|
| 159 | " line 4\n\r"
|
| 160 | " line 5\r-->";
|
| 161 |
|
| 162 | XMLDocument doc;
|
| 163 | doc.Parse( test );
|
| 164 | doc.Print();
|
| 165 | }
|
| 166 |
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 167 | {
|
| 168 | static const char* test = "<element>Text before.</element>";
|
| 169 | XMLDocument doc;
|
| 170 | doc.Parse( test );
|
| 171 | XMLElement* root = doc.FirstChildElement();
|
| 172 | XMLElement* newElement = doc.NewElement( "Subelement" );
|
| 173 | root->InsertEndChild( newElement );
|
| 174 | doc.Print();
|
| 175 | }
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 176 | {
|
| 177 | XMLDocument* doc = new XMLDocument();
|
| 178 | static const char* test = "<element><sub/></element>";
|
| 179 | doc->Parse( test );
|
| 180 | delete doc;
|
| 181 | }
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 182 | {
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 183 | // Test: Programmatic DOM
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 184 | // Build:
|
| 185 | // <element>
|
| 186 | // <!--comment-->
|
| 187 | // <sub attrib="1" />
|
| 188 | // <sub attrib="2" />
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 189 | // <sub attrib="3" >& Text!</sub>
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 190 | // <element>
|
| 191 |
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 192 | XMLDocument* doc = new XMLDocument();
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 193 | XMLNode* element = doc->InsertEndChild( doc->NewElement( "element" ) );
|
| 194 |
|
| 195 | XMLElement* sub[3] = { doc->NewElement( "sub" ), doc->NewElement( "sub" ), doc->NewElement( "sub" ) };
|
| 196 | for( int i=0; i<3; ++i ) {
|
| 197 | sub[i]->SetAttribute( "attrib", i );
|
| 198 | }
|
| 199 | element->InsertEndChild( sub[2] );
|
| 200 | XMLNode* comment = element->InsertFirstChild( doc->NewComment( "comment" ) );
|
| 201 | element->InsertAfterChild( comment, sub[0] );
|
| 202 | element->InsertAfterChild( sub[0], sub[1] );
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 203 | sub[2]->InsertFirstChild( doc->NewText( "& Text!" ));
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 204 | doc->Print();
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 205 | XMLTest( "Programmatic DOM", "comment", doc->FirstChildElement( "element" )->FirstChild()->Value() );
|
| 206 | XMLTest( "Programmatic DOM", "0", doc->FirstChildElement( "element" )->FirstChildElement()->Attribute( "attrib" ) );
|
| 207 | XMLTest( "Programmatic DOM", 2, doc->FirstChildElement()->LastChildElement( "sub" )->IntAttribute( "attrib" ) );
|
| 208 | XMLTest( "Programmatic DOM", "& Text!",
|
| 209 | doc->FirstChildElement()->LastChildElement( "sub" )->FirstChild()->ToText()->Value() );
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 210 |
|
| 211 | // And now deletion:
|
| 212 | element->DeleteChild( sub[2] );
|
| 213 | doc->DeleteNode( comment );
|
| 214 |
|
| 215 | element->FirstChildElement()->SetAttribute( "attrib", true );
|
| 216 | element->LastChildElement()->DeleteAttribute( "attrib" );
|
| 217 |
|
| 218 | XMLTest( "Programmatic DOM", true, doc->FirstChildElement()->FirstChildElement()->BoolAttribute( "attrib" ) );
|
| 219 | int value = 10;
|
| 220 | int result = doc->FirstChildElement()->LastChildElement()->QueryIntAttribute( "attrib", &value );
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 221 | XMLTest( "Programmatic DOM", result, XML_NO_ATTRIBUTE );
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 222 | XMLTest( "Programmatic DOM", value, 10 );
|
| 223 |
|
| 224 | doc->Print();
|
| 225 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 226 | XMLPrinter streamer;
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 227 | doc->Print( &streamer );
|
| 228 | printf( "%s", streamer.CStr() );
|
| 229 |
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 230 | delete doc;
|
| 231 | }
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 232 | {
|
| 233 | // Test: Dream
|
| 234 | // XML1 : 1,187,569 bytes in 31,209 allocations
|
| 235 | // XML2 : 469,073 bytes in 323 allocations
|
| 236 | //int newStart = gNew;
|
| 237 | XMLDocument doc;
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 238 | doc.LoadFile( "dream.xml" );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 239 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 240 | doc.SaveFile( "dreamout.xml" );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 241 | doc.PrintError();
|
| 242 |
|
| 243 | XMLTest( "Dream", "xml version=\"1.0\"",
|
| 244 | doc.FirstChild()->ToDeclaration()->Value() );
|
| 245 | XMLTest( "Dream", true, doc.FirstChild()->NextSibling()->ToUnknown() ? true : false );
|
| 246 | XMLTest( "Dream", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
|
| 247 | doc.FirstChild()->NextSibling()->ToUnknown()->Value() );
|
| 248 | XMLTest( "Dream", "And Robin shall restore amends.",
|
| 249 | doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
|
| 250 | XMLTest( "Dream", "And Robin shall restore amends.",
|
| 251 | doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
|
| 252 |
|
| 253 | XMLDocument doc2;
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 254 | doc2.LoadFile( "dreamout.xml" );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 255 | XMLTest( "Dream-out", "xml version=\"1.0\"",
|
| 256 | doc2.FirstChild()->ToDeclaration()->Value() );
|
| 257 | XMLTest( "Dream-out", true, doc2.FirstChild()->NextSibling()->ToUnknown() ? true : false );
|
| 258 | XMLTest( "Dream-out", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
|
| 259 | doc2.FirstChild()->NextSibling()->ToUnknown()->Value() );
|
| 260 | XMLTest( "Dream-out", "And Robin shall restore amends.",
|
| 261 | doc2.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
|
| 262 |
|
| 263 | //gNewTotal = gNew - newStart;
|
| 264 | }
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 265 |
|
| 266 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 267 | {
|
| 268 | const char* error = "<?xml version=\"1.0\" standalone=\"no\" ?>\n"
|
| 269 | "<passages count=\"006\" formatversion=\"20020620\">\n"
|
| 270 | " <wrong error>\n"
|
| 271 | "</passages>";
|
| 272 |
|
| 273 | XMLDocument doc;
|
| 274 | doc.Parse( error );
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 275 | XMLTest( "Bad XML", doc.ErrorID(), XML_ERROR_PARSING_ATTRIBUTE );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 276 | }
|
| 277 |
|
| 278 | {
|
| 279 | const char* str = "<doc attr0='1' attr1='2.0' attr2='foo' />";
|
| 280 |
|
| 281 | XMLDocument doc;
|
| 282 | doc.Parse( str );
|
| 283 |
|
| 284 | XMLElement* ele = doc.FirstChildElement();
|
| 285 |
|
| 286 | int iVal, result;
|
| 287 | double dVal;
|
| 288 |
|
| 289 | result = ele->QueryDoubleAttribute( "attr0", &dVal );
|
| 290 | XMLTest( "Query attribute: int as double", result, XML_NO_ERROR );
|
| 291 | XMLTest( "Query attribute: int as double", (int)dVal, 1 );
|
| 292 | result = ele->QueryDoubleAttribute( "attr1", &dVal );
|
| 293 | XMLTest( "Query attribute: double as double", (int)dVal, 2 );
|
| 294 | result = ele->QueryIntAttribute( "attr1", &iVal );
|
| 295 | XMLTest( "Query attribute: double as int", result, XML_NO_ERROR );
|
| 296 | XMLTest( "Query attribute: double as int", iVal, 2 );
|
| 297 | result = ele->QueryIntAttribute( "attr2", &iVal );
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 298 | XMLTest( "Query attribute: not a number", result, XML_WRONG_ATTRIBUTE_TYPE );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 299 | result = ele->QueryIntAttribute( "bar", &iVal );
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 300 | XMLTest( "Query attribute: does not exist", result, XML_NO_ATTRIBUTE );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 301 | }
|
| 302 |
|
| 303 | {
|
| 304 | const char* str = "<doc/>";
|
| 305 |
|
| 306 | XMLDocument doc;
|
| 307 | doc.Parse( str );
|
| 308 |
|
| 309 | XMLElement* ele = doc.FirstChildElement();
|
| 310 |
|
| 311 | int iVal;
|
| 312 | double dVal;
|
| 313 |
|
| 314 | ele->SetAttribute( "str", "strValue" );
|
| 315 | ele->SetAttribute( "int", 1 );
|
| 316 | ele->SetAttribute( "double", -1.0 );
|
| 317 |
|
| 318 | const char* cStr = ele->Attribute( "str" );
|
| 319 | ele->QueryIntAttribute( "int", &iVal );
|
| 320 | ele->QueryDoubleAttribute( "double", &dVal );
|
| 321 |
|
Lee Thomason | 8ba7f7d | 2012-03-24 13:04:04 -0700 | [diff] [blame] | 322 | XMLTest( "Attribute match test", ele->Attribute( "str", "strValue" ), "strValue" );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 323 | XMLTest( "Attribute round trip. c-string.", "strValue", cStr );
|
| 324 | XMLTest( "Attribute round trip. int.", 1, iVal );
|
| 325 | XMLTest( "Attribute round trip. double.", -1, (int)dVal );
|
| 326 | }
|
| 327 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 328 | {
|
| 329 | XMLDocument doc;
|
| 330 | doc.LoadFile( "utf8test.xml" );
|
| 331 |
|
| 332 | // Get the attribute "value" from the "Russian" element and check it.
|
| 333 | XMLElement* element = doc.FirstChildElement( "document" )->FirstChildElement( "Russian" );
|
| 334 | const unsigned char correctValue[] = { 0xd1U, 0x86U, 0xd0U, 0xb5U, 0xd0U, 0xbdU, 0xd0U, 0xbdU,
|
| 335 | 0xd0U, 0xbeU, 0xd1U, 0x81U, 0xd1U, 0x82U, 0xd1U, 0x8cU, 0 };
|
| 336 |
|
| 337 | XMLTest( "UTF-8: Russian value.", (const char*)correctValue, element->Attribute( "value" ) );
|
| 338 |
|
| 339 | const unsigned char russianElementName[] = { 0xd0U, 0xa0U, 0xd1U, 0x83U,
|
| 340 | 0xd1U, 0x81U, 0xd1U, 0x81U,
|
| 341 | 0xd0U, 0xbaU, 0xd0U, 0xb8U,
|
| 342 | 0xd0U, 0xb9U, 0 };
|
| 343 | const char russianText[] = "<\xD0\xB8\xD0\xBC\xD0\xB5\xD0\xB5\xD1\x82>";
|
| 344 |
|
| 345 | XMLText* text = doc.FirstChildElement( "document" )->FirstChildElement( (const char*) russianElementName )->FirstChild()->ToText();
|
| 346 | XMLTest( "UTF-8: Browsing russian element name.",
|
| 347 | russianText,
|
| 348 | text->Value() );
|
| 349 |
|
| 350 | // Now try for a round trip.
|
| 351 | doc.SaveFile( "utf8testout.xml" );
|
| 352 |
|
| 353 | // Check the round trip.
|
| 354 | char savedBuf[256];
|
| 355 | char verifyBuf[256];
|
| 356 | int okay = 0;
|
| 357 |
|
Lee Thomason (grinliz) | 5ce4d97 | 2012-02-26 21:14:23 -0800 | [diff] [blame] | 358 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 359 | #if defined(_MSC_VER)
|
Lee Thomason (grinliz) | 5ce4d97 | 2012-02-26 21:14:23 -0800 | [diff] [blame] | 360 | #pragma warning ( push )
|
| 361 | #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 362 | #endif
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 363 | FILE* saved = fopen( "utf8testout.xml", "r" );
|
| 364 | FILE* verify = fopen( "utf8testverify.xml", "r" );
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 365 | #if defined(_MSC_VER)
|
Lee Thomason (grinliz) | 5ce4d97 | 2012-02-26 21:14:23 -0800 | [diff] [blame] | 366 | #pragma warning ( pop )
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 367 | #endif
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 368 |
|
| 369 | if ( saved && verify )
|
| 370 | {
|
| 371 | okay = 1;
|
| 372 | while ( fgets( verifyBuf, 256, verify ) )
|
| 373 | {
|
| 374 | fgets( savedBuf, 256, saved );
|
| 375 | NullLineEndings( verifyBuf );
|
| 376 | NullLineEndings( savedBuf );
|
| 377 |
|
| 378 | if ( strcmp( verifyBuf, savedBuf ) )
|
| 379 | {
|
| 380 | printf( "verify:%s<\n", verifyBuf );
|
| 381 | printf( "saved :%s<\n", savedBuf );
|
| 382 | okay = 0;
|
| 383 | break;
|
| 384 | }
|
| 385 | }
|
| 386 | }
|
| 387 | if ( saved )
|
| 388 | fclose( saved );
|
| 389 | if ( verify )
|
| 390 | fclose( verify );
|
| 391 | XMLTest( "UTF-8: Verified multi-language round trip.", 1, okay );
|
| 392 | }
|
| 393 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 394 | // --------GetText()-----------
|
| 395 | {
|
| 396 | const char* str = "<foo>This is text</foo>";
|
| 397 | XMLDocument doc;
|
| 398 | doc.Parse( str );
|
| 399 | const XMLElement* element = doc.RootElement();
|
| 400 |
|
| 401 | XMLTest( "GetText() normal use.", "This is text", element->GetText() );
|
| 402 |
|
| 403 | str = "<foo><b>This is text</b></foo>";
|
| 404 | doc.Parse( str );
|
| 405 | element = doc.RootElement();
|
| 406 |
|
| 407 | XMLTest( "GetText() contained element.", element->GetText() == 0, true );
|
| 408 | }
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 409 |
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 410 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 411 | // ---------- CDATA ---------------
|
| 412 | {
|
| 413 | const char* str = "<xmlElement>"
|
| 414 | "<![CDATA["
|
| 415 | "I am > the rules!\n"
|
| 416 | "...since I make symbolic puns"
|
| 417 | "]]>"
|
| 418 | "</xmlElement>";
|
| 419 | XMLDocument doc;
|
| 420 | doc.Parse( str );
|
| 421 | doc.Print();
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 422 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 423 | XMLTest( "CDATA parse.", doc.FirstChildElement()->FirstChild()->Value(),
|
| 424 | "I am > the rules!\n...since I make symbolic puns",
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 425 | false );
|
| 426 | }
|
| 427 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 428 | // ----------- CDATA -------------
|
| 429 | {
|
| 430 | const char* str = "<xmlElement>"
|
| 431 | "<![CDATA["
|
| 432 | "<b>I am > the rules!</b>\n"
|
| 433 | "...since I make symbolic puns"
|
| 434 | "]]>"
|
| 435 | "</xmlElement>";
|
| 436 | XMLDocument doc;
|
| 437 | doc.Parse( str );
|
| 438 | doc.Print();
|
| 439 |
|
| 440 | XMLTest( "CDATA parse. [ tixml1:1480107 ]", doc.FirstChildElement()->FirstChild()->Value(),
|
| 441 | "<b>I am > the rules!</b>\n...since I make symbolic puns",
|
| 442 | false );
|
| 443 | }
|
| 444 |
|
| 445 | // InsertAfterChild causes crash.
|
| 446 | {
|
| 447 | // InsertBeforeChild and InsertAfterChild causes crash.
|
| 448 | XMLDocument doc;
|
| 449 | XMLElement* parent = doc.NewElement( "Parent" );
|
| 450 | doc.InsertFirstChild( parent );
|
| 451 |
|
| 452 | XMLElement* childText0 = doc.NewElement( "childText0" );
|
| 453 | XMLElement* childText1 = doc.NewElement( "childText1" );
|
| 454 |
|
| 455 | XMLNode* childNode0 = parent->InsertEndChild( childText0 );
|
| 456 | XMLNode* childNode1 = parent->InsertAfterChild( childNode0, childText1 );
|
| 457 |
|
| 458 | XMLTest( "Test InsertAfterChild on empty node. ", ( childNode1 == parent->LastChild() ), true );
|
| 459 | }
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 460 |
|
| 461 | {
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 462 | // Entities not being written correctly.
|
| 463 | // From Lynn Allen
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 464 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 465 | const char* passages =
|
| 466 | "<?xml version=\"1.0\" standalone=\"no\" ?>"
|
| 467 | "<passages count=\"006\" formatversion=\"20020620\">"
|
| 468 | "<psg context=\"Line 5 has "quotation marks" and 'apostrophe marks'."
|
| 469 | " It also has <, >, and &, as well as a fake copyright ©.\"> </psg>"
|
| 470 | "</passages>";
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 471 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 472 | XMLDocument doc;
|
| 473 | doc.Parse( passages );
|
| 474 | XMLElement* psg = doc.RootElement()->FirstChildElement();
|
| 475 | const char* context = psg->Attribute( "context" );
|
| 476 | const char* expected = "Line 5 has \"quotation marks\" and 'apostrophe marks'. It also has <, >, and &, as well as a fake copyright \xC2\xA9.";
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 477 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 478 | XMLTest( "Entity transformation: read. ", expected, context, true );
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 479 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 480 | #if defined(_MSC_VER)
|
Lee Thomason (grinliz) | 5ce4d97 | 2012-02-26 21:14:23 -0800 | [diff] [blame] | 481 | #pragma warning ( push )
|
| 482 | #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 483 | #endif
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 484 | FILE* textfile = fopen( "textfile.txt", "w" );
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 485 | #if defined(_MSC_VER)
|
Lee Thomason (grinliz) | 5ce4d97 | 2012-02-26 21:14:23 -0800 | [diff] [blame] | 486 | #pragma warning ( pop )
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 487 | #endif
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 488 | if ( textfile )
|
| 489 | {
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 490 | XMLPrinter streamer( textfile );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 491 | psg->Accept( &streamer );
|
| 492 | fclose( textfile );
|
| 493 | }
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 494 | #if defined(_MSC_VER)
|
Lee Thomason (grinliz) | 5ce4d97 | 2012-02-26 21:14:23 -0800 | [diff] [blame] | 495 | #pragma warning ( push )
|
| 496 | #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 497 | #endif
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 498 | textfile = fopen( "textfile.txt", "r" );
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 499 | #if defined(_MSC_VER)
|
Lee Thomason (grinliz) | 5ce4d97 | 2012-02-26 21:14:23 -0800 | [diff] [blame] | 500 | #pragma warning ( pop )
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 501 | #endif
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 502 | TIXMLASSERT( textfile );
|
| 503 | if ( textfile )
|
| 504 | {
|
| 505 | char buf[ 1024 ];
|
| 506 | fgets( buf, 1024, textfile );
|
| 507 | XMLTest( "Entity transformation: write. ",
|
| 508 | "<psg context=\"Line 5 has "quotation marks" and 'apostrophe marks'."
|
| 509 | " It also has <, >, and &, as well as a fake copyright \xC2\xA9.\"/>\n",
|
| 510 | buf, false );
|
| 511 | }
|
| 512 | fclose( textfile );
|
| 513 | }
|
| 514 |
|
| 515 | {
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 516 | // Suppress entities.
|
| 517 | const char* passages =
|
| 518 | "<?xml version=\"1.0\" standalone=\"no\" ?>"
|
| 519 | "<passages count=\"006\" formatversion=\"20020620\">"
|
| 520 | "<psg context=\"Line 5 has "quotation marks" and 'apostrophe marks'.\">Crazy &ttk;</psg>"
|
| 521 | "</passages>";
|
| 522 |
|
| 523 | XMLDocument doc( false );
|
| 524 | doc.Parse( passages );
|
| 525 |
|
| 526 | XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->Attribute( "context" ),
|
| 527 | "Line 5 has "quotation marks" and 'apostrophe marks'." );
|
| 528 | XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->FirstChild()->Value(),
|
| 529 | "Crazy &ttk;" );
|
| 530 | doc.Print();
|
| 531 | }
|
| 532 |
|
| 533 | {
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 534 | const char* test = "<?xml version='1.0'?><a.elem xmi.version='2.0'/>";
|
| 535 |
|
| 536 | XMLDocument doc;
|
| 537 | doc.Parse( test );
|
| 538 | XMLTest( "dot in names", doc.Error(), 0);
|
| 539 | XMLTest( "dot in names", doc.FirstChildElement()->Name(), "a.elem" );
|
| 540 | XMLTest( "dot in names", doc.FirstChildElement()->Attribute( "xmi.version" ), "2.0" );
|
| 541 | }
|
| 542 |
|
| 543 | {
|
| 544 | const char* test = "<element><Name>1.1 Start easy ignore fin thickness
</Name></element>";
|
| 545 |
|
| 546 | XMLDocument doc;
|
| 547 | doc.Parse( test );
|
| 548 |
|
| 549 | XMLText* text = doc.FirstChildElement()->FirstChildElement()->FirstChild()->ToText();
|
| 550 | XMLTest( "Entity with one digit.",
|
| 551 | text->Value(), "1.1 Start easy ignore fin thickness\n",
|
| 552 | false );
|
| 553 | }
|
| 554 |
|
| 555 | {
|
| 556 | // DOCTYPE not preserved (950171)
|
| 557 | //
|
| 558 | const char* doctype =
|
| 559 | "<?xml version=\"1.0\" ?>"
|
| 560 | "<!DOCTYPE PLAY SYSTEM 'play.dtd'>"
|
| 561 | "<!ELEMENT title (#PCDATA)>"
|
| 562 | "<!ELEMENT books (title,authors)>"
|
| 563 | "<element />";
|
| 564 |
|
| 565 | XMLDocument doc;
|
| 566 | doc.Parse( doctype );
|
| 567 | doc.SaveFile( "test7.xml" );
|
| 568 | doc.DeleteChild( doc.RootElement() );
|
| 569 | doc.LoadFile( "test7.xml" );
|
| 570 | doc.Print();
|
| 571 |
|
| 572 | const XMLUnknown* decl = doc.FirstChild()->NextSibling()->ToUnknown();
|
| 573 | XMLTest( "Correct value of unknown.", "DOCTYPE PLAY SYSTEM 'play.dtd'", decl->Value() );
|
| 574 |
|
| 575 | }
|
| 576 |
|
| 577 | {
|
| 578 | // Comments do not stream out correctly.
|
| 579 | const char* doctype =
|
| 580 | "<!-- Somewhat<evil> -->";
|
| 581 | XMLDocument doc;
|
| 582 | doc.Parse( doctype );
|
| 583 |
|
| 584 | XMLComment* comment = doc.FirstChild()->ToComment();
|
| 585 |
|
| 586 | XMLTest( "Comment formatting.", " Somewhat<evil> ", comment->Value() );
|
| 587 | }
|
| 588 | {
|
| 589 | // Double attributes
|
| 590 | const char* doctype = "<element attr='red' attr='blue' />";
|
| 591 |
|
| 592 | XMLDocument doc;
|
| 593 | doc.Parse( doctype );
|
| 594 |
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 595 | XMLTest( "Parsing repeated attributes.", XML_ERROR_PARSING_ATTRIBUTE, doc.ErrorID() ); // is an error to tinyxml (didn't use to be, but caused issues)
|
Lee Thomason (grinliz) | 0a4df40 | 2012-02-27 20:50:52 -0800 | [diff] [blame] | 596 | doc.PrintError();
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 597 | }
|
| 598 |
|
| 599 | {
|
| 600 | // Embedded null in stream.
|
| 601 | const char* doctype = "<element att\0r='red' attr='blue' />";
|
| 602 |
|
| 603 | XMLDocument doc;
|
| 604 | doc.Parse( doctype );
|
| 605 | XMLTest( "Embedded null throws error.", true, doc.Error() );
|
| 606 | }
|
| 607 |
|
| 608 | {
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 609 | // Empty documents should return TIXML_XML_ERROR_PARSING_EMPTY, bug 1070717
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 610 | const char* str = " ";
|
| 611 | XMLDocument doc;
|
| 612 | doc.Parse( str );
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 613 | XMLTest( "Empty document error", XML_ERROR_EMPTY_DOCUMENT, doc.ErrorID() );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 614 | }
|
| 615 |
|
| 616 | {
|
| 617 | // Low entities
|
| 618 | XMLDocument doc;
|
| 619 | doc.Parse( "<test></test>" );
|
| 620 | const char result[] = { 0x0e, 0 };
|
| 621 | XMLTest( "Low entities.", doc.FirstChildElement()->GetText(), result );
|
| 622 | doc.Print();
|
| 623 | }
|
| 624 |
|
| 625 | {
|
| 626 | // Attribute values with trailing quotes not handled correctly
|
| 627 | XMLDocument doc;
|
| 628 | doc.Parse( "<foo attribute=bar\" />" );
|
| 629 | XMLTest( "Throw error with bad end quotes.", doc.Error(), true );
|
| 630 | }
|
| 631 |
|
| 632 | {
|
| 633 | // [ 1663758 ] Failure to report error on bad XML
|
| 634 | XMLDocument xml;
|
| 635 | xml.Parse("<x>");
|
| 636 | XMLTest("Missing end tag at end of input", xml.Error(), true);
|
| 637 | xml.Parse("<x> ");
|
| 638 | XMLTest("Missing end tag with trailing whitespace", xml.Error(), true);
|
| 639 | xml.Parse("<x></y>");
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 640 | XMLTest("Mismatched tags", xml.ErrorID(), XML_ERROR_MISMATCHED_ELEMENT);
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 641 | }
|
| 642 |
|
| 643 |
|
| 644 | {
|
| 645 | // [ 1475201 ] TinyXML parses entities in comments
|
| 646 | XMLDocument xml;
|
| 647 | xml.Parse("<!-- declarations for <head> & <body> -->"
|
| 648 | "<!-- far & away -->" );
|
| 649 |
|
| 650 | XMLNode* e0 = xml.FirstChild();
|
| 651 | XMLNode* e1 = e0->NextSibling();
|
| 652 | XMLComment* c0 = e0->ToComment();
|
| 653 | XMLComment* c1 = e1->ToComment();
|
| 654 |
|
| 655 | XMLTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
|
| 656 | XMLTest( "Comments ignore entities.", " far & away ", c1->Value(), true );
|
| 657 | }
|
| 658 |
|
| 659 | {
|
| 660 | XMLDocument xml;
|
| 661 | xml.Parse( "<Parent>"
|
| 662 | "<child1 att=''/>"
|
| 663 | "<!-- With this comment, child2 will not be parsed! -->"
|
| 664 | "<child2 att=''/>"
|
| 665 | "</Parent>" );
|
| 666 | xml.Print();
|
| 667 |
|
| 668 | int count = 0;
|
| 669 |
|
| 670 | for( XMLNode* ele = xml.FirstChildElement( "Parent" )->FirstChild();
|
| 671 | ele;
|
| 672 | ele = ele->NextSibling() )
|
| 673 | {
|
| 674 | ++count;
|
| 675 | }
|
| 676 |
|
| 677 | XMLTest( "Comments iterate correctly.", 3, count );
|
| 678 | }
|
| 679 |
|
| 680 | {
|
| 681 | // trying to repro ]1874301]. If it doesn't go into an infinite loop, all is well.
|
| 682 | unsigned char buf[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?><feed><![CDATA[Test XMLblablablalblbl";
|
| 683 | buf[60] = 239;
|
| 684 | buf[61] = 0;
|
| 685 |
|
| 686 | XMLDocument doc;
|
| 687 | doc.Parse( (const char*)buf);
|
| 688 | }
|
| 689 |
|
| 690 |
|
| 691 | {
|
| 692 | // bug 1827248 Error while parsing a little bit malformed file
|
| 693 | // Actually not malformed - should work.
|
| 694 | XMLDocument xml;
|
| 695 | xml.Parse( "<attributelist> </attributelist >" );
|
| 696 | XMLTest( "Handle end tag whitespace", false, xml.Error() );
|
| 697 | }
|
| 698 |
|
| 699 | {
|
| 700 | // This one must not result in an infinite loop
|
| 701 | XMLDocument xml;
|
| 702 | xml.Parse( "<infinite>loop" );
|
| 703 | XMLTest( "Infinite loop test.", true, true );
|
| 704 | }
|
| 705 | #endif
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 706 | {
|
| 707 | const char* pub = "<?xml version='1.0'?> <element><sub/></element> <!--comment--> <!DOCTYPE>";
|
| 708 | XMLDocument doc;
|
| 709 | doc.Parse( pub );
|
| 710 |
|
| 711 | XMLDocument clone;
|
| 712 | for( const XMLNode* node=doc.FirstChild(); node; node=node->NextSibling() ) {
|
| 713 | XMLNode* copy = node->ShallowClone( &clone );
|
| 714 | clone.InsertEndChild( copy );
|
| 715 | }
|
| 716 |
|
| 717 | clone.Print();
|
| 718 |
|
| 719 | int count=0;
|
| 720 | const XMLNode* a=clone.FirstChild();
|
| 721 | const XMLNode* b=doc.FirstChild();
|
| 722 | for( ; a && b; a=a->NextSibling(), b=b->NextSibling() ) {
|
| 723 | ++count;
|
| 724 | XMLTest( "Clone and Equal", true, a->ShallowEqual( b ));
|
| 725 | }
|
| 726 | XMLTest( "Clone and Equal", 4, count );
|
| 727 | }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 728 |
|
Lee Thomason | 5708f81 | 2012-03-28 17:46:41 -0700 | [diff] [blame] | 729 | // -------- Handles ------------
|
| 730 | {
|
| 731 | static const char* xml = "<element attrib='bar'><sub>Text</sub></element>";
|
| 732 | XMLDocument doc;
|
| 733 | doc.Parse( xml );
|
| 734 | const XMLDocument& docC = doc;
|
| 735 |
|
| 736 | XMLElement* ele = XMLHandle( doc ).FirstChildElement( "element" ).FirstChild().ToElement();
|
| 737 | XMLTest( "Handle, success, mutable", ele->Value(), "sub" );
|
| 738 |
|
Lee Thomason | 5761203 | 2012-04-03 16:43:22 -0700 | [diff] [blame^] | 739 | const XMLElement* eleC = XMLConstHandle( docC ).FirstChildElement( "element" ).FirstChild().ToElement();
|
| 740 | XMLTest( "Handle, success, mutable", eleC->Value(), "sub" );
|
Lee Thomason | 5708f81 | 2012-03-28 17:46:41 -0700 | [diff] [blame] | 741 | }
|
| 742 |
|
| 743 |
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 744 | // ----------- Performance tracking --------------
|
| 745 | {
|
| 746 | #if defined( _MSC_VER )
|
| 747 | __int64 start, end, freq;
|
| 748 | QueryPerformanceFrequency( (LARGE_INTEGER*) &freq );
|
| 749 | #endif
|
| 750 |
|
| 751 | #if defined(_MSC_VER)
|
| 752 | #pragma warning ( push )
|
| 753 | #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
|
| 754 | #endif
|
| 755 | FILE* fp = fopen( "dream.xml", "r" );
|
| 756 | #if defined(_MSC_VER)
|
| 757 | #pragma warning ( pop )
|
| 758 | #endif
|
| 759 | fseek( fp, 0, SEEK_END );
|
| 760 | long size = ftell( fp );
|
| 761 | fseek( fp, 0, SEEK_SET );
|
| 762 |
|
| 763 | char* mem = new char[size+1];
|
| 764 | fread( mem, size, 1, fp );
|
| 765 | fclose( fp );
|
| 766 | mem[size] = 0;
|
| 767 |
|
| 768 | #if defined( _MSC_VER )
|
| 769 | QueryPerformanceCounter( (LARGE_INTEGER*) &start );
|
| 770 | #else
|
| 771 | clock_t cstart = clock();
|
| 772 | #endif
|
| 773 | static const int COUNT = 10;
|
| 774 | for( int i=0; i<COUNT; ++i ) {
|
| 775 | XMLDocument doc;
|
| 776 | doc.Parse( mem );
|
| 777 | }
|
| 778 | #if defined( _MSC_VER )
|
| 779 | QueryPerformanceCounter( (LARGE_INTEGER*) &end );
|
| 780 | #else
|
| 781 | clock_t cend = clock();
|
| 782 | #endif
|
| 783 |
|
| 784 | delete [] mem;
|
| 785 |
|
| 786 | static const char* note =
|
| 787 | #ifdef DEBUG
|
| 788 | "DEBUG";
|
| 789 | #else
|
| 790 | "Release";
|
| 791 | #endif
|
| 792 |
|
| 793 | #if defined( _MSC_VER )
|
| 794 | printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, 1000.0 * (double)(end-start) / ( (double)freq * (double)COUNT) );
|
| 795 | #else
|
| 796 | printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, (double)(cend - cstart)/(double)COUNT );
|
| 797 | #endif
|
| 798 | }
|
| 799 |
|
Lee Thomason (grinliz) | 7ca5558 | 2012-03-07 21:54:57 -0800 | [diff] [blame] | 800 | #if defined( _MSC_VER ) && defined( DEBUG )
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 801 | _CrtMemCheckpoint( &endMemState );
|
| 802 | //_CrtMemDumpStatistics( &endMemState );
|
| 803 |
|
| 804 | _CrtMemState diffMemState;
|
| 805 | _CrtMemDifference( &diffMemState, &startMemState, &endMemState );
|
| 806 | _CrtMemDumpStatistics( &diffMemState );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 807 | //printf( "new total=%d\n", gNewTotal );
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 808 | #endif
|
| 809 |
|
| 810 | printf ("\nPass %d, Fail %d\n", gPass, gFail);
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 811 | return 0;
|
Lee Thomason (grinliz) | 9b093cc | 2012-02-25 21:30:18 -0800 | [diff] [blame] | 812 | }
|