1 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 31 public class BitFieldTest extends TestCase { 32 33 public static void main(String [] 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 51 public BitFieldTest(String name) { 52 super(name); 53 } 54 55 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 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 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 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 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 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 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 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 assertEquals(bf_single.setValue(0x4000, 2), 0); 137 } 138 139 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 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 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 185 public void testClear() { 186 assertEquals(bf_multi.clear(-1), 0xFFFFC07F); 187 assertEquals(bf_single.clear(-1), 0xFFFFBFFF); 188 } 189 190 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 201 public void testSet() { 202 assertEquals(bf_multi.set(0), 0x3F80); 203 assertEquals(bf_single.set(0), 0x4000); 204 } 205 206 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 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 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 |