KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > util > TestLittleEndian


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.util;
20
21 import org.apache.poi.util.LittleEndian.BufferUnderrunException;
22
23 import java.io.*;
24
25 import junit.framework.*;
26
27 /**
28  * Class to test LittleEndian functionality
29  *
30  * @author Marc Johnson
31  */

32
33 public class TestLittleEndian
34     extends TestCase
35 {
36
37     /**
38      * Constructor TestLittleEndian
39      *
40      * @param name
41      */

42
43     public TestLittleEndian(String JavaDoc name)
44     {
45         super(name);
46     }
47
48     /**
49      * test the getShort() method
50      */

51
52     public void testGetShort()
53     {
54         byte[] testdata = new byte[ LittleEndian.SHORT_SIZE + 1 ];
55
56         testdata[ 0 ] = 0x01;
57         testdata[ 1 ] = ( byte ) 0xFF;
58         testdata[ 2 ] = 0x02;
59         short expected[] = new short[ 2 ];
60
61         expected[ 0 ] = ( short ) 0xFF01;
62         expected[ 1 ] = 0x02FF;
63         assertEquals(expected[ 0 ], LittleEndian.getShort(testdata));
64         assertEquals(expected[ 1 ], LittleEndian.getShort(testdata, 1));
65     }
66
67     public void testGetUShort()
68     {
69         byte[] testdata = new byte[ LittleEndian.SHORT_SIZE + 1 ];
70
71         testdata[ 0 ] = 0x01;
72         testdata[ 1 ] = ( byte ) 0xFF;
73         testdata[ 2 ] = 0x02;
74
75         byte[] testdata2 = new byte[ LittleEndian.SHORT_SIZE + 1 ];
76         
77         testdata2[ 0 ] = 0x0D;
78         testdata2[ 1 ] = ( byte )0x93;
79         testdata2[ 2 ] = ( byte )0xFF;
80
81         int expected[] = new int[ 4 ];
82
83         expected[ 0 ] = 0xFF01;
84         expected[ 1 ] = 0x02FF;
85         expected[ 2 ] = 0x930D;
86         expected[ 3 ] = 0xFF93;
87         assertEquals(expected[ 0 ], LittleEndian.getUShort(testdata));
88         assertEquals(expected[ 1 ], LittleEndian.getUShort(testdata, 1));
89         assertEquals(expected[ 2 ], LittleEndian.getUShort(testdata2));
90         assertEquals(expected[ 3 ], LittleEndian.getUShort(testdata2, 1));
91
92         byte[] testdata3 = new byte[ LittleEndian.SHORT_SIZE + 1 ];
93         LittleEndian.putShort(testdata3, 0, ( short ) expected[2] );
94         LittleEndian.putShort(testdata3, 1, ( short ) expected[3] );
95         assertEquals(testdata3[ 0 ], 0x0D);
96         assertEquals(testdata3[ 1 ], (byte)0x93);
97         assertEquals(testdata3[ 2 ], (byte)0xFF);
98         assertEquals(expected[ 2 ], LittleEndian.getUShort(testdata3));
99         assertEquals(expected[ 3 ], LittleEndian.getUShort(testdata3, 1));
100         //System.out.println("TD[1][0]: "+LittleEndian.getUShort(testdata)+" expecting 65281");
101
//System.out.println("TD[1][1]: "+LittleEndian.getUShort(testdata, 1)+" expecting 767");
102
//System.out.println("TD[2][0]: "+LittleEndian.getUShort(testdata2)+" expecting 37645");
103
//System.out.println("TD[2][1]: "+LittleEndian.getUShort(testdata2, 1)+" expecting 65427");
104
//System.out.println("TD[3][0]: "+LittleEndian.getUShort(testdata3)+" expecting 37645");
105
//System.out.println("TD[3][1]: "+LittleEndian.getUShort(testdata3, 1)+" expecting 65427");
106

107     }
108
109     private static final byte[] _double_array =
110     {
111         56, 50, -113, -4, -63, -64, -13, 63, 76, -32, -42, -35, 60, -43, 3, 64
112     };
113     private static final byte[] _nan_double_array =
114     {
115         (byte)0x00, (byte)0x00, (byte)0x3C, (byte)0x00, (byte)0x20, (byte)0x04, (byte)0xFF, (byte)0xFF
116     };
117     private static final double[] _doubles =
118     {
119         1.23456, 2.47912, Double.NaN
120     };
121
122     /**
123      * test the getDouble() method
124      */

125
126     public void testGetDouble()
127     {
128         assertEquals(_doubles[ 0 ], LittleEndian.getDouble(_double_array), 0.000001 );
129         assertEquals(_doubles[ 1 ], LittleEndian.getDouble( _double_array, LittleEndian.DOUBLE_SIZE), 0.000001);
130         assertTrue(Double.isNaN(LittleEndian.getDouble(_nan_double_array)));
131
132         double nan = LittleEndian.getDouble(_nan_double_array);
133         byte[] data = new byte[8];
134         LittleEndian.putDouble(data, nan);
135         for ( int i = 0; i < data.length; i++ )
136         {
137             byte b = data[i];
138             assertEquals(data[i], _nan_double_array[i]);
139         }
140     }
141
142     /**
143      * test the getInt() method
144      */

145
146     public void testGetInt()
147     {
148         byte[] testdata = new byte[ LittleEndian.INT_SIZE + 1 ];
149
150         testdata[ 0 ] = 0x01;
151         testdata[ 1 ] = ( byte ) 0xFF;
152         testdata[ 2 ] = ( byte ) 0xFF;
153         testdata[ 3 ] = ( byte ) 0xFF;
154         testdata[ 4 ] = 0x02;
155         int expected[] = new int[ 2 ];
156
157         expected[ 0 ] = 0xFFFFFF01;
158         expected[ 1 ] = 0x02FFFFFF;
159         assertEquals(expected[ 0 ], LittleEndian.getInt(testdata));
160         assertEquals(expected[ 1 ], LittleEndian.getInt(testdata, 1));
161     }
162
163     /**
164      * test the getLong method
165      */

166
167     public void testGetLong()
168     {
169         byte[] testdata = new byte[ LittleEndian.LONG_SIZE + 1 ];
170
171         testdata[ 0 ] = 0x01;
172         testdata[ 1 ] = ( byte ) 0xFF;
173         testdata[ 2 ] = ( byte ) 0xFF;
174         testdata[ 3 ] = ( byte ) 0xFF;
175         testdata[ 4 ] = ( byte ) 0xFF;
176         testdata[ 5 ] = ( byte ) 0xFF;
177         testdata[ 6 ] = ( byte ) 0xFF;
178         testdata[ 7 ] = ( byte ) 0xFF;
179         testdata[ 8 ] = 0x02;
180         long expected[] = new long[ 2 ];
181
182         expected[ 0 ] = 0xFFFFFFFFFFFFFF01L;
183         expected[ 1 ] = 0x02FFFFFFFFFFFFFFL;
184         assertEquals(expected[ 0 ], LittleEndian.getLong(testdata));
185         assertEquals(expected[ 1 ], LittleEndian.getLong(testdata, 1));
186     }
187
188     /**
189      * test the PutShort method
190      */

191
192     public void testPutShort()
193     {
194         byte[] expected = new byte[ LittleEndian.SHORT_SIZE + 1 ];
195
196         expected[ 0 ] = 0x01;
197         expected[ 1 ] = ( byte ) 0xFF;
198         expected[ 2 ] = 0x02;
199         byte[] received = new byte[ LittleEndian.SHORT_SIZE + 1 ];
200         short testdata[] = new short[ 2 ];
201
202         testdata[ 0 ] = ( short ) 0xFF01;
203         testdata[ 1 ] = 0x02FF;
204         LittleEndian.putShort(received, testdata[ 0 ]);
205         assertTrue(ba_equivalent(received, expected, 0,
206                                  LittleEndian.SHORT_SIZE));
207         LittleEndian.putShort(received, 1, testdata[ 1 ]);
208         assertTrue(ba_equivalent(received, expected, 1,
209                                  LittleEndian.SHORT_SIZE));
210     }
211
212     /**
213      * test the putInt method
214      */

215
216     public void testPutInt()
217     {
218         byte[] expected = new byte[ LittleEndian.INT_SIZE + 1 ];
219
220         expected[ 0 ] = 0x01;
221         expected[ 1 ] = ( byte ) 0xFF;
222         expected[ 2 ] = ( byte ) 0xFF;
223         expected[ 3 ] = ( byte ) 0xFF;
224         expected[ 4 ] = 0x02;
225         byte[] received = new byte[ LittleEndian.INT_SIZE + 1 ];
226         int testdata[] = new int[ 2 ];
227
228         testdata[ 0 ] = 0xFFFFFF01;
229         testdata[ 1 ] = 0x02FFFFFF;
230         LittleEndian.putInt(received, testdata[ 0 ]);
231         assertTrue(ba_equivalent(received, expected, 0,
232                                  LittleEndian.INT_SIZE));
233         LittleEndian.putInt(received, 1, testdata[ 1 ]);
234         assertTrue(ba_equivalent(received, expected, 1,
235                                  LittleEndian.INT_SIZE));
236     }
237
238     /**
239      * test the putDouble methods
240      */

241
242     public void testPutDouble()
243     {
244         byte[] received = new byte[ LittleEndian.DOUBLE_SIZE + 1 ];
245
246         LittleEndian.putDouble(received, _doubles[ 0 ]);
247         assertTrue(ba_equivalent(received, _double_array, 0,
248                                  LittleEndian.DOUBLE_SIZE));
249         LittleEndian.putDouble(received, 1, _doubles[ 1 ]);
250         byte[] expected = new byte[ LittleEndian.DOUBLE_SIZE + 1 ];
251
252         System.arraycopy(_double_array, LittleEndian.DOUBLE_SIZE, expected,
253                          1, LittleEndian.DOUBLE_SIZE);
254         assertTrue(ba_equivalent(received, expected, 1,
255                                  LittleEndian.DOUBLE_SIZE));
256     }
257
258     /**
259      * test the putLong method
260      */

261
262     public void testPutLong()
263     {
264         byte[] expected = new byte[ LittleEndian.LONG_SIZE + 1 ];
265
266         expected[ 0 ] = 0x01;
267         expected[ 1 ] = ( byte ) 0xFF;
268         expected[ 2 ] = ( byte ) 0xFF;
269         expected[ 3 ] = ( byte ) 0xFF;
270         expected[ 4 ] = ( byte ) 0xFF;
271         expected[ 5 ] = ( byte ) 0xFF;
272         expected[ 6 ] = ( byte ) 0xFF;
273         expected[ 7 ] = ( byte ) 0xFF;
274         expected[ 8 ] = 0x02;
275         byte[] received = new byte[ LittleEndian.LONG_SIZE + 1 ];
276         long testdata[] = new long[ 2 ];
277
278         testdata[ 0 ] = 0xFFFFFFFFFFFFFF01L;
279         testdata[ 1 ] = 0x02FFFFFFFFFFFFFFL;
280         LittleEndian.putLong(received, testdata[ 0 ]);
281         assertTrue(ba_equivalent(received, expected, 0,
282                                  LittleEndian.LONG_SIZE));
283         LittleEndian.putLong(received, 1, testdata[ 1 ]);
284         assertTrue(ba_equivalent(received, expected, 1,
285                                  LittleEndian.LONG_SIZE));
286     }
287
288     private static byte[] _good_array =
289     {
290         0x01, 0x02, 0x01, 0x02, 0x01, 0x02, 0x01, 0x02, 0x01, 0x02, 0x01,
291         0x02, 0x01, 0x02, 0x01, 0x02
292     };
293     private static byte[] _bad_array =
294     {
295         0x01
296     };
297
298     /**
299      * test the readShort method
300      */

301
302     public void testReadShort()
303         throws IOException
304     {
305         short expected_value = 0x0201;
306         InputStream stream = new ByteArrayInputStream(_good_array);
307         int count = 0;
308
309         while (true)
310         {
311             short value = LittleEndian.readShort(stream);
312
313             if (value == 0)
314             {
315                 break;
316             }
317             assertEquals(value, expected_value);
318             count++;
319         }
320         assertEquals(count,
321                      _good_array.length / LittleEndianConsts.SHORT_SIZE);
322         stream = new ByteArrayInputStream(_bad_array);
323         try
324         {
325             LittleEndian.readShort(stream);
326             fail("Should have caught BufferUnderrunException");
327         }
328         catch (BufferUnderrunException ignored)
329         {
330
331             // as expected
332
}
333     }
334
335     /**
336      * test the readInt method
337      */

338
339     public void testReadInt()
340         throws IOException
341     {
342         int expected_value = 0x02010201;
343         InputStream stream = new ByteArrayInputStream(_good_array);
344         int count = 0;
345
346         while (true)
347         {
348             int value = LittleEndian.readInt(stream);
349
350             if (value == 0)
351             {
352                 break;
353             }
354             assertEquals(value, expected_value);
355             count++;
356         }
357         assertEquals(count, _good_array.length / LittleEndianConsts.INT_SIZE);
358         stream = new ByteArrayInputStream(_bad_array);
359         try
360         {
361             LittleEndian.readInt(stream);
362             fail("Should have caught BufferUnderrunException");
363         }
364         catch (BufferUnderrunException ignored)
365         {
366
367             // as expected
368
}
369     }
370
371     /**
372      * test the readLong method
373      */

374
375     public void testReadLong()
376         throws IOException
377     {
378         long expected_value = 0x0201020102010201L;
379         InputStream stream = new ByteArrayInputStream(_good_array);
380         int count = 0;
381
382         while (true)
383         {
384             long value = LittleEndian.readLong(stream);
385
386             if (value == 0)
387             {
388                 break;
389             }
390             assertEquals(value, expected_value);
391             count++;
392         }
393         assertEquals(count,
394                      _good_array.length / LittleEndianConsts.LONG_SIZE);
395         stream = new ByteArrayInputStream(_bad_array);
396         try
397         {
398             LittleEndian.readLong(stream);
399             fail("Should have caught BufferUnderrunException");
400         }
401         catch (BufferUnderrunException ignored)
402         {
403
404             // as expected
405
}
406     }
407
408     /**
409      * test the readFromStream method
410      */

411
412     public void testReadFromStream()
413         throws IOException
414     {
415         InputStream stream = new ByteArrayInputStream(_good_array);
416         byte[] value = LittleEndian.readFromStream(stream,
417                                  _good_array.length);
418
419         assertTrue(ba_equivalent(value, _good_array, 0, _good_array.length));
420         stream = new ByteArrayInputStream(_good_array);
421         try
422         {
423             value = LittleEndian.readFromStream(stream,
424                                                 _good_array.length + 1);
425             fail("Should have caught BufferUnderrunException");
426         }
427         catch (BufferUnderrunException ignored)
428         {
429
430             // as expected
431
}
432     }
433
434     public void testUnsignedByteToInt()
435             throws Exception JavaDoc
436     {
437         assertEquals(255, LittleEndian.ubyteToInt((byte)255));
438     }
439
440     private boolean ba_equivalent(byte [] received, byte [] expected,
441                                   int offset, int size)
442     {
443         boolean result = true;
444
445         for (int j = offset; j < offset + size; j++)
446         {
447             if (received[ j ] != expected[ j ])
448             {
449                 System.out.println("difference at index " + j);
450                 result = false;
451                 break;
452             }
453         }
454         return result;
455     }
456
457     public void testUnsignedShort()
458             throws Exception JavaDoc
459     {
460         assertEquals(0xffff, LittleEndian.getUShort(new byte[] { (byte)0xff, (byte)0xff }, 0));
461     }
462
463     /**
464      * main method to run the unit tests
465      *
466      * @param ignored_args
467      */

468
469     public static void main(String JavaDoc [] ignored_args)
470     {
471         System.out.println("Testing util.LittleEndian functionality");
472         junit.textui.TestRunner.run(TestLittleEndian.class);
473     }
474 }
475
Popular Tags