blob: a7194e0f955e0d54686822ec29378fb8a179fa69 [file] [log] [blame]
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001#include "tinyxml2.h"
2
3#include <stdio.h>
4#include <stdlib.h>
Lee Thomasonec5a7b42012-02-13 18:16:52 -08005#include <string.h>
U-Lama\Leee13c3e62011-12-28 14:36:55 -08006
Lee Thomason1ff38e02012-02-14 18:18:16 -08007#if defined( WIN32 )
8 #include <crtdbg.h>
9 _CrtMemState startMemState;
10 _CrtMemState endMemState;
11#endif
Lee Thomasone9ecdab2012-02-13 18:11:20 -080012
U-Lama\Leee13c3e62011-12-28 14:36:55 -080013using namespace tinyxml2;
Lee Thomasonec5a7b42012-02-13 18:16:52 -080014int gPass = 0;
15int gFail = 0;
16
U-Stream\Lee09a11c52012-02-17 08:31:16 -080017bool XMLTest (const char* testString, const char* expected, const char* found, bool echo=true )
Lee Thomason1ff38e02012-02-14 18:18:16 -080018{
19 bool pass = !strcmp( expected, found );
20 if ( pass )
21 printf ("[pass]");
22 else
23 printf ("[fail]");
24
U-Stream\Lee09a11c52012-02-17 08:31:16 -080025 if ( !echo )
Lee Thomason1ff38e02012-02-14 18:18:16 -080026 printf (" %s\n", testString);
27 else
28 printf (" %s [%s][%s]\n", testString, expected, found);
29
30 if ( pass )
31 ++gPass;
32 else
33 ++gFail;
34 return pass;
35}
36
37
U-Stream\Lee09a11c52012-02-17 08:31:16 -080038bool XMLTest( const char* testString, int expected, int found, bool echo=true )
Lee Thomason1ff38e02012-02-14 18:18:16 -080039{
40 bool pass = ( expected == found );
41 if ( pass )
42 printf ("[pass]");
43 else
44 printf ("[fail]");
45
U-Stream\Lee09a11c52012-02-17 08:31:16 -080046 if ( !echo )
Lee Thomason1ff38e02012-02-14 18:18:16 -080047 printf (" %s\n", testString);
48 else
49 printf (" %s [%d][%d]\n", testString, expected, found);
50
51 if ( pass )
52 ++gPass;
53 else
54 ++gFail;
55 return pass;
56}
Lee Thomasonec5a7b42012-02-13 18:16:52 -080057
U-Lama\Leee13c3e62011-12-28 14:36:55 -080058
59int main( int argc, const char* argv )
60{
Lee Thomason1ff38e02012-02-14 18:18:16 -080061 #if defined( WIN32 )
62 _CrtMemCheckpoint( &startMemState );
63 #endif
Lee Thomasone9ecdab2012-02-13 18:11:20 -080064
65#if 0
U-Lama\Lee4cee6112011-12-31 14:58:18 -080066 {
Lee Thomasonfde6a752012-01-14 18:08:12 -080067 static const char* test = "<!--hello world\n"
Lee Thomason8a5dfee2012-01-18 17:43:40 -080068 " line 2\r"
69 " line 3\r\n"
70 " line 4\n\r"
71 " line 5\r-->";
72
73 XMLDocument doc;
74 doc.Parse( test );
Lee Thomason50adb4c2012-02-13 15:07:09 -080075 doc.Print();
Lee Thomason8a5dfee2012-01-18 17:43:40 -080076 }
77#endif
Lee Thomason50adb4c2012-02-13 15:07:09 -080078#if 0
Lee Thomason8a5dfee2012-01-18 17:43:40 -080079 {
Lee Thomason43f59302012-02-06 18:18:11 -080080 static const char* test[] = { "<element />",
81 "<element></element>",
82 "<element><subelement/></element>",
83 "<element><subelement></subelement></element>",
84 "<element><subelement><subsub/></subelement></element>",
85 "<!--comment beside elements--><element><subelement></subelement></element>",
86 "<!--comment beside elements, this time with spaces--> \n <element> <subelement> \n </subelement> </element>",
87 "<element attrib1='foo' attrib2=\"bar\" ></element>",
88 "<element attrib1='foo' attrib2=\"bar\" ><subelement attrib3='yeehaa' /></element>",
89 "<element>Text inside element.</element>",
90 "<element><b></b></element>",
91 "<element>Text inside and <b>bolded</b> in the element.</element>",
92 "<outer><element>Text inside and <b>bolded</b> in the element.</element></outer>",
Lee Thomason8ee79892012-01-25 17:44:30 -080093 "<element>This &amp; That.</element>",
Lee Thomason18d68bd2012-01-26 18:17:26 -080094 "<element attrib='This&lt;That' />",
Lee Thomasondadcdfa2012-01-18 17:55:48 -080095 0
96 };
Lee Thomason6ee99fc2012-01-21 18:45:16 -080097 for( int i=0; test[i]; ++i ) {
Lee Thomasondadcdfa2012-01-18 17:55:48 -080098 XMLDocument doc;
Lee Thomason6ee99fc2012-01-21 18:45:16 -080099 doc.Parse( test[i] );
Lee Thomason5cae8972012-01-24 18:03:07 -0800100 doc.Print();
Lee Thomasonec975ce2012-01-23 11:42:06 -0800101 printf( "----------------------------------------------\n" );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800102 }
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800103 }
Lee Thomason50adb4c2012-02-13 15:07:09 -0800104#endif
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800105#if 0
Lee Thomason2c85a712012-01-31 08:24:24 -0800106 {
107 static const char* test = "<element>Text before.</element>";
108 XMLDocument doc;
109 doc.Parse( test );
110 XMLElement* root = doc.FirstChildElement();
111 XMLElement* newElement = doc.NewElement( "Subelement" );
112 root->InsertEndChild( newElement );
113 doc.Print();
114 }
Lee Thomasond1983222012-02-06 08:41:24 -0800115 {
116 XMLDocument* doc = new XMLDocument();
117 static const char* test = "<element><sub/></element>";
118 doc->Parse( test );
119 delete doc;
120 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800121#endif
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800122 {
Lee Thomason1ff38e02012-02-14 18:18:16 -0800123 // Test: Programmatic DOM
Lee Thomasonec5a7b42012-02-13 18:16:52 -0800124 // Build:
125 // <element>
126 // <!--comment-->
127 // <sub attrib="1" />
128 // <sub attrib="2" />
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800129 // <sub attrib="3" >& Text!</sub>
Lee Thomasonec5a7b42012-02-13 18:16:52 -0800130 // <element>
131
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800132 XMLDocument* doc = new XMLDocument();
Lee Thomason1ff38e02012-02-14 18:18:16 -0800133 XMLNode* element = doc->InsertEndChild( doc->NewElement( "element" ) );
134
135 XMLElement* sub[3] = { doc->NewElement( "sub" ), doc->NewElement( "sub" ), doc->NewElement( "sub" ) };
136 for( int i=0; i<3; ++i ) {
137 sub[i]->SetAttribute( "attrib", i );
138 }
139 element->InsertEndChild( sub[2] );
140 XMLNode* comment = element->InsertFirstChild( doc->NewComment( "comment" ) );
141 element->InsertAfterChild( comment, sub[0] );
142 element->InsertAfterChild( sub[0], sub[1] );
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800143 sub[2]->InsertFirstChild( doc->NewText( "& Text!" ));
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800144 doc->Print();
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800145 XMLTest( "Programmatic DOM", "comment", doc->FirstChildElement( "element" )->FirstChild()->Value() );
146 XMLTest( "Programmatic DOM", "0", doc->FirstChildElement( "element" )->FirstChildElement()->Attribute( "attrib" ) );
147 XMLTest( "Programmatic DOM", 2, doc->FirstChildElement()->LastChildElement( "sub" )->IntAttribute( "attrib" ) );
148 XMLTest( "Programmatic DOM", "& Text!",
149 doc->FirstChildElement()->LastChildElement( "sub" )->FirstChild()->ToText()->Value() );
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800150 delete doc;
151 }
Lee Thomasonec5a7b42012-02-13 18:16:52 -0800152
Lee Thomason1ff38e02012-02-14 18:18:16 -0800153 #if defined( WIN32 )
154 _CrtMemCheckpoint( &endMemState );
155 //_CrtMemDumpStatistics( &endMemState );
156
157 _CrtMemState diffMemState;
158 _CrtMemDifference( &diffMemState, &startMemState, &endMemState );
159 _CrtMemDumpStatistics( &diffMemState );
160 #endif
161
162 printf ("\nPass %d, Fail %d\n", gPass, gFail);
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800163 return 0;
164}