~baltix/+junk/irrlicht-test

« back to all changes in this revision

Viewing changes to tests/testXML.cpp

  • Committer: Mantas Kriaučiūnas
  • Date: 2011-07-18 13:06:25 UTC
  • Revision ID: mantas@akl.lt-20110718130625-c5pvifp61e7kj1ol
Included whole irrlicht SVN libraries to work around launchpad recipe issue with quilt, see https://answers.launchpad.net/launchpad/+question/165193

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (C) 2009-2011 Christian Stehno
 
2
// No rights reserved: this software is in the public domain.
 
3
 
 
4
#include "testUtils.h"
 
5
 
 
6
using namespace irr;
 
7
using namespace core;
 
8
 
 
9
bool simple_xml( irr::io::IFileSystem * fs )
 
10
{
 
11
        io::IXMLReaderUTF8* reader = fs->createXMLReaderUTF8("media/test.xml");
 
12
        if (!reader)
 
13
        {
 
14
                logTestString("Could not create XML reader.\n");
 
15
                return false;
 
16
        }
 
17
 
 
18
        const core::stringc expected[] = {
 
19
                "a", "b", "c"
 
20
        };
 
21
 
 
22
        bool retVal = true;
 
23
        u32 i=0;
 
24
        while(reader->read())
 
25
        {
 
26
                if (reader->getNodeType() == io::EXN_ELEMENT)
 
27
                {
 
28
                        if (expected[i++] != reader->getNodeName())
 
29
                        {
 
30
                                logTestString("Did not find expected string in XML element name.\n");
 
31
                                retVal = false;
 
32
                                break;
 
33
                        }
 
34
                }
 
35
        }
 
36
 
 
37
        reader->drop();
 
38
        return retVal;
 
39
}
 
40
 
 
41
// CDATA should return everything between "![CDATA[" and "]]>" as it's in the file
 
42
bool cdata( irr::io::IFileSystem * fs )
 
43
{
 
44
        io::IXMLReaderUTF8* reader = fs->createXMLReaderUTF8("media/cdata.xml");
 
45
        if (!reader)
 
46
        {
 
47
                logTestString("Could not create XML reader.\n");
 
48
                return false;
 
49
        }
 
50
 
 
51
        const core::stringc textNode("text");
 
52
        core::array< core::stringc > compareStrings;
 
53
        compareStrings.push_back("simple");
 
54
        compareStrings.push_back("");
 
55
        compareStrings.push_back("] ]> ");
 
56
        compareStrings.push_back("]\n]> ");
 
57
        compareStrings.push_back("\nNewlines\n\tand tabs\n\t\tgogogo");
 
58
        compareStrings.push_back("&&#@#$%*()@#$%*()#$%*(");
 
59
        compareStrings.push_back("& &  && &&& &a &ü &ä &ö &&#");
 
60
 
 
61
        bool result = true;
 
62
        size_t count = 0;
 
63
        while(reader->read())
 
64
        {
 
65
                if (reader->getNodeType() == io::EXN_ELEMENT)
 
66
                {
 
67
                        if ( core::stringc(reader->getNodeName()) == textNode )
 
68
                        {
 
69
                                while(reader->read())
 
70
                                {
 
71
                                        if (reader->getNodeType() == io::EXN_CDATA)
 
72
                                        {
 
73
                                                core::stringc data = reader->getNodeData();
 
74
                                                core::stringc name = reader->getNodeName();
 
75
                                                if ( count == compareStrings.size() )
 
76
                                                {
 
77
                                                        logTestString("too many cdata elements for reading in %s:%d\n", __FILE__, __LINE__);
 
78
                                                }
 
79
                                                else if ( count < compareStrings.size() )
 
80
                                                {
 
81
                                                        core::stringc cmpString(compareStrings[count]);
 
82
                                                        
 
83
                                                        // some (unused) variables to ease debugging
 
84
                                                        // const c8* dataRaw = data.c_str();
 
85
                                                        // const c8* cmpRaw = cmpString.c_str();
 
86
                                                        if ( cmpString != data )
 
87
                                                        {
 
88
                                                                result = false;
 
89
                                                                logTestString("cdata read failed for string %d in %s:%d\n", count, __FILE__, __LINE__);
 
90
                                                        }
 
91
                                                }
 
92
                                                ++count;
 
93
                                        }
 
94
                                        if ( reader->getNodeType() == io::EXN_ELEMENT_END )
 
95
                                        {
 
96
                                                break;
 
97
                                        }
 
98
                                }
 
99
                        }
 
100
                }
 
101
        }
 
102
 
 
103
        reader->drop();
 
104
        return result;
 
105
}
 
106
 
 
107
bool attributeValues(irr::io::IFileSystem * fs)
 
108
{
 
109
        io::IXMLReaderUTF8* reader = fs->createXMLReaderUTF8("media/attributes.xml");
 
110
        if (!reader)
 
111
        {
 
112
                logTestString("Could not create XML reader.\n");
 
113
                return false;
 
114
        }
 
115
 
 
116
        bool result = true;
 
117
        bool hasNode = false;
 
118
        while (reader->read())
 
119
        {
 
120
                if (io::EXN_ELEMENT == reader->getNodeType() )
 
121
                {
 
122
                        if ( core::stringc(reader->getNodeName()) == core::stringc("element_position") )
 
123
                        {
 
124
                                hasNode = true;
 
125
                                int id1 = reader->getAttributeValueAsInt("id1");
 
126
                                if ( id1 != 152722522 )
 
127
                                {
 
128
                                        logTestString("id1 is %d in %s:%d\n", id1, __FILE__, __LINE__);
 
129
                                        result = false;
 
130
                                }
 
131
                                int id2 = reader->getAttributeValueAsInt("id2");
 
132
                                result &= id2 == 3;
 
133
                                int x = reader->getAttributeValueAsInt("x");
 
134
                                result &= x == 301;
 
135
                                int y = reader->getAttributeValueAsInt("y");
 
136
                                result &= y == 118;
 
137
                        }
 
138
                }
 
139
        }
 
140
 
 
141
        if ( !hasNode )
 
142
        {
 
143
                logTestString("missing node in xml in %s:%d\n", __FILE__, __LINE__);
 
144
                return false;
 
145
        }
 
146
 
 
147
        reader->drop();
 
148
        return result;
 
149
}
 
150
 
 
151
/** Tests for XML handling */
 
152
bool testXML(void)
 
153
{
 
154
        IrrlichtDevice *device = createDevice(video::EDT_NULL, dimension2du(400, 200));
 
155
 
 
156
        bool result = true;
 
157
 
 
158
        logTestString("Test simple XML reader features.\n");
 
159
        result &= simple_xml(device->getFileSystem());
 
160
        logTestString("Test XML reader CDATA support.\n");
 
161
        result &= cdata(device->getFileSystem());
 
162
        logTestString("Test XML reader attribute support.\n");
 
163
        result &= attributeValues(device->getFileSystem());     
 
164
 
 
165
        device->closeDevice();
 
166
        device->run();
 
167
        device->drop();
 
168
        return result;
 
169
}