source: icXML/icXML-devel/tests/src/UtilTests/CoreTests_RefArray.cpp @ 2726

Last change on this file since 2726 was 2726, checked in by cameron, 7 years ago

Add original Xerces tests and samples directories

File size: 6.9 KB
Line 
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/*
19 * $Id: CoreTests_RefArray.cpp 470088 2006-11-01 20:35:12Z amassari $
20 */
21
22// ---------------------------------------------------------------------------
23//  XML4C2 includes
24// ---------------------------------------------------------------------------
25#include "CoreTests.hpp"
26#include <xercesc/util/RefArrayOf.hpp>
27#include <xercesc/util/ArrayIndexOutOfBoundsException.hpp>
28
29
30
31// ---------------------------------------------------------------------------
32//  Force a full instantiation of our array and its enumerator, just to
33//  insure that all methods get instantiated and compiled.
34// ---------------------------------------------------------------------------
35template RefArrayOf<int>;
36template RefArrayEnumerator<int>;
37
38
39// ---------------------------------------------------------------------------
40//  Local functions
41// ---------------------------------------------------------------------------
42static bool constructorTests()
43{
44    // Some values to test with
45    double testVals[16];
46    unsigned int index;
47    for (index = 0; index < 16; index++)
48        testVals[index] = index;
49
50    // Do a basic constructor with just the count of elements
51    RefArrayOf<double> testArray1(255);
52
53    // Make sure that it has the right initial size
54    if (testArray1.length() != 255)
55    {
56        XERCES_STD_QUALIFIER wcout  << L"    The ctor created wrong length() value"
57                    << XERCES_STD_QUALIFIER endl;
58        return false;
59    }
60
61    // Copy construct another array from it and test the length
62    RefArrayOf<double> testArray2(testArray1);
63
64    if (testArray2.length() != 255)
65    {
66        XERCES_STD_QUALIFIER wcout  << L"    The copy ctor created wrong length() value"
67                    << XERCES_STD_QUALIFIER endl;
68        return false;
69    }
70
71    // Test the equality of the two arrays
72    if (testArray1 != testArray2)
73    {
74        XERCES_STD_QUALIFIER wcout  << L"    The copy ctor created unequal arrays"
75                    << XERCES_STD_QUALIFIER endl;
76        return false;
77    }
78
79    //
80    //  Do another one where we provide the initial values.
81    //
82    double* initValues[16];
83    for (index = 0; index < 16; index++)
84        initValues[index ] = &testVals[index];
85    RefArrayOf<double> testArray3(initValues, 16);
86
87    if (testArray3.length() != 16)
88    {
89        XERCES_STD_QUALIFIER wcout  << L"    The init values ctor created wrong length() value"
90                    << XERCES_STD_QUALIFIER endl;
91        return false;
92    }
93
94    // Make sure the initial values are correct
95    for (index = 0; index < 16; index++)
96    {
97        if (*testArray3[index] != (double)index)
98        {
99            XERCES_STD_QUALIFIER wcout  << L"    The init values ctor did not init contents correctly"
100                        << XERCES_STD_QUALIFIER endl;
101            return false;
102        }
103    }
104
105    //
106    //  Create another array of a different size and assign one of the
107    //  existing ones to it and make sure that they are equal.
108    //
109    RefArrayOf<double> testArray4(15);
110    testArray4 = testArray3;
111
112    if (testArray4 != testArray3)
113    {
114        XERCES_STD_QUALIFIER wcout  << L"    Assignment did not create equal arrays"
115                    << XERCES_STD_QUALIFIER endl;
116        return false;
117    }
118
119    return true;
120}
121
122
123static bool accessTests()
124{
125    // Some values to test with
126    unsigned int testVals[16];
127    unsigned int index;
128    for (index = 0; index < 16; index++)
129        testVals[index] = index;
130
131    RefArrayOf<unsigned int> testArray1(16);
132
133    // Fill in the array
134    for (index = 0; index < 16; index++)
135        testArray1[index] = &testVals[index];
136
137    // Read them back again
138    for (index = 0; index < 16; index++)
139    {
140        if (testArray1[index] != &testVals[index])
141        {
142            XERCES_STD_QUALIFIER wcout  << L"    Failed to read back values just set"
143                        << XERCES_STD_QUALIFIER endl;
144            return false;
145        }
146    }
147
148    // Make sure we get the expected array index error
149    bool caughtIt = false;
150    try
151    {
152        testArray1[16];
153    }
154
155    catch(const ArrayIndexOutOfBoundsException&)
156    {
157        caughtIt = true;
158    }
159
160    if (!caughtIt)
161    {
162        XERCES_STD_QUALIFIER wcout << L"    Failed to catch index error" << XERCES_STD_QUALIFIER endl;
163        return false;
164    }
165
166    return true;
167}
168
169
170// ---------------------------------------------------------------------------
171//  Test entry point
172// ---------------------------------------------------------------------------
173bool testRefArray()
174{
175    XERCES_STD_QUALIFIER wcout  << L"----------------------------------\n"
176                << L"Testing RefArrayOf template class\n"
177                << L"----------------------------------" << XERCES_STD_QUALIFIER endl;
178
179    bool retVal = true;
180
181    try
182    {
183        // Call other local methods to do specific tests
184        XERCES_STD_QUALIFIER wcout << L"Testing RefArrayOf contructors" << XERCES_STD_QUALIFIER endl;
185        if (!constructorTests())
186        {
187            XERCES_STD_QUALIFIER wcout  << L"RefArrayOf constructor tests failed"
188                        << XERCES_STD_QUALIFIER endl;
189            retVal = false;
190        }
191         else
192        {
193            XERCES_STD_QUALIFIER wcout  << L"RefArrayOf constructor tests passed"
194                        << XERCES_STD_QUALIFIER endl;
195        }
196        XERCES_STD_QUALIFIER wcout << XERCES_STD_QUALIFIER endl;
197
198        XERCES_STD_QUALIFIER wcout << L"Testing RefArrayOf element access" << XERCES_STD_QUALIFIER endl;
199        if (!accessTests())
200        {
201            XERCES_STD_QUALIFIER wcout  << L"RefArrayOf element access tests failed"
202                        << XERCES_STD_QUALIFIER endl;
203            retVal = false;
204        }
205         else
206        {
207            XERCES_STD_QUALIFIER wcout  << L"RefArrayOf element access tests passed"
208                        << XERCES_STD_QUALIFIER endl;
209        }
210        XERCES_STD_QUALIFIER wcout << XERCES_STD_QUALIFIER endl;
211    }
212
213    catch(const XMLException& toCatch)
214    {
215        XERCES_STD_QUALIFIER wcout  << L"  ERROR: Unexpected exception!\n   Msg: "
216                    << toCatch.getMessage() << XERCES_STD_QUALIFIER endl;
217        return false;
218    }
219    return retVal;
220}
Note: See TracBrowser for help on using the repository browser.