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