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: package org.apache.commons.lang;
018:
019: import junit.framework.Test;
020: import junit.framework.TestCase;
021: import junit.framework.TestSuite;
022: import junit.textui.TestRunner;
023:
024: /**
025: * Class to test BitField functionality
026: *
027: * @author Scott Sanders
028: * @author Marc Johnson
029: * @author Glen Stampoultzis
030: * @version $Id: BitFieldTest.java 437554 2006-08-28 06:21:41Z bayard $
031: */
032: public class BitFieldTest extends TestCase {
033:
034: public static void main(String[] args) {
035: TestRunner.run(suite());
036: }
037:
038: public static Test suite() {
039: TestSuite suite = new TestSuite(BitFieldTest.class);
040: suite.setName("BitField Tests");
041: return suite;
042: }
043:
044: private static BitField bf_multi = new BitField(0x3F80);
045: private static BitField bf_single = new BitField(0x4000);
046: private static BitField bf_zero = new BitField(0);
047:
048: /**
049: * Constructor BitFieldTest
050: *
051: * @param name
052: */
053: public BitFieldTest(String name) {
054: super (name);
055: }
056:
057: /**
058: * test the getValue() method
059: */
060: public void testGetValue() {
061: assertEquals(bf_multi.getValue(-1), 127);
062: assertEquals(bf_multi.getValue(0), 0);
063: assertEquals(bf_single.getValue(-1), 1);
064: assertEquals(bf_single.getValue(0), 0);
065: assertEquals(bf_zero.getValue(-1), 0);
066: assertEquals(bf_zero.getValue(0), 0);
067: }
068:
069: /**
070: * test the getShortValue() method
071: */
072: public void testGetShortValue() {
073: assertEquals(bf_multi.getShortValue((short) -1), (short) 127);
074: assertEquals(bf_multi.getShortValue((short) 0), (short) 0);
075: assertEquals(bf_single.getShortValue((short) -1), (short) 1);
076: assertEquals(bf_single.getShortValue((short) 0), (short) 0);
077: assertEquals(bf_zero.getShortValue((short) -1), (short) 0);
078: assertEquals(bf_zero.getShortValue((short) 0), (short) 0);
079: }
080:
081: /**
082: * test the getRawValue() method
083: */
084: public void testGetRawValue() {
085: assertEquals(bf_multi.getRawValue(-1), 0x3F80);
086: assertEquals(bf_multi.getRawValue(0), 0);
087: assertEquals(bf_single.getRawValue(-1), 0x4000);
088: assertEquals(bf_single.getRawValue(0), 0);
089: assertEquals(bf_zero.getRawValue(-1), 0);
090: assertEquals(bf_zero.getRawValue(0), 0);
091: }
092:
093: /**
094: * test the getShortRawValue() method
095: */
096: public void testGetShortRawValue() {
097: assertEquals(bf_multi.getShortRawValue((short) -1),
098: (short) 0x3F80);
099: assertEquals(bf_multi.getShortRawValue((short) 0), (short) 0);
100: assertEquals(bf_single.getShortRawValue((short) -1),
101: (short) 0x4000);
102: assertEquals(bf_single.getShortRawValue((short) 0), (short) 0);
103: assertEquals(bf_zero.getShortRawValue((short) -1), (short) 0);
104: assertEquals(bf_zero.getShortRawValue((short) 0), (short) 0);
105: }
106:
107: /**
108: * test the isSet() method
109: */
110: public void testIsSet() {
111: assertTrue(!bf_multi.isSet(0));
112: assertTrue(!bf_zero.isSet(0));
113: for (int j = 0x80; j <= 0x3F80; j += 0x80) {
114: assertTrue(bf_multi.isSet(j));
115: }
116: for (int j = 0x80; j <= 0x3F80; j += 0x80) {
117: assertTrue(!bf_zero.isSet(j));
118: }
119: assertTrue(!bf_single.isSet(0));
120: assertTrue(bf_single.isSet(0x4000));
121: }
122:
123: /**
124: * test the isAllSet() method
125: */
126: public void testIsAllSet() {
127: for (int j = 0; j < 0x3F80; j += 0x80) {
128: assertTrue(!bf_multi.isAllSet(j));
129: assertTrue(bf_zero.isAllSet(j));
130: }
131: assertTrue(bf_multi.isAllSet(0x3F80));
132: assertTrue(!bf_single.isAllSet(0));
133: assertTrue(bf_single.isAllSet(0x4000));
134: }
135:
136: /**
137: * test the setValue() method
138: */
139: public void testSetValue() {
140: for (int j = 0; j < 128; j++) {
141: assertEquals(bf_multi.getValue(bf_multi.setValue(0, j)), j);
142: assertEquals(bf_multi.setValue(0, j), j << 7);
143: }
144: for (int j = 0; j < 128; j++) {
145: assertEquals(bf_zero.getValue(bf_zero.setValue(0, j)), 0);
146: assertEquals(bf_zero.setValue(0, j), 0);
147: }
148:
149: // verify that excess bits are stripped off
150: assertEquals(bf_multi.setValue(0x3f80, 128), 0);
151: for (int j = 0; j < 2; j++) {
152: assertEquals(bf_single.getValue(bf_single.setValue(0, j)),
153: j);
154: assertEquals(bf_single.setValue(0, j), j << 14);
155: }
156:
157: // verify that excess bits are stripped off
158: assertEquals(bf_single.setValue(0x4000, 2), 0);
159: }
160:
161: /**
162: * test the setShortValue() method
163: */
164: public void testSetShortValue() {
165: for (int j = 0; j < 128; j++) {
166: assertEquals(bf_multi.getShortValue(bf_multi.setShortValue(
167: (short) 0, (short) j)), (short) j);
168: assertEquals(bf_multi.setShortValue((short) 0, (short) j),
169: (short) (j << 7));
170: }
171: for (int j = 0; j < 128; j++) {
172: assertEquals(bf_zero.getShortValue(bf_zero.setShortValue(
173: (short) 0, (short) j)), (short) 0);
174: assertEquals(bf_zero.setShortValue((short) 0, (short) j),
175: (short) (0));
176: }
177:
178: // verify that excess bits are stripped off
179: assertEquals(bf_multi
180: .setShortValue((short) 0x3f80, (short) 128), (short) 0);
181: for (int j = 0; j < 2; j++) {
182: assertEquals(bf_single.getShortValue(bf_single
183: .setShortValue((short) 0, (short) j)), (short) j);
184: assertEquals(bf_single.setShortValue((short) 0, (short) j),
185: (short) (j << 14));
186: }
187:
188: // verify that excess bits are stripped off
189: assertEquals(
190: bf_single.setShortValue((short) 0x4000, (short) 2),
191: (short) 0);
192: }
193:
194: public void testByte() {
195: assertEquals(0, new BitField(0).setByteBoolean((byte) 0, true));
196: assertEquals(1, new BitField(1).setByteBoolean((byte) 0, true));
197: assertEquals(2, new BitField(2).setByteBoolean((byte) 0, true));
198: assertEquals(4, new BitField(4).setByteBoolean((byte) 0, true));
199: assertEquals(8, new BitField(8).setByteBoolean((byte) 0, true));
200: assertEquals(16, new BitField(16)
201: .setByteBoolean((byte) 0, true));
202: assertEquals(32, new BitField(32)
203: .setByteBoolean((byte) 0, true));
204: assertEquals(64, new BitField(64)
205: .setByteBoolean((byte) 0, true));
206: assertEquals(-128, new BitField(128).setByteBoolean((byte) 0,
207: true));
208: assertEquals(1, new BitField(0).setByteBoolean((byte) 1, false));
209: assertEquals(0, new BitField(1).setByteBoolean((byte) 1, false));
210: assertEquals(0, new BitField(2).setByteBoolean((byte) 2, false));
211: assertEquals(0, new BitField(4).setByteBoolean((byte) 4, false));
212: assertEquals(0, new BitField(8).setByteBoolean((byte) 8, false));
213: assertEquals(0, new BitField(16).setByteBoolean((byte) 16,
214: false));
215: assertEquals(0, new BitField(32).setByteBoolean((byte) 32,
216: false));
217: assertEquals(0, new BitField(64).setByteBoolean((byte) 64,
218: false));
219: assertEquals(0, new BitField(128).setByteBoolean((byte) 128,
220: false));
221: assertEquals(-2, new BitField(1).setByteBoolean((byte) 255,
222: false));
223: byte clearedBit = new BitField(0x40).setByteBoolean((byte) -63,
224: false);
225:
226: assertEquals(false, new BitField(0x40).isSet(clearedBit));
227: }
228:
229: /**
230: * test the clear() method
231: */
232: public void testClear() {
233: assertEquals(bf_multi.clear(-1), 0xFFFFC07F);
234: assertEquals(bf_single.clear(-1), 0xFFFFBFFF);
235: assertEquals(bf_zero.clear(-1), 0xFFFFFFFF);
236: }
237:
238: /**
239: * test the clearShort() method
240: */
241: public void testClearShort() {
242: assertEquals(bf_multi.clearShort((short) -1), (short) 0xC07F);
243: assertEquals(bf_single.clearShort((short) -1), (short) 0xBFFF);
244: assertEquals(bf_zero.clearShort((short) -1), (short) 0xFFFF);
245: }
246:
247: /**
248: * test the set() method
249: */
250: public void testSet() {
251: assertEquals(bf_multi.set(0), 0x3F80);
252: assertEquals(bf_single.set(0), 0x4000);
253: assertEquals(bf_zero.set(0), 0);
254: }
255:
256: /**
257: * test the setShort() method
258: */
259: public void testSetShort() {
260: assertEquals(bf_multi.setShort((short) 0), (short) 0x3F80);
261: assertEquals(bf_single.setShort((short) 0), (short) 0x4000);
262: assertEquals(bf_zero.setShort((short) 0), (short) 0);
263: }
264:
265: /**
266: * test the setBoolean() method
267: */
268: public void testSetBoolean() {
269: assertEquals(bf_multi.set(0), bf_multi.setBoolean(0, true));
270: assertEquals(bf_single.set(0), bf_single.setBoolean(0, true));
271: assertEquals(bf_zero.set(0), bf_zero.setBoolean(0, true));
272: assertEquals(bf_multi.clear(-1), bf_multi.setBoolean(-1, false));
273: assertEquals(bf_single.clear(-1), bf_single.setBoolean(-1,
274: false));
275: assertEquals(bf_zero.clear(-1), bf_zero.setBoolean(-1, false));
276: }
277:
278: /**
279: * test the setShortBoolean() method
280: */
281: public void testSetShortBoolean() {
282: assertEquals(bf_multi.setShort((short) 0), bf_multi
283: .setShortBoolean((short) 0, true));
284: assertEquals(bf_single.setShort((short) 0), bf_single
285: .setShortBoolean((short) 0, true));
286: assertEquals(bf_zero.setShort((short) 0), bf_zero
287: .setShortBoolean((short) 0, true));
288: assertEquals(bf_multi.clearShort((short) -1), bf_multi
289: .setShortBoolean((short) -1, false));
290: assertEquals(bf_single.clearShort((short) -1), bf_single
291: .setShortBoolean((short) -1, false));
292: assertEquals(bf_zero.clearShort((short) -1), bf_zero
293: .setShortBoolean((short) -1, false));
294: }
295:
296: }
|