added wxXmlNode::InsertChildAfter and use it for (much) faster XML parsing (based...
[wxWidgets.git] / tests / xml / xmltest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/xml/xmltest.cpp
3 // Purpose: XML classes unit test
4 // Author: Vaclav Slavik
5 // Created: 2008-03-29
6 // RCS-ID: $Id$
7 // Copyright: (c) 2008 Vaclav Slavik
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #ifndef WX_PRECOMP
21 #include "wx/wx.h"
22 #endif // WX_PRECOMP
23
24 #include "wx/xml/xml.h"
25
26 #include <stdarg.h>
27
28 // ----------------------------------------------------------------------------
29 // helpers for testing XML tree
30 // ----------------------------------------------------------------------------
31
32 namespace
33 {
34
35 void CheckXml(wxXmlNode *n, ...)
36 {
37 va_list args;
38 va_start(args, n);
39
40 wxXmlNode *child = n->GetChildren();
41
42 for (;;)
43 {
44 const char *childName = va_arg(args, char*);
45 if ( childName == NULL )
46 break;
47
48 CPPUNIT_ASSERT( child );
49 WX_ASSERT_STR_EQUAL( childName, child->GetName() );
50 CPPUNIT_ASSERT( child->GetChildren() == NULL );
51 CPPUNIT_ASSERT( child->GetParent() == n );
52
53 child = child->GetNext();
54 }
55
56 va_end(args);
57
58 CPPUNIT_ASSERT( child == NULL ); // no more children
59 }
60
61 } // anon namespace
62
63 // ----------------------------------------------------------------------------
64 // test class
65 // ----------------------------------------------------------------------------
66
67 class XmlTestCase : public CppUnit::TestCase
68 {
69 public:
70 XmlTestCase() {}
71
72 private:
73 CPPUNIT_TEST_SUITE( XmlTestCase );
74 CPPUNIT_TEST( InsertChild );
75 CPPUNIT_TEST( InsertChildAfter );
76 CPPUNIT_TEST_SUITE_END();
77
78 void InsertChild();
79 void InsertChildAfter();
80
81 DECLARE_NO_COPY_CLASS(XmlTestCase)
82 };
83
84 // register in the unnamed registry so that these tests are run by default
85 CPPUNIT_TEST_SUITE_REGISTRATION( XmlTestCase );
86
87 // also include in it's own registry so that these tests can be run alone
88 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( XmlTestCase, "XmlTestCase" );
89
90 void XmlTestCase::InsertChild()
91 {
92 wxXmlNode *root = new wxXmlNode(wxXML_ELEMENT_NODE, "root");
93 root->AddChild(new wxXmlNode(wxXML_ELEMENT_NODE, "1"));
94 wxXmlNode *two = new wxXmlNode(wxXML_ELEMENT_NODE, "2");
95 root->AddChild(two);
96 root->AddChild(new wxXmlNode(wxXML_ELEMENT_NODE, "3"));
97 CheckXml(root, "1", "2", "3", NULL);
98
99 // check inserting in front:
100 root->InsertChild(new wxXmlNode(wxXML_ELEMENT_NODE, "A"), NULL);
101 CheckXml(root, "A", "1", "2", "3", NULL);
102 root->InsertChild(new wxXmlNode(wxXML_ELEMENT_NODE, "B"), root->GetChildren());
103 CheckXml(root, "B", "A", "1", "2", "3", NULL);
104
105 // and in the middle:
106 root->InsertChild(new wxXmlNode(wxXML_ELEMENT_NODE, "C"), two);
107 CheckXml(root, "B", "A", "1", "C", "2", "3", NULL);
108 }
109
110 void XmlTestCase::InsertChildAfter()
111 {
112 wxXmlNode *root = new wxXmlNode(wxXML_ELEMENT_NODE, "root");
113
114 root->InsertChildAfter(new wxXmlNode(wxXML_ELEMENT_NODE, "1"), NULL);
115 CheckXml(root, "1", NULL);
116
117 wxXmlNode *two = new wxXmlNode(wxXML_ELEMENT_NODE, "2");
118 root->AddChild(two);
119 wxXmlNode *three = new wxXmlNode(wxXML_ELEMENT_NODE, "3");
120 root->AddChild(three);
121 CheckXml(root, "1", "2", "3", NULL);
122
123 // check inserting in the middle:
124 root->InsertChildAfter(new wxXmlNode(wxXML_ELEMENT_NODE, "A"), root->GetChildren());
125 CheckXml(root, "1", "A", "2", "3", NULL);
126 root->InsertChildAfter(new wxXmlNode(wxXML_ELEMENT_NODE, "B"), two);
127 CheckXml(root, "1", "A", "2", "B", "3", NULL);
128
129 // and at the end:
130 root->InsertChildAfter(new wxXmlNode(wxXML_ELEMENT_NODE, "C"), three);
131 CheckXml(root, "1", "A", "2", "B", "3", "C", NULL);
132 }