001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.util;
019:
020: import junit.framework.*;
021:
022: /**
023: * Class to test BitField functionality
024: *
025: * @author Marc Johnson
026: * @author Glen Stampoultzis (gstamp@iprimus.com.au)
027: */
028:
029: public class TestBitField extends TestCase {
030: private static BitField bf_multi = BitFieldFactory
031: .getInstance(0x3F80);
032: private static BitField bf_single = BitFieldFactory
033: .getInstance(0x4000);
034:
035: /**
036: * Constructor TestBitField
037: *
038: * @param name
039: */
040:
041: public TestBitField(String name) {
042: super (name);
043: }
044:
045: /**
046: * test the getValue() method
047: */
048:
049: public void testGetValue() {
050: assertEquals(bf_multi.getValue(-1), 127);
051: assertEquals(bf_multi.getValue(0), 0);
052: assertEquals(bf_single.getValue(-1), 1);
053: assertEquals(bf_single.getValue(0), 0);
054: }
055:
056: /**
057: * test the getShortValue() method
058: */
059:
060: public void testGetShortValue() {
061: assertEquals(bf_multi.getShortValue((short) -1), (short) 127);
062: assertEquals(bf_multi.getShortValue((short) 0), (short) 0);
063: assertEquals(bf_single.getShortValue((short) -1), (short) 1);
064: assertEquals(bf_single.getShortValue((short) 0), (short) 0);
065: }
066:
067: /**
068: * test the getRawValue() method
069: */
070:
071: public void testGetRawValue() {
072: assertEquals(bf_multi.getRawValue(-1), 0x3F80);
073: assertEquals(bf_multi.getRawValue(0), 0);
074: assertEquals(bf_single.getRawValue(-1), 0x4000);
075: assertEquals(bf_single.getRawValue(0), 0);
076: }
077:
078: /**
079: * test the getShortRawValue() method
080: */
081:
082: public void testGetShortRawValue() {
083: assertEquals(bf_multi.getShortRawValue((short) -1),
084: (short) 0x3F80);
085: assertEquals(bf_multi.getShortRawValue((short) 0), (short) 0);
086: assertEquals(bf_single.getShortRawValue((short) -1),
087: (short) 0x4000);
088: assertEquals(bf_single.getShortRawValue((short) 0), (short) 0);
089: }
090:
091: /**
092: * test the isSet() method
093: */
094:
095: public void testIsSet() {
096: assertTrue(!bf_multi.isSet(0));
097: for (int j = 0x80; j <= 0x3F80; j += 0x80) {
098: assertTrue(bf_multi.isSet(j));
099: }
100: assertTrue(!bf_single.isSet(0));
101: assertTrue(bf_single.isSet(0x4000));
102: }
103:
104: /**
105: * test the isAllSet() method
106: */
107:
108: public void testIsAllSet() {
109: for (int j = 0; j < 0x3F80; j += 0x80) {
110: assertTrue(!bf_multi.isAllSet(j));
111: }
112: assertTrue(bf_multi.isAllSet(0x3F80));
113: assertTrue(!bf_single.isAllSet(0));
114: assertTrue(bf_single.isAllSet(0x4000));
115: }
116:
117: /**
118: * test the setValue() method
119: */
120:
121: public void testSetValue() {
122: for (int j = 0; j < 128; j++) {
123: assertEquals(bf_multi.getValue(bf_multi.setValue(0, j)), j);
124: assertEquals(bf_multi.setValue(0, j), j << 7);
125: }
126:
127: // verify that excess bits are stripped off
128: assertEquals(bf_multi.setValue(0x3f80, 128), 0);
129: for (int j = 0; j < 2; j++) {
130: assertEquals(bf_single.getValue(bf_single.setValue(0, j)),
131: j);
132: assertEquals(bf_single.setValue(0, j), j << 14);
133: }
134:
135: // verify that excess bits are stripped off
136: assertEquals(bf_single.setValue(0x4000, 2), 0);
137: }
138:
139: /**
140: * test the setShortValue() method
141: */
142:
143: public void testSetShortValue() {
144: for (int j = 0; j < 128; j++) {
145: assertEquals(bf_multi.getShortValue(bf_multi.setShortValue(
146: (short) 0, (short) j)), (short) j);
147: assertEquals(bf_multi.setShortValue((short) 0, (short) j),
148: (short) (j << 7));
149: }
150:
151: // verify that excess bits are stripped off
152: assertEquals(bf_multi
153: .setShortValue((short) 0x3f80, (short) 128), (short) 0);
154: for (int j = 0; j < 2; j++) {
155: assertEquals(bf_single.getShortValue(bf_single
156: .setShortValue((short) 0, (short) j)), (short) j);
157: assertEquals(bf_single.setShortValue((short) 0, (short) j),
158: (short) (j << 14));
159: }
160:
161: // verify that excess bits are stripped off
162: assertEquals(
163: bf_single.setShortValue((short) 0x4000, (short) 2),
164: (short) 0);
165: }
166:
167: public void testByte() {
168: assertEquals(1, BitFieldFactory.getInstance(1).setByteBoolean(
169: (byte) 0, true));
170: assertEquals(2, BitFieldFactory.getInstance(2).setByteBoolean(
171: (byte) 0, true));
172: assertEquals(4, BitFieldFactory.getInstance(4).setByteBoolean(
173: (byte) 0, true));
174: assertEquals(8, BitFieldFactory.getInstance(8).setByteBoolean(
175: (byte) 0, true));
176: assertEquals(16, BitFieldFactory.getInstance(16)
177: .setByteBoolean((byte) 0, true));
178: assertEquals(32, BitFieldFactory.getInstance(32)
179: .setByteBoolean((byte) 0, true));
180: assertEquals(64, BitFieldFactory.getInstance(64)
181: .setByteBoolean((byte) 0, true));
182: assertEquals(-128, BitFieldFactory.getInstance(128)
183: .setByteBoolean((byte) 0, true));
184: assertEquals(0, BitFieldFactory.getInstance(1).setByteBoolean(
185: (byte) 1, false));
186: assertEquals(0, BitFieldFactory.getInstance(2).setByteBoolean(
187: (byte) 2, false));
188: assertEquals(0, BitFieldFactory.getInstance(4).setByteBoolean(
189: (byte) 4, false));
190: assertEquals(0, BitFieldFactory.getInstance(8).setByteBoolean(
191: (byte) 8, false));
192: assertEquals(0, BitFieldFactory.getInstance(16).setByteBoolean(
193: (byte) 16, false));
194: assertEquals(0, BitFieldFactory.getInstance(32).setByteBoolean(
195: (byte) 32, false));
196: assertEquals(0, BitFieldFactory.getInstance(64).setByteBoolean(
197: (byte) 64, false));
198: assertEquals(0, BitFieldFactory.getInstance(128)
199: .setByteBoolean((byte) 128, false));
200: assertEquals(-2, BitFieldFactory.getInstance(1).setByteBoolean(
201: (byte) 255, false));
202: byte clearedBit = BitFieldFactory.getInstance(0x40)
203: .setByteBoolean((byte) -63, false);
204:
205: assertEquals(false, BitFieldFactory.getInstance(0x40).isSet(
206: clearedBit));
207: }
208:
209: /**
210: * test the clear() method
211: */
212:
213: public void testClear() {
214: assertEquals(bf_multi.clear(-1), 0xFFFFC07F);
215: assertEquals(bf_single.clear(-1), 0xFFFFBFFF);
216: }
217:
218: /**
219: * test the clearShort() method
220: */
221:
222: public void testClearShort() {
223: assertEquals(bf_multi.clearShort((short) -1), (short) 0xC07F);
224: assertEquals(bf_single.clearShort((short) -1), (short) 0xBFFF);
225: }
226:
227: /**
228: * test the set() method
229: */
230:
231: public void testSet() {
232: assertEquals(bf_multi.set(0), 0x3F80);
233: assertEquals(bf_single.set(0), 0x4000);
234: }
235:
236: /**
237: * test the setShort() method
238: */
239:
240: public void testSetShort() {
241: assertEquals(bf_multi.setShort((short) 0), (short) 0x3F80);
242: assertEquals(bf_single.setShort((short) 0), (short) 0x4000);
243: }
244:
245: /**
246: * test the setBoolean() method
247: */
248:
249: public void testSetBoolean() {
250: assertEquals(bf_multi.set(0), bf_multi.setBoolean(0, true));
251: assertEquals(bf_single.set(0), bf_single.setBoolean(0, true));
252: assertEquals(bf_multi.clear(-1), bf_multi.setBoolean(-1, false));
253: assertEquals(bf_single.clear(-1), bf_single.setBoolean(-1,
254: false));
255: }
256:
257: /**
258: * test the setShortBoolean() method
259: */
260:
261: public void testSetShortBoolean() {
262: assertEquals(bf_multi.setShort((short) 0), bf_multi
263: .setShortBoolean((short) 0, true));
264: assertEquals(bf_single.setShort((short) 0), bf_single
265: .setShortBoolean((short) 0, true));
266: assertEquals(bf_multi.clearShort((short) -1), bf_multi
267: .setShortBoolean((short) -1, false));
268: assertEquals(bf_single.clearShort((short) -1), bf_single
269: .setShortBoolean((short) -1, false));
270: }
271:
272: /**
273: * main method to run the unit tests
274: *
275: * @param ignored_args
276: */
277:
278: public static void main(String[] ignored_args) {
279: System.out.println("Testing util.BitField functionality");
280: junit.textui.TestRunner.run(TestBitField.class);
281: }
282: }
|