blob: c0258411682b66dc6c77fea263f1ae59dde8d55b [file] [log] [blame]
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001#include "tinyxml2.h"
2
3#include <stdio.h>
4#include <stdlib.h>
Lee Thomasonec5a7b42012-02-13 18:16:52 -08005#include <string.h>
U-Lama\Leee13c3e62011-12-28 14:36:55 -08006
Lee Thomason1ff38e02012-02-14 18:18:16 -08007#if defined( WIN32 )
8 #include <crtdbg.h>
9 _CrtMemState startMemState;
10 _CrtMemState endMemState;
11#endif
Lee Thomasone9ecdab2012-02-13 18:11:20 -080012
U-Lama\Leee13c3e62011-12-28 14:36:55 -080013using namespace tinyxml2;
Lee Thomasonec5a7b42012-02-13 18:16:52 -080014int gPass = 0;
15int gFail = 0;
16
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -080017//#define DREAM_ONLY
18
19/*
20int gNew = 0;
21int gNewTotal = 0;
22
23void* operator new( size_t size )
24{
25 ++gNew;
26 return malloc( size );
27}
28
29void* operator new[]( size_t size )
30{
31 ++gNew;
32 return malloc( size );
33}
34
35void operator delete[]( void* mem )
36{
37 free( mem );
38}
39
40void operator delete( void* mem )
41{
42 free( mem );
43}
44*/
45
46
U-Stream\Lee09a11c52012-02-17 08:31:16 -080047bool XMLTest (const char* testString, const char* expected, const char* found, bool echo=true )
Lee Thomason1ff38e02012-02-14 18:18:16 -080048{
49 bool pass = !strcmp( expected, found );
50 if ( pass )
51 printf ("[pass]");
52 else
53 printf ("[fail]");
54
U-Stream\Lee09a11c52012-02-17 08:31:16 -080055 if ( !echo )
Lee Thomason1ff38e02012-02-14 18:18:16 -080056 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\Lee09a11c52012-02-17 08:31:16 -080068bool XMLTest( const char* testString, int expected, int found, bool echo=true )
Lee Thomason1ff38e02012-02-14 18:18:16 -080069{
70 bool pass = ( expected == found );
71 if ( pass )
72 printf ("[pass]");
73 else
74 printf ("[fail]");
75
U-Stream\Lee09a11c52012-02-17 08:31:16 -080076 if ( !echo )
Lee Thomason1ff38e02012-02-14 18:18:16 -080077 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 Thomasonec5a7b42012-02-13 18:16:52 -080087
U-Lama\Leee13c3e62011-12-28 14:36:55 -080088
89int main( int argc, const char* argv )
90{
Lee Thomason1ff38e02012-02-14 18:18:16 -080091 #if defined( WIN32 )
92 _CrtMemCheckpoint( &startMemState );
93 #endif
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -080094#ifndef DREAM_ONLY
Lee Thomasone9ecdab2012-02-13 18:11:20 -080095#if 0
U-Lama\Lee4cee6112011-12-31 14:58:18 -080096 {
Lee Thomasonfde6a752012-01-14 18:08:12 -080097 static const char* test = "<!--hello world\n"
Lee Thomason8a5dfee2012-01-18 17:43:40 -080098 " 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 Thomason50adb4c2012-02-13 15:07:09 -0800105 doc.Print();
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800106 }
107#endif
Lee Thomason50adb4c2012-02-13 15:07:09 -0800108#if 0
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800109 {
Lee Thomason43f59302012-02-06 18:18:11 -0800110 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 Thomason8ee79892012-01-25 17:44:30 -0800123 "<element>This &amp; That.</element>",
Lee Thomason18d68bd2012-01-26 18:17:26 -0800124 "<element attrib='This&lt;That' />",
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800125 0
126 };
Lee Thomason6ee99fc2012-01-21 18:45:16 -0800127 for( int i=0; test[i]; ++i ) {
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800128 XMLDocument doc;
Lee Thomason6ee99fc2012-01-21 18:45:16 -0800129 doc.Parse( test[i] );
Lee Thomason5cae8972012-01-24 18:03:07 -0800130 doc.Print();
Lee Thomasonec975ce2012-01-23 11:42:06 -0800131 printf( "----------------------------------------------\n" );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800132 }
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800133 }
Lee Thomason50adb4c2012-02-13 15:07:09 -0800134#endif
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800135#if 0
Lee Thomason2c85a712012-01-31 08:24:24 -0800136 {
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 Thomasond1983222012-02-06 08:41:24 -0800145 {
146 XMLDocument* doc = new XMLDocument();
147 static const char* test = "<element><sub/></element>";
148 doc->Parse( test );
149 delete doc;
150 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800151#endif
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800152 {
Lee Thomason1ff38e02012-02-14 18:18:16 -0800153 // Test: Programmatic DOM
Lee Thomasonec5a7b42012-02-13 18:16:52 -0800154 // Build:
155 // <element>
156 // <!--comment-->
157 // <sub attrib="1" />
158 // <sub attrib="2" />
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800159 // <sub attrib="3" >& Text!</sub>
Lee Thomasonec5a7b42012-02-13 18:16:52 -0800160 // <element>
161
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800162 XMLDocument* doc = new XMLDocument();
Lee Thomason1ff38e02012-02-14 18:18:16 -0800163 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\Lee09a11c52012-02-17 08:31:16 -0800173 sub[2]->InsertFirstChild( doc->NewText( "& Text!" ));
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800174 doc->Print();
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800175 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\Leeae25a442012-02-17 17:48:16 -0800180
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 Thomasone9ecdab2012-02-13 18:11:20 -0800200 delete doc;
201 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800202#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 Thomasonec5a7b42012-02-13 18:16:52 -0800236
Lee Thomason1ff38e02012-02-14 18:18:16 -0800237 #if defined( WIN32 )
238 _CrtMemCheckpoint( &endMemState );
239 //_CrtMemDumpStatistics( &endMemState );
240
241 _CrtMemState diffMemState;
242 _CrtMemDifference( &diffMemState, &startMemState, &endMemState );
243 _CrtMemDumpStatistics( &diffMemState );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800244 //printf( "new total=%d\n", gNewTotal );
Lee Thomason1ff38e02012-02-14 18:18:16 -0800245 #endif
246
247 printf ("\nPass %d, Fail %d\n", gPass, gFail);
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800248 return 0;
249}