📄 testlittleendian.java
字号:
/* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and * "Apache POI" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * "Apache POI", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */package org.apache.poi.util;import org.apache.poi.util.LittleEndian.BufferUnderrunException;import java.io.*;import junit.framework.*;/** * Class to test LittleEndian functionality * * @author Marc Johnson */public class TestLittleEndian extends TestCase{ /** * Constructor TestLittleEndian * * @param name */ public TestLittleEndian(String name) { super(name); } /** * test the getShort() method */ public void testGetShort() { byte[] testdata = new byte[ LittleEndian.SHORT_SIZE + 1 ]; testdata[ 0 ] = 0x01; testdata[ 1 ] = ( byte ) 0xFF; testdata[ 2 ] = 0x02; short expected[] = new short[ 2 ]; expected[ 0 ] = ( short ) 0xFF01; expected[ 1 ] = 0x02FF; assertEquals(expected[ 0 ], LittleEndian.getShort(testdata)); assertEquals(expected[ 1 ], LittleEndian.getShort(testdata, 1)); } public void testGetUShort() { byte[] testdata = new byte[ LittleEndian.SHORT_SIZE + 1 ]; testdata[ 0 ] = 0x01; testdata[ 1 ] = ( byte ) 0xFF; testdata[ 2 ] = 0x02; byte[] testdata2 = new byte[ LittleEndian.SHORT_SIZE + 1 ]; testdata2[ 0 ] = 0x0D; testdata2[ 1 ] = ( byte )0x93; testdata2[ 2 ] = ( byte )0xFF; int expected[] = new int[ 4 ]; expected[ 0 ] = 0xFF01; expected[ 1 ] = 0x02FF; expected[ 2 ] = 0x930D; expected[ 3 ] = 0xFF93; assertEquals(expected[ 0 ], LittleEndian.getUShort(testdata)); assertEquals(expected[ 1 ], LittleEndian.getUShort(testdata, 1)); assertEquals(expected[ 2 ], LittleEndian.getUShort(testdata2)); assertEquals(expected[ 3 ], LittleEndian.getUShort(testdata2, 1)); byte[] testdata3 = new byte[ LittleEndian.SHORT_SIZE + 1 ]; LittleEndian.putShort(testdata3, 0, ( short ) expected[2] ); LittleEndian.putShort(testdata3, 1, ( short ) expected[3] ); assertEquals(testdata3[ 0 ], 0x0D); assertEquals(testdata3[ 1 ], (byte)0x93); assertEquals(testdata3[ 2 ], (byte)0xFF); assertEquals(expected[ 2 ], LittleEndian.getUShort(testdata3)); assertEquals(expected[ 3 ], LittleEndian.getUShort(testdata3, 1)); //System.out.println("TD[1][0]: "+LittleEndian.getUShort(testdata)+" expecting 65281"); //System.out.println("TD[1][1]: "+LittleEndian.getUShort(testdata, 1)+" expecting 767"); //System.out.println("TD[2][0]: "+LittleEndian.getUShort(testdata2)+" expecting 37645"); //System.out.println("TD[2][1]: "+LittleEndian.getUShort(testdata2, 1)+" expecting 65427"); //System.out.println("TD[3][0]: "+LittleEndian.getUShort(testdata3)+" expecting 37645"); //System.out.println("TD[3][1]: "+LittleEndian.getUShort(testdata3, 1)+" expecting 65427"); } private static final byte[] _double_array = { 56, 50, -113, -4, -63, -64, -13, 63, 76, -32, -42, -35, 60, -43, 3, 64 }; private static final byte[] _nan_double_array = { (byte)0x00, (byte)0x00, (byte)0x3C, (byte)0x00, (byte)0x20, (byte)0x04, (byte)0xFF, (byte)0xFF }; private static final double[] _doubles = { 1.23456, 2.47912, Double.NaN }; /** * test the getDouble() method */ public void testGetDouble() { assertEquals(_doubles[ 0 ], LittleEndian.getDouble(_double_array), 0.000001 ); assertEquals(_doubles[ 1 ], LittleEndian.getDouble( _double_array, LittleEndian.DOUBLE_SIZE), 0.000001); assertTrue(Double.isNaN(LittleEndian.getDouble(_nan_double_array))); double nan = LittleEndian.getDouble(_nan_double_array); byte[] data = new byte[8]; LittleEndian.putDouble(data, nan); for ( int i = 0; i < data.length; i++ ) { byte b = data[i]; assertEquals(data[i], _nan_double_array[i]); } } /** * test the getInt() method */ public void testGetInt() { byte[] testdata = new byte[ LittleEndian.INT_SIZE + 1 ]; testdata[ 0 ] = 0x01; testdata[ 1 ] = ( byte ) 0xFF; testdata[ 2 ] = ( byte ) 0xFF; testdata[ 3 ] = ( byte ) 0xFF; testdata[ 4 ] = 0x02; int expected[] = new int[ 2 ]; expected[ 0 ] = 0xFFFFFF01; expected[ 1 ] = 0x02FFFFFF; assertEquals(expected[ 0 ], LittleEndian.getInt(testdata)); assertEquals(expected[ 1 ], LittleEndian.getInt(testdata, 1)); } /** * test the getLong method */ public void testGetLong() { byte[] testdata = new byte[ LittleEndian.LONG_SIZE + 1 ]; testdata[ 0 ] = 0x01; testdata[ 1 ] = ( byte ) 0xFF; testdata[ 2 ] = ( byte ) 0xFF; testdata[ 3 ] = ( byte ) 0xFF; testdata[ 4 ] = ( byte ) 0xFF; testdata[ 5 ] = ( byte ) 0xFF; testdata[ 6 ] = ( byte ) 0xFF; testdata[ 7 ] = ( byte ) 0xFF; testdata[ 8 ] = 0x02; long expected[] = new long[ 2 ]; expected[ 0 ] = 0xFFFFFFFFFFFFFF01L; expected[ 1 ] = 0x02FFFFFFFFFFFFFFL; assertEquals(expected[ 0 ], LittleEndian.getLong(testdata)); assertEquals(expected[ 1 ], LittleEndian.getLong(testdata, 1)); } /** * test the PutShort method */ public void testPutShort() { byte[] expected = new byte[ LittleEndian.SHORT_SIZE + 1 ]; expected[ 0 ] = 0x01; expected[ 1 ] = ( byte ) 0xFF; expected[ 2 ] = 0x02; byte[] received = new byte[ LittleEndian.SHORT_SIZE + 1 ]; short testdata[] = new short[ 2 ]; testdata[ 0 ] = ( short ) 0xFF01; testdata[ 1 ] = 0x02FF; LittleEndian.putShort(received, testdata[ 0 ]); assertTrue(ba_equivalent(received, expected, 0, LittleEndian.SHORT_SIZE)); LittleEndian.putShort(received, 1, testdata[ 1 ]); assertTrue(ba_equivalent(received, expected, 1, LittleEndian.SHORT_SIZE)); } /** * test the putInt method */ public void testPutInt() { byte[] expected = new byte[ LittleEndian.INT_SIZE + 1 ];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -