1 | /* |
---|
2 | Test program for TinyXML. |
---|
3 | */ |
---|
4 | |
---|
5 | |
---|
6 | #ifdef TIXML_USE_STL |
---|
7 | #include <iostream> |
---|
8 | #include <sstream> |
---|
9 | using namespace std; |
---|
10 | #else |
---|
11 | #include <stdio.h> |
---|
12 | #endif |
---|
13 | |
---|
14 | #if defined( WIN32 ) && defined( TUNE ) |
---|
15 | #include <crtdbg.h> |
---|
16 | _CrtMemState startMemState; |
---|
17 | _CrtMemState endMemState; |
---|
18 | #endif |
---|
19 | |
---|
20 | #include "tinyxml.h" |
---|
21 | |
---|
22 | static int gPass = 0; |
---|
23 | static int gFail = 0; |
---|
24 | |
---|
25 | |
---|
26 | bool XmlTest (const char* testString, const char* expected, const char* found, bool noEcho = false) |
---|
27 | { |
---|
28 | bool pass = !strcmp( expected, found ); |
---|
29 | if ( pass ) |
---|
30 | printf ("[pass]"); |
---|
31 | else |
---|
32 | printf ("[fail]"); |
---|
33 | |
---|
34 | if ( noEcho ) |
---|
35 | printf (" %s\n", testString); |
---|
36 | else |
---|
37 | printf (" %s [%s][%s]\n", testString, expected, found); |
---|
38 | |
---|
39 | if ( pass ) |
---|
40 | ++gPass; |
---|
41 | else |
---|
42 | ++gFail; |
---|
43 | return pass; |
---|
44 | } |
---|
45 | |
---|
46 | |
---|
47 | bool XmlTest( const char* testString, int expected, int found, bool noEcho = false ) |
---|
48 | { |
---|
49 | bool pass = ( expected == found ); |
---|
50 | if ( pass ) |
---|
51 | printf ("[pass]"); |
---|
52 | else |
---|
53 | printf ("[fail]"); |
---|
54 | |
---|
55 | if ( noEcho ) |
---|
56 | printf (" %s\n", testString); |
---|
57 | else |
---|
58 | printf (" %s [%d][%d]\n", testString, expected, found); |
---|
59 | |
---|
60 | if ( pass ) |
---|
61 | ++gPass; |
---|
62 | else |
---|
63 | ++gFail; |
---|
64 | return pass; |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | // |
---|
69 | // This file demonstrates some basic functionality of TinyXml. |
---|
70 | // Note that the example is very contrived. It presumes you know |
---|
71 | // what is in the XML file. But it does test the basic operations, |
---|
72 | // and show how to add and remove nodes. |
---|
73 | // |
---|
74 | |
---|
75 | int main() |
---|
76 | { |
---|
77 | // |
---|
78 | // We start with the 'demoStart' todo list. Process it. And |
---|
79 | // should hopefully end up with the todo list as illustrated. |
---|
80 | // |
---|
81 | const char* demoStart = |
---|
82 | "<?xml version=\"1.0\" standalone='no' >\n" |
---|
83 | "<!-- Our to do list data -->" |
---|
84 | "<ToDo>\n" |
---|
85 | "<!-- Do I need a secure PDA? -->\n" |
---|
86 | "<Item priority=\"1\" distance='close'> Go to the <bold>Toy store!</bold></Item>" |
---|
87 | "<Item priority=\"2\" distance='none'> Do bills </Item>" |
---|
88 | "<Item priority=\"2\" distance='far & back'> Look for Evil Dinosaurs! </Item>" |
---|
89 | "</ToDo>"; |
---|
90 | |
---|
91 | { |
---|
92 | |
---|
93 | #ifdef TIXML_USE_STL |
---|
94 | /* What the todo list should look like after processing. |
---|
95 | In stream (no formatting) representation. */ |
---|
96 | const char* demoEnd = |
---|
97 | "<?xml version=\"1.0\" standalone=\"no\" ?>" |
---|
98 | "<!-- Our to do list data -->" |
---|
99 | "<ToDo>" |
---|
100 | "<!-- Do I need a secure PDA? -->" |
---|
101 | "<Item priority=\"2\" distance=\"close\">Go to the" |
---|
102 | "<bold>Toy store!" |
---|
103 | "</bold>" |
---|
104 | "</Item>" |
---|
105 | "<Item priority=\"1\" distance=\"far\">Talk to:" |
---|
106 | "<Meeting where=\"School\">" |
---|
107 | "<Attendee name=\"Marple\" position=\"teacher\" />" |
---|
108 | "<Attendee name=\"Voel\" position=\"counselor\" />" |
---|
109 | "</Meeting>" |
---|
110 | "<Meeting where=\"Lunch\" />" |
---|
111 | "</Item>" |
---|
112 | "<Item priority=\"2\" distance=\"here\">Do bills" |
---|
113 | "</Item>" |
---|
114 | "</ToDo>"; |
---|
115 | #endif |
---|
116 | |
---|
117 | // The example parses from the character string (above): |
---|
118 | #if defined( WIN32 ) && defined( TUNE ) |
---|
119 | _CrtMemCheckpoint( &startMemState ); |
---|
120 | #endif |
---|
121 | |
---|
122 | { |
---|
123 | // Write to a file and read it back, to check file I/O. |
---|
124 | |
---|
125 | TiXmlDocument doc( "demotest.xml" ); |
---|
126 | doc.Parse( demoStart ); |
---|
127 | |
---|
128 | if ( doc.Error() ) |
---|
129 | { |
---|
130 | printf( "Error in %s: %s\n", doc.Value(), doc.ErrorDesc() ); |
---|
131 | exit( 1 ); |
---|
132 | } |
---|
133 | doc.SaveFile(); |
---|
134 | } |
---|
135 | |
---|
136 | TiXmlDocument doc( "demotest.xml" ); |
---|
137 | bool loadOkay = doc.LoadFile(); |
---|
138 | |
---|
139 | if ( !loadOkay ) |
---|
140 | { |
---|
141 | printf( "Could not load test file 'demotest.xml'. Error='%s'. Exiting.\n", doc.ErrorDesc() ); |
---|
142 | exit( 1 ); |
---|
143 | } |
---|
144 | |
---|
145 | printf( "** Demo doc read from disk: ** \n\n" ); |
---|
146 | printf( "** Printing via doc.Print **\n" ); |
---|
147 | doc.Print( stdout ); |
---|
148 | |
---|
149 | { |
---|
150 | printf( "** Printing via TiXmlPrinter **\n" ); |
---|
151 | TiXmlPrinter printer; |
---|
152 | doc.Accept( &printer ); |
---|
153 | fprintf( stdout, "%s", printer.CStr() ); |
---|
154 | } |
---|
155 | #ifdef TIXML_USE_STL |
---|
156 | { |
---|
157 | printf( "** Printing via operator<< **\n" ); |
---|
158 | std::cout << doc; |
---|
159 | } |
---|
160 | #endif |
---|
161 | TiXmlNode* node = 0; |
---|
162 | TiXmlElement* todoElement = 0; |
---|
163 | TiXmlElement* itemElement = 0; |
---|
164 | |
---|
165 | |
---|
166 | // -------------------------------------------------------- |
---|
167 | // An example of changing existing attributes, and removing |
---|
168 | // an element from the document. |
---|
169 | // -------------------------------------------------------- |
---|
170 | |
---|
171 | // Get the "ToDo" element. |
---|
172 | // It is a child of the document, and can be selected by name. |
---|
173 | node = doc.FirstChild( "ToDo" ); |
---|
174 | assert( node ); |
---|
175 | todoElement = node->ToElement(); |
---|
176 | assert( todoElement ); |
---|
177 | |
---|
178 | // Going to the toy store is now our second priority... |
---|
179 | // So set the "priority" attribute of the first item in the list. |
---|
180 | node = todoElement->FirstChildElement(); // This skips the "PDA" comment. |
---|
181 | assert( node ); |
---|
182 | itemElement = node->ToElement(); |
---|
183 | assert( itemElement ); |
---|
184 | itemElement->SetAttribute( "priority", 2 ); |
---|
185 | |
---|
186 | // Change the distance to "doing bills" from |
---|
187 | // "none" to "here". It's the next sibling element. |
---|
188 | itemElement = itemElement->NextSiblingElement(); |
---|
189 | assert( itemElement ); |
---|
190 | itemElement->SetAttribute( "distance", "here" ); |
---|
191 | |
---|
192 | // Remove the "Look for Evil Dinosaurs!" item. |
---|
193 | // It is 1 more sibling away. We ask the parent to remove |
---|
194 | // a particular child. |
---|
195 | itemElement = itemElement->NextSiblingElement(); |
---|
196 | todoElement->RemoveChild( itemElement ); |
---|
197 | |
---|
198 | itemElement = 0; |
---|
199 | |
---|
200 | // -------------------------------------------------------- |
---|
201 | // What follows is an example of created elements and text |
---|
202 | // nodes and adding them to the document. |
---|
203 | // -------------------------------------------------------- |
---|
204 | |
---|
205 | // Add some meetings. |
---|
206 | TiXmlElement item( "Item" ); |
---|
207 | item.SetAttribute( "priority", "1" ); |
---|
208 | item.SetAttribute( "distance", "far" ); |
---|
209 | |
---|
210 | TiXmlText text( "Talk to:" ); |
---|
211 | |
---|
212 | TiXmlElement meeting1( "Meeting" ); |
---|
213 | meeting1.SetAttribute( "where", "School" ); |
---|
214 | |
---|
215 | TiXmlElement meeting2( "Meeting" ); |
---|
216 | meeting2.SetAttribute( "where", "Lunch" ); |
---|
217 | |
---|
218 | TiXmlElement attendee1( "Attendee" ); |
---|
219 | attendee1.SetAttribute( "name", "Marple" ); |
---|
220 | attendee1.SetAttribute( "position", "teacher" ); |
---|
221 | |
---|
222 | TiXmlElement attendee2( "Attendee" ); |
---|
223 | attendee2.SetAttribute( "name", "Voel" ); |
---|
224 | attendee2.SetAttribute( "position", "counselor" ); |
---|
225 | |
---|
226 | // Assemble the nodes we've created: |
---|
227 | meeting1.InsertEndChild( attendee1 ); |
---|
228 | meeting1.InsertEndChild( attendee2 ); |
---|
229 | |
---|
230 | item.InsertEndChild( text ); |
---|
231 | item.InsertEndChild( meeting1 ); |
---|
232 | item.InsertEndChild( meeting2 ); |
---|
233 | |
---|
234 | // And add the node to the existing list after the first child. |
---|
235 | node = todoElement->FirstChild( "Item" ); |
---|
236 | assert( node ); |
---|
237 | itemElement = node->ToElement(); |
---|
238 | assert( itemElement ); |
---|
239 | |
---|
240 | todoElement->InsertAfterChild( itemElement, item ); |
---|
241 | |
---|
242 | printf( "\n** Demo doc processed: ** \n\n" ); |
---|
243 | doc.Print( stdout ); |
---|
244 | |
---|
245 | |
---|
246 | #ifdef TIXML_USE_STL |
---|
247 | printf( "** Demo doc processed to stream: ** \n\n" ); |
---|
248 | cout << doc << endl << endl; |
---|
249 | #endif |
---|
250 | |
---|
251 | // -------------------------------------------------------- |
---|
252 | // Different tests...do we have what we expect? |
---|
253 | // -------------------------------------------------------- |
---|
254 | |
---|
255 | int count = 0; |
---|
256 | TiXmlElement* element; |
---|
257 | |
---|
258 | ////////////////////////////////////////////////////// |
---|
259 | |
---|
260 | #ifdef TIXML_USE_STL |
---|
261 | cout << "** Basic structure. **\n"; |
---|
262 | ostringstream outputStream( ostringstream::out ); |
---|
263 | outputStream << doc; |
---|
264 | XmlTest( "Output stream correct.", string( demoEnd ).c_str(), |
---|
265 | outputStream.str().c_str(), true ); |
---|
266 | #endif |
---|
267 | |
---|
268 | node = doc.RootElement(); |
---|
269 | XmlTest( "Root element exists.", true, ( node != 0 && node->ToElement() ) ); |
---|
270 | XmlTest ( "Root element value is 'ToDo'.", "ToDo", node->Value()); |
---|
271 | |
---|
272 | node = node->FirstChild(); |
---|
273 | XmlTest( "First child exists & is a comment.", true, ( node != 0 && node->ToComment() ) ); |
---|
274 | node = node->NextSibling(); |
---|
275 | XmlTest( "Sibling element exists & is an element.", true, ( node != 0 && node->ToElement() ) ); |
---|
276 | XmlTest ( "Value is 'Item'.", "Item", node->Value() ); |
---|
277 | |
---|
278 | node = node->FirstChild(); |
---|
279 | XmlTest ( "First child exists.", true, ( node != 0 && node->ToText() ) ); |
---|
280 | XmlTest ( "Value is 'Go to the'.", "Go to the", node->Value() ); |
---|
281 | |
---|
282 | |
---|
283 | ////////////////////////////////////////////////////// |
---|
284 | printf ("\n** Iterators. **\n"); |
---|
285 | |
---|
286 | // Walk all the top level nodes of the document. |
---|
287 | count = 0; |
---|
288 | for( node = doc.FirstChild(); |
---|
289 | node; |
---|
290 | node = node->NextSibling() ) |
---|
291 | { |
---|
292 | count++; |
---|
293 | } |
---|
294 | XmlTest( "Top level nodes, using First / Next.", 3, count ); |
---|
295 | |
---|
296 | count = 0; |
---|
297 | for( node = doc.LastChild(); |
---|
298 | node; |
---|
299 | node = node->PreviousSibling() ) |
---|
300 | { |
---|
301 | count++; |
---|
302 | } |
---|
303 | XmlTest( "Top level nodes, using Last / Previous.", 3, count ); |
---|
304 | |
---|
305 | // Walk all the top level nodes of the document, |
---|
306 | // using a different syntax. |
---|
307 | count = 0; |
---|
308 | for( node = doc.IterateChildren( 0 ); |
---|
309 | node; |
---|
310 | node = doc.IterateChildren( node ) ) |
---|
311 | { |
---|
312 | count++; |
---|
313 | } |
---|
314 | XmlTest( "Top level nodes, using IterateChildren.", 3, count ); |
---|
315 | |
---|
316 | // Walk all the elements in a node. |
---|
317 | count = 0; |
---|
318 | for( element = todoElement->FirstChildElement(); |
---|
319 | element; |
---|
320 | element = element->NextSiblingElement() ) |
---|
321 | { |
---|
322 | count++; |
---|
323 | } |
---|
324 | XmlTest( "Children of the 'ToDo' element, using First / Next.", |
---|
325 | 3, count ); |
---|
326 | |
---|
327 | // Walk all the elements in a node by value. |
---|
328 | count = 0; |
---|
329 | for( node = todoElement->FirstChild( "Item" ); |
---|
330 | node; |
---|
331 | node = node->NextSibling( "Item" ) ) |
---|
332 | { |
---|
333 | count++; |
---|
334 | } |
---|
335 | XmlTest( "'Item' children of the 'ToDo' element, using First/Next.", 3, count ); |
---|
336 | |
---|
337 | count = 0; |
---|
338 | for( node = todoElement->LastChild( "Item" ); |
---|
339 | node; |
---|
340 | node = node->PreviousSibling( "Item" ) ) |
---|
341 | { |
---|
342 | count++; |
---|
343 | } |
---|
344 | XmlTest( "'Item' children of the 'ToDo' element, using Last/Previous.", 3, count ); |
---|
345 | |
---|
346 | #ifdef TIXML_USE_STL |
---|
347 | { |
---|
348 | cout << "\n** Parsing. **\n"; |
---|
349 | istringstream parse0( "<Element0 attribute0='foo0' attribute1= noquotes attribute2 = '>' />" ); |
---|
350 | TiXmlElement element0( "default" ); |
---|
351 | parse0 >> element0; |
---|
352 | |
---|
353 | XmlTest ( "Element parsed, value is 'Element0'.", "Element0", element0.Value() ); |
---|
354 | XmlTest ( "Reads attribute 'attribute0=\"foo0\"'.", "foo0", element0.Attribute( "attribute0" )); |
---|
355 | XmlTest ( "Reads incorrectly formatted 'attribute1=noquotes'.", "noquotes", element0.Attribute( "attribute1" ) ); |
---|
356 | XmlTest ( "Read attribute with entity value '>'.", ">", element0.Attribute( "attribute2" ) ); |
---|
357 | } |
---|
358 | #endif |
---|
359 | |
---|
360 | { |
---|
361 | const char* error = "<?xml version=\"1.0\" standalone=\"no\" ?>\n" |
---|
362 | "<passages count=\"006\" formatversion=\"20020620\">\n" |
---|
363 | " <wrong error>\n" |
---|
364 | "</passages>"; |
---|
365 | |
---|
366 | TiXmlDocument docTest; |
---|
367 | docTest.Parse( error ); |
---|
368 | XmlTest( "Error row", docTest.ErrorRow(), 3 ); |
---|
369 | XmlTest( "Error column", docTest.ErrorCol(), 17 ); |
---|
370 | //printf( "error=%d id='%s' row %d col%d\n", (int) doc.Error(), doc.ErrorDesc(), doc.ErrorRow()+1, doc.ErrorCol() + 1 ); |
---|
371 | |
---|
372 | } |
---|
373 | |
---|
374 | #ifdef TIXML_USE_STL |
---|
375 | { |
---|
376 | ////////////////////////////////////////////////////// |
---|
377 | cout << "\n** Streaming. **\n"; |
---|
378 | |
---|
379 | // Round trip check: stream in, then stream back out to verify. The stream |
---|
380 | // out has already been checked, above. We use the output |
---|
381 | |
---|
382 | istringstream inputStringStream( outputStream.str() ); |
---|
383 | TiXmlDocument document0; |
---|
384 | |
---|
385 | inputStringStream >> document0; |
---|
386 | |
---|
387 | ostringstream outputStream0( ostringstream::out ); |
---|
388 | outputStream0 << document0; |
---|
389 | |
---|
390 | XmlTest( "Stream round trip correct.", string( demoEnd ).c_str(), |
---|
391 | outputStream0.str().c_str(), true ); |
---|
392 | |
---|
393 | std::string str; |
---|
394 | str << document0; |
---|
395 | |
---|
396 | XmlTest( "String printing correct.", string( demoEnd ).c_str(), |
---|
397 | str.c_str(), true ); |
---|
398 | } |
---|
399 | #endif |
---|
400 | } |
---|
401 | |
---|
402 | { |
---|
403 | const char* str = "<doc attr0='1' attr1='2.0' attr2='foo' />"; |
---|
404 | |
---|
405 | TiXmlDocument doc; |
---|
406 | doc.Parse( str ); |
---|
407 | |
---|
408 | TiXmlElement* ele = doc.FirstChildElement(); |
---|
409 | |
---|
410 | int iVal, result; |
---|
411 | double dVal; |
---|
412 | |
---|
413 | result = ele->QueryDoubleAttribute( "attr0", &dVal ); |
---|
414 | XmlTest( "Query attribute: int as double", result, TIXML_SUCCESS ); |
---|
415 | XmlTest( "Query attribute: int as double", (int)dVal, 1 ); |
---|
416 | result = ele->QueryDoubleAttribute( "attr1", &dVal ); |
---|
417 | XmlTest( "Query attribute: double as double", (int)dVal, 2 ); |
---|
418 | result = ele->QueryIntAttribute( "attr1", &iVal ); |
---|
419 | XmlTest( "Query attribute: double as int", result, TIXML_SUCCESS ); |
---|
420 | XmlTest( "Query attribute: double as int", iVal, 2 ); |
---|
421 | result = ele->QueryIntAttribute( "attr2", &iVal ); |
---|
422 | XmlTest( "Query attribute: not a number", result, TIXML_WRONG_TYPE ); |
---|
423 | result = ele->QueryIntAttribute( "bar", &iVal ); |
---|
424 | XmlTest( "Query attribute: does not exist", result, TIXML_NO_ATTRIBUTE ); |
---|
425 | } |
---|
426 | |
---|
427 | { |
---|
428 | const char* str = "\t<?xml version=\"1.0\" standalone=\"no\" ?>\t<room doors='2'>\n" |
---|
429 | "</room>"; |
---|
430 | |
---|
431 | TiXmlDocument doc; |
---|
432 | doc.SetTabSize( 8 ); |
---|
433 | doc.Parse( str ); |
---|
434 | |
---|
435 | TiXmlHandle docHandle( &doc ); |
---|
436 | TiXmlHandle roomHandle = docHandle.FirstChildElement( "room" ); |
---|
437 | |
---|
438 | assert( docHandle.Node() ); |
---|
439 | assert( roomHandle.Element() ); |
---|
440 | |
---|
441 | TiXmlElement* room = roomHandle.Element(); |
---|
442 | assert( room ); |
---|
443 | TiXmlAttribute* doors = room->FirstAttribute(); |
---|
444 | assert( doors ); |
---|
445 | |
---|
446 | XmlTest( "Location tracking: Tab 8: room row", room->Row(), 1 ); |
---|
447 | XmlTest( "Location tracking: Tab 8: room col", room->Column(), 49 ); |
---|
448 | XmlTest( "Location tracking: Tab 8: doors row", doors->Row(), 1 ); |
---|
449 | XmlTest( "Location tracking: Tab 8: doors col", doors->Column(), 55 ); |
---|
450 | } |
---|
451 | |
---|
452 | { |
---|
453 | const char* str = "\t<?xml version=\"1.0\" standalone=\"no\" ?>\t<room doors='2'>\n" |
---|
454 | " <!-- Silly example -->\n" |
---|
455 | " <door wall='north'>A great door!</door>\n" |
---|
456 | "\t<door wall='east'/>" |
---|
457 | "</room>"; |
---|
458 | |
---|
459 | TiXmlDocument doc; |
---|
460 | doc.Parse( str ); |
---|
461 | |
---|
462 | TiXmlHandle docHandle( &doc ); |
---|
463 | TiXmlHandle roomHandle = docHandle.FirstChildElement( "room" ); |
---|
464 | TiXmlHandle commentHandle = docHandle.FirstChildElement( "room" ).FirstChild(); |
---|
465 | TiXmlHandle textHandle = docHandle.FirstChildElement( "room" ).ChildElement( "door", 0 ).FirstChild(); |
---|
466 | TiXmlHandle door0Handle = docHandle.FirstChildElement( "room" ).ChildElement( 0 ); |
---|
467 | TiXmlHandle door1Handle = docHandle.FirstChildElement( "room" ).ChildElement( 1 ); |
---|
468 | |
---|
469 | assert( docHandle.Node() ); |
---|
470 | assert( roomHandle.Element() ); |
---|
471 | assert( commentHandle.Node() ); |
---|
472 | assert( textHandle.Text() ); |
---|
473 | assert( door0Handle.Element() ); |
---|
474 | assert( door1Handle.Element() ); |
---|
475 | |
---|
476 | TiXmlDeclaration* declaration = doc.FirstChild()->ToDeclaration(); |
---|
477 | assert( declaration ); |
---|
478 | TiXmlElement* room = roomHandle.Element(); |
---|
479 | assert( room ); |
---|
480 | TiXmlAttribute* doors = room->FirstAttribute(); |
---|
481 | assert( doors ); |
---|
482 | TiXmlText* text = textHandle.Text(); |
---|
483 | TiXmlComment* comment = commentHandle.Node()->ToComment(); |
---|
484 | assert( comment ); |
---|
485 | TiXmlElement* door0 = door0Handle.Element(); |
---|
486 | TiXmlElement* door1 = door1Handle.Element(); |
---|
487 | |
---|
488 | XmlTest( "Location tracking: Declaration row", declaration->Row(), 1 ); |
---|
489 | XmlTest( "Location tracking: Declaration col", declaration->Column(), 5 ); |
---|
490 | XmlTest( "Location tracking: room row", room->Row(), 1 ); |
---|
491 | XmlTest( "Location tracking: room col", room->Column(), 45 ); |
---|
492 | XmlTest( "Location tracking: doors row", doors->Row(), 1 ); |
---|
493 | XmlTest( "Location tracking: doors col", doors->Column(), 51 ); |
---|
494 | XmlTest( "Location tracking: Comment row", comment->Row(), 2 ); |
---|
495 | XmlTest( "Location tracking: Comment col", comment->Column(), 3 ); |
---|
496 | XmlTest( "Location tracking: text row", text->Row(), 3 ); |
---|
497 | XmlTest( "Location tracking: text col", text->Column(), 24 ); |
---|
498 | XmlTest( "Location tracking: door0 row", door0->Row(), 3 ); |
---|
499 | XmlTest( "Location tracking: door0 col", door0->Column(), 5 ); |
---|
500 | XmlTest( "Location tracking: door1 row", door1->Row(), 4 ); |
---|
501 | XmlTest( "Location tracking: door1 col", door1->Column(), 5 ); |
---|
502 | } |
---|
503 | |
---|
504 | |
---|
505 | // -------------------------------------------------------- |
---|
506 | // UTF-8 testing. It is important to test: |
---|
507 | // 1. Making sure name, value, and text read correctly |
---|
508 | // 2. Row, Col functionality |
---|
509 | // 3. Correct output |
---|
510 | // -------------------------------------------------------- |
---|
511 | printf ("\n** UTF-8 **\n"); |
---|
512 | { |
---|
513 | TiXmlDocument doc( "utf8test.xml" ); |
---|
514 | doc.LoadFile(); |
---|
515 | if ( doc.Error() && doc.ErrorId() == TiXmlBase::TIXML_ERROR_OPENING_FILE ) { |
---|
516 | printf( "WARNING: File 'utf8test.xml' not found.\n" |
---|
517 | "(Are you running the test from the wrong directory?)\n" |
---|
518 | "Could not test UTF-8 functionality.\n" ); |
---|
519 | } |
---|
520 | else |
---|
521 | { |
---|
522 | TiXmlHandle docH( &doc ); |
---|
523 | // Get the attribute "value" from the "Russian" element and check it. |
---|
524 | TiXmlElement* element = docH.FirstChildElement( "document" ).FirstChildElement( "Russian" ).Element(); |
---|
525 | const unsigned char correctValue[] = { 0xd1U, 0x86U, 0xd0U, 0xb5U, 0xd0U, 0xbdU, 0xd0U, 0xbdU, |
---|
526 | 0xd0U, 0xbeU, 0xd1U, 0x81U, 0xd1U, 0x82U, 0xd1U, 0x8cU, 0 }; |
---|
527 | |
---|
528 | XmlTest( "UTF-8: Russian value.", (const char*)correctValue, element->Attribute( "value" ), true ); |
---|
529 | XmlTest( "UTF-8: Russian value row.", 4, element->Row() ); |
---|
530 | XmlTest( "UTF-8: Russian value column.", 5, element->Column() ); |
---|
531 | |
---|
532 | const unsigned char russianElementName[] = { 0xd0U, 0xa0U, 0xd1U, 0x83U, |
---|
533 | 0xd1U, 0x81U, 0xd1U, 0x81U, |
---|
534 | 0xd0U, 0xbaU, 0xd0U, 0xb8U, |
---|
535 | 0xd0U, 0xb9U, 0 }; |
---|
536 | const char russianText[] = "<\xD0\xB8\xD0\xBC\xD0\xB5\xD0\xB5\xD1\x82>"; |
---|
537 | |
---|
538 | TiXmlText* text = docH.FirstChildElement( "document" ).FirstChildElement( (const char*) russianElementName ).Child( 0 ).Text(); |
---|
539 | XmlTest( "UTF-8: Browsing russian element name.", |
---|
540 | russianText, |
---|
541 | text->Value(), |
---|
542 | true ); |
---|
543 | XmlTest( "UTF-8: Russian element name row.", 7, text->Row() ); |
---|
544 | XmlTest( "UTF-8: Russian element name column.", 47, text->Column() ); |
---|
545 | |
---|
546 | TiXmlDeclaration* dec = docH.Child( 0 ).Node()->ToDeclaration(); |
---|
547 | XmlTest( "UTF-8: Declaration column.", 1, dec->Column() ); |
---|
548 | XmlTest( "UTF-8: Document column.", 1, doc.Column() ); |
---|
549 | |
---|
550 | // Now try for a round trip. |
---|
551 | doc.SaveFile( "utf8testout.xml" ); |
---|
552 | |
---|
553 | // Check the round trip. |
---|
554 | char savedBuf[256]; |
---|
555 | char verifyBuf[256]; |
---|
556 | int okay = 1; |
---|
557 | |
---|
558 | FILE* saved = fopen( "utf8testout.xml", "r" ); |
---|
559 | FILE* verify = fopen( "utf8testverify.xml", "r" ); |
---|
560 | if ( saved && verify ) |
---|
561 | { |
---|
562 | while ( fgets( verifyBuf, 256, verify ) ) |
---|
563 | { |
---|
564 | fgets( savedBuf, 256, saved ); |
---|
565 | if ( strcmp( verifyBuf, savedBuf ) ) |
---|
566 | { |
---|
567 | okay = 0; |
---|
568 | break; |
---|
569 | } |
---|
570 | } |
---|
571 | fclose( saved ); |
---|
572 | fclose( verify ); |
---|
573 | } |
---|
574 | XmlTest( "UTF-8: Verified multi-language round trip.", 1, okay ); |
---|
575 | |
---|
576 | // On most Western machines, this is an element that contains |
---|
577 | // the word "resume" with the correct accents, in a latin encoding. |
---|
578 | // It will be something else completely on non-wester machines, |
---|
579 | // which is why TinyXml is switching to UTF-8. |
---|
580 | const char latin[] = "<element>r\x82sum\x82</element>"; |
---|
581 | |
---|
582 | TiXmlDocument latinDoc; |
---|
583 | latinDoc.Parse( latin, 0, TIXML_ENCODING_LEGACY ); |
---|
584 | |
---|
585 | text = latinDoc.FirstChildElement()->FirstChild()->ToText(); |
---|
586 | XmlTest( "Legacy encoding: Verify text element.", "r\x82sum\x82", text->Value() ); |
---|
587 | } |
---|
588 | } |
---|
589 | |
---|
590 | ////////////////////// |
---|
591 | // Copy and assignment |
---|
592 | ////////////////////// |
---|
593 | printf ("\n** Copy and Assignment **\n"); |
---|
594 | { |
---|
595 | TiXmlElement element( "foo" ); |
---|
596 | element.Parse( "<element name='value' />", 0, TIXML_ENCODING_UNKNOWN ); |
---|
597 | |
---|
598 | TiXmlElement elementCopy( element ); |
---|
599 | TiXmlElement elementAssign( "foo" ); |
---|
600 | elementAssign.Parse( "<incorrect foo='bar'/>", 0, TIXML_ENCODING_UNKNOWN ); |
---|
601 | elementAssign = element; |
---|
602 | |
---|
603 | XmlTest( "Copy/Assign: element copy #1.", "element", elementCopy.Value() ); |
---|
604 | XmlTest( "Copy/Assign: element copy #2.", "value", elementCopy.Attribute( "name" ) ); |
---|
605 | XmlTest( "Copy/Assign: element assign #1.", "element", elementAssign.Value() ); |
---|
606 | XmlTest( "Copy/Assign: element assign #2.", "value", elementAssign.Attribute( "name" ) ); |
---|
607 | XmlTest( "Copy/Assign: element assign #3.", true, ( 0 == elementAssign.Attribute( "foo" )) ); |
---|
608 | |
---|
609 | TiXmlComment comment; |
---|
610 | comment.Parse( "<!--comment-->", 0, TIXML_ENCODING_UNKNOWN ); |
---|
611 | TiXmlComment commentCopy( comment ); |
---|
612 | TiXmlComment commentAssign; |
---|
613 | commentAssign = commentCopy; |
---|
614 | XmlTest( "Copy/Assign: comment copy.", "comment", commentCopy.Value() ); |
---|
615 | XmlTest( "Copy/Assign: comment assign.", "comment", commentAssign.Value() ); |
---|
616 | |
---|
617 | TiXmlUnknown unknown; |
---|
618 | unknown.Parse( "<[unknown]>", 0, TIXML_ENCODING_UNKNOWN ); |
---|
619 | TiXmlUnknown unknownCopy( unknown ); |
---|
620 | TiXmlUnknown unknownAssign; |
---|
621 | unknownAssign.Parse( "incorrect", 0, TIXML_ENCODING_UNKNOWN ); |
---|
622 | unknownAssign = unknownCopy; |
---|
623 | XmlTest( "Copy/Assign: unknown copy.", "[unknown]", unknownCopy.Value() ); |
---|
624 | XmlTest( "Copy/Assign: unknown assign.", "[unknown]", unknownAssign.Value() ); |
---|
625 | |
---|
626 | TiXmlText text( "TextNode" ); |
---|
627 | TiXmlText textCopy( text ); |
---|
628 | TiXmlText textAssign( "incorrect" ); |
---|
629 | textAssign = text; |
---|
630 | XmlTest( "Copy/Assign: text copy.", "TextNode", textCopy.Value() ); |
---|
631 | XmlTest( "Copy/Assign: text assign.", "TextNode", textAssign.Value() ); |
---|
632 | |
---|
633 | TiXmlDeclaration dec; |
---|
634 | dec.Parse( "<?xml version='1.0' encoding='UTF-8'?>", 0, TIXML_ENCODING_UNKNOWN ); |
---|
635 | TiXmlDeclaration decCopy( dec ); |
---|
636 | TiXmlDeclaration decAssign; |
---|
637 | decAssign = dec; |
---|
638 | |
---|
639 | XmlTest( "Copy/Assign: declaration copy.", "UTF-8", decCopy.Encoding() ); |
---|
640 | XmlTest( "Copy/Assign: text assign.", "UTF-8", decAssign.Encoding() ); |
---|
641 | |
---|
642 | TiXmlDocument doc; |
---|
643 | elementCopy.InsertEndChild( textCopy ); |
---|
644 | doc.InsertEndChild( decAssign ); |
---|
645 | doc.InsertEndChild( elementCopy ); |
---|
646 | doc.InsertEndChild( unknownAssign ); |
---|
647 | |
---|
648 | TiXmlDocument docCopy( doc ); |
---|
649 | TiXmlDocument docAssign; |
---|
650 | docAssign = docCopy; |
---|
651 | |
---|
652 | #ifdef TIXML_USE_STL |
---|
653 | std::string original, copy, assign; |
---|
654 | original << doc; |
---|
655 | copy << docCopy; |
---|
656 | assign << docAssign; |
---|
657 | XmlTest( "Copy/Assign: document copy.", original.c_str(), copy.c_str(), true ); |
---|
658 | XmlTest( "Copy/Assign: document assign.", original.c_str(), assign.c_str(), true ); |
---|
659 | |
---|
660 | #endif |
---|
661 | } |
---|
662 | |
---|
663 | ////////////////////////////////////////////////////// |
---|
664 | #ifdef TIXML_USE_STL |
---|
665 | printf ("\n** Parsing, no Condense Whitespace **\n"); |
---|
666 | TiXmlBase::SetCondenseWhiteSpace( false ); |
---|
667 | { |
---|
668 | istringstream parse1( "<start>This is \ntext</start>" ); |
---|
669 | TiXmlElement text1( "text" ); |
---|
670 | parse1 >> text1; |
---|
671 | |
---|
672 | XmlTest ( "Condense white space OFF.", "This is \ntext", |
---|
673 | text1.FirstChild()->Value(), |
---|
674 | true ); |
---|
675 | } |
---|
676 | TiXmlBase::SetCondenseWhiteSpace( true ); |
---|
677 | #endif |
---|
678 | |
---|
679 | ////////////////////////////////////////////////////// |
---|
680 | // GetText(); |
---|
681 | { |
---|
682 | const char* str = "<foo>This is text</foo>"; |
---|
683 | TiXmlDocument doc; |
---|
684 | doc.Parse( str ); |
---|
685 | const TiXmlElement* element = doc.RootElement(); |
---|
686 | |
---|
687 | XmlTest( "GetText() normal use.", "This is text", element->GetText() ); |
---|
688 | |
---|
689 | str = "<foo><b>This is text</b></foo>"; |
---|
690 | doc.Clear(); |
---|
691 | doc.Parse( str ); |
---|
692 | element = doc.RootElement(); |
---|
693 | |
---|
694 | XmlTest( "GetText() contained element.", element->GetText() == 0, true ); |
---|
695 | |
---|
696 | str = "<foo>This is <b>text</b></foo>"; |
---|
697 | doc.Clear(); |
---|
698 | TiXmlBase::SetCondenseWhiteSpace( false ); |
---|
699 | doc.Parse( str ); |
---|
700 | TiXmlBase::SetCondenseWhiteSpace( true ); |
---|
701 | element = doc.RootElement(); |
---|
702 | |
---|
703 | XmlTest( "GetText() partial.", "This is ", element->GetText() ); |
---|
704 | } |
---|
705 | |
---|
706 | |
---|
707 | ////////////////////////////////////////////////////// |
---|
708 | // CDATA |
---|
709 | { |
---|
710 | const char* str = "<xmlElement>" |
---|
711 | "<![CDATA[" |
---|
712 | "I am > the rules!\n" |
---|
713 | "...since I make symbolic puns" |
---|
714 | "]]>" |
---|
715 | "</xmlElement>"; |
---|
716 | TiXmlDocument doc; |
---|
717 | doc.Parse( str ); |
---|
718 | doc.Print(); |
---|
719 | |
---|
720 | XmlTest( "CDATA parse.", doc.FirstChildElement()->FirstChild()->Value(), |
---|
721 | "I am > the rules!\n...since I make symbolic puns", |
---|
722 | true ); |
---|
723 | |
---|
724 | #ifdef TIXML_USE_STL |
---|
725 | //cout << doc << '\n'; |
---|
726 | |
---|
727 | doc.Clear(); |
---|
728 | |
---|
729 | istringstream parse0( str ); |
---|
730 | parse0 >> doc; |
---|
731 | //cout << doc << '\n'; |
---|
732 | |
---|
733 | XmlTest( "CDATA stream.", doc.FirstChildElement()->FirstChild()->Value(), |
---|
734 | "I am > the rules!\n...since I make symbolic puns", |
---|
735 | true ); |
---|
736 | #endif |
---|
737 | |
---|
738 | TiXmlDocument doc1 = doc; |
---|
739 | //doc.Print(); |
---|
740 | |
---|
741 | XmlTest( "CDATA copy.", doc1.FirstChildElement()->FirstChild()->Value(), |
---|
742 | "I am > the rules!\n...since I make symbolic puns", |
---|
743 | true ); |
---|
744 | } |
---|
745 | { |
---|
746 | // [ 1482728 ] Wrong wide char parsing |
---|
747 | char buf[256]; |
---|
748 | buf[255] = 0; |
---|
749 | for( int i=0; i<255; ++i ) buf[i] = i>=32 ? i : 32; |
---|
750 | TIXML_STRING str( "<xmlElement><![CDATA[" ); |
---|
751 | str += buf; |
---|
752 | str += "]]></xmlElement>"; |
---|
753 | |
---|
754 | TiXmlDocument doc; |
---|
755 | doc.Parse( str.c_str() ); |
---|
756 | |
---|
757 | TiXmlPrinter printer; |
---|
758 | printer.SetStreamPrinting(); |
---|
759 | doc.Accept( &printer ); |
---|
760 | |
---|
761 | XmlTest( "CDATA with all bytes #1.", str.c_str(), printer.CStr(), true ); |
---|
762 | |
---|
763 | #ifdef TIXML_USE_STL |
---|
764 | doc.Clear(); |
---|
765 | istringstream iss( printer.Str() ); |
---|
766 | iss >> doc; |
---|
767 | std::string out; |
---|
768 | out << doc; |
---|
769 | XmlTest( "CDATA with all bytes #2.", out.c_str(), printer.CStr(), true ); |
---|
770 | #endif |
---|
771 | } |
---|
772 | { |
---|
773 | // [ 1480107 ] Bug-fix for STL-streaming of CDATA that contains tags |
---|
774 | // CDATA streaming had a couple of bugs, that this tests for. |
---|
775 | const char* str = "<xmlElement>" |
---|
776 | "<![CDATA[" |
---|
777 | "<b>I am > the rules!</b>\n" |
---|
778 | "...since I make symbolic puns" |
---|
779 | "]]>" |
---|
780 | "</xmlElement>"; |
---|
781 | TiXmlDocument doc; |
---|
782 | doc.Parse( str ); |
---|
783 | doc.Print(); |
---|
784 | |
---|
785 | XmlTest( "CDATA parse. [ 1480107 ]", doc.FirstChildElement()->FirstChild()->Value(), |
---|
786 | "<b>I am > the rules!</b>\n...since I make symbolic puns", |
---|
787 | true ); |
---|
788 | |
---|
789 | #ifdef TIXML_USE_STL |
---|
790 | |
---|
791 | doc.Clear(); |
---|
792 | |
---|
793 | istringstream parse0( str ); |
---|
794 | parse0 >> doc; |
---|
795 | |
---|
796 | XmlTest( "CDATA stream. [ 1480107 ]", doc.FirstChildElement()->FirstChild()->Value(), |
---|
797 | "<b>I am > the rules!</b>\n...since I make symbolic puns", |
---|
798 | true ); |
---|
799 | #endif |
---|
800 | |
---|
801 | TiXmlDocument doc1 = doc; |
---|
802 | //doc.Print(); |
---|
803 | |
---|
804 | XmlTest( "CDATA copy. [ 1480107 ]", doc1.FirstChildElement()->FirstChild()->Value(), |
---|
805 | "<b>I am > the rules!</b>\n...since I make symbolic puns", |
---|
806 | true ); |
---|
807 | } |
---|
808 | ////////////////////////////////////////////////////// |
---|
809 | // Visit() |
---|
810 | |
---|
811 | |
---|
812 | |
---|
813 | ////////////////////////////////////////////////////// |
---|
814 | printf( "\n** Fuzzing... **\n" ); |
---|
815 | |
---|
816 | const int FUZZ_ITERATION = 300; |
---|
817 | |
---|
818 | // The only goal is not to crash on bad input. |
---|
819 | int len = (int) strlen( demoStart ); |
---|
820 | for( int i=0; i<FUZZ_ITERATION; ++i ) |
---|
821 | { |
---|
822 | char* demoCopy = new char[ len+1 ]; |
---|
823 | strcpy( demoCopy, demoStart ); |
---|
824 | |
---|
825 | demoCopy[ i%len ] = (char)((i+1)*3); |
---|
826 | demoCopy[ (i*7)%len ] = '>'; |
---|
827 | demoCopy[ (i*11)%len ] = '<'; |
---|
828 | |
---|
829 | TiXmlDocument xml; |
---|
830 | xml.Parse( demoCopy ); |
---|
831 | |
---|
832 | delete [] demoCopy; |
---|
833 | } |
---|
834 | printf( "** Fuzzing Complete. **\n" ); |
---|
835 | |
---|
836 | ////////////////////////////////////////////////////// |
---|
837 | printf ("\n** Bug regression tests **\n"); |
---|
838 | |
---|
839 | // InsertBeforeChild and InsertAfterChild causes crash. |
---|
840 | { |
---|
841 | TiXmlElement parent( "Parent" ); |
---|
842 | TiXmlElement childText0( "childText0" ); |
---|
843 | TiXmlElement childText1( "childText1" ); |
---|
844 | TiXmlNode* childNode0 = parent.InsertEndChild( childText0 ); |
---|
845 | TiXmlNode* childNode1 = parent.InsertBeforeChild( childNode0, childText1 ); |
---|
846 | |
---|
847 | XmlTest( "Test InsertBeforeChild on empty node.", ( childNode1 == parent.FirstChild() ), true ); |
---|
848 | } |
---|
849 | |
---|
850 | { |
---|
851 | // InsertBeforeChild and InsertAfterChild causes crash. |
---|
852 | TiXmlElement parent( "Parent" ); |
---|
853 | TiXmlElement childText0( "childText0" ); |
---|
854 | TiXmlElement childText1( "childText1" ); |
---|
855 | TiXmlNode* childNode0 = parent.InsertEndChild( childText0 ); |
---|
856 | TiXmlNode* childNode1 = parent.InsertAfterChild( childNode0, childText1 ); |
---|
857 | |
---|
858 | XmlTest( "Test InsertAfterChild on empty node. ", ( childNode1 == parent.LastChild() ), true ); |
---|
859 | } |
---|
860 | |
---|
861 | // Reports of missing constructors, irregular string problems. |
---|
862 | { |
---|
863 | // Missing constructor implementation. No test -- just compiles. |
---|
864 | TiXmlText text( "Missing" ); |
---|
865 | |
---|
866 | #ifdef TIXML_USE_STL |
---|
867 | // Missing implementation: |
---|
868 | TiXmlDocument doc; |
---|
869 | string name = "missing"; |
---|
870 | doc.LoadFile( name ); |
---|
871 | |
---|
872 | TiXmlText textSTL( name ); |
---|
873 | #else |
---|
874 | // verifying some basic string functions: |
---|
875 | TiXmlString a; |
---|
876 | TiXmlString b( "Hello" ); |
---|
877 | TiXmlString c( "ooga" ); |
---|
878 | |
---|
879 | c = " World!"; |
---|
880 | a = b; |
---|
881 | a += c; |
---|
882 | a = a; |
---|
883 | |
---|
884 | XmlTest( "Basic TiXmlString test. ", "Hello World!", a.c_str() ); |
---|
885 | #endif |
---|
886 | } |
---|
887 | |
---|
888 | // Long filenames crashing STL version |
---|
889 | { |
---|
890 | TiXmlDocument doc( "midsummerNightsDreamWithAVeryLongFilenameToConfuseTheStringHandlingRoutines.xml" ); |
---|
891 | bool loadOkay = doc.LoadFile(); |
---|
892 | loadOkay = true; // get rid of compiler warning. |
---|
893 | // Won't pass on non-dev systems. Just a "no crash" check. |
---|
894 | //XmlTest( "Long filename. ", true, loadOkay ); |
---|
895 | } |
---|
896 | |
---|
897 | { |
---|
898 | // Entities not being written correctly. |
---|
899 | // From Lynn Allen |
---|
900 | |
---|
901 | const char* passages = |
---|
902 | "<?xml version=\"1.0\" standalone=\"no\" ?>" |
---|
903 | "<passages count=\"006\" formatversion=\"20020620\">" |
---|
904 | "<psg context=\"Line 5 has "quotation marks" and 'apostrophe marks'." |
---|
905 | " It also has <, >, and &, as well as a fake copyright ©.\"> </psg>" |
---|
906 | "</passages>"; |
---|
907 | |
---|
908 | TiXmlDocument doc( "passages.xml" ); |
---|
909 | doc.Parse( passages ); |
---|
910 | TiXmlElement* psg = doc.RootElement()->FirstChildElement(); |
---|
911 | const char* context = psg->Attribute( "context" ); |
---|
912 | const char* expected = "Line 5 has \"quotation marks\" and 'apostrophe marks'. It also has <, >, and &, as well as a fake copyright \xC2\xA9."; |
---|
913 | |
---|
914 | XmlTest( "Entity transformation: read. ", expected, context, true ); |
---|
915 | |
---|
916 | FILE* textfile = fopen( "textfile.txt", "w" ); |
---|
917 | if ( textfile ) |
---|
918 | { |
---|
919 | psg->Print( textfile, 0 ); |
---|
920 | fclose( textfile ); |
---|
921 | } |
---|
922 | textfile = fopen( "textfile.txt", "r" ); |
---|
923 | assert( textfile ); |
---|
924 | if ( textfile ) |
---|
925 | { |
---|
926 | char buf[ 1024 ]; |
---|
927 | fgets( buf, 1024, textfile ); |
---|
928 | XmlTest( "Entity transformation: write. ", |
---|
929 | "<psg context=\'Line 5 has "quotation marks" and 'apostrophe marks'." |
---|
930 | " It also has <, >, and &, as well as a fake copyright \xC2\xA9.' />", |
---|
931 | buf, |
---|
932 | true ); |
---|
933 | } |
---|
934 | fclose( textfile ); |
---|
935 | } |
---|
936 | |
---|
937 | { |
---|
938 | FILE* textfile = fopen( "test5.xml", "w" ); |
---|
939 | if ( textfile ) |
---|
940 | { |
---|
941 | fputs("<?xml version='1.0'?><a.elem xmi.version='2.0'/>", textfile); |
---|
942 | fclose(textfile); |
---|
943 | |
---|
944 | TiXmlDocument doc; |
---|
945 | doc.LoadFile( "test5.xml" ); |
---|
946 | XmlTest( "dot in element attributes and names", doc.Error(), 0); |
---|
947 | } |
---|
948 | } |
---|
949 | |
---|
950 | { |
---|
951 | FILE* textfile = fopen( "test6.xml", "w" ); |
---|
952 | if ( textfile ) |
---|
953 | { |
---|
954 | fputs("<element><Name>1.1 Start easy ignore fin thickness
</Name></element>", textfile ); |
---|
955 | fclose(textfile); |
---|
956 | |
---|
957 | TiXmlDocument doc; |
---|
958 | bool result = doc.LoadFile( "test6.xml" ); |
---|
959 | XmlTest( "Entity with one digit.", result, true ); |
---|
960 | |
---|
961 | TiXmlText* text = doc.FirstChildElement()->FirstChildElement()->FirstChild()->ToText(); |
---|
962 | XmlTest( "Entity with one digit.", |
---|
963 | text->Value(), "1.1 Start easy ignore fin thickness\n" ); |
---|
964 | } |
---|
965 | } |
---|
966 | |
---|
967 | { |
---|
968 | // DOCTYPE not preserved (950171) |
---|
969 | // |
---|
970 | const char* doctype = |
---|
971 | "<?xml version=\"1.0\" ?>" |
---|
972 | "<!DOCTYPE PLAY SYSTEM 'play.dtd'>" |
---|
973 | "<!ELEMENT title (#PCDATA)>" |
---|
974 | "<!ELEMENT books (title,authors)>" |
---|
975 | "<element />"; |
---|
976 | |
---|
977 | TiXmlDocument doc; |
---|
978 | doc.Parse( doctype ); |
---|
979 | doc.SaveFile( "test7.xml" ); |
---|
980 | doc.Clear(); |
---|
981 | doc.LoadFile( "test7.xml" ); |
---|
982 | |
---|
983 | TiXmlHandle docH( &doc ); |
---|
984 | TiXmlUnknown* unknown = docH.Child( 1 ).Unknown(); |
---|
985 | XmlTest( "Correct value of unknown.", "!DOCTYPE PLAY SYSTEM 'play.dtd'", unknown->Value() ); |
---|
986 | #ifdef TIXML_USE_STL |
---|
987 | TiXmlNode* node = docH.Child( 2 ).Node(); |
---|
988 | std::string str; |
---|
989 | str << (*node); |
---|
990 | XmlTest( "Correct streaming of unknown.", "<!ELEMENT title (#PCDATA)>", str.c_str() ); |
---|
991 | #endif |
---|
992 | } |
---|
993 | |
---|
994 | { |
---|
995 | // [ 791411 ] Formatting bug |
---|
996 | // Comments do not stream out correctly. |
---|
997 | const char* doctype = |
---|
998 | "<!-- Somewhat<evil> -->"; |
---|
999 | TiXmlDocument doc; |
---|
1000 | doc.Parse( doctype ); |
---|
1001 | |
---|
1002 | TiXmlHandle docH( &doc ); |
---|
1003 | TiXmlComment* comment = docH.Child( 0 ).Node()->ToComment(); |
---|
1004 | |
---|
1005 | XmlTest( "Comment formatting.", " Somewhat<evil> ", comment->Value() ); |
---|
1006 | #ifdef TIXML_USE_STL |
---|
1007 | std::string str; |
---|
1008 | str << (*comment); |
---|
1009 | XmlTest( "Comment streaming.", "<!-- Somewhat<evil> -->", str.c_str() ); |
---|
1010 | #endif |
---|
1011 | } |
---|
1012 | |
---|
1013 | { |
---|
1014 | // [ 870502 ] White space issues |
---|
1015 | TiXmlDocument doc; |
---|
1016 | TiXmlText* text; |
---|
1017 | TiXmlHandle docH( &doc ); |
---|
1018 | |
---|
1019 | const char* doctype0 = "<element> This has leading and trailing space </element>"; |
---|
1020 | const char* doctype1 = "<element>This has internal space</element>"; |
---|
1021 | const char* doctype2 = "<element> This has leading, trailing, and internal space </element>"; |
---|
1022 | |
---|
1023 | TiXmlBase::SetCondenseWhiteSpace( false ); |
---|
1024 | doc.Clear(); |
---|
1025 | doc.Parse( doctype0 ); |
---|
1026 | text = docH.FirstChildElement( "element" ).Child( 0 ).Text(); |
---|
1027 | XmlTest( "White space kept.", " This has leading and trailing space ", text->Value() ); |
---|
1028 | |
---|
1029 | doc.Clear(); |
---|
1030 | doc.Parse( doctype1 ); |
---|
1031 | text = docH.FirstChildElement( "element" ).Child( 0 ).Text(); |
---|
1032 | XmlTest( "White space kept.", "This has internal space", text->Value() ); |
---|
1033 | |
---|
1034 | doc.Clear(); |
---|
1035 | doc.Parse( doctype2 ); |
---|
1036 | text = docH.FirstChildElement( "element" ).Child( 0 ).Text(); |
---|
1037 | XmlTest( "White space kept.", " This has leading, trailing, and internal space ", text->Value() ); |
---|
1038 | |
---|
1039 | TiXmlBase::SetCondenseWhiteSpace( true ); |
---|
1040 | doc.Clear(); |
---|
1041 | doc.Parse( doctype0 ); |
---|
1042 | text = docH.FirstChildElement( "element" ).Child( 0 ).Text(); |
---|
1043 | XmlTest( "White space condensed.", "This has leading and trailing space", text->Value() ); |
---|
1044 | |
---|
1045 | doc.Clear(); |
---|
1046 | doc.Parse( doctype1 ); |
---|
1047 | text = docH.FirstChildElement( "element" ).Child( 0 ).Text(); |
---|
1048 | XmlTest( "White space condensed.", "This has internal space", text->Value() ); |
---|
1049 | |
---|
1050 | doc.Clear(); |
---|
1051 | doc.Parse( doctype2 ); |
---|
1052 | text = docH.FirstChildElement( "element" ).Child( 0 ).Text(); |
---|
1053 | XmlTest( "White space condensed.", "This has leading, trailing, and internal space", text->Value() ); |
---|
1054 | } |
---|
1055 | |
---|
1056 | { |
---|
1057 | // Double attributes |
---|
1058 | const char* doctype = "<element attr='red' attr='blue' />"; |
---|
1059 | |
---|
1060 | TiXmlDocument doc; |
---|
1061 | doc.Parse( doctype ); |
---|
1062 | |
---|
1063 | XmlTest( "Parsing repeated attributes.", 0, (int)doc.Error() ); // not an error to tinyxml |
---|
1064 | XmlTest( "Parsing repeated attributes.", "blue", doc.FirstChildElement( "element" )->Attribute( "attr" ) ); |
---|
1065 | } |
---|
1066 | |
---|
1067 | { |
---|
1068 | // Embedded null in stream. |
---|
1069 | const char* doctype = "<element att\0r='red' attr='blue' />"; |
---|
1070 | |
---|
1071 | TiXmlDocument doc; |
---|
1072 | doc.Parse( doctype ); |
---|
1073 | XmlTest( "Embedded null throws error.", true, doc.Error() ); |
---|
1074 | |
---|
1075 | #ifdef TIXML_USE_STL |
---|
1076 | istringstream strm( doctype ); |
---|
1077 | doc.Clear(); |
---|
1078 | doc.ClearError(); |
---|
1079 | strm >> doc; |
---|
1080 | XmlTest( "Embedded null throws error.", true, doc.Error() ); |
---|
1081 | #endif |
---|
1082 | } |
---|
1083 | |
---|
1084 | { |
---|
1085 | // Legacy mode test. (This test may only pass on a western system) |
---|
1086 | const char* str = |
---|
1087 | "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" |
---|
1088 | "<ä>" |
---|
1089 | "CöntäntßäöüÄÖÜ" |
---|
1090 | "</ä>"; |
---|
1091 | |
---|
1092 | TiXmlDocument doc; |
---|
1093 | doc.Parse( str ); |
---|
1094 | |
---|
1095 | TiXmlHandle docHandle( &doc ); |
---|
1096 | TiXmlHandle aHandle = docHandle.FirstChildElement( "ä" ); |
---|
1097 | TiXmlHandle tHandle = aHandle.Child( 0 ); |
---|
1098 | assert( aHandle.Element() ); |
---|
1099 | assert( tHandle.Text() ); |
---|
1100 | XmlTest( "ISO-8859-1 Parsing.", "CöntäntßäöüÄÖÜ", tHandle.Text()->Value() ); |
---|
1101 | } |
---|
1102 | |
---|
1103 | { |
---|
1104 | // Empty documents should return TIXML_ERROR_PARSING_EMPTY, bug 1070717 |
---|
1105 | const char* str = " "; |
---|
1106 | TiXmlDocument doc; |
---|
1107 | doc.Parse( str ); |
---|
1108 | XmlTest( "Empty document error TIXML_ERROR_DOCUMENT_EMPTY", TiXmlBase::TIXML_ERROR_DOCUMENT_EMPTY, doc.ErrorId() ); |
---|
1109 | } |
---|
1110 | #ifndef TIXML_USE_STL |
---|
1111 | { |
---|
1112 | // String equality. [ 1006409 ] string operator==/!= no worky in all cases |
---|
1113 | TiXmlString temp; |
---|
1114 | XmlTest( "Empty tinyxml string compare equal", ( temp == "" ), true ); |
---|
1115 | |
---|
1116 | TiXmlString foo; |
---|
1117 | TiXmlString bar( "" ); |
---|
1118 | XmlTest( "Empty tinyxml string compare equal", ( foo == bar ), true ); |
---|
1119 | } |
---|
1120 | |
---|
1121 | #endif |
---|
1122 | { |
---|
1123 | // Bug [ 1195696 ] from marlonism |
---|
1124 | TiXmlBase::SetCondenseWhiteSpace(false); |
---|
1125 | TiXmlDocument xml; |
---|
1126 | xml.Parse("<text><break/>This hangs</text>"); |
---|
1127 | XmlTest( "Test safe error return.", xml.Error(), false ); |
---|
1128 | } |
---|
1129 | |
---|
1130 | { |
---|
1131 | // Bug [ 1243992 ] - another infinite loop |
---|
1132 | TiXmlDocument doc; |
---|
1133 | doc.SetCondenseWhiteSpace(false); |
---|
1134 | doc.Parse("<p><pb></pb>test</p>"); |
---|
1135 | } |
---|
1136 | { |
---|
1137 | // Low entities |
---|
1138 | TiXmlDocument xml; |
---|
1139 | xml.Parse( "<test></test>" ); |
---|
1140 | const char result[] = { 0x0e, 0 }; |
---|
1141 | XmlTest( "Low entities.", xml.FirstChildElement()->GetText(), result ); |
---|
1142 | xml.Print(); |
---|
1143 | } |
---|
1144 | { |
---|
1145 | // Bug [ 1451649 ] Attribute values with trailing quotes not handled correctly |
---|
1146 | TiXmlDocument xml; |
---|
1147 | xml.Parse( "<foo attribute=bar\" />" ); |
---|
1148 | XmlTest( "Throw error with bad end quotes.", xml.Error(), true ); |
---|
1149 | } |
---|
1150 | #ifdef TIXML_USE_STL |
---|
1151 | { |
---|
1152 | // Bug [ 1449463 ] Consider generic query |
---|
1153 | TiXmlDocument xml; |
---|
1154 | xml.Parse( "<foo bar='3' />" ); |
---|
1155 | TiXmlElement* ele = xml.FirstChildElement(); |
---|
1156 | double d; |
---|
1157 | int i; |
---|
1158 | float f; |
---|
1159 | bool b; |
---|
1160 | |
---|
1161 | XmlTest( "QueryValueAttribute", ele->QueryValueAttribute( "bar", &d ), TIXML_SUCCESS ); |
---|
1162 | XmlTest( "QueryValueAttribute", ele->QueryValueAttribute( "bar", &i ), TIXML_SUCCESS ); |
---|
1163 | XmlTest( "QueryValueAttribute", ele->QueryValueAttribute( "bar", &f ), TIXML_SUCCESS ); |
---|
1164 | XmlTest( "QueryValueAttribute", ele->QueryValueAttribute( "bar", &b ), TIXML_WRONG_TYPE ); |
---|
1165 | XmlTest( "QueryValueAttribute", ele->QueryValueAttribute( "nobar", &b ), TIXML_NO_ATTRIBUTE ); |
---|
1166 | |
---|
1167 | XmlTest( "QueryValueAttribute", (d==3.0), true ); |
---|
1168 | XmlTest( "QueryValueAttribute", (i==3), true ); |
---|
1169 | XmlTest( "QueryValueAttribute", (f==3.0f), true ); |
---|
1170 | } |
---|
1171 | #endif |
---|
1172 | |
---|
1173 | #ifdef TIXML_USE_STL |
---|
1174 | { |
---|
1175 | // [ 1505267 ] redundant malloc in TiXmlElement::Attribute |
---|
1176 | TiXmlDocument xml; |
---|
1177 | xml.Parse( "<foo bar='3' />" ); |
---|
1178 | TiXmlElement* ele = xml.FirstChildElement(); |
---|
1179 | double d; |
---|
1180 | int i; |
---|
1181 | |
---|
1182 | std::string bar = "bar"; |
---|
1183 | |
---|
1184 | const std::string* atrrib = ele->Attribute( bar ); |
---|
1185 | ele->Attribute( bar, &d ); |
---|
1186 | ele->Attribute( bar, &i ); |
---|
1187 | |
---|
1188 | XmlTest( "Attribute", atrrib->empty(), false ); |
---|
1189 | XmlTest( "Attribute", (d==3.0), true ); |
---|
1190 | XmlTest( "Attribute", (i==3), true ); |
---|
1191 | } |
---|
1192 | #endif |
---|
1193 | |
---|
1194 | { |
---|
1195 | // [ 1356059 ] Allow TiXMLDocument to only be at the top level |
---|
1196 | TiXmlDocument xml, xml2; |
---|
1197 | xml.InsertEndChild( xml2 ); |
---|
1198 | XmlTest( "Document only at top level.", xml.Error(), true ); |
---|
1199 | XmlTest( "Document only at top level.", xml.ErrorId(), TiXmlBase::TIXML_ERROR_DOCUMENT_TOP_ONLY ); |
---|
1200 | } |
---|
1201 | |
---|
1202 | /* 1417717 experiment |
---|
1203 | { |
---|
1204 | TiXmlDocument xml; |
---|
1205 | xml.Parse("<text>Dan & Tracie</text>"); |
---|
1206 | xml.Print(stdout); |
---|
1207 | } |
---|
1208 | { |
---|
1209 | TiXmlDocument xml; |
---|
1210 | xml.Parse("<text>Dan &foo; Tracie</text>"); |
---|
1211 | xml.Print(stdout); |
---|
1212 | } |
---|
1213 | */ |
---|
1214 | #if defined( WIN32 ) && defined( TUNE ) |
---|
1215 | _CrtMemCheckpoint( &endMemState ); |
---|
1216 | //_CrtMemDumpStatistics( &endMemState ); |
---|
1217 | |
---|
1218 | _CrtMemState diffMemState; |
---|
1219 | _CrtMemDifference( &diffMemState, &startMemState, &endMemState ); |
---|
1220 | _CrtMemDumpStatistics( &diffMemState ); |
---|
1221 | #endif |
---|
1222 | |
---|
1223 | printf ("\nPass %d, Fail %d\n", gPass, gFail); |
---|
1224 | return gFail; |
---|
1225 | } |
---|
1226 | |
---|
1227 | |
---|