KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > util > TestBitField


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    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 package org.apache.poi.util;
20
21 import junit.framework.*;
22
23 /**
24  * Class to test BitField functionality
25  *
26  * @author Marc Johnson
27  * @author Glen Stampoultzis (gstamp@iprimus.com.au)
28  */

29
30 public class TestBitField
31     extends TestCase
32 {
33     private static BitField bf_multi = new BitField(0x3F80);
34     private static BitField bf_single = new BitField(0x4000);
35
36     /**
37      * Constructor TestBitField
38      *
39      * @param name
40      */

41
42     public TestBitField(String JavaDoc name)
43     {
44         super(name);
45     }
46
47     /**
48      * test the getValue() method
49      */

50
51     public void testGetValue()
52     {
53         assertEquals(bf_multi.getValue(-1), 127);
54         assertEquals(bf_multi.getValue(0), 0);
55         assertEquals(bf_single.getValue(-1), 1);
56         assertEquals(bf_single.getValue(0), 0);
57     }
58
59     /**
60      * test the getShortValue() method
61      */

62
63     public void testGetShortValue()
64     {
65         assertEquals(bf_multi.getShortValue(( short ) -1), ( short ) 127);
66         assertEquals(bf_multi.getShortValue(( short ) 0), ( short ) 0);
67         assertEquals(bf_single.getShortValue(( short ) -1), ( short ) 1);
68         assertEquals(bf_single.getShortValue(( short ) 0), ( short ) 0);
69     }
70
71     /**
72      * test the getRawValue() method
73      */

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

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

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

115
116     public void testIsAllSet()
117     {
118         for (int j = 0; j < 0x3F80; j += 0x80)
119         {
120             assertTrue(!bf_multi.isAllSet(j));
121         }
122         assertTrue(bf_multi.isAllSet(0x3F80));
123         assertTrue(!bf_single.isAllSet(0));
124         assertTrue(bf_single.isAllSet(0x4000));
125     }
126
127     /**
128      * test the setValue() method
129      */

130
131     public void testSetValue()
132     {
133         for (int j = 0; j < 128; j++)
134         {
135             assertEquals(bf_multi.getValue(bf_multi.setValue(0, j)), j);
136             assertEquals(bf_multi.setValue(0, j), j << 7);
137         }
138
139         // verify that excess bits are stripped off
140
assertEquals(bf_multi.setValue(0x3f80, 128), 0);
141         for (int j = 0; j < 2; j++)
142         {
143             assertEquals(bf_single.getValue(bf_single.setValue(0, j)), j);
144             assertEquals(bf_single.setValue(0, j), j << 14);
145         }
146
147         // verify that excess bits are stripped off
148
assertEquals(bf_single.setValue(0x4000, 2), 0);
149     }
150
151     /**
152      * test the setShortValue() method
153      */

154
155     public void testSetShortValue()
156     {
157         for (int j = 0; j < 128; j++)
158         {
159             assertEquals(bf_multi
160                 .getShortValue(bf_multi
161                     .setShortValue(( short ) 0, ( short ) j)), ( short ) j);
162             assertEquals(bf_multi.setShortValue(( short ) 0, ( short ) j),
163                          ( short ) (j << 7));
164         }
165
166         // verify that excess bits are stripped off
167
assertEquals(bf_multi.setShortValue(( short ) 0x3f80, ( short ) 128),
168                      ( short ) 0);
169         for (int j = 0; j < 2; j++)
170         {
171             assertEquals(bf_single
172                 .getShortValue(bf_single
173                     .setShortValue(( short ) 0, ( short ) j)), ( short ) j);
174             assertEquals(bf_single.setShortValue(( short ) 0, ( short ) j),
175                          ( short ) (j << 14));
176         }
177
178         // verify that excess bits are stripped off
179
assertEquals(bf_single.setShortValue(( short ) 0x4000, ( short ) 2),
180                      ( short ) 0);
181     }
182
183     public void testByte()
184     {
185         assertEquals(1, new BitField(1).setByteBoolean(( byte ) 0, true));
186         assertEquals(2, new BitField(2).setByteBoolean(( byte ) 0, true));
187         assertEquals(4, new BitField(4).setByteBoolean(( byte ) 0, true));
188         assertEquals(8, new BitField(8).setByteBoolean(( byte ) 0, true));
189         assertEquals(16, new BitField(16).setByteBoolean(( byte ) 0, true));
190         assertEquals(32, new BitField(32).setByteBoolean(( byte ) 0, true));
191         assertEquals(64, new BitField(64).setByteBoolean(( byte ) 0, true));
192         assertEquals(-128,
193                      new BitField(128).setByteBoolean(( byte ) 0, true));
194         assertEquals(0, new BitField(1).setByteBoolean(( byte ) 1, false));
195         assertEquals(0, new BitField(2).setByteBoolean(( byte ) 2, false));
196         assertEquals(0, new BitField(4).setByteBoolean(( byte ) 4, false));
197         assertEquals(0, new BitField(8).setByteBoolean(( byte ) 8, false));
198         assertEquals(0, new BitField(16).setByteBoolean(( byte ) 16, false));
199         assertEquals(0, new BitField(32).setByteBoolean(( byte ) 32, false));
200         assertEquals(0, new BitField(64).setByteBoolean(( byte ) 64, false));
201         assertEquals(0, new BitField(128).setByteBoolean(( byte ) 128,
202                                      false));
203         assertEquals(-2, new BitField(1).setByteBoolean(( byte ) 255, false));
204         byte clearedBit = new BitField(0x40).setByteBoolean(( byte ) -63,
205                                        false);
206
207         assertEquals(false, new BitField(0x40).isSet(clearedBit));
208     }
209
210     /**
211      * test the clear() method
212      */

213
214     public void testClear()
215     {
216         assertEquals(bf_multi.clear(-1), 0xFFFFC07F);
217         assertEquals(bf_single.clear(-1), 0xFFFFBFFF);
218     }
219
220     /**
221      * test the clearShort() method
222      */

223
224     public void testClearShort()
225     {
226         assertEquals(bf_multi.clearShort(( short ) -1), ( short ) 0xC07F);
227         assertEquals(bf_single.clearShort(( short ) -1), ( short ) 0xBFFF);
228     }
229
230     /**
231      * test the set() method
232      */

233
234     public void testSet()
235     {
236         assertEquals(bf_multi.set(0), 0x3F80);
237         assertEquals(bf_single.set(0), 0x4000);
238     }
239
240     /**
241      * test the setShort() method
242      */

243
244     public void testSetShort()
245     {
246         assertEquals(bf_multi.setShort(( short ) 0), ( short ) 0x3F80);
247         assertEquals(bf_single.setShort(( short ) 0), ( short ) 0x4000);
248     }
249
250     /**
251      * test the setBoolean() method
252      */

253
254     public void testSetBoolean()
255     {
256         assertEquals(bf_multi.set(0), bf_multi.setBoolean(0, true));
257         assertEquals(bf_single.set(0), bf_single.setBoolean(0, true));
258         assertEquals(bf_multi.clear(-1), bf_multi.setBoolean(-1, false));
259         assertEquals(bf_single.clear(-1), bf_single.setBoolean(-1, false));
260     }
261
262     /**
263      * test the setShortBoolean() method
264      */

265
266     public void testSetShortBoolean()
267     {
268         assertEquals(bf_multi.setShort(( short ) 0),
269                      bf_multi.setShortBoolean(( short ) 0, true));
270         assertEquals(bf_single.setShort(( short ) 0),
271                      bf_single.setShortBoolean(( short ) 0, true));
272         assertEquals(bf_multi.clearShort(( short ) -1),
273                      bf_multi.setShortBoolean(( short ) -1, false));
274         assertEquals(bf_single.clearShort(( short ) -1),
275                      bf_single.setShortBoolean(( short ) -1, false));
276     }
277
278     /**
279      * main method to run the unit tests
280      *
281      * @param ignored_args
282      */

283
284     public static void main(String JavaDoc [] ignored_args)
285     {
286         System.out.println("Testing util.BitField functionality");
287         junit.textui.TestRunner.run(TestBitField.class);
288     }
289 }
290
Popular Tags