KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > BitFieldTest


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.lang;
17
18 import junit.framework.Test;
19 import junit.framework.TestCase;
20 import junit.framework.TestSuite;
21 import junit.textui.TestRunner;
22
23 /**
24  * Class to test BitField functionality
25  *
26  * @author Scott Sanders
27  * @author Marc Johnson
28  * @author Glen Stampoultzis
29  * @version $Id: BitFieldTest.java 161244 2005-04-14 06:16:36Z ggregory $
30  */

31 public class BitFieldTest extends TestCase {
32
33     public static void main(String JavaDoc[] args) {
34         TestRunner.run(suite());
35     }
36
37     public static Test suite() {
38         TestSuite suite = new TestSuite(BitFieldTest.class);
39         suite.setName("BitField Tests");
40         return suite;
41     }
42
43     private static BitField bf_multi = new BitField(0x3F80);
44     private static BitField bf_single = new BitField(0x4000);
45
46     /**
47      * Constructor BitFieldTest
48      *
49      * @param name
50      */

51     public BitFieldTest(String JavaDoc name) {
52         super(name);
53     }
54
55     /**
56      * test the getValue() method
57      */

58     public void testGetValue() {
59         assertEquals(bf_multi.getValue(-1), 127);
60         assertEquals(bf_multi.getValue(0), 0);
61         assertEquals(bf_single.getValue(-1), 1);
62         assertEquals(bf_single.getValue(0), 0);
63     }
64
65     /**
66      * test the getShortValue() method
67      */

68     public void testGetShortValue() {
69         assertEquals(bf_multi.getShortValue((short) - 1), (short) 127);
70         assertEquals(bf_multi.getShortValue((short) 0), (short) 0);
71         assertEquals(bf_single.getShortValue((short) - 1), (short) 1);
72         assertEquals(bf_single.getShortValue((short) 0), (short) 0);
73     }
74
75     /**
76      * test the getRawValue() method
77      */

78     public void testGetRawValue() {
79         assertEquals(bf_multi.getRawValue(-1), 0x3F80);
80         assertEquals(bf_multi.getRawValue(0), 0);
81         assertEquals(bf_single.getRawValue(-1), 0x4000);
82         assertEquals(bf_single.getRawValue(0), 0);
83     }
84
85     /**
86      * test the getShortRawValue() method
87      */

88     public void testGetShortRawValue() {
89         assertEquals(bf_multi.getShortRawValue((short) - 1), (short) 0x3F80);
90         assertEquals(bf_multi.getShortRawValue((short) 0), (short) 0);
91         assertEquals(bf_single.getShortRawValue((short) - 1), (short) 0x4000);
92         assertEquals(bf_single.getShortRawValue((short) 0), (short) 0);
93     }
94
95     /**
96      * test the isSet() method
97      */

98     public void testIsSet() {
99         assertTrue(!bf_multi.isSet(0));
100         for (int j = 0x80; j <= 0x3F80; j += 0x80) {
101             assertTrue(bf_multi.isSet(j));
102         }
103         assertTrue(!bf_single.isSet(0));
104         assertTrue(bf_single.isSet(0x4000));
105     }
106
107     /**
108      * test the isAllSet() method
109      */

110     public void testIsAllSet() {
111         for (int j = 0; j < 0x3F80; j += 0x80) {
112             assertTrue(!bf_multi.isAllSet(j));
113         }
114         assertTrue(bf_multi.isAllSet(0x3F80));
115         assertTrue(!bf_single.isAllSet(0));
116         assertTrue(bf_single.isAllSet(0x4000));
117     }
118
119     /**
120      * test the setValue() method
121      */

122     public void testSetValue() {
123         for (int j = 0; j < 128; j++) {
124             assertEquals(bf_multi.getValue(bf_multi.setValue(0, j)), j);
125             assertEquals(bf_multi.setValue(0, j), j << 7);
126         }
127
128         // verify that excess bits are stripped off
129
assertEquals(bf_multi.setValue(0x3f80, 128), 0);
130         for (int j = 0; j < 2; j++) {
131             assertEquals(bf_single.getValue(bf_single.setValue(0, j)), 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     public void testSetShortValue() {
143         for (int j = 0; j < 128; j++) {
144             assertEquals(bf_multi.getShortValue(bf_multi.setShortValue((short) 0, (short) j)), (short) j);
145             assertEquals(bf_multi.setShortValue((short) 0, (short) j), (short) (j << 7));
146         }
147
148         // verify that excess bits are stripped off
149
assertEquals(bf_multi.setShortValue((short) 0x3f80, (short) 128), (short) 0);
150         for (int j = 0; j < 2; j++) {
151             assertEquals(bf_single.getShortValue(bf_single.setShortValue((short) 0, (short) j)), (short) j);
152             assertEquals(bf_single.setShortValue((short) 0, (short) j), (short) (j << 14));
153         }
154
155         // verify that excess bits are stripped off
156
assertEquals(bf_single.setShortValue((short) 0x4000, (short) 2), (short) 0);
157     }
158
159     public void testByte() {
160         assertEquals(1, new BitField(1).setByteBoolean((byte) 0, true));
161         assertEquals(2, new BitField(2).setByteBoolean((byte) 0, true));
162         assertEquals(4, new BitField(4).setByteBoolean((byte) 0, true));
163         assertEquals(8, new BitField(8).setByteBoolean((byte) 0, true));
164         assertEquals(16, new BitField(16).setByteBoolean((byte) 0, true));
165         assertEquals(32, new BitField(32).setByteBoolean((byte) 0, true));
166         assertEquals(64, new BitField(64).setByteBoolean((byte) 0, true));
167         assertEquals(-128, new BitField(128).setByteBoolean((byte) 0, true));
168         assertEquals(0, new BitField(1).setByteBoolean((byte) 1, false));
169         assertEquals(0, new BitField(2).setByteBoolean((byte) 2, false));
170         assertEquals(0, new BitField(4).setByteBoolean((byte) 4, false));
171         assertEquals(0, new BitField(8).setByteBoolean((byte) 8, false));
172         assertEquals(0, new BitField(16).setByteBoolean((byte) 16, false));
173         assertEquals(0, new BitField(32).setByteBoolean((byte) 32, false));
174         assertEquals(0, new BitField(64).setByteBoolean((byte) 64, false));
175         assertEquals(0, new BitField(128).setByteBoolean((byte) 128, false));
176         assertEquals(-2, new BitField(1).setByteBoolean((byte) 255, false));
177         byte clearedBit = new BitField(0x40).setByteBoolean((byte) - 63, false);
178
179         assertEquals(false, new BitField(0x40).isSet(clearedBit));
180     }
181
182     /**
183      * test the clear() method
184      */

185     public void testClear() {
186         assertEquals(bf_multi.clear(-1), 0xFFFFC07F);
187         assertEquals(bf_single.clear(-1), 0xFFFFBFFF);
188     }
189
190     /**
191      * test the clearShort() method
192      */

193     public void testClearShort() {
194         assertEquals(bf_multi.clearShort((short) - 1), (short) 0xC07F);
195         assertEquals(bf_single.clearShort((short) - 1), (short) 0xBFFF);
196     }
197
198     /**
199      * test the set() method
200      */

201     public void testSet() {
202         assertEquals(bf_multi.set(0), 0x3F80);
203         assertEquals(bf_single.set(0), 0x4000);
204     }
205
206     /**
207      * test the setShort() method
208      */

209     public void testSetShort() {
210         assertEquals(bf_multi.setShort((short) 0), (short) 0x3F80);
211         assertEquals(bf_single.setShort((short) 0), (short) 0x4000);
212     }
213
214     /**
215      * test the setBoolean() method
216      */

217     public void testSetBoolean() {
218         assertEquals(bf_multi.set(0), bf_multi.setBoolean(0, true));
219         assertEquals(bf_single.set(0), bf_single.setBoolean(0, true));
220         assertEquals(bf_multi.clear(-1), bf_multi.setBoolean(-1, false));
221         assertEquals(bf_single.clear(-1), bf_single.setBoolean(-1, false));
222     }
223
224     /**
225      * test the setShortBoolean() method
226      */

227     public void testSetShortBoolean() {
228         assertEquals(bf_multi.setShort((short) 0), bf_multi.setShortBoolean((short) 0, true));
229         assertEquals(bf_single.setShort((short) 0), bf_single.setShortBoolean((short) 0, true));
230         assertEquals(bf_multi.clearShort((short) - 1), bf_multi.setShortBoolean((short) - 1, false));
231         assertEquals(bf_single.clearShort((short) - 1), bf_single.setShortBoolean((short) - 1, false));
232     }
233
234 }
235
Popular Tags