1*7485b225SElliott Hughes #if defined( _MSC_VER )
2*7485b225SElliott Hughes #if !defined( _CRT_SECURE_NO_WARNINGS )
3*7485b225SElliott Hughes #define _CRT_SECURE_NO_WARNINGS // This test file is not intended to be secure.
4*7485b225SElliott Hughes #endif
5*7485b225SElliott Hughes #endif
6*7485b225SElliott Hughes
7*7485b225SElliott Hughes #include "tinyxml2.h"
8*7485b225SElliott Hughes #include <cerrno>
9*7485b225SElliott Hughes #include <cstdlib>
10*7485b225SElliott Hughes #include <cstring>
11*7485b225SElliott Hughes #include <ctime>
12*7485b225SElliott Hughes
13*7485b225SElliott Hughes #if defined( _MSC_VER ) || defined (WIN32)
14*7485b225SElliott Hughes #include <crtdbg.h>
15*7485b225SElliott Hughes #define WIN32_LEAN_AND_MEAN
16*7485b225SElliott Hughes #include <windows.h>
17*7485b225SElliott Hughes _CrtMemState startMemState;
18*7485b225SElliott Hughes _CrtMemState endMemState;
19*7485b225SElliott Hughes #else
20*7485b225SElliott Hughes #include <sys/stat.h>
21*7485b225SElliott Hughes #include <sys/types.h>
22*7485b225SElliott Hughes #endif
23*7485b225SElliott Hughes
24*7485b225SElliott Hughes using namespace tinyxml2;
25*7485b225SElliott Hughes using namespace std;
26*7485b225SElliott Hughes int gPass = 0;
27*7485b225SElliott Hughes int gFail = 0;
28*7485b225SElliott Hughes
29*7485b225SElliott Hughes
XMLTest(const char * testString,const char * expected,const char * found,bool echo=true,bool extraNL=false)30*7485b225SElliott Hughes bool XMLTest (const char* testString, const char* expected, const char* found, bool echo=true, bool extraNL=false )
31*7485b225SElliott Hughes {
32*7485b225SElliott Hughes bool pass;
33*7485b225SElliott Hughes if ( !expected && !found )
34*7485b225SElliott Hughes pass = true;
35*7485b225SElliott Hughes else if ( !expected || !found )
36*7485b225SElliott Hughes pass = false;
37*7485b225SElliott Hughes else
38*7485b225SElliott Hughes pass = !strcmp( expected, found );
39*7485b225SElliott Hughes if ( pass )
40*7485b225SElliott Hughes printf ("[pass]");
41*7485b225SElliott Hughes else
42*7485b225SElliott Hughes printf ("[fail]");
43*7485b225SElliott Hughes
44*7485b225SElliott Hughes if ( !echo ) {
45*7485b225SElliott Hughes printf (" %s\n", testString);
46*7485b225SElliott Hughes }
47*7485b225SElliott Hughes else {
48*7485b225SElliott Hughes if ( extraNL ) {
49*7485b225SElliott Hughes printf( " %s\n", testString );
50*7485b225SElliott Hughes printf( "%s\n", expected );
51*7485b225SElliott Hughes printf( "%s\n", found );
52*7485b225SElliott Hughes }
53*7485b225SElliott Hughes else {
54*7485b225SElliott Hughes printf (" %s [%s][%s]\n", testString, expected, found);
55*7485b225SElliott Hughes }
56*7485b225SElliott Hughes }
57*7485b225SElliott Hughes
58*7485b225SElliott Hughes if ( pass )
59*7485b225SElliott Hughes ++gPass;
60*7485b225SElliott Hughes else
61*7485b225SElliott Hughes ++gFail;
62*7485b225SElliott Hughes return pass;
63*7485b225SElliott Hughes }
64*7485b225SElliott Hughes
XMLTest(const char * testString,XMLError expected,XMLError found,bool echo=true,bool extraNL=false)65*7485b225SElliott Hughes bool XMLTest(const char* testString, XMLError expected, XMLError found, bool echo = true, bool extraNL = false)
66*7485b225SElliott Hughes {
67*7485b225SElliott Hughes return XMLTest(testString, XMLDocument::ErrorIDToName(expected), XMLDocument::ErrorIDToName(found), echo, extraNL);
68*7485b225SElliott Hughes }
69*7485b225SElliott Hughes
XMLTest(const char * testString,bool expected,bool found,bool echo=true,bool extraNL=false)70*7485b225SElliott Hughes bool XMLTest(const char* testString, bool expected, bool found, bool echo = true, bool extraNL = false)
71*7485b225SElliott Hughes {
72*7485b225SElliott Hughes return XMLTest(testString, expected ? "true" : "false", found ? "true" : "false", echo, extraNL);
73*7485b225SElliott Hughes }
74*7485b225SElliott Hughes
XMLTest(const char * testString,T expected,T found,bool echo=true)75*7485b225SElliott Hughes template< class T > bool XMLTest( const char* testString, T expected, T found, bool echo=true )
76*7485b225SElliott Hughes {
77*7485b225SElliott Hughes bool pass = ( expected == found );
78*7485b225SElliott Hughes if ( pass )
79*7485b225SElliott Hughes printf ("[pass]");
80*7485b225SElliott Hughes else
81*7485b225SElliott Hughes printf ("[fail]");
82*7485b225SElliott Hughes
83*7485b225SElliott Hughes if ( !echo )
84*7485b225SElliott Hughes printf (" %s\n", testString);
85*7485b225SElliott Hughes else {
86*7485b225SElliott Hughes char expectedAsString[64];
87*7485b225SElliott Hughes XMLUtil::ToStr(expected, expectedAsString, sizeof(expectedAsString));
88*7485b225SElliott Hughes
89*7485b225SElliott Hughes char foundAsString[64];
90*7485b225SElliott Hughes XMLUtil::ToStr(found, foundAsString, sizeof(foundAsString));
91*7485b225SElliott Hughes
92*7485b225SElliott Hughes printf (" %s [%s][%s]\n", testString, expectedAsString, foundAsString );
93*7485b225SElliott Hughes }
94*7485b225SElliott Hughes
95*7485b225SElliott Hughes if ( pass )
96*7485b225SElliott Hughes ++gPass;
97*7485b225SElliott Hughes else
98*7485b225SElliott Hughes ++gFail;
99*7485b225SElliott Hughes return pass;
100*7485b225SElliott Hughes }
101*7485b225SElliott Hughes
102*7485b225SElliott Hughes
NullLineEndings(char * p)103*7485b225SElliott Hughes void NullLineEndings( char* p )
104*7485b225SElliott Hughes {
105*7485b225SElliott Hughes while( p && *p ) {
106*7485b225SElliott Hughes if ( *p == '\n' || *p == '\r' ) {
107*7485b225SElliott Hughes *p = 0;
108*7485b225SElliott Hughes return;
109*7485b225SElliott Hughes }
110*7485b225SElliott Hughes ++p;
111*7485b225SElliott Hughes }
112*7485b225SElliott Hughes }
113*7485b225SElliott Hughes
114*7485b225SElliott Hughes
example_1()115*7485b225SElliott Hughes int example_1()
116*7485b225SElliott Hughes {
117*7485b225SElliott Hughes XMLDocument doc;
118*7485b225SElliott Hughes doc.LoadFile( "resources/dream.xml" );
119*7485b225SElliott Hughes
120*7485b225SElliott Hughes return doc.ErrorID();
121*7485b225SElliott Hughes }
122*7485b225SElliott Hughes /** @page Example_1 Load an XML File
123*7485b225SElliott Hughes * @dontinclude ./xmltest.cpp
124*7485b225SElliott Hughes * Basic XML file loading.
125*7485b225SElliott Hughes * The basic syntax to load an XML file from
126*7485b225SElliott Hughes * disk and check for an error. (ErrorID()
127*7485b225SElliott Hughes * will return 0 for no error.)
128*7485b225SElliott Hughes * @skip example_1()
129*7485b225SElliott Hughes * @until }
130*7485b225SElliott Hughes */
131*7485b225SElliott Hughes
132*7485b225SElliott Hughes
example_2()133*7485b225SElliott Hughes int example_2()
134*7485b225SElliott Hughes {
135*7485b225SElliott Hughes static const char* xml = "<element/>";
136*7485b225SElliott Hughes XMLDocument doc;
137*7485b225SElliott Hughes doc.Parse( xml );
138*7485b225SElliott Hughes
139*7485b225SElliott Hughes return doc.ErrorID();
140*7485b225SElliott Hughes }
141*7485b225SElliott Hughes /** @page Example_2 Parse an XML from char buffer
142*7485b225SElliott Hughes * @dontinclude ./xmltest.cpp
143*7485b225SElliott Hughes * Basic XML string parsing.
144*7485b225SElliott Hughes * The basic syntax to parse an XML for
145*7485b225SElliott Hughes * a char* and check for an error. (ErrorID()
146*7485b225SElliott Hughes * will return 0 for no error.)
147*7485b225SElliott Hughes * @skip example_2()
148*7485b225SElliott Hughes * @until }
149*7485b225SElliott Hughes */
150*7485b225SElliott Hughes
151*7485b225SElliott Hughes
example_3()152*7485b225SElliott Hughes int example_3()
153*7485b225SElliott Hughes {
154*7485b225SElliott Hughes static const char* xml =
155*7485b225SElliott Hughes "<?xml version=\"1.0\"?>"
156*7485b225SElliott Hughes "<!DOCTYPE PLAY SYSTEM \"play.dtd\">"
157*7485b225SElliott Hughes "<PLAY>"
158*7485b225SElliott Hughes "<TITLE>A Midsummer Night's Dream</TITLE>"
159*7485b225SElliott Hughes "</PLAY>";
160*7485b225SElliott Hughes
161*7485b225SElliott Hughes XMLDocument doc;
162*7485b225SElliott Hughes doc.Parse( xml );
163*7485b225SElliott Hughes
164*7485b225SElliott Hughes XMLElement* titleElement = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" );
165*7485b225SElliott Hughes const char* title = titleElement->GetText();
166*7485b225SElliott Hughes printf( "Name of play (1): %s\n", title );
167*7485b225SElliott Hughes
168*7485b225SElliott Hughes XMLText* textNode = titleElement->FirstChild()->ToText();
169*7485b225SElliott Hughes title = textNode->Value();
170*7485b225SElliott Hughes printf( "Name of play (2): %s\n", title );
171*7485b225SElliott Hughes
172*7485b225SElliott Hughes return doc.ErrorID();
173*7485b225SElliott Hughes }
174*7485b225SElliott Hughes /** @page Example_3 Get information out of XML
175*7485b225SElliott Hughes @dontinclude ./xmltest.cpp
176*7485b225SElliott Hughes In this example, we navigate a simple XML
177*7485b225SElliott Hughes file, and read some interesting text. Note
178*7485b225SElliott Hughes that this example doesn't use error
179*7485b225SElliott Hughes checking; working code should check for null
180*7485b225SElliott Hughes pointers when walking an XML tree, or use
181*7485b225SElliott Hughes XMLHandle.
182*7485b225SElliott Hughes
183*7485b225SElliott Hughes (The XML is an excerpt from "dream.xml").
184*7485b225SElliott Hughes
185*7485b225SElliott Hughes @skip example_3()
186*7485b225SElliott Hughes @until </PLAY>";
187*7485b225SElliott Hughes
188*7485b225SElliott Hughes The structure of the XML file is:
189*7485b225SElliott Hughes
190*7485b225SElliott Hughes <ul>
191*7485b225SElliott Hughes <li>(declaration)</li>
192*7485b225SElliott Hughes <li>(dtd stuff)</li>
193*7485b225SElliott Hughes <li>Element "PLAY"</li>
194*7485b225SElliott Hughes <ul>
195*7485b225SElliott Hughes <li>Element "TITLE"</li>
196*7485b225SElliott Hughes <ul>
197*7485b225SElliott Hughes <li>Text "A Midsummer Night's Dream"</li>
198*7485b225SElliott Hughes </ul>
199*7485b225SElliott Hughes </ul>
200*7485b225SElliott Hughes </ul>
201*7485b225SElliott Hughes
202*7485b225SElliott Hughes For this example, we want to print out the
203*7485b225SElliott Hughes title of the play. The text of the title (what
204*7485b225SElliott Hughes we want) is child of the "TITLE" element which
205*7485b225SElliott Hughes is a child of the "PLAY" element.
206*7485b225SElliott Hughes
207*7485b225SElliott Hughes We want to skip the declaration and dtd, so the
208*7485b225SElliott Hughes method FirstChildElement() is a good choice. The
209*7485b225SElliott Hughes FirstChildElement() of the Document is the "PLAY"
210*7485b225SElliott Hughes Element, the FirstChildElement() of the "PLAY" Element
211*7485b225SElliott Hughes is the "TITLE" Element.
212*7485b225SElliott Hughes
213*7485b225SElliott Hughes @until ( "TITLE" );
214*7485b225SElliott Hughes
215*7485b225SElliott Hughes We can then use the convenience function GetText()
216*7485b225SElliott Hughes to get the title of the play.
217*7485b225SElliott Hughes
218*7485b225SElliott Hughes @until title );
219*7485b225SElliott Hughes
220*7485b225SElliott Hughes Text is just another Node in the XML DOM. And in
221*7485b225SElliott Hughes fact you should be a little cautious with it, as
222*7485b225SElliott Hughes text nodes can contain elements.
223*7485b225SElliott Hughes
224*7485b225SElliott Hughes @verbatim
225*7485b225SElliott Hughes Consider: A Midsummer Night's <b>Dream</b>
226*7485b225SElliott Hughes @endverbatim
227*7485b225SElliott Hughes
228*7485b225SElliott Hughes It is more correct to actually query the Text Node
229*7485b225SElliott Hughes if in doubt:
230*7485b225SElliott Hughes
231*7485b225SElliott Hughes @until title );
232*7485b225SElliott Hughes
233*7485b225SElliott Hughes Noting that here we use FirstChild() since we are
234*7485b225SElliott Hughes looking for XMLText, not an element, and ToText()
235*7485b225SElliott Hughes is a cast from a Node to a XMLText.
236*7485b225SElliott Hughes */
237*7485b225SElliott Hughes
238*7485b225SElliott Hughes
example_4()239*7485b225SElliott Hughes bool example_4()
240*7485b225SElliott Hughes {
241*7485b225SElliott Hughes static const char* xml =
242*7485b225SElliott Hughes "<information>"
243*7485b225SElliott Hughes " <attributeApproach v='2' />"
244*7485b225SElliott Hughes " <textApproach>"
245*7485b225SElliott Hughes " <v>2</v>"
246*7485b225SElliott Hughes " </textApproach>"
247*7485b225SElliott Hughes "</information>";
248*7485b225SElliott Hughes
249*7485b225SElliott Hughes XMLDocument doc;
250*7485b225SElliott Hughes doc.Parse( xml );
251*7485b225SElliott Hughes
252*7485b225SElliott Hughes int v0 = 0;
253*7485b225SElliott Hughes int v1 = 0;
254*7485b225SElliott Hughes
255*7485b225SElliott Hughes XMLElement* attributeApproachElement = doc.FirstChildElement()->FirstChildElement( "attributeApproach" );
256*7485b225SElliott Hughes attributeApproachElement->QueryIntAttribute( "v", &v0 );
257*7485b225SElliott Hughes
258*7485b225SElliott Hughes XMLElement* textApproachElement = doc.FirstChildElement()->FirstChildElement( "textApproach" );
259*7485b225SElliott Hughes textApproachElement->FirstChildElement( "v" )->QueryIntText( &v1 );
260*7485b225SElliott Hughes
261*7485b225SElliott Hughes printf( "Both values are the same: %d and %d\n", v0, v1 );
262*7485b225SElliott Hughes
263*7485b225SElliott Hughes return !doc.Error() && ( v0 == v1 );
264*7485b225SElliott Hughes }
265*7485b225SElliott Hughes /** @page Example_4 Read attributes and text information.
266*7485b225SElliott Hughes @dontinclude ./xmltest.cpp
267*7485b225SElliott Hughes
268*7485b225SElliott Hughes There are fundamentally 2 ways of writing a key-value
269*7485b225SElliott Hughes pair into an XML file. (Something that's always annoyed
270*7485b225SElliott Hughes me about XML.) Either by using attributes, or by writing
271*7485b225SElliott Hughes the key name into an element and the value into
272*7485b225SElliott Hughes the text node wrapped by the element. Both approaches
273*7485b225SElliott Hughes are illustrated in this example, which shows two ways
274*7485b225SElliott Hughes to encode the value "2" into the key "v":
275*7485b225SElliott Hughes
276*7485b225SElliott Hughes @skip example_4()
277*7485b225SElliott Hughes @until "</information>";
278*7485b225SElliott Hughes
279*7485b225SElliott Hughes TinyXML-2 has accessors for both approaches.
280*7485b225SElliott Hughes
281*7485b225SElliott Hughes When using an attribute, you navigate to the XMLElement
282*7485b225SElliott Hughes with that attribute and use the QueryIntAttribute()
283*7485b225SElliott Hughes group of methods. (Also QueryFloatAttribute(), etc.)
284*7485b225SElliott Hughes
285*7485b225SElliott Hughes @skip XMLElement* attributeApproachElement
286*7485b225SElliott Hughes @until &v0 );
287*7485b225SElliott Hughes
288*7485b225SElliott Hughes When using the text approach, you need to navigate
289*7485b225SElliott Hughes down one more step to the XMLElement that contains
290*7485b225SElliott Hughes the text. Note the extra FirstChildElement( "v" )
291*7485b225SElliott Hughes in the code below. The value of the text can then
292*7485b225SElliott Hughes be safely queried with the QueryIntText() group
293*7485b225SElliott Hughes of methods. (Also QueryFloatText(), etc.)
294*7485b225SElliott Hughes
295*7485b225SElliott Hughes @skip XMLElement* textApproachElement
296*7485b225SElliott Hughes @until &v1 );
297*7485b225SElliott Hughes */
298*7485b225SElliott Hughes
299*7485b225SElliott Hughes
main(int argc,const char ** argv)300*7485b225SElliott Hughes int main( int argc, const char ** argv )
301*7485b225SElliott Hughes {
302*7485b225SElliott Hughes #if defined( _MSC_VER ) && defined( TINYXML2_DEBUG )
303*7485b225SElliott Hughes _CrtMemCheckpoint( &startMemState );
304*7485b225SElliott Hughes // Enable MS Visual C++ debug heap memory leaks dump on exit
305*7485b225SElliott Hughes _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
306*7485b225SElliott Hughes {
307*7485b225SElliott Hughes int leaksOnStart = _CrtDumpMemoryLeaks();
308*7485b225SElliott Hughes XMLTest( "No leaks on start?", FALSE, leaksOnStart );
309*7485b225SElliott Hughes }
310*7485b225SElliott Hughes #endif
311*7485b225SElliott Hughes
312*7485b225SElliott Hughes {
313*7485b225SElliott Hughes TIXMLASSERT( true );
314*7485b225SElliott Hughes }
315*7485b225SElliott Hughes
316*7485b225SElliott Hughes if ( argc > 1 ) {
317*7485b225SElliott Hughes XMLDocument* doc = new XMLDocument();
318*7485b225SElliott Hughes clock_t startTime = clock();
319*7485b225SElliott Hughes doc->LoadFile( argv[1] );
320*7485b225SElliott Hughes clock_t loadTime = clock();
321*7485b225SElliott Hughes int errorID = doc->ErrorID();
322*7485b225SElliott Hughes delete doc; doc = 0;
323*7485b225SElliott Hughes clock_t deleteTime = clock();
324*7485b225SElliott Hughes
325*7485b225SElliott Hughes printf( "Test file '%s' loaded. ErrorID=%d\n", argv[1], errorID );
326*7485b225SElliott Hughes if ( !errorID ) {
327*7485b225SElliott Hughes printf( "Load time=%u\n", (unsigned)(loadTime - startTime) );
328*7485b225SElliott Hughes printf( "Delete time=%u\n", (unsigned)(deleteTime - loadTime) );
329*7485b225SElliott Hughes printf( "Total time=%u\n", (unsigned)(deleteTime - startTime) );
330*7485b225SElliott Hughes }
331*7485b225SElliott Hughes exit(0);
332*7485b225SElliott Hughes }
333*7485b225SElliott Hughes
334*7485b225SElliott Hughes FILE* fp = fopen( "resources/dream.xml", "r" );
335*7485b225SElliott Hughes if ( !fp ) {
336*7485b225SElliott Hughes printf( "Error opening test file 'dream.xml'.\n"
337*7485b225SElliott Hughes "Is your working directory the same as where \n"
338*7485b225SElliott Hughes "the xmltest.cpp and dream.xml file are?\n\n"
339*7485b225SElliott Hughes #if defined( _MSC_VER )
340*7485b225SElliott Hughes "In windows Visual Studio you may need to set\n"
341*7485b225SElliott Hughes "Properties->Debugging->Working Directory to '..'\n"
342*7485b225SElliott Hughes #endif
343*7485b225SElliott Hughes );
344*7485b225SElliott Hughes exit( 1 );
345*7485b225SElliott Hughes }
346*7485b225SElliott Hughes fclose( fp );
347*7485b225SElliott Hughes
348*7485b225SElliott Hughes XMLTest( "Example_1", 0, example_1() );
349*7485b225SElliott Hughes XMLTest( "Example_2", 0, example_2() );
350*7485b225SElliott Hughes XMLTest( "Example_3", 0, example_3() );
351*7485b225SElliott Hughes XMLTest( "Example_4", true, example_4() );
352*7485b225SElliott Hughes
353*7485b225SElliott Hughes /* ------ Example 2: Lookup information. ---- */
354*7485b225SElliott Hughes
355*7485b225SElliott Hughes {
356*7485b225SElliott Hughes static const char* test[] = { "<element />",
357*7485b225SElliott Hughes "<element></element>",
358*7485b225SElliott Hughes "<element><subelement/></element>",
359*7485b225SElliott Hughes "<element><subelement></subelement></element>",
360*7485b225SElliott Hughes "<element><subelement><subsub/></subelement></element>",
361*7485b225SElliott Hughes "<!--comment beside elements--><element><subelement></subelement></element>",
362*7485b225SElliott Hughes "<!--comment beside elements, this time with spaces--> \n <element> <subelement> \n </subelement> </element>",
363*7485b225SElliott Hughes "<element attrib1='foo' attrib2=\"bar\" ></element>",
364*7485b225SElliott Hughes "<element attrib1='foo' attrib2=\"bar\" ><subelement attrib3='yeehaa' /></element>",
365*7485b225SElliott Hughes "<element>Text inside element.</element>",
366*7485b225SElliott Hughes "<element><b></b></element>",
367*7485b225SElliott Hughes "<element>Text inside and <b>bolded</b> in the element.</element>",
368*7485b225SElliott Hughes "<outer><element>Text inside and <b>bolded</b> in the element.</element></outer>",
369*7485b225SElliott Hughes "<element>This & That.</element>",
370*7485b225SElliott Hughes "<element attrib='This<That' />",
371*7485b225SElliott Hughes 0
372*7485b225SElliott Hughes };
373*7485b225SElliott Hughes for( int i=0; test[i]; ++i ) {
374*7485b225SElliott Hughes XMLDocument doc;
375*7485b225SElliott Hughes doc.Parse( test[i] );
376*7485b225SElliott Hughes XMLTest( "Element test", false, doc.Error() );
377*7485b225SElliott Hughes doc.Print();
378*7485b225SElliott Hughes printf( "----------------------------------------------\n" );
379*7485b225SElliott Hughes }
380*7485b225SElliott Hughes }
381*7485b225SElliott Hughes #if 1
382*7485b225SElliott Hughes {
383*7485b225SElliott Hughes static const char* test = "<!--hello world\n"
384*7485b225SElliott Hughes " line 2\r"
385*7485b225SElliott Hughes " line 3\r\n"
386*7485b225SElliott Hughes " line 4\n\r"
387*7485b225SElliott Hughes " line 5\r-->";
388*7485b225SElliott Hughes
389*7485b225SElliott Hughes XMLDocument doc;
390*7485b225SElliott Hughes doc.Parse( test );
391*7485b225SElliott Hughes XMLTest( "Hello world declaration", false, doc.Error() );
392*7485b225SElliott Hughes doc.Print();
393*7485b225SElliott Hughes }
394*7485b225SElliott Hughes
395*7485b225SElliott Hughes {
396*7485b225SElliott Hughes // This test is pre-test for the next one
397*7485b225SElliott Hughes // (where Element1 is inserted "after itself".
398*7485b225SElliott Hughes // This code didn't use to crash.
399*7485b225SElliott Hughes XMLDocument doc;
400*7485b225SElliott Hughes XMLElement* element1 = doc.NewElement("Element1");
401*7485b225SElliott Hughes XMLElement* element2 = doc.NewElement("Element2");
402*7485b225SElliott Hughes doc.InsertEndChild(element1);
403*7485b225SElliott Hughes doc.InsertEndChild(element2);
404*7485b225SElliott Hughes doc.InsertAfterChild(element2, element2);
405*7485b225SElliott Hughes doc.InsertAfterChild(element2, element2);
406*7485b225SElliott Hughes }
407*7485b225SElliott Hughes
408*7485b225SElliott Hughes {
409*7485b225SElliott Hughes XMLDocument doc;
410*7485b225SElliott Hughes XMLElement* element1 = doc.NewElement("Element1");
411*7485b225SElliott Hughes XMLElement* element2 = doc.NewElement("Element2");
412*7485b225SElliott Hughes doc.InsertEndChild(element1);
413*7485b225SElliott Hughes doc.InsertEndChild(element2);
414*7485b225SElliott Hughes
415*7485b225SElliott Hughes // This insertion "after itself"
416*7485b225SElliott Hughes // used to cause invalid memory access and crash
417*7485b225SElliott Hughes doc.InsertAfterChild(element1, element1);
418*7485b225SElliott Hughes doc.InsertAfterChild(element1, element1);
419*7485b225SElliott Hughes doc.InsertAfterChild(element2, element2);
420*7485b225SElliott Hughes doc.InsertAfterChild(element2, element2);
421*7485b225SElliott Hughes }
422*7485b225SElliott Hughes
423*7485b225SElliott Hughes {
424*7485b225SElliott Hughes static const char* test = "<element>Text before.</element>";
425*7485b225SElliott Hughes XMLDocument doc;
426*7485b225SElliott Hughes doc.Parse( test );
427*7485b225SElliott Hughes XMLTest( "Element text before", false, doc.Error() );
428*7485b225SElliott Hughes XMLElement* root = doc.FirstChildElement();
429*7485b225SElliott Hughes XMLElement* newElement = doc.NewElement( "Subelement" );
430*7485b225SElliott Hughes root->InsertEndChild( newElement );
431*7485b225SElliott Hughes doc.Print();
432*7485b225SElliott Hughes }
433*7485b225SElliott Hughes {
434*7485b225SElliott Hughes XMLDocument* doc = new XMLDocument();
435*7485b225SElliott Hughes static const char* test = "<element><sub/></element>";
436*7485b225SElliott Hughes doc->Parse( test );
437*7485b225SElliott Hughes XMLTest( "Element with sub element", false, doc->Error() );
438*7485b225SElliott Hughes delete doc;
439*7485b225SElliott Hughes }
440*7485b225SElliott Hughes {
441*7485b225SElliott Hughes // Test: Programmatic DOM nodes insertion return values
442*7485b225SElliott Hughes XMLDocument doc;
443*7485b225SElliott Hughes
444*7485b225SElliott Hughes XMLNode* first = doc.NewElement( "firstElement" );
445*7485b225SElliott Hughes XMLTest( "New element", true, first != 0 );
446*7485b225SElliott Hughes XMLNode* firstAfterInsertion = doc.InsertFirstChild( first );
447*7485b225SElliott Hughes XMLTest( "New element inserted first", true, firstAfterInsertion == first );
448*7485b225SElliott Hughes
449*7485b225SElliott Hughes XMLNode* last = doc.NewElement( "lastElement" );
450*7485b225SElliott Hughes XMLTest( "New element", true, last != 0 );
451*7485b225SElliott Hughes XMLNode* lastAfterInsertion = doc.InsertEndChild( last );
452*7485b225SElliott Hughes XMLTest( "New element inserted last", true, lastAfterInsertion == last );
453*7485b225SElliott Hughes
454*7485b225SElliott Hughes XMLNode* middle = doc.NewElement( "middleElement" );
455*7485b225SElliott Hughes XMLTest( "New element", true, middle != 0 );
456*7485b225SElliott Hughes XMLNode* middleAfterInsertion = doc.InsertAfterChild( first, middle );
457*7485b225SElliott Hughes XMLTest( "New element inserted middle", true, middleAfterInsertion == middle );
458*7485b225SElliott Hughes }
459*7485b225SElliott Hughes {
460*7485b225SElliott Hughes // Test: Programmatic DOM
461*7485b225SElliott Hughes // Build:
462*7485b225SElliott Hughes // <element>
463*7485b225SElliott Hughes // <!--comment-->
464*7485b225SElliott Hughes // <sub attrib="0" />
465*7485b225SElliott Hughes // <sub attrib="1" />
466*7485b225SElliott Hughes // <sub attrib="2" >& Text!</sub>
467*7485b225SElliott Hughes // <element>
468*7485b225SElliott Hughes
469*7485b225SElliott Hughes XMLDocument* doc = new XMLDocument();
470*7485b225SElliott Hughes XMLNode* element = doc->InsertEndChild( doc->NewElement( "element" ) );
471*7485b225SElliott Hughes
472*7485b225SElliott Hughes XMLElement* sub[3] = { doc->NewElement( "sub" ), doc->NewElement( "sub" ), doc->NewElement( "sub" ) };
473*7485b225SElliott Hughes for( int i=0; i<3; ++i ) {
474*7485b225SElliott Hughes sub[i]->SetAttribute( "attrib", i );
475*7485b225SElliott Hughes }
476*7485b225SElliott Hughes element->InsertEndChild( sub[2] );
477*7485b225SElliott Hughes
478*7485b225SElliott Hughes const int dummyInitialValue = 1000;
479*7485b225SElliott Hughes int dummyValue = dummyInitialValue;
480*7485b225SElliott Hughes
481*7485b225SElliott Hughes XMLNode* comment = element->InsertFirstChild( doc->NewComment( "comment" ) );
482*7485b225SElliott Hughes comment->SetUserData(&dummyValue);
483*7485b225SElliott Hughes element->InsertAfterChild( comment, sub[0] );
484*7485b225SElliott Hughes element->InsertAfterChild( sub[0], sub[1] );
485*7485b225SElliott Hughes sub[2]->InsertFirstChild( doc->NewText( "& Text!" ));
486*7485b225SElliott Hughes doc->Print();
487*7485b225SElliott Hughes XMLTest( "Programmatic DOM", "comment", doc->FirstChildElement( "element" )->FirstChild()->Value() );
488*7485b225SElliott Hughes XMLTest( "Programmatic DOM", "0", doc->FirstChildElement( "element" )->FirstChildElement()->Attribute( "attrib" ) );
489*7485b225SElliott Hughes XMLTest( "Programmatic DOM", 2, doc->FirstChildElement()->LastChildElement( "sub" )->IntAttribute( "attrib" ) );
490*7485b225SElliott Hughes XMLTest( "Programmatic DOM", "& Text!",
491*7485b225SElliott Hughes doc->FirstChildElement()->LastChildElement( "sub" )->FirstChild()->ToText()->Value() );
492*7485b225SElliott Hughes XMLTest("User data - pointer", true, &dummyValue == comment->GetUserData(), false);
493*7485b225SElliott Hughes XMLTest("User data - value behind pointer", dummyInitialValue, dummyValue, false);
494*7485b225SElliott Hughes
495*7485b225SElliott Hughes // And now deletion:
496*7485b225SElliott Hughes element->DeleteChild( sub[2] );
497*7485b225SElliott Hughes doc->DeleteNode( comment );
498*7485b225SElliott Hughes
499*7485b225SElliott Hughes element->FirstChildElement()->SetAttribute( "attrib", true );
500*7485b225SElliott Hughes element->LastChildElement()->DeleteAttribute( "attrib" );
501*7485b225SElliott Hughes
502*7485b225SElliott Hughes XMLTest( "Programmatic DOM", true, doc->FirstChildElement()->FirstChildElement()->BoolAttribute( "attrib" ) );
503*7485b225SElliott Hughes const int defaultIntValue = 10;
504*7485b225SElliott Hughes const int replacementIntValue = 20;
505*7485b225SElliott Hughes int value1 = defaultIntValue;
506*7485b225SElliott Hughes int value2 = doc->FirstChildElement()->LastChildElement()->IntAttribute( "attrib", replacementIntValue );
507*7485b225SElliott Hughes XMLError result = doc->FirstChildElement()->LastChildElement()->QueryIntAttribute( "attrib", &value1 );
508*7485b225SElliott Hughes XMLTest( "Programmatic DOM", XML_NO_ATTRIBUTE, result );
509*7485b225SElliott Hughes XMLTest( "Programmatic DOM", defaultIntValue, value1 );
510*7485b225SElliott Hughes XMLTest( "Programmatic DOM", replacementIntValue, value2 );
511*7485b225SElliott Hughes
512*7485b225SElliott Hughes doc->Print();
513*7485b225SElliott Hughes
514*7485b225SElliott Hughes {
515*7485b225SElliott Hughes XMLPrinter streamer;
516*7485b225SElliott Hughes doc->Print( &streamer );
517*7485b225SElliott Hughes printf( "%s", streamer.CStr() );
518*7485b225SElliott Hughes }
519*7485b225SElliott Hughes {
520*7485b225SElliott Hughes XMLPrinter streamer( 0, true );
521*7485b225SElliott Hughes doc->Print( &streamer );
522*7485b225SElliott Hughes XMLTest( "Compact mode", "<element><sub attrib=\"true\"/><sub/></element>", streamer.CStr(), false );
523*7485b225SElliott Hughes }
524*7485b225SElliott Hughes doc->SaveFile( "./resources/out/pretty.xml" );
525*7485b225SElliott Hughes XMLTest( "Save pretty.xml", false, doc->Error() );
526*7485b225SElliott Hughes doc->SaveFile( "./resources/out/compact.xml", true );
527*7485b225SElliott Hughes XMLTest( "Save compact.xml", false, doc->Error() );
528*7485b225SElliott Hughes delete doc;
529*7485b225SElliott Hughes }
530*7485b225SElliott Hughes {
531*7485b225SElliott Hughes // Test: Dream
532*7485b225SElliott Hughes // XML1 : 1,187,569 bytes in 31,209 allocations
533*7485b225SElliott Hughes // XML2 : 469,073 bytes in 323 allocations
534*7485b225SElliott Hughes //int newStart = gNew;
535*7485b225SElliott Hughes XMLDocument doc;
536*7485b225SElliott Hughes doc.LoadFile( "resources/dream.xml" );
537*7485b225SElliott Hughes XMLTest( "Load dream.xml", false, doc.Error() );
538*7485b225SElliott Hughes
539*7485b225SElliott Hughes doc.SaveFile( "resources/out/dreamout.xml" );
540*7485b225SElliott Hughes XMLTest( "Save dreamout.xml", false, doc.Error() );
541*7485b225SElliott Hughes doc.PrintError();
542*7485b225SElliott Hughes
543*7485b225SElliott Hughes XMLTest( "Dream", "xml version=\"1.0\"",
544*7485b225SElliott Hughes doc.FirstChild()->ToDeclaration()->Value() );
545*7485b225SElliott Hughes XMLTest( "Dream", true, doc.FirstChild()->NextSibling()->ToUnknown() != 0 );
546*7485b225SElliott Hughes XMLTest( "Dream", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
547*7485b225SElliott Hughes doc.FirstChild()->NextSibling()->ToUnknown()->Value() );
548*7485b225SElliott Hughes XMLTest( "Dream", "And Robin shall restore amends.",
549*7485b225SElliott Hughes doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
550*7485b225SElliott Hughes XMLTest( "Dream", "And Robin shall restore amends.",
551*7485b225SElliott Hughes doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
552*7485b225SElliott Hughes
553*7485b225SElliott Hughes XMLDocument doc2;
554*7485b225SElliott Hughes doc2.LoadFile( "resources/out/dreamout.xml" );
555*7485b225SElliott Hughes XMLTest( "Load dreamout.xml", false, doc2.Error() );
556*7485b225SElliott Hughes XMLTest( "Dream-out", "xml version=\"1.0\"",
557*7485b225SElliott Hughes doc2.FirstChild()->ToDeclaration()->Value() );
558*7485b225SElliott Hughes XMLTest( "Dream-out", true, doc2.FirstChild()->NextSibling()->ToUnknown() != 0 );
559*7485b225SElliott Hughes XMLTest( "Dream-out", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
560*7485b225SElliott Hughes doc2.FirstChild()->NextSibling()->ToUnknown()->Value() );
561*7485b225SElliott Hughes XMLTest( "Dream-out", "And Robin shall restore amends.",
562*7485b225SElliott Hughes doc2.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
563*7485b225SElliott Hughes
564*7485b225SElliott Hughes //gNewTotal = gNew - newStart;
565*7485b225SElliott Hughes }
566*7485b225SElliott Hughes
567*7485b225SElliott Hughes
568*7485b225SElliott Hughes {
569*7485b225SElliott Hughes const char* error = "<?xml version=\"1.0\" standalone=\"no\" ?>\n"
570*7485b225SElliott Hughes "<passages count=\"006\" formatversion=\"20020620\">\n"
571*7485b225SElliott Hughes " <wrong error>\n"
572*7485b225SElliott Hughes "</passages>";
573*7485b225SElliott Hughes
574*7485b225SElliott Hughes XMLDocument doc;
575*7485b225SElliott Hughes doc.Parse( error );
576*7485b225SElliott Hughes XMLTest( "Bad XML", XML_ERROR_PARSING_ATTRIBUTE, doc.ErrorID() );
577*7485b225SElliott Hughes const char* errorStr = doc.ErrorStr();
578*7485b225SElliott Hughes XMLTest("Formatted error string",
579*7485b225SElliott Hughes "Error=XML_ERROR_PARSING_ATTRIBUTE ErrorID=7 (0x7) Line number=3: XMLElement name=wrong",
580*7485b225SElliott Hughes errorStr);
581*7485b225SElliott Hughes }
582*7485b225SElliott Hughes
583*7485b225SElliott Hughes {
584*7485b225SElliott Hughes const char* str = "<doc attr0='1' attr1='2.0' attr2='foo' />";
585*7485b225SElliott Hughes
586*7485b225SElliott Hughes XMLDocument doc;
587*7485b225SElliott Hughes doc.Parse( str );
588*7485b225SElliott Hughes XMLTest( "Top level attributes", false, doc.Error() );
589*7485b225SElliott Hughes
590*7485b225SElliott Hughes XMLElement* ele = doc.FirstChildElement();
591*7485b225SElliott Hughes
592*7485b225SElliott Hughes int iVal;
593*7485b225SElliott Hughes XMLError result;
594*7485b225SElliott Hughes double dVal;
595*7485b225SElliott Hughes
596*7485b225SElliott Hughes result = ele->QueryDoubleAttribute( "attr0", &dVal );
597*7485b225SElliott Hughes XMLTest( "Query attribute: int as double", XML_SUCCESS, result);
598*7485b225SElliott Hughes XMLTest( "Query attribute: int as double", 1, (int)dVal );
599*7485b225SElliott Hughes XMLTest( "Query attribute: int as double", 1, (int)ele->DoubleAttribute("attr0"));
600*7485b225SElliott Hughes
601*7485b225SElliott Hughes result = ele->QueryDoubleAttribute( "attr1", &dVal );
602*7485b225SElliott Hughes XMLTest( "Query attribute: double as double", XML_SUCCESS, result);
603*7485b225SElliott Hughes XMLTest( "Query attribute: double as double", 2.0, dVal );
604*7485b225SElliott Hughes XMLTest( "Query attribute: double as double", 2.0, ele->DoubleAttribute("attr1") );
605*7485b225SElliott Hughes
606*7485b225SElliott Hughes result = ele->QueryIntAttribute( "attr1", &iVal );
607*7485b225SElliott Hughes XMLTest( "Query attribute: double as int", XML_SUCCESS, result);
608*7485b225SElliott Hughes XMLTest( "Query attribute: double as int", 2, iVal );
609*7485b225SElliott Hughes
610*7485b225SElliott Hughes result = ele->QueryIntAttribute( "attr2", &iVal );
611*7485b225SElliott Hughes XMLTest( "Query attribute: not a number", XML_WRONG_ATTRIBUTE_TYPE, result );
612*7485b225SElliott Hughes XMLTest( "Query attribute: not a number", 4.0, ele->DoubleAttribute("attr2", 4.0) );
613*7485b225SElliott Hughes
614*7485b225SElliott Hughes result = ele->QueryIntAttribute( "bar", &iVal );
615*7485b225SElliott Hughes XMLTest( "Query attribute: does not exist", XML_NO_ATTRIBUTE, result );
616*7485b225SElliott Hughes XMLTest( "Query attribute: does not exist", true, ele->BoolAttribute("bar", true) );
617*7485b225SElliott Hughes }
618*7485b225SElliott Hughes
619*7485b225SElliott Hughes {
620*7485b225SElliott Hughes const char* str = "<doc/>";
621*7485b225SElliott Hughes
622*7485b225SElliott Hughes XMLDocument doc;
623*7485b225SElliott Hughes doc.Parse( str );
624*7485b225SElliott Hughes XMLTest( "Empty top element", false, doc.Error() );
625*7485b225SElliott Hughes
626*7485b225SElliott Hughes XMLElement* ele = doc.FirstChildElement();
627*7485b225SElliott Hughes
628*7485b225SElliott Hughes int iVal, iVal2;
629*7485b225SElliott Hughes double dVal, dVal2;
630*7485b225SElliott Hughes
631*7485b225SElliott Hughes ele->SetAttribute( "str", "strValue" );
632*7485b225SElliott Hughes ele->SetAttribute( "int", 1 );
633*7485b225SElliott Hughes ele->SetAttribute( "double", -1.0 );
634*7485b225SElliott Hughes
635*7485b225SElliott Hughes const char* answer = 0;
636*7485b225SElliott Hughes ele->QueryAttribute("str", &answer);
637*7485b225SElliott Hughes XMLTest("Query char attribute", "strValue", answer);
638*7485b225SElliott Hughes
639*7485b225SElliott Hughes const char* cStr = ele->Attribute( "str" );
640*7485b225SElliott Hughes {
641*7485b225SElliott Hughes XMLError queryResult = ele->QueryIntAttribute( "int", &iVal );
642*7485b225SElliott Hughes XMLTest( "Query int attribute", XML_SUCCESS, queryResult);
643*7485b225SElliott Hughes }
644*7485b225SElliott Hughes {
645*7485b225SElliott Hughes XMLError queryResult = ele->QueryDoubleAttribute( "double", &dVal );
646*7485b225SElliott Hughes XMLTest( "Query double attribute", XML_SUCCESS, queryResult);
647*7485b225SElliott Hughes }
648*7485b225SElliott Hughes
649*7485b225SElliott Hughes {
650*7485b225SElliott Hughes XMLError queryResult = ele->QueryAttribute( "int", &iVal2 );
651*7485b225SElliott Hughes XMLTest( "Query int attribute generic", (int)XML_SUCCESS, queryResult);
652*7485b225SElliott Hughes }
653*7485b225SElliott Hughes {
654*7485b225SElliott Hughes XMLError queryResult = ele->QueryAttribute( "double", &dVal2 );
655*7485b225SElliott Hughes XMLTest( "Query double attribute generic", (int)XML_SUCCESS, queryResult);
656*7485b225SElliott Hughes }
657*7485b225SElliott Hughes
658*7485b225SElliott Hughes XMLTest( "Attribute match test", "strValue", ele->Attribute( "str", "strValue" ) );
659*7485b225SElliott Hughes XMLTest( "Attribute round trip. c-string.", "strValue", cStr );
660*7485b225SElliott Hughes XMLTest( "Attribute round trip. int.", 1, iVal );
661*7485b225SElliott Hughes XMLTest( "Attribute round trip. double.", -1, (int)dVal );
662*7485b225SElliott Hughes XMLTest( "Alternate query", true, iVal == iVal2 );
663*7485b225SElliott Hughes XMLTest( "Alternate query", true, dVal == dVal2 );
664*7485b225SElliott Hughes XMLTest( "Alternate query", true, iVal == ele->IntAttribute("int") );
665*7485b225SElliott Hughes XMLTest( "Alternate query", true, dVal == ele->DoubleAttribute("double") );
666*7485b225SElliott Hughes }
667*7485b225SElliott Hughes
668*7485b225SElliott Hughes {
669*7485b225SElliott Hughes XMLDocument doc;
670*7485b225SElliott Hughes doc.LoadFile( "resources/utf8test.xml" );
671*7485b225SElliott Hughes XMLTest( "Load utf8test.xml", false, doc.Error() );
672*7485b225SElliott Hughes
673*7485b225SElliott Hughes // Get the attribute "value" from the "Russian" element and check it.
674*7485b225SElliott Hughes XMLElement* element = doc.FirstChildElement( "document" )->FirstChildElement( "Russian" );
675*7485b225SElliott Hughes const unsigned char correctValue[] = { 0xd1U, 0x86U, 0xd0U, 0xb5U, 0xd0U, 0xbdU, 0xd0U, 0xbdU,
676*7485b225SElliott Hughes 0xd0U, 0xbeU, 0xd1U, 0x81U, 0xd1U, 0x82U, 0xd1U, 0x8cU, 0 };
677*7485b225SElliott Hughes
678*7485b225SElliott Hughes XMLTest( "UTF-8: Russian value.", (const char*)correctValue, element->Attribute( "value" ) );
679*7485b225SElliott Hughes
680*7485b225SElliott Hughes const unsigned char russianElementName[] = { 0xd0U, 0xa0U, 0xd1U, 0x83U,
681*7485b225SElliott Hughes 0xd1U, 0x81U, 0xd1U, 0x81U,
682*7485b225SElliott Hughes 0xd0U, 0xbaU, 0xd0U, 0xb8U,
683*7485b225SElliott Hughes 0xd0U, 0xb9U, 0 };
684*7485b225SElliott Hughes const char russianText[] = "<\xD0\xB8\xD0\xBC\xD0\xB5\xD0\xB5\xD1\x82>";
685*7485b225SElliott Hughes
686*7485b225SElliott Hughes XMLText* text = doc.FirstChildElement( "document" )->FirstChildElement( (const char*) russianElementName )->FirstChild()->ToText();
687*7485b225SElliott Hughes XMLTest( "UTF-8: Browsing russian element name.",
688*7485b225SElliott Hughes russianText,
689*7485b225SElliott Hughes text->Value() );
690*7485b225SElliott Hughes
691*7485b225SElliott Hughes // Now try for a round trip.
692*7485b225SElliott Hughes doc.SaveFile( "resources/out/utf8testout.xml" );
693*7485b225SElliott Hughes XMLTest( "UTF-8: Save testout.xml", false, doc.Error() );
694*7485b225SElliott Hughes
695*7485b225SElliott Hughes // Check the round trip.
696*7485b225SElliott Hughes bool roundTripOkay = false;
697*7485b225SElliott Hughes
698*7485b225SElliott Hughes FILE* saved = fopen( "resources/out/utf8testout.xml", "r" );
699*7485b225SElliott Hughes XMLTest( "UTF-8: Open utf8testout.xml", true, saved != 0 );
700*7485b225SElliott Hughes
701*7485b225SElliott Hughes FILE* verify = fopen( "resources/utf8testverify.xml", "r" );
702*7485b225SElliott Hughes XMLTest( "UTF-8: Open utf8testverify.xml", true, verify != 0 );
703*7485b225SElliott Hughes
704*7485b225SElliott Hughes if ( saved && verify )
705*7485b225SElliott Hughes {
706*7485b225SElliott Hughes roundTripOkay = true;
707*7485b225SElliott Hughes char verifyBuf[256];
708*7485b225SElliott Hughes while ( fgets( verifyBuf, 256, verify ) )
709*7485b225SElliott Hughes {
710*7485b225SElliott Hughes char savedBuf[256];
711*7485b225SElliott Hughes fgets( savedBuf, 256, saved );
712*7485b225SElliott Hughes NullLineEndings( verifyBuf );
713*7485b225SElliott Hughes NullLineEndings( savedBuf );
714*7485b225SElliott Hughes
715*7485b225SElliott Hughes if ( strcmp( verifyBuf, savedBuf ) )
716*7485b225SElliott Hughes {
717*7485b225SElliott Hughes printf( "verify:%s<\n", verifyBuf );
718*7485b225SElliott Hughes printf( "saved :%s<\n", savedBuf );
719*7485b225SElliott Hughes roundTripOkay = false;
720*7485b225SElliott Hughes break;
721*7485b225SElliott Hughes }
722*7485b225SElliott Hughes }
723*7485b225SElliott Hughes }
724*7485b225SElliott Hughes if ( saved )
725*7485b225SElliott Hughes fclose( saved );
726*7485b225SElliott Hughes if ( verify )
727*7485b225SElliott Hughes fclose( verify );
728*7485b225SElliott Hughes XMLTest( "UTF-8: Verified multi-language round trip.", true, roundTripOkay );
729*7485b225SElliott Hughes }
730*7485b225SElliott Hughes
731*7485b225SElliott Hughes // --------GetText()-----------
732*7485b225SElliott Hughes {
733*7485b225SElliott Hughes const char* str = "<foo>This is text</foo>";
734*7485b225SElliott Hughes XMLDocument doc;
735*7485b225SElliott Hughes doc.Parse( str );
736*7485b225SElliott Hughes XMLTest( "Double whitespace", false, doc.Error() );
737*7485b225SElliott Hughes const XMLElement* element = doc.RootElement();
738*7485b225SElliott Hughes
739*7485b225SElliott Hughes XMLTest( "GetText() normal use.", "This is text", element->GetText() );
740*7485b225SElliott Hughes
741*7485b225SElliott Hughes str = "<foo><b>This is text</b></foo>";
742*7485b225SElliott Hughes doc.Parse( str );
743*7485b225SElliott Hughes XMLTest( "Bold text simulation", false, doc.Error() );
744*7485b225SElliott Hughes element = doc.RootElement();
745*7485b225SElliott Hughes
746*7485b225SElliott Hughes XMLTest( "GetText() contained element.", element->GetText() == 0, true );
747*7485b225SElliott Hughes }
748*7485b225SElliott Hughes
749*7485b225SElliott Hughes
750*7485b225SElliott Hughes // --------SetText()-----------
751*7485b225SElliott Hughes {
752*7485b225SElliott Hughes const char* str = "<foo></foo>";
753*7485b225SElliott Hughes XMLDocument doc;
754*7485b225SElliott Hughes doc.Parse( str );
755*7485b225SElliott Hughes XMLTest( "Empty closed element", false, doc.Error() );
756*7485b225SElliott Hughes XMLElement* element = doc.RootElement();
757*7485b225SElliott Hughes
758*7485b225SElliott Hughes element->SetText("darkness.");
759*7485b225SElliott Hughes XMLTest( "SetText() normal use (open/close).", "darkness.", element->GetText() );
760*7485b225SElliott Hughes
761*7485b225SElliott Hughes element->SetText("blue flame.");
762*7485b225SElliott Hughes XMLTest( "SetText() replace.", "blue flame.", element->GetText() );
763*7485b225SElliott Hughes
764*7485b225SElliott Hughes str = "<foo/>";
765*7485b225SElliott Hughes doc.Parse( str );
766*7485b225SElliott Hughes XMLTest( "Empty self-closed element", false, doc.Error() );
767*7485b225SElliott Hughes element = doc.RootElement();
768*7485b225SElliott Hughes
769*7485b225SElliott Hughes element->SetText("The driver");
770*7485b225SElliott Hughes XMLTest( "SetText() normal use. (self-closing)", "The driver", element->GetText() );
771*7485b225SElliott Hughes
772*7485b225SElliott Hughes element->SetText("<b>horses</b>");
773*7485b225SElliott Hughes XMLTest( "SetText() replace with tag-like text.", "<b>horses</b>", element->GetText() );
774*7485b225SElliott Hughes //doc.Print();
775*7485b225SElliott Hughes
776*7485b225SElliott Hughes str = "<foo><bar>Text in nested element</bar></foo>";
777*7485b225SElliott Hughes doc.Parse( str );
778*7485b225SElliott Hughes XMLTest( "Text in nested element", false, doc.Error() );
779*7485b225SElliott Hughes element = doc.RootElement();
780*7485b225SElliott Hughes
781*7485b225SElliott Hughes element->SetText("wolves");
782*7485b225SElliott Hughes XMLTest( "SetText() prefix to nested non-text children.", "wolves", element->GetText() );
783*7485b225SElliott Hughes
784*7485b225SElliott Hughes str = "<foo/>";
785*7485b225SElliott Hughes doc.Parse( str );
786*7485b225SElliott Hughes XMLTest( "Empty self-closed element round 2", false, doc.Error() );
787*7485b225SElliott Hughes element = doc.RootElement();
788*7485b225SElliott Hughes
789*7485b225SElliott Hughes element->SetText( "str" );
790*7485b225SElliott Hughes XMLTest( "SetText types", "str", element->GetText() );
791*7485b225SElliott Hughes
792*7485b225SElliott Hughes element->SetText( 1 );
793*7485b225SElliott Hughes XMLTest( "SetText types", "1", element->GetText() );
794*7485b225SElliott Hughes
795*7485b225SElliott Hughes element->SetText( 1U );
796*7485b225SElliott Hughes XMLTest( "SetText types", "1", element->GetText() );
797*7485b225SElliott Hughes
798*7485b225SElliott Hughes element->SetText( true );
799*7485b225SElliott Hughes XMLTest( "SetText types", "true", element->GetText() );
800*7485b225SElliott Hughes
801*7485b225SElliott Hughes element->SetText( 1.5f );
802*7485b225SElliott Hughes XMLTest( "SetText types", "1.5", element->GetText() );
803*7485b225SElliott Hughes
804*7485b225SElliott Hughes element->SetText( 1.5 );
805*7485b225SElliott Hughes XMLTest( "SetText types", "1.5", element->GetText() );
806*7485b225SElliott Hughes }
807*7485b225SElliott Hughes
808*7485b225SElliott Hughes // ---------- Attributes ---------
809*7485b225SElliott Hughes {
810*7485b225SElliott Hughes static const int64_t BIG = -123456789012345678;
811*7485b225SElliott Hughes static const uint64_t BIG_POS = 123456789012345678;
812*7485b225SElliott Hughes XMLDocument doc;
813*7485b225SElliott Hughes XMLElement* element = doc.NewElement("element");
814*7485b225SElliott Hughes doc.InsertFirstChild(element);
815*7485b225SElliott Hughes
816*7485b225SElliott Hughes {
817*7485b225SElliott Hughes element->SetAttribute("attrib", int(-100));
818*7485b225SElliott Hughes {
819*7485b225SElliott Hughes int v = 0;
820*7485b225SElliott Hughes XMLError queryResult = element->QueryIntAttribute("attrib", &v);
821*7485b225SElliott Hughes XMLTest("Attribute: int", XML_SUCCESS, queryResult, true);
822*7485b225SElliott Hughes XMLTest("Attribute: int", -100, v, true);
823*7485b225SElliott Hughes }
824*7485b225SElliott Hughes {
825*7485b225SElliott Hughes int v = 0;
826*7485b225SElliott Hughes XMLError queryResult = element->QueryAttribute("attrib", &v);
827*7485b225SElliott Hughes XMLTest("Attribute: int", (int)XML_SUCCESS, queryResult, true);
828*7485b225SElliott Hughes XMLTest("Attribute: int", -100, v, true);
829*7485b225SElliott Hughes }
830*7485b225SElliott Hughes XMLTest("Attribute: int", -100, element->IntAttribute("attrib"), true);
831*7485b225SElliott Hughes }
832*7485b225SElliott Hughes {
833*7485b225SElliott Hughes element->SetAttribute("attrib", unsigned(100));
834*7485b225SElliott Hughes {
835*7485b225SElliott Hughes unsigned v = 0;
836*7485b225SElliott Hughes XMLError queryResult = element->QueryUnsignedAttribute("attrib", &v);
837*7485b225SElliott Hughes XMLTest("Attribute: unsigned", XML_SUCCESS, queryResult, true);
838*7485b225SElliott Hughes XMLTest("Attribute: unsigned", unsigned(100), v, true);
839*7485b225SElliott Hughes }
840*7485b225SElliott Hughes {
841*7485b225SElliott Hughes unsigned v = 0;
842*7485b225SElliott Hughes XMLError queryResult = element->QueryAttribute("attrib", &v);
843*7485b225SElliott Hughes XMLTest("Attribute: unsigned", (int)XML_SUCCESS, queryResult, true);
844*7485b225SElliott Hughes XMLTest("Attribute: unsigned", unsigned(100), v, true);
845*7485b225SElliott Hughes }
846*7485b225SElliott Hughes {
847*7485b225SElliott Hughes const char* v = "failed";
848*7485b225SElliott Hughes XMLError queryResult = element->QueryStringAttribute("not-attrib", &v);
849*7485b225SElliott Hughes XMLTest("Attribute: string default", false, queryResult == XML_SUCCESS);
850*7485b225SElliott Hughes queryResult = element->QueryStringAttribute("attrib", &v);
851*7485b225SElliott Hughes XMLTest("Attribute: string", XML_SUCCESS, queryResult, true);
852*7485b225SElliott Hughes XMLTest("Attribute: string", "100", v);
853*7485b225SElliott Hughes }
854*7485b225SElliott Hughes XMLTest("Attribute: unsigned", unsigned(100), element->UnsignedAttribute("attrib"), true);
855*7485b225SElliott Hughes }
856*7485b225SElliott Hughes {
857*7485b225SElliott Hughes element->SetAttribute("attrib", BIG);
858*7485b225SElliott Hughes {
859*7485b225SElliott Hughes int64_t v = 0;
860*7485b225SElliott Hughes XMLError queryResult = element->QueryInt64Attribute("attrib", &v);
861*7485b225SElliott Hughes XMLTest("Attribute: int64_t", XML_SUCCESS, queryResult, true);
862*7485b225SElliott Hughes XMLTest("Attribute: int64_t", BIG, v, true);
863*7485b225SElliott Hughes }
864*7485b225SElliott Hughes {
865*7485b225SElliott Hughes int64_t v = 0;
866*7485b225SElliott Hughes XMLError queryResult = element->QueryAttribute("attrib", &v);
867*7485b225SElliott Hughes XMLTest("Attribute: int64_t", (int)XML_SUCCESS, queryResult, true);
868*7485b225SElliott Hughes XMLTest("Attribute: int64_t", BIG, v, true);
869*7485b225SElliott Hughes }
870*7485b225SElliott Hughes XMLTest("Attribute: int64_t", BIG, element->Int64Attribute("attrib"), true);
871*7485b225SElliott Hughes }
872*7485b225SElliott Hughes {
873*7485b225SElliott Hughes element->SetAttribute("attrib", BIG_POS);
874*7485b225SElliott Hughes {
875*7485b225SElliott Hughes uint64_t v = 0;
876*7485b225SElliott Hughes XMLError queryResult = element->QueryUnsigned64Attribute("attrib", &v);
877*7485b225SElliott Hughes XMLTest("Attribute: uint64_t", XML_SUCCESS, queryResult, true);
878*7485b225SElliott Hughes XMLTest("Attribute: uint64_t", BIG_POS, v, true);
879*7485b225SElliott Hughes }
880*7485b225SElliott Hughes {
881*7485b225SElliott Hughes uint64_t v = 0;
882*7485b225SElliott Hughes XMLError queryResult = element->QueryAttribute("attrib", &v);
883*7485b225SElliott Hughes XMLTest("Attribute: uint64_t", (int)XML_SUCCESS, queryResult, true);
884*7485b225SElliott Hughes XMLTest("Attribute: uint64_t", BIG_POS, v, true);
885*7485b225SElliott Hughes }
886*7485b225SElliott Hughes XMLTest("Attribute: uint64_t", BIG_POS, element->Unsigned64Attribute("attrib"), true);
887*7485b225SElliott Hughes }
888*7485b225SElliott Hughes {
889*7485b225SElliott Hughes element->SetAttribute("attrib", true);
890*7485b225SElliott Hughes {
891*7485b225SElliott Hughes bool v = false;
892*7485b225SElliott Hughes XMLError queryResult = element->QueryBoolAttribute("attrib", &v);
893*7485b225SElliott Hughes XMLTest("Attribute: bool", XML_SUCCESS, queryResult, true);
894*7485b225SElliott Hughes XMLTest("Attribute: bool", true, v, true);
895*7485b225SElliott Hughes }
896*7485b225SElliott Hughes {
897*7485b225SElliott Hughes bool v = false;
898*7485b225SElliott Hughes XMLError queryResult = element->QueryAttribute("attrib", &v);
899*7485b225SElliott Hughes XMLTest("Attribute: bool", (int)XML_SUCCESS, queryResult, true);
900*7485b225SElliott Hughes XMLTest("Attribute: bool", true, v, true);
901*7485b225SElliott Hughes }
902*7485b225SElliott Hughes XMLTest("Attribute: bool", true, element->BoolAttribute("attrib"), true);
903*7485b225SElliott Hughes }
904*7485b225SElliott Hughes {
905*7485b225SElliott Hughes element->SetAttribute("attrib", true);
906*7485b225SElliott Hughes const char* result = element->Attribute("attrib");
907*7485b225SElliott Hughes XMLTest("Bool true is 'true'", "true", result);
908*7485b225SElliott Hughes
909*7485b225SElliott Hughes XMLUtil::SetBoolSerialization("1", "0");
910*7485b225SElliott Hughes element->SetAttribute("attrib", true);
911*7485b225SElliott Hughes result = element->Attribute("attrib");
912*7485b225SElliott Hughes XMLTest("Bool true is '1'", "1", result);
913*7485b225SElliott Hughes
914*7485b225SElliott Hughes XMLUtil::SetBoolSerialization(0, 0);
915*7485b225SElliott Hughes }
916*7485b225SElliott Hughes {
917*7485b225SElliott Hughes element->SetAttribute("attrib", 100.0);
918*7485b225SElliott Hughes {
919*7485b225SElliott Hughes double v = 0;
920*7485b225SElliott Hughes XMLError queryResult = element->QueryDoubleAttribute("attrib", &v);
921*7485b225SElliott Hughes XMLTest("Attribute: double", XML_SUCCESS, queryResult, true);
922*7485b225SElliott Hughes XMLTest("Attribute: double", 100.0, v, true);
923*7485b225SElliott Hughes }
924*7485b225SElliott Hughes {
925*7485b225SElliott Hughes double v = 0;
926*7485b225SElliott Hughes XMLError queryResult = element->QueryAttribute("attrib", &v);
927*7485b225SElliott Hughes XMLTest("Attribute: bool", (int)XML_SUCCESS, queryResult, true);
928*7485b225SElliott Hughes XMLTest("Attribute: double", 100.0, v, true);
929*7485b225SElliott Hughes }
930*7485b225SElliott Hughes XMLTest("Attribute: double", 100.0, element->DoubleAttribute("attrib"), true);
931*7485b225SElliott Hughes }
932*7485b225SElliott Hughes {
933*7485b225SElliott Hughes element->SetAttribute("attrib", 100.0f);
934*7485b225SElliott Hughes {
935*7485b225SElliott Hughes float v = 0;
936*7485b225SElliott Hughes XMLError queryResult = element->QueryFloatAttribute("attrib", &v);
937*7485b225SElliott Hughes XMLTest("Attribute: float", XML_SUCCESS, queryResult, true);
938*7485b225SElliott Hughes XMLTest("Attribute: float", 100.0f, v, true);
939*7485b225SElliott Hughes }
940*7485b225SElliott Hughes {
941*7485b225SElliott Hughes float v = 0;
942*7485b225SElliott Hughes XMLError queryResult = element->QueryAttribute("attrib", &v);
943*7485b225SElliott Hughes XMLTest("Attribute: float", (int)XML_SUCCESS, queryResult, true);
944*7485b225SElliott Hughes XMLTest("Attribute: float", 100.0f, v, true);
945*7485b225SElliott Hughes }
946*7485b225SElliott Hughes XMLTest("Attribute: float", 100.0f, element->FloatAttribute("attrib"), true);
947*7485b225SElliott Hughes }
948*7485b225SElliott Hughes {
949*7485b225SElliott Hughes element->SetText(BIG);
950*7485b225SElliott Hughes int64_t v = 0;
951*7485b225SElliott Hughes XMLError queryResult = element->QueryInt64Text(&v);
952*7485b225SElliott Hughes XMLTest("Element: int64_t", XML_SUCCESS, queryResult, true);
953*7485b225SElliott Hughes XMLTest("Element: int64_t", BIG, v, true);
954*7485b225SElliott Hughes }
955*7485b225SElliott Hughes {
956*7485b225SElliott Hughes element->SetText(BIG_POS);
957*7485b225SElliott Hughes uint64_t v = 0;
958*7485b225SElliott Hughes XMLError queryResult = element->QueryUnsigned64Text(&v);
959*7485b225SElliott Hughes XMLTest("Element: uint64_t", XML_SUCCESS, queryResult, true);
960*7485b225SElliott Hughes XMLTest("Element: uint64_t", BIG_POS, v, true);
961*7485b225SElliott Hughes }
962*7485b225SElliott Hughes }
963*7485b225SElliott Hughes
964*7485b225SElliott Hughes // ---------- XMLPrinter stream mode ------
965*7485b225SElliott Hughes {
966*7485b225SElliott Hughes {
967*7485b225SElliott Hughes FILE* printerfp = fopen("resources/out/printer.xml", "w");
968*7485b225SElliott Hughes XMLTest("Open printer.xml", true, printerfp != 0);
969*7485b225SElliott Hughes XMLPrinter printer(printerfp);
970*7485b225SElliott Hughes printer.OpenElement("foo");
971*7485b225SElliott Hughes printer.PushAttribute("attrib-text", "text");
972*7485b225SElliott Hughes printer.PushAttribute("attrib-int", int(1));
973*7485b225SElliott Hughes printer.PushAttribute("attrib-unsigned", unsigned(2));
974*7485b225SElliott Hughes printer.PushAttribute("attrib-int64", int64_t(3));
975*7485b225SElliott Hughes printer.PushAttribute("attrib-uint64", uint64_t(37));
976*7485b225SElliott Hughes printer.PushAttribute("attrib-bool", true);
977*7485b225SElliott Hughes printer.PushAttribute("attrib-double", 4.0);
978*7485b225SElliott Hughes printer.CloseElement();
979*7485b225SElliott Hughes fclose(printerfp);
980*7485b225SElliott Hughes }
981*7485b225SElliott Hughes {
982*7485b225SElliott Hughes XMLDocument doc;
983*7485b225SElliott Hughes doc.LoadFile("resources/out/printer.xml");
984*7485b225SElliott Hughes XMLTest("XMLPrinter Stream mode: load", XML_SUCCESS, doc.ErrorID(), true);
985*7485b225SElliott Hughes
986*7485b225SElliott Hughes const XMLDocument& cdoc = doc;
987*7485b225SElliott Hughes
988*7485b225SElliott Hughes const XMLAttribute* attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-text");
989*7485b225SElliott Hughes XMLTest("attrib-text", "text", attrib->Value(), true);
990*7485b225SElliott Hughes attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-int");
991*7485b225SElliott Hughes XMLTest("attrib-int", int(1), attrib->IntValue(), true);
992*7485b225SElliott Hughes attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-unsigned");
993*7485b225SElliott Hughes XMLTest("attrib-unsigned", unsigned(2), attrib->UnsignedValue(), true);
994*7485b225SElliott Hughes attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-int64");
995*7485b225SElliott Hughes XMLTest("attrib-int64", int64_t(3), attrib->Int64Value(), true);
996*7485b225SElliott Hughes attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-uint64");
997*7485b225SElliott Hughes XMLTest("attrib-uint64", uint64_t(37), attrib->Unsigned64Value(), true);
998*7485b225SElliott Hughes attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-bool");
999*7485b225SElliott Hughes XMLTest("attrib-bool", true, attrib->BoolValue(), true);
1000*7485b225SElliott Hughes attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-double");
1001*7485b225SElliott Hughes XMLTest("attrib-double", 4.0, attrib->DoubleValue(), true);
1002*7485b225SElliott Hughes }
1003*7485b225SElliott Hughes // Add API_testcatse :PushDeclaration();PushText();PushComment()
1004*7485b225SElliott Hughes {
1005*7485b225SElliott Hughes FILE* fp1 = fopen("resources/out/printer_1.xml", "w");
1006*7485b225SElliott Hughes XMLPrinter printer(fp1);
1007*7485b225SElliott Hughes
1008*7485b225SElliott Hughes printer.PushDeclaration("version = '1.0' enconding = 'utf-8'");
1009*7485b225SElliott Hughes
1010*7485b225SElliott Hughes printer.OpenElement("foo");
1011*7485b225SElliott Hughes printer.PushAttribute("attrib-text", "text");
1012*7485b225SElliott Hughes
1013*7485b225SElliott Hughes printer.OpenElement("text");
1014*7485b225SElliott Hughes printer.PushText("Tinyxml2");
1015*7485b225SElliott Hughes printer.CloseElement();
1016*7485b225SElliott Hughes
1017*7485b225SElliott Hughes printer.OpenElement("int");
1018*7485b225SElliott Hughes printer.PushText(int(11));
1019*7485b225SElliott Hughes printer.CloseElement();
1020*7485b225SElliott Hughes
1021*7485b225SElliott Hughes printer.OpenElement("unsigned");
1022*7485b225SElliott Hughes printer.PushText(unsigned(12));
1023*7485b225SElliott Hughes printer.CloseElement();
1024*7485b225SElliott Hughes
1025*7485b225SElliott Hughes printer.OpenElement("int64_t");
1026*7485b225SElliott Hughes printer.PushText(int64_t(13));
1027*7485b225SElliott Hughes printer.CloseElement();
1028*7485b225SElliott Hughes
1029*7485b225SElliott Hughes printer.OpenElement("uint64_t");
1030*7485b225SElliott Hughes printer.PushText(uint64_t(14));
1031*7485b225SElliott Hughes printer.CloseElement();
1032*7485b225SElliott Hughes
1033*7485b225SElliott Hughes printer.OpenElement("bool");
1034*7485b225SElliott Hughes printer.PushText(true);
1035*7485b225SElliott Hughes printer.CloseElement();
1036*7485b225SElliott Hughes
1037*7485b225SElliott Hughes printer.OpenElement("float");
1038*7485b225SElliott Hughes printer.PushText("1.56");
1039*7485b225SElliott Hughes printer.CloseElement();
1040*7485b225SElliott Hughes
1041*7485b225SElliott Hughes printer.OpenElement("double");
1042*7485b225SElliott Hughes printer.PushText("12.12");
1043*7485b225SElliott Hughes printer.CloseElement();
1044*7485b225SElliott Hughes
1045*7485b225SElliott Hughes printer.OpenElement("comment");
1046*7485b225SElliott Hughes printer.PushComment("this is Tinyxml2");
1047*7485b225SElliott Hughes printer.CloseElement();
1048*7485b225SElliott Hughes
1049*7485b225SElliott Hughes printer.CloseElement();
1050*7485b225SElliott Hughes fclose(fp1);
1051*7485b225SElliott Hughes }
1052*7485b225SElliott Hughes {
1053*7485b225SElliott Hughes XMLDocument doc;
1054*7485b225SElliott Hughes doc.LoadFile("resources/out/printer_1.xml");
1055*7485b225SElliott Hughes XMLTest("XMLPrinter Stream mode: load", XML_SUCCESS, doc.ErrorID(), true);
1056*7485b225SElliott Hughes
1057*7485b225SElliott Hughes const XMLDocument& cdoc = doc;
1058*7485b225SElliott Hughes
1059*7485b225SElliott Hughes const XMLElement* root = cdoc.FirstChildElement("foo");
1060*7485b225SElliott Hughes
1061*7485b225SElliott Hughes const char* text_value;
1062*7485b225SElliott Hughes text_value = root->FirstChildElement("text")->GetText();
1063*7485b225SElliott Hughes XMLTest("PushText( const char* text, bool cdata=false ) test", "Tinyxml2", text_value);
1064*7485b225SElliott Hughes
1065*7485b225SElliott Hughes int int_value;
1066*7485b225SElliott Hughes int_value = root->FirstChildElement("int")->IntText();
1067*7485b225SElliott Hughes XMLTest("PushText( int value ) test", 11, int_value);
1068*7485b225SElliott Hughes
1069*7485b225SElliott Hughes unsigned unsigned_value;
1070*7485b225SElliott Hughes unsigned_value = root->FirstChildElement("unsigned")->UnsignedText();
1071*7485b225SElliott Hughes XMLTest("PushText( unsigned value ) test", (unsigned)12, unsigned_value);
1072*7485b225SElliott Hughes
1073*7485b225SElliott Hughes int64_t int64_t_value;
1074*7485b225SElliott Hughes int64_t_value = root->FirstChildElement("int64_t")->Int64Text();
1075*7485b225SElliott Hughes XMLTest("PushText( int64_t value ) test", (int64_t) 13, int64_t_value);
1076*7485b225SElliott Hughes
1077*7485b225SElliott Hughes uint64_t uint64_t_value;
1078*7485b225SElliott Hughes uint64_t_value = root->FirstChildElement("uint64_t")->Unsigned64Text();
1079*7485b225SElliott Hughes XMLTest("PushText( uint64_t value ) test", (uint64_t) 14, uint64_t_value);
1080*7485b225SElliott Hughes
1081*7485b225SElliott Hughes float float_value;
1082*7485b225SElliott Hughes float_value = root->FirstChildElement("float")->FloatText();
1083*7485b225SElliott Hughes XMLTest("PushText( float value ) test", 1.56f, float_value);
1084*7485b225SElliott Hughes
1085*7485b225SElliott Hughes double double_value;
1086*7485b225SElliott Hughes double_value = root->FirstChildElement("double")->DoubleText();
1087*7485b225SElliott Hughes XMLTest("PushText( double value ) test", 12.12, double_value);
1088*7485b225SElliott Hughes
1089*7485b225SElliott Hughes bool bool_value;
1090*7485b225SElliott Hughes bool_value = root->FirstChildElement("bool")->BoolText();
1091*7485b225SElliott Hughes XMLTest("PushText( bool value ) test", true, bool_value);
1092*7485b225SElliott Hughes
1093*7485b225SElliott Hughes const XMLComment* comment = root->FirstChildElement("comment")->FirstChild()->ToComment();
1094*7485b225SElliott Hughes const char* comment_value = comment->Value();
1095*7485b225SElliott Hughes XMLTest("PushComment() test", "this is Tinyxml2", comment_value);
1096*7485b225SElliott Hughes
1097*7485b225SElliott Hughes const XMLDeclaration* declaration = cdoc.FirstChild()->ToDeclaration();
1098*7485b225SElliott Hughes const char* declaration_value = declaration->Value();
1099*7485b225SElliott Hughes XMLTest("PushDeclaration() test", "version = '1.0' enconding = 'utf-8'", declaration_value);
1100*7485b225SElliott Hughes }
1101*7485b225SElliott Hughes }
1102*7485b225SElliott Hughes
1103*7485b225SElliott Hughes
1104*7485b225SElliott Hughes // ---------- CDATA ---------------
1105*7485b225SElliott Hughes {
1106*7485b225SElliott Hughes const char* str = "<xmlElement>"
1107*7485b225SElliott Hughes "<![CDATA["
1108*7485b225SElliott Hughes "I am > the rules!\n"
1109*7485b225SElliott Hughes "...since I make symbolic puns"
1110*7485b225SElliott Hughes "]]>"
1111*7485b225SElliott Hughes "</xmlElement>";
1112*7485b225SElliott Hughes XMLDocument doc;
1113*7485b225SElliott Hughes doc.Parse( str );
1114*7485b225SElliott Hughes XMLTest( "CDATA symbolic puns round 1", false, doc.Error() );
1115*7485b225SElliott Hughes doc.Print();
1116*7485b225SElliott Hughes
1117*7485b225SElliott Hughes XMLTest( "CDATA parse.", "I am > the rules!\n...since I make symbolic puns",
1118*7485b225SElliott Hughes doc.FirstChildElement()->FirstChild()->Value(),
1119*7485b225SElliott Hughes false );
1120*7485b225SElliott Hughes }
1121*7485b225SElliott Hughes
1122*7485b225SElliott Hughes // ----------- CDATA -------------
1123*7485b225SElliott Hughes {
1124*7485b225SElliott Hughes const char* str = "<xmlElement>"
1125*7485b225SElliott Hughes "<![CDATA["
1126*7485b225SElliott Hughes "<b>I am > the rules!</b>\n"
1127*7485b225SElliott Hughes "...since I make symbolic puns"
1128*7485b225SElliott Hughes "]]>"
1129*7485b225SElliott Hughes "</xmlElement>";
1130*7485b225SElliott Hughes XMLDocument doc;
1131*7485b225SElliott Hughes doc.Parse( str );
1132*7485b225SElliott Hughes XMLTest( "CDATA symbolic puns round 2", false, doc.Error() );
1133*7485b225SElliott Hughes doc.Print();
1134*7485b225SElliott Hughes
1135*7485b225SElliott Hughes XMLTest( "CDATA parse. [ tixml1:1480107 ]",
1136*7485b225SElliott Hughes "<b>I am > the rules!</b>\n...since I make symbolic puns",
1137*7485b225SElliott Hughes doc.FirstChildElement()->FirstChild()->Value(),
1138*7485b225SElliott Hughes false );
1139*7485b225SElliott Hughes }
1140*7485b225SElliott Hughes
1141*7485b225SElliott Hughes // InsertAfterChild causes crash.
1142*7485b225SElliott Hughes {
1143*7485b225SElliott Hughes // InsertBeforeChild and InsertAfterChild causes crash.
1144*7485b225SElliott Hughes XMLDocument doc;
1145*7485b225SElliott Hughes XMLElement* parent = doc.NewElement( "Parent" );
1146*7485b225SElliott Hughes doc.InsertFirstChild( parent );
1147*7485b225SElliott Hughes
1148*7485b225SElliott Hughes XMLElement* childText0 = doc.NewElement( "childText0" );
1149*7485b225SElliott Hughes XMLElement* childText1 = doc.NewElement( "childText1" );
1150*7485b225SElliott Hughes
1151*7485b225SElliott Hughes XMLNode* childNode0 = parent->InsertEndChild( childText0 );
1152*7485b225SElliott Hughes XMLTest( "InsertEndChild() return", true, childNode0 == childText0 );
1153*7485b225SElliott Hughes XMLNode* childNode1 = parent->InsertAfterChild( childNode0, childText1 );
1154*7485b225SElliott Hughes XMLTest( "InsertAfterChild() return", true, childNode1 == childText1 );
1155*7485b225SElliott Hughes
1156*7485b225SElliott Hughes XMLTest( "Test InsertAfterChild on empty node. ", true, ( childNode1 == parent->LastChild() ) );
1157*7485b225SElliott Hughes }
1158*7485b225SElliott Hughes
1159*7485b225SElliott Hughes {
1160*7485b225SElliott Hughes // Entities not being written correctly.
1161*7485b225SElliott Hughes // From Lynn Allen
1162*7485b225SElliott Hughes
1163*7485b225SElliott Hughes const char* passages =
1164*7485b225SElliott Hughes "<?xml version=\"1.0\" standalone=\"no\" ?>"
1165*7485b225SElliott Hughes "<passages count=\"006\" formatversion=\"20020620\">"
1166*7485b225SElliott Hughes "<psg context=\"Line 5 has "quotation marks" and 'apostrophe marks'."
1167*7485b225SElliott Hughes " It also has <, >, and &, as well as a fake copyright ©.\"> </psg>"
1168*7485b225SElliott Hughes "</passages>";
1169*7485b225SElliott Hughes
1170*7485b225SElliott Hughes XMLDocument doc;
1171*7485b225SElliott Hughes doc.Parse( passages );
1172*7485b225SElliott Hughes XMLTest( "Entity transformation parse round 1", false, doc.Error() );
1173*7485b225SElliott Hughes XMLElement* psg = doc.RootElement()->FirstChildElement();
1174*7485b225SElliott Hughes const char* context = psg->Attribute( "context" );
1175*7485b225SElliott Hughes const char* expected = "Line 5 has \"quotation marks\" and 'apostrophe marks'. It also has <, >, and &, as well as a fake copyright \xC2\xA9.";
1176*7485b225SElliott Hughes
1177*7485b225SElliott Hughes XMLTest( "Entity transformation: read. ", expected, context, true );
1178*7485b225SElliott Hughes
1179*7485b225SElliott Hughes const char* textFilePath = "resources/out/textfile.txt";
1180*7485b225SElliott Hughes FILE* textfile = fopen( textFilePath, "w" );
1181*7485b225SElliott Hughes XMLTest( "Entity transformation: open text file for writing", true, textfile != 0, true );
1182*7485b225SElliott Hughes if ( textfile )
1183*7485b225SElliott Hughes {
1184*7485b225SElliott Hughes XMLPrinter streamer( textfile );
1185*7485b225SElliott Hughes bool acceptResult = psg->Accept( &streamer );
1186*7485b225SElliott Hughes fclose( textfile );
1187*7485b225SElliott Hughes XMLTest( "Entity transformation: Accept", true, acceptResult );
1188*7485b225SElliott Hughes }
1189*7485b225SElliott Hughes
1190*7485b225SElliott Hughes textfile = fopen( textFilePath, "r" );
1191*7485b225SElliott Hughes XMLTest( "Entity transformation: open text file for reading", true, textfile != 0, true );
1192*7485b225SElliott Hughes if ( textfile )
1193*7485b225SElliott Hughes {
1194*7485b225SElliott Hughes char buf[ 1024 ];
1195*7485b225SElliott Hughes fgets( buf, 1024, textfile );
1196*7485b225SElliott Hughes XMLTest( "Entity transformation: write. ",
1197*7485b225SElliott Hughes "<psg context=\"Line 5 has "quotation marks" and 'apostrophe marks'."
1198*7485b225SElliott Hughes " It also has <, >, and &, as well as a fake copyright \xC2\xA9.\"/>\n",
1199*7485b225SElliott Hughes buf, false );
1200*7485b225SElliott Hughes fclose( textfile );
1201*7485b225SElliott Hughes }
1202*7485b225SElliott Hughes }
1203*7485b225SElliott Hughes
1204*7485b225SElliott Hughes {
1205*7485b225SElliott Hughes // Suppress entities.
1206*7485b225SElliott Hughes const char* passages =
1207*7485b225SElliott Hughes "<?xml version=\"1.0\" standalone=\"no\" ?>"
1208*7485b225SElliott Hughes "<passages count=\"006\" formatversion=\"20020620\">"
1209*7485b225SElliott Hughes "<psg context=\"Line 5 has "quotation marks" and 'apostrophe marks'.\">Crazy &ttk;</psg>"
1210*7485b225SElliott Hughes "</passages>";
1211*7485b225SElliott Hughes
1212*7485b225SElliott Hughes XMLDocument doc( false );
1213*7485b225SElliott Hughes doc.Parse( passages );
1214*7485b225SElliott Hughes XMLTest( "Entity transformation parse round 2", false, doc.Error() );
1215*7485b225SElliott Hughes
1216*7485b225SElliott Hughes XMLTest( "No entity parsing.",
1217*7485b225SElliott Hughes "Line 5 has "quotation marks" and 'apostrophe marks'.",
1218*7485b225SElliott Hughes doc.FirstChildElement()->FirstChildElement()->Attribute( "context" ) );
1219*7485b225SElliott Hughes XMLTest( "No entity parsing.", "Crazy &ttk;",
1220*7485b225SElliott Hughes doc.FirstChildElement()->FirstChildElement()->FirstChild()->Value() );
1221*7485b225SElliott Hughes doc.Print();
1222*7485b225SElliott Hughes }
1223*7485b225SElliott Hughes
1224*7485b225SElliott Hughes {
1225*7485b225SElliott Hughes const char* test = "<?xml version='1.0'?><a.elem xmi.version='2.0'/>";
1226*7485b225SElliott Hughes
1227*7485b225SElliott Hughes XMLDocument doc;
1228*7485b225SElliott Hughes doc.Parse( test );
1229*7485b225SElliott Hughes XMLTest( "dot in names", false, doc.Error() );
1230*7485b225SElliott Hughes XMLTest( "dot in names", "a.elem", doc.FirstChildElement()->Name() );
1231*7485b225SElliott Hughes XMLTest( "dot in names", "2.0", doc.FirstChildElement()->Attribute( "xmi.version" ) );
1232*7485b225SElliott Hughes }
1233*7485b225SElliott Hughes
1234*7485b225SElliott Hughes {
1235*7485b225SElliott Hughes const char* test = "<element><Name>1.1 Start easy ignore fin thickness
</Name></element>";
1236*7485b225SElliott Hughes
1237*7485b225SElliott Hughes XMLDocument doc;
1238*7485b225SElliott Hughes doc.Parse( test );
1239*7485b225SElliott Hughes XMLTest( "fin thickness", false, doc.Error() );
1240*7485b225SElliott Hughes
1241*7485b225SElliott Hughes XMLText* text = doc.FirstChildElement()->FirstChildElement()->FirstChild()->ToText();
1242*7485b225SElliott Hughes XMLTest( "Entity with one digit.",
1243*7485b225SElliott Hughes "1.1 Start easy ignore fin thickness\n", text->Value(),
1244*7485b225SElliott Hughes false );
1245*7485b225SElliott Hughes }
1246*7485b225SElliott Hughes
1247*7485b225SElliott Hughes {
1248*7485b225SElliott Hughes // DOCTYPE not preserved (950171)
1249*7485b225SElliott Hughes //
1250*7485b225SElliott Hughes const char* doctype =
1251*7485b225SElliott Hughes "<?xml version=\"1.0\" ?>"
1252*7485b225SElliott Hughes "<!DOCTYPE PLAY SYSTEM 'play.dtd'>"
1253*7485b225SElliott Hughes "<!ELEMENT title (#PCDATA)>"
1254*7485b225SElliott Hughes "<!ELEMENT books (title,authors)>"
1255*7485b225SElliott Hughes "<element />";
1256*7485b225SElliott Hughes
1257*7485b225SElliott Hughes XMLDocument doc;
1258*7485b225SElliott Hughes doc.Parse( doctype );
1259*7485b225SElliott Hughes XMLTest( "PLAY SYSTEM parse", false, doc.Error() );
1260*7485b225SElliott Hughes doc.SaveFile( "resources/out/test7.xml" );
1261*7485b225SElliott Hughes XMLTest( "PLAY SYSTEM save", false, doc.Error() );
1262*7485b225SElliott Hughes doc.DeleteChild( doc.RootElement() );
1263*7485b225SElliott Hughes doc.LoadFile( "resources/out/test7.xml" );
1264*7485b225SElliott Hughes XMLTest( "PLAY SYSTEM load", false, doc.Error() );
1265*7485b225SElliott Hughes doc.Print();
1266*7485b225SElliott Hughes
1267*7485b225SElliott Hughes const XMLUnknown* decl = doc.FirstChild()->NextSibling()->ToUnknown();
1268*7485b225SElliott Hughes XMLTest( "Correct value of unknown.", "DOCTYPE PLAY SYSTEM 'play.dtd'", decl->Value() );
1269*7485b225SElliott Hughes
1270*7485b225SElliott Hughes }
1271*7485b225SElliott Hughes
1272*7485b225SElliott Hughes {
1273*7485b225SElliott Hughes // Comments do not stream out correctly.
1274*7485b225SElliott Hughes const char* doctype =
1275*7485b225SElliott Hughes "<!-- Somewhat<evil> -->";
1276*7485b225SElliott Hughes XMLDocument doc;
1277*7485b225SElliott Hughes doc.Parse( doctype );
1278*7485b225SElliott Hughes XMLTest( "Comment somewhat evil", false, doc.Error() );
1279*7485b225SElliott Hughes
1280*7485b225SElliott Hughes XMLComment* comment = doc.FirstChild()->ToComment();
1281*7485b225SElliott Hughes
1282*7485b225SElliott Hughes XMLTest( "Comment formatting.", " Somewhat<evil> ", comment->Value() );
1283*7485b225SElliott Hughes }
1284*7485b225SElliott Hughes {
1285*7485b225SElliott Hughes // Double attributes
1286*7485b225SElliott Hughes const char* doctype = "<element attr='red' attr='blue' />";
1287*7485b225SElliott Hughes
1288*7485b225SElliott Hughes XMLDocument doc;
1289*7485b225SElliott Hughes doc.Parse( doctype );
1290*7485b225SElliott Hughes
1291*7485b225SElliott Hughes XMLTest( "Parsing repeated attributes.", XML_ERROR_PARSING_ATTRIBUTE, doc.ErrorID() ); // is an error to tinyxml (didn't use to be, but caused issues)
1292*7485b225SElliott Hughes doc.PrintError();
1293*7485b225SElliott Hughes }
1294*7485b225SElliott Hughes
1295*7485b225SElliott Hughes {
1296*7485b225SElliott Hughes // Embedded null in stream.
1297*7485b225SElliott Hughes const char* doctype = "<element att\0r='red' attr='blue' />";
1298*7485b225SElliott Hughes
1299*7485b225SElliott Hughes XMLDocument doc;
1300*7485b225SElliott Hughes doc.Parse( doctype );
1301*7485b225SElliott Hughes XMLTest( "Embedded null throws error.", true, doc.Error() );
1302*7485b225SElliott Hughes }
1303*7485b225SElliott Hughes
1304*7485b225SElliott Hughes {
1305*7485b225SElliott Hughes // Empty documents should return TIXML_XML_ERROR_PARSING_EMPTY, bug 1070717
1306*7485b225SElliott Hughes const char* str = "";
1307*7485b225SElliott Hughes XMLDocument doc;
1308*7485b225SElliott Hughes doc.Parse( str );
1309*7485b225SElliott Hughes XMLTest( "Empty document error", XML_ERROR_EMPTY_DOCUMENT, doc.ErrorID() );
1310*7485b225SElliott Hughes
1311*7485b225SElliott Hughes // But be sure there is an error string!
1312*7485b225SElliott Hughes const char* errorStr = doc.ErrorStr();
1313*7485b225SElliott Hughes XMLTest("Error string should be set",
1314*7485b225SElliott Hughes "Error=XML_ERROR_EMPTY_DOCUMENT ErrorID=13 (0xd) Line number=0",
1315*7485b225SElliott Hughes errorStr);
1316*7485b225SElliott Hughes }
1317*7485b225SElliott Hughes
1318*7485b225SElliott Hughes {
1319*7485b225SElliott Hughes // Documents with all whitespaces should return TIXML_XML_ERROR_PARSING_EMPTY, bug 1070717
1320*7485b225SElliott Hughes const char* str = " ";
1321*7485b225SElliott Hughes XMLDocument doc;
1322*7485b225SElliott Hughes doc.Parse( str );
1323*7485b225SElliott Hughes XMLTest( "All whitespaces document error", XML_ERROR_EMPTY_DOCUMENT, doc.ErrorID() );
1324*7485b225SElliott Hughes }
1325*7485b225SElliott Hughes
1326*7485b225SElliott Hughes {
1327*7485b225SElliott Hughes // Low entities
1328*7485b225SElliott Hughes XMLDocument doc;
1329*7485b225SElliott Hughes doc.Parse( "<test></test>" );
1330*7485b225SElliott Hughes XMLTest( "Hex values", false, doc.Error() );
1331*7485b225SElliott Hughes const char result[] = { 0x0e, 0 };
1332*7485b225SElliott Hughes XMLTest( "Low entities.", result, doc.FirstChildElement()->GetText() );
1333*7485b225SElliott Hughes doc.Print();
1334*7485b225SElliott Hughes }
1335*7485b225SElliott Hughes
1336*7485b225SElliott Hughes {
1337*7485b225SElliott Hughes // Attribute values with trailing quotes not handled correctly
1338*7485b225SElliott Hughes XMLDocument doc;
1339*7485b225SElliott Hughes doc.Parse( "<foo attribute=bar\" />" );
1340*7485b225SElliott Hughes XMLTest( "Throw error with bad end quotes.", true, doc.Error() );
1341*7485b225SElliott Hughes }
1342*7485b225SElliott Hughes
1343*7485b225SElliott Hughes {
1344*7485b225SElliott Hughes // [ 1663758 ] Failure to report error on bad XML
1345*7485b225SElliott Hughes XMLDocument xml;
1346*7485b225SElliott Hughes xml.Parse("<x>");
1347*7485b225SElliott Hughes XMLTest("Missing end tag at end of input", true, xml.Error());
1348*7485b225SElliott Hughes xml.Parse("<x> ");
1349*7485b225SElliott Hughes XMLTest("Missing end tag with trailing whitespace", true, xml.Error());
1350*7485b225SElliott Hughes xml.Parse("<x></y>");
1351*7485b225SElliott Hughes XMLTest("Mismatched tags", XML_ERROR_MISMATCHED_ELEMENT, xml.ErrorID() );
1352*7485b225SElliott Hughes }
1353*7485b225SElliott Hughes
1354*7485b225SElliott Hughes
1355*7485b225SElliott Hughes {
1356*7485b225SElliott Hughes // [ 1475201 ] TinyXML parses entities in comments
1357*7485b225SElliott Hughes XMLDocument xml;
1358*7485b225SElliott Hughes xml.Parse("<!-- declarations for <head> & <body> -->"
1359*7485b225SElliott Hughes "<!-- far & away -->" );
1360*7485b225SElliott Hughes XMLTest( "Declarations for head and body", false, xml.Error() );
1361*7485b225SElliott Hughes
1362*7485b225SElliott Hughes XMLNode* e0 = xml.FirstChild();
1363*7485b225SElliott Hughes XMLNode* e1 = e0->NextSibling();
1364*7485b225SElliott Hughes XMLComment* c0 = e0->ToComment();
1365*7485b225SElliott Hughes XMLComment* c1 = e1->ToComment();
1366*7485b225SElliott Hughes
1367*7485b225SElliott Hughes XMLTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
1368*7485b225SElliott Hughes XMLTest( "Comments ignore entities.", " far & away ", c1->Value(), true );
1369*7485b225SElliott Hughes }
1370*7485b225SElliott Hughes
1371*7485b225SElliott Hughes {
1372*7485b225SElliott Hughes XMLDocument xml;
1373*7485b225SElliott Hughes xml.Parse( "<Parent>"
1374*7485b225SElliott Hughes "<child1 att=''/>"
1375*7485b225SElliott Hughes "<!-- With this comment, child2 will not be parsed! -->"
1376*7485b225SElliott Hughes "<child2 att=''/>"
1377*7485b225SElliott Hughes "</Parent>" );
1378*7485b225SElliott Hughes XMLTest( "Comments iteration", false, xml.Error() );
1379*7485b225SElliott Hughes xml.Print();
1380*7485b225SElliott Hughes
1381*7485b225SElliott Hughes int count = 0;
1382*7485b225SElliott Hughes
1383*7485b225SElliott Hughes for( XMLNode* ele = xml.FirstChildElement( "Parent" )->FirstChild();
1384*7485b225SElliott Hughes ele;
1385*7485b225SElliott Hughes ele = ele->NextSibling() )
1386*7485b225SElliott Hughes {
1387*7485b225SElliott Hughes ++count;
1388*7485b225SElliott Hughes }
1389*7485b225SElliott Hughes
1390*7485b225SElliott Hughes XMLTest( "Comments iterate correctly.", 3, count );
1391*7485b225SElliott Hughes }
1392*7485b225SElliott Hughes
1393*7485b225SElliott Hughes {
1394*7485b225SElliott Hughes // trying to repro [1874301]. If it doesn't go into an infinite loop, all is well.
1395*7485b225SElliott Hughes unsigned char buf[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?><feed><![CDATA[Test XMLblablablalblbl";
1396*7485b225SElliott Hughes buf[60] = 239;
1397*7485b225SElliott Hughes buf[61] = 0;
1398*7485b225SElliott Hughes
1399*7485b225SElliott Hughes XMLDocument doc;
1400*7485b225SElliott Hughes doc.Parse( (const char*)buf);
1401*7485b225SElliott Hughes XMLTest( "Broken CDATA", true, doc.Error() );
1402*7485b225SElliott Hughes }
1403*7485b225SElliott Hughes
1404*7485b225SElliott Hughes
1405*7485b225SElliott Hughes {
1406*7485b225SElliott Hughes // bug 1827248 Error while parsing a little bit malformed file
1407*7485b225SElliott Hughes // Actually not malformed - should work.
1408*7485b225SElliott Hughes XMLDocument xml;
1409*7485b225SElliott Hughes xml.Parse( "<attributelist> </attributelist >" );
1410*7485b225SElliott Hughes XMLTest( "Handle end tag whitespace", false, xml.Error() );
1411*7485b225SElliott Hughes }
1412*7485b225SElliott Hughes
1413*7485b225SElliott Hughes {
1414*7485b225SElliott Hughes // This one must not result in an infinite loop
1415*7485b225SElliott Hughes XMLDocument xml;
1416*7485b225SElliott Hughes xml.Parse( "<infinite>loop" );
1417*7485b225SElliott Hughes XMLTest( "No closing element", true, xml.Error() );
1418*7485b225SElliott Hughes XMLTest( "Infinite loop test.", true, true );
1419*7485b225SElliott Hughes }
1420*7485b225SElliott Hughes #endif
1421*7485b225SElliott Hughes {
1422*7485b225SElliott Hughes const char* pub = "<?xml version='1.0'?> <element><sub/></element> <!--comment--> <!DOCTYPE>";
1423*7485b225SElliott Hughes XMLDocument doc;
1424*7485b225SElliott Hughes doc.Parse( pub );
1425*7485b225SElliott Hughes XMLTest( "Trailing DOCTYPE", false, doc.Error() );
1426*7485b225SElliott Hughes
1427*7485b225SElliott Hughes XMLDocument clone;
1428*7485b225SElliott Hughes for( const XMLNode* node=doc.FirstChild(); node; node=node->NextSibling() ) {
1429*7485b225SElliott Hughes XMLNode* copy = node->ShallowClone( &clone );
1430*7485b225SElliott Hughes clone.InsertEndChild( copy );
1431*7485b225SElliott Hughes }
1432*7485b225SElliott Hughes
1433*7485b225SElliott Hughes clone.Print();
1434*7485b225SElliott Hughes
1435*7485b225SElliott Hughes int count=0;
1436*7485b225SElliott Hughes const XMLNode* a=clone.FirstChild();
1437*7485b225SElliott Hughes const XMLNode* b=doc.FirstChild();
1438*7485b225SElliott Hughes for( ; a && b; a=a->NextSibling(), b=b->NextSibling() ) {
1439*7485b225SElliott Hughes ++count;
1440*7485b225SElliott Hughes XMLTest( "Clone and Equal", true, a->ShallowEqual( b ));
1441*7485b225SElliott Hughes }
1442*7485b225SElliott Hughes XMLTest( "Clone and Equal", 4, count );
1443*7485b225SElliott Hughes }
1444*7485b225SElliott Hughes
1445*7485b225SElliott Hughes {
1446*7485b225SElliott Hughes // Deep Cloning of root element.
1447*7485b225SElliott Hughes XMLDocument doc2;
1448*7485b225SElliott Hughes XMLPrinter printer1;
1449*7485b225SElliott Hughes {
1450*7485b225SElliott Hughes // Make sure doc1 is deleted before we test doc2
1451*7485b225SElliott Hughes const char* xml =
1452*7485b225SElliott Hughes "<root>"
1453*7485b225SElliott Hughes " <child1 foo='bar'/>"
1454*7485b225SElliott Hughes " <!-- comment thing -->"
1455*7485b225SElliott Hughes " <child2 val='1'>Text</child2>"
1456*7485b225SElliott Hughes "</root>";
1457*7485b225SElliott Hughes XMLDocument doc;
1458*7485b225SElliott Hughes doc.Parse(xml);
1459*7485b225SElliott Hughes XMLTest( "Parse before deep cloning root element", false, doc.Error() );
1460*7485b225SElliott Hughes
1461*7485b225SElliott Hughes doc.Print(&printer1);
1462*7485b225SElliott Hughes XMLNode* root = doc.RootElement()->DeepClone(&doc2);
1463*7485b225SElliott Hughes doc2.InsertFirstChild(root);
1464*7485b225SElliott Hughes }
1465*7485b225SElliott Hughes XMLPrinter printer2;
1466*7485b225SElliott Hughes doc2.Print(&printer2);
1467*7485b225SElliott Hughes
1468*7485b225SElliott Hughes XMLTest("Deep clone of element.", printer1.CStr(), printer2.CStr(), true);
1469*7485b225SElliott Hughes }
1470*7485b225SElliott Hughes
1471*7485b225SElliott Hughes {
1472*7485b225SElliott Hughes // Deep Cloning of sub element.
1473*7485b225SElliott Hughes XMLDocument doc2;
1474*7485b225SElliott Hughes XMLPrinter printer1;
1475*7485b225SElliott Hughes {
1476*7485b225SElliott Hughes // Make sure doc1 is deleted before we test doc2
1477*7485b225SElliott Hughes const char* xml =
1478*7485b225SElliott Hughes "<?xml version ='1.0'?>"
1479*7485b225SElliott Hughes "<root>"
1480*7485b225SElliott Hughes " <child1 foo='bar'/>"
1481*7485b225SElliott Hughes " <!-- comment thing -->"
1482*7485b225SElliott Hughes " <child2 val='1'>Text</child2>"
1483*7485b225SElliott Hughes "</root>";
1484*7485b225SElliott Hughes XMLDocument doc;
1485*7485b225SElliott Hughes doc.Parse(xml);
1486*7485b225SElliott Hughes XMLTest( "Parse before deep cloning sub element", false, doc.Error() );
1487*7485b225SElliott Hughes
1488*7485b225SElliott Hughes const XMLElement* subElement = doc.FirstChildElement("root")->FirstChildElement("child2");
1489*7485b225SElliott Hughes bool acceptResult = subElement->Accept(&printer1);
1490*7485b225SElliott Hughes XMLTest( "Accept before deep cloning", true, acceptResult );
1491*7485b225SElliott Hughes
1492*7485b225SElliott Hughes XMLNode* clonedSubElement = subElement->DeepClone(&doc2);
1493*7485b225SElliott Hughes doc2.InsertFirstChild(clonedSubElement);
1494*7485b225SElliott Hughes }
1495*7485b225SElliott Hughes XMLPrinter printer2;
1496*7485b225SElliott Hughes doc2.Print(&printer2);
1497*7485b225SElliott Hughes
1498*7485b225SElliott Hughes XMLTest("Deep clone of sub-element.", printer1.CStr(), printer2.CStr(), true);
1499*7485b225SElliott Hughes }
1500*7485b225SElliott Hughes
1501*7485b225SElliott Hughes {
1502*7485b225SElliott Hughes // Deep cloning of document.
1503*7485b225SElliott Hughes XMLDocument doc2;
1504*7485b225SElliott Hughes XMLPrinter printer1;
1505*7485b225SElliott Hughes {
1506*7485b225SElliott Hughes // Make sure doc1 is deleted before we test doc2
1507*7485b225SElliott Hughes const char* xml =
1508*7485b225SElliott Hughes "<?xml version ='1.0'?>"
1509*7485b225SElliott Hughes "<!-- Top level comment. -->"
1510*7485b225SElliott Hughes "<root>"
1511*7485b225SElliott Hughes " <child1 foo='bar'/>"
1512*7485b225SElliott Hughes " <!-- comment thing -->"
1513*7485b225SElliott Hughes " <child2 val='1'>Text</child2>"
1514*7485b225SElliott Hughes "</root>";
1515*7485b225SElliott Hughes XMLDocument doc;
1516*7485b225SElliott Hughes doc.Parse(xml);
1517*7485b225SElliott Hughes XMLTest( "Parse before deep cloning document", false, doc.Error() );
1518*7485b225SElliott Hughes doc.Print(&printer1);
1519*7485b225SElliott Hughes
1520*7485b225SElliott Hughes doc.DeepCopy(&doc2);
1521*7485b225SElliott Hughes }
1522*7485b225SElliott Hughes XMLPrinter printer2;
1523*7485b225SElliott Hughes doc2.Print(&printer2);
1524*7485b225SElliott Hughes
1525*7485b225SElliott Hughes XMLTest("DeepCopy of document.", printer1.CStr(), printer2.CStr(), true);
1526*7485b225SElliott Hughes }
1527*7485b225SElliott Hughes
1528*7485b225SElliott Hughes
1529*7485b225SElliott Hughes {
1530*7485b225SElliott Hughes // This shouldn't crash.
1531*7485b225SElliott Hughes XMLDocument doc;
1532*7485b225SElliott Hughes if(XML_SUCCESS != doc.LoadFile( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ))
1533*7485b225SElliott Hughes {
1534*7485b225SElliott Hughes doc.PrintError();
1535*7485b225SElliott Hughes }
1536*7485b225SElliott Hughes XMLTest( "Error in snprinf handling.", true, doc.Error() );
1537*7485b225SElliott Hughes }
1538*7485b225SElliott Hughes
1539*7485b225SElliott Hughes {
1540*7485b225SElliott Hughes // Attribute ordering.
1541*7485b225SElliott Hughes static const char* xml = "<element attrib1=\"1\" attrib2=\"2\" attrib3=\"3\" />";
1542*7485b225SElliott Hughes XMLDocument doc;
1543*7485b225SElliott Hughes doc.Parse( xml );
1544*7485b225SElliott Hughes XMLTest( "Parse for attribute ordering", false, doc.Error() );
1545*7485b225SElliott Hughes XMLElement* ele = doc.FirstChildElement();
1546*7485b225SElliott Hughes
1547*7485b225SElliott Hughes const XMLAttribute* a = ele->FirstAttribute();
1548*7485b225SElliott Hughes XMLTest( "Attribute order", "1", a->Value() );
1549*7485b225SElliott Hughes a = a->Next();
1550*7485b225SElliott Hughes XMLTest( "Attribute order", "2", a->Value() );
1551*7485b225SElliott Hughes a = a->Next();
1552*7485b225SElliott Hughes XMLTest( "Attribute order", "3", a->Value() );
1553*7485b225SElliott Hughes XMLTest( "Attribute order", "attrib3", a->Name() );
1554*7485b225SElliott Hughes
1555*7485b225SElliott Hughes ele->DeleteAttribute( "attrib2" );
1556*7485b225SElliott Hughes a = ele->FirstAttribute();
1557*7485b225SElliott Hughes XMLTest( "Attribute order", "1", a->Value() );
1558*7485b225SElliott Hughes a = a->Next();
1559*7485b225SElliott Hughes XMLTest( "Attribute order", "3", a->Value() );
1560*7485b225SElliott Hughes
1561*7485b225SElliott Hughes ele->DeleteAttribute( "attrib1" );
1562*7485b225SElliott Hughes ele->DeleteAttribute( "attrib3" );
1563*7485b225SElliott Hughes XMLTest( "Attribute order (empty)", true, ele->FirstAttribute() == 0 );
1564*7485b225SElliott Hughes }
1565*7485b225SElliott Hughes
1566*7485b225SElliott Hughes {
1567*7485b225SElliott Hughes // Make sure an attribute with a space in it succeeds.
1568*7485b225SElliott Hughes static const char* xml0 = "<element attribute1= \"Test Attribute\"/>";
1569*7485b225SElliott Hughes static const char* xml1 = "<element attribute1 =\"Test Attribute\"/>";
1570*7485b225SElliott Hughes static const char* xml2 = "<element attribute1 = \"Test Attribute\"/>";
1571*7485b225SElliott Hughes XMLDocument doc0;
1572*7485b225SElliott Hughes doc0.Parse( xml0 );
1573*7485b225SElliott Hughes XMLTest( "Parse attribute with space 1", false, doc0.Error() );
1574*7485b225SElliott Hughes XMLDocument doc1;
1575*7485b225SElliott Hughes doc1.Parse( xml1 );
1576*7485b225SElliott Hughes XMLTest( "Parse attribute with space 2", false, doc1.Error() );
1577*7485b225SElliott Hughes XMLDocument doc2;
1578*7485b225SElliott Hughes doc2.Parse( xml2 );
1579*7485b225SElliott Hughes XMLTest( "Parse attribute with space 3", false, doc2.Error() );
1580*7485b225SElliott Hughes
1581*7485b225SElliott Hughes XMLElement* ele = 0;
1582*7485b225SElliott Hughes ele = doc0.FirstChildElement();
1583*7485b225SElliott Hughes XMLTest( "Attribute with space #1", "Test Attribute", ele->Attribute( "attribute1" ) );
1584*7485b225SElliott Hughes ele = doc1.FirstChildElement();
1585*7485b225SElliott Hughes XMLTest( "Attribute with space #2", "Test Attribute", ele->Attribute( "attribute1" ) );
1586*7485b225SElliott Hughes ele = doc2.FirstChildElement();
1587*7485b225SElliott Hughes XMLTest( "Attribute with space #3", "Test Attribute", ele->Attribute( "attribute1" ) );
1588*7485b225SElliott Hughes }
1589*7485b225SElliott Hughes
1590*7485b225SElliott Hughes {
1591*7485b225SElliott Hughes // Make sure we don't go into an infinite loop.
1592*7485b225SElliott Hughes static const char* xml = "<doc><element attribute='attribute'/><element attribute='attribute'/></doc>";
1593*7485b225SElliott Hughes XMLDocument doc;
1594*7485b225SElliott Hughes doc.Parse( xml );
1595*7485b225SElliott Hughes XMLTest( "Parse two elements with attribute", false, doc.Error() );
1596*7485b225SElliott Hughes XMLElement* ele0 = doc.FirstChildElement()->FirstChildElement();
1597*7485b225SElliott Hughes XMLElement* ele1 = ele0->NextSiblingElement();
1598*7485b225SElliott Hughes bool equal = ele0->ShallowEqual( ele1 );
1599*7485b225SElliott Hughes
1600*7485b225SElliott Hughes XMLTest( "Infinite loop in shallow equal.", true, equal );
1601*7485b225SElliott Hughes }
1602*7485b225SElliott Hughes
1603*7485b225SElliott Hughes // -------- Handles ------------
1604*7485b225SElliott Hughes {
1605*7485b225SElliott Hughes static const char* xml = "<element attrib='bar'><sub>Text</sub></element>";
1606*7485b225SElliott Hughes XMLDocument doc;
1607*7485b225SElliott Hughes doc.Parse( xml );
1608*7485b225SElliott Hughes XMLTest( "Handle, parse element with attribute and nested element", false, doc.Error() );
1609*7485b225SElliott Hughes
1610*7485b225SElliott Hughes {
1611*7485b225SElliott Hughes XMLElement* ele = XMLHandle( doc ).FirstChildElement( "element" ).FirstChild().ToElement();
1612*7485b225SElliott Hughes XMLTest( "Handle, non-const, element is found", true, ele != 0 );
1613*7485b225SElliott Hughes XMLTest( "Handle, non-const, element name matches", "sub", ele->Value() );
1614*7485b225SElliott Hughes }
1615*7485b225SElliott Hughes
1616*7485b225SElliott Hughes {
1617*7485b225SElliott Hughes XMLHandle docH( doc );
1618*7485b225SElliott Hughes XMLElement* ele = docH.FirstChildElement( "noSuchElement" ).FirstChildElement( "element" ).ToElement();
1619*7485b225SElliott Hughes XMLTest( "Handle, non-const, element not found", true, ele == 0 );
1620*7485b225SElliott Hughes }
1621*7485b225SElliott Hughes
1622*7485b225SElliott Hughes {
1623*7485b225SElliott Hughes const XMLElement* ele = XMLConstHandle( doc ).FirstChildElement( "element" ).FirstChild().ToElement();
1624*7485b225SElliott Hughes XMLTest( "Handle, const, element is found", true, ele != 0 );
1625*7485b225SElliott Hughes XMLTest( "Handle, const, element name matches", "sub", ele->Value() );
1626*7485b225SElliott Hughes }
1627*7485b225SElliott Hughes
1628*7485b225SElliott Hughes {
1629*7485b225SElliott Hughes XMLConstHandle docH( doc );
1630*7485b225SElliott Hughes const XMLElement* ele = docH.FirstChildElement( "noSuchElement" ).FirstChildElement( "element" ).ToElement();
1631*7485b225SElliott Hughes XMLTest( "Handle, const, element not found", true, ele == 0 );
1632*7485b225SElliott Hughes }
1633*7485b225SElliott Hughes }
1634*7485b225SElliott Hughes {
1635*7485b225SElliott Hughes // Default Declaration & BOM
1636*7485b225SElliott Hughes XMLDocument doc;
1637*7485b225SElliott Hughes doc.InsertEndChild( doc.NewDeclaration() );
1638*7485b225SElliott Hughes doc.SetBOM( true );
1639*7485b225SElliott Hughes
1640*7485b225SElliott Hughes XMLPrinter printer;
1641*7485b225SElliott Hughes doc.Print( &printer );
1642*7485b225SElliott Hughes
1643*7485b225SElliott Hughes static const char* result = "\xef\xbb\xbf<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
1644*7485b225SElliott Hughes XMLTest( "BOM and default declaration", result, printer.CStr(), false );
1645*7485b225SElliott Hughes XMLTest( "CStrSize", 42, printer.CStrSize(), false );
1646*7485b225SElliott Hughes }
1647*7485b225SElliott Hughes {
1648*7485b225SElliott Hughes const char* xml = "<ipxml ws='1'><info bla=' /></ipxml>";
1649*7485b225SElliott Hughes XMLDocument doc;
1650*7485b225SElliott Hughes doc.Parse( xml );
1651*7485b225SElliott Hughes XMLTest( "Ill formed XML", true, doc.Error() );
1652*7485b225SElliott Hughes }
1653*7485b225SElliott Hughes
1654*7485b225SElliott Hughes {
1655*7485b225SElliott Hughes //API:IntText(),UnsignedText(),Int64Text(),DoubleText(),BoolText() and FloatText() test
1656*7485b225SElliott Hughes const char* xml = "<point> <IntText>-24</IntText> <UnsignedText>42</UnsignedText> \
1657*7485b225SElliott Hughes <Int64Text>38</Int64Text> <BoolText>true</BoolText> <DoubleText>2.35</DoubleText> </point>";
1658*7485b225SElliott Hughes XMLDocument doc;
1659*7485b225SElliott Hughes doc.Parse(xml);
1660*7485b225SElliott Hughes
1661*7485b225SElliott Hughes const XMLElement* pointElement = doc.RootElement();
1662*7485b225SElliott Hughes int test1 = pointElement->FirstChildElement("IntText")->IntText();
1663*7485b225SElliott Hughes XMLTest("IntText() test", -24, test1);
1664*7485b225SElliott Hughes
1665*7485b225SElliott Hughes unsigned test2 = pointElement->FirstChildElement("UnsignedText")->UnsignedText();
1666*7485b225SElliott Hughes XMLTest("UnsignedText() test", static_cast<unsigned>(42), test2);
1667*7485b225SElliott Hughes
1668*7485b225SElliott Hughes int64_t test3 = pointElement->FirstChildElement("Int64Text")->Int64Text();
1669*7485b225SElliott Hughes XMLTest("Int64Text() test", static_cast<int64_t>(38), test3);
1670*7485b225SElliott Hughes
1671*7485b225SElliott Hughes double test4 = pointElement->FirstChildElement("DoubleText")->DoubleText();
1672*7485b225SElliott Hughes XMLTest("DoubleText() test", 2.35, test4);
1673*7485b225SElliott Hughes
1674*7485b225SElliott Hughes float test5 = pointElement->FirstChildElement("DoubleText")->FloatText();
1675*7485b225SElliott Hughes XMLTest("FloatText()) test", 2.35f, test5);
1676*7485b225SElliott Hughes
1677*7485b225SElliott Hughes bool test6 = pointElement->FirstChildElement("BoolText")->BoolText();
1678*7485b225SElliott Hughes XMLTest("FloatText()) test", true, test6);
1679*7485b225SElliott Hughes }
1680*7485b225SElliott Hughes
1681*7485b225SElliott Hughes {
1682*7485b225SElliott Hughes // hex value test
1683*7485b225SElliott Hughes const char* xml = "<point> <IntText> 0x2020</IntText> <UnsignedText>0X2020</UnsignedText> \
1684*7485b225SElliott Hughes <Int64Text> 0x1234</Int64Text></point>";
1685*7485b225SElliott Hughes XMLDocument doc;
1686*7485b225SElliott Hughes doc.Parse(xml);
1687*7485b225SElliott Hughes
1688*7485b225SElliott Hughes const XMLElement* pointElement = doc.RootElement();
1689*7485b225SElliott Hughes int test1 = pointElement->FirstChildElement("IntText")->IntText();
1690*7485b225SElliott Hughes XMLTest("IntText() hex value test", 0x2020, test1);
1691*7485b225SElliott Hughes
1692*7485b225SElliott Hughes unsigned test2 = pointElement->FirstChildElement("UnsignedText")->UnsignedText();
1693*7485b225SElliott Hughes XMLTest("UnsignedText() hex value test", static_cast<unsigned>(0x2020), test2);
1694*7485b225SElliott Hughes
1695*7485b225SElliott Hughes int64_t test3 = pointElement->FirstChildElement("Int64Text")->Int64Text();
1696*7485b225SElliott Hughes XMLTest("Int64Text() hex value test", static_cast<int64_t>(0x1234), test3);
1697*7485b225SElliott Hughes }
1698*7485b225SElliott Hughes
1699*7485b225SElliott Hughes {
1700*7485b225SElliott Hughes //API:ShallowEqual() test
1701*7485b225SElliott Hughes const char* xml = "<playlist id = 'playlist'>"
1702*7485b225SElliott Hughes "<property name = 'track_name'>voice</property>"
1703*7485b225SElliott Hughes "</playlist>";
1704*7485b225SElliott Hughes XMLDocument doc;
1705*7485b225SElliott Hughes doc.Parse( xml );
1706*7485b225SElliott Hughes const XMLNode* PlaylistNode = doc.RootElement();
1707*7485b225SElliott Hughes const XMLNode* PropertyNode = PlaylistNode->FirstChildElement();
1708*7485b225SElliott Hughes bool result;
1709*7485b225SElliott Hughes result = PlaylistNode->ShallowEqual(PropertyNode);
1710*7485b225SElliott Hughes XMLTest("ShallowEqual() test",false,result);
1711*7485b225SElliott Hughes result = PlaylistNode->ShallowEqual(PlaylistNode);
1712*7485b225SElliott Hughes XMLTest("ShallowEqual() test",true,result);
1713*7485b225SElliott Hughes }
1714*7485b225SElliott Hughes
1715*7485b225SElliott Hughes {
1716*7485b225SElliott Hughes //API: previousSiblingElement() and NextSiblingElement() test
1717*7485b225SElliott Hughes const char* xml = "<playlist id = 'playlist'>"
1718*7485b225SElliott Hughes "<property name = 'track_name'>voice</property>"
1719*7485b225SElliott Hughes "<entry out = '946' producer = '2_playlist1' in = '0'/>"
1720*7485b225SElliott Hughes "<blank length = '1'/>"
1721*7485b225SElliott Hughes "</playlist>";
1722*7485b225SElliott Hughes XMLDocument doc;
1723*7485b225SElliott Hughes doc.Parse( xml );
1724*7485b225SElliott Hughes XMLElement* ElementPlaylist = doc.FirstChildElement("playlist");
1725*7485b225SElliott Hughes XMLTest("previousSiblingElement() test",true,ElementPlaylist != 0);
1726*7485b225SElliott Hughes const XMLElement* pre = ElementPlaylist->PreviousSiblingElement();
1727*7485b225SElliott Hughes XMLTest("previousSiblingElement() test",true,pre == 0);
1728*7485b225SElliott Hughes const XMLElement* ElementBlank = ElementPlaylist->FirstChildElement("entry")->NextSiblingElement("blank");
1729*7485b225SElliott Hughes XMLTest("NextSiblingElement() test",true,ElementBlank != 0);
1730*7485b225SElliott Hughes const XMLElement* next = ElementBlank->NextSiblingElement();
1731*7485b225SElliott Hughes XMLTest("NextSiblingElement() test",true,next == 0);
1732*7485b225SElliott Hughes const XMLElement* ElementEntry = ElementBlank->PreviousSiblingElement("entry");
1733*7485b225SElliott Hughes XMLTest("PreviousSiblingElement test",true,ElementEntry != 0);
1734*7485b225SElliott Hughes }
1735*7485b225SElliott Hughes
1736*7485b225SElliott Hughes // QueryXYZText
1737*7485b225SElliott Hughes {
1738*7485b225SElliott Hughes const char* xml = "<point> <x>1.2</x> <y>1</y> <z>38</z> <valid>true</valid> </point>";
1739*7485b225SElliott Hughes XMLDocument doc;
1740*7485b225SElliott Hughes doc.Parse( xml );
1741*7485b225SElliott Hughes XMLTest( "Parse points", false, doc.Error() );
1742*7485b225SElliott Hughes
1743*7485b225SElliott Hughes const XMLElement* pointElement = doc.RootElement();
1744*7485b225SElliott Hughes
1745*7485b225SElliott Hughes {
1746*7485b225SElliott Hughes int intValue = 0;
1747*7485b225SElliott Hughes XMLError queryResult = pointElement->FirstChildElement( "y" )->QueryIntText( &intValue );
1748*7485b225SElliott Hughes XMLTest( "QueryIntText result", XML_SUCCESS, queryResult, false );
1749*7485b225SElliott Hughes XMLTest( "QueryIntText", 1, intValue, false );
1750*7485b225SElliott Hughes }
1751*7485b225SElliott Hughes
1752*7485b225SElliott Hughes {
1753*7485b225SElliott Hughes unsigned unsignedValue = 0;
1754*7485b225SElliott Hughes XMLError queryResult = pointElement->FirstChildElement( "y" )->QueryUnsignedText( &unsignedValue );
1755*7485b225SElliott Hughes XMLTest( "QueryUnsignedText result", XML_SUCCESS, queryResult, false );
1756*7485b225SElliott Hughes XMLTest( "QueryUnsignedText", (unsigned)1, unsignedValue, false );
1757*7485b225SElliott Hughes }
1758*7485b225SElliott Hughes
1759*7485b225SElliott Hughes {
1760*7485b225SElliott Hughes float floatValue = 0;
1761*7485b225SElliott Hughes XMLError queryResult = pointElement->FirstChildElement( "x" )->QueryFloatText( &floatValue );
1762*7485b225SElliott Hughes XMLTest( "QueryFloatText result", XML_SUCCESS, queryResult, false );
1763*7485b225SElliott Hughes XMLTest( "QueryFloatText", 1.2f, floatValue, false );
1764*7485b225SElliott Hughes }
1765*7485b225SElliott Hughes
1766*7485b225SElliott Hughes {
1767*7485b225SElliott Hughes double doubleValue = 0;
1768*7485b225SElliott Hughes XMLError queryResult = pointElement->FirstChildElement( "x" )->QueryDoubleText( &doubleValue );
1769*7485b225SElliott Hughes XMLTest( "QueryDoubleText result", XML_SUCCESS, queryResult, false );
1770*7485b225SElliott Hughes XMLTest( "QueryDoubleText", 1.2, doubleValue, false );
1771*7485b225SElliott Hughes }
1772*7485b225SElliott Hughes
1773*7485b225SElliott Hughes {
1774*7485b225SElliott Hughes bool boolValue = false;
1775*7485b225SElliott Hughes XMLError queryResult = pointElement->FirstChildElement( "valid" )->QueryBoolText( &boolValue );
1776*7485b225SElliott Hughes XMLTest( "QueryBoolText result", XML_SUCCESS, queryResult, false );
1777*7485b225SElliott Hughes XMLTest( "QueryBoolText", true, boolValue, false );
1778*7485b225SElliott Hughes }
1779*7485b225SElliott Hughes }
1780*7485b225SElliott Hughes
1781*7485b225SElliott Hughes {
1782*7485b225SElliott Hughes const char* xml = "<element><_sub/><:sub/><sub:sub/><sub-sub/></element>";
1783*7485b225SElliott Hughes XMLDocument doc;
1784*7485b225SElliott Hughes doc.Parse( xml );
1785*7485b225SElliott Hughes XMLTest( "Non-alpha element lead letter parses.", false, doc.Error() );
1786*7485b225SElliott Hughes }
1787*7485b225SElliott Hughes
1788*7485b225SElliott Hughes {
1789*7485b225SElliott Hughes const char* xml = "<element _attr1=\"foo\" :attr2=\"bar\"></element>";
1790*7485b225SElliott Hughes XMLDocument doc;
1791*7485b225SElliott Hughes doc.Parse( xml );
1792*7485b225SElliott Hughes XMLTest("Non-alpha attribute lead character parses.", false, doc.Error());
1793*7485b225SElliott Hughes }
1794*7485b225SElliott Hughes
1795*7485b225SElliott Hughes {
1796*7485b225SElliott Hughes const char* xml = "<3lement></3lement>";
1797*7485b225SElliott Hughes XMLDocument doc;
1798*7485b225SElliott Hughes doc.Parse( xml );
1799*7485b225SElliott Hughes XMLTest("Element names with lead digit fail to parse.", true, doc.Error());
1800*7485b225SElliott Hughes }
1801*7485b225SElliott Hughes
1802*7485b225SElliott Hughes {
1803*7485b225SElliott Hughes const char* xml = "<element/>WOA THIS ISN'T GOING TO PARSE";
1804*7485b225SElliott Hughes XMLDocument doc;
1805*7485b225SElliott Hughes doc.Parse( xml, 10 );
1806*7485b225SElliott Hughes XMLTest( "Set length of incoming data", false, doc.Error() );
1807*7485b225SElliott Hughes }
1808*7485b225SElliott Hughes
1809*7485b225SElliott Hughes {
1810*7485b225SElliott Hughes XMLDocument doc;
1811*7485b225SElliott Hughes XMLTest( "Document is initially empty", true, doc.NoChildren() );
1812*7485b225SElliott Hughes doc.Clear();
1813*7485b225SElliott Hughes XMLTest( "Empty is empty after Clear()", true, doc.NoChildren() );
1814*7485b225SElliott Hughes doc.LoadFile( "resources/dream.xml" );
1815*7485b225SElliott Hughes XMLTest( "Load dream.xml", false, doc.Error() );
1816*7485b225SElliott Hughes XMLTest( "Document has something to Clear()", false, doc.NoChildren() );
1817*7485b225SElliott Hughes doc.Clear();
1818*7485b225SElliott Hughes XMLTest( "Document Clear()'s", true, doc.NoChildren() );
1819*7485b225SElliott Hughes }
1820*7485b225SElliott Hughes
1821*7485b225SElliott Hughes {
1822*7485b225SElliott Hughes XMLDocument doc;
1823*7485b225SElliott Hughes XMLTest( "No error initially", false, doc.Error() );
1824*7485b225SElliott Hughes XMLError error = doc.Parse( "This is not XML" );
1825*7485b225SElliott Hughes XMLTest( "Error after invalid XML", true, doc.Error() );
1826*7485b225SElliott Hughes XMLTest( "Error after invalid XML", error, doc.ErrorID() );
1827*7485b225SElliott Hughes doc.Clear();
1828*7485b225SElliott Hughes XMLTest( "No error after Clear()", false, doc.Error() );
1829*7485b225SElliott Hughes }
1830*7485b225SElliott Hughes
1831*7485b225SElliott Hughes // ----------- Whitespace ------------
1832*7485b225SElliott Hughes {
1833*7485b225SElliott Hughes const char* xml = "<element>"
1834*7485b225SElliott Hughes "<a> This \nis ' text ' </a>"
1835*7485b225SElliott Hughes "<b> This is ' text ' \n</b>"
1836*7485b225SElliott Hughes "<c>This is ' \n\n text '</c>"
1837*7485b225SElliott Hughes "</element>";
1838*7485b225SElliott Hughes XMLDocument doc( true, COLLAPSE_WHITESPACE );
1839*7485b225SElliott Hughes doc.Parse( xml );
1840*7485b225SElliott Hughes XMLTest( "Parse with whitespace collapsing and &apos", false, doc.Error() );
1841*7485b225SElliott Hughes
1842*7485b225SElliott Hughes const XMLElement* element = doc.FirstChildElement();
1843*7485b225SElliott Hughes for( const XMLElement* parent = element->FirstChildElement();
1844*7485b225SElliott Hughes parent;
1845*7485b225SElliott Hughes parent = parent->NextSiblingElement() )
1846*7485b225SElliott Hughes {
1847*7485b225SElliott Hughes XMLTest( "Whitespace collapse", "This is ' text '", parent->GetText() );
1848*7485b225SElliott Hughes }
1849*7485b225SElliott Hughes }
1850*7485b225SElliott Hughes
1851*7485b225SElliott Hughes #if 0
1852*7485b225SElliott Hughes {
1853*7485b225SElliott Hughes // Passes if assert doesn't fire.
1854*7485b225SElliott Hughes XMLDocument xmlDoc;
1855*7485b225SElliott Hughes
1856*7485b225SElliott Hughes xmlDoc.NewDeclaration();
1857*7485b225SElliott Hughes xmlDoc.NewComment("Configuration file");
1858*7485b225SElliott Hughes
1859*7485b225SElliott Hughes XMLElement *root = xmlDoc.NewElement("settings");
1860*7485b225SElliott Hughes root->SetAttribute("version", 2);
1861*7485b225SElliott Hughes }
1862*7485b225SElliott Hughes #endif
1863*7485b225SElliott Hughes
1864*7485b225SElliott Hughes {
1865*7485b225SElliott Hughes const char* xml = "<element> </element>";
1866*7485b225SElliott Hughes XMLDocument doc( true, COLLAPSE_WHITESPACE );
1867*7485b225SElliott Hughes doc.Parse( xml );
1868*7485b225SElliott Hughes XMLTest( "Parse with all whitespaces", false, doc.Error() );
1869*7485b225SElliott Hughes XMLTest( "Whitespace all space", true, 0 == doc.FirstChildElement()->FirstChild() );
1870*7485b225SElliott Hughes }
1871*7485b225SElliott Hughes
1872*7485b225SElliott Hughes // ----------- Preserve Whitespace ------------
1873*7485b225SElliott Hughes {
1874*7485b225SElliott Hughes const char* xml = "<element>This is ' \n\n text '</element>";
1875*7485b225SElliott Hughes XMLDocument doc(true, PRESERVE_WHITESPACE);
1876*7485b225SElliott Hughes doc.Parse(xml);
1877*7485b225SElliott Hughes XMLTest("Parse with whitespace preserved", false, doc.Error());
1878*7485b225SElliott Hughes XMLTest("Whitespace preserved", "This is ' \n\n text '", doc.FirstChildElement()->GetText());
1879*7485b225SElliott Hughes }
1880*7485b225SElliott Hughes
1881*7485b225SElliott Hughes {
1882*7485b225SElliott Hughes const char* xml = "<element> This \nis ' text ' </element>";
1883*7485b225SElliott Hughes XMLDocument doc(true, PRESERVE_WHITESPACE);
1884*7485b225SElliott Hughes doc.Parse(xml);
1885*7485b225SElliott Hughes XMLTest("Parse with whitespace preserved", false, doc.Error());
1886*7485b225SElliott Hughes XMLTest("Whitespace preserved", " This \nis ' text ' ", doc.FirstChildElement()->GetText());
1887*7485b225SElliott Hughes }
1888*7485b225SElliott Hughes
1889*7485b225SElliott Hughes {
1890*7485b225SElliott Hughes const char* xml = "<element> \n This is ' text ' \n</element>";
1891*7485b225SElliott Hughes XMLDocument doc(true, PRESERVE_WHITESPACE);
1892*7485b225SElliott Hughes doc.Parse(xml);
1893*7485b225SElliott Hughes XMLTest("Parse with whitespace preserved", false, doc.Error());
1894*7485b225SElliott Hughes XMLTest("Whitespace preserved", " \n This is ' text ' \n", doc.FirstChildElement()->GetText());
1895*7485b225SElliott Hughes }
1896*7485b225SElliott Hughes
1897*7485b225SElliott Hughes // Following cases are for text that is all whitespace which are not preserved intentionally
1898*7485b225SElliott Hughes {
1899*7485b225SElliott Hughes const char* xml = "<element> </element>";
1900*7485b225SElliott Hughes XMLDocument doc(true, PRESERVE_WHITESPACE);
1901*7485b225SElliott Hughes doc.Parse(xml);
1902*7485b225SElliott Hughes XMLTest("Parse with whitespace preserved", false, doc.Error());
1903*7485b225SElliott Hughes XMLTest("Whitespace preserved", true, 0 == doc.FirstChildElement()->GetText());
1904*7485b225SElliott Hughes }
1905*7485b225SElliott Hughes
1906*7485b225SElliott Hughes {
1907*7485b225SElliott Hughes const char* xml = "<element> </element>";
1908*7485b225SElliott Hughes XMLDocument doc(true, PRESERVE_WHITESPACE);
1909*7485b225SElliott Hughes doc.Parse(xml);
1910*7485b225SElliott Hughes XMLTest("Parse with whitespace preserved", false, doc.Error());
1911*7485b225SElliott Hughes XMLTest("Whitespace preserved", true, 0 == doc.FirstChildElement()->GetText());
1912*7485b225SElliott Hughes }
1913*7485b225SElliott Hughes
1914*7485b225SElliott Hughes {
1915*7485b225SElliott Hughes const char* xml = "<element>\n\n</element>";
1916*7485b225SElliott Hughes XMLDocument doc(true, PRESERVE_WHITESPACE);
1917*7485b225SElliott Hughes doc.Parse(xml);
1918*7485b225SElliott Hughes XMLTest("Parse with whitespace preserved", false, doc.Error());
1919*7485b225SElliott Hughes XMLTest("Whitespace preserved", true, 0 == doc.FirstChildElement()->GetText());
1920*7485b225SElliott Hughes }
1921*7485b225SElliott Hughes
1922*7485b225SElliott Hughes {
1923*7485b225SElliott Hughes const char* xml = "<element> \n</element>";
1924*7485b225SElliott Hughes XMLDocument doc(true, PRESERVE_WHITESPACE);
1925*7485b225SElliott Hughes doc.Parse(xml);
1926*7485b225SElliott Hughes XMLTest("Parse with whitespace preserved", false, doc.Error());
1927*7485b225SElliott Hughes XMLTest("Whitespace preserved", true, 0 == doc.FirstChildElement()->GetText());
1928*7485b225SElliott Hughes }
1929*7485b225SElliott Hughes
1930*7485b225SElliott Hughes {
1931*7485b225SElliott Hughes const char* xml = "<element> \n \n </element>";
1932*7485b225SElliott Hughes XMLDocument doc(true, PRESERVE_WHITESPACE);
1933*7485b225SElliott Hughes doc.Parse(xml);
1934*7485b225SElliott Hughes XMLTest("Parse with whitespace preserved", false, doc.Error());
1935*7485b225SElliott Hughes XMLTest("Whitespace preserved", true, 0 == doc.FirstChildElement()->GetText());
1936*7485b225SElliott Hughes }
1937*7485b225SElliott Hughes
1938*7485b225SElliott Hughes // ----------- Pedantic Whitespace ------------
1939*7485b225SElliott Hughes {
1940*7485b225SElliott Hughes const char* xml = "<element>This is ' \n\n text '</element>";
1941*7485b225SElliott Hughes XMLDocument doc(true, PEDANTIC_WHITESPACE);
1942*7485b225SElliott Hughes doc.Parse(xml);
1943*7485b225SElliott Hughes XMLTest("Parse with pedantic whitespace", false, doc.Error());
1944*7485b225SElliott Hughes XMLTest("Pedantic whitespace", "This is ' \n\n text '", doc.FirstChildElement()->GetText());
1945*7485b225SElliott Hughes }
1946*7485b225SElliott Hughes
1947*7485b225SElliott Hughes {
1948*7485b225SElliott Hughes const char* xml = "<element> This \nis ' text ' </element>";
1949*7485b225SElliott Hughes XMLDocument doc(true, PEDANTIC_WHITESPACE);
1950*7485b225SElliott Hughes doc.Parse(xml);
1951*7485b225SElliott Hughes XMLTest("Parse with pedantic whitespace", false, doc.Error());
1952*7485b225SElliott Hughes XMLTest("Pedantic whitespace", " This \nis ' text ' ", doc.FirstChildElement()->GetText());
1953*7485b225SElliott Hughes }
1954*7485b225SElliott Hughes
1955*7485b225SElliott Hughes {
1956*7485b225SElliott Hughes const char* xml = "<element> \n This is ' text ' \n</element>";
1957*7485b225SElliott Hughes XMLDocument doc(true, PEDANTIC_WHITESPACE);
1958*7485b225SElliott Hughes doc.Parse(xml);
1959*7485b225SElliott Hughes XMLTest("Parse with pedantic whitespace", false, doc.Error());
1960*7485b225SElliott Hughes XMLTest("Pedantic whitespace", " \n This is ' text ' \n", doc.FirstChildElement()->GetText());
1961*7485b225SElliott Hughes }
1962*7485b225SElliott Hughes
1963*7485b225SElliott Hughes // Following cases are for text that is all whitespace which is preserved with pedantic mode
1964*7485b225SElliott Hughes {
1965*7485b225SElliott Hughes const char* xml = "<element> </element>";
1966*7485b225SElliott Hughes XMLDocument doc(true, PEDANTIC_WHITESPACE);
1967*7485b225SElliott Hughes doc.Parse(xml);
1968*7485b225SElliott Hughes XMLTest("Parse with pedantic whitespace", false, doc.Error());
1969*7485b225SElliott Hughes XMLTest("Pedantic whitespace", " ", doc.FirstChildElement()->GetText());
1970*7485b225SElliott Hughes }
1971*7485b225SElliott Hughes
1972*7485b225SElliott Hughes {
1973*7485b225SElliott Hughes const char* xml = "<element> </element>";
1974*7485b225SElliott Hughes XMLDocument doc(true, PEDANTIC_WHITESPACE);
1975*7485b225SElliott Hughes doc.Parse(xml);
1976*7485b225SElliott Hughes XMLTest("Parse with pedantic whitespace", false, doc.Error());
1977*7485b225SElliott Hughes XMLTest("Pedantic whitespace", " ", doc.FirstChildElement()->GetText());
1978*7485b225SElliott Hughes }
1979*7485b225SElliott Hughes
1980*7485b225SElliott Hughes {
1981*7485b225SElliott Hughes const char* xml = "<element>\n\n</element>\n";
1982*7485b225SElliott Hughes XMLDocument doc(true, PEDANTIC_WHITESPACE);
1983*7485b225SElliott Hughes doc.Parse(xml);
1984*7485b225SElliott Hughes XMLTest("Parse with pedantic whitespace", false, doc.Error());
1985*7485b225SElliott Hughes XMLTest("Pedantic whitespace", "\n\n", doc.FirstChildElement()->GetText());
1986*7485b225SElliott Hughes }
1987*7485b225SElliott Hughes
1988*7485b225SElliott Hughes {
1989*7485b225SElliott Hughes const char* xml = "<element> \n</element> \n ";
1990*7485b225SElliott Hughes XMLDocument doc(true, PEDANTIC_WHITESPACE);
1991*7485b225SElliott Hughes doc.Parse(xml);
1992*7485b225SElliott Hughes XMLTest("Parse with pedantic whitespace", false, doc.Error());
1993*7485b225SElliott Hughes XMLTest("Pedantic whitespace", " \n", doc.FirstChildElement()->GetText());
1994*7485b225SElliott Hughes }
1995*7485b225SElliott Hughes
1996*7485b225SElliott Hughes {
1997*7485b225SElliott Hughes const char* xml = "<element> \n \n </element> ";
1998*7485b225SElliott Hughes XMLDocument doc(true, PEDANTIC_WHITESPACE);
1999*7485b225SElliott Hughes doc.Parse(xml);
2000*7485b225SElliott Hughes XMLTest("Parse with pedantic whitespace", false, doc.Error());
2001*7485b225SElliott Hughes XMLTest("Pedantic whitespace", " \n \n ", doc.FirstChildElement()->GetText());
2002*7485b225SElliott Hughes }
2003*7485b225SElliott Hughes
2004*7485b225SElliott Hughes // Following cases are for checking nested elements are still parsed with pedantic whitespace
2005*7485b225SElliott Hughes {
2006*7485b225SElliott Hughes const char* xml = "<element>\n\t<a> This is nested text </a>\n</element> ";
2007*7485b225SElliott Hughes XMLDocument doc(true, PEDANTIC_WHITESPACE);
2008*7485b225SElliott Hughes doc.Parse(xml);
2009*7485b225SElliott Hughes XMLTest("Parse nested elements with pedantic whitespace", false, doc.Error());
2010*7485b225SElliott Hughes XMLTest("Pedantic whitespace", " This is nested text ", doc.RootElement()->FirstChildElement()->GetText());
2011*7485b225SElliott Hughes }
2012*7485b225SElliott Hughes
2013*7485b225SElliott Hughes {
2014*7485b225SElliott Hughes const char* xml = "<element> <b> </b> </element>\n";
2015*7485b225SElliott Hughes XMLDocument doc(true, PEDANTIC_WHITESPACE);
2016*7485b225SElliott Hughes doc.Parse(xml);
2017*7485b225SElliott Hughes XMLTest("Parse nested elements with pedantic whitespace", false, doc.Error());
2018*7485b225SElliott Hughes XMLTest("Pedantic whitespace", " ", doc.RootElement()->FirstChildElement()->GetText());
2019*7485b225SElliott Hughes }
2020*7485b225SElliott Hughes
2021*7485b225SElliott Hughes {
2022*7485b225SElliott Hughes const char* xml = "<element> <c attribute=\"test\"/> </element>\n ";
2023*7485b225SElliott Hughes XMLDocument doc(true, PEDANTIC_WHITESPACE);
2024*7485b225SElliott Hughes doc.Parse(xml);
2025*7485b225SElliott Hughes XMLTest("Parse nested elements with pedantic whitespace", false, doc.Error());
2026*7485b225SElliott Hughes XMLTest("Pedantic whitespace", true, 0 == doc.RootElement()->FirstChildElement()->GetText());
2027*7485b225SElliott Hughes }
2028*7485b225SElliott Hughes
2029*7485b225SElliott Hughes // Check sample xml can be parsed with pedantic mode
2030*7485b225SElliott Hughes {
2031*7485b225SElliott Hughes XMLDocument doc(true, PEDANTIC_WHITESPACE);
2032*7485b225SElliott Hughes doc.LoadFile("resources/dream.xml");
2033*7485b225SElliott Hughes XMLTest("Load dream.xml with pedantic whitespace mode", false, doc.Error());
2034*7485b225SElliott Hughes
2035*7485b225SElliott Hughes XMLTest("Dream", "xml version=\"1.0\"",
2036*7485b225SElliott Hughes doc.FirstChild()->ToDeclaration()->Value());
2037*7485b225SElliott Hughes XMLTest("Dream", true, doc.FirstChild()->NextSibling()->ToUnknown() != 0);
2038*7485b225SElliott Hughes XMLTest("Dream", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
2039*7485b225SElliott Hughes doc.FirstChild()->NextSibling()->ToUnknown()->Value());
2040*7485b225SElliott Hughes XMLTest("Dream", "And Robin shall restore amends.",
2041*7485b225SElliott Hughes doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText());
2042*7485b225SElliott Hughes }
2043*7485b225SElliott Hughes
2044*7485b225SElliott Hughes {
2045*7485b225SElliott Hughes // An assert should not fire.
2046*7485b225SElliott Hughes const char* xml = "<element/>";
2047*7485b225SElliott Hughes XMLDocument doc;
2048*7485b225SElliott Hughes doc.Parse( xml );
2049*7485b225SElliott Hughes XMLTest( "Parse with self-closed element", false, doc.Error() );
2050*7485b225SElliott Hughes XMLElement* ele = doc.NewElement( "unused" ); // This will get cleaned up with the 'doc' going out of scope.
2051*7485b225SElliott Hughes XMLTest( "Tracking unused elements", true, ele != 0, false );
2052*7485b225SElliott Hughes }
2053*7485b225SElliott Hughes
2054*7485b225SElliott Hughes
2055*7485b225SElliott Hughes {
2056*7485b225SElliott Hughes const char* xml = "<parent><child>abc</child></parent>";
2057*7485b225SElliott Hughes XMLDocument doc;
2058*7485b225SElliott Hughes doc.Parse( xml );
2059*7485b225SElliott Hughes XMLTest( "Parse for printing of sub-element", false, doc.Error() );
2060*7485b225SElliott Hughes XMLElement* ele = doc.FirstChildElement( "parent")->FirstChildElement( "child");
2061*7485b225SElliott Hughes
2062*7485b225SElliott Hughes XMLPrinter printer;
2063*7485b225SElliott Hughes bool acceptResult = ele->Accept( &printer );
2064*7485b225SElliott Hughes XMLTest( "Accept of sub-element", true, acceptResult );
2065*7485b225SElliott Hughes XMLTest( "Printing of sub-element", "<child>abc</child>\n", printer.CStr(), false );
2066*7485b225SElliott Hughes }
2067*7485b225SElliott Hughes
2068*7485b225SElliott Hughes
2069*7485b225SElliott Hughes {
2070*7485b225SElliott Hughes XMLDocument doc;
2071*7485b225SElliott Hughes XMLError error = doc.LoadFile( "resources/empty.xml" );
2072*7485b225SElliott Hughes XMLTest( "Loading an empty file", XML_ERROR_EMPTY_DOCUMENT, error );
2073*7485b225SElliott Hughes XMLTest( "Loading an empty file and ErrorName as string", "XML_ERROR_EMPTY_DOCUMENT", doc.ErrorName() );
2074*7485b225SElliott Hughes doc.PrintError();
2075*7485b225SElliott Hughes }
2076*7485b225SElliott Hughes
2077*7485b225SElliott Hughes {
2078*7485b225SElliott Hughes // BOM preservation
2079*7485b225SElliott Hughes static const char* xml_bom_preservation = "\xef\xbb\xbf<element/>\n";
2080*7485b225SElliott Hughes {
2081*7485b225SElliott Hughes XMLDocument doc;
2082*7485b225SElliott Hughes XMLTest( "BOM preservation (parse)", XML_SUCCESS, doc.Parse( xml_bom_preservation ), false );
2083*7485b225SElliott Hughes XMLPrinter printer;
2084*7485b225SElliott Hughes doc.Print( &printer );
2085*7485b225SElliott Hughes
2086*7485b225SElliott Hughes XMLTest( "BOM preservation (compare)", xml_bom_preservation, printer.CStr(), false, true );
2087*7485b225SElliott Hughes doc.SaveFile( "resources/out/bomtest.xml" );
2088*7485b225SElliott Hughes XMLTest( "Save bomtest.xml", false, doc.Error() );
2089*7485b225SElliott Hughes }
2090*7485b225SElliott Hughes {
2091*7485b225SElliott Hughes XMLDocument doc;
2092*7485b225SElliott Hughes doc.LoadFile( "resources/out/bomtest.xml" );
2093*7485b225SElliott Hughes XMLTest( "Load bomtest.xml", false, doc.Error() );
2094*7485b225SElliott Hughes XMLTest( "BOM preservation (load)", true, doc.HasBOM(), false );
2095*7485b225SElliott Hughes
2096*7485b225SElliott Hughes XMLPrinter printer;
2097*7485b225SElliott Hughes doc.Print( &printer );
2098*7485b225SElliott Hughes XMLTest( "BOM preservation (compare)", xml_bom_preservation, printer.CStr(), false, true );
2099*7485b225SElliott Hughes }
2100*7485b225SElliott Hughes }
2101*7485b225SElliott Hughes
2102*7485b225SElliott Hughes {
2103*7485b225SElliott Hughes // Insertion with Removal
2104*7485b225SElliott Hughes const char* xml = "<?xml version=\"1.0\" ?>"
2105*7485b225SElliott Hughes "<root>"
2106*7485b225SElliott Hughes "<one>"
2107*7485b225SElliott Hughes "<subtree>"
2108*7485b225SElliott Hughes "<elem>element 1</elem>text<!-- comment -->"
2109*7485b225SElliott Hughes "</subtree>"
2110*7485b225SElliott Hughes "</one>"
2111*7485b225SElliott Hughes "<two/>"
2112*7485b225SElliott Hughes "</root>";
2113*7485b225SElliott Hughes const char* xmlInsideTwo = "<?xml version=\"1.0\" ?>"
2114*7485b225SElliott Hughes "<root>"
2115*7485b225SElliott Hughes "<one/>"
2116*7485b225SElliott Hughes "<two>"
2117*7485b225SElliott Hughes "<subtree>"
2118*7485b225SElliott Hughes "<elem>element 1</elem>text<!-- comment -->"
2119*7485b225SElliott Hughes "</subtree>"
2120*7485b225SElliott Hughes "</two>"
2121*7485b225SElliott Hughes "</root>";
2122*7485b225SElliott Hughes const char* xmlAfterOne = "<?xml version=\"1.0\" ?>"
2123*7485b225SElliott Hughes "<root>"
2124*7485b225SElliott Hughes "<one/>"
2125*7485b225SElliott Hughes "<subtree>"
2126*7485b225SElliott Hughes "<elem>element 1</elem>text<!-- comment -->"
2127*7485b225SElliott Hughes "</subtree>"
2128*7485b225SElliott Hughes "<two/>"
2129*7485b225SElliott Hughes "</root>";
2130*7485b225SElliott Hughes const char* xmlAfterTwo = "<?xml version=\"1.0\" ?>"
2131*7485b225SElliott Hughes "<root>"
2132*7485b225SElliott Hughes "<one/>"
2133*7485b225SElliott Hughes "<two/>"
2134*7485b225SElliott Hughes "<subtree>"
2135*7485b225SElliott Hughes "<elem>element 1</elem>text<!-- comment -->"
2136*7485b225SElliott Hughes "</subtree>"
2137*7485b225SElliott Hughes "</root>";
2138*7485b225SElliott Hughes
2139*7485b225SElliott Hughes XMLDocument doc;
2140*7485b225SElliott Hughes doc.Parse(xml);
2141*7485b225SElliott Hughes XMLTest( "Insertion with removal parse round 1", false, doc.Error() );
2142*7485b225SElliott Hughes XMLElement* subtree = doc.RootElement()->FirstChildElement("one")->FirstChildElement("subtree");
2143*7485b225SElliott Hughes XMLElement* two = doc.RootElement()->FirstChildElement("two");
2144*7485b225SElliott Hughes two->InsertFirstChild(subtree);
2145*7485b225SElliott Hughes XMLPrinter printer1(0, true);
2146*7485b225SElliott Hughes bool acceptResult = doc.Accept(&printer1);
2147*7485b225SElliott Hughes XMLTest("Move node from within <one> to <two> - Accept()", true, acceptResult);
2148*7485b225SElliott Hughes XMLTest("Move node from within <one> to <two>", xmlInsideTwo, printer1.CStr());
2149*7485b225SElliott Hughes
2150*7485b225SElliott Hughes doc.Parse(xml);
2151*7485b225SElliott Hughes XMLTest( "Insertion with removal parse round 2", false, doc.Error() );
2152*7485b225SElliott Hughes subtree = doc.RootElement()->FirstChildElement("one")->FirstChildElement("subtree");
2153*7485b225SElliott Hughes two = doc.RootElement()->FirstChildElement("two");
2154*7485b225SElliott Hughes doc.RootElement()->InsertAfterChild(two, subtree);
2155*7485b225SElliott Hughes XMLPrinter printer2(0, true);
2156*7485b225SElliott Hughes acceptResult = doc.Accept(&printer2);
2157*7485b225SElliott Hughes XMLTest("Move node from within <one> after <two> - Accept()", true, acceptResult);
2158*7485b225SElliott Hughes XMLTest("Move node from within <one> after <two>", xmlAfterTwo, printer2.CStr(), false);
2159*7485b225SElliott Hughes
2160*7485b225SElliott Hughes doc.Parse(xml);
2161*7485b225SElliott Hughes XMLTest( "Insertion with removal parse round 3", false, doc.Error() );
2162*7485b225SElliott Hughes XMLNode* one = doc.RootElement()->FirstChildElement("one");
2163*7485b225SElliott Hughes subtree = one->FirstChildElement("subtree");
2164*7485b225SElliott Hughes doc.RootElement()->InsertAfterChild(one, subtree);
2165*7485b225SElliott Hughes XMLPrinter printer3(0, true);
2166*7485b225SElliott Hughes acceptResult = doc.Accept(&printer3);
2167*7485b225SElliott Hughes XMLTest("Move node from within <one> after <one> - Accept()", true, acceptResult);
2168*7485b225SElliott Hughes XMLTest("Move node from within <one> after <one>", xmlAfterOne, printer3.CStr(), false);
2169*7485b225SElliott Hughes
2170*7485b225SElliott Hughes doc.Parse(xml);
2171*7485b225SElliott Hughes XMLTest( "Insertion with removal parse round 4", false, doc.Error() );
2172*7485b225SElliott Hughes subtree = doc.RootElement()->FirstChildElement("one")->FirstChildElement("subtree");
2173*7485b225SElliott Hughes two = doc.RootElement()->FirstChildElement("two");
2174*7485b225SElliott Hughes XMLTest("<two> is the last child at root level", true, two == doc.RootElement()->LastChildElement());
2175*7485b225SElliott Hughes doc.RootElement()->InsertEndChild(subtree);
2176*7485b225SElliott Hughes XMLPrinter printer4(0, true);
2177*7485b225SElliott Hughes acceptResult = doc.Accept(&printer4);
2178*7485b225SElliott Hughes XMLTest("Move node from within <one> after <two> - Accept()", true, acceptResult);
2179*7485b225SElliott Hughes XMLTest("Move node from within <one> after <two>", xmlAfterTwo, printer4.CStr(), false);
2180*7485b225SElliott Hughes }
2181*7485b225SElliott Hughes
2182*7485b225SElliott Hughes {
2183*7485b225SElliott Hughes const char* xml = "<svg width = \"128\" height = \"128\">"
2184*7485b225SElliott Hughes " <text> </text>"
2185*7485b225SElliott Hughes "</svg>";
2186*7485b225SElliott Hughes XMLDocument doc;
2187*7485b225SElliott Hughes doc.Parse(xml);
2188*7485b225SElliott Hughes XMLTest( "Parse svg with text", false, doc.Error() );
2189*7485b225SElliott Hughes doc.Print();
2190*7485b225SElliott Hughes }
2191*7485b225SElliott Hughes
2192*7485b225SElliott Hughes {
2193*7485b225SElliott Hughes // Test that it doesn't crash.
2194*7485b225SElliott Hughes const char* xml = "<?xml version=\"1.0\"?><root><sample><field0><1</field0><field1>2</field1></sample></root>";
2195*7485b225SElliott Hughes XMLDocument doc;
2196*7485b225SElliott Hughes doc.Parse(xml);
2197*7485b225SElliott Hughes XMLTest( "Parse root-sample-field0", true, doc.Error() );
2198*7485b225SElliott Hughes doc.PrintError();
2199*7485b225SElliott Hughes }
2200*7485b225SElliott Hughes
2201*7485b225SElliott Hughes #if 1
2202*7485b225SElliott Hughes // the question being explored is what kind of print to use:
2203*7485b225SElliott Hughes // https://github.com/leethomason/tinyxml2/issues/63
2204*7485b225SElliott Hughes {
2205*7485b225SElliott Hughes //const char* xml = "<element attrA='123456789.123456789' attrB='1.001e9' attrC='1.0e-10' attrD='1001000000.000000' attrE='0.1234567890123456789'/>";
2206*7485b225SElliott Hughes const char* xml = "<element/>";
2207*7485b225SElliott Hughes XMLDocument doc;
2208*7485b225SElliott Hughes doc.Parse( xml );
2209*7485b225SElliott Hughes XMLTest( "Parse self-closed empty element", false, doc.Error() );
2210*7485b225SElliott Hughes doc.FirstChildElement()->SetAttribute( "attrA-f64", 123456789.123456789 );
2211*7485b225SElliott Hughes doc.FirstChildElement()->SetAttribute( "attrB-f64", 1.001e9 );
2212*7485b225SElliott Hughes doc.FirstChildElement()->SetAttribute( "attrC-f64", 1.0e9 );
2213*7485b225SElliott Hughes doc.FirstChildElement()->SetAttribute( "attrC-f64", 1.0e20 );
2214*7485b225SElliott Hughes doc.FirstChildElement()->SetAttribute( "attrD-f64", 1.0e-10 );
2215*7485b225SElliott Hughes doc.FirstChildElement()->SetAttribute( "attrD-f64", 0.123456789 );
2216*7485b225SElliott Hughes
2217*7485b225SElliott Hughes doc.FirstChildElement()->SetAttribute( "attrA-f32", 123456789.123456789f );
2218*7485b225SElliott Hughes doc.FirstChildElement()->SetAttribute( "attrB-f32", 1.001e9f );
2219*7485b225SElliott Hughes doc.FirstChildElement()->SetAttribute( "attrC-f32", 1.0e9f );
2220*7485b225SElliott Hughes doc.FirstChildElement()->SetAttribute( "attrC-f32", 1.0e20f );
2221*7485b225SElliott Hughes doc.FirstChildElement()->SetAttribute( "attrD-f32", 1.0e-10f );
2222*7485b225SElliott Hughes doc.FirstChildElement()->SetAttribute( "attrD-f32", 0.123456789f );
2223*7485b225SElliott Hughes
2224*7485b225SElliott Hughes doc.Print();
2225*7485b225SElliott Hughes
2226*7485b225SElliott Hughes /* The result of this test is platform, compiler, and library version dependent. :("
2227*7485b225SElliott Hughes XMLPrinter printer;
2228*7485b225SElliott Hughes doc.Print( &printer );
2229*7485b225SElliott Hughes XMLTest( "Float and double formatting.",
2230*7485b225SElliott Hughes "<element attrA-f64=\"123456789.12345679\" attrB-f64=\"1001000000\" attrC-f64=\"1e+20\" attrD-f64=\"0.123456789\" attrA-f32=\"1.2345679e+08\" attrB-f32=\"1.001e+09\" attrC-f32=\"1e+20\" attrD-f32=\"0.12345679\"/>\n",
2231*7485b225SElliott Hughes printer.CStr(),
2232*7485b225SElliott Hughes true );
2233*7485b225SElliott Hughes */
2234*7485b225SElliott Hughes }
2235*7485b225SElliott Hughes #endif
2236*7485b225SElliott Hughes
2237*7485b225SElliott Hughes {
2238*7485b225SElliott Hughes // Issue #184
2239*7485b225SElliott Hughes // If it doesn't assert, it passes. Caused by objects
2240*7485b225SElliott Hughes // getting created during parsing which are then
2241*7485b225SElliott Hughes // inaccessible in the memory pools.
2242*7485b225SElliott Hughes const char* xmlText = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><test>";
2243*7485b225SElliott Hughes {
2244*7485b225SElliott Hughes XMLDocument doc;
2245*7485b225SElliott Hughes doc.Parse(xmlText);
2246*7485b225SElliott Hughes XMLTest( "Parse hex no closing tag round 1", true, doc.Error() );
2247*7485b225SElliott Hughes }
2248*7485b225SElliott Hughes {
2249*7485b225SElliott Hughes XMLDocument doc;
2250*7485b225SElliott Hughes doc.Parse(xmlText);
2251*7485b225SElliott Hughes XMLTest( "Parse hex no closing tag round 2", true, doc.Error() );
2252*7485b225SElliott Hughes doc.Clear();
2253*7485b225SElliott Hughes }
2254*7485b225SElliott Hughes }
2255*7485b225SElliott Hughes
2256*7485b225SElliott Hughes {
2257*7485b225SElliott Hughes // If this doesn't assert in TINYXML2_DEBUG, all is well.
2258*7485b225SElliott Hughes tinyxml2::XMLDocument doc;
2259*7485b225SElliott Hughes tinyxml2::XMLElement *pRoot = doc.NewElement("Root");
2260*7485b225SElliott Hughes doc.DeleteNode(pRoot);
2261*7485b225SElliott Hughes }
2262*7485b225SElliott Hughes
2263*7485b225SElliott Hughes {
2264*7485b225SElliott Hughes XMLDocument doc;
2265*7485b225SElliott Hughes XMLElement* root = doc.NewElement( "Root" );
2266*7485b225SElliott Hughes XMLTest( "Node document before insertion", true, &doc == root->GetDocument() );
2267*7485b225SElliott Hughes doc.InsertEndChild( root );
2268*7485b225SElliott Hughes XMLTest( "Node document after insertion", true, &doc == root->GetDocument() );
2269*7485b225SElliott Hughes }
2270*7485b225SElliott Hughes
2271*7485b225SElliott Hughes {
2272*7485b225SElliott Hughes // If this doesn't assert in TINYXML2_DEBUG, all is well.
2273*7485b225SElliott Hughes XMLDocument doc;
2274*7485b225SElliott Hughes XMLElement* unlinkedRoot = doc.NewElement( "Root" );
2275*7485b225SElliott Hughes XMLElement* linkedRoot = doc.NewElement( "Root" );
2276*7485b225SElliott Hughes doc.InsertFirstChild( linkedRoot );
2277*7485b225SElliott Hughes unlinkedRoot->GetDocument()->DeleteNode( linkedRoot );
2278*7485b225SElliott Hughes unlinkedRoot->GetDocument()->DeleteNode( unlinkedRoot );
2279*7485b225SElliott Hughes }
2280*7485b225SElliott Hughes
2281*7485b225SElliott Hughes {
2282*7485b225SElliott Hughes // Should not assert in TINYXML2_DEBUG
2283*7485b225SElliott Hughes XMLPrinter printer;
2284*7485b225SElliott Hughes }
2285*7485b225SElliott Hughes
2286*7485b225SElliott Hughes {
2287*7485b225SElliott Hughes // Issue 291. Should not crash
2288*7485b225SElliott Hughes const char* xml = "�</a>";
2289*7485b225SElliott Hughes XMLDocument doc;
2290*7485b225SElliott Hughes doc.Parse( xml );
2291*7485b225SElliott Hughes XMLTest( "Parse hex with closing tag", false, doc.Error() );
2292*7485b225SElliott Hughes
2293*7485b225SElliott Hughes XMLPrinter printer;
2294*7485b225SElliott Hughes doc.Print( &printer );
2295*7485b225SElliott Hughes }
2296*7485b225SElliott Hughes {
2297*7485b225SElliott Hughes // Issue 299. Can print elements that are not linked in.
2298*7485b225SElliott Hughes // Will crash if issue not fixed.
2299*7485b225SElliott Hughes XMLDocument doc;
2300*7485b225SElliott Hughes XMLElement* newElement = doc.NewElement( "printme" );
2301*7485b225SElliott Hughes XMLPrinter printer;
2302*7485b225SElliott Hughes bool acceptResult = newElement->Accept( &printer );
2303*7485b225SElliott Hughes XMLTest( "printme - Accept()", true, acceptResult );
2304*7485b225SElliott Hughes // Delete the node to avoid possible memory leak report in debug output
2305*7485b225SElliott Hughes doc.DeleteNode( newElement );
2306*7485b225SElliott Hughes }
2307*7485b225SElliott Hughes {
2308*7485b225SElliott Hughes // Issue 302. Clear errors from LoadFile/SaveFile
2309*7485b225SElliott Hughes XMLDocument doc;
2310*7485b225SElliott Hughes XMLTest( "Issue 302. Should be no error initially", "XML_SUCCESS", doc.ErrorName() );
2311*7485b225SElliott Hughes doc.SaveFile( "./no/such/path/pretty.xml" );
2312*7485b225SElliott Hughes XMLTest( "Issue 302. Fail to save", "XML_ERROR_FILE_COULD_NOT_BE_OPENED", doc.ErrorName() );
2313*7485b225SElliott Hughes doc.SaveFile( "./resources/out/compact.xml", true );
2314*7485b225SElliott Hughes XMLTest( "Issue 302. Subsequent success in saving", "XML_SUCCESS", doc.ErrorName() );
2315*7485b225SElliott Hughes }
2316*7485b225SElliott Hughes
2317*7485b225SElliott Hughes {
2318*7485b225SElliott Hughes // If a document fails to load then subsequent
2319*7485b225SElliott Hughes // successful loads should clear the error
2320*7485b225SElliott Hughes XMLDocument doc;
2321*7485b225SElliott Hughes XMLTest( "Should be no error initially", false, doc.Error() );
2322*7485b225SElliott Hughes doc.LoadFile( "resources/no-such-file.xml" );
2323*7485b225SElliott Hughes XMLTest( "No such file - should fail", true, doc.Error() );
2324*7485b225SElliott Hughes
2325*7485b225SElliott Hughes doc.LoadFile("resources/dream.xml");
2326*7485b225SElliott Hughes XMLTest("Error should be cleared", false, doc.Error());
2327*7485b225SElliott Hughes
2328*7485b225SElliott Hughes doc.LoadFile( "resources/xmltest-5330.xml" );
2329*7485b225SElliott Hughes XMLTest( "parse errors occur - should fail", true, doc.Error() );
2330*7485b225SElliott Hughes
2331*7485b225SElliott Hughes doc.LoadFile( "resources/dream.xml" );
2332*7485b225SElliott Hughes XMLTest( "Error should be cleared", false, doc.Error() );
2333*7485b225SElliott Hughes }
2334*7485b225SElliott Hughes
2335*7485b225SElliott Hughes {
2336*7485b225SElliott Hughes // Check that declarations are allowed only at beginning of document
2337*7485b225SElliott Hughes const char* xml0 = "<?xml version=\"1.0\" ?>"
2338*7485b225SElliott Hughes " <!-- xml version=\"1.1\" -->"
2339*7485b225SElliott Hughes "<first />";
2340*7485b225SElliott Hughes const char* xml1 = "<?xml version=\"1.0\" ?>"
2341*7485b225SElliott Hughes "<?xml-stylesheet type=\"text/xsl\" href=\"Anything.xsl\"?>"
2342*7485b225SElliott Hughes "<first />";
2343*7485b225SElliott Hughes const char* xml2 = "<first />"
2344*7485b225SElliott Hughes "<?xml version=\"1.0\" ?>";
2345*7485b225SElliott Hughes const char* xml3 = "<first></first>"
2346*7485b225SElliott Hughes "<?xml version=\"1.0\" ?>";
2347*7485b225SElliott Hughes
2348*7485b225SElliott Hughes const char* xml4 = "<first><?xml version=\"1.0\" ?></first>";
2349*7485b225SElliott Hughes
2350*7485b225SElliott Hughes XMLDocument doc;
2351*7485b225SElliott Hughes doc.Parse(xml0);
2352*7485b225SElliott Hughes XMLTest("Test that the code changes do not affect normal parsing", false, doc.Error() );
2353*7485b225SElliott Hughes doc.Parse(xml1);
2354*7485b225SElliott Hughes XMLTest("Test that the second declaration is allowed", false, doc.Error() );
2355*7485b225SElliott Hughes doc.Parse(xml2);
2356*7485b225SElliott Hughes XMLTest("Test that declaration after self-closed child is not allowed", XML_ERROR_PARSING_DECLARATION, doc.ErrorID() );
2357*7485b225SElliott Hughes doc.Parse(xml3);
2358*7485b225SElliott Hughes XMLTest("Test that declaration after a child is not allowed", XML_ERROR_PARSING_DECLARATION, doc.ErrorID() );
2359*7485b225SElliott Hughes doc.Parse(xml4);
2360*7485b225SElliott Hughes XMLTest("Test that declaration inside a child is not allowed", XML_ERROR_PARSING_DECLARATION, doc.ErrorID() );
2361*7485b225SElliott Hughes }
2362*7485b225SElliott Hughes
2363*7485b225SElliott Hughes {
2364*7485b225SElliott Hughes // No matter - before or after successfully parsing a text -
2365*7485b225SElliott Hughes // calling XMLDocument::Value() used to cause an assert in debug.
2366*7485b225SElliott Hughes // Null must be returned.
2367*7485b225SElliott Hughes const char* validXml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
2368*7485b225SElliott Hughes "<first />"
2369*7485b225SElliott Hughes "<second />";
2370*7485b225SElliott Hughes XMLDocument* doc = new XMLDocument();
2371*7485b225SElliott Hughes XMLTest( "XMLDocument::Value() returns null?", NULL, doc->Value() );
2372*7485b225SElliott Hughes doc->Parse( validXml );
2373*7485b225SElliott Hughes XMLTest( "Parse to test XMLDocument::Value()", false, doc->Error());
2374*7485b225SElliott Hughes XMLTest( "XMLDocument::Value() returns null?", NULL, doc->Value() );
2375*7485b225SElliott Hughes delete doc;
2376*7485b225SElliott Hughes }
2377*7485b225SElliott Hughes
2378*7485b225SElliott Hughes {
2379*7485b225SElliott Hughes XMLDocument doc;
2380*7485b225SElliott Hughes for( int i = 0; i < XML_ERROR_COUNT; i++ ) {
2381*7485b225SElliott Hughes const XMLError error = static_cast<XMLError>(i);
2382*7485b225SElliott Hughes const char* name = XMLDocument::ErrorIDToName(error);
2383*7485b225SElliott Hughes XMLTest( "ErrorName() not null after ClearError()", true, name != 0 );
2384*7485b225SElliott Hughes if( name == 0 ) {
2385*7485b225SElliott Hughes // passing null pointer into strlen() is undefined behavior, so
2386*7485b225SElliott Hughes // compiler is allowed to optimise away the null test above if it's
2387*7485b225SElliott Hughes // as reachable as the strlen() call
2388*7485b225SElliott Hughes continue;
2389*7485b225SElliott Hughes }
2390*7485b225SElliott Hughes XMLTest( "ErrorName() not empty after ClearError()", true, strlen(name) > 0 );
2391*7485b225SElliott Hughes }
2392*7485b225SElliott Hughes }
2393*7485b225SElliott Hughes
2394*7485b225SElliott Hughes {
2395*7485b225SElliott Hughes const char* html("<!DOCTYPE html><html><body><p>test</p><p><br/></p></body></html>");
2396*7485b225SElliott Hughes XMLDocument doc(false);
2397*7485b225SElliott Hughes doc.Parse(html);
2398*7485b225SElliott Hughes
2399*7485b225SElliott Hughes XMLPrinter printer(0, true);
2400*7485b225SElliott Hughes doc.Print(&printer);
2401*7485b225SElliott Hughes
2402*7485b225SElliott Hughes XMLTest(html, html, printer.CStr());
2403*7485b225SElliott Hughes }
2404*7485b225SElliott Hughes
2405*7485b225SElliott Hughes {
2406*7485b225SElliott Hughes // Evil memory leaks.
2407*7485b225SElliott Hughes // If an XMLElement (etc) is allocated via NewElement() (etc.)
2408*7485b225SElliott Hughes // and NOT added to the XMLDocument, what happens?
2409*7485b225SElliott Hughes //
2410*7485b225SElliott Hughes // Previously (buggy):
2411*7485b225SElliott Hughes // The memory would be free'd when the XMLDocument is
2412*7485b225SElliott Hughes // destructed. But the XMLElement destructor wasn't called, so
2413*7485b225SElliott Hughes // memory allocated for the XMLElement text would not be free'd.
2414*7485b225SElliott Hughes // In practice this meant strings allocated for the XMLElement
2415*7485b225SElliott Hughes // text would be leaked. An edge case, but annoying.
2416*7485b225SElliott Hughes // Now:
2417*7485b225SElliott Hughes // The XMLElement destructor is called. But the unlinked nodes
2418*7485b225SElliott Hughes // have to be tracked using a list. This has a minor performance
2419*7485b225SElliott Hughes // impact that can become significant if you have a lot of
2420*7485b225SElliott Hughes // unlinked nodes. (But why would you do that?)
2421*7485b225SElliott Hughes // The only way to see this bug was in a Visual C++ runtime debug heap
2422*7485b225SElliott Hughes // leak tracker. This is compiled in by default on Windows Debug and
2423*7485b225SElliott Hughes // enabled with _CRTDBG_LEAK_CHECK_DF parameter passed to _CrtSetDbgFlag().
2424*7485b225SElliott Hughes {
2425*7485b225SElliott Hughes XMLDocument doc;
2426*7485b225SElliott Hughes doc.NewElement("LEAK 1");
2427*7485b225SElliott Hughes }
2428*7485b225SElliott Hughes {
2429*7485b225SElliott Hughes XMLDocument doc;
2430*7485b225SElliott Hughes XMLElement* ele = doc.NewElement("LEAK 2");
2431*7485b225SElliott Hughes doc.DeleteNode(ele);
2432*7485b225SElliott Hughes }
2433*7485b225SElliott Hughes }
2434*7485b225SElliott Hughes
2435*7485b225SElliott Hughes {
2436*7485b225SElliott Hughes // Bad bad crash. Parsing error results in stack overflow, if uncaught.
2437*7485b225SElliott Hughes const char* TESTS[] = {
2438*7485b225SElliott Hughes "./resources/xmltest-5330.xml",
2439*7485b225SElliott Hughes "./resources/xmltest-4636783552757760.xml",
2440*7485b225SElliott Hughes "./resources/xmltest-5720541257269248.xml",
2441*7485b225SElliott Hughes 0
2442*7485b225SElliott Hughes };
2443*7485b225SElliott Hughes for (int i=0; TESTS[i]; ++i) {
2444*7485b225SElliott Hughes XMLDocument doc;
2445*7485b225SElliott Hughes doc.LoadFile(TESTS[i]);
2446*7485b225SElliott Hughes XMLTest("Stack overflow prevented.", XML_ELEMENT_DEPTH_EXCEEDED, doc.ErrorID());
2447*7485b225SElliott Hughes }
2448*7485b225SElliott Hughes }
2449*7485b225SElliott Hughes {
2450*7485b225SElliott Hughes const char* TESTS[] = {
2451*7485b225SElliott Hughes "./resources/xmltest-5662204197076992.xml", // Security-level performance issue.
2452*7485b225SElliott Hughes 0
2453*7485b225SElliott Hughes };
2454*7485b225SElliott Hughes for (int i = 0; TESTS[i]; ++i) {
2455*7485b225SElliott Hughes XMLDocument doc;
2456*7485b225SElliott Hughes doc.LoadFile(TESTS[i]);
2457*7485b225SElliott Hughes // Need only not crash / lock up.
2458*7485b225SElliott Hughes XMLTest("Fuzz attack prevented.", true, true);
2459*7485b225SElliott Hughes }
2460*7485b225SElliott Hughes }
2461*7485b225SElliott Hughes {
2462*7485b225SElliott Hughes // Crashing reported via email.
2463*7485b225SElliott Hughes const char* xml =
2464*7485b225SElliott Hughes "<playlist id='playlist1'>"
2465*7485b225SElliott Hughes "<property name='track_name'>voice</property>"
2466*7485b225SElliott Hughes "<property name='audio_track'>1</property>"
2467*7485b225SElliott Hughes "<entry out = '604' producer = '4_playlist1' in = '0' />"
2468*7485b225SElliott Hughes "<blank length = '1' />"
2469*7485b225SElliott Hughes "<entry out = '1625' producer = '3_playlist' in = '0' />"
2470*7485b225SElliott Hughes "<blank length = '2' />"
2471*7485b225SElliott Hughes "<entry out = '946' producer = '2_playlist1' in = '0' />"
2472*7485b225SElliott Hughes "<blank length = '1' />"
2473*7485b225SElliott Hughes "<entry out = '128' producer = '1_playlist1' in = '0' />"
2474*7485b225SElliott Hughes "</playlist>";
2475*7485b225SElliott Hughes
2476*7485b225SElliott Hughes // It's not a good idea to delete elements as you walk the
2477*7485b225SElliott Hughes // list. I'm not sure this technically should work; but it's
2478*7485b225SElliott Hughes // an interesting test case.
2479*7485b225SElliott Hughes XMLDocument doc;
2480*7485b225SElliott Hughes XMLError err = doc.Parse(xml);
2481*7485b225SElliott Hughes XMLTest("Crash bug parsing", XML_SUCCESS, err );
2482*7485b225SElliott Hughes
2483*7485b225SElliott Hughes XMLElement* playlist = doc.FirstChildElement("playlist");
2484*7485b225SElliott Hughes XMLTest("Crash bug parsing", true, playlist != 0);
2485*7485b225SElliott Hughes
2486*7485b225SElliott Hughes {
2487*7485b225SElliott Hughes const char* elementName = "entry";
2488*7485b225SElliott Hughes XMLElement* entry = playlist->FirstChildElement(elementName);
2489*7485b225SElliott Hughes XMLTest("Crash bug parsing", true, entry != 0);
2490*7485b225SElliott Hughes while (entry) {
2491*7485b225SElliott Hughes XMLElement* todelete = entry;
2492*7485b225SElliott Hughes entry = entry->NextSiblingElement(elementName);
2493*7485b225SElliott Hughes playlist->DeleteChild(todelete);
2494*7485b225SElliott Hughes }
2495*7485b225SElliott Hughes entry = playlist->FirstChildElement(elementName);
2496*7485b225SElliott Hughes XMLTest("Crash bug parsing", true, entry == 0);
2497*7485b225SElliott Hughes }
2498*7485b225SElliott Hughes {
2499*7485b225SElliott Hughes const char* elementName = "blank";
2500*7485b225SElliott Hughes XMLElement* blank = playlist->FirstChildElement(elementName);
2501*7485b225SElliott Hughes XMLTest("Crash bug parsing", true, blank != 0);
2502*7485b225SElliott Hughes while (blank) {
2503*7485b225SElliott Hughes XMLElement* todelete = blank;
2504*7485b225SElliott Hughes blank = blank->NextSiblingElement(elementName);
2505*7485b225SElliott Hughes playlist->DeleteChild(todelete);
2506*7485b225SElliott Hughes }
2507*7485b225SElliott Hughes XMLTest("Crash bug parsing", true, blank == 0);
2508*7485b225SElliott Hughes }
2509*7485b225SElliott Hughes
2510*7485b225SElliott Hughes tinyxml2::XMLPrinter printer;
2511*7485b225SElliott Hughes const bool acceptResult = playlist->Accept(&printer);
2512*7485b225SElliott Hughes XMLTest("Crash bug parsing - Accept()", true, acceptResult);
2513*7485b225SElliott Hughes printf("%s\n", printer.CStr());
2514*7485b225SElliott Hughes
2515*7485b225SElliott Hughes // No test; it only need to not crash.
2516*7485b225SElliott Hughes // Still, wrap it up with a sanity check
2517*7485b225SElliott Hughes int nProperty = 0;
2518*7485b225SElliott Hughes for (const XMLElement* p = playlist->FirstChildElement("property"); p; p = p->NextSiblingElement("property")) {
2519*7485b225SElliott Hughes nProperty++;
2520*7485b225SElliott Hughes }
2521*7485b225SElliott Hughes XMLTest("Crash bug parsing", 2, nProperty);
2522*7485b225SElliott Hughes }
2523*7485b225SElliott Hughes
2524*7485b225SElliott Hughes // ----------- Line Number Tracking --------------
2525*7485b225SElliott Hughes {
2526*7485b225SElliott Hughes struct TestUtil: XMLVisitor
2527*7485b225SElliott Hughes {
2528*7485b225SElliott Hughes TestUtil() : str() {}
2529*7485b225SElliott Hughes
2530*7485b225SElliott Hughes void TestParseError(const char *testString, const char *docStr, XMLError expected_error, int expectedLine)
2531*7485b225SElliott Hughes {
2532*7485b225SElliott Hughes XMLDocument doc;
2533*7485b225SElliott Hughes const XMLError parseError = doc.Parse(docStr);
2534*7485b225SElliott Hughes
2535*7485b225SElliott Hughes XMLTest(testString, parseError, doc.ErrorID());
2536*7485b225SElliott Hughes XMLTest(testString, true, doc.Error());
2537*7485b225SElliott Hughes XMLTest(testString, expected_error, parseError);
2538*7485b225SElliott Hughes XMLTest(testString, expectedLine, doc.ErrorLineNum());
2539*7485b225SElliott Hughes };
2540*7485b225SElliott Hughes
2541*7485b225SElliott Hughes void TestStringLines(const char *testString, const char *docStr, const char *expectedLines)
2542*7485b225SElliott Hughes {
2543*7485b225SElliott Hughes XMLDocument doc;
2544*7485b225SElliott Hughes doc.Parse(docStr);
2545*7485b225SElliott Hughes XMLTest(testString, false, doc.Error());
2546*7485b225SElliott Hughes TestDocLines(testString, doc, expectedLines);
2547*7485b225SElliott Hughes }
2548*7485b225SElliott Hughes
2549*7485b225SElliott Hughes void TestFileLines(const char *testString, const char *file_name, const char *expectedLines)
2550*7485b225SElliott Hughes {
2551*7485b225SElliott Hughes XMLDocument doc;
2552*7485b225SElliott Hughes doc.LoadFile(file_name);
2553*7485b225SElliott Hughes XMLTest(testString, false, doc.Error());
2554*7485b225SElliott Hughes TestDocLines(testString, doc, expectedLines);
2555*7485b225SElliott Hughes }
2556*7485b225SElliott Hughes
2557*7485b225SElliott Hughes private:
2558*7485b225SElliott Hughes DynArray<char, 10> str;
2559*7485b225SElliott Hughes
2560*7485b225SElliott Hughes void Push(char type, int lineNum)
2561*7485b225SElliott Hughes {
2562*7485b225SElliott Hughes str.Push(type);
2563*7485b225SElliott Hughes str.Push(char('0' + (lineNum / 10)));
2564*7485b225SElliott Hughes str.Push(char('0' + (lineNum % 10)));
2565*7485b225SElliott Hughes }
2566*7485b225SElliott Hughes
2567*7485b225SElliott Hughes bool VisitEnter(const XMLDocument& doc)
2568*7485b225SElliott Hughes {
2569*7485b225SElliott Hughes Push('D', doc.GetLineNum());
2570*7485b225SElliott Hughes return true;
2571*7485b225SElliott Hughes }
2572*7485b225SElliott Hughes bool VisitEnter(const XMLElement& element, const XMLAttribute* firstAttribute)
2573*7485b225SElliott Hughes {
2574*7485b225SElliott Hughes Push('E', element.GetLineNum());
2575*7485b225SElliott Hughes for (const XMLAttribute *attr = firstAttribute; attr != 0; attr = attr->Next())
2576*7485b225SElliott Hughes Push('A', attr->GetLineNum());
2577*7485b225SElliott Hughes return true;
2578*7485b225SElliott Hughes }
2579*7485b225SElliott Hughes bool Visit(const XMLDeclaration& declaration)
2580*7485b225SElliott Hughes {
2581*7485b225SElliott Hughes Push('L', declaration.GetLineNum());
2582*7485b225SElliott Hughes return true;
2583*7485b225SElliott Hughes }
2584*7485b225SElliott Hughes bool Visit(const XMLText& text)
2585*7485b225SElliott Hughes {
2586*7485b225SElliott Hughes Push('T', text.GetLineNum());
2587*7485b225SElliott Hughes return true;
2588*7485b225SElliott Hughes }
2589*7485b225SElliott Hughes bool Visit(const XMLComment& comment)
2590*7485b225SElliott Hughes {
2591*7485b225SElliott Hughes Push('C', comment.GetLineNum());
2592*7485b225SElliott Hughes return true;
2593*7485b225SElliott Hughes }
2594*7485b225SElliott Hughes bool Visit(const XMLUnknown& unknown)
2595*7485b225SElliott Hughes {
2596*7485b225SElliott Hughes Push('U', unknown.GetLineNum());
2597*7485b225SElliott Hughes return true;
2598*7485b225SElliott Hughes }
2599*7485b225SElliott Hughes
2600*7485b225SElliott Hughes void TestDocLines(const char *testString, XMLDocument &doc, const char *expectedLines)
2601*7485b225SElliott Hughes {
2602*7485b225SElliott Hughes str.Clear();
2603*7485b225SElliott Hughes const bool acceptResult = doc.Accept(this);
2604*7485b225SElliott Hughes XMLTest(testString, true, acceptResult);
2605*7485b225SElliott Hughes str.Push(0);
2606*7485b225SElliott Hughes XMLTest(testString, expectedLines, str.Mem());
2607*7485b225SElliott Hughes }
2608*7485b225SElliott Hughes } tester;
2609*7485b225SElliott Hughes
2610*7485b225SElliott Hughes tester.TestParseError("ErrorLine-Parsing", "\n<root>\n foo \n<unclosed/>", XML_ERROR_PARSING, 2);
2611*7485b225SElliott Hughes tester.TestParseError("ErrorLine-Declaration", "<root>\n<?xml version=\"1.0\"?>", XML_ERROR_PARSING_DECLARATION, 2);
2612*7485b225SElliott Hughes tester.TestParseError("ErrorLine-Mismatch", "\n<root>\n</mismatch>", XML_ERROR_MISMATCHED_ELEMENT, 2);
2613*7485b225SElliott Hughes tester.TestParseError("ErrorLine-CData", "\n<root><![CDATA[ \n foo bar \n", XML_ERROR_PARSING_CDATA, 2);
2614*7485b225SElliott Hughes tester.TestParseError("ErrorLine-Text", "\n<root>\n foo bar \n", XML_ERROR_PARSING_TEXT, 3);
2615*7485b225SElliott Hughes tester.TestParseError("ErrorLine-Comment", "\n<root>\n<!-- >\n", XML_ERROR_PARSING_COMMENT, 3);
2616*7485b225SElliott Hughes tester.TestParseError("ErrorLine-Declaration", "\n<root>\n<? >\n", XML_ERROR_PARSING_DECLARATION, 3);
2617*7485b225SElliott Hughes tester.TestParseError("ErrorLine-Unknown", "\n<root>\n<! \n", XML_ERROR_PARSING_UNKNOWN, 3);
2618*7485b225SElliott Hughes tester.TestParseError("ErrorLine-Element", "\n<root>\n<unclosed \n", XML_ERROR_PARSING_ELEMENT, 3);
2619*7485b225SElliott Hughes tester.TestParseError("ErrorLine-Attribute", "\n<root>\n<unclosed \n att\n", XML_ERROR_PARSING_ATTRIBUTE, 4);
2620*7485b225SElliott Hughes tester.TestParseError("ErrorLine-ElementClose", "\n<root>\n<unclosed \n/unexpected", XML_ERROR_PARSING_ELEMENT, 3);
2621*7485b225SElliott Hughes
2622*7485b225SElliott Hughes tester.TestStringLines(
2623*7485b225SElliott Hughes "LineNumbers-String",
2624*7485b225SElliott Hughes
2625*7485b225SElliott Hughes "<?xml version=\"1.0\"?>\n" // 1 Doc, DecL
2626*7485b225SElliott Hughes "<root a='b' \n" // 2 Element Attribute
2627*7485b225SElliott Hughes "c='d'> d <blah/> \n" // 3 Attribute Text Element
2628*7485b225SElliott Hughes "newline in text \n" // 4 Text
2629*7485b225SElliott Hughes "and second <zxcv/><![CDATA[\n" // 5 Element Text
2630*7485b225SElliott Hughes " cdata test ]]><!-- comment -->\n" // 6 Comment
2631*7485b225SElliott Hughes "<! unknown></root>", // 7 Unknown
2632*7485b225SElliott Hughes
2633*7485b225SElliott Hughes "D01L01E02A02A03T03E03T04E05T05C06U07");
2634*7485b225SElliott Hughes
2635*7485b225SElliott Hughes tester.TestStringLines(
2636*7485b225SElliott Hughes "LineNumbers-CRLF",
2637*7485b225SElliott Hughes
2638*7485b225SElliott Hughes "\r\n" // 1 Doc (arguably should be line 2)
2639*7485b225SElliott Hughes "<?xml version=\"1.0\"?>\n" // 2 DecL
2640*7485b225SElliott Hughes "<root>\r\n" // 3 Element
2641*7485b225SElliott Hughes "\n" // 4
2642*7485b225SElliott Hughes "text contining new line \n" // 5 Text
2643*7485b225SElliott Hughes " and also containing crlf \r\n" // 6
2644*7485b225SElliott Hughes "<sub><![CDATA[\n" // 7 Element Text
2645*7485b225SElliott Hughes "cdata containing new line \n" // 8
2646*7485b225SElliott Hughes " and also containing cflr\r\n" // 9
2647*7485b225SElliott Hughes "]]></sub><sub2/></root>", // 10 Element
2648*7485b225SElliott Hughes
2649*7485b225SElliott Hughes "D01L02E03T05E07T07E10");
2650*7485b225SElliott Hughes
2651*7485b225SElliott Hughes tester.TestFileLines(
2652*7485b225SElliott Hughes "LineNumbers-File",
2653*7485b225SElliott Hughes "resources/utf8test.xml",
2654*7485b225SElliott Hughes "D01L01E02E03A03A03T03E04A04A04T04E05A05A05T05E06A06A06T06E07A07A07T07E08A08A08T08E09T09E10T10");
2655*7485b225SElliott Hughes }
2656*7485b225SElliott Hughes
2657*7485b225SElliott Hughes {
2658*7485b225SElliott Hughes const char* xml = "<Hello>Text</Error>";
2659*7485b225SElliott Hughes XMLDocument doc;
2660*7485b225SElliott Hughes doc.Parse(xml);
2661*7485b225SElliott Hughes XMLTest("Test mismatched elements.", true, doc.Error());
2662*7485b225SElliott Hughes XMLTest("Test mismatched elements.", XML_ERROR_MISMATCHED_ELEMENT, doc.ErrorID());
2663*7485b225SElliott Hughes // For now just make sure calls work & doesn't crash.
2664*7485b225SElliott Hughes // May solidify the error output in the future.
2665*7485b225SElliott Hughes printf("%s\n", doc.ErrorStr());
2666*7485b225SElliott Hughes doc.PrintError();
2667*7485b225SElliott Hughes }
2668*7485b225SElliott Hughes
2669*7485b225SElliott Hughes // ----------- Performance tracking --------------
2670*7485b225SElliott Hughes {
2671*7485b225SElliott Hughes #if defined( _MSC_VER )
2672*7485b225SElliott Hughes __int64 start, end, freq;
2673*7485b225SElliott Hughes QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
2674*7485b225SElliott Hughes #endif
2675*7485b225SElliott Hughes
2676*7485b225SElliott Hughes FILE* perfFP = fopen("resources/dream.xml", "r");
2677*7485b225SElliott Hughes XMLTest("Open dream.xml", true, perfFP != 0);
2678*7485b225SElliott Hughes fseek(perfFP, 0, SEEK_END);
2679*7485b225SElliott Hughes long size = ftell(perfFP);
2680*7485b225SElliott Hughes fseek(perfFP, 0, SEEK_SET);
2681*7485b225SElliott Hughes
2682*7485b225SElliott Hughes char* mem = new char[size + 1];
2683*7485b225SElliott Hughes memset(mem, 0xfe, size);
2684*7485b225SElliott Hughes size_t bytesRead = fread(mem, 1, size, perfFP);
2685*7485b225SElliott Hughes XMLTest("Read dream.xml", true, uint32_t(size) >= uint32_t(bytesRead));
2686*7485b225SElliott Hughes fclose(perfFP);
2687*7485b225SElliott Hughes mem[size] = 0;
2688*7485b225SElliott Hughes
2689*7485b225SElliott Hughes #if defined( _MSC_VER )
2690*7485b225SElliott Hughes QueryPerformanceCounter((LARGE_INTEGER*)&start);
2691*7485b225SElliott Hughes #else
2692*7485b225SElliott Hughes clock_t cstart = clock();
2693*7485b225SElliott Hughes #endif
2694*7485b225SElliott Hughes bool parseDreamXmlFailed = false;
2695*7485b225SElliott Hughes static const int COUNT = 10;
2696*7485b225SElliott Hughes for (int i = 0; i < COUNT; ++i) {
2697*7485b225SElliott Hughes XMLDocument doc;
2698*7485b225SElliott Hughes doc.Parse(mem);
2699*7485b225SElliott Hughes parseDreamXmlFailed = parseDreamXmlFailed || doc.Error();
2700*7485b225SElliott Hughes }
2701*7485b225SElliott Hughes #if defined( _MSC_VER )
2702*7485b225SElliott Hughes QueryPerformanceCounter((LARGE_INTEGER*)&end);
2703*7485b225SElliott Hughes #else
2704*7485b225SElliott Hughes clock_t cend = clock();
2705*7485b225SElliott Hughes #endif
2706*7485b225SElliott Hughes XMLTest( "Parse dream.xml", false, parseDreamXmlFailed );
2707*7485b225SElliott Hughes
2708*7485b225SElliott Hughes delete[] mem;
2709*7485b225SElliott Hughes
2710*7485b225SElliott Hughes static const char* note =
2711*7485b225SElliott Hughes #ifdef TINYXML2_DEBUG
2712*7485b225SElliott Hughes "DEBUG";
2713*7485b225SElliott Hughes #else
2714*7485b225SElliott Hughes "Release";
2715*7485b225SElliott Hughes #endif
2716*7485b225SElliott Hughes
2717*7485b225SElliott Hughes #if defined( _MSC_VER )
2718*7485b225SElliott Hughes const double duration = 1000.0 * (double)(end - start) / ((double)freq * (double)COUNT);
2719*7485b225SElliott Hughes #else
2720*7485b225SElliott Hughes const double duration = (double)(cend - cstart) / (double)COUNT;
2721*7485b225SElliott Hughes #endif
2722*7485b225SElliott Hughes printf("\nParsing dream.xml (%s): %.3f milli-seconds\n", note, duration);
2723*7485b225SElliott Hughes }
2724*7485b225SElliott Hughes
2725*7485b225SElliott Hughes #if defined( _MSC_VER ) && defined( TINYXML2_DEBUG )
2726*7485b225SElliott Hughes {
2727*7485b225SElliott Hughes _CrtMemCheckpoint( &endMemState );
2728*7485b225SElliott Hughes
2729*7485b225SElliott Hughes _CrtMemState diffMemState;
2730*7485b225SElliott Hughes _CrtMemDifference( &diffMemState, &startMemState, &endMemState );
2731*7485b225SElliott Hughes _CrtMemDumpStatistics( &diffMemState );
2732*7485b225SElliott Hughes
2733*7485b225SElliott Hughes {
2734*7485b225SElliott Hughes int leaksBeforeExit = _CrtDumpMemoryLeaks();
2735*7485b225SElliott Hughes XMLTest( "No leaks before exit?", FALSE, leaksBeforeExit );
2736*7485b225SElliott Hughes }
2737*7485b225SElliott Hughes }
2738*7485b225SElliott Hughes #endif
2739*7485b225SElliott Hughes
2740*7485b225SElliott Hughes printf ("\nPass %d, Fail %d\n", gPass, gFail);
2741*7485b225SElliott Hughes
2742*7485b225SElliott Hughes return gFail;
2743*7485b225SElliott Hughes }
2744