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;
|
Bruno Dias | a2d4e6e | 2012-05-07 04:58:11 -0300 | [diff] [blame] | 78 | doc.LoadFile( "resources/dream.xml" );
|
Lee Thomason (grinliz) | 6a22be2 | 2012-04-04 12:39:05 -0700 | [diff] [blame] | 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 |
|
Bruno Dias | a2d4e6e | 2012-05-07 04:58:11 -0300 | [diff] [blame] | 130 | FILE* fp = fopen( "resources/dream.xml", "r" );
|
Lee Thomason | 7f7b162 | 2012-03-24 12:49:03 -0700 | [diff] [blame] | 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 | 7b1b86a | 2012-06-04 17:01:38 -0700 | [diff] [blame] | 251 | {
|
| 252 | XMLPrinter streamer;
|
| 253 | doc->Print( &streamer );
|
| 254 | printf( "%s", streamer.CStr() );
|
| 255 | }
|
| 256 | {
|
| 257 | XMLPrinter streamer( 0, true );
|
| 258 | doc->Print( &streamer );
|
| 259 | XMLTest( "Compact mode", "<element><sub attrib=\"1\"/><sub/></element>", streamer.CStr(), false );
|
| 260 | }
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 261 | delete doc;
|
| 262 | }
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 263 | {
|
| 264 | // Test: Dream
|
| 265 | // XML1 : 1,187,569 bytes in 31,209 allocations
|
| 266 | // XML2 : 469,073 bytes in 323 allocations
|
| 267 | //int newStart = gNew;
|
| 268 | XMLDocument doc;
|
Bruno Dias | a2d4e6e | 2012-05-07 04:58:11 -0300 | [diff] [blame] | 269 | doc.LoadFile( "resources/dream.xml" );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 270 |
|
Bruno Dias | a2d4e6e | 2012-05-07 04:58:11 -0300 | [diff] [blame] | 271 | doc.SaveFile( "resources/dreamout.xml" );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 272 | doc.PrintError();
|
| 273 |
|
| 274 | XMLTest( "Dream", "xml version=\"1.0\"",
|
| 275 | doc.FirstChild()->ToDeclaration()->Value() );
|
| 276 | XMLTest( "Dream", true, doc.FirstChild()->NextSibling()->ToUnknown() ? true : false );
|
| 277 | XMLTest( "Dream", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
|
| 278 | doc.FirstChild()->NextSibling()->ToUnknown()->Value() );
|
| 279 | XMLTest( "Dream", "And Robin shall restore amends.",
|
| 280 | doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
|
| 281 | XMLTest( "Dream", "And Robin shall restore amends.",
|
| 282 | doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
|
| 283 |
|
| 284 | XMLDocument doc2;
|
Bruno Dias | a2d4e6e | 2012-05-07 04:58:11 -0300 | [diff] [blame] | 285 | doc2.LoadFile( "resources/dreamout.xml" );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 286 | XMLTest( "Dream-out", "xml version=\"1.0\"",
|
| 287 | doc2.FirstChild()->ToDeclaration()->Value() );
|
| 288 | XMLTest( "Dream-out", true, doc2.FirstChild()->NextSibling()->ToUnknown() ? true : false );
|
| 289 | XMLTest( "Dream-out", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
|
| 290 | doc2.FirstChild()->NextSibling()->ToUnknown()->Value() );
|
| 291 | XMLTest( "Dream-out", "And Robin shall restore amends.",
|
| 292 | doc2.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
|
| 293 |
|
| 294 | //gNewTotal = gNew - newStart;
|
| 295 | }
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 296 |
|
| 297 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 298 | {
|
| 299 | const char* error = "<?xml version=\"1.0\" standalone=\"no\" ?>\n"
|
| 300 | "<passages count=\"006\" formatversion=\"20020620\">\n"
|
| 301 | " <wrong error>\n"
|
| 302 | "</passages>";
|
| 303 |
|
| 304 | XMLDocument doc;
|
| 305 | doc.Parse( error );
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 306 | XMLTest( "Bad XML", doc.ErrorID(), XML_ERROR_PARSING_ATTRIBUTE );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 307 | }
|
| 308 |
|
| 309 | {
|
| 310 | const char* str = "<doc attr0='1' attr1='2.0' attr2='foo' />";
|
| 311 |
|
| 312 | XMLDocument doc;
|
| 313 | doc.Parse( str );
|
| 314 |
|
| 315 | XMLElement* ele = doc.FirstChildElement();
|
| 316 |
|
| 317 | int iVal, result;
|
| 318 | double dVal;
|
| 319 |
|
| 320 | result = ele->QueryDoubleAttribute( "attr0", &dVal );
|
| 321 | XMLTest( "Query attribute: int as double", result, XML_NO_ERROR );
|
| 322 | XMLTest( "Query attribute: int as double", (int)dVal, 1 );
|
| 323 | result = ele->QueryDoubleAttribute( "attr1", &dVal );
|
| 324 | XMLTest( "Query attribute: double as double", (int)dVal, 2 );
|
| 325 | result = ele->QueryIntAttribute( "attr1", &iVal );
|
| 326 | XMLTest( "Query attribute: double as int", result, XML_NO_ERROR );
|
| 327 | XMLTest( "Query attribute: double as int", iVal, 2 );
|
| 328 | result = ele->QueryIntAttribute( "attr2", &iVal );
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 329 | XMLTest( "Query attribute: not a number", result, XML_WRONG_ATTRIBUTE_TYPE );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 330 | result = ele->QueryIntAttribute( "bar", &iVal );
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 331 | XMLTest( "Query attribute: does not exist", result, XML_NO_ATTRIBUTE );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 332 | }
|
| 333 |
|
| 334 | {
|
| 335 | const char* str = "<doc/>";
|
| 336 |
|
| 337 | XMLDocument doc;
|
| 338 | doc.Parse( str );
|
| 339 |
|
| 340 | XMLElement* ele = doc.FirstChildElement();
|
| 341 |
|
| 342 | int iVal;
|
| 343 | double dVal;
|
| 344 |
|
| 345 | ele->SetAttribute( "str", "strValue" );
|
| 346 | ele->SetAttribute( "int", 1 );
|
| 347 | ele->SetAttribute( "double", -1.0 );
|
| 348 |
|
| 349 | const char* cStr = ele->Attribute( "str" );
|
| 350 | ele->QueryIntAttribute( "int", &iVal );
|
| 351 | ele->QueryDoubleAttribute( "double", &dVal );
|
| 352 |
|
Lee Thomason | 8ba7f7d | 2012-03-24 13:04:04 -0700 | [diff] [blame] | 353 | XMLTest( "Attribute match test", ele->Attribute( "str", "strValue" ), "strValue" );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 354 | XMLTest( "Attribute round trip. c-string.", "strValue", cStr );
|
| 355 | XMLTest( "Attribute round trip. int.", 1, iVal );
|
| 356 | XMLTest( "Attribute round trip. double.", -1, (int)dVal );
|
| 357 | }
|
| 358 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 359 | {
|
| 360 | XMLDocument doc;
|
Bruno Dias | a2d4e6e | 2012-05-07 04:58:11 -0300 | [diff] [blame] | 361 | doc.LoadFile( "resources/utf8test.xml" );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 362 |
|
| 363 | // Get the attribute "value" from the "Russian" element and check it.
|
| 364 | XMLElement* element = doc.FirstChildElement( "document" )->FirstChildElement( "Russian" );
|
| 365 | const unsigned char correctValue[] = { 0xd1U, 0x86U, 0xd0U, 0xb5U, 0xd0U, 0xbdU, 0xd0U, 0xbdU,
|
| 366 | 0xd0U, 0xbeU, 0xd1U, 0x81U, 0xd1U, 0x82U, 0xd1U, 0x8cU, 0 };
|
| 367 |
|
| 368 | XMLTest( "UTF-8: Russian value.", (const char*)correctValue, element->Attribute( "value" ) );
|
| 369 |
|
| 370 | const unsigned char russianElementName[] = { 0xd0U, 0xa0U, 0xd1U, 0x83U,
|
| 371 | 0xd1U, 0x81U, 0xd1U, 0x81U,
|
| 372 | 0xd0U, 0xbaU, 0xd0U, 0xb8U,
|
| 373 | 0xd0U, 0xb9U, 0 };
|
| 374 | const char russianText[] = "<\xD0\xB8\xD0\xBC\xD0\xB5\xD0\xB5\xD1\x82>";
|
| 375 |
|
| 376 | XMLText* text = doc.FirstChildElement( "document" )->FirstChildElement( (const char*) russianElementName )->FirstChild()->ToText();
|
| 377 | XMLTest( "UTF-8: Browsing russian element name.",
|
| 378 | russianText,
|
| 379 | text->Value() );
|
| 380 |
|
| 381 | // Now try for a round trip.
|
Bruno Dias | a2d4e6e | 2012-05-07 04:58:11 -0300 | [diff] [blame] | 382 | doc.SaveFile( "resources/utf8testout.xml" );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 383 |
|
| 384 | // Check the round trip.
|
| 385 | char savedBuf[256];
|
| 386 | char verifyBuf[256];
|
| 387 | int okay = 0;
|
| 388 |
|
Lee Thomason (grinliz) | 5ce4d97 | 2012-02-26 21:14:23 -0800 | [diff] [blame] | 389 |
|
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 ( push )
|
| 392 | #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] | 393 | #endif
|
Bruno Dias | a2d4e6e | 2012-05-07 04:58:11 -0300 | [diff] [blame] | 394 | FILE* saved = fopen( "resources/utf8testout.xml", "r" );
|
| 395 | FILE* verify = fopen( "resources/utf8testverify.xml", "r" );
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 396 | #if defined(_MSC_VER)
|
Lee Thomason (grinliz) | 5ce4d97 | 2012-02-26 21:14:23 -0800 | [diff] [blame] | 397 | #pragma warning ( pop )
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 398 | #endif
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 399 |
|
| 400 | if ( saved && verify )
|
| 401 | {
|
| 402 | okay = 1;
|
| 403 | while ( fgets( verifyBuf, 256, verify ) )
|
| 404 | {
|
| 405 | fgets( savedBuf, 256, saved );
|
| 406 | NullLineEndings( verifyBuf );
|
| 407 | NullLineEndings( savedBuf );
|
| 408 |
|
| 409 | if ( strcmp( verifyBuf, savedBuf ) )
|
| 410 | {
|
| 411 | printf( "verify:%s<\n", verifyBuf );
|
| 412 | printf( "saved :%s<\n", savedBuf );
|
| 413 | okay = 0;
|
| 414 | break;
|
| 415 | }
|
| 416 | }
|
| 417 | }
|
| 418 | if ( saved )
|
| 419 | fclose( saved );
|
| 420 | if ( verify )
|
| 421 | fclose( verify );
|
| 422 | XMLTest( "UTF-8: Verified multi-language round trip.", 1, okay );
|
| 423 | }
|
| 424 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 425 | // --------GetText()-----------
|
| 426 | {
|
| 427 | const char* str = "<foo>This is text</foo>";
|
| 428 | XMLDocument doc;
|
| 429 | doc.Parse( str );
|
| 430 | const XMLElement* element = doc.RootElement();
|
| 431 |
|
| 432 | XMLTest( "GetText() normal use.", "This is text", element->GetText() );
|
| 433 |
|
| 434 | str = "<foo><b>This is text</b></foo>";
|
| 435 | doc.Parse( str );
|
| 436 | element = doc.RootElement();
|
| 437 |
|
| 438 | XMLTest( "GetText() contained element.", element->GetText() == 0, true );
|
| 439 | }
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 440 |
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 441 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 442 | // ---------- CDATA ---------------
|
| 443 | {
|
| 444 | const char* str = "<xmlElement>"
|
| 445 | "<![CDATA["
|
| 446 | "I am > the rules!\n"
|
| 447 | "...since I make symbolic puns"
|
| 448 | "]]>"
|
| 449 | "</xmlElement>";
|
| 450 | XMLDocument doc;
|
| 451 | doc.Parse( str );
|
| 452 | doc.Print();
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 453 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 454 | XMLTest( "CDATA parse.", doc.FirstChildElement()->FirstChild()->Value(),
|
| 455 | "I am > the rules!\n...since I make symbolic puns",
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 456 | false );
|
| 457 | }
|
| 458 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 459 | // ----------- CDATA -------------
|
| 460 | {
|
| 461 | const char* str = "<xmlElement>"
|
| 462 | "<![CDATA["
|
| 463 | "<b>I am > the rules!</b>\n"
|
| 464 | "...since I make symbolic puns"
|
| 465 | "]]>"
|
| 466 | "</xmlElement>";
|
| 467 | XMLDocument doc;
|
| 468 | doc.Parse( str );
|
| 469 | doc.Print();
|
| 470 |
|
| 471 | XMLTest( "CDATA parse. [ tixml1:1480107 ]", doc.FirstChildElement()->FirstChild()->Value(),
|
| 472 | "<b>I am > the rules!</b>\n...since I make symbolic puns",
|
| 473 | false );
|
| 474 | }
|
| 475 |
|
| 476 | // InsertAfterChild causes crash.
|
| 477 | {
|
| 478 | // InsertBeforeChild and InsertAfterChild causes crash.
|
| 479 | XMLDocument doc;
|
| 480 | XMLElement* parent = doc.NewElement( "Parent" );
|
| 481 | doc.InsertFirstChild( parent );
|
| 482 |
|
| 483 | XMLElement* childText0 = doc.NewElement( "childText0" );
|
| 484 | XMLElement* childText1 = doc.NewElement( "childText1" );
|
| 485 |
|
| 486 | XMLNode* childNode0 = parent->InsertEndChild( childText0 );
|
| 487 | XMLNode* childNode1 = parent->InsertAfterChild( childNode0, childText1 );
|
| 488 |
|
| 489 | XMLTest( "Test InsertAfterChild on empty node. ", ( childNode1 == parent->LastChild() ), true );
|
| 490 | }
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 491 |
|
| 492 | {
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 493 | // Entities not being written correctly.
|
| 494 | // From Lynn Allen
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 495 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 496 | const char* passages =
|
| 497 | "<?xml version=\"1.0\" standalone=\"no\" ?>"
|
| 498 | "<passages count=\"006\" formatversion=\"20020620\">"
|
| 499 | "<psg context=\"Line 5 has "quotation marks" and 'apostrophe marks'."
|
| 500 | " It also has <, >, and &, as well as a fake copyright ©.\"> </psg>"
|
| 501 | "</passages>";
|
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 | XMLDocument doc;
|
| 504 | doc.Parse( passages );
|
| 505 | XMLElement* psg = doc.RootElement()->FirstChildElement();
|
| 506 | const char* context = psg->Attribute( "context" );
|
| 507 | 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] | 508 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 509 | XMLTest( "Entity transformation: read. ", expected, context, true );
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 510 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 511 | #if defined(_MSC_VER)
|
Lee Thomason (grinliz) | 5ce4d97 | 2012-02-26 21:14:23 -0800 | [diff] [blame] | 512 | #pragma warning ( push )
|
| 513 | #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] | 514 | #endif
|
Bruno Dias | a2d4e6e | 2012-05-07 04:58:11 -0300 | [diff] [blame] | 515 | FILE* textfile = fopen( "resources/textfile.txt", "w" );
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 516 | #if defined(_MSC_VER)
|
Lee Thomason (grinliz) | 5ce4d97 | 2012-02-26 21:14:23 -0800 | [diff] [blame] | 517 | #pragma warning ( pop )
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 518 | #endif
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 519 | if ( textfile )
|
| 520 | {
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 521 | XMLPrinter streamer( textfile );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 522 | psg->Accept( &streamer );
|
| 523 | fclose( textfile );
|
| 524 | }
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 525 | #if defined(_MSC_VER)
|
Lee Thomason (grinliz) | 5ce4d97 | 2012-02-26 21:14:23 -0800 | [diff] [blame] | 526 | #pragma warning ( push )
|
| 527 | #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] | 528 | #endif
|
Bruno Dias | a2d4e6e | 2012-05-07 04:58:11 -0300 | [diff] [blame] | 529 | textfile = fopen( "resources/textfile.txt", "r" );
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 530 | #if defined(_MSC_VER)
|
Lee Thomason (grinliz) | 5ce4d97 | 2012-02-26 21:14:23 -0800 | [diff] [blame] | 531 | #pragma warning ( pop )
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 532 | #endif
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 533 | TIXMLASSERT( textfile );
|
| 534 | if ( textfile )
|
| 535 | {
|
| 536 | char buf[ 1024 ];
|
| 537 | fgets( buf, 1024, textfile );
|
| 538 | XMLTest( "Entity transformation: write. ",
|
| 539 | "<psg context=\"Line 5 has "quotation marks" and 'apostrophe marks'."
|
| 540 | " It also has <, >, and &, as well as a fake copyright \xC2\xA9.\"/>\n",
|
| 541 | buf, false );
|
| 542 | }
|
| 543 | fclose( textfile );
|
| 544 | }
|
| 545 |
|
| 546 | {
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 547 | // Suppress entities.
|
| 548 | const char* passages =
|
| 549 | "<?xml version=\"1.0\" standalone=\"no\" ?>"
|
| 550 | "<passages count=\"006\" formatversion=\"20020620\">"
|
| 551 | "<psg context=\"Line 5 has "quotation marks" and 'apostrophe marks'.\">Crazy &ttk;</psg>"
|
| 552 | "</passages>";
|
| 553 |
|
| 554 | XMLDocument doc( false );
|
| 555 | doc.Parse( passages );
|
| 556 |
|
| 557 | XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->Attribute( "context" ),
|
| 558 | "Line 5 has "quotation marks" and 'apostrophe marks'." );
|
| 559 | XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->FirstChild()->Value(),
|
| 560 | "Crazy &ttk;" );
|
| 561 | doc.Print();
|
| 562 | }
|
| 563 |
|
| 564 | {
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 565 | const char* test = "<?xml version='1.0'?><a.elem xmi.version='2.0'/>";
|
| 566 |
|
| 567 | XMLDocument doc;
|
| 568 | doc.Parse( test );
|
| 569 | XMLTest( "dot in names", doc.Error(), 0);
|
| 570 | XMLTest( "dot in names", doc.FirstChildElement()->Name(), "a.elem" );
|
| 571 | XMLTest( "dot in names", doc.FirstChildElement()->Attribute( "xmi.version" ), "2.0" );
|
| 572 | }
|
| 573 |
|
| 574 | {
|
| 575 | const char* test = "<element><Name>1.1 Start easy ignore fin thickness
</Name></element>";
|
| 576 |
|
| 577 | XMLDocument doc;
|
| 578 | doc.Parse( test );
|
| 579 |
|
| 580 | XMLText* text = doc.FirstChildElement()->FirstChildElement()->FirstChild()->ToText();
|
| 581 | XMLTest( "Entity with one digit.",
|
| 582 | text->Value(), "1.1 Start easy ignore fin thickness\n",
|
| 583 | false );
|
| 584 | }
|
| 585 |
|
| 586 | {
|
| 587 | // DOCTYPE not preserved (950171)
|
| 588 | //
|
| 589 | const char* doctype =
|
| 590 | "<?xml version=\"1.0\" ?>"
|
| 591 | "<!DOCTYPE PLAY SYSTEM 'play.dtd'>"
|
| 592 | "<!ELEMENT title (#PCDATA)>"
|
| 593 | "<!ELEMENT books (title,authors)>"
|
| 594 | "<element />";
|
| 595 |
|
| 596 | XMLDocument doc;
|
| 597 | doc.Parse( doctype );
|
Bruno Dias | a2d4e6e | 2012-05-07 04:58:11 -0300 | [diff] [blame] | 598 | doc.SaveFile( "resources/test7.xml" );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 599 | doc.DeleteChild( doc.RootElement() );
|
Bruno Dias | a2d4e6e | 2012-05-07 04:58:11 -0300 | [diff] [blame] | 600 | doc.LoadFile( "resources/test7.xml" );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 601 | doc.Print();
|
| 602 |
|
| 603 | const XMLUnknown* decl = doc.FirstChild()->NextSibling()->ToUnknown();
|
| 604 | XMLTest( "Correct value of unknown.", "DOCTYPE PLAY SYSTEM 'play.dtd'", decl->Value() );
|
| 605 |
|
| 606 | }
|
| 607 |
|
| 608 | {
|
| 609 | // Comments do not stream out correctly.
|
| 610 | const char* doctype =
|
| 611 | "<!-- Somewhat<evil> -->";
|
| 612 | XMLDocument doc;
|
| 613 | doc.Parse( doctype );
|
| 614 |
|
| 615 | XMLComment* comment = doc.FirstChild()->ToComment();
|
| 616 |
|
| 617 | XMLTest( "Comment formatting.", " Somewhat<evil> ", comment->Value() );
|
| 618 | }
|
| 619 | {
|
| 620 | // Double attributes
|
| 621 | const char* doctype = "<element attr='red' attr='blue' />";
|
| 622 |
|
| 623 | XMLDocument doc;
|
| 624 | doc.Parse( doctype );
|
| 625 |
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 626 | 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] | 627 | doc.PrintError();
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 628 | }
|
| 629 |
|
| 630 | {
|
| 631 | // Embedded null in stream.
|
| 632 | const char* doctype = "<element att\0r='red' attr='blue' />";
|
| 633 |
|
| 634 | XMLDocument doc;
|
| 635 | doc.Parse( doctype );
|
| 636 | XMLTest( "Embedded null throws error.", true, doc.Error() );
|
| 637 | }
|
| 638 |
|
| 639 | {
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 640 | // Empty documents should return TIXML_XML_ERROR_PARSING_EMPTY, bug 1070717
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 641 | const char* str = " ";
|
| 642 | XMLDocument doc;
|
| 643 | doc.Parse( str );
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 644 | XMLTest( "Empty document error", XML_ERROR_EMPTY_DOCUMENT, doc.ErrorID() );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 645 | }
|
| 646 |
|
| 647 | {
|
| 648 | // Low entities
|
| 649 | XMLDocument doc;
|
| 650 | doc.Parse( "<test></test>" );
|
| 651 | const char result[] = { 0x0e, 0 };
|
| 652 | XMLTest( "Low entities.", doc.FirstChildElement()->GetText(), result );
|
| 653 | doc.Print();
|
| 654 | }
|
| 655 |
|
| 656 | {
|
| 657 | // Attribute values with trailing quotes not handled correctly
|
| 658 | XMLDocument doc;
|
| 659 | doc.Parse( "<foo attribute=bar\" />" );
|
| 660 | XMLTest( "Throw error with bad end quotes.", doc.Error(), true );
|
| 661 | }
|
| 662 |
|
| 663 | {
|
| 664 | // [ 1663758 ] Failure to report error on bad XML
|
| 665 | XMLDocument xml;
|
| 666 | xml.Parse("<x>");
|
| 667 | XMLTest("Missing end tag at end of input", xml.Error(), true);
|
| 668 | xml.Parse("<x> ");
|
| 669 | XMLTest("Missing end tag with trailing whitespace", xml.Error(), true);
|
| 670 | xml.Parse("<x></y>");
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 671 | XMLTest("Mismatched tags", xml.ErrorID(), XML_ERROR_MISMATCHED_ELEMENT);
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 672 | }
|
| 673 |
|
| 674 |
|
| 675 | {
|
| 676 | // [ 1475201 ] TinyXML parses entities in comments
|
| 677 | XMLDocument xml;
|
| 678 | xml.Parse("<!-- declarations for <head> & <body> -->"
|
| 679 | "<!-- far & away -->" );
|
| 680 |
|
| 681 | XMLNode* e0 = xml.FirstChild();
|
| 682 | XMLNode* e1 = e0->NextSibling();
|
| 683 | XMLComment* c0 = e0->ToComment();
|
| 684 | XMLComment* c1 = e1->ToComment();
|
| 685 |
|
| 686 | XMLTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
|
| 687 | XMLTest( "Comments ignore entities.", " far & away ", c1->Value(), true );
|
| 688 | }
|
| 689 |
|
| 690 | {
|
| 691 | XMLDocument xml;
|
| 692 | xml.Parse( "<Parent>"
|
| 693 | "<child1 att=''/>"
|
| 694 | "<!-- With this comment, child2 will not be parsed! -->"
|
| 695 | "<child2 att=''/>"
|
| 696 | "</Parent>" );
|
| 697 | xml.Print();
|
| 698 |
|
| 699 | int count = 0;
|
| 700 |
|
| 701 | for( XMLNode* ele = xml.FirstChildElement( "Parent" )->FirstChild();
|
| 702 | ele;
|
| 703 | ele = ele->NextSibling() )
|
| 704 | {
|
| 705 | ++count;
|
| 706 | }
|
| 707 |
|
| 708 | XMLTest( "Comments iterate correctly.", 3, count );
|
| 709 | }
|
| 710 |
|
| 711 | {
|
| 712 | // trying to repro ]1874301]. If it doesn't go into an infinite loop, all is well.
|
| 713 | unsigned char buf[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?><feed><![CDATA[Test XMLblablablalblbl";
|
| 714 | buf[60] = 239;
|
| 715 | buf[61] = 0;
|
| 716 |
|
| 717 | XMLDocument doc;
|
| 718 | doc.Parse( (const char*)buf);
|
| 719 | }
|
| 720 |
|
| 721 |
|
| 722 | {
|
| 723 | // bug 1827248 Error while parsing a little bit malformed file
|
| 724 | // Actually not malformed - should work.
|
| 725 | XMLDocument xml;
|
| 726 | xml.Parse( "<attributelist> </attributelist >" );
|
| 727 | XMLTest( "Handle end tag whitespace", false, xml.Error() );
|
| 728 | }
|
| 729 |
|
| 730 | {
|
| 731 | // This one must not result in an infinite loop
|
| 732 | XMLDocument xml;
|
| 733 | xml.Parse( "<infinite>loop" );
|
| 734 | XMLTest( "Infinite loop test.", true, true );
|
| 735 | }
|
| 736 | #endif
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 737 | {
|
| 738 | const char* pub = "<?xml version='1.0'?> <element><sub/></element> <!--comment--> <!DOCTYPE>";
|
| 739 | XMLDocument doc;
|
| 740 | doc.Parse( pub );
|
| 741 |
|
| 742 | XMLDocument clone;
|
| 743 | for( const XMLNode* node=doc.FirstChild(); node; node=node->NextSibling() ) {
|
| 744 | XMLNode* copy = node->ShallowClone( &clone );
|
| 745 | clone.InsertEndChild( copy );
|
| 746 | }
|
| 747 |
|
| 748 | clone.Print();
|
| 749 |
|
| 750 | int count=0;
|
| 751 | const XMLNode* a=clone.FirstChild();
|
| 752 | const XMLNode* b=doc.FirstChild();
|
| 753 | for( ; a && b; a=a->NextSibling(), b=b->NextSibling() ) {
|
| 754 | ++count;
|
| 755 | XMLTest( "Clone and Equal", true, a->ShallowEqual( b ));
|
| 756 | }
|
| 757 | XMLTest( "Clone and Equal", 4, count );
|
| 758 | }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 759 |
|
Lee Thomason (grinliz) | a4a36ba | 2012-04-06 21:24:29 -0700 | [diff] [blame] | 760 | {
|
| 761 | // This shouldn't crash.
|
| 762 | XMLDocument doc;
|
| 763 | if(XML_NO_ERROR != doc.LoadFile( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ))
|
| 764 | {
|
| 765 | doc.PrintError();
|
| 766 | }
|
| 767 | XMLTest( "Error in snprinf handling.", true, doc.Error() );
|
| 768 | }
|
Lee Thomason | 5e3803c | 2012-04-16 08:57:05 -0700 | [diff] [blame] | 769 |
|
| 770 | {
|
| 771 | // Attribute ordering.
|
| 772 | static const char* xml = "<element attrib1=\"1\" attrib2=\"2\" attrib3=\"3\" />";
|
| 773 | XMLDocument doc;
|
| 774 | doc.Parse( xml );
|
| 775 | XMLElement* ele = doc.FirstChildElement();
|
| 776 |
|
| 777 | const XMLAttribute* a = ele->FirstAttribute();
|
| 778 | XMLTest( "Attribute order", "1", a->Value() );
|
| 779 | a = a->Next();
|
| 780 | XMLTest( "Attribute order", "2", a->Value() );
|
| 781 | a = a->Next();
|
| 782 | XMLTest( "Attribute order", "3", a->Value() );
|
| 783 | XMLTest( "Attribute order", "attrib3", a->Name() );
|
| 784 |
|
| 785 | ele->DeleteAttribute( "attrib2" );
|
| 786 | a = ele->FirstAttribute();
|
| 787 | XMLTest( "Attribute order", "1", a->Value() );
|
| 788 | a = a->Next();
|
| 789 | XMLTest( "Attribute order", "3", a->Value() );
|
| 790 |
|
| 791 | ele->DeleteAttribute( "attrib1" );
|
| 792 | ele->DeleteAttribute( "attrib3" );
|
| 793 | XMLTest( "Attribute order (empty)", false, ele->FirstAttribute() ? true : false );
|
| 794 | }
|
Lee Thomason (grinliz) | a4a36ba | 2012-04-06 21:24:29 -0700 | [diff] [blame] | 795 |
|
Lee Thomason (grinliz) | 390e978 | 2012-07-01 21:22:53 -0700 | [diff] [blame] | 796 | {
|
| 797 | // Make sure an attribute with a space in it succeeds.
|
Lee Thomason | 78a773d | 2012-07-02 10:10:19 -0700 | [diff] [blame^] | 798 | static const char* xml0 = "<element attribute1= \"Test Attribute\"/>";
|
| 799 | static const char* xml1 = "<element attribute1 =\"Test Attribute\"/>";
|
| 800 | static const char* xml2 = "<element attribute1 = \"Test Attribute\"/>";
|
| 801 | XMLDocument doc0;
|
| 802 | doc0.Parse( xml0 );
|
| 803 | XMLDocument doc1;
|
| 804 | doc1.Parse( xml1 );
|
| 805 | XMLDocument doc2;
|
| 806 | doc2.Parse( xml2 );
|
Lee Thomason (grinliz) | 390e978 | 2012-07-01 21:22:53 -0700 | [diff] [blame] | 807 |
|
Lee Thomason | 78a773d | 2012-07-02 10:10:19 -0700 | [diff] [blame^] | 808 | XMLElement* ele = 0;
|
| 809 | ele = doc0.FirstChildElement();
|
| 810 | XMLTest( "Attribute with space #1", "Test Attribute", ele->Attribute( "attribute1" ) );
|
| 811 | ele = doc1.FirstChildElement();
|
| 812 | XMLTest( "Attribute with space #2", "Test Attribute", ele->Attribute( "attribute1" ) );
|
| 813 | ele = doc2.FirstChildElement();
|
| 814 | XMLTest( "Attribute with space #3", "Test Attribute", ele->Attribute( "attribute1" ) );
|
Lee Thomason (grinliz) | 390e978 | 2012-07-01 21:22:53 -0700 | [diff] [blame] | 815 | }
|
| 816 |
|
| 817 | {
|
| 818 | // Make sure we don't go into an infinite loop.
|
| 819 | static const char* xml = "<doc><element attribute='attribute'/><element attribute='attribute'/></doc>";
|
| 820 | XMLDocument doc;
|
| 821 | doc.Parse( xml );
|
| 822 | XMLElement* ele0 = doc.FirstChildElement()->FirstChildElement();
|
| 823 | XMLElement* ele1 = ele0->NextSiblingElement();
|
| 824 | bool equal = ele0->ShallowEqual( ele1 );
|
| 825 |
|
| 826 | XMLTest( "Infinite loop in shallow equal.", true, equal );
|
| 827 | }
|
| 828 |
|
Lee Thomason | 5708f81 | 2012-03-28 17:46:41 -0700 | [diff] [blame] | 829 | // -------- Handles ------------
|
| 830 | {
|
| 831 | static const char* xml = "<element attrib='bar'><sub>Text</sub></element>";
|
| 832 | XMLDocument doc;
|
| 833 | doc.Parse( xml );
|
Lee Thomason | 5708f81 | 2012-03-28 17:46:41 -0700 | [diff] [blame] | 834 |
|
| 835 | XMLElement* ele = XMLHandle( doc ).FirstChildElement( "element" ).FirstChild().ToElement();
|
| 836 | XMLTest( "Handle, success, mutable", ele->Value(), "sub" );
|
| 837 |
|
Lee Thomason (grinliz) | ae209f6 | 2012-04-04 22:00:07 -0700 | [diff] [blame] | 838 | XMLHandle docH( doc );
|
| 839 | ele = docH.FirstChildElement( "none" ).FirstChildElement( "element" ).ToElement();
|
Lee Thomason | d0b19df | 2012-04-12 08:41:37 -0700 | [diff] [blame] | 840 | XMLTest( "Handle, dne, mutable", false, ele != 0 );
|
Lee Thomason | 5708f81 | 2012-03-28 17:46:41 -0700 | [diff] [blame] | 841 | }
|
| 842 |
|
Lee Thomason (grinliz) | ae209f6 | 2012-04-04 22:00:07 -0700 | [diff] [blame] | 843 | {
|
| 844 | static const char* xml = "<element attrib='bar'><sub>Text</sub></element>";
|
| 845 | XMLDocument doc;
|
| 846 | doc.Parse( xml );
|
| 847 | XMLConstHandle docH( doc );
|
| 848 |
|
| 849 | const XMLElement* ele = docH.FirstChildElement( "element" ).FirstChild().ToElement();
|
| 850 | XMLTest( "Handle, success, const", ele->Value(), "sub" );
|
| 851 |
|
| 852 | ele = docH.FirstChildElement( "none" ).FirstChildElement( "element" ).ToElement();
|
Lee Thomason | d0b19df | 2012-04-12 08:41:37 -0700 | [diff] [blame] | 853 | XMLTest( "Handle, dne, const", false, ele != 0 );
|
Lee Thomason (grinliz) | ae209f6 | 2012-04-04 22:00:07 -0700 | [diff] [blame] | 854 | }
|
Lee Thomason | f68c438 | 2012-04-28 14:37:11 -0700 | [diff] [blame] | 855 | {
|
| 856 | // Default Declaration & BOM
|
| 857 | XMLDocument doc;
|
| 858 | doc.InsertEndChild( doc.NewDeclaration() );
|
| 859 | doc.SetBOM( true );
|
| 860 |
|
| 861 | XMLPrinter printer;
|
| 862 | doc.Print( &printer );
|
| 863 |
|
| 864 | static const char* result = "\xef\xbb\xbf<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
|
| 865 | XMLTest( "BOM and default declaration", printer.CStr(), result, false );
|
Lee Thomason (grinliz) | 48ea0bc | 2012-05-26 14:41:14 -0700 | [diff] [blame] | 866 | XMLTest( "CStrSize", printer.CStrSize(), 42, false );
|
Lee Thomason | f68c438 | 2012-04-28 14:37:11 -0700 | [diff] [blame] | 867 | }
|
Lee Thomason (grinliz) | ae209f6 | 2012-04-04 22:00:07 -0700 | [diff] [blame] | 868 |
|
| 869 |
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 870 | // ----------- Performance tracking --------------
|
| 871 | {
|
| 872 | #if defined( _MSC_VER )
|
| 873 | __int64 start, end, freq;
|
| 874 | QueryPerformanceFrequency( (LARGE_INTEGER*) &freq );
|
| 875 | #endif
|
| 876 |
|
| 877 | #if defined(_MSC_VER)
|
| 878 | #pragma warning ( push )
|
| 879 | #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
|
| 880 | #endif
|
Bruno Dias | a2d4e6e | 2012-05-07 04:58:11 -0300 | [diff] [blame] | 881 | FILE* fp = fopen( "resources/dream.xml", "r" );
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 882 | #if defined(_MSC_VER)
|
| 883 | #pragma warning ( pop )
|
| 884 | #endif
|
| 885 | fseek( fp, 0, SEEK_END );
|
| 886 | long size = ftell( fp );
|
| 887 | fseek( fp, 0, SEEK_SET );
|
| 888 |
|
| 889 | char* mem = new char[size+1];
|
| 890 | fread( mem, size, 1, fp );
|
| 891 | fclose( fp );
|
| 892 | mem[size] = 0;
|
| 893 |
|
| 894 | #if defined( _MSC_VER )
|
| 895 | QueryPerformanceCounter( (LARGE_INTEGER*) &start );
|
| 896 | #else
|
| 897 | clock_t cstart = clock();
|
| 898 | #endif
|
| 899 | static const int COUNT = 10;
|
| 900 | for( int i=0; i<COUNT; ++i ) {
|
| 901 | XMLDocument doc;
|
| 902 | doc.Parse( mem );
|
| 903 | }
|
| 904 | #if defined( _MSC_VER )
|
| 905 | QueryPerformanceCounter( (LARGE_INTEGER*) &end );
|
| 906 | #else
|
| 907 | clock_t cend = clock();
|
| 908 | #endif
|
| 909 |
|
| 910 | delete [] mem;
|
| 911 |
|
| 912 | static const char* note =
|
| 913 | #ifdef DEBUG
|
| 914 | "DEBUG";
|
| 915 | #else
|
| 916 | "Release";
|
| 917 | #endif
|
| 918 |
|
| 919 | #if defined( _MSC_VER )
|
| 920 | printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, 1000.0 * (double)(end-start) / ( (double)freq * (double)COUNT) );
|
| 921 | #else
|
| 922 | printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, (double)(cend - cstart)/(double)COUNT );
|
| 923 | #endif
|
| 924 | }
|
| 925 |
|
Lee Thomason (grinliz) | 7ca5558 | 2012-03-07 21:54:57 -0800 | [diff] [blame] | 926 | #if defined( _MSC_VER ) && defined( DEBUG )
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 927 | _CrtMemCheckpoint( &endMemState );
|
| 928 | //_CrtMemDumpStatistics( &endMemState );
|
| 929 |
|
| 930 | _CrtMemState diffMemState;
|
| 931 | _CrtMemDifference( &diffMemState, &startMemState, &endMemState );
|
| 932 | _CrtMemDumpStatistics( &diffMemState );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 933 | //printf( "new total=%d\n", gNewTotal );
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 934 | #endif
|
| 935 |
|
| 936 | printf ("\nPass %d, Fail %d\n", gPass, gFail);
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 937 | return 0;
|
Lee Thomason (grinliz) | 9b093cc | 2012-02-25 21:30:18 -0800 | [diff] [blame] | 938 | }
|