2016-02-10 14:23:14 +01:00
|
|
|
/*
|
|
|
|
|
* The MIT License (MIT)
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2015 Ziver Koc
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
* THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-03-04 17:48:59 +01:00
|
|
|
package zutil;
|
2016-01-31 15:13:07 +01:00
|
|
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
2016-04-15 17:13:36 +02:00
|
|
|
import static org.junit.Assert.assertArrayEquals;
|
2016-01-31 15:13:07 +01:00
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
|
|
2016-03-04 17:48:59 +01:00
|
|
|
|
2016-01-31 15:13:07 +01:00
|
|
|
/**
|
|
|
|
|
* Created by Ziver on 2016-01-31.
|
|
|
|
|
*/
|
|
|
|
|
public class ByteUtilTest {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
2016-02-02 17:06:06 +01:00
|
|
|
public void getShiftedBits(){
|
|
|
|
|
assertEquals(1, ByteUtil.getShiftedBits((byte)0b1000_0000, 7, 1));
|
|
|
|
|
assertEquals(1, ByteUtil.getShiftedBits((byte)0b0001_0000, 4, 1));
|
|
|
|
|
assertEquals(1, ByteUtil.getShiftedBits((byte)0b0000_0001, 0, 1));
|
2016-01-31 15:13:07 +01:00
|
|
|
|
2016-02-02 17:06:06 +01:00
|
|
|
assertEquals(3, ByteUtil.getShiftedBits((byte)0b0110_0000, 6, 2));
|
2016-01-31 15:13:07 +01:00
|
|
|
|
2016-02-02 17:06:06 +01:00
|
|
|
assertEquals((byte)0xFF, ByteUtil.getShiftedBits((byte)0b1111_1111, 7, 8));
|
2016-01-31 15:13:07 +01:00
|
|
|
}
|
2016-04-15 17:13:36 +02:00
|
|
|
|
|
|
|
|
|
2016-04-18 14:09:11 +02:00
|
|
|
@Test
|
|
|
|
|
public void getBits(){
|
|
|
|
|
assertEquals(0x01, ByteUtil.getBits((byte)0xFF, 1));
|
|
|
|
|
assertEquals(0x0F, ByteUtil.getBits((byte)0xFF, 4));
|
|
|
|
|
assertEquals((byte)0xFF, ByteUtil.getBits((byte)0xFF, 8));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-15 17:13:36 +02:00
|
|
|
@Test
|
|
|
|
|
public void getBitsArray(){
|
|
|
|
|
assertArrayEquals(new byte[]{}, ByteUtil.getBits(new byte[]{0x00}, 0));
|
|
|
|
|
assertArrayEquals(new byte[]{0x00}, ByteUtil.getBits(new byte[]{}, 1));
|
|
|
|
|
assertArrayEquals(new byte[]{0x00,0x00,0x00,0x00}, ByteUtil.getBits(new byte[]{0x00}, 32));
|
|
|
|
|
assertArrayEquals(new byte[]{0x00}, ByteUtil.getBits(new byte[]{0x00,0x10}, 1));
|
|
|
|
|
assertArrayEquals(new byte[]{0x00}, ByteUtil.getBits(new byte[]{0x00,0x10}, 8));
|
|
|
|
|
assertArrayEquals(new byte[]{0x00,0x01}, ByteUtil.getBits(new byte[]{0x00,0x01}, 9));
|
|
|
|
|
assertArrayEquals(new byte[]{0x00,0x01}, ByteUtil.getBits(new byte[]{0x00,0x11}, 9));
|
|
|
|
|
}
|
2016-04-15 17:50:58 +02:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void getReverseByteOrder(){
|
|
|
|
|
assertArrayEquals(new byte[]{}, ByteUtil.getReverseByteOrder(new byte[]{}));
|
|
|
|
|
assertArrayEquals(new byte[]{0x11}, ByteUtil.getReverseByteOrder(new byte[]{0x11}));
|
|
|
|
|
assertArrayEquals(new byte[]{0x22,0x11}, ByteUtil.getReverseByteOrder(new byte[]{0x11,0x22}));
|
|
|
|
|
assertArrayEquals(new byte[]{0x44,0x33,0x22,0x11}, ByteUtil.getReverseByteOrder(new byte[]{0x11,0x22,0x33,0x44}));
|
|
|
|
|
}
|
2016-04-26 14:24:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void toFormattedStringTest(){
|
|
|
|
|
byte[] data = new byte[1];
|
|
|
|
|
assertEquals("000 00 '. '",
|
|
|
|
|
ByteUtil.toFormattedString(data));
|
|
|
|
|
|
|
|
|
|
data[0] = 65;
|
|
|
|
|
assertEquals("000 41 'A '",
|
|
|
|
|
ByteUtil.toFormattedString(data));
|
|
|
|
|
|
|
|
|
|
byte[] data2 = new byte[8];
|
|
|
|
|
data2[4] = 65;
|
|
|
|
|
assertEquals("000 00 00 00 00 41 00 00 00 '....A...'",
|
|
|
|
|
ByteUtil.toFormattedString(data2));
|
|
|
|
|
|
|
|
|
|
byte[] data3 = new byte[32];
|
|
|
|
|
data3[4] = 65;
|
|
|
|
|
assertEquals("000 00 00 00 00 41 00 00 00 '....A...'\n"+
|
|
|
|
|
"008 00 00 00 00 00 00 00 00 '........'\n"+
|
|
|
|
|
"016 00 00 00 00 00 00 00 00 '........'\n"+
|
|
|
|
|
"024 00 00 00 00 00 00 00 00 '........'",
|
|
|
|
|
ByteUtil.toFormattedString(data3));
|
|
|
|
|
}
|
2016-01-31 15:13:07 +01:00
|
|
|
}
|