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 |
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame^] | 17 | 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] | 18 | {
|
| 19 | bool pass = !strcmp( expected, found );
|
| 20 | if ( pass )
|
| 21 | printf ("[pass]");
|
| 22 | else
|
| 23 | printf ("[fail]");
|
| 24 |
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame^] | 25 | if ( !echo )
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 26 | printf (" %s\n", testString);
|
| 27 | else
|
| 28 | printf (" %s [%s][%s]\n", testString, expected, found);
|
| 29 |
|
| 30 | if ( pass )
|
| 31 | ++gPass;
|
| 32 | else
|
| 33 | ++gFail;
|
| 34 | return pass;
|
| 35 | }
|
| 36 |
|
| 37 |
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame^] | 38 | bool XMLTest( const char* testString, int expected, int found, bool echo=true )
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 39 | {
|
| 40 | bool pass = ( expected == found );
|
| 41 | if ( pass )
|
| 42 | printf ("[pass]");
|
| 43 | else
|
| 44 | printf ("[fail]");
|
| 45 |
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame^] | 46 | if ( !echo )
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 47 | printf (" %s\n", testString);
|
| 48 | else
|
| 49 | printf (" %s [%d][%d]\n", testString, expected, found);
|
| 50 |
|
| 51 | if ( pass )
|
| 52 | ++gPass;
|
| 53 | else
|
| 54 | ++gFail;
|
| 55 | return pass;
|
| 56 | }
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 57 |
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 58 |
|
| 59 | int main( int argc, const char* argv )
|
| 60 | {
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 61 | #if defined( WIN32 )
|
| 62 | _CrtMemCheckpoint( &startMemState );
|
| 63 | #endif
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 64 |
|
| 65 | #if 0
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 66 | {
|
Lee Thomason | fde6a75 | 2012-01-14 18:08:12 -0800 | [diff] [blame] | 67 | static const char* test = "<!--hello world\n"
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 68 | " line 2\r"
|
| 69 | " line 3\r\n"
|
| 70 | " line 4\n\r"
|
| 71 | " line 5\r-->";
|
| 72 |
|
| 73 | XMLDocument doc;
|
| 74 | doc.Parse( test );
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 75 | doc.Print();
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 76 | }
|
| 77 | #endif
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 78 | #if 0
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 79 | {
|
Lee Thomason | 43f5930 | 2012-02-06 18:18:11 -0800 | [diff] [blame] | 80 | static const char* test[] = { "<element />",
|
| 81 | "<element></element>",
|
| 82 | "<element><subelement/></element>",
|
| 83 | "<element><subelement></subelement></element>",
|
| 84 | "<element><subelement><subsub/></subelement></element>",
|
| 85 | "<!--comment beside elements--><element><subelement></subelement></element>",
|
| 86 | "<!--comment beside elements, this time with spaces--> \n <element> <subelement> \n </subelement> </element>",
|
| 87 | "<element attrib1='foo' attrib2=\"bar\" ></element>",
|
| 88 | "<element attrib1='foo' attrib2=\"bar\" ><subelement attrib3='yeehaa' /></element>",
|
| 89 | "<element>Text inside element.</element>",
|
| 90 | "<element><b></b></element>",
|
| 91 | "<element>Text inside and <b>bolded</b> in the element.</element>",
|
| 92 | "<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] | 93 | "<element>This & That.</element>",
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 94 | "<element attrib='This<That' />",
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 95 | 0
|
| 96 | };
|
Lee Thomason | 6ee99fc | 2012-01-21 18:45:16 -0800 | [diff] [blame] | 97 | for( int i=0; test[i]; ++i ) {
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 98 | XMLDocument doc;
|
Lee Thomason | 6ee99fc | 2012-01-21 18:45:16 -0800 | [diff] [blame] | 99 | doc.Parse( test[i] );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 100 | doc.Print();
|
Lee Thomason | ec975ce | 2012-01-23 11:42:06 -0800 | [diff] [blame] | 101 | printf( "----------------------------------------------\n" );
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 102 | }
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 103 | }
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 104 | #endif
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 105 | #if 0
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 106 | {
|
| 107 | static const char* test = "<element>Text before.</element>";
|
| 108 | XMLDocument doc;
|
| 109 | doc.Parse( test );
|
| 110 | XMLElement* root = doc.FirstChildElement();
|
| 111 | XMLElement* newElement = doc.NewElement( "Subelement" );
|
| 112 | root->InsertEndChild( newElement );
|
| 113 | doc.Print();
|
| 114 | }
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 115 | {
|
| 116 | XMLDocument* doc = new XMLDocument();
|
| 117 | static const char* test = "<element><sub/></element>";
|
| 118 | doc->Parse( test );
|
| 119 | delete doc;
|
| 120 | }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 121 | #endif
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 122 | {
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 123 | // Test: Programmatic DOM
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 124 | // Build:
|
| 125 | // <element>
|
| 126 | // <!--comment-->
|
| 127 | // <sub attrib="1" />
|
| 128 | // <sub attrib="2" />
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame^] | 129 | // <sub attrib="3" >& Text!</sub>
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 130 | // <element>
|
| 131 |
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 132 | XMLDocument* doc = new XMLDocument();
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 133 | XMLNode* element = doc->InsertEndChild( doc->NewElement( "element" ) );
|
| 134 |
|
| 135 | XMLElement* sub[3] = { doc->NewElement( "sub" ), doc->NewElement( "sub" ), doc->NewElement( "sub" ) };
|
| 136 | for( int i=0; i<3; ++i ) {
|
| 137 | sub[i]->SetAttribute( "attrib", i );
|
| 138 | }
|
| 139 | element->InsertEndChild( sub[2] );
|
| 140 | XMLNode* comment = element->InsertFirstChild( doc->NewComment( "comment" ) );
|
| 141 | element->InsertAfterChild( comment, sub[0] );
|
| 142 | element->InsertAfterChild( sub[0], sub[1] );
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame^] | 143 | sub[2]->InsertFirstChild( doc->NewText( "& Text!" ));
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 144 | doc->Print();
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame^] | 145 | XMLTest( "Programmatic DOM", "comment", doc->FirstChildElement( "element" )->FirstChild()->Value() );
|
| 146 | XMLTest( "Programmatic DOM", "0", doc->FirstChildElement( "element" )->FirstChildElement()->Attribute( "attrib" ) );
|
| 147 | XMLTest( "Programmatic DOM", 2, doc->FirstChildElement()->LastChildElement( "sub" )->IntAttribute( "attrib" ) );
|
| 148 | XMLTest( "Programmatic DOM", "& Text!",
|
| 149 | doc->FirstChildElement()->LastChildElement( "sub" )->FirstChild()->ToText()->Value() );
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 150 | delete doc;
|
| 151 | }
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 152 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 153 | #if defined( WIN32 )
|
| 154 | _CrtMemCheckpoint( &endMemState );
|
| 155 | //_CrtMemDumpStatistics( &endMemState );
|
| 156 |
|
| 157 | _CrtMemState diffMemState;
|
| 158 | _CrtMemDifference( &diffMemState, &startMemState, &endMemState );
|
| 159 | _CrtMemDumpStatistics( &diffMemState );
|
| 160 | #endif
|
| 161 |
|
| 162 | printf ("\nPass %d, Fail %d\n", gPass, gFail);
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 163 | return 0;
|
| 164 | } |