1 2 17 18 19 package org.apache.poi.util; 20 21 import junit.framework.*; 22 23 import java.io.*; 24 25 30 31 public class TestIntegerField 32 extends TestCase 33 { 34 35 40 41 public TestIntegerField(String name) 42 { 43 super(name); 44 } 45 46 static private final int[] _test_array = 47 { 48 Integer.MIN_VALUE, -1, 0, 1, Integer.MAX_VALUE 49 }; 50 51 54 55 public void testConstructors() 56 { 57 try 58 { 59 new IntegerField(-1); 60 fail("Should have caught ArrayIndexOutOfBoundsException"); 61 } 62 catch (ArrayIndexOutOfBoundsException ignored_e) 63 { 64 65 } 67 IntegerField field = new IntegerField(2); 68 69 assertEquals(0, field.get()); 70 try 71 { 72 new IntegerField(-1, 1); 73 fail("Should have caught ArrayIndexOutOfBoundsException"); 74 } 75 catch (ArrayIndexOutOfBoundsException ignored_e) 76 { 77 78 } 80 field = new IntegerField(2, 0x12345678); 81 assertEquals(0x12345678, field.get()); 82 byte[] array = new byte[ 6 ]; 83 84 try 85 { 86 new IntegerField(-1, 1, array); 87 fail("Should have caught ArrayIndexOutOfBoundsException"); 88 } 89 catch (ArrayIndexOutOfBoundsException ignored_e) 90 { 91 92 } 94 field = new IntegerField(2, 0x12345678, array); 95 assertEquals(0x12345678, field.get()); 96 assertEquals(( byte ) 0x78, array[ 2 ]); 97 assertEquals(( byte ) 0x56, array[ 3 ]); 98 assertEquals(( byte ) 0x34, array[ 4 ]); 99 assertEquals(( byte ) 0x12, array[ 5 ]); 100 array = new byte[ 5 ]; 101 try 102 { 103 new IntegerField(2, 5, array); 104 fail("should have gotten ArrayIndexOutOfBoundsException"); 105 } 106 catch (ArrayIndexOutOfBoundsException ignored_e) 107 { 108 109 } 111 for (int j = 0; j < _test_array.length; j++) 112 { 113 array = new byte[ 4 ]; 114 new IntegerField(0, _test_array[ j ], array); 115 assertEquals(_test_array[ j ], new IntegerField(0, array).get()); 116 } 117 } 118 119 122 123 public void testSet() 124 { 125 IntegerField field = new IntegerField(0); 126 byte[] array = new byte[ 4 ]; 127 128 for (int j = 0; j < _test_array.length; j++) 129 { 130 field.set(_test_array[ j ]); 131 assertEquals("testing _1 " + j, _test_array[ j ], field.get()); 132 field = new IntegerField(0); 133 field.set(_test_array[ j ], array); 134 assertEquals("testing _2 ", _test_array[ j ], field.get()); 135 assertEquals("testing _3.0 " + _test_array[ j ], 136 ( byte ) (_test_array[ j ] % 256), array[ 0 ]); 137 assertEquals("testing _3.1 " + _test_array[ j ], 138 ( byte ) ((_test_array[ j ] >> 8) % 256), 139 array[ 1 ]); 140 assertEquals("testing _3.2 " + _test_array[ j ], 141 ( byte ) ((_test_array[ j ] >> 16) % 256), 142 array[ 2 ]); 143 assertEquals("testing _3.3 " + _test_array[ j ], 144 ( byte ) ((_test_array[ j ] >> 24) % 256), 145 array[ 3 ]); 146 } 147 } 148 149 152 153 public void testReadFromBytes() 154 { 155 IntegerField field = new IntegerField(1); 156 byte[] array = new byte[ 4 ]; 157 158 try 159 { 160 field.readFromBytes(array); 161 fail("should have caught ArrayIndexOutOfBoundsException"); 162 } 163 catch (ArrayIndexOutOfBoundsException ignored_e) 164 { 165 166 } 168 field = new IntegerField(0); 169 for (int j = 0; j < _test_array.length; j++) 170 { 171 array[ 0 ] = ( byte ) (_test_array[ j ] % 256); 172 array[ 1 ] = ( byte ) ((_test_array[ j ] >> 8) % 256); 173 array[ 2 ] = ( byte ) ((_test_array[ j ] >> 16) % 256); 174 array[ 3 ] = ( byte ) ((_test_array[ j ] >> 24) % 256); 175 field.readFromBytes(array); 176 assertEquals("testing " + j, _test_array[ j ], field.get()); 177 } 178 } 179 180 185 186 public void testReadFromStream() 187 throws IOException 188 { 189 IntegerField field = new IntegerField(0); 190 byte[] buffer = new byte[ _test_array.length * 4 ]; 191 192 for (int j = 0; j < _test_array.length; j++) 193 { 194 buffer[ (j * 4) + 0 ] = ( byte ) (_test_array[ j ] % 256); 195 buffer[ (j * 4) + 1 ] = ( byte ) ((_test_array[ j ] >> 8) % 256); 196 buffer[ (j * 4) + 2 ] = ( byte ) ((_test_array[ j ] >> 16) % 256); 197 buffer[ (j * 4) + 3 ] = ( byte ) ((_test_array[ j ] >> 24) % 256); 198 } 199 ByteArrayInputStream stream = new ByteArrayInputStream(buffer); 200 201 for (int j = 0; j < buffer.length / 4; j++) 202 { 203 field.readFromStream(stream); 204 assertEquals("Testing " + j, _test_array[ j ], field.get()); 205 } 206 } 207 208 211 212 public void testWriteToBytes() 213 { 214 IntegerField field = new IntegerField(0); 215 byte[] array = new byte[ 4 ]; 216 217 for (int j = 0; j < _test_array.length; j++) 218 { 219 field.set(_test_array[ j ]); 220 field.writeToBytes(array); 221 int val = array[ 3 ] << 24; 222 223 val &= 0xFF000000; 224 val += (array[ 2 ] << 16) & 0x00FF0000; 225 val += (array[ 1 ] << 8) & 0x0000FF00; 226 val += (array[ 0 ] & 0x000000FF); 227 assertEquals("testing ", _test_array[ j ], val); 228 } 229 } 230 231 236 237 public static void main(String [] args) 238 { 239 System.out.println("Testing util.IntegerField functionality"); 240 junit.textui.TestRunner.run(TestIntegerField.class); 241 } 242 } 243 | Popular Tags |