U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1 | #include "tinyxml2.h"
|
| 2 |
|
| 3 | #include <stdio.h>
|
| 4 | #include <stdlib.h>
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 5 | #include <string.h>
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 6 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 7 | #if defined( WIN32 )
|
| 8 | #include <crtdbg.h>
|
| 9 | _CrtMemState startMemState;
|
| 10 | _CrtMemState endMemState;
|
| 11 | #endif
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 12 |
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 13 | using namespace tinyxml2;
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 14 | int gPass = 0;
|
| 15 | int gFail = 0;
|
| 16 |
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame^] | 17 | //#define DREAM_ONLY
|
| 18 |
|
| 19 | /*
|
| 20 | int gNew = 0;
|
| 21 | int gNewTotal = 0;
|
| 22 |
|
| 23 | void* operator new( size_t size )
|
| 24 | {
|
| 25 | ++gNew;
|
| 26 | return malloc( size );
|
| 27 | }
|
| 28 |
|
| 29 | void* operator new[]( size_t size )
|
| 30 | {
|
| 31 | ++gNew;
|
| 32 | return malloc( size );
|
| 33 | }
|
| 34 |
|
| 35 | void operator delete[]( void* mem )
|
| 36 | {
|
| 37 | free( mem );
|
| 38 | }
|
| 39 |
|
| 40 | void operator delete( void* mem )
|
| 41 | {
|
| 42 | free( mem );
|
| 43 | }
|
| 44 | */
|
| 45 |
|
| 46 |
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 47 | 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] | 48 | {
|
| 49 | bool pass = !strcmp( expected, found );
|
| 50 | if ( pass )
|
| 51 | printf ("[pass]");
|
| 52 | else
|
| 53 | printf ("[fail]");
|
| 54 |
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 55 | if ( !echo )
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 56 | printf (" %s\n", testString);
|
| 57 | else
|
| 58 | printf (" %s [%s][%s]\n", testString, expected, found);
|
| 59 |
|
| 60 | if ( pass )
|
| 61 | ++gPass;
|
| 62 | else
|
| 63 | ++gFail;
|
| 64 | return pass;
|
| 65 | }
|
| 66 |
|
| 67 |
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 68 | bool XMLTest( const char* testString, int expected, int found, bool echo=true )
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 69 | {
|
| 70 | bool pass = ( expected == found );
|
| 71 | if ( pass )
|
| 72 | printf ("[pass]");
|
| 73 | else
|
| 74 | printf ("[fail]");
|
| 75 |
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 76 | if ( !echo )
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 77 | printf (" %s\n", testString);
|
| 78 | else
|
| 79 | printf (" %s [%d][%d]\n", testString, expected, found);
|
| 80 |
|
| 81 | if ( pass )
|
| 82 | ++gPass;
|
| 83 | else
|
| 84 | ++gFail;
|
| 85 | return pass;
|
| 86 | }
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 87 |
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 88 |
|
| 89 | int main( int argc, const char* argv )
|
| 90 | {
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 91 | #if defined( WIN32 )
|
| 92 | _CrtMemCheckpoint( &startMemState );
|
| 93 | #endif
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame^] | 94 | #ifndef DREAM_ONLY
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 95 | #if 0
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 96 | {
|
Lee Thomason | fde6a75 | 2012-01-14 18:08:12 -0800 | [diff] [blame] | 97 | static const char* test = "<!--hello world\n"
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 98 | " line 2\r"
|
| 99 | " line 3\r\n"
|
| 100 | " line 4\n\r"
|
| 101 | " line 5\r-->";
|
| 102 |
|
| 103 | XMLDocument doc;
|
| 104 | doc.Parse( test );
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 105 | doc.Print();
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 106 | }
|
| 107 | #endif
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 108 | #if 0
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 109 | {
|
Lee Thomason | 43f5930 | 2012-02-06 18:18:11 -0800 | [diff] [blame] | 110 | static const char* test[] = { "<element />",
|
| 111 | "<element></element>",
|
| 112 | "<element><subelement/></element>",
|
| 113 | "<element><subelement></subelement></element>",
|
| 114 | "<element><subelement><subsub/></subelement></element>",
|
| 115 | "<!--comment beside elements--><element><subelement></subelement></element>",
|
| 116 | "<!--comment beside elements, this time with spaces--> \n <element> <subelement> \n </subelement> </element>",
|
| 117 | "<element attrib1='foo' attrib2=\"bar\" ></element>",
|
| 118 | "<element attrib1='foo' attrib2=\"bar\" ><subelement attrib3='yeehaa' /></element>",
|
| 119 | "<element>Text inside element.</element>",
|
| 120 | "<element><b></b></element>",
|
| 121 | "<element>Text inside and <b>bolded</b> in the element.</element>",
|
| 122 | "<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] | 123 | "<element>This & That.</element>",
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 124 | "<element attrib='This<That' />",
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 125 | 0
|
| 126 | };
|
Lee Thomason | 6ee99fc | 2012-01-21 18:45:16 -0800 | [diff] [blame] | 127 | for( int i=0; test[i]; ++i ) {
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 128 | XMLDocument doc;
|
Lee Thomason | 6ee99fc | 2012-01-21 18:45:16 -0800 | [diff] [blame] | 129 | doc.Parse( test[i] );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 130 | doc.Print();
|
Lee Thomason | ec975ce | 2012-01-23 11:42:06 -0800 | [diff] [blame] | 131 | printf( "----------------------------------------------\n" );
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 132 | }
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 133 | }
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 134 | #endif
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 135 | #if 0
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 136 | {
|
| 137 | static const char* test = "<element>Text before.</element>";
|
| 138 | XMLDocument doc;
|
| 139 | doc.Parse( test );
|
| 140 | XMLElement* root = doc.FirstChildElement();
|
| 141 | XMLElement* newElement = doc.NewElement( "Subelement" );
|
| 142 | root->InsertEndChild( newElement );
|
| 143 | doc.Print();
|
| 144 | }
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 145 | {
|
| 146 | XMLDocument* doc = new XMLDocument();
|
| 147 | static const char* test = "<element><sub/></element>";
|
| 148 | doc->Parse( test );
|
| 149 | delete doc;
|
| 150 | }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 151 | #endif
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 152 | {
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 153 | // Test: Programmatic DOM
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 154 | // Build:
|
| 155 | // <element>
|
| 156 | // <!--comment-->
|
| 157 | // <sub attrib="1" />
|
| 158 | // <sub attrib="2" />
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 159 | // <sub attrib="3" >& Text!</sub>
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 160 | // <element>
|
| 161 |
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 162 | XMLDocument* doc = new XMLDocument();
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 163 | XMLNode* element = doc->InsertEndChild( doc->NewElement( "element" ) );
|
| 164 |
|
| 165 | XMLElement* sub[3] = { doc->NewElement( "sub" ), doc->NewElement( "sub" ), doc->NewElement( "sub" ) };
|
| 166 | for( int i=0; i<3; ++i ) {
|
| 167 | sub[i]->SetAttribute( "attrib", i );
|
| 168 | }
|
| 169 | element->InsertEndChild( sub[2] );
|
| 170 | XMLNode* comment = element->InsertFirstChild( doc->NewComment( "comment" ) );
|
| 171 | element->InsertAfterChild( comment, sub[0] );
|
| 172 | element->InsertAfterChild( sub[0], sub[1] );
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 173 | sub[2]->InsertFirstChild( doc->NewText( "& Text!" ));
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 174 | doc->Print();
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 175 | XMLTest( "Programmatic DOM", "comment", doc->FirstChildElement( "element" )->FirstChild()->Value() );
|
| 176 | XMLTest( "Programmatic DOM", "0", doc->FirstChildElement( "element" )->FirstChildElement()->Attribute( "attrib" ) );
|
| 177 | XMLTest( "Programmatic DOM", 2, doc->FirstChildElement()->LastChildElement( "sub" )->IntAttribute( "attrib" ) );
|
| 178 | XMLTest( "Programmatic DOM", "& Text!",
|
| 179 | doc->FirstChildElement()->LastChildElement( "sub" )->FirstChild()->ToText()->Value() );
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 180 |
|
| 181 | // And now deletion:
|
| 182 | element->DeleteChild( sub[2] );
|
| 183 | doc->DeleteNode( comment );
|
| 184 |
|
| 185 | element->FirstChildElement()->SetAttribute( "attrib", true );
|
| 186 | element->LastChildElement()->DeleteAttribute( "attrib" );
|
| 187 |
|
| 188 | XMLTest( "Programmatic DOM", true, doc->FirstChildElement()->FirstChildElement()->BoolAttribute( "attrib" ) );
|
| 189 | int value = 10;
|
| 190 | int result = doc->FirstChildElement()->LastChildElement()->QueryIntAttribute( "attrib", &value );
|
| 191 | XMLTest( "Programmatic DOM", result, NO_ATTRIBUTE );
|
| 192 | XMLTest( "Programmatic DOM", value, 10 );
|
| 193 |
|
| 194 | doc->Print();
|
| 195 |
|
| 196 | XMLStreamer streamer;
|
| 197 | doc->Print( &streamer );
|
| 198 | printf( "%s", streamer.CStr() );
|
| 199 |
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 200 | delete doc;
|
| 201 | }
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame^] | 202 | #endif
|
| 203 | {
|
| 204 | // Test: Dream
|
| 205 | // XML1 : 1,187,569 bytes in 31,209 allocations
|
| 206 | // XML2 : 469,073 bytes in 323 allocations
|
| 207 | //int newStart = gNew;
|
| 208 | XMLDocument doc;
|
| 209 | doc.Load( "dream.xml" );
|
| 210 |
|
| 211 | doc.Save( "dreamout.xml" );
|
| 212 | doc.PrintError();
|
| 213 |
|
| 214 | XMLTest( "Dream", "xml version=\"1.0\"",
|
| 215 | doc.FirstChild()->ToDeclaration()->Value() );
|
| 216 | XMLTest( "Dream", true, doc.FirstChild()->NextSibling()->ToUnknown() ? true : false );
|
| 217 | XMLTest( "Dream", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
|
| 218 | doc.FirstChild()->NextSibling()->ToUnknown()->Value() );
|
| 219 | XMLTest( "Dream", "And Robin shall restore amends.",
|
| 220 | doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
|
| 221 | XMLTest( "Dream", "And Robin shall restore amends.",
|
| 222 | doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
|
| 223 |
|
| 224 | XMLDocument doc2;
|
| 225 | doc2.Load( "dreamout.xml" );
|
| 226 | XMLTest( "Dream-out", "xml version=\"1.0\"",
|
| 227 | doc2.FirstChild()->ToDeclaration()->Value() );
|
| 228 | XMLTest( "Dream-out", true, doc2.FirstChild()->NextSibling()->ToUnknown() ? true : false );
|
| 229 | XMLTest( "Dream-out", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
|
| 230 | doc2.FirstChild()->NextSibling()->ToUnknown()->Value() );
|
| 231 | XMLTest( "Dream-out", "And Robin shall restore amends.",
|
| 232 | doc2.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
|
| 233 |
|
| 234 | //gNewTotal = gNew - newStart;
|
| 235 | }
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 236 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 237 | #if defined( WIN32 )
|
| 238 | _CrtMemCheckpoint( &endMemState );
|
| 239 | //_CrtMemDumpStatistics( &endMemState );
|
| 240 |
|
| 241 | _CrtMemState diffMemState;
|
| 242 | _CrtMemDifference( &diffMemState, &startMemState, &endMemState );
|
| 243 | _CrtMemDumpStatistics( &diffMemState );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame^] | 244 | //printf( "new total=%d\n", gNewTotal );
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 245 | #endif
|
| 246 |
|
| 247 | printf ("\nPass %d, Fail %d\n", gPass, gFail);
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 248 | return 0;
|
| 249 | } |