⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testlittleendian.java

📁 Office格式转换代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        expected[ 0 ] = 0x01;        expected[ 1 ] = ( byte ) 0xFF;        expected[ 2 ] = ( byte ) 0xFF;        expected[ 3 ] = ( byte ) 0xFF;        expected[ 4 ] = 0x02;        byte[] received   = new byte[ LittleEndian.INT_SIZE + 1 ];        int    testdata[] = new int[ 2 ];        testdata[ 0 ] = 0xFFFFFF01;        testdata[ 1 ] = 0x02FFFFFF;        LittleEndian.putInt(received, testdata[ 0 ]);        assertTrue(ba_equivalent(received, expected, 0,                                 LittleEndian.INT_SIZE));        LittleEndian.putInt(received, 1, testdata[ 1 ]);        assertTrue(ba_equivalent(received, expected, 1,                                 LittleEndian.INT_SIZE));    }    /**     * test the putDouble methods     */    public void testPutDouble()    {        byte[] received = new byte[ LittleEndian.DOUBLE_SIZE + 1 ];        LittleEndian.putDouble(received, _doubles[ 0 ]);        assertTrue(ba_equivalent(received, _double_array, 0,                                 LittleEndian.DOUBLE_SIZE));        LittleEndian.putDouble(received, 1, _doubles[ 1 ]);        byte[] expected = new byte[ LittleEndian.DOUBLE_SIZE + 1 ];        System.arraycopy(_double_array, LittleEndian.DOUBLE_SIZE, expected,                         1, LittleEndian.DOUBLE_SIZE);        assertTrue(ba_equivalent(received, expected, 1,                                 LittleEndian.DOUBLE_SIZE));    }    /**     * test the putLong method     */    public void testPutLong()    {        byte[] expected = new byte[ LittleEndian.LONG_SIZE + 1 ];        expected[ 0 ] = 0x01;        expected[ 1 ] = ( byte ) 0xFF;        expected[ 2 ] = ( byte ) 0xFF;        expected[ 3 ] = ( byte ) 0xFF;        expected[ 4 ] = ( byte ) 0xFF;        expected[ 5 ] = ( byte ) 0xFF;        expected[ 6 ] = ( byte ) 0xFF;        expected[ 7 ] = ( byte ) 0xFF;        expected[ 8 ] = 0x02;        byte[] received   = new byte[ LittleEndian.LONG_SIZE + 1 ];        long   testdata[] = new long[ 2 ];        testdata[ 0 ] = 0xFFFFFFFFFFFFFF01L;        testdata[ 1 ] = 0x02FFFFFFFFFFFFFFL;        LittleEndian.putLong(received, testdata[ 0 ]);        assertTrue(ba_equivalent(received, expected, 0,                                 LittleEndian.LONG_SIZE));        LittleEndian.putLong(received, 1, testdata[ 1 ]);        assertTrue(ba_equivalent(received, expected, 1,                                 LittleEndian.LONG_SIZE));    }    private static byte[] _good_array =    {        0x01, 0x02, 0x01, 0x02, 0x01, 0x02, 0x01, 0x02, 0x01, 0x02, 0x01,        0x02, 0x01, 0x02, 0x01, 0x02    };    private static byte[] _bad_array  =    {        0x01    };    /**     * test the readShort method     */    public void testReadShort()        throws IOException    {        short       expected_value = 0x0201;        InputStream stream         = new ByteArrayInputStream(_good_array);        int         count          = 0;        while (true)        {            short value = LittleEndian.readShort(stream);            if (value == 0)            {                break;            }            assertEquals(value, expected_value);            count++;        }        assertEquals(count,                     _good_array.length / LittleEndianConsts.SHORT_SIZE);        stream = new ByteArrayInputStream(_bad_array);        try        {            LittleEndian.readShort(stream);            fail("Should have caught BufferUnderrunException");        }        catch (BufferUnderrunException ignored)        {            // as expected        }    }    /**     * test the readInt method     */    public void testReadInt()        throws IOException    {        int         expected_value = 0x02010201;        InputStream stream         = new ByteArrayInputStream(_good_array);        int         count          = 0;        while (true)        {            int value = LittleEndian.readInt(stream);            if (value == 0)            {                break;            }            assertEquals(value, expected_value);            count++;        }        assertEquals(count, _good_array.length / LittleEndianConsts.INT_SIZE);        stream = new ByteArrayInputStream(_bad_array);        try        {            LittleEndian.readInt(stream);            fail("Should have caught BufferUnderrunException");        }        catch (BufferUnderrunException ignored)        {            // as expected        }    }    /**     * test the readLong method     */    public void testReadLong()        throws IOException    {        long        expected_value = 0x0201020102010201L;        InputStream stream         = new ByteArrayInputStream(_good_array);        int         count          = 0;        while (true)        {            long value = LittleEndian.readLong(stream);            if (value == 0)            {                break;            }            assertEquals(value, expected_value);            count++;        }        assertEquals(count,                     _good_array.length / LittleEndianConsts.LONG_SIZE);        stream = new ByteArrayInputStream(_bad_array);        try        {            LittleEndian.readLong(stream);            fail("Should have caught BufferUnderrunException");        }        catch (BufferUnderrunException ignored)        {            // as expected        }    }    /**     * test the readFromStream method     */    public void testReadFromStream()        throws IOException    {        InputStream stream = new ByteArrayInputStream(_good_array);        byte[]      value  = LittleEndian.readFromStream(stream,                                 _good_array.length);        assertTrue(ba_equivalent(value, _good_array, 0, _good_array.length));        stream = new ByteArrayInputStream(_good_array);        try        {            value = LittleEndian.readFromStream(stream,                                                _good_array.length + 1);            fail("Should have caught BufferUnderrunException");        }        catch (BufferUnderrunException ignored)        {            // as expected        }    }    public void testUnsignedByteToInt()            throws Exception    {        assertEquals(255, LittleEndian.ubyteToInt((byte)255));    }    private boolean ba_equivalent(byte [] received, byte [] expected,                                  int offset, int size)    {        boolean result = true;        for (int j = offset; j < offset + size; j++)        {            if (received[ j ] != expected[ j ])            {                System.out.println("difference at index " + j);                result = false;                break;            }        }        return result;    }    public void testUnsignedShort()            throws Exception    {        assertEquals(0xffff, LittleEndian.getUShort(new byte[] { (byte)0xff, (byte)0xff }, 0));    }    /**     * main method to run the unit tests     *     * @param ignored_args     */    public static void main(String [] ignored_args)    {        System.out.println("Testing util.LittleEndian functionality");        junit.textui.TestRunner.run(TestLittleEndian.class);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -