2 /* ====================================================================
3 Licensed to the Apache Software Foundation (ASF) under one or more
4 contributor license agreements. See the NOTICE file distributed with
5 this work for additional information regarding copyright ownership.
6 The ASF licenses this file to You under the Apache License, Version 2.0
7 (the "License"); you may not use this file except in compliance with
8 the License. You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================== */
20 package org
.apache
.poi
.util
;
22 import junit
.framework
.TestCase
;
23 import org
.apache
.poi
.util
.LittleEndian
.BufferUnderrunException
;
25 import java
.io
.ByteArrayInputStream
;
26 import java
.io
.IOException
;
27 import java
.io
.InputStream
;
30 * Class to test LittleEndian functionality
32 * @author Marc Johnson
35 public class TestLittleEndian
40 * Constructor TestLittleEndian
44 public TestLittleEndian(String name
)
50 * test the getShort() method
53 public void testGetShort()
55 byte[] testdata
= new byte[ LittleEndian
.SHORT_SIZE
+ 1 ];
58 testdata
[ 1 ] = ( byte ) 0xFF;
60 short expected
[] = new short[ 2 ];
62 expected
[ 0 ] = ( short ) 0xFF01;
63 expected
[ 1 ] = 0x02FF;
64 assertEquals(expected
[ 0 ], LittleEndian
.getShort(testdata
));
65 assertEquals(expected
[ 1 ], LittleEndian
.getShort(testdata
, 1));
68 public void testGetUShort()
70 byte[] testdata
= new byte[ LittleEndian
.SHORT_SIZE
+ 1 ];
73 testdata
[ 1 ] = ( byte ) 0xFF;
76 byte[] testdata2
= new byte[ LittleEndian
.SHORT_SIZE
+ 1 ];
78 testdata2
[ 0 ] = 0x0D;
79 testdata2
[ 1 ] = ( byte )0x93;
80 testdata2
[ 2 ] = ( byte )0xFF;
82 int expected
[] = new int[ 4 ];
84 expected
[ 0 ] = 0xFF01;
85 expected
[ 1 ] = 0x02FF;
86 expected
[ 2 ] = 0x930D;
87 expected
[ 3 ] = 0xFF93;
88 assertEquals(expected
[ 0 ], LittleEndian
.getUShort(testdata
));
89 assertEquals(expected
[ 1 ], LittleEndian
.getUShort(testdata
, 1));
90 assertEquals(expected
[ 2 ], LittleEndian
.getUShort(testdata2
));
91 assertEquals(expected
[ 3 ], LittleEndian
.getUShort(testdata2
, 1));
93 byte[] testdata3
= new byte[ LittleEndian
.SHORT_SIZE
+ 1 ];
94 LittleEndian
.putShort(testdata3
, 0, ( short ) expected
[2] );
95 LittleEndian
.putShort(testdata3
, 1, ( short ) expected
[3] );
96 assertEquals(testdata3
[ 0 ], 0x0D);
97 assertEquals(testdata3
[ 1 ], (byte)0x93);
98 assertEquals(testdata3
[ 2 ], (byte)0xFF);
99 assertEquals(expected
[ 2 ], LittleEndian
.getUShort(testdata3
));
100 assertEquals(expected
[ 3 ], LittleEndian
.getUShort(testdata3
, 1));
101 //System.out.println("TD[1][0]: "+LittleEndian.getUShort(testdata)+" expecting 65281");
102 //System.out.println("TD[1][1]: "+LittleEndian.getUShort(testdata, 1)+" expecting 767");
103 //System.out.println("TD[2][0]: "+LittleEndian.getUShort(testdata2)+" expecting 37645");
104 //System.out.println("TD[2][1]: "+LittleEndian.getUShort(testdata2, 1)+" expecting 65427");
105 //System.out.println("TD[3][0]: "+LittleEndian.getUShort(testdata3)+" expecting 37645");
106 //System.out.println("TD[3][1]: "+LittleEndian.getUShort(testdata3, 1)+" expecting 65427");
110 private static final byte[] _double_array
=
112 56, 50, -113, -4, -63, -64, -13, 63, 76, -32, -42, -35, 60, -43, 3, 64
114 private static final byte[] _nan_double_array
=
116 (byte)0x00, (byte)0x00, (byte)0x3C, (byte)0x00, (byte)0x20, (byte)0x04, (byte)0xFF, (byte)0xFF
118 private static final double[] _doubles
=
120 1.23456, 2.47912, Double
.NaN
124 * test the getDouble() method
127 public void testGetDouble()
129 assertEquals(_doubles
[ 0 ], LittleEndian
.getDouble(_double_array
), 0.000001 );
130 assertEquals(_doubles
[ 1 ], LittleEndian
.getDouble( _double_array
, LittleEndian
.DOUBLE_SIZE
), 0.000001);
131 assertTrue(Double
.isNaN(LittleEndian
.getDouble(_nan_double_array
)));
133 double nan
= LittleEndian
.getDouble(_nan_double_array
);
134 byte[] data
= new byte[8];
135 LittleEndian
.putDouble(data
, nan
);
136 for ( int i
= 0; i
< data
.length
; i
++ )
139 assertEquals(data
[i
], _nan_double_array
[i
]);
144 * test the getInt() method
147 public void testGetInt()
149 byte[] testdata
= new byte[ LittleEndian
.INT_SIZE
+ 1 ];
151 testdata
[ 0 ] = 0x01;
152 testdata
[ 1 ] = ( byte ) 0xFF;
153 testdata
[ 2 ] = ( byte ) 0xFF;
154 testdata
[ 3 ] = ( byte ) 0xFF;
155 testdata
[ 4 ] = 0x02;
156 int expected
[] = new int[ 2 ];
158 expected
[ 0 ] = 0xFFFFFF01;
159 expected
[ 1 ] = 0x02FFFFFF;
160 assertEquals(expected
[ 0 ], LittleEndian
.getInt(testdata
));
161 assertEquals(expected
[ 1 ], LittleEndian
.getInt(testdata
, 1));
165 * test the getLong method
168 public void testGetLong()
170 byte[] testdata
= new byte[ LittleEndian
.LONG_SIZE
+ 1 ];
172 testdata
[ 0 ] = 0x01;
173 testdata
[ 1 ] = ( byte ) 0xFF;
174 testdata
[ 2 ] = ( byte ) 0xFF;
175 testdata
[ 3 ] = ( byte ) 0xFF;
176 testdata
[ 4 ] = ( byte ) 0xFF;
177 testdata
[ 5 ] = ( byte ) 0xFF;
178 testdata
[ 6 ] = ( byte ) 0xFF;
179 testdata
[ 7 ] = ( byte ) 0xFF;
180 testdata
[ 8 ] = 0x02;
181 long expected
[] = new long[ 2 ];
183 expected
[ 0 ] = 0xFFFFFFFFFFFFFF01L
;
184 expected
[ 1 ] = 0x02FFFFFFFFFFFFFFL
;
185 assertEquals(expected
[ 0 ], LittleEndian
.getLong(testdata
));
186 assertEquals(expected
[ 1 ], LittleEndian
.getLong(testdata
, 1));
190 * test the PutShort method
193 public void testPutShort()
195 byte[] expected
= new byte[ LittleEndian
.SHORT_SIZE
+ 1 ];
197 expected
[ 0 ] = 0x01;
198 expected
[ 1 ] = ( byte ) 0xFF;
199 expected
[ 2 ] = 0x02;
200 byte[] received
= new byte[ LittleEndian
.SHORT_SIZE
+ 1 ];
201 short testdata
[] = new short[ 2 ];
203 testdata
[ 0 ] = ( short ) 0xFF01;
204 testdata
[ 1 ] = 0x02FF;
205 LittleEndian
.putShort(received
, testdata
[ 0 ]);
206 assertTrue(ba_equivalent(received
, expected
, 0,
207 LittleEndian
.SHORT_SIZE
));
208 LittleEndian
.putShort(received
, 1, testdata
[ 1 ]);
209 assertTrue(ba_equivalent(received
, expected
, 1,
210 LittleEndian
.SHORT_SIZE
));
214 * test the putInt method
217 public void testPutInt()
219 byte[] expected
= new byte[ LittleEndian
.INT_SIZE
+ 1 ];
221 expected
[ 0 ] = 0x01;
222 expected
[ 1 ] = ( byte ) 0xFF;
223 expected
[ 2 ] = ( byte ) 0xFF;
224 expected
[ 3 ] = ( byte ) 0xFF;
225 expected
[ 4 ] = 0x02;
226 byte[] received
= new byte[ LittleEndian
.INT_SIZE
+ 1 ];
227 int testdata
[] = new int[ 2 ];
229 testdata
[ 0 ] = 0xFFFFFF01;
230 testdata
[ 1 ] = 0x02FFFFFF;
231 LittleEndian
.putInt(received
, testdata
[ 0 ]);
232 assertTrue(ba_equivalent(received
, expected
, 0,
233 LittleEndian
.INT_SIZE
));
234 LittleEndian
.putInt(received
, 1, testdata
[ 1 ]);
235 assertTrue(ba_equivalent(received
, expected
, 1,
236 LittleEndian
.INT_SIZE
));
240 * test the putDouble methods
243 public void testPutDouble()
245 byte[] received
= new byte[ LittleEndian
.DOUBLE_SIZE
+ 1 ];
247 LittleEndian
.putDouble(received
, _doubles
[ 0 ]);
248 assertTrue(ba_equivalent(received
, _double_array
, 0,
249 LittleEndian
.DOUBLE_SIZE
));
250 LittleEndian
.putDouble(received
, 1, _doubles
[ 1 ]);
251 byte[] expected
= new byte[ LittleEndian
.DOUBLE_SIZE
+ 1 ];
253 System
.arraycopy(_double_array
, LittleEndian
.DOUBLE_SIZE
, expected
,
254 1, LittleEndian
.DOUBLE_SIZE
);
255 assertTrue(ba_equivalent(received
, expected
, 1,
256 LittleEndian
.DOUBLE_SIZE
));
260 * test the putLong method
263 public void testPutLong()
265 byte[] expected
= new byte[ LittleEndian
.LONG_SIZE
+ 1 ];
267 expected
[ 0 ] = 0x01;
268 expected
[ 1 ] = ( byte ) 0xFF;
269 expected
[ 2 ] = ( byte ) 0xFF;
270 expected
[ 3 ] = ( byte ) 0xFF;
271 expected
[ 4 ] = ( byte ) 0xFF;
272 expected
[ 5 ] = ( byte ) 0xFF;
273 expected
[ 6 ] = ( byte ) 0xFF;
274 expected
[ 7 ] = ( byte ) 0xFF;
275 expected
[ 8 ] = 0x02;
276 byte[] received
= new byte[ LittleEndian
.LONG_SIZE
+ 1 ];
277 long testdata
[] = new long[ 2 ];
279 testdata
[ 0 ] = 0xFFFFFFFFFFFFFF01L
;
280 testdata
[ 1 ] = 0x02FFFFFFFFFFFFFFL
;
281 LittleEndian
.putLong(received
, testdata
[ 0 ]);
282 assertTrue(ba_equivalent(received
, expected
, 0,
283 LittleEndian
.LONG_SIZE
));
284 LittleEndian
.putLong(received
, 1, testdata
[ 1 ]);
285 assertTrue(ba_equivalent(received
, expected
, 1,
286 LittleEndian
.LONG_SIZE
));
289 private static byte[] _good_array
=
291 0x01, 0x02, 0x01, 0x02, 0x01, 0x02, 0x01, 0x02, 0x01, 0x02, 0x01,
292 0x02, 0x01, 0x02, 0x01, 0x02
294 private static byte[] _bad_array
=
300 * test the readShort method
303 public void testReadShort()
306 short expected_value
= 0x0201;
307 InputStream stream
= new ByteArrayInputStream(_good_array
);
312 short value
= LittleEndian
.readShort(stream
);
318 assertEquals(value
, expected_value
);
322 _good_array
.length
/ LittleEndianConsts
.SHORT_SIZE
);
323 stream
= new ByteArrayInputStream(_bad_array
);
326 LittleEndian
.readShort(stream
);
327 fail("Should have caught BufferUnderrunException");
329 catch (BufferUnderrunException ignored
)
337 * test the readInt method
340 public void testReadInt()
343 int expected_value
= 0x02010201;
344 InputStream stream
= new ByteArrayInputStream(_good_array
);
349 int value
= LittleEndian
.readInt(stream
);
355 assertEquals(value
, expected_value
);
358 assertEquals(count
, _good_array
.length
/ LittleEndianConsts
.INT_SIZE
);
359 stream
= new ByteArrayInputStream(_bad_array
);
362 LittleEndian
.readInt(stream
);
363 fail("Should have caught BufferUnderrunException");
365 catch (BufferUnderrunException ignored
)
373 * test the readLong method
376 public void testReadLong()
379 long expected_value
= 0x0201020102010201L
;
380 InputStream stream
= new ByteArrayInputStream(_good_array
);
385 long value
= LittleEndian
.readLong(stream
);
391 assertEquals(value
, expected_value
);
395 _good_array
.length
/ LittleEndianConsts
.LONG_SIZE
);
396 stream
= new ByteArrayInputStream(_bad_array
);
399 LittleEndian
.readLong(stream
);
400 fail("Should have caught BufferUnderrunException");
402 catch (BufferUnderrunException ignored
)
410 * test the readFromStream method
413 public void testReadFromStream()
416 InputStream stream
= new ByteArrayInputStream(_good_array
);
417 byte[] value
= LittleEndian
.readFromStream(stream
,
420 assertTrue(ba_equivalent(value
, _good_array
, 0, _good_array
.length
));
421 stream
= new ByteArrayInputStream(_good_array
);
424 value
= LittleEndian
.readFromStream(stream
,
425 _good_array
.length
+ 1);
426 fail("Should have caught BufferUnderrunException");
428 catch (BufferUnderrunException ignored
)
435 public void testUnsignedByteToInt()
438 assertEquals(255, LittleEndian
.ubyteToInt((byte)255));
441 private boolean ba_equivalent(byte [] received
, byte [] expected
,
442 int offset
, int size
)
444 boolean result
= true;
446 for (int j
= offset
; j
< offset
+ size
; j
++)
448 if (received
[ j
] != expected
[ j
])
450 System
.out
.println("difference at index " + j
);
458 public void testUnsignedShort()
461 assertEquals(0xffff, LittleEndian
.getUShort(new byte[] { (byte)0xff, (byte)0xff }, 0));
465 * main method to run the unit tests
467 * @param ignored_args
470 public static void main(String
[] ignored_args
)
472 System
.out
.println("Testing util.LittleEndian functionality");
473 junit
.textui
.TestRunner
.run(TestLittleEndian
.class);