1 2 17 18 19 package org.apache.poi.util; 20 21 import junit.framework.*; 22 23 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 41 42 public TestBitField(String name) 43 { 44 super(name); 45 } 46 47 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 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 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 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 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 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 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 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 assertEquals(bf_single.setValue(0x4000, 2), 0); 149 } 150 151 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 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 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 213 214 public void testClear() 215 { 216 assertEquals(bf_multi.clear(-1), 0xFFFFC07F); 217 assertEquals(bf_single.clear(-1), 0xFFFFBFFF); 218 } 219 220 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 233 234 public void testSet() 235 { 236 assertEquals(bf_multi.set(0), 0x3F80); 237 assertEquals(bf_single.set(0), 0x4000); 238 } 239 240 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 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 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 283 284 public static void main(String [] ignored_args) 285 { 286 System.out.println("Testing util.BitField functionality"); 287 junit.textui.TestRunner.run(TestBitField.class); 288 } 289 } 290 | Popular Tags |