source: icXML/icXML-devel/tests/src/UtilTests/CoreTests_BitSet.cpp

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

Add original Xerces tests and samples directories

File size: 7.6 KB
RevLine 
[2726]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_BitSet.cpp 470088 2006-11-01 20:35:12Z amassari $
20 */
21
22// ---------------------------------------------------------------------------
23//  XML4C2 Includes
24// ---------------------------------------------------------------------------
25#include "CoreTests.hpp"
26#include <xercesc/util/BitSet.hpp>
27
28
29
30// ---------------------------------------------------------------------------
31//  Local testing methods
32// ---------------------------------------------------------------------------
33static bool basicTests()
34{
35    //
36    //  Create a bitset with 32 bits. We just happen to know that this is
37    //  the unit of expansion, so it should come back with exactly that
38    //  number of bits of size.
39    //
40    BitSet setTest(32);
41
42    if (setTest.size() != 32)
43    {
44        XERCES_STD_QUALIFIER wcout  << L"    Ctor did not create set of correct size"
45                    << XERCES_STD_QUALIFIER endl;
46        return false;
47    }
48
49    //
50    //  Check the value of all of the bits and make sure that they all come
51    //  back zero.
52    //
53    const unsigned int count = setTest.size();
54    unsigned int index;
55    for (index = 0; index < count; index++)
56    {
57        if (setTest.get(index))
58        {
59            XERCES_STD_QUALIFIER wcout << L"    A bit's initial value was not zero"
60                       << XERCES_STD_QUALIFIER endl;
61            return false;
62        }
63    }
64
65    // Make sure that allAreCleared() agrees
66    if (!setTest.allAreCleared())
67    {
68        XERCES_STD_QUALIFIER wcout  << L"    allAreCleared() disagrees with individual bit gets"
69                    << XERCES_STD_QUALIFIER endl;
70        return false;
71    }
72
73    // Set and clear each bit and make sure that they come back right
74    for (index = 0; index < count; index++)
75    {
76        setTest.set(index);
77        if (!setTest.get(index))
78        {
79            XERCES_STD_QUALIFIER wcout << L"    Bit was set but get returned false"
80                       << XERCES_STD_QUALIFIER endl;
81            return false;
82        }
83
84        setTest.clear(index);
85        if (setTest.get(index))
86        {
87            XERCES_STD_QUALIFIER wcout  << L"    Bit was cleared but get returned true"
88                        << XERCES_STD_QUALIFIER endl;
89            return false;
90        }
91    }
92
93    // And once more make sure they are all zero
94    for (index = 0; index < count; index++)
95    {
96        if (setTest.get(index))
97        {
98            XERCES_STD_QUALIFIER wcout << L"    A bit remained set after clearing"
99                       << XERCES_STD_QUALIFIER endl;
100            return false;
101        }
102    }
103
104    //
105    //  Set some bits, then copy construct another bitset from this one. Then
106    //  see if they come out equal.
107    //
108    setTest.set(1);
109    setTest.set(16);
110    setTest.set(20);
111    setTest.set(24);
112
113    BitSet setTest2(setTest);
114    if (!setTest.equals(setTest2))
115    {
116        XERCES_STD_QUALIFIER wcout  << L"    Copy ctor did not create equal sets"
117                    << XERCES_STD_QUALIFIER endl;
118        return false;
119    }
120
121    // Clear all bits of the copy and make sure they are all cleared
122    setTest2.clearAll();
123    for (index = 0; index < count; index++)
124    {
125        if (setTest2.get(index))
126        {
127            XERCES_STD_QUALIFIER wcout  << L"    clearAll() did not clear all bits"
128                        << XERCES_STD_QUALIFIER endl;
129            return false;
130        }
131    }
132
133    // Set a bit beyond the current size
134    setTest2.set(32);
135
136    // Make sure it expanded
137    if (setTest2.size() != 64)
138    {
139        XERCES_STD_QUALIFIER wcout  << L"    Set of bit beyond size did not expand"
140                    << XERCES_STD_QUALIFIER endl;
141        return false;
142    }
143
144    // Set all the bits
145    for (index = 0; index < count; index++)
146        setTest.set(index);
147
148    // Make sure that allAreSet() sees them all set
149    if (!setTest.allAreSet())
150    {
151        XERCES_STD_QUALIFIER wcout  << L"    After setting all bits, allAreSet() returned false"
152                    << XERCES_STD_QUALIFIER endl;
153        return false;
154    }
155
156    return true;
157}
158
159static bool bitopsTests()
160{
161    // Create a bit set to test
162    BitSet setTest(48);
163
164    // Set some bits
165    setTest.set(1);
166    setTest.set(10);
167    setTest.set(16);
168    setTest.set(21);
169    setTest.set(33);
170    setTest.set(41);
171
172    // Create another set to do ops on
173    BitSet setTest2(48);
174
175    // Or with the new set
176    setTest2.orWith(setTest);
177
178    // They should be equal now
179    if (!setTest.equals(setTest2))
180    {
181        XERCES_STD_QUALIFIER wcout  << L"    OR of set with empty set did not create equal sets"
182                    << XERCES_STD_QUALIFIER endl;
183        return false;
184    }
185
186    // Xor with original which should get back an empty set
187    setTest2.xorWith(setTest);
188    if (!setTest2.allAreCleared())
189    {
190        XERCES_STD_QUALIFIER wcout  << L"    XOR against original set did not get back original"
191                    << XERCES_STD_QUALIFIER endl;
192        return false;
193    }
194
195    // And them, which should have no effect
196    setTest2.andWith(setTest);
197    if (!setTest2.allAreCleared())
198    {
199        XERCES_STD_QUALIFIER wcout << L"    AND against empty set changed bits" << XERCES_STD_QUALIFIER endl;
200        return false;
201    }
202
203    return true;
204}
205
206
207// ---------------------------------------------------------------------------
208//  Test entry point
209// ---------------------------------------------------------------------------
210bool testBitSet()
211{
212    XERCES_STD_QUALIFIER wcout  << L"----------------------------------\n"
213                << L"Testing BitSet class\n"
214                << L"----------------------------------" << XERCES_STD_QUALIFIER endl;
215
216    bool retVal = true;
217
218    try
219    {
220        XERCES_STD_QUALIFIER wcout << L"Testing basic BitSet methods" << XERCES_STD_QUALIFIER endl;
221        if (!basicTests())
222        {
223            XERCES_STD_QUALIFIER wcout << L"Bitset basic test methods failed" << XERCES_STD_QUALIFIER endl;
224            retVal = false;
225        }
226         else
227        {
228            XERCES_STD_QUALIFIER wcout << L"Bitset basic tests passed" << XERCES_STD_QUALIFIER endl;
229        }
230        XERCES_STD_QUALIFIER wcout << XERCES_STD_QUALIFIER endl;
231
232        XERCES_STD_QUALIFIER wcout << L"Testing BitSet logical bit ops methods" << XERCES_STD_QUALIFIER endl;
233        if (!bitopsTests())
234        {
235            XERCES_STD_QUALIFIER wcout << L"Bitset logical bit ops failed" << XERCES_STD_QUALIFIER endl;
236            retVal = false;
237        }
238         else
239        {
240            XERCES_STD_QUALIFIER wcout << L"Bitset logical bit ops passed" << XERCES_STD_QUALIFIER endl;
241        }
242        XERCES_STD_QUALIFIER wcout << XERCES_STD_QUALIFIER endl;
243    }
244
245    catch(const XMLException& toCatch)
246    {
247        XERCES_STD_QUALIFIER wcout << L"  ERROR: Unexpected exception!\n   Msg: "
248                << toCatch.getMessage() << XERCES_STD_QUALIFIER endl;
249        return false;
250    }
251    return retVal;
252}
Note: See TracBrowser for help on using the repository browser.