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 TestLongField 32 extends TestCase 33 { 34 35 40 41 public TestLongField(String name) 42 { 43 super(name); 44 } 45 46 static private final long[] _test_array = 47 { 48 Long.MIN_VALUE, -1L, 0L, 1L, Long.MAX_VALUE 49 }; 50 51 54 55 public void testConstructors() 56 { 57 try 58 { 59 new LongField(-1); 60 fail("Should have caught ArrayIndexOutOfBoundsException"); 61 } 62 catch (ArrayIndexOutOfBoundsException ignored_e) 63 { 64 65 } 67 LongField field = new LongField(2); 68 69 assertEquals(0L, field.get()); 70 try 71 { 72 new LongField(-1, 1L); 73 fail("Should have caught ArrayIndexOutOfBoundsException"); 74 } 75 catch (ArrayIndexOutOfBoundsException ignored_e) 76 { 77 78 } 80 field = new LongField(2, 0x123456789ABCDEF0L); 81 assertEquals(0x123456789ABCDEF0L, field.get()); 82 byte[] array = new byte[ 10 ]; 83 84 try 85 { 86 new LongField(-1, 1L, array); 87 fail("Should have caught ArrayIndexOutOfBoundsException"); 88 } 89 catch (ArrayIndexOutOfBoundsException ignored_e) 90 { 91 92 } 94 field = new LongField(2, 0x123456789ABCDEF0L, array); 95 assertEquals(0x123456789ABCDEF0L, field.get()); 96 assertEquals(( byte ) 0xF0, array[ 2 ]); 97 assertEquals(( byte ) 0xDE, array[ 3 ]); 98 assertEquals(( byte ) 0xBC, array[ 4 ]); 99 assertEquals(( byte ) 0x9A, array[ 5 ]); 100 assertEquals(( byte ) 0x78, array[ 6 ]); 101 assertEquals(( byte ) 0x56, array[ 7 ]); 102 assertEquals(( byte ) 0x34, array[ 8 ]); 103 assertEquals(( byte ) 0x12, array[ 9 ]); 104 array = new byte[ 9 ]; 105 try 106 { 107 new LongField(2, 5L, array); 108 fail("should have gotten ArrayIndexOutOfBoundsException"); 109 } 110 catch (ArrayIndexOutOfBoundsException ignored_e) 111 { 112 113 } 115 for (int j = 0; j < _test_array.length; j++) 116 { 117 array = new byte[ 8 ]; 118 new LongField(0, _test_array[ j ], array); 119 assertEquals(_test_array[ j ], new LongField(0, array).get()); 120 } 121 } 122 123 126 127 public void testSet() 128 { 129 LongField field = new LongField(0); 130 byte[] array = new byte[ 8 ]; 131 132 for (int j = 0; j < _test_array.length; j++) 133 { 134 field.set(_test_array[ j ]); 135 assertEquals("testing _1 " + j, _test_array[ j ], field.get()); 136 field = new LongField(0); 137 field.set(_test_array[ j ], array); 138 assertEquals("testing _2 ", _test_array[ j ], field.get()); 139 assertEquals("testing _3.0 " + _test_array[ j ], 140 ( byte ) (_test_array[ j ] % 256), array[ 0 ]); 141 assertEquals("testing _3.1 " + _test_array[ j ], 142 ( byte ) ((_test_array[ j ] >> 8) % 256), 143 array[ 1 ]); 144 assertEquals("testing _3.2 " + _test_array[ j ], 145 ( byte ) ((_test_array[ j ] >> 16) % 256), 146 array[ 2 ]); 147 assertEquals("testing _3.3 " + _test_array[ j ], 148 ( byte ) ((_test_array[ j ] >> 24) % 256), 149 array[ 3 ]); 150 assertEquals("testing _3.4 " + _test_array[ j ], 151 ( byte ) ((_test_array[ j ] >> 32) % 256), 152 array[ 4 ]); 153 assertEquals("testing _3.5 " + _test_array[ j ], 154 ( byte ) ((_test_array[ j ] >> 40) % 256), 155 array[ 5 ]); 156 assertEquals("testing _3.6 " + _test_array[ j ], 157 ( byte ) ((_test_array[ j ] >> 48) % 256), 158 array[ 6 ]); 159 assertEquals("testing _3.7 " + _test_array[ j ], 160 ( byte ) ((_test_array[ j ] >> 56) % 256), 161 array[ 7 ]); 162 } 163 } 164 165 168 169 public void testReadFromBytes() 170 { 171 LongField field = new LongField(1); 172 byte[] array = new byte[ 8 ]; 173 174 try 175 { 176 field.readFromBytes(array); 177 fail("should have caught ArrayIndexOutOfBoundsException"); 178 } 179 catch (ArrayIndexOutOfBoundsException ignored_e) 180 { 181 182 } 184 field = new LongField(0); 185 for (int j = 0; j < _test_array.length; j++) 186 { 187 array[ 0 ] = ( byte ) (_test_array[ j ] % 256); 188 array[ 1 ] = ( byte ) ((_test_array[ j ] >> 8) % 256); 189 array[ 2 ] = ( byte ) ((_test_array[ j ] >> 16) % 256); 190 array[ 3 ] = ( byte ) ((_test_array[ j ] >> 24) % 256); 191 array[ 4 ] = ( byte ) ((_test_array[ j ] >> 32) % 256); 192 array[ 5 ] = ( byte ) ((_test_array[ j ] >> 40) % 256); 193 array[ 6 ] = ( byte ) ((_test_array[ j ] >> 48) % 256); 194 array[ 7 ] = ( byte ) ((_test_array[ j ] >> 56) % 256); 195 field.readFromBytes(array); 196 assertEquals("testing " + j, _test_array[ j ], field.get()); 197 } 198 } 199 200 205 206 public void testReadFromStream() 207 throws IOException 208 { 209 LongField field = new LongField(0); 210 byte[] buffer = new byte[ _test_array.length * 8 ]; 211 212 for (int j = 0; j < _test_array.length; j++) 213 { 214 buffer[ (j * 8) + 0 ] = ( byte ) (_test_array[ j ] % 256); 215 buffer[ (j * 8) + 1 ] = ( byte ) ((_test_array[ j ] >> 8) % 256); 216 buffer[ (j * 8) + 2 ] = ( byte ) ((_test_array[ j ] >> 16) % 256); 217 buffer[ (j * 8) + 3 ] = ( byte ) ((_test_array[ j ] >> 24) % 256); 218 buffer[ (j * 8) + 4 ] = ( byte ) ((_test_array[ j ] >> 32) % 256); 219 buffer[ (j * 8) + 5 ] = ( byte ) ((_test_array[ j ] >> 40) % 256); 220 buffer[ (j * 8) + 6 ] = ( byte ) ((_test_array[ j ] >> 48) % 256); 221 buffer[ (j * 8) + 7 ] = ( byte ) ((_test_array[ j ] >> 56) % 256); 222 } 223 ByteArrayInputStream stream = new ByteArrayInputStream(buffer); 224 225 for (int j = 0; j < buffer.length / 8; j++) 226 { 227 field.readFromStream(stream); 228 assertEquals("Testing " + j, _test_array[ j ], field.get()); 229 } 230 } 231 232 235 236 public void testWriteToBytes() 237 { 238 LongField field = new LongField(0); 239 byte[] array = new byte[ 8 ]; 240 241 for (int j = 0; j < _test_array.length; j++) 242 { 243 field.set(_test_array[ j ]); 244 field.writeToBytes(array); 245 long val = (( long ) array[ 7 ]) << 56; 246 247 val &= 0xFF00000000000000L; 248 val += ((( long ) array[ 6 ]) << 48) & 0x00FF000000000000L; 249 val += ((( long ) array[ 5 ]) << 40) & 0x0000FF0000000000L; 250 val += ((( long ) array[ 4 ]) << 32) & 0x000000FF00000000L; 251 val += ((( long ) array[ 3 ]) << 24) & 0x00000000FF000000L; 252 val += ((( long ) array[ 2 ]) << 16) & 0x0000000000FF0000L; 253 val += ((( long ) array[ 1 ]) << 8) & 0x000000000000FF00L; 254 val += (array[ 0 ] & 0x00000000000000FFL); 255 assertEquals("testing ", _test_array[ j ], val); 256 } 257 } 258 259 264 265 public static void main(String [] args) 266 { 267 System.out.println("Testing util.LongField functionality"); 268 junit.textui.TestRunner.run(TestLongField.class); 269 } 270 } 271 | Popular Tags |