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 | e9699e6 | 2012-07-25 12:24:23 -0700 | [diff] [blame] | 8 | #include <direct.h> // _mkdir
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 9 | #include <crtdbg.h>
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 10 | #define WIN32_LEAN_AND_MEAN
|
| 11 | #include <windows.h>
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 12 | _CrtMemState startMemState;
|
| 13 | _CrtMemState endMemState;
|
Lee Thomason | e9699e6 | 2012-07-25 12:24:23 -0700 | [diff] [blame] | 14 | #else
|
| 15 | #include <sys/stat.h> // mkdir
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 16 | #endif
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 17 |
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 18 | using namespace tinyxml2;
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 19 | int gPass = 0;
|
| 20 | int gFail = 0;
|
| 21 |
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 22 |
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 23 | 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] | 24 | {
|
| 25 | bool pass = !strcmp( expected, found );
|
| 26 | if ( pass )
|
| 27 | printf ("[pass]");
|
| 28 | else
|
| 29 | printf ("[fail]");
|
| 30 |
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 31 | if ( !echo )
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 32 | printf (" %s\n", testString);
|
| 33 | else
|
| 34 | printf (" %s [%s][%s]\n", testString, expected, found);
|
| 35 |
|
| 36 | if ( pass )
|
| 37 | ++gPass;
|
| 38 | else
|
| 39 | ++gFail;
|
| 40 | return pass;
|
| 41 | }
|
| 42 |
|
| 43 |
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 44 | template< class T > bool XMLTest( const char* testString, T expected, T found, bool echo=true )
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 45 | {
|
| 46 | bool pass = ( expected == found );
|
| 47 | if ( pass )
|
| 48 | printf ("[pass]");
|
| 49 | else
|
| 50 | printf ("[fail]");
|
| 51 |
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 52 | if ( !echo )
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 53 | printf (" %s\n", testString);
|
| 54 | else
|
Lee Thomason | c831279 | 2012-07-16 12:44:41 -0700 | [diff] [blame] | 55 | printf (" %s [%d][%d]\n", testString, static_cast<int>(expected), static_cast<int>(found) );
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 56 |
|
| 57 | if ( pass )
|
| 58 | ++gPass;
|
| 59 | else
|
| 60 | ++gFail;
|
| 61 | return pass;
|
| 62 | }
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 63 |
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 64 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 65 | void NullLineEndings( char* p )
|
| 66 | {
|
| 67 | while( p && *p ) {
|
| 68 | if ( *p == '\n' || *p == '\r' ) {
|
| 69 | *p = 0;
|
| 70 | return;
|
| 71 | }
|
| 72 | ++p;
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 |
|
Lee Thomason (grinliz) | 6a22be2 | 2012-04-04 12:39:05 -0700 | [diff] [blame] | 77 | // Comments in the header. (Don't know how to get Doxygen to read comments in this file.)
|
| 78 | int example_1()
|
| 79 | {
|
| 80 | XMLDocument doc;
|
Bruno Dias | a2d4e6e | 2012-05-07 04:58:11 -0300 | [diff] [blame] | 81 | doc.LoadFile( "resources/dream.xml" );
|
Lee Thomason (grinliz) | 6a22be2 | 2012-04-04 12:39:05 -0700 | [diff] [blame] | 82 |
|
| 83 | return doc.ErrorID();
|
| 84 | }
|
| 85 |
|
| 86 |
|
| 87 | // Comments in the header. (Don't know how to get Doxygen to read comments in this file.)
|
| 88 | int example_2()
|
| 89 | {
|
| 90 | static const char* xml = "<element/>";
|
| 91 | XMLDocument doc;
|
| 92 | doc.Parse( xml );
|
| 93 |
|
| 94 | return doc.ErrorID();
|
| 95 | }
|
| 96 |
|
| 97 |
|
| 98 | int example_3()
|
| 99 | {
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 100 | static const char* xml =
|
Lee Thomason (grinliz) | a4a36ba | 2012-04-06 21:24:29 -0700 | [diff] [blame] | 101 | "<?xml version=\"1.0\"?>"
|
| 102 | "<!DOCTYPE PLAY SYSTEM \"play.dtd\">"
|
| 103 | "<PLAY>"
|
| 104 | "<TITLE>A Midsummer Night's Dream</TITLE>"
|
| 105 | "</PLAY>";
|
Lee Thomason (grinliz) | 6a22be2 | 2012-04-04 12:39:05 -0700 | [diff] [blame] | 106 |
|
| 107 | XMLDocument doc;
|
| 108 | doc.Parse( xml );
|
| 109 |
|
| 110 | XMLElement* titleElement = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" );
|
| 111 | const char* title = titleElement->GetText();
|
| 112 | printf( "Name of play (1): %s\n", title );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 113 |
|
Lee Thomason (grinliz) | 6a22be2 | 2012-04-04 12:39:05 -0700 | [diff] [blame] | 114 | XMLText* textNode = titleElement->FirstChild()->ToText();
|
| 115 | title = textNode->Value();
|
| 116 | printf( "Name of play (2): %s\n", title );
|
| 117 |
|
| 118 | return doc.ErrorID();
|
| 119 | }
|
| 120 |
|
| 121 |
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 122 | bool example_4()
|
| 123 | {
|
| 124 | static const char* xml =
|
| 125 | "<information>"
|
| 126 | " <attributeApproach v='2' />"
|
| 127 | " <textApproach>"
|
| 128 | " <v>2</v>"
|
| 129 | " </textApproach>"
|
| 130 | "</information>";
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 131 |
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 132 | XMLDocument doc;
|
| 133 | doc.Parse( xml );
|
| 134 |
|
| 135 | int v0 = 0;
|
| 136 | int v1 = 0;
|
| 137 |
|
| 138 | XMLElement* attributeApproachElement = doc.FirstChildElement()->FirstChildElement( "attributeApproach" );
|
| 139 | attributeApproachElement->QueryIntAttribute( "v", &v0 );
|
| 140 |
|
| 141 | XMLElement* textApproachElement = doc.FirstChildElement()->FirstChildElement( "textApproach" );
|
| 142 | textApproachElement->FirstChildElement( "v" )->QueryIntText( &v1 );
|
| 143 |
|
| 144 | printf( "Both values are the same: %d and %d\n", v0, v1 );
|
| 145 |
|
| 146 | return !doc.Error() && ( v0 == v1 );
|
| 147 | }
|
| 148 |
|
| 149 |
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 150 | int main( int /*argc*/, const char ** /*argv*/ )
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 151 | {
|
Lee Thomason (grinliz) | 0a4df40 | 2012-02-27 20:50:52 -0800 | [diff] [blame] | 152 | #if defined( _MSC_VER ) && defined( DEBUG )
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 153 | _CrtMemCheckpoint( &startMemState );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 154 | #endif
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 155 |
|
Lee Thomason | 7f7b162 | 2012-03-24 12:49:03 -0700 | [diff] [blame] | 156 | #if defined(_MSC_VER)
|
Lee Thomason | e9699e6 | 2012-07-25 12:24:23 -0700 | [diff] [blame] | 157 | _mkdir( "resources/out/" );
|
| 158 | #else
|
| 159 | mkdir( "resources/out/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
|
| 160 | #endif
|
Arkadiy Shapkin | ff72d1f | 2012-07-24 00:24:07 +0400 | [diff] [blame] | 161 |
|
Bruno Dias | a2d4e6e | 2012-05-07 04:58:11 -0300 | [diff] [blame] | 162 | FILE* fp = fopen( "resources/dream.xml", "r" );
|
Lee Thomason | 7f7b162 | 2012-03-24 12:49:03 -0700 | [diff] [blame] | 163 | if ( !fp ) {
|
| 164 | printf( "Error opening test file 'dream.xml'.\n"
|
| 165 | "Is your working directory the same as where \n"
|
| 166 | "the xmltest.cpp and dream.xml file are?\n\n"
|
| 167 | #if defined( _MSC_VER )
|
| 168 | "In windows Visual Studio you may need to set\n"
|
| 169 | "Properties->Debugging->Working Directory to '..'\n"
|
| 170 | #endif
|
| 171 | );
|
| 172 | exit( 1 );
|
| 173 | }
|
| 174 | fclose( fp );
|
| 175 |
|
Lee Thomason (grinliz) | 6a22be2 | 2012-04-04 12:39:05 -0700 | [diff] [blame] | 176 | XMLTest( "Example-1", 0, example_1() );
|
| 177 | XMLTest( "Example-2", 0, example_2() );
|
| 178 | XMLTest( "Example-3", 0, example_3() );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 179 | XMLTest( "Example-4", true, example_4() );
|
Lee Thomason | 87e475a | 2012-03-20 11:55:29 -0700 | [diff] [blame] | 180 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 181 | /* ------ Example 2: Lookup information. ---- */
|
Lee Thomason | 87e475a | 2012-03-20 11:55:29 -0700 | [diff] [blame] | 182 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 183 | {
|
Lee Thomason | 43f5930 | 2012-02-06 18:18:11 -0800 | [diff] [blame] | 184 | static const char* test[] = { "<element />",
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 185 | "<element></element>",
|
Lee Thomason | 43f5930 | 2012-02-06 18:18:11 -0800 | [diff] [blame] | 186 | "<element><subelement/></element>",
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 187 | "<element><subelement></subelement></element>",
|
| 188 | "<element><subelement><subsub/></subelement></element>",
|
| 189 | "<!--comment beside elements--><element><subelement></subelement></element>",
|
| 190 | "<!--comment beside elements, this time with spaces--> \n <element> <subelement> \n </subelement> </element>",
|
| 191 | "<element attrib1='foo' attrib2=\"bar\" ></element>",
|
| 192 | "<element attrib1='foo' attrib2=\"bar\" ><subelement attrib3='yeehaa' /></element>",
|
Lee Thomason | 43f5930 | 2012-02-06 18:18:11 -0800 | [diff] [blame] | 193 | "<element>Text inside element.</element>",
|
| 194 | "<element><b></b></element>",
|
| 195 | "<element>Text inside and <b>bolded</b> in the element.</element>",
|
| 196 | "<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] | 197 | "<element>This & That.</element>",
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 198 | "<element attrib='This<That' />",
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 199 | 0
|
| 200 | };
|
Lee Thomason | 6ee99fc | 2012-01-21 18:45:16 -0800 | [diff] [blame] | 201 | for( int i=0; test[i]; ++i ) {
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 202 | XMLDocument doc;
|
Lee Thomason | 6ee99fc | 2012-01-21 18:45:16 -0800 | [diff] [blame] | 203 | doc.Parse( test[i] );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 204 | doc.Print();
|
Lee Thomason | ec975ce | 2012-01-23 11:42:06 -0800 | [diff] [blame] | 205 | printf( "----------------------------------------------\n" );
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 206 | }
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 207 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 208 | #if 1
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 209 | {
|
| 210 | static const char* test = "<!--hello world\n"
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 211 | " line 2\r"
|
| 212 | " line 3\r\n"
|
| 213 | " line 4\n\r"
|
| 214 | " line 5\r-->";
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 215 |
|
| 216 | XMLDocument doc;
|
| 217 | doc.Parse( test );
|
| 218 | doc.Print();
|
| 219 | }
|
| 220 |
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 221 | {
|
| 222 | static const char* test = "<element>Text before.</element>";
|
| 223 | XMLDocument doc;
|
| 224 | doc.Parse( test );
|
| 225 | XMLElement* root = doc.FirstChildElement();
|
| 226 | XMLElement* newElement = doc.NewElement( "Subelement" );
|
| 227 | root->InsertEndChild( newElement );
|
| 228 | doc.Print();
|
| 229 | }
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 230 | {
|
| 231 | XMLDocument* doc = new XMLDocument();
|
| 232 | static const char* test = "<element><sub/></element>";
|
| 233 | doc->Parse( test );
|
| 234 | delete doc;
|
| 235 | }
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 236 | {
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 237 | // Test: Programmatic DOM
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 238 | // Build:
|
| 239 | // <element>
|
| 240 | // <!--comment-->
|
| 241 | // <sub attrib="1" />
|
| 242 | // <sub attrib="2" />
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 243 | // <sub attrib="3" >& Text!</sub>
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 244 | // <element>
|
| 245 |
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 246 | XMLDocument* doc = new XMLDocument();
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 247 | XMLNode* element = doc->InsertEndChild( doc->NewElement( "element" ) );
|
| 248 |
|
| 249 | XMLElement* sub[3] = { doc->NewElement( "sub" ), doc->NewElement( "sub" ), doc->NewElement( "sub" ) };
|
| 250 | for( int i=0; i<3; ++i ) {
|
| 251 | sub[i]->SetAttribute( "attrib", i );
|
| 252 | }
|
| 253 | element->InsertEndChild( sub[2] );
|
| 254 | XMLNode* comment = element->InsertFirstChild( doc->NewComment( "comment" ) );
|
| 255 | element->InsertAfterChild( comment, sub[0] );
|
| 256 | element->InsertAfterChild( sub[0], sub[1] );
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 257 | sub[2]->InsertFirstChild( doc->NewText( "& Text!" ));
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 258 | doc->Print();
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 259 | XMLTest( "Programmatic DOM", "comment", doc->FirstChildElement( "element" )->FirstChild()->Value() );
|
| 260 | XMLTest( "Programmatic DOM", "0", doc->FirstChildElement( "element" )->FirstChildElement()->Attribute( "attrib" ) );
|
| 261 | XMLTest( "Programmatic DOM", 2, doc->FirstChildElement()->LastChildElement( "sub" )->IntAttribute( "attrib" ) );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 262 | XMLTest( "Programmatic DOM", "& Text!",
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 263 | doc->FirstChildElement()->LastChildElement( "sub" )->FirstChild()->ToText()->Value() );
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 264 |
|
| 265 | // And now deletion:
|
| 266 | element->DeleteChild( sub[2] );
|
| 267 | doc->DeleteNode( comment );
|
| 268 |
|
| 269 | element->FirstChildElement()->SetAttribute( "attrib", true );
|
| 270 | element->LastChildElement()->DeleteAttribute( "attrib" );
|
| 271 |
|
| 272 | XMLTest( "Programmatic DOM", true, doc->FirstChildElement()->FirstChildElement()->BoolAttribute( "attrib" ) );
|
| 273 | int value = 10;
|
| 274 | int result = doc->FirstChildElement()->LastChildElement()->QueryIntAttribute( "attrib", &value );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 275 | XMLTest( "Programmatic DOM", result, (int)XML_NO_ATTRIBUTE );
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 276 | XMLTest( "Programmatic DOM", value, 10 );
|
| 277 |
|
| 278 | doc->Print();
|
| 279 |
|
Lee Thomason | 7b1b86a | 2012-06-04 17:01:38 -0700 | [diff] [blame] | 280 | {
|
| 281 | XMLPrinter streamer;
|
| 282 | doc->Print( &streamer );
|
| 283 | printf( "%s", streamer.CStr() );
|
| 284 | }
|
| 285 | {
|
| 286 | XMLPrinter streamer( 0, true );
|
| 287 | doc->Print( &streamer );
|
| 288 | XMLTest( "Compact mode", "<element><sub attrib=\"1\"/><sub/></element>", streamer.CStr(), false );
|
| 289 | }
|
Lee Thomason (grinliz) | 6b8b012 | 2012-09-08 21:21:00 -0700 | [diff] [blame] | 290 | doc->SaveFile( "./resources/out/pretty.xml" );
|
| 291 | doc->SaveFile( "./resources/out/compact.xml", true );
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 292 | delete doc;
|
| 293 | }
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 294 | {
|
| 295 | // Test: Dream
|
| 296 | // XML1 : 1,187,569 bytes in 31,209 allocations
|
| 297 | // XML2 : 469,073 bytes in 323 allocations
|
| 298 | //int newStart = gNew;
|
| 299 | XMLDocument doc;
|
Bruno Dias | a2d4e6e | 2012-05-07 04:58:11 -0300 | [diff] [blame] | 300 | doc.LoadFile( "resources/dream.xml" );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 301 |
|
Arkadiy Shapkin | ff72d1f | 2012-07-24 00:24:07 +0400 | [diff] [blame] | 302 | doc.SaveFile( "resources/out/dreamout.xml" );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 303 | doc.PrintError();
|
| 304 |
|
| 305 | XMLTest( "Dream", "xml version=\"1.0\"",
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 306 | doc.FirstChild()->ToDeclaration()->Value() );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 307 | XMLTest( "Dream", true, doc.FirstChild()->NextSibling()->ToUnknown() ? true : false );
|
| 308 | XMLTest( "Dream", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
|
| 309 | doc.FirstChild()->NextSibling()->ToUnknown()->Value() );
|
| 310 | XMLTest( "Dream", "And Robin shall restore amends.",
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 311 | doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 312 | XMLTest( "Dream", "And Robin shall restore amends.",
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 313 | doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 314 |
|
| 315 | XMLDocument doc2;
|
Arkadiy Shapkin | ff72d1f | 2012-07-24 00:24:07 +0400 | [diff] [blame] | 316 | doc2.LoadFile( "resources/out/dreamout.xml" );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 317 | XMLTest( "Dream-out", "xml version=\"1.0\"",
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 318 | doc2.FirstChild()->ToDeclaration()->Value() );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 319 | XMLTest( "Dream-out", true, doc2.FirstChild()->NextSibling()->ToUnknown() ? true : false );
|
| 320 | XMLTest( "Dream-out", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
|
| 321 | doc2.FirstChild()->NextSibling()->ToUnknown()->Value() );
|
| 322 | XMLTest( "Dream-out", "And Robin shall restore amends.",
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 323 | doc2.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 324 |
|
| 325 | //gNewTotal = gNew - newStart;
|
| 326 | }
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 327 |
|
| 328 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 329 | {
|
| 330 | const char* error = "<?xml version=\"1.0\" standalone=\"no\" ?>\n"
|
| 331 | "<passages count=\"006\" formatversion=\"20020620\">\n"
|
| 332 | " <wrong error>\n"
|
| 333 | "</passages>";
|
| 334 |
|
| 335 | XMLDocument doc;
|
| 336 | doc.Parse( error );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 337 | XMLTest( "Bad XML", doc.ErrorID(), (int)XML_ERROR_PARSING_ATTRIBUTE );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 338 | }
|
| 339 |
|
| 340 | {
|
| 341 | const char* str = "<doc attr0='1' attr1='2.0' attr2='foo' />";
|
| 342 |
|
| 343 | XMLDocument doc;
|
| 344 | doc.Parse( str );
|
| 345 |
|
| 346 | XMLElement* ele = doc.FirstChildElement();
|
| 347 |
|
| 348 | int iVal, result;
|
| 349 | double dVal;
|
| 350 |
|
| 351 | result = ele->QueryDoubleAttribute( "attr0", &dVal );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 352 | XMLTest( "Query attribute: int as double", result, (int)XML_NO_ERROR );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 353 | XMLTest( "Query attribute: int as double", (int)dVal, 1 );
|
| 354 | result = ele->QueryDoubleAttribute( "attr1", &dVal );
|
| 355 | XMLTest( "Query attribute: double as double", (int)dVal, 2 );
|
| 356 | result = ele->QueryIntAttribute( "attr1", &iVal );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 357 | XMLTest( "Query attribute: double as int", result, (int)XML_NO_ERROR );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 358 | XMLTest( "Query attribute: double as int", iVal, 2 );
|
| 359 | result = ele->QueryIntAttribute( "attr2", &iVal );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 360 | XMLTest( "Query attribute: not a number", result, (int)XML_WRONG_ATTRIBUTE_TYPE );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 361 | result = ele->QueryIntAttribute( "bar", &iVal );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 362 | XMLTest( "Query attribute: does not exist", result, (int)XML_NO_ATTRIBUTE );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 363 | }
|
| 364 |
|
| 365 | {
|
| 366 | const char* str = "<doc/>";
|
| 367 |
|
| 368 | XMLDocument doc;
|
| 369 | doc.Parse( str );
|
| 370 |
|
| 371 | XMLElement* ele = doc.FirstChildElement();
|
| 372 |
|
| 373 | int iVal;
|
| 374 | double dVal;
|
| 375 |
|
| 376 | ele->SetAttribute( "str", "strValue" );
|
| 377 | ele->SetAttribute( "int", 1 );
|
| 378 | ele->SetAttribute( "double", -1.0 );
|
| 379 |
|
| 380 | const char* cStr = ele->Attribute( "str" );
|
| 381 | ele->QueryIntAttribute( "int", &iVal );
|
| 382 | ele->QueryDoubleAttribute( "double", &dVal );
|
| 383 |
|
Lee Thomason | 8ba7f7d | 2012-03-24 13:04:04 -0700 | [diff] [blame] | 384 | XMLTest( "Attribute match test", ele->Attribute( "str", "strValue" ), "strValue" );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 385 | XMLTest( "Attribute round trip. c-string.", "strValue", cStr );
|
| 386 | XMLTest( "Attribute round trip. int.", 1, iVal );
|
| 387 | XMLTest( "Attribute round trip. double.", -1, (int)dVal );
|
| 388 | }
|
| 389 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 390 | {
|
| 391 | XMLDocument doc;
|
Bruno Dias | a2d4e6e | 2012-05-07 04:58:11 -0300 | [diff] [blame] | 392 | doc.LoadFile( "resources/utf8test.xml" );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 393 |
|
| 394 | // Get the attribute "value" from the "Russian" element and check it.
|
| 395 | XMLElement* element = doc.FirstChildElement( "document" )->FirstChildElement( "Russian" );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 396 | const unsigned char correctValue[] = { 0xd1U, 0x86U, 0xd0U, 0xb5U, 0xd0U, 0xbdU, 0xd0U, 0xbdU,
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 397 | 0xd0U, 0xbeU, 0xd1U, 0x81U, 0xd1U, 0x82U, 0xd1U, 0x8cU, 0 };
|
| 398 |
|
| 399 | XMLTest( "UTF-8: Russian value.", (const char*)correctValue, element->Attribute( "value" ) );
|
| 400 |
|
| 401 | const unsigned char russianElementName[] = { 0xd0U, 0xa0U, 0xd1U, 0x83U,
|
| 402 | 0xd1U, 0x81U, 0xd1U, 0x81U,
|
| 403 | 0xd0U, 0xbaU, 0xd0U, 0xb8U,
|
| 404 | 0xd0U, 0xb9U, 0 };
|
| 405 | const char russianText[] = "<\xD0\xB8\xD0\xBC\xD0\xB5\xD0\xB5\xD1\x82>";
|
| 406 |
|
| 407 | XMLText* text = doc.FirstChildElement( "document" )->FirstChildElement( (const char*) russianElementName )->FirstChild()->ToText();
|
| 408 | XMLTest( "UTF-8: Browsing russian element name.",
|
| 409 | russianText,
|
| 410 | text->Value() );
|
| 411 |
|
| 412 | // Now try for a round trip.
|
Arkadiy Shapkin | ff72d1f | 2012-07-24 00:24:07 +0400 | [diff] [blame] | 413 | doc.SaveFile( "resources/out/utf8testout.xml" );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 414 |
|
| 415 | // Check the round trip.
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 416 | int okay = 0;
|
| 417 |
|
Thomas Roß | a6dd8c6 | 2012-07-26 20:42:18 +0200 | [diff] [blame] | 418 | FILE* saved = fopen( "resources/out/utf8testout.xml", "r" );
|
Bruno Dias | a2d4e6e | 2012-05-07 04:58:11 -0300 | [diff] [blame] | 419 | FILE* verify = fopen( "resources/utf8testverify.xml", "r" );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 420 |
|
| 421 | if ( saved && verify )
|
| 422 | {
|
| 423 | okay = 1;
|
PKEuS | c28ba3a | 2012-07-16 03:08:47 -0700 | [diff] [blame] | 424 | char verifyBuf[256];
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 425 | while ( fgets( verifyBuf, 256, verify ) )
|
| 426 | {
|
PKEuS | c28ba3a | 2012-07-16 03:08:47 -0700 | [diff] [blame] | 427 | char savedBuf[256];
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 428 | fgets( savedBuf, 256, saved );
|
| 429 | NullLineEndings( verifyBuf );
|
| 430 | NullLineEndings( savedBuf );
|
| 431 |
|
| 432 | if ( strcmp( verifyBuf, savedBuf ) )
|
| 433 | {
|
| 434 | printf( "verify:%s<\n", verifyBuf );
|
| 435 | printf( "saved :%s<\n", savedBuf );
|
| 436 | okay = 0;
|
| 437 | break;
|
| 438 | }
|
| 439 | }
|
| 440 | }
|
| 441 | if ( saved )
|
| 442 | fclose( saved );
|
| 443 | if ( verify )
|
| 444 | fclose( verify );
|
| 445 | XMLTest( "UTF-8: Verified multi-language round trip.", 1, okay );
|
| 446 | }
|
| 447 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 448 | // --------GetText()-----------
|
| 449 | {
|
| 450 | const char* str = "<foo>This is text</foo>";
|
| 451 | XMLDocument doc;
|
| 452 | doc.Parse( str );
|
| 453 | const XMLElement* element = doc.RootElement();
|
| 454 |
|
| 455 | XMLTest( "GetText() normal use.", "This is text", element->GetText() );
|
| 456 |
|
| 457 | str = "<foo><b>This is text</b></foo>";
|
| 458 | doc.Parse( str );
|
| 459 | element = doc.RootElement();
|
| 460 |
|
| 461 | XMLTest( "GetText() contained element.", element->GetText() == 0, true );
|
| 462 | }
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 463 |
|
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 | // ---------- CDATA ---------------
|
| 466 | {
|
| 467 | const char* str = "<xmlElement>"
|
| 468 | "<![CDATA["
|
| 469 | "I am > the rules!\n"
|
| 470 | "...since I make symbolic puns"
|
| 471 | "]]>"
|
| 472 | "</xmlElement>";
|
| 473 | XMLDocument doc;
|
| 474 | doc.Parse( str );
|
| 475 | doc.Print();
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 476 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 477 | XMLTest( "CDATA parse.", doc.FirstChildElement()->FirstChild()->Value(),
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 478 | "I am > the rules!\n...since I make symbolic puns",
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 479 | false );
|
| 480 | }
|
| 481 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 482 | // ----------- CDATA -------------
|
| 483 | {
|
| 484 | const char* str = "<xmlElement>"
|
| 485 | "<![CDATA["
|
| 486 | "<b>I am > the rules!</b>\n"
|
| 487 | "...since I make symbolic puns"
|
| 488 | "]]>"
|
| 489 | "</xmlElement>";
|
| 490 | XMLDocument doc;
|
| 491 | doc.Parse( str );
|
| 492 | doc.Print();
|
| 493 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 494 | XMLTest( "CDATA parse. [ tixml1:1480107 ]", doc.FirstChildElement()->FirstChild()->Value(),
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 495 | "<b>I am > the rules!</b>\n...since I make symbolic puns",
|
| 496 | false );
|
| 497 | }
|
| 498 |
|
| 499 | // InsertAfterChild causes crash.
|
| 500 | {
|
| 501 | // InsertBeforeChild and InsertAfterChild causes crash.
|
| 502 | XMLDocument doc;
|
| 503 | XMLElement* parent = doc.NewElement( "Parent" );
|
| 504 | doc.InsertFirstChild( parent );
|
| 505 |
|
| 506 | XMLElement* childText0 = doc.NewElement( "childText0" );
|
| 507 | XMLElement* childText1 = doc.NewElement( "childText1" );
|
| 508 |
|
| 509 | XMLNode* childNode0 = parent->InsertEndChild( childText0 );
|
| 510 | XMLNode* childNode1 = parent->InsertAfterChild( childNode0, childText1 );
|
| 511 |
|
| 512 | XMLTest( "Test InsertAfterChild on empty node. ", ( childNode1 == parent->LastChild() ), true );
|
| 513 | }
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 514 |
|
| 515 | {
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 516 | // Entities not being written correctly.
|
| 517 | // From Lynn Allen
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 518 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 519 | const char* passages =
|
| 520 | "<?xml version=\"1.0\" standalone=\"no\" ?>"
|
| 521 | "<passages count=\"006\" formatversion=\"20020620\">"
|
| 522 | "<psg context=\"Line 5 has "quotation marks" and 'apostrophe marks'."
|
| 523 | " It also has <, >, and &, as well as a fake copyright ©.\"> </psg>"
|
| 524 | "</passages>";
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 525 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 526 | XMLDocument doc;
|
| 527 | doc.Parse( passages );
|
| 528 | XMLElement* psg = doc.RootElement()->FirstChildElement();
|
| 529 | const char* context = psg->Attribute( "context" );
|
| 530 | 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] | 531 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 532 | XMLTest( "Entity transformation: read. ", expected, context, true );
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 533 |
|
Arkadiy Shapkin | ff72d1f | 2012-07-24 00:24:07 +0400 | [diff] [blame] | 534 | FILE* textfile = fopen( "resources/out/textfile.txt", "w" );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 535 | if ( textfile )
|
| 536 | {
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 537 | XMLPrinter streamer( textfile );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 538 | psg->Accept( &streamer );
|
| 539 | fclose( textfile );
|
| 540 | }
|
Thomas Roß | 0922b73 | 2012-09-23 16:31:22 +0200 | [diff] [blame] | 541 |
|
| 542 | textfile = fopen( "resources/out/textfile.txt", "r" );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 543 | TIXMLASSERT( textfile );
|
| 544 | if ( textfile )
|
| 545 | {
|
| 546 | char buf[ 1024 ];
|
| 547 | fgets( buf, 1024, textfile );
|
| 548 | XMLTest( "Entity transformation: write. ",
|
| 549 | "<psg context=\"Line 5 has "quotation marks" and 'apostrophe marks'."
|
| 550 | " It also has <, >, and &, as well as a fake copyright \xC2\xA9.\"/>\n",
|
| 551 | buf, false );
|
PKEuS | c28ba3a | 2012-07-16 03:08:47 -0700 | [diff] [blame] | 552 | fclose( textfile );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 553 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 554 | }
|
| 555 |
|
| 556 | {
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 557 | // Suppress entities.
|
| 558 | const char* passages =
|
| 559 | "<?xml version=\"1.0\" standalone=\"no\" ?>"
|
| 560 | "<passages count=\"006\" formatversion=\"20020620\">"
|
| 561 | "<psg context=\"Line 5 has "quotation marks" and 'apostrophe marks'.\">Crazy &ttk;</psg>"
|
| 562 | "</passages>";
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 563 |
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 564 | XMLDocument doc( false );
|
| 565 | doc.Parse( passages );
|
| 566 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 567 | XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->Attribute( "context" ),
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 568 | "Line 5 has "quotation marks" and 'apostrophe marks'." );
|
| 569 | XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->FirstChild()->Value(),
|
| 570 | "Crazy &ttk;" );
|
| 571 | doc.Print();
|
| 572 | }
|
| 573 |
|
| 574 | {
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 575 | const char* test = "<?xml version='1.0'?><a.elem xmi.version='2.0'/>";
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 576 |
|
| 577 | XMLDocument doc;
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 578 | doc.Parse( test );
|
| 579 | XMLTest( "dot in names", doc.Error(), false );
|
| 580 | XMLTest( "dot in names", doc.FirstChildElement()->Name(), "a.elem" );
|
| 581 | XMLTest( "dot in names", doc.FirstChildElement()->Attribute( "xmi.version" ), "2.0" );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 582 | }
|
| 583 |
|
| 584 | {
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 585 | const char* test = "<element><Name>1.1 Start easy ignore fin thickness
</Name></element>";
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 586 |
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 587 | XMLDocument doc;
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 588 | doc.Parse( test );
|
| 589 |
|
| 590 | XMLText* text = doc.FirstChildElement()->FirstChildElement()->FirstChild()->ToText();
|
| 591 | XMLTest( "Entity with one digit.",
|
| 592 | text->Value(), "1.1 Start easy ignore fin thickness\n",
|
| 593 | false );
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 594 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 595 |
|
| 596 | {
|
| 597 | // DOCTYPE not preserved (950171)
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 598 | //
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 599 | const char* doctype =
|
| 600 | "<?xml version=\"1.0\" ?>"
|
| 601 | "<!DOCTYPE PLAY SYSTEM 'play.dtd'>"
|
| 602 | "<!ELEMENT title (#PCDATA)>"
|
| 603 | "<!ELEMENT books (title,authors)>"
|
| 604 | "<element />";
|
| 605 |
|
| 606 | XMLDocument doc;
|
| 607 | doc.Parse( doctype );
|
Arkadiy Shapkin | ff72d1f | 2012-07-24 00:24:07 +0400 | [diff] [blame] | 608 | doc.SaveFile( "resources/out/test7.xml" );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 609 | doc.DeleteChild( doc.RootElement() );
|
Arkadiy Shapkin | ff72d1f | 2012-07-24 00:24:07 +0400 | [diff] [blame] | 610 | doc.LoadFile( "resources/out/test7.xml" );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 611 | doc.Print();
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 612 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 613 | const XMLUnknown* decl = doc.FirstChild()->NextSibling()->ToUnknown();
|
| 614 | XMLTest( "Correct value of unknown.", "DOCTYPE PLAY SYSTEM 'play.dtd'", decl->Value() );
|
| 615 |
|
| 616 | }
|
| 617 |
|
| 618 | {
|
| 619 | // Comments do not stream out correctly.
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 620 | const char* doctype =
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 621 | "<!-- Somewhat<evil> -->";
|
| 622 | XMLDocument doc;
|
| 623 | doc.Parse( doctype );
|
| 624 |
|
| 625 | XMLComment* comment = doc.FirstChild()->ToComment();
|
| 626 |
|
| 627 | XMLTest( "Comment formatting.", " Somewhat<evil> ", comment->Value() );
|
| 628 | }
|
| 629 | {
|
| 630 | // Double attributes
|
| 631 | const char* doctype = "<element attr='red' attr='blue' />";
|
| 632 |
|
| 633 | XMLDocument doc;
|
| 634 | doc.Parse( doctype );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 635 |
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 636 | XMLTest( "Parsing repeated attributes.", (int)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] | 637 | doc.PrintError();
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 638 | }
|
| 639 |
|
| 640 | {
|
| 641 | // Embedded null in stream.
|
| 642 | const char* doctype = "<element att\0r='red' attr='blue' />";
|
| 643 |
|
| 644 | XMLDocument doc;
|
| 645 | doc.Parse( doctype );
|
| 646 | XMLTest( "Embedded null throws error.", true, doc.Error() );
|
| 647 | }
|
| 648 |
|
| 649 | {
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 650 | // Empty documents should return TIXML_XML_ERROR_PARSING_EMPTY, bug 1070717
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 651 | const char* str = " ";
|
| 652 | XMLDocument doc;
|
| 653 | doc.Parse( str );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 654 | XMLTest( "Empty document error", (int)XML_ERROR_EMPTY_DOCUMENT, doc.ErrorID() );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 655 | }
|
| 656 |
|
| 657 | {
|
| 658 | // Low entities
|
| 659 | XMLDocument doc;
|
| 660 | doc.Parse( "<test></test>" );
|
| 661 | const char result[] = { 0x0e, 0 };
|
| 662 | XMLTest( "Low entities.", doc.FirstChildElement()->GetText(), result );
|
| 663 | doc.Print();
|
| 664 | }
|
| 665 |
|
| 666 | {
|
| 667 | // Attribute values with trailing quotes not handled correctly
|
| 668 | XMLDocument doc;
|
| 669 | doc.Parse( "<foo attribute=bar\" />" );
|
| 670 | XMLTest( "Throw error with bad end quotes.", doc.Error(), true );
|
| 671 | }
|
| 672 |
|
| 673 | {
|
| 674 | // [ 1663758 ] Failure to report error on bad XML
|
| 675 | XMLDocument xml;
|
| 676 | xml.Parse("<x>");
|
| 677 | XMLTest("Missing end tag at end of input", xml.Error(), true);
|
| 678 | xml.Parse("<x> ");
|
| 679 | XMLTest("Missing end tag with trailing whitespace", xml.Error(), true);
|
| 680 | xml.Parse("<x></y>");
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 681 | XMLTest("Mismatched tags", xml.ErrorID(), (int)XML_ERROR_MISMATCHED_ELEMENT);
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 682 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 683 |
|
| 684 |
|
| 685 | {
|
| 686 | // [ 1475201 ] TinyXML parses entities in comments
|
| 687 | XMLDocument xml;
|
| 688 | xml.Parse("<!-- declarations for <head> & <body> -->"
|
| 689 | "<!-- far & away -->" );
|
| 690 |
|
| 691 | XMLNode* e0 = xml.FirstChild();
|
| 692 | XMLNode* e1 = e0->NextSibling();
|
| 693 | XMLComment* c0 = e0->ToComment();
|
| 694 | XMLComment* c1 = e1->ToComment();
|
| 695 |
|
| 696 | XMLTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
|
| 697 | XMLTest( "Comments ignore entities.", " far & away ", c1->Value(), true );
|
| 698 | }
|
| 699 |
|
| 700 | {
|
| 701 | XMLDocument xml;
|
| 702 | xml.Parse( "<Parent>"
|
| 703 | "<child1 att=''/>"
|
| 704 | "<!-- With this comment, child2 will not be parsed! -->"
|
| 705 | "<child2 att=''/>"
|
| 706 | "</Parent>" );
|
| 707 | xml.Print();
|
| 708 |
|
| 709 | int count = 0;
|
| 710 |
|
| 711 | for( XMLNode* ele = xml.FirstChildElement( "Parent" )->FirstChild();
|
| 712 | ele;
|
| 713 | ele = ele->NextSibling() )
|
| 714 | {
|
| 715 | ++count;
|
| 716 | }
|
| 717 |
|
| 718 | XMLTest( "Comments iterate correctly.", 3, count );
|
| 719 | }
|
| 720 |
|
| 721 | {
|
| 722 | // trying to repro ]1874301]. If it doesn't go into an infinite loop, all is well.
|
| 723 | unsigned char buf[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?><feed><![CDATA[Test XMLblablablalblbl";
|
| 724 | buf[60] = 239;
|
| 725 | buf[61] = 0;
|
| 726 |
|
| 727 | XMLDocument doc;
|
| 728 | doc.Parse( (const char*)buf);
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 729 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 730 |
|
| 731 |
|
| 732 | {
|
| 733 | // bug 1827248 Error while parsing a little bit malformed file
|
| 734 | // Actually not malformed - should work.
|
| 735 | XMLDocument xml;
|
| 736 | xml.Parse( "<attributelist> </attributelist >" );
|
| 737 | XMLTest( "Handle end tag whitespace", false, xml.Error() );
|
| 738 | }
|
| 739 |
|
| 740 | {
|
| 741 | // This one must not result in an infinite loop
|
| 742 | XMLDocument xml;
|
| 743 | xml.Parse( "<infinite>loop" );
|
| 744 | XMLTest( "Infinite loop test.", true, true );
|
| 745 | }
|
| 746 | #endif
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 747 | {
|
| 748 | const char* pub = "<?xml version='1.0'?> <element><sub/></element> <!--comment--> <!DOCTYPE>";
|
| 749 | XMLDocument doc;
|
| 750 | doc.Parse( pub );
|
| 751 |
|
| 752 | XMLDocument clone;
|
| 753 | for( const XMLNode* node=doc.FirstChild(); node; node=node->NextSibling() ) {
|
| 754 | XMLNode* copy = node->ShallowClone( &clone );
|
| 755 | clone.InsertEndChild( copy );
|
| 756 | }
|
| 757 |
|
| 758 | clone.Print();
|
| 759 |
|
| 760 | int count=0;
|
| 761 | const XMLNode* a=clone.FirstChild();
|
| 762 | const XMLNode* b=doc.FirstChild();
|
| 763 | for( ; a && b; a=a->NextSibling(), b=b->NextSibling() ) {
|
| 764 | ++count;
|
| 765 | XMLTest( "Clone and Equal", true, a->ShallowEqual( b ));
|
| 766 | }
|
| 767 | XMLTest( "Clone and Equal", 4, count );
|
| 768 | }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 769 |
|
Lee Thomason (grinliz) | a4a36ba | 2012-04-06 21:24:29 -0700 | [diff] [blame] | 770 | {
|
| 771 | // This shouldn't crash.
|
| 772 | XMLDocument doc;
|
| 773 | if(XML_NO_ERROR != doc.LoadFile( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ))
|
| 774 | {
|
| 775 | doc.PrintError();
|
| 776 | }
|
| 777 | XMLTest( "Error in snprinf handling.", true, doc.Error() );
|
| 778 | }
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 779 |
|
Lee Thomason | 5e3803c | 2012-04-16 08:57:05 -0700 | [diff] [blame] | 780 | {
|
| 781 | // Attribute ordering.
|
| 782 | static const char* xml = "<element attrib1=\"1\" attrib2=\"2\" attrib3=\"3\" />";
|
| 783 | XMLDocument doc;
|
| 784 | doc.Parse( xml );
|
| 785 | XMLElement* ele = doc.FirstChildElement();
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 786 |
|
Lee Thomason | 5e3803c | 2012-04-16 08:57:05 -0700 | [diff] [blame] | 787 | const XMLAttribute* a = ele->FirstAttribute();
|
| 788 | XMLTest( "Attribute order", "1", a->Value() );
|
| 789 | a = a->Next();
|
| 790 | XMLTest( "Attribute order", "2", a->Value() );
|
| 791 | a = a->Next();
|
| 792 | XMLTest( "Attribute order", "3", a->Value() );
|
| 793 | XMLTest( "Attribute order", "attrib3", a->Name() );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 794 |
|
Lee Thomason | 5e3803c | 2012-04-16 08:57:05 -0700 | [diff] [blame] | 795 | ele->DeleteAttribute( "attrib2" );
|
| 796 | a = ele->FirstAttribute();
|
| 797 | XMLTest( "Attribute order", "1", a->Value() );
|
| 798 | a = a->Next();
|
| 799 | XMLTest( "Attribute order", "3", a->Value() );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 800 |
|
Lee Thomason | 5e3803c | 2012-04-16 08:57:05 -0700 | [diff] [blame] | 801 | ele->DeleteAttribute( "attrib1" );
|
| 802 | ele->DeleteAttribute( "attrib3" );
|
| 803 | XMLTest( "Attribute order (empty)", false, ele->FirstAttribute() ? true : false );
|
| 804 | }
|
Lee Thomason (grinliz) | a4a36ba | 2012-04-06 21:24:29 -0700 | [diff] [blame] | 805 |
|
Lee Thomason (grinliz) | 390e978 | 2012-07-01 21:22:53 -0700 | [diff] [blame] | 806 | {
|
| 807 | // Make sure an attribute with a space in it succeeds.
|
Lee Thomason | 78a773d | 2012-07-02 10:10:19 -0700 | [diff] [blame] | 808 | static const char* xml0 = "<element attribute1= \"Test Attribute\"/>";
|
| 809 | static const char* xml1 = "<element attribute1 =\"Test Attribute\"/>";
|
| 810 | static const char* xml2 = "<element attribute1 = \"Test Attribute\"/>";
|
| 811 | XMLDocument doc0;
|
| 812 | doc0.Parse( xml0 );
|
| 813 | XMLDocument doc1;
|
| 814 | doc1.Parse( xml1 );
|
| 815 | XMLDocument doc2;
|
| 816 | doc2.Parse( xml2 );
|
Lee Thomason (grinliz) | 390e978 | 2012-07-01 21:22:53 -0700 | [diff] [blame] | 817 |
|
Lee Thomason | 78a773d | 2012-07-02 10:10:19 -0700 | [diff] [blame] | 818 | XMLElement* ele = 0;
|
| 819 | ele = doc0.FirstChildElement();
|
| 820 | XMLTest( "Attribute with space #1", "Test Attribute", ele->Attribute( "attribute1" ) );
|
| 821 | ele = doc1.FirstChildElement();
|
| 822 | XMLTest( "Attribute with space #2", "Test Attribute", ele->Attribute( "attribute1" ) );
|
| 823 | ele = doc2.FirstChildElement();
|
| 824 | XMLTest( "Attribute with space #3", "Test Attribute", ele->Attribute( "attribute1" ) );
|
Lee Thomason (grinliz) | 390e978 | 2012-07-01 21:22:53 -0700 | [diff] [blame] | 825 | }
|
| 826 |
|
| 827 | {
|
| 828 | // Make sure we don't go into an infinite loop.
|
| 829 | static const char* xml = "<doc><element attribute='attribute'/><element attribute='attribute'/></doc>";
|
| 830 | XMLDocument doc;
|
| 831 | doc.Parse( xml );
|
| 832 | XMLElement* ele0 = doc.FirstChildElement()->FirstChildElement();
|
| 833 | XMLElement* ele1 = ele0->NextSiblingElement();
|
| 834 | bool equal = ele0->ShallowEqual( ele1 );
|
| 835 |
|
| 836 | XMLTest( "Infinite loop in shallow equal.", true, equal );
|
| 837 | }
|
| 838 |
|
Lee Thomason | 5708f81 | 2012-03-28 17:46:41 -0700 | [diff] [blame] | 839 | // -------- Handles ------------
|
| 840 | {
|
| 841 | static const char* xml = "<element attrib='bar'><sub>Text</sub></element>";
|
| 842 | XMLDocument doc;
|
| 843 | doc.Parse( xml );
|
Lee Thomason | 5708f81 | 2012-03-28 17:46:41 -0700 | [diff] [blame] | 844 |
|
| 845 | XMLElement* ele = XMLHandle( doc ).FirstChildElement( "element" ).FirstChild().ToElement();
|
| 846 | XMLTest( "Handle, success, mutable", ele->Value(), "sub" );
|
| 847 |
|
Lee Thomason (grinliz) | ae209f6 | 2012-04-04 22:00:07 -0700 | [diff] [blame] | 848 | XMLHandle docH( doc );
|
| 849 | ele = docH.FirstChildElement( "none" ).FirstChildElement( "element" ).ToElement();
|
Lee Thomason | d0b19df | 2012-04-12 08:41:37 -0700 | [diff] [blame] | 850 | XMLTest( "Handle, dne, mutable", false, ele != 0 );
|
Lee Thomason | 5708f81 | 2012-03-28 17:46:41 -0700 | [diff] [blame] | 851 | }
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 852 |
|
Lee Thomason (grinliz) | ae209f6 | 2012-04-04 22:00:07 -0700 | [diff] [blame] | 853 | {
|
| 854 | static const char* xml = "<element attrib='bar'><sub>Text</sub></element>";
|
| 855 | XMLDocument doc;
|
| 856 | doc.Parse( xml );
|
| 857 | XMLConstHandle docH( doc );
|
| 858 |
|
| 859 | const XMLElement* ele = docH.FirstChildElement( "element" ).FirstChild().ToElement();
|
| 860 | XMLTest( "Handle, success, const", ele->Value(), "sub" );
|
| 861 |
|
| 862 | ele = docH.FirstChildElement( "none" ).FirstChildElement( "element" ).ToElement();
|
Lee Thomason | d0b19df | 2012-04-12 08:41:37 -0700 | [diff] [blame] | 863 | XMLTest( "Handle, dne, const", false, ele != 0 );
|
Lee Thomason (grinliz) | ae209f6 | 2012-04-04 22:00:07 -0700 | [diff] [blame] | 864 | }
|
Lee Thomason | f68c438 | 2012-04-28 14:37:11 -0700 | [diff] [blame] | 865 | {
|
| 866 | // Default Declaration & BOM
|
| 867 | XMLDocument doc;
|
| 868 | doc.InsertEndChild( doc.NewDeclaration() );
|
| 869 | doc.SetBOM( true );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 870 |
|
Lee Thomason | f68c438 | 2012-04-28 14:37:11 -0700 | [diff] [blame] | 871 | XMLPrinter printer;
|
| 872 | doc.Print( &printer );
|
| 873 |
|
| 874 | static const char* result = "\xef\xbb\xbf<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
|
| 875 | XMLTest( "BOM and default declaration", printer.CStr(), result, false );
|
Lee Thomason (grinliz) | 48ea0bc | 2012-05-26 14:41:14 -0700 | [diff] [blame] | 876 | XMLTest( "CStrSize", printer.CStrSize(), 42, false );
|
Lee Thomason | f68c438 | 2012-04-28 14:37:11 -0700 | [diff] [blame] | 877 | }
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 878 | {
|
| 879 | const char* xml = "<ipxml ws='1'><info bla=' /></ipxml>";
|
| 880 | XMLDocument doc;
|
| 881 | doc.Parse( xml );
|
| 882 | XMLTest( "Ill formed XML", true, doc.Error() );
|
| 883 | }
|
| 884 |
|
| 885 | // QueryXYZText
|
| 886 | {
|
| 887 | const char* xml = "<point> <x>1.2</x> <y>1</y> <z>38</z> <valid>true</valid> </point>";
|
| 888 | XMLDocument doc;
|
| 889 | doc.Parse( xml );
|
| 890 |
|
| 891 | const XMLElement* pointElement = doc.RootElement();
|
| 892 |
|
| 893 | int intValue = 0;
|
| 894 | unsigned unsignedValue = 0;
|
| 895 | float floatValue = 0;
|
| 896 | double doubleValue = 0;
|
| 897 | bool boolValue = false;
|
| 898 |
|
| 899 | pointElement->FirstChildElement( "y" )->QueryIntText( &intValue );
|
| 900 | pointElement->FirstChildElement( "y" )->QueryUnsignedText( &unsignedValue );
|
| 901 | pointElement->FirstChildElement( "x" )->QueryFloatText( &floatValue );
|
| 902 | pointElement->FirstChildElement( "x" )->QueryDoubleText( &doubleValue );
|
| 903 | pointElement->FirstChildElement( "valid" )->QueryBoolText( &boolValue );
|
| 904 |
|
| 905 |
|
| 906 | XMLTest( "QueryIntText", intValue, 1, false );
|
| 907 | XMLTest( "QueryUnsignedText", unsignedValue, (unsigned)1, false );
|
| 908 | XMLTest( "QueryFloatText", floatValue, 1.2f, false );
|
| 909 | XMLTest( "QueryDoubleText", doubleValue, 1.2, false );
|
| 910 | XMLTest( "QueryBoolText", boolValue, true, false );
|
| 911 | }
|
Lee Thomason (grinliz) | ae209f6 | 2012-04-04 22:00:07 -0700 | [diff] [blame] | 912 |
|
Lee Thomason (grinliz) | 5fbacbe | 2012-09-08 21:40:53 -0700 | [diff] [blame] | 913 | {
|
| 914 | const char* xml = "<element><_sub/><:sub/><sub:sub/><sub-sub/></element>";
|
| 915 | XMLDocument doc;
|
| 916 | doc.Parse( xml );
|
| 917 | XMLTest( "Non-alpha element lead letter parses.", doc.Error(), false );
|
| 918 | }
|
Lee Thomason (grinliz) | 62d1c5a | 2012-09-08 21:44:12 -0700 | [diff] [blame] | 919 |
|
Lee Thomason (grinliz) | e2bcb32 | 2012-09-17 17:58:25 -0700 | [diff] [blame] | 920 | {
|
| 921 | const char* xml = "<element/>WOA THIS ISN'T GOING TO PARSE";
|
| 922 | XMLDocument doc;
|
| 923 | doc.Parse( xml, 10 );
|
Lee Thomason (grinliz) | e2bcb32 | 2012-09-17 17:58:25 -0700 | [diff] [blame] | 924 | XMLTest( "Set length of incoming data", doc.Error(), false );
|
| 925 | }
|
| 926 |
|
Lee Thomason (grinliz) | bc1bfb7 | 2012-08-20 22:00:38 -0700 | [diff] [blame] | 927 | // ----------- Whitespace ------------
|
| 928 | {
|
| 929 | const char* xml = "<element>"
|
| 930 | "<a> This \nis ' text ' </a>"
|
| 931 | "<b> This is ' text ' \n</b>"
|
| 932 | "<c>This is ' \n\n text '</c>"
|
| 933 | "</element>";
|
| 934 | XMLDocument doc( true, COLLAPSE_WHITESPACE );
|
| 935 | doc.Parse( xml );
|
| 936 |
|
| 937 | const XMLElement* element = doc.FirstChildElement();
|
| 938 | for( const XMLElement* parent = element->FirstChildElement();
|
| 939 | parent;
|
| 940 | parent = parent->NextSiblingElement() )
|
| 941 | {
|
| 942 | XMLTest( "Whitespace collapse", "This is ' text '", parent->GetText() );
|
| 943 | }
|
| 944 | }
|
Lee Thomason (grinliz) | 0fa8299 | 2012-09-08 21:53:47 -0700 | [diff] [blame] | 945 |
|
| 946 | {
|
| 947 | const char* xml = "<element> </element>";
|
| 948 | XMLDocument doc( true, COLLAPSE_WHITESPACE );
|
| 949 | doc.Parse( xml );
|
| 950 | XMLTest( "Whitespace all space", true, 0 == doc.FirstChildElement()->FirstChild() );
|
| 951 | }
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 952 |
|
Lee Thomason (grinliz) | fc6320e | 2012-09-23 20:25:50 -0700 | [diff] [blame] | 953 | #if 0 // the question being explored is what kind of print to use:
|
| 954 | // https://github.com/leethomason/tinyxml2/issues/63
|
| 955 | {
|
| 956 | const char* xml = "<element attrA='123456789.123456789' attrB='1.001e9'/>";
|
| 957 | XMLDocument doc;
|
| 958 | doc.Parse( xml );
|
| 959 | doc.FirstChildElement()->SetAttribute( "attrA", 123456789.123456789 );
|
| 960 | doc.FirstChildElement()->SetAttribute( "attrB", 1.001e9 );
|
| 961 | doc.Print();
|
| 962 | }
|
| 963 | #endif
|
| 964 |
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 965 | // ----------- Performance tracking --------------
|
| 966 | {
|
| 967 | #if defined( _MSC_VER )
|
| 968 | __int64 start, end, freq;
|
| 969 | QueryPerformanceFrequency( (LARGE_INTEGER*) &freq );
|
| 970 | #endif
|
| 971 |
|
Bruno Dias | a2d4e6e | 2012-05-07 04:58:11 -0300 | [diff] [blame] | 972 | FILE* fp = fopen( "resources/dream.xml", "r" );
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 973 | fseek( fp, 0, SEEK_END );
|
| 974 | long size = ftell( fp );
|
| 975 | fseek( fp, 0, SEEK_SET );
|
| 976 |
|
| 977 | char* mem = new char[size+1];
|
| 978 | fread( mem, size, 1, fp );
|
| 979 | fclose( fp );
|
| 980 | mem[size] = 0;
|
| 981 |
|
| 982 | #if defined( _MSC_VER )
|
| 983 | QueryPerformanceCounter( (LARGE_INTEGER*) &start );
|
| 984 | #else
|
| 985 | clock_t cstart = clock();
|
| 986 | #endif
|
| 987 | static const int COUNT = 10;
|
| 988 | for( int i=0; i<COUNT; ++i ) {
|
| 989 | XMLDocument doc;
|
| 990 | doc.Parse( mem );
|
| 991 | }
|
| 992 | #if defined( _MSC_VER )
|
| 993 | QueryPerformanceCounter( (LARGE_INTEGER*) &end );
|
| 994 | #else
|
| 995 | clock_t cend = clock();
|
| 996 | #endif
|
| 997 |
|
| 998 | delete [] mem;
|
| 999 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1000 | static const char* note =
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1001 | #ifdef DEBUG
|
| 1002 | "DEBUG";
|
| 1003 | #else
|
| 1004 | "Release";
|
| 1005 | #endif
|
| 1006 |
|
| 1007 | #if defined( _MSC_VER )
|
| 1008 | printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, 1000.0 * (double)(end-start) / ( (double)freq * (double)COUNT) );
|
| 1009 | #else
|
| 1010 | printf( "\nParsing %s of dream.xml: %.3f milli-seconds\n", note, (double)(cend - cstart)/(double)COUNT );
|
| 1011 | #endif
|
| 1012 | }
|
| 1013 |
|
Lee Thomason (grinliz) | 7ca5558 | 2012-03-07 21:54:57 -0800 | [diff] [blame] | 1014 | #if defined( _MSC_VER ) && defined( DEBUG )
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1015 | _CrtMemCheckpoint( &endMemState );
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1016 | //_CrtMemDumpStatistics( &endMemState );
|
| 1017 |
|
| 1018 | _CrtMemState diffMemState;
|
| 1019 | _CrtMemDifference( &diffMemState, &startMemState, &endMemState );
|
| 1020 | _CrtMemDumpStatistics( &diffMemState );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1021 | //printf( "new total=%d\n", gNewTotal );
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1022 | #endif
|
| 1023 |
|
| 1024 | printf ("\nPass %d, Fail %d\n", gPass, gFail);
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1025 | return 0;
|
Lee Thomason (grinliz) | 9b093cc | 2012-02-25 21:30:18 -0800 | [diff] [blame] | 1026 | }
|