forked from leethomason/tinyxml2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmltest.cpp
2743 lines (2361 loc) · 92.9 KB
/
xmltest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#if defined( _MSC_VER )
#if !defined( _CRT_SECURE_NO_WARNINGS )
#define _CRT_SECURE_NO_WARNINGS // This test file is not intended to be secure.
#endif
#endif
#include "tinyxml2.h"
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if defined( _MSC_VER ) || defined (WIN32)
#include <crtdbg.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
_CrtMemState startMemState;
_CrtMemState endMemState;
#else
#include <sys/stat.h>
#include <sys/types.h>
#endif
using namespace tinyxml2;
using namespace std;
int gPass = 0;
int gFail = 0;
bool XMLTest (const char* testString, const char* expected, const char* found, bool echo=true, bool extraNL=false )
{
bool pass;
if ( !expected && !found )
pass = true;
else if ( !expected || !found )
pass = false;
else
pass = !strcmp( expected, found );
if ( pass )
printf ("[pass]");
else
printf ("[fail]");
if ( !echo ) {
printf (" %s\n", testString);
}
else {
if ( extraNL ) {
printf( " %s\n", testString );
printf( "%s\n", expected );
printf( "%s\n", found );
}
else {
printf (" %s [%s][%s]\n", testString, expected, found);
}
}
if ( pass )
++gPass;
else
++gFail;
return pass;
}
bool XMLTest(const char* testString, XMLError expected, XMLError found, bool echo = true, bool extraNL = false)
{
return XMLTest(testString, XMLDocument::ErrorIDToName(expected), XMLDocument::ErrorIDToName(found), echo, extraNL);
}
bool XMLTest(const char* testString, bool expected, bool found, bool echo = true, bool extraNL = false)
{
return XMLTest(testString, expected ? "true" : "false", found ? "true" : "false", echo, extraNL);
}
template< class T > bool XMLTest( const char* testString, T expected, T found, bool echo=true )
{
bool pass = ( expected == found );
if ( pass )
printf ("[pass]");
else
printf ("[fail]");
if ( !echo )
printf (" %s\n", testString);
else {
char expectedAsString[64];
XMLUtil::ToStr(expected, expectedAsString, sizeof(expectedAsString));
char foundAsString[64];
XMLUtil::ToStr(found, foundAsString, sizeof(foundAsString));
printf (" %s [%s][%s]\n", testString, expectedAsString, foundAsString );
}
if ( pass )
++gPass;
else
++gFail;
return pass;
}
void NullLineEndings( char* p )
{
while( p && *p ) {
if ( *p == '\n' || *p == '\r' ) {
*p = 0;
return;
}
++p;
}
}
int example_1()
{
XMLDocument doc;
doc.LoadFile( "resources/dream.xml" );
return doc.ErrorID();
}
/** @page Example_1 Load an XML File
* @dontinclude ./xmltest.cpp
* Basic XML file loading.
* The basic syntax to load an XML file from
* disk and check for an error. (ErrorID()
* will return 0 for no error.)
* @skip example_1()
* @until }
*/
int example_2()
{
static const char* xml = "<element/>";
XMLDocument doc;
doc.Parse( xml );
return doc.ErrorID();
}
/** @page Example_2 Parse an XML from char buffer
* @dontinclude ./xmltest.cpp
* Basic XML string parsing.
* The basic syntax to parse an XML for
* a char* and check for an error. (ErrorID()
* will return 0 for no error.)
* @skip example_2()
* @until }
*/
int example_3()
{
static const char* xml =
"<?xml version=\"1.0\"?>"
"<!DOCTYPE PLAY SYSTEM \"play.dtd\">"
"<PLAY>"
"<TITLE>A Midsummer Night's Dream</TITLE>"
"</PLAY>";
XMLDocument doc;
doc.Parse( xml );
XMLElement* titleElement = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" );
const char* title = titleElement->GetText();
printf( "Name of play (1): %s\n", title );
XMLText* textNode = titleElement->FirstChild()->ToText();
title = textNode->Value();
printf( "Name of play (2): %s\n", title );
return doc.ErrorID();
}
/** @page Example_3 Get information out of XML
@dontinclude ./xmltest.cpp
In this example, we navigate a simple XML
file, and read some interesting text. Note
that this example doesn't use error
checking; working code should check for null
pointers when walking an XML tree, or use
XMLHandle.
(The XML is an excerpt from "dream.xml").
@skip example_3()
@until </PLAY>";
The structure of the XML file is:
<ul>
<li>(declaration)</li>
<li>(dtd stuff)</li>
<li>Element "PLAY"</li>
<ul>
<li>Element "TITLE"</li>
<ul>
<li>Text "A Midsummer Night's Dream"</li>
</ul>
</ul>
</ul>
For this example, we want to print out the
title of the play. The text of the title (what
we want) is child of the "TITLE" element which
is a child of the "PLAY" element.
We want to skip the declaration and dtd, so the
method FirstChildElement() is a good choice. The
FirstChildElement() of the Document is the "PLAY"
Element, the FirstChildElement() of the "PLAY" Element
is the "TITLE" Element.
@until ( "TITLE" );
We can then use the convenience function GetText()
to get the title of the play.
@until title );
Text is just another Node in the XML DOM. And in
fact you should be a little cautious with it, as
text nodes can contain elements.
@verbatim
Consider: A Midsummer Night's <b>Dream</b>
@endverbatim
It is more correct to actually query the Text Node
if in doubt:
@until title );
Noting that here we use FirstChild() since we are
looking for XMLText, not an element, and ToText()
is a cast from a Node to a XMLText.
*/
bool example_4()
{
static const char* xml =
"<information>"
" <attributeApproach v='2' />"
" <textApproach>"
" <v>2</v>"
" </textApproach>"
"</information>";
XMLDocument doc;
doc.Parse( xml );
int v0 = 0;
int v1 = 0;
XMLElement* attributeApproachElement = doc.FirstChildElement()->FirstChildElement( "attributeApproach" );
attributeApproachElement->QueryIntAttribute( "v", &v0 );
XMLElement* textApproachElement = doc.FirstChildElement()->FirstChildElement( "textApproach" );
textApproachElement->FirstChildElement( "v" )->QueryIntText( &v1 );
printf( "Both values are the same: %d and %d\n", v0, v1 );
return !doc.Error() && ( v0 == v1 );
}
/** @page Example_4 Read attributes and text information.
@dontinclude ./xmltest.cpp
There are fundamentally 2 ways of writing a key-value
pair into an XML file. (Something that's always annoyed
me about XML.) Either by using attributes, or by writing
the key name into an element and the value into
the text node wrapped by the element. Both approaches
are illustrated in this example, which shows two ways
to encode the value "2" into the key "v":
@skip example_4()
@until "</information>";
TinyXML-2 has accessors for both approaches.
When using an attribute, you navigate to the XMLElement
with that attribute and use the QueryIntAttribute()
group of methods. (Also QueryFloatAttribute(), etc.)
@skip XMLElement* attributeApproachElement
@until &v0 );
When using the text approach, you need to navigate
down one more step to the XMLElement that contains
the text. Note the extra FirstChildElement( "v" )
in the code below. The value of the text can then
be safely queried with the QueryIntText() group
of methods. (Also QueryFloatText(), etc.)
@skip XMLElement* textApproachElement
@until &v1 );
*/
int main( int argc, const char ** argv )
{
#if defined( _MSC_VER ) && defined( TINYXML2_DEBUG )
_CrtMemCheckpoint( &startMemState );
// Enable MS Visual C++ debug heap memory leaks dump on exit
_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
{
int leaksOnStart = _CrtDumpMemoryLeaks();
XMLTest( "No leaks on start?", FALSE, leaksOnStart );
}
#endif
{
TIXMLASSERT( true );
}
if ( argc > 1 ) {
XMLDocument* doc = new XMLDocument();
clock_t startTime = clock();
doc->LoadFile( argv[1] );
clock_t loadTime = clock();
int errorID = doc->ErrorID();
delete doc; doc = 0;
clock_t deleteTime = clock();
printf( "Test file '%s' loaded. ErrorID=%d\n", argv[1], errorID );
if ( !errorID ) {
printf( "Load time=%u\n", (unsigned)(loadTime - startTime) );
printf( "Delete time=%u\n", (unsigned)(deleteTime - loadTime) );
printf( "Total time=%u\n", (unsigned)(deleteTime - startTime) );
}
exit(0);
}
FILE* fp = fopen( "resources/dream.xml", "r" );
if ( !fp ) {
printf( "Error opening test file 'dream.xml'.\n"
"Is your working directory the same as where \n"
"the xmltest.cpp and dream.xml file are?\n\n"
#if defined( _MSC_VER )
"In windows Visual Studio you may need to set\n"
"Properties->Debugging->Working Directory to '..'\n"
#endif
);
exit( 1 );
}
fclose( fp );
XMLTest( "Example_1", 0, example_1() );
XMLTest( "Example_2", 0, example_2() );
XMLTest( "Example_3", 0, example_3() );
XMLTest( "Example_4", true, example_4() );
/* ------ Example 2: Lookup information. ---- */
{
static const char* test[] = { "<element />",
"<element></element>",
"<element><subelement/></element>",
"<element><subelement></subelement></element>",
"<element><subelement><subsub/></subelement></element>",
"<!--comment beside elements--><element><subelement></subelement></element>",
"<!--comment beside elements, this time with spaces--> \n <element> <subelement> \n </subelement> </element>",
"<element attrib1='foo' attrib2=\"bar\" ></element>",
"<element attrib1='foo' attrib2=\"bar\" ><subelement attrib3='yeehaa' /></element>",
"<element>Text inside element.</element>",
"<element><b></b></element>",
"<element>Text inside and <b>bolded</b> in the element.</element>",
"<outer><element>Text inside and <b>bolded</b> in the element.</element></outer>",
"<element>This & That.</element>",
"<element attrib='This<That' />",
0
};
for( int i=0; test[i]; ++i ) {
XMLDocument doc;
doc.Parse( test[i] );
XMLTest( "Element test", false, doc.Error() );
doc.Print();
printf( "----------------------------------------------\n" );
}
}
#if 1
{
static const char* test = "<!--hello world\n"
" line 2\r"
" line 3\r\n"
" line 4\n\r"
" line 5\r-->";
XMLDocument doc;
doc.Parse( test );
XMLTest( "Hello world declaration", false, doc.Error() );
doc.Print();
}
{
// This test is pre-test for the next one
// (where Element1 is inserted "after itself".
// This code didn't use to crash.
XMLDocument doc;
XMLElement* element1 = doc.NewElement("Element1");
XMLElement* element2 = doc.NewElement("Element2");
doc.InsertEndChild(element1);
doc.InsertEndChild(element2);
doc.InsertAfterChild(element2, element2);
doc.InsertAfterChild(element2, element2);
}
{
XMLDocument doc;
XMLElement* element1 = doc.NewElement("Element1");
XMLElement* element2 = doc.NewElement("Element2");
doc.InsertEndChild(element1);
doc.InsertEndChild(element2);
// This insertion "after itself"
// used to cause invalid memory access and crash
doc.InsertAfterChild(element1, element1);
doc.InsertAfterChild(element1, element1);
doc.InsertAfterChild(element2, element2);
doc.InsertAfterChild(element2, element2);
}
{
static const char* test = "<element>Text before.</element>";
XMLDocument doc;
doc.Parse( test );
XMLTest( "Element text before", false, doc.Error() );
XMLElement* root = doc.FirstChildElement();
XMLElement* newElement = doc.NewElement( "Subelement" );
root->InsertEndChild( newElement );
doc.Print();
}
{
XMLDocument* doc = new XMLDocument();
static const char* test = "<element><sub/></element>";
doc->Parse( test );
XMLTest( "Element with sub element", false, doc->Error() );
delete doc;
}
{
// Test: Programmatic DOM nodes insertion return values
XMLDocument doc;
XMLNode* first = doc.NewElement( "firstElement" );
XMLTest( "New element", true, first != 0 );
XMLNode* firstAfterInsertion = doc.InsertFirstChild( first );
XMLTest( "New element inserted first", true, firstAfterInsertion == first );
XMLNode* last = doc.NewElement( "lastElement" );
XMLTest( "New element", true, last != 0 );
XMLNode* lastAfterInsertion = doc.InsertEndChild( last );
XMLTest( "New element inserted last", true, lastAfterInsertion == last );
XMLNode* middle = doc.NewElement( "middleElement" );
XMLTest( "New element", true, middle != 0 );
XMLNode* middleAfterInsertion = doc.InsertAfterChild( first, middle );
XMLTest( "New element inserted middle", true, middleAfterInsertion == middle );
}
{
// Test: Programmatic DOM
// Build:
// <element>
// <!--comment-->
// <sub attrib="0" />
// <sub attrib="1" />
// <sub attrib="2" >& Text!</sub>
// <element>
XMLDocument* doc = new XMLDocument();
XMLNode* element = doc->InsertEndChild( doc->NewElement( "element" ) );
XMLElement* sub[3] = { doc->NewElement( "sub" ), doc->NewElement( "sub" ), doc->NewElement( "sub" ) };
for( int i=0; i<3; ++i ) {
sub[i]->SetAttribute( "attrib", i );
}
element->InsertEndChild( sub[2] );
const int dummyInitialValue = 1000;
int dummyValue = dummyInitialValue;
XMLNode* comment = element->InsertFirstChild( doc->NewComment( "comment" ) );
comment->SetUserData(&dummyValue);
element->InsertAfterChild( comment, sub[0] );
element->InsertAfterChild( sub[0], sub[1] );
sub[2]->InsertFirstChild( doc->NewText( "& Text!" ));
doc->Print();
XMLTest( "Programmatic DOM", "comment", doc->FirstChildElement( "element" )->FirstChild()->Value() );
XMLTest( "Programmatic DOM", "0", doc->FirstChildElement( "element" )->FirstChildElement()->Attribute( "attrib" ) );
XMLTest( "Programmatic DOM", 2, doc->FirstChildElement()->LastChildElement( "sub" )->IntAttribute( "attrib" ) );
XMLTest( "Programmatic DOM", "& Text!",
doc->FirstChildElement()->LastChildElement( "sub" )->FirstChild()->ToText()->Value() );
XMLTest("User data - pointer", true, &dummyValue == comment->GetUserData(), false);
XMLTest("User data - value behind pointer", dummyInitialValue, dummyValue, false);
// And now deletion:
element->DeleteChild( sub[2] );
doc->DeleteNode( comment );
element->FirstChildElement()->SetAttribute( "attrib", true );
element->LastChildElement()->DeleteAttribute( "attrib" );
XMLTest( "Programmatic DOM", true, doc->FirstChildElement()->FirstChildElement()->BoolAttribute( "attrib" ) );
const int defaultIntValue = 10;
const int replacementIntValue = 20;
int value1 = defaultIntValue;
int value2 = doc->FirstChildElement()->LastChildElement()->IntAttribute( "attrib", replacementIntValue );
XMLError result = doc->FirstChildElement()->LastChildElement()->QueryIntAttribute( "attrib", &value1 );
XMLTest( "Programmatic DOM", XML_NO_ATTRIBUTE, result );
XMLTest( "Programmatic DOM", defaultIntValue, value1 );
XMLTest( "Programmatic DOM", replacementIntValue, value2 );
doc->Print();
{
XMLPrinter streamer;
doc->Print( &streamer );
printf( "%s", streamer.CStr() );
}
{
XMLPrinter streamer( 0, true );
doc->Print( &streamer );
XMLTest( "Compact mode", "<element><sub attrib=\"true\"/><sub/></element>", streamer.CStr(), false );
}
doc->SaveFile( "./resources/out/pretty.xml" );
XMLTest( "Save pretty.xml", false, doc->Error() );
doc->SaveFile( "./resources/out/compact.xml", true );
XMLTest( "Save compact.xml", false, doc->Error() );
delete doc;
}
{
// Test: Dream
// XML1 : 1,187,569 bytes in 31,209 allocations
// XML2 : 469,073 bytes in 323 allocations
//int newStart = gNew;
XMLDocument doc;
doc.LoadFile( "resources/dream.xml" );
XMLTest( "Load dream.xml", false, doc.Error() );
doc.SaveFile( "resources/out/dreamout.xml" );
XMLTest( "Save dreamout.xml", false, doc.Error() );
doc.PrintError();
XMLTest( "Dream", "xml version=\"1.0\"",
doc.FirstChild()->ToDeclaration()->Value() );
XMLTest( "Dream", true, doc.FirstChild()->NextSibling()->ToUnknown() != 0 );
XMLTest( "Dream", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
doc.FirstChild()->NextSibling()->ToUnknown()->Value() );
XMLTest( "Dream", "And Robin shall restore amends.",
doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
XMLTest( "Dream", "And Robin shall restore amends.",
doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
XMLDocument doc2;
doc2.LoadFile( "resources/out/dreamout.xml" );
XMLTest( "Load dreamout.xml", false, doc2.Error() );
XMLTest( "Dream-out", "xml version=\"1.0\"",
doc2.FirstChild()->ToDeclaration()->Value() );
XMLTest( "Dream-out", true, doc2.FirstChild()->NextSibling()->ToUnknown() != 0 );
XMLTest( "Dream-out", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
doc2.FirstChild()->NextSibling()->ToUnknown()->Value() );
XMLTest( "Dream-out", "And Robin shall restore amends.",
doc2.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
//gNewTotal = gNew - newStart;
}
{
const char* error = "<?xml version=\"1.0\" standalone=\"no\" ?>\n"
"<passages count=\"006\" formatversion=\"20020620\">\n"
" <wrong error>\n"
"</passages>";
XMLDocument doc;
doc.Parse( error );
XMLTest( "Bad XML", XML_ERROR_PARSING_ATTRIBUTE, doc.ErrorID() );
const char* errorStr = doc.ErrorStr();
XMLTest("Formatted error string",
"Error=XML_ERROR_PARSING_ATTRIBUTE ErrorID=7 (0x7) Line number=3: XMLElement name=wrong",
errorStr);
}
{
const char* str = "<doc attr0='1' attr1='2.0' attr2='foo' />";
XMLDocument doc;
doc.Parse( str );
XMLTest( "Top level attributes", false, doc.Error() );
XMLElement* ele = doc.FirstChildElement();
int iVal;
XMLError result;
double dVal;
result = ele->QueryDoubleAttribute( "attr0", &dVal );
XMLTest( "Query attribute: int as double", XML_SUCCESS, result);
XMLTest( "Query attribute: int as double", 1, (int)dVal );
XMLTest( "Query attribute: int as double", 1, (int)ele->DoubleAttribute("attr0"));
result = ele->QueryDoubleAttribute( "attr1", &dVal );
XMLTest( "Query attribute: double as double", XML_SUCCESS, result);
XMLTest( "Query attribute: double as double", 2.0, dVal );
XMLTest( "Query attribute: double as double", 2.0, ele->DoubleAttribute("attr1") );
result = ele->QueryIntAttribute( "attr1", &iVal );
XMLTest( "Query attribute: double as int", XML_SUCCESS, result);
XMLTest( "Query attribute: double as int", 2, iVal );
result = ele->QueryIntAttribute( "attr2", &iVal );
XMLTest( "Query attribute: not a number", XML_WRONG_ATTRIBUTE_TYPE, result );
XMLTest( "Query attribute: not a number", 4.0, ele->DoubleAttribute("attr2", 4.0) );
result = ele->QueryIntAttribute( "bar", &iVal );
XMLTest( "Query attribute: does not exist", XML_NO_ATTRIBUTE, result );
XMLTest( "Query attribute: does not exist", true, ele->BoolAttribute("bar", true) );
}
{
const char* str = "<doc/>";
XMLDocument doc;
doc.Parse( str );
XMLTest( "Empty top element", false, doc.Error() );
XMLElement* ele = doc.FirstChildElement();
int iVal, iVal2;
double dVal, dVal2;
ele->SetAttribute( "str", "strValue" );
ele->SetAttribute( "int", 1 );
ele->SetAttribute( "double", -1.0 );
const char* answer = 0;
ele->QueryAttribute("str", &answer);
XMLTest("Query char attribute", "strValue", answer);
const char* cStr = ele->Attribute( "str" );
{
XMLError queryResult = ele->QueryIntAttribute( "int", &iVal );
XMLTest( "Query int attribute", XML_SUCCESS, queryResult);
}
{
XMLError queryResult = ele->QueryDoubleAttribute( "double", &dVal );
XMLTest( "Query double attribute", XML_SUCCESS, queryResult);
}
{
XMLError queryResult = ele->QueryAttribute( "int", &iVal2 );
XMLTest( "Query int attribute generic", (int)XML_SUCCESS, queryResult);
}
{
XMLError queryResult = ele->QueryAttribute( "double", &dVal2 );
XMLTest( "Query double attribute generic", (int)XML_SUCCESS, queryResult);
}
XMLTest( "Attribute match test", "strValue", ele->Attribute( "str", "strValue" ) );
XMLTest( "Attribute round trip. c-string.", "strValue", cStr );
XMLTest( "Attribute round trip. int.", 1, iVal );
XMLTest( "Attribute round trip. double.", -1, (int)dVal );
XMLTest( "Alternate query", true, iVal == iVal2 );
XMLTest( "Alternate query", true, dVal == dVal2 );
XMLTest( "Alternate query", true, iVal == ele->IntAttribute("int") );
XMLTest( "Alternate query", true, dVal == ele->DoubleAttribute("double") );
}
{
XMLDocument doc;
doc.LoadFile( "resources/utf8test.xml" );
XMLTest( "Load utf8test.xml", false, doc.Error() );
// Get the attribute "value" from the "Russian" element and check it.
XMLElement* element = doc.FirstChildElement( "document" )->FirstChildElement( "Russian" );
const unsigned char correctValue[] = { 0xd1U, 0x86U, 0xd0U, 0xb5U, 0xd0U, 0xbdU, 0xd0U, 0xbdU,
0xd0U, 0xbeU, 0xd1U, 0x81U, 0xd1U, 0x82U, 0xd1U, 0x8cU, 0 };
XMLTest( "UTF-8: Russian value.", (const char*)correctValue, element->Attribute( "value" ) );
const unsigned char russianElementName[] = { 0xd0U, 0xa0U, 0xd1U, 0x83U,
0xd1U, 0x81U, 0xd1U, 0x81U,
0xd0U, 0xbaU, 0xd0U, 0xb8U,
0xd0U, 0xb9U, 0 };
const char russianText[] = "<\xD0\xB8\xD0\xBC\xD0\xB5\xD0\xB5\xD1\x82>";
XMLText* text = doc.FirstChildElement( "document" )->FirstChildElement( (const char*) russianElementName )->FirstChild()->ToText();
XMLTest( "UTF-8: Browsing russian element name.",
russianText,
text->Value() );
// Now try for a round trip.
doc.SaveFile( "resources/out/utf8testout.xml" );
XMLTest( "UTF-8: Save testout.xml", false, doc.Error() );
// Check the round trip.
bool roundTripOkay = false;
FILE* saved = fopen( "resources/out/utf8testout.xml", "r" );
XMLTest( "UTF-8: Open utf8testout.xml", true, saved != 0 );
FILE* verify = fopen( "resources/utf8testverify.xml", "r" );
XMLTest( "UTF-8: Open utf8testverify.xml", true, verify != 0 );
if ( saved && verify )
{
roundTripOkay = true;
char verifyBuf[256];
while ( fgets( verifyBuf, 256, verify ) )
{
char savedBuf[256];
fgets( savedBuf, 256, saved );
NullLineEndings( verifyBuf );
NullLineEndings( savedBuf );
if ( strcmp( verifyBuf, savedBuf ) )
{
printf( "verify:%s<\n", verifyBuf );
printf( "saved :%s<\n", savedBuf );
roundTripOkay = false;
break;
}
}
}
if ( saved )
fclose( saved );
if ( verify )
fclose( verify );
XMLTest( "UTF-8: Verified multi-language round trip.", true, roundTripOkay );
}
// --------GetText()-----------
{
const char* str = "<foo>This is text</foo>";
XMLDocument doc;
doc.Parse( str );
XMLTest( "Double whitespace", false, doc.Error() );
const XMLElement* element = doc.RootElement();
XMLTest( "GetText() normal use.", "This is text", element->GetText() );
str = "<foo><b>This is text</b></foo>";
doc.Parse( str );
XMLTest( "Bold text simulation", false, doc.Error() );
element = doc.RootElement();
XMLTest( "GetText() contained element.", element->GetText() == 0, true );
}
// --------SetText()-----------
{
const char* str = "<foo></foo>";
XMLDocument doc;
doc.Parse( str );
XMLTest( "Empty closed element", false, doc.Error() );
XMLElement* element = doc.RootElement();
element->SetText("darkness.");
XMLTest( "SetText() normal use (open/close).", "darkness.", element->GetText() );
element->SetText("blue flame.");
XMLTest( "SetText() replace.", "blue flame.", element->GetText() );
str = "<foo/>";
doc.Parse( str );
XMLTest( "Empty self-closed element", false, doc.Error() );
element = doc.RootElement();
element->SetText("The driver");
XMLTest( "SetText() normal use. (self-closing)", "The driver", element->GetText() );
element->SetText("<b>horses</b>");
XMLTest( "SetText() replace with tag-like text.", "<b>horses</b>", element->GetText() );
//doc.Print();
str = "<foo><bar>Text in nested element</bar></foo>";
doc.Parse( str );
XMLTest( "Text in nested element", false, doc.Error() );
element = doc.RootElement();
element->SetText("wolves");
XMLTest( "SetText() prefix to nested non-text children.", "wolves", element->GetText() );
str = "<foo/>";
doc.Parse( str );
XMLTest( "Empty self-closed element round 2", false, doc.Error() );
element = doc.RootElement();
element->SetText( "str" );
XMLTest( "SetText types", "str", element->GetText() );
element->SetText( 1 );
XMLTest( "SetText types", "1", element->GetText() );
element->SetText( 1U );
XMLTest( "SetText types", "1", element->GetText() );
element->SetText( true );
XMLTest( "SetText types", "true", element->GetText() );
element->SetText( 1.5f );
XMLTest( "SetText types", "1.5", element->GetText() );
element->SetText( 1.5 );
XMLTest( "SetText types", "1.5", element->GetText() );
}
// ---------- Attributes ---------
{
static const int64_t BIG = -123456789012345678;
static const uint64_t BIG_POS = 123456789012345678;
XMLDocument doc;
XMLElement* element = doc.NewElement("element");
doc.InsertFirstChild(element);
{
element->SetAttribute("attrib", int(-100));
{
int v = 0;
XMLError queryResult = element->QueryIntAttribute("attrib", &v);
XMLTest("Attribute: int", XML_SUCCESS, queryResult, true);
XMLTest("Attribute: int", -100, v, true);
}
{
int v = 0;
XMLError queryResult = element->QueryAttribute("attrib", &v);
XMLTest("Attribute: int", (int)XML_SUCCESS, queryResult, true);
XMLTest("Attribute: int", -100, v, true);
}
XMLTest("Attribute: int", -100, element->IntAttribute("attrib"), true);
}
{
element->SetAttribute("attrib", unsigned(100));
{
unsigned v = 0;
XMLError queryResult = element->QueryUnsignedAttribute("attrib", &v);
XMLTest("Attribute: unsigned", XML_SUCCESS, queryResult, true);
XMLTest("Attribute: unsigned", unsigned(100), v, true);
}
{
unsigned v = 0;
XMLError queryResult = element->QueryAttribute("attrib", &v);
XMLTest("Attribute: unsigned", (int)XML_SUCCESS, queryResult, true);
XMLTest("Attribute: unsigned", unsigned(100), v, true);
}
{
const char* v = "failed";
XMLError queryResult = element->QueryStringAttribute("not-attrib", &v);
XMLTest("Attribute: string default", false, queryResult == XML_SUCCESS);
queryResult = element->QueryStringAttribute("attrib", &v);
XMLTest("Attribute: string", XML_SUCCESS, queryResult, true);
XMLTest("Attribute: string", "100", v);
}
XMLTest("Attribute: unsigned", unsigned(100), element->UnsignedAttribute("attrib"), true);
}
{
element->SetAttribute("attrib", BIG);
{
int64_t v = 0;
XMLError queryResult = element->QueryInt64Attribute("attrib", &v);
XMLTest("Attribute: int64_t", XML_SUCCESS, queryResult, true);
XMLTest("Attribute: int64_t", BIG, v, true);
}
{
int64_t v = 0;
XMLError queryResult = element->QueryAttribute("attrib", &v);
XMLTest("Attribute: int64_t", (int)XML_SUCCESS, queryResult, true);
XMLTest("Attribute: int64_t", BIG, v, true);
}
XMLTest("Attribute: int64_t", BIG, element->Int64Attribute("attrib"), true);
}
{
element->SetAttribute("attrib", BIG_POS);
{
uint64_t v = 0;
XMLError queryResult = element->QueryUnsigned64Attribute("attrib", &v);
XMLTest("Attribute: uint64_t", XML_SUCCESS, queryResult, true);
XMLTest("Attribute: uint64_t", BIG_POS, v, true);
}
{
uint64_t v = 0;
XMLError queryResult = element->QueryAttribute("attrib", &v);
XMLTest("Attribute: uint64_t", (int)XML_SUCCESS, queryResult, true);
XMLTest("Attribute: uint64_t", BIG_POS, v, true);
}
XMLTest("Attribute: uint64_t", BIG_POS, element->Unsigned64Attribute("attrib"), true);
}
{
element->SetAttribute("attrib", true);
{
bool v = false;
XMLError queryResult = element->QueryBoolAttribute("attrib", &v);
XMLTest("Attribute: bool", XML_SUCCESS, queryResult, true);
XMLTest("Attribute: bool", true, v, true);
}
{
bool v = false;
XMLError queryResult = element->QueryAttribute("attrib", &v);
XMLTest("Attribute: bool", (int)XML_SUCCESS, queryResult, true);
XMLTest("Attribute: bool", true, v, true);
}
XMLTest("Attribute: bool", true, element->BoolAttribute("attrib"), true);
}
{
element->SetAttribute("attrib", true);
const char* result = element->Attribute("attrib");
XMLTest("Bool true is 'true'", "true", result);
XMLUtil::SetBoolSerialization("1", "0");
element->SetAttribute("attrib", true);
result = element->Attribute("attrib");
XMLTest("Bool true is '1'", "1", result);
XMLUtil::SetBoolSerialization(0, 0);
}
{
element->SetAttribute("attrib", 100.0);
{
double v = 0;
XMLError queryResult = element->QueryDoubleAttribute("attrib", &v);
XMLTest("Attribute: double", XML_SUCCESS, queryResult, true);
XMLTest("Attribute: double", 100.0, v, true);
}
{
double v = 0;
XMLError queryResult = element->QueryAttribute("attrib", &v);
XMLTest("Attribute: bool", (int)XML_SUCCESS, queryResult, true);
XMLTest("Attribute: double", 100.0, v, true);
}
XMLTest("Attribute: double", 100.0, element->DoubleAttribute("attrib"), true);
}
{
element->SetAttribute("attrib", 100.0f);
{
float v = 0;
XMLError queryResult = element->QueryFloatAttribute("attrib", &v);
XMLTest("Attribute: float", XML_SUCCESS, queryResult, true);
XMLTest("Attribute: float", 100.0f, v, true);
}
{
float v = 0;
XMLError queryResult = element->QueryAttribute("attrib", &v);
XMLTest("Attribute: float", (int)XML_SUCCESS, queryResult, true);
XMLTest("Attribute: float", 100.0f, v, true);
}
XMLTest("Attribute: float", 100.0f, element->FloatAttribute("attrib"), true);
}
{
element->SetText(BIG);
int64_t v = 0;
XMLError queryResult = element->QueryInt64Text(&v);
XMLTest("Element: int64_t", XML_SUCCESS, queryResult, true);
XMLTest("Element: int64_t", BIG, v, true);
}
{
element->SetText(BIG_POS);
uint64_t v = 0;
XMLError queryResult = element->QueryUnsigned64Text(&v);
XMLTest("Element: uint64_t", XML_SUCCESS, queryResult, true);
XMLTest("Element: uint64_t", BIG_POS, v, true);
}
}
// ---------- XMLPrinter stream mode ------
{
{
FILE* printerfp = fopen("resources/out/printer.xml", "w");
XMLTest("Open printer.xml", true, printerfp != 0);
XMLPrinter printer(printerfp);
printer.OpenElement("foo");
printer.PushAttribute("attrib-text", "text");
printer.PushAttribute("attrib-int", int(1));
printer.PushAttribute("attrib-unsigned", unsigned(2));
printer.PushAttribute("attrib-int64", int64_t(3));
printer.PushAttribute("attrib-uint64", uint64_t(37));
printer.PushAttribute("attrib-bool", true);
printer.PushAttribute("attrib-double", 4.0);
printer.CloseElement();
fclose(printerfp);
}
{
XMLDocument doc;
doc.LoadFile("resources/out/printer.xml");
XMLTest("XMLPrinter Stream mode: load", XML_SUCCESS, doc.ErrorID(), true);
const XMLDocument& cdoc = doc;
const XMLAttribute* attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-text");
XMLTest("attrib-text", "text", attrib->Value(), true);
attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-int");
XMLTest("attrib-int", int(1), attrib->IntValue(), true);
attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-unsigned");
XMLTest("attrib-unsigned", unsigned(2), attrib->UnsignedValue(), true);
attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-int64");
XMLTest("attrib-int64", int64_t(3), attrib->Int64Value(), true);
attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-uint64");
XMLTest("attrib-uint64", uint64_t(37), attrib->Unsigned64Value(), true);
attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-bool");
XMLTest("attrib-bool", true, attrib->BoolValue(), true);
attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-double");